| 1 | <?php
|
| 2 |
|
| 3 | /**
|
| 4 | * ###############################################
|
| 5 | *
|
| 6 | * SWIFT Framework
|
| 7 | * _______________________________________________
|
| 8 | *
|
| 9 | * @author Varun Shoor
|
| 10 | *
|
| 11 | * @package SWIFT
|
| 12 | * @copyright Copyright (c) 2001-2012, Kayako
|
| 13 | * @license http://www.kayako.com/license
|
| 14 | * @link http://www.kayako.com
|
| 15 | *
|
| 16 | * ###############################################
|
| 17 | */
|
| 18 |
|
| 19 | /**
|
| 20 | * The TicketCustomField API Controller
|
| 21 | *
|
| 22 | * @author Varun Shoor
|
| 23 | */
|
| 24 | class Controller_TicketCustomField extends Controller_api implements SWIFT_REST_Interface
|
| 25 | {
|
| 26 |
|
| 27 | /**
|
| 28 | * Constructor
|
| 29 | *
|
| 30 | * @author Varun Shoor
|
| 31 | * @return bool "true" on Success, "false" otherwise
|
| 32 | */
|
| 33 | public function __construct()
|
| 34 | {
|
| 35 | parent::__construct();
|
| 36 |
|
| 37 | $this->Load->Library('XML:XML');
|
| 38 |
|
| 39 | $this->Load->Library('CustomField:CustomFieldManager');
|
| 40 |
|
| 41 | $this->Language->Load('staff_ticketsmain');
|
| 42 | $this->Language->Load('staff_ticketsmanage');
|
| 43 | $this->Language->Load('staff_ticketssearch');
|
| 44 |
|
| 45 | SWIFT_Ticket::LoadLanguageTable();
|
| 46 |
|
| 47 | return true;
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Destructor
|
| 52 | *
|
| 53 | * @author Varun Shoor
|
| 54 | * @return bool "true" on Success, "false" otherwise
|
| 55 | */
|
| 56 | public function __destruct()
|
| 57 | {
|
| 58 | parent::__destruct();
|
| 59 |
|
| 60 | return true;
|
| 61 | }
|
| 62 |
|
| 63 | /**
|
| 64 | * GetList
|
| 65 | *
|
| 66 | * @author Varun Shoor
|
| 67 | * @return bool "true" on Success, "false" otherwise
|
| 68 | * @throws SWIFT_Exception If the Class is not Loaded
|
| 69 | */
|
| 70 | public function GetList()
|
| 71 | {
|
| 72 | if (!$this->GetIsClassLoaded()) {
|
| 73 | throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);
|
| 74 |
|
| 75 | return false;
|
| 76 | }
|
| 77 |
|
| 78 | $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Not Implemented, Call GET /Tickets/TicketCustomField/$ticketid$ instead.');
|
| 79 |
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Get a list of custom fields for the given ticket
|
| 85 | *
|
| 86 | * Example Output: http://wiki.kayako.com/display/DEV/REST+-+TicketCustomField
|
| 87 | *
|
| 88 | * @author Varun Shoor
|
| 89 | * @param int $_ticketID The Ticket ID
|
| 90 | * @return bool "true" on Success, "false" otherwise
|
| 91 | * @throws SWIFT_Exception If the Class is not Loaded
|
| 92 | */
|
| 93 | public function Get($_ticketID)
|
| 94 | {
|
| 95 | if (!$this->GetIsClassLoaded()) {
|
| 96 | throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);
|
| 97 |
|
| 98 | return false;
|
| 99 | }
|
| 100 |
|
| 101 | $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_ticketID);
|
| 102 | if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded()) {
|
| 103 | $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_NOTFOUND, 'Ticket not Found');
|
| 104 |
|
| 105 | return false;
|
| 106 | }
|
| 107 |
|
| 108 | $_customFieldCache = $this->Cache->Get('customfieldcache');
|
| 109 | $_customFieldIDCache = $this->Cache->Get('customfieldidcache');
|
| 110 | $_customFieldMapCache = $this->Cache->Get('customfieldmapcache');
|
| 111 | $_customFieldOptionCache = $this->Cache->Get('customfieldoptioncache');
|
| 112 |
|
| 113 | $_customFieldIDList = array();
|
| 114 | if (isset($_customFieldIDCache['ticketcustomfieldidlist'])) {
|
| 115 | $_customFieldIDList = $_customFieldIDCache['ticketcustomfieldidlist'];
|
| 116 | }
|
| 117 |
|
| 118 | $_customFieldGroupTypeList = array(SWIFT_CustomFieldGroup::GROUP_STAFFTICKET, SWIFT_CustomFieldGroup::GROUP_STAFFUSERTICKET, SWIFT_CustomFieldGroup::GROUP_USERTICKET);
|
| 119 |
|
| 120 | $_rawCustomFieldValueContainer = $_customFieldValueContainer = $_customArguments = array();
|
| 121 |
|
| 122 | $this->Database->Query("SELECT * FROM " . TABLE_PREFIX . "customfieldvalues WHERE customfieldid IN (" . BuildIN($_customFieldIDList) . ") AND typeid = '" . intval($_ticketID) . "'");
|
| 123 | while ($this->Database->NextRecord()) {
|
| 124 | if (!isset($_customFieldMapCache[$this->Database->Record['customfieldid']])) {
|
| 125 | continue;
|
| 126 | }
|
| 127 |
|
| 128 | $_rawCustomFieldValueContainer[$this->Database->Record['customfieldid']] = $this->Database->Record;
|
| 129 |
|
| 130 | // If we already have data set from POST request then we continue as is
|
| 131 | if (isset($_customFieldValueContainer[$this->Database->Record['customfieldid']])) {
|
| 132 | continue;
|
| 133 | }
|
| 134 |
|
| 135 | $_fieldValue = '';
|
| 136 | if ($this->Database->Record['isencrypted'] == '1') {
|
| 137 | $_fieldValue = SWIFT_CustomFieldManager::Decrypt($this->Database->Record['fieldvalue']);
|
| 138 | } else {
|
| 139 | $_fieldValue = $this->Database->Record['fieldvalue'];
|
| 140 | }
|
| 141 |
|
| 142 | if ($this->Database->Record['isserialized'] == '1') {
|
| 143 | $_fieldValue = mb_unserialize($_fieldValue);
|
| 144 | }
|
| 145 |
|
| 146 | $_customField = $_customFieldMapCache[$this->Database->Record['customfieldid']];
|
| 147 |
|
| 148 | if (_is_array($_fieldValue) && ($_customField['fieldtype'] == SWIFT_CustomField::TYPE_CHECKBOX || $_customField['fieldtype'] == SWIFT_CustomField::TYPE_SELECTMULTIPLE)) {
|
| 149 | foreach ($_fieldValue as $_key => $_val) {
|
| 150 | if (isset($_customFieldOptionCache[$_val])) {
|
| 151 | $_fieldValue[$_key] = $_customFieldOptionCache[$_val];
|
| 152 | }
|
| 153 | }
|
| 154 | } else if ($_customField['fieldtype'] == SWIFT_CustomField::TYPE_RADIO || $_customField['fieldtype'] == SWIFT_CustomField::TYPE_SELECT) {
|
| 155 | if (isset($_customFieldOptionCache[$_fieldValue])) {
|
| 156 | $_fieldValue = $_customFieldOptionCache[$_fieldValue];
|
| 157 | }
|
| 158 | } else if ($_customField['fieldtype'] == SWIFT_CustomField::TYPE_SELECTLINKED) {
|
| 159 | $_fieldValueInterim = '';
|
| 160 | if (isset($_customFieldOptionCache[$_fieldValue[0]])) {
|
| 161 | $_fieldValueInterim = $_customFieldOptionCache[$_fieldValue[0]];
|
| 162 |
|
| 163 | foreach ($_fieldValue[1] as $_key => $_val) {
|
| 164 | if (isset($_customFieldOptionCache[$_val])) {
|
| 165 | $_fieldValueInterim .= ' > ' . $_customFieldOptionCache[$_val];
|
| 166 | break;
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | $_fieldValue = $_fieldValueInterim;
|
| 172 | } else if ($_customField['fieldtype'] == SWIFT_CustomField::TYPE_FILE) {
|
| 173 | $_fieldValueInterim = '';
|
| 174 |
|
| 175 | try {
|
| 176 | $_SWIFT_FileManagerObject = new SWIFT_FileManager($_fieldValue);
|
| 177 |
|
| 178 | $_fieldValueInterim = $_SWIFT_FileManagerObject->GetBase64();
|
| 179 | $_customArguments[$_customField['customfieldid']]['filename'] = $_SWIFT_FileManagerObject->GetProperty('originalfilename');
|
| 180 | } catch (SWIFT_Exception $_SWIFT_ExceptionObject) {
|
| 181 |
|
| 182 | }
|
| 183 |
|
| 184 | $_fieldValue = $_fieldValueInterim;
|
| 185 | }
|
| 186 |
|
| 187 | $_customFieldValueContainer[$this->Database->Record['customfieldid']] = $_fieldValue;
|
| 188 | }
|
| 189 |
|
| 190 | $this->XML->AddParentTag('customfields');
|
| 191 |
|
| 192 | if (_is_array($_customFieldCache)) {
|
| 193 | foreach ($_customFieldCache as $_groupType => $_customFieldGroupContainer) {
|
| 194 | if (!in_array($_groupType, $_customFieldGroupTypeList)) {
|
| 195 | continue;
|
| 196 | }
|
| 197 |
|
| 198 | foreach ($_customFieldGroupContainer as $_customFieldGroupID => $_customFieldGroup) {
|
| 199 | $this->XML->AddParentTag('group', array('id' => $_customFieldGroupID, 'title' => $_customFieldGroup['title']));
|
| 200 |
|
| 201 | foreach ($_customFieldGroup['_fields'] as $_customFieldID => $_customField) {
|
| 202 | $_customFieldValue = '';
|
| 203 |
|
| 204 | /*
|
| 205 | * BUG FIX - Varun Shoor
|
| 206 | *
|
| 207 | * SWIFT-2023 [Notice]: Undefined offset: 15 (api/class.Controller_TicketCustomField.php:279)
|
| 208 | *
|
| 209 | */
|
| 210 | if (isset($_customFieldValueContainer[$_customFieldID])) {
|
| 211 | if (_is_array($_customFieldValueContainer[$_customFieldID])) {
|
| 212 | $_customFieldValue = implode(', ', $_customFieldValueContainer[$_customFieldID]);
|
| 213 | } else {
|
| 214 | $_customFieldValue = $_customFieldValueContainer[$_customFieldID];
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | $_fieldArguments = array('id' => $_customFieldID, 'title' => $_customField['title'], 'type' => $_customField['fieldtype'], 'name' => $_customField['fieldname']);
|
| 219 |
|
| 220 | if (isset($_customArguments[$_customFieldID])) {
|
| 221 | $_fieldArguments = array_merge($_fieldArguments, $_customArguments[$_customFieldID]);
|
| 222 | }
|
| 223 |
|
| 224 | $this->XML->AddTag('field', $_customFieldValue, $_fieldArguments);
|
| 225 | }
|
| 226 |
|
| 227 | $this->XML->EndParentTag('group');
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | $this->XML->EndParentTag('customfields');
|
| 233 |
|
| 234 | $this->XML->EchoXML();
|
| 235 |
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Create/Update a list of custom fields for the given ticket
|
| 241 | *
|
| 242 | * @author Pavel Titkov
|
| 243 | * @param int $_ticketID The Ticket ID
|
| 244 | * @return bool "true" on Success, "false" otherwise
|
| 245 | * @throws SWIFT_Exception If the Class is not Loaded
|
| 246 | */
|
| 247 | public function Post($_ticketID)
|
| 248 | {
|
| 249 | if (!$this->GetIsClassLoaded()) {
|
| 250 | throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);
|
| 251 |
|
| 252 | return false;
|
| 253 | }
|
| 254 |
|
| 255 | $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_ticketID);
|
| 256 | if (!$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded()) {
|
| 257 | $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_NOTFOUND, 'Ticket not Found');
|
| 258 |
|
| 259 | return false;
|
| 260 | }
|
| 261 |
|
| 262 | $_customFieldCheckResultContainer = $this->CustomFieldManager->Check(SWIFT_CustomFieldManager::MODE_POST, SWIFT_UserInterface::MODE_EDIT, array(
|
| 263 | SWIFT_CustomFieldGroup::GROUP_STAFFTICKET,
|
| 264 | SWIFT_CustomFieldGroup::GROUP_USERTICKET,
|
| 265 | SWIFT_CustomFieldGroup::GROUP_STAFFUSERTICKET
|
| 266 | ), SWIFT_CustomFieldManager::CHECKMODE_CLIENT, $_SWIFT_TicketObject->GetProperty('departmentid'));
|
| 267 | if (!$_customFieldCheckResultContainer[0]) {
|
| 268 | $this->RESTServer->DispatchStatus(SWIFT_RESTServer::HTTP_BADREQUEST, 'Custom Field Data is Invalid: ' . implode(', ', $_customFieldCheckResultContainer[1]));
|
| 269 |
|
| 270 | return false;
|
| 271 | }
|
| 272 |
|
| 273 | // Update Custom Field Values
|
| 274 | $this->CustomFieldManager->Update(
|
| 275 | SWIFT_CustomFieldManager::MODE_POST, SWIFT_UserInterface::MODE_INSERT, array(
|
| 276 | SWIFT_CustomFieldGroup::GROUP_STAFFTICKET,
|
| 277 | SWIFT_CustomFieldGroup::GROUP_USERTICKET,
|
| 278 | SWIFT_CustomFieldGroup::GROUP_STAFFUSERTICKET
|
| 279 | ), SWIFT_CustomFieldManager::CHECKMODE_CLIENT, $_SWIFT_TicketObject->GetTicketID(), $_SWIFT_TicketObject->GetProperty('departmentid'));
|
| 280 |
|
| 281 | return $this->Get($_ticketID);
|
| 282 | }
|
| 283 |
|
| 284 | }
|
| 285 |
|
| 286 | ?> |