Example #1
0
 public function AjaxHelpdeskPostCreate()
 {
     $oAccount = null;
     $oUser = $this->getHelpdeskAccountFromParam($oAccount);
     /* @var $oAccount CAccount */
     $iThreadId = (int) $this->getParamValue('ThreadId', 0);
     $sSubject = trim((string) $this->getParamValue('Subject', ''));
     $sText = trim((string) $this->getParamValue('Text', ''));
     $bIsInternal = '1' === (string) $this->getParamValue('IsInternal', '0');
     $mAttachments = $this->getParamValue('Attachments', null);
     if (0 === strlen($sText) || 0 === $iThreadId && 0 === strlen($sSubject)) {
         throw new \ProjectSeven\Exceptions\ClientException(\ProjectSeven\Notifications::InvalidInputParameter);
     }
     $mResult = false;
     $bIsNew = false;
     $oThread = null;
     if (0 === $iThreadId) {
         $bIsNew = true;
         $oThread = new \CHelpdeskThread();
         $oThread->IdTenant = $oUser->IdTenant;
         $oThread->IdOwner = $oUser->IdHelpdeskUser;
         $oThread->Type = \EHelpdeskThreadType::Pending;
         $oThread->Subject = $sSubject;
         if (!$this->ApiHelpdesk()->CreateThread($oUser, $oThread)) {
             $oThread = null;
         }
     } else {
         $oThread = $this->ApiHelpdesk()->GetThreadById($oUser, $iThreadId);
     }
     if ($oThread && 0 < $oThread->IdHelpdeskThread) {
         $oPost = new \CHelpdeskPost();
         $oPost->IdTenant = $oUser->IdTenant;
         $oPost->IdOwner = $oUser->IdHelpdeskUser;
         $oPost->IdHelpdeskThread = $oThread->IdHelpdeskThread;
         $oPost->Type = $bIsInternal ? \EHelpdeskPostType::Internal : \EHelpdeskPostType::Normal;
         $oPost->SystemType = \EHelpdeskPostSystemType::None;
         $oPost->Text = $sText;
         $aResultAttachment = array();
         if (is_array($mAttachments) && 0 < count($mAttachments)) {
             foreach ($mAttachments as $sTempName => $sHash) {
                 $aDecodeData = \CApi::DecodeKeyValues($sHash);
                 if (!isset($aDecodeData['HelpdeskUserID'])) {
                     continue;
                 }
                 $rData = $this->ApiFileCache()->GetFile($oUser, $sTempName);
                 if ($rData) {
                     $iFileSize = $this->ApiFileCache()->FileSize($oUser, $sTempName);
                     $sThreadID = (string) $oThread->IdHelpdeskThread;
                     $sThreadID = str_pad($sThreadID, 2, '0', STR_PAD_LEFT);
                     $sThreadIDSubFolder = substr($sThreadID, 0, 2);
                     $sThreadFolderName = API_HELPDESK_PUBLIC_NAME . '/' . $sThreadIDSubFolder . '/' . $sThreadID;
                     $this->oApiFilestorage->CreateFolder($oUser, \EFileStorageTypeStr::Corporate, '', $sThreadFolderName);
                     $sUploadName = isset($aDecodeData['Name']) ? $aDecodeData['Name'] : $sTempName;
                     $this->oApiFilestorage->CreateFile($oUser, \EFileStorageTypeStr::Corporate, $sThreadFolderName, $sUploadName, $rData, false);
                     $oAttachment = new \CHelpdeskAttachment();
                     $oAttachment->IdHelpdeskThread = $oThread->IdHelpdeskThread;
                     $oAttachment->IdHelpdeskPost = $oPost->IdHelpdeskPost;
                     $oAttachment->IdOwner = $oUser->IdHelpdeskUser;
                     $oAttachment->IdTenant = $oUser->IdTenant;
                     $oAttachment->FileName = $sUploadName;
                     $oAttachment->SizeInBytes = $iFileSize;
                     $oAttachment->EncodeHash($oUser, $sThreadFolderName);
                     $aResultAttachment[] = $oAttachment;
                 }
             }
             if (is_array($aResultAttachment) && 0 < count($aResultAttachment)) {
                 $oPost->Attachments = $aResultAttachment;
             }
         }
         $mResult = $this->ApiHelpdesk()->CreatePost($oUser, $oThread, $oPost, $bIsNew);
         if ($mResult) {
             $mResult = $oThread->IdHelpdeskThread;
         }
     }
     return $this->DefaultResponse($oAccount, __FUNCTION__, $mResult);
 }