<?php
class Join extends CAction
{
	public $room;

	public function __construct($controller, $id)
	{

		parent::__construct($controller, $id);

		// Find room by attributes
		if(!$this -> room = RoomModel::model() -> room(
			$this -> controller -> id, $controller -> publisher -> id
		) -> find()) {
			throw new CHttpException(400, Yii::t("app", "You don't have permissions to access this session"));
		}
	}

	public function run()
	{
		$controller = $this -> getController();

		// Initialize open tok library
		$opentok = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);

		$data = array(
			'name' => $controller -> publisher -> name,
		);
		
		InformerModel::model() -> deleteAll('to_sessionid=:publisherid AND sessionid=:to', array(
			':publisherid' => $controller -> publisher -> id,
			':to' => $controller -> subscriber -> id,
		));
		
		$token = $opentok -> generateToken($this -> room -> opentok_id, RoleConstants::PUBLISHER, Yii::app() -> params['tokenTime'], json_encode($data));
		
		$controller -> render("index", array(
			"publisher" => $controller -> publisher,
			"subscriber" => $controller -> subscriber,
			"token" => $token,
			"opentokid" => $this -> room -> opentok_id,
			"roomid" => $this -> room -> id,
		));
	}
}