<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/SelectedUsers.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Users.inc.php" );


	class JobRecipients extends Widget {
		protected $sRecipientType = "unknown";		// unposted
		protected $aCustomRecipients = array();
		protected $aSelectedCustomRecipients = array( -1 );	// unposted
		protected $bIsCustomAvailable = false;

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "refresh_job_recipients":
				if (!isset( $this->aP["wid"] )) return parent::handleEvents();
				if ($this->aP["wid"]!=$this->getId()) return parent::handleEvents();

				$sum = new SelectedUsersModel();
				$um = new UsersModel();
				$user_ids = $sum->getAllSelectedUsers();
				$members = $um->getMembersFromUserIds( $user_ids );

				$data = array();
				foreach ($members as $member) $data[$member["member_id"]] = $member["user_name"];

				FNTH_echo_json_response( $data );
				exit();
			}
			return parent::handleEvents();
		}	

		protected function prepareRender() {
			$sum = new SelectedUsersModel();
			$um = new UsersModel();
			$user_ids = $sum->getAllSelectedUsers();
			$this->bIsCustomAvailable = (!empty( $user_ids ));
			if ($this->bIsCustomAvailable) {
				$members = $um->getMembersFromUserIds( $user_ids );
				foreach ($members as $member)
					$this->aCustomRecipients[$member["member_id"]]=$member["user_name"];
			}
			// if not posted then default value
			if ($this->aSelectedCustomRecipients === array( -1 ))
				$this->aSelectedCustomRecipients = array_keys( $this->aCustomRecipients );
			if ($this->sRecipientType=="unknown")
				$this->sRecipientType = ($this->bIsCustomAvailable) ? "custom" : "all";
		}

		protected function getRecipientTypes() {
			$retval = array();
			$retval["agencies"]=__("recipients_Agencies" );
			$retval["freelancers"]=__("recipients_Freelancers" );
			$retval["all"]=__("recipients_All" );
			$retval["custom"]=__("recipients_Custom" );
			return $retval;
		}

		protected function getCustomRecipients() {
			return $this->aCustomRecipients;
		}

		public function getValue() {
			$retval = array();
			$retval["type"]=$this->sRecipientType;
			if ($this->sRecipientType=="custom") {
				$retval["custom_recipients"]=$this->aSelectedCustomRecipients;
			}
			return $retval;
		}

		public function refreshValueFromArray( $arr = null ) {
			if (!isset( $arr )) $arr = $this->aP;
			if (isset( $arr[$this->getId()] )) $this->sRecipientType=$arr[$this->getId()];

			// clear if posted
			if (isset( $arr[$this->getId()] )) $this->aSelectedCustomRecipients=array();

			if ($this->sRecipientType=="custom") {
				// FNTH_da( $this->aP );
				$prefix = $this->getId()."_";
				foreach ($arr as $k => $v) {
					if (strrpos( $k , $prefix )!==0) continue;
					$num = (int)str_replace( $prefix , "" , $k );
					if ($num) $this->aSelectedCustomRecipients[]=$num;
				}
			}
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->prepareRender();
			fo3_add_js_langdef( "Modify_job_recipients" );
			$this->setParam( "recipient_types" , $this->getRecipientTypes() );
			$this->setParam( "bIsCustomAvailable" , $this->bIsCustomAvailable );
			$this->setParam( "selected_recipient_type" , $this->sRecipientType );
			$this->setParam( "selected_custom_recipients" , $this->aSelectedCustomRecipients );
			$this->setParam( "custom_recipients" , $this->getCustomRecipients() );
			FNTHPage::addUniquePageFullInit( sprintf( "InitJobRecipients('%s');\n",$this->getId() ) );
			return parent::render();
		}
	}

?>
