<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/JobRecipients.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Member.inc.php" );

	class AdminJobRecipients extends JobRecipients {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
		}

		public function setValue( $val ) {
			$this->sRecipientType = $val["type"];
			if ($val["type"]=="custom") {
				$this->aSelectedCustomRecipients = $val["custom_recipients"];
				$um = new UsersModel();
			
				$members = $um->getMembersFromMemberIds( $this->aSelectedCustomRecipients );
				foreach ($members as $member)
					$this->aCustomRecipients[$member["member_id"]]=$member["user_name"];
			}
		}

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "ajr_add_member":
				if (!isset( $this->aP["ajaxtarget"] )) return parent::handleEvents();
				if ($this->aP["ajaxtarget"]!=$this->getId()) return parent::handleEvents();
				if (!isset( $this->aP["member_id"] )) return parent::handleEvents();
				$um = new UserModel();
				$mm = new MemberModel();
				$member = $mm->getMemberFromMemberId( $this->aP["member_id"] );
				$user = $um->getUserFromUserId( $member["user_id"] );
				$data = array();
				$data["member_id"]=$this->aP["member_id"];
				$data["user_name"]=$user["user_name"];
				FNTH_echo_json_response( $data );
				fo3_exit();
			default:
				return parent::handleEvents();
			}
		}

		protected function prepareRender() {
			parent::prepareRender();
			$this->bIsCustomAvailable = true;
		}


	}
?>
