<?php
                    /**
                    * ###############################################
                    *
                    * SWIFT Framework
                    * _______________________________________________
                    *
                    * @package        SWIFT
                    * @copyright    Copyright (c) 2001-2014, Kayako
                    * @license        http://www.kayako.com/license
                    * @link        http://www.kayako.com
                    *
                    * ###############################################
                    */

                       class Controller_Staff extends Controller_api implements SWIFT_REST_Interface {  public function __construct() { parent::__construct(); $this->Load->Library('XML:XML'); return true; }  public function __destruct() { parent::__destruct(); return true; }  protected function ProcessStaff($_staffID = false) { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $_staffContainer = array(); if (!empty($_staffID)) { $this->Database->Query("SELECT staff.*, signatures.signature AS signature, staff.staffid AS staffid FROM " . TABLE_PREFIX . "staff AS staff
				LEFT JOIN " . TABLE_PREFIX . "signatures AS signatures ON (staff.staffid = signatures.staffid) WHERE staff.staffid = '" . intval($_staffID) . "'"); } else { $this->Database->Query("SELECT staff.*, signatures.signature AS signature, staff.staffid AS staffid FROM " . TABLE_PREFIX . "staff AS staff
				LEFT JOIN " . TABLE_PREFIX . "signatures AS signatures ON (staff.staffid = signatures.staffid) ORDER BY staff.staffid ASC"); } while ($this->Database->NextRecord()) { $_staffContainer[$this->Database->Record['staffid']] = $this->Database->Record; } $this->XML->AddParentTag('staffusers'); foreach ($_staffContainer as $_staffID => $_staff) { $this->XML->AddParentTag('staff'); $this->XML->AddTag('id', $_staffID); $this->XML->AddTag('staffgroupid', $_staff['staffgroupid']); $this->XML->AddTag('firstname', $_staff['firstname']); $this->XML->AddTag('lastname', $_staff['lastname']); $this->XML->AddTag('fullname', $_staff['fullname']); $this->XML->AddTag('username', $_staff['username']); $this->XML->AddTag('email', $_staff['email']); $this->XML->AddTag('designation', $_staff['designation']); $this->XML->AddTag('greeting', $_staff['greeting']); $this->XML->AddTag('mobilenumber', $_staff['mobilenumber']); $this->XML->AddTag('isenabled', $_staff['isenabled']); $this->XML->AddTag('timezone', $_staff['timezonephp']); $this->XML->AddTag('enabledst', $_staff['enabledst']); $this->XML->AddTag('signature', $_staff['signature']); $this->XML->EndParentTag('staff'); } $this->XML->EndParentTag('staffusers'); return true; }  public function GetList() { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $this->ProcessStaff(false); $this->XML->EchoXML(); return true; }  public function Get($_staffID) { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $this->ProcessStaff(intval($_staffID)); $this->XML->EchoXML(); return true; }  public function Post() { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $_staffGroupCache = $this->Cache->Get('staffgroupcache'); if (!isset($_POST['firstname']) || trim($_POST['firstname']) == '' || empty($_POST['firstname'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'First name is empty'); return false; } else if (!isset($_POST['lastname']) || trim($_POST['lastname']) == '' || empty($_POST['lastname'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Last name is empty'); return false; } else if (!isset($_POST['email']) || trim($_POST['email']) == '' || empty($_POST['email']) || !IsEmailValid($_POST['email'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Email address is empty or Invalid'); return false; } else if (!isset($_POST['staffgroupid']) || trim($_POST['staffgroupid']) == '' || empty($_POST['staffgroupid']) || !isset($_staffGroupCache[$_POST['staffgroupid']])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Staff Group ID is Invalid'); return false; } else if (!isset($_POST['password']) || trim($_POST['password']) == '' || empty($_POST['password'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Invalid Password Specified'); return false; } else if (!isset($_POST['username']) || trim($_POST['username']) == '' || empty($_POST['username'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Invalid Username Specified'); return false; } $_designation = ''; if (isset($_POST['designation']) && !empty($_POST['designation'])) { $_designation = $_POST['designation']; } $_mobileNumber = ''; if (isset($_POST['mobilenumber']) && !empty($_POST['mobilenumber'])) { $_mobileNumber = $_POST['mobilenumber']; } $_staffSignature = ''; if (isset($_POST['staffsignature']) && !empty($_POST['staffsignature'])) { $_staffSignature = $_POST['staffsignature']; } $_isEnabled = true; if (isset($_POST['isenabled']) && !empty($_POST['isenabled'])) { $_isEnabled = intval($_POST['isenabled']); } $_greeting = ''; if (isset($_POST['greeting']) && !empty($_POST['greeting'])) { $_greeting = $_POST['greeting']; } $_timeZone = ''; if (isset($_POST['timezone']) && !empty($_POST['timezone'])) { $_timeZone = $_POST['timezone']; } $_enableDST = true; if (isset($_POST['enabledst']) && !empty($_POST['enabledst'])) { $_enableDST = $_POST['enabledst']; } $_SWIFT_StaffObject = SWIFT_Staff::Create($_POST['firstname'], $_POST['lastname'], $_designation, $_POST['username'], $_POST['password'], $_POST['staffgroupid'], $_POST['email'], $_mobileNumber, $_staffSignature, true, $_isEnabled, $_greeting, '', $_timeZone, $_enableDST); if (!$_SWIFT_StaffObject instanceof SWIFT_Staff || !$_SWIFT_StaffObject->GetIsClassLoaded()) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Staff Creation Failed'); return false; } $this->ProcessStaff($_SWIFT_StaffObject->GetStaffID()); $this->XML->EchoXML(); return true; }  public function Put($_staffID) { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $_SWIFT_StaffObject = false; $_errorMessage = ''; try { $_SWIFT_StaffObject = new SWIFT_Staff(new SWIFT_DataID($_staffID)); } catch (SWIFT_Exception $_SWIFT_ExceptionObject) { $_errorMessage = ': ' . $_SWIFT_ExceptionObject->getMessage(); } if (!$_SWIFT_StaffObject instanceof SWIFT_Staff || !$_SWIFT_StaffObject->GetIsClassLoaded()) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Staff Object Load Failed' . $_errorMessage); return false; } $_staffGroupCache = $this->Cache->Get('staffgroupcache'); if (!isset($_POST['firstname']) || trim($_POST['firstname']) == '' || empty($_POST['firstname'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'First name is empty'); return false; } else if (!isset($_POST['lastname']) || trim($_POST['lastname']) == '' || empty($_POST['lastname'])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Last name is empty'); return false; } else if (!isset($_POST['staffgroupid']) || trim($_POST['staffgroupid']) == '' || empty($_POST['staffgroupid']) || !isset($_staffGroupCache[$_POST['staffgroupid']])) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Staff Group ID is Invalid'); return false; } $_email = $_SWIFT_StaffObject->GetProperty('email'); if (isset($_POST['email']) && !empty($_POST['email'])) { $_email = $_POST['email']; } $_staffGroupID = $_SWIFT_StaffObject->GetProperty('staffgroupid'); if (isset($_POST['staffgroupid']) && !empty($_POST['staffgroupid']) && isset($_staffGroupCache[$_POST['staffgroupid']])) { $_staffGroupID = $_POST['staffgroupid']; } $_designation = $_SWIFT_StaffObject->GetProperty('designation'); if (isset($_POST['designation']) && !empty($_POST['designation'])) { $_designation = $_POST['designation']; } $_mobileNumber = $_SWIFT_StaffObject->GetProperty('mobilenumber'); if (isset($_POST['mobilenumber']) && !empty($_POST['mobilenumber'])) { $_mobileNumber = $_POST['mobilenumber']; } $_staffSignature = $_SWIFT_StaffObject->GetProperty('signature'); if (isset($_POST['staffsignature']) && !empty($_POST['staffsignature'])) { $_staffSignature = $_POST['staffsignature']; } $_isEnabled = $_SWIFT_StaffObject->GetProperty('isenabled'); if (isset($_POST['isenabled'])) { $_isEnabled = intval($_POST['isenabled']); } $_greeting = $_SWIFT_StaffObject->GetProperty('greeting'); if (isset($_POST['greeting']) && !empty($_POST['greeting'])) { $_greeting = $_POST['greeting']; } $_timeZone = $_SWIFT_StaffObject->GetProperty('timezonephp'); if (isset($_POST['timezone']) && !empty($_POST['timezone'])) { $_timeZone = $_POST['timezone']; } $_enableDST = $_SWIFT_StaffObject->GetProperty('enabledst'); if (isset($_POST['enabledst'])) { $_enableDST = $_POST['enabledst']; } $_password = ''; if (isset($_POST['password']) && !empty($_POST['password'])) { $_password = $_POST['password']; } $_username = $_SWIFT_StaffObject->GetProperty('username'); if (isset($_POST['username']) && !empty($_POST['username'])) { $_username = $_POST['username']; } $_SWIFT_StaffObject->Update($_POST['firstname'], $_POST['lastname'], $_designation, $_username, $_password, $_staffGroupID, $_email, $_mobileNumber, $_staffSignature, true, $_isEnabled, $_greeting, '', $_timeZone, $_enableDST); $this->ProcessStaff($_SWIFT_StaffObject->GetStaffID()); $this->XML->EchoXML(); return true; }  public function Delete($_staffID) { $_SWIFT = SWIFT::GetInstance(); if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $_SWIFT_StaffObject = false; $_errorMessage = ''; try { $_SWIFT_StaffObject = new SWIFT_Staff(new SWIFT_DataID($_staffID)); } catch (SWIFT_Exception $_SWIFT_ExceptionObject) { $_errorMessage = ': ' . $_SWIFT_ExceptionObject->getMessage(); } if (!$_SWIFT_StaffObject instanceof SWIFT_Staff || !$_SWIFT_StaffObject->GetIsClassLoaded()) { $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Staff Load Failed' . $_errorMessage); return false; } SWIFT_Staff::DeleteList(array($_staffID)); return true; } } ?>