diff --git a/workflow/engine/classes/class.labelsGmail.php b/workflow/engine/classes/class.labelsGmail.php index 5c52f4bd1..a0771a6d7 100644 --- a/workflow/engine/classes/class.labelsGmail.php +++ b/workflow/engine/classes/class.labelsGmail.php @@ -1,4 +1,6 @@ setAddLabelIds($labelsToAdd); $mods->setRemoveLabelIds($labelsToRemove); @@ -43,12 +46,14 @@ class labelsGmail * Get list of Messages in user's mailbox. * * @param Google_Service_Gmail $service Authorized Gmail API instance. - * @param string $userId User's email address. The special value 'me' - * can be used to indicate the authenticated user. + * @param string $userId User's email address. The special value 'me' + * can be used to indicate the authenticated user. + * * @return array Array of Messages. */ - function listMessages($service, $userId, $query, $labels) { - $pageToken = NULL; + function listMessages($service, $userId, $query, $labels) + { + $pageToken = null; $messages = array(); $opt_param = array(); do { @@ -72,14 +77,12 @@ class labelsGmail return $messages; } - public function setLabelsToPauseCase ($caseId, $index) { - require_once PATH_TRUNK . 'vendor' . PATH_SEP . 'google' . PATH_SEP . 'apiclient' . PATH_SEP . 'src' . PATH_SEP . 'Google' . PATH_SEP . 'autoload.php'; - require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.pmGoogleApi.php"); - + public function setLabelsToPauseCase($caseId, $index) + { $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); $appData = $Pmgmail->getDraftApp($caseId, $index); - foreach ($appData as $application){ + foreach ($appData as $application) { $appNumber = $application['APP_NUMBER']; $index = $application['DEL_INDEX']; $threadUsr = $application['USR_UID']; @@ -94,43 +97,42 @@ class labelsGmail $mail = $usrData['USR_EMAIL']; //The Subject to search the email - $subject = "[PM] " .$proName. " Case: ". $appNumber; + $subject = "[PM] " . $proName . " Case: " . $appNumber; $pmGoogle = new PMGoogleApi(); $pmGoogle->setUser($mail); - $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $pmGoogle->setScope(PMGoogleApi::GMAIL_MODIFY); $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); $labelsIds = $this->getLabelsIds($service); - if($appStatus == 'DRAFT'){ + if ($appStatus == 'DRAFT') { $labelsToRemove = $labelsIds['Draft']; $labelsToSearch = "*-draft"; $labelsToAdd = $labelsIds['Paused']; } - if($appStatus == 'TO_DO'){ + if ($appStatus == 'TO_DO') { $labelsToRemove = $labelsIds['Inbox']; $labelsToSearch = "*-inbox"; $labelsToAdd = $labelsIds['Paused']; } - $q = "subject:('".preg_quote($subject, '-')."') label:('".$labelsToSearch."')"; + $q = "subject:('" . preg_quote($subject, '-') . "') label:('" . $labelsToSearch . "')"; $messageList = $this->listMessages($service, $mail, $q, $labelsToRemove); foreach ($messageList as $message) { $messageId = $message->getId(); - $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), array($labelsToRemove)); + $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), + array($labelsToRemove)); } } - function setLabelsTounpauseCase ($caseId, $index) { - require_once PATH_TRUNK . 'vendor' . PATH_SEP . 'google' . PATH_SEP . 'apiclient' . PATH_SEP . 'src' . PATH_SEP . 'Google' . PATH_SEP . 'autoload.php'; - require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.pmGoogleApi.php"); - + function setLabelsTounpauseCase($caseId, $index) + { $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); $appData = $Pmgmail->getDraftApp($caseId, $index); - foreach ($appData as $application){ + foreach ($appData as $application) { $appNumber = $application['APP_NUMBER']; $index = $application['DEL_INDEX']; $threadUsr = $application['USR_UID']; @@ -145,41 +147,43 @@ class labelsGmail $mail = $usrData['USR_EMAIL']; //The Subject to search the email - $subject = "[PM] " .$proName. " Case: ". $appNumber; + $subject = "[PM] " . $proName . " Case: " . $appNumber; $pmGoogle = new PMGoogleApi(); $pmGoogle->setUser($mail); - $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $pmGoogle->setScope(PMGoogleApi::GMAIL_MODIFY); $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); $labelsIds = $this->getLabelsIds($service); - if($appStatus == 'DRAFT'){ + if ($appStatus == 'DRAFT') { $labelsToRemove = $labelsIds['Paused']; $labelsToSearch = "*-paused"; $labelsToAdd = $labelsIds['Draft']; } - if($appStatus == 'TO_DO'){ + if ($appStatus == 'TO_DO') { $labelsToRemove = $labelsIds['Paused']; $labelsToSearch = "*-paused"; $labelsToAdd = $labelsIds['Inbox']; } - $q = "subject:('".preg_quote($subject, '-')."') label:('".$labelsToSearch."')"; + $q = "subject:('" . preg_quote($subject, '-') . "') label:('" . $labelsToSearch . "')"; $messageList = $this->listMessages($service, $mail, $q, $labelsToRemove); foreach ($messageList as $message) { $messageId = $message->getId(); - $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), array($labelsToRemove)); + $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), + array($labelsToRemove)); } } - public function setLabels($caseId, $index, $actualLastIndex, $unassigned=false){ + public function setLabels($caseId, $index, $actualLastIndex, $unassigned = false) + { //First getting the actual thread data $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); $appData = $Pmgmail->getDraftApp($caseId, $index); - foreach ($appData as $application){ + foreach ($appData as $application) { $appNumber = $application['APP_NUMBER']; $index = $application['DEL_INDEX']; $threadUsr = $application['USR_UID']; @@ -189,7 +193,7 @@ class labelsGmail $tasUid = $application['TAS_UID']; } - if($threadStatus == 'CLOSED' || $unassigned == true) { + if ($threadStatus == 'CLOSED' || $unassigned == true) { //Getting the privious User email $oUsers = new \Users(); @@ -197,51 +201,54 @@ class labelsGmail $mail = $usrData['USR_EMAIL']; //The Subject to search the email - $subject = "[PM] " .$proName. " Case: ". $appNumber; + $subject = "[PM] " . $proName . " Case: " . $appNumber; - require_once PATH_TRUNK . 'vendor' . PATH_SEP . 'google' . PATH_SEP . 'apiclient' . PATH_SEP . 'src' . PATH_SEP . 'Google' . PATH_SEP . 'autoload.php'; - require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.pmGoogleApi.php"); $pmGoogle = new PMGoogleApi(); $pmGoogle->setUser($mail); - $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $pmGoogle->setScope(PMGoogleApi::GMAIL_MODIFY); $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); $labelsIds = $this->getLabelsIds($service); - if($actualLastIndex == 0){ + if ($actualLastIndex == 0) { $labelsToRemove = $labelsIds['Draft']; $labelsToSearch = "*-draft"; $labelsToAdd = $labelsIds['Participated']; - } else if ( ($actualLastIndex == -1) && ($unassigned == true) ){ //Unassigned - $labelsToRemove = $labelsIds['Unassigned']; - $labelsToSearch = "*-unassigned"; - $labelsToAdd = $labelsIds['Inbox']; - } else if($actualLastIndex >= 1) { - $labelsToRemove = $labelsIds['Inbox']; - $labelsToSearch = "*-inbox"; - $labelsToAdd = $labelsIds['Participated']; + } else { + if (($actualLastIndex == -1) && ($unassigned == true)) { //Unassigned + $labelsToRemove = $labelsIds['Unassigned']; + $labelsToSearch = "*-unassigned"; + $labelsToAdd = $labelsIds['Inbox']; + } else { + if ($actualLastIndex >= 1) { + $labelsToRemove = $labelsIds['Inbox']; + $labelsToSearch = "*-inbox"; + $labelsToAdd = $labelsIds['Participated']; + } + } } //Searching the email in the user's mail - $q = "subject:('".preg_quote($subject, '-')."') label:('".$labelsToSearch."')"; + $q = "subject:('" . preg_quote($subject, '-') . "') label:('" . $labelsToSearch . "')"; $messageList = $this->listMessages($service, $mail, $q, $labelsToRemove); foreach ($messageList as $message) { $messageId = $message->getId(); - $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), array($labelsToRemove)); + $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), + array($labelsToRemove)); } //in is unassigned we must remove the label to the orher users if ($labelsToRemove === $labelsIds['Unassigned']) { - require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "TaskUser.php"); + require_once(PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "TaskUser.php"); $oTaskUsers = new \TaskUser(); $taskUsers = $oTaskUsers->getAllUsersTask($tasUid); - foreach ($taskUsers as $user){ + foreach ($taskUsers as $user) { $usrData = $oUsers->loadDetails($user['USR_UID']); $nextMail = $usrData['USR_EMAIL']; //this operation is just for the users that didn't make the case claim @@ -264,10 +271,10 @@ class labelsGmail $tasUid = $application['TAS_UID']; //The Subject to search the email - $subject = "[PM] " .$proName. " Case: ". $appNumber; + $subject = "[PM] " . $proName . " Case: " . $appNumber; $pmGoogle = new PMGoogleApi(); $pmGoogle->setUser($mail); - $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $pmGoogle->setScope(PMGoogleApi::GMAIL_MODIFY); $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); $labelsIds = $this->getLabelsIds($service); @@ -276,12 +283,13 @@ class labelsGmail $labelsToAdd = $labelsIds['Participated']; //Searching the email in the user's mail - $q = "subject:('".preg_quote($subject, '-')."') label:('".$labelsToSearch."')"; + $q = "subject:('" . preg_quote($subject, '-') . "') label:('" . $labelsToSearch . "')"; $messageList = $this->listMessages($service, $mail, $q, $labelsToRemove); foreach ($messageList as $message) { $messageId = $message->getId(); - $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), array($labelsToRemove)); + $modifyResult = $this->modifyMessage($service, $mail, $messageId, array($labelsToAdd), + array($labelsToRemove)); } } @@ -291,9 +299,9 @@ class labelsGmail * Delete Label with given ID. * * @param Google_Service_Gmail $service Authorized Gmail API instance. - * @param string $userId User's email address. The special value 'me' - * can be used to indicate the authenticated user. - * @param string $labelId Id of Label to be updated. + * @param string $userId User's email address. The special value 'me' + * can be used to indicate the authenticated user. + * @param string $labelId Id of Label to be updated. */ public function deleteLabel($service, $user, $labelId) { @@ -306,17 +314,17 @@ class labelsGmail /** * Delete PMGmail integration labels getting the list of labels in an email account. + * * @param string $mail User mail adress. * */ public function deletePMGmailLabels($mail) { - require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.pmGoogleApi.php"); $pmGoogle = new PMGoogleApi(); $pmGoogle->setUser($mail); - $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $pmGoogle->setScope(PMGoogleApi::GMAIL_MODIFY); $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); @@ -338,7 +346,7 @@ class labelsGmail return $count . ' labels successfully deleted.'; } - public function addRelabelingToQueue($caseId, $index, $actualLastIndex, $unassigned=false) + public function addRelabelingToQueue($caseId, $index, $actualLastIndex, $unassigned = false) { $labelingQueue = new GmailRelabeling(); $labelingQueue->setCreateDate(date('Y-m-d H:i:s')); @@ -353,11 +361,11 @@ class labelsGmail public function processPendingRelabelingInQueue() { - $c = new \Criteria( 'workflow' ); - $c->add( \GmailRelabelingPeer::STATUS, 'pending' ); + $c = new \Criteria('workflow'); + $c->add(\GmailRelabelingPeer::STATUS, 'pending'); $c->addAscendingOrderByColumn('CREATE_DATE'); $list = \GmailRelabelingPeer::doSelect($c); - foreach($list as $task) { + foreach ($list as $task) { try { $oResponse = $this->setLabels($task->getAppUid(), $task->getDelIndex(), @@ -365,8 +373,7 @@ class labelsGmail ($task->getUnassigned() === 1) ? true : false ); $task->setStatus('completed'); - } - catch(exception $e){ + } catch (exception $e) { $task->setMsgError($e->getMessage()); $task->setStatus('pending'); } @@ -374,14 +381,15 @@ class labelsGmail } } - private function getLabelsIds($service) { + private function getLabelsIds($service) + { $result = array(); try { $listlabels = $this->listLabels($service); foreach ($listlabels as $label) { - $labId = $label->getId(); + $labId = $label->getId(); $labName = $label->getName(); - switch($labName){ + switch ($labName) { case "* Inbox": $result['Inbox'] = $labId; break; @@ -399,8 +407,7 @@ class labelsGmail break; } } - } - catch(Exception $e) { + } catch (Exception $e) { throw $e; } return $result; diff --git a/workflow/engine/classes/class.pmGoogleApi.php b/workflow/engine/classes/class.pmGoogleApi.php index f4191cb44..85f778008 100755 --- a/workflow/engine/classes/class.pmGoogleApi.php +++ b/workflow/engine/classes/class.pmGoogleApi.php @@ -17,6 +17,7 @@ class PMGoogleApi const DRIVE_METADATA_READONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly'; const DRIVE_APPDATA = 'https://www.googleapis.com/auth/drive.appdata'; const DRIVE_PHOTOS_READONLY = 'https://www.googleapis.com/auth/drive.photos.readonly'; + const GMAIL_MODIFY = 'https://www.googleapis.com/auth/gmail.modify'; private $scope = array(); private $serviceAccountEmail;