Example #1
0
 /**
  * @param CHelpdeskUser $oHelpdeskUser
  * @param CHelpdeskThread $oThread
  * @param CHelpdeskPost $oPost
  * @param bool $bIsNew = false
  * @param bool $bSendNotify = true
  *
  * @return bool
  */
 public function CreatePost(CHelpdeskUser $oHelpdeskUser, $oThread, CHelpdeskPost $oPost, $bIsNew = false, $bSendNotify = true)
 {
     $bResult = false;
     try {
         if ($oPost->Validate()) {
             if ($oPost->Type === EHelpdeskPostType::Internal && !$oHelpdeskUser->IsAgent) {
                 $oPost->Type = EHelpdeskPostType::Normal;
             }
             if ($oHelpdeskUser->IsAgent && !$bIsNew && $oHelpdeskUser->IdHelpdeskUser !== $oThread->IdOwner) {
                 if ($oPost->Type !== EHelpdeskPostType::Internal) {
                     $oThread->Type = EHelpdeskThreadType::Answered;
                 }
             } else {
                 $oThread->Type = EHelpdeskThreadType::Pending;
             }
             $bResult = $this->oStorage->CreatePost($oHelpdeskUser, $oPost);
             if (!$bResult) {
                 $this->moveStorageExceptionToManager();
                 throw new CApiManagerException(Errs::HelpdeskManager_PostCreateFailed);
             } else {
                 if (is_array($oPost->Attachments) && 0 < count($oPost->Attachments)) {
                     $this->oStorage->AddAttachments($oHelpdeskUser, $oThread, $oPost, $oPost->Attachments);
                 }
                 $oThread->Updated = time();
                 $oThread->PostCount = $this->GetPostsCount($oHelpdeskUser, $oThread);
                 $oThread->LastPostId = $oPost->IdHelpdeskPost;
                 $oThread->LastPostOwnerId = $oPost->IdOwner;
                 $oThread->Notificated = false;
                 if (!$oThread->HasAttachments) {
                     $oThread->HasAttachments = is_array($oPost->Attachments) && 0 < count($oPost->Attachments);
                 }
                 $bResult = $this->UpdateThread($oHelpdeskUser, $oThread);
                 $this->SetThreadSeen($oHelpdeskUser, $oThread);
                 if ($bSendNotify) {
                     $this->NotifyPost($oThread, $oPost, $bIsNew);
                 }
             }
         }
     } catch (CApiBaseException $oException) {
         $bResult = false;
         $this->setLastException($oException);
     }
     return $bResult;
 }