From 7f825d18a2c533f96609e2571c54870e51867d39 Mon Sep 17 00:00:00 2001 From: Dante Date: Mon, 7 Dec 2015 11:55:46 -0400 Subject: [PATCH] =?UTF-8?q?GI-127=20Gmail=20Integration>=20No=20se=20est?= =?UTF-8?q?=C3=A1n=20derivando=20los=20casos=20a=20la=20lista=20de=20pause?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workflow/engine/classes/class.case.php | 11 ++ workflow/engine/classes/class.labelsGmail.php | 172 ++++++++++++++---- .../engine/methods/cases/derivatedGmail.php | 2 +- .../ProcessMaker/BusinessModel/Pmgmail.php | 13 ++ 4 files changed, 164 insertions(+), 34 deletions(-) diff --git a/workflow/engine/classes/class.case.php b/workflow/engine/classes/class.case.php index e83fe2e92..70c3b7c66 100755 --- a/workflow/engine/classes/class.case.php +++ b/workflow/engine/classes/class.case.php @@ -4214,6 +4214,11 @@ class Cases $oListPaused = new ListPaused(); $oListPaused->create($data); /*----------------------------------********---------------------------------*/ + + /*----------------------------------********---------------------------------*/ + $pmGmail = new \ProcessMaker\BusinessModel\Pmgmail(); + $pmGmail->modifyMailToPauseCase($aData['APP_UID'], $aData['APP_DEL_INDEX']); + /*----------------------------------********---------------------------------*/ } /* @@ -4310,6 +4315,11 @@ class Cases $oListPaused = new ListPaused(); $oListPaused->remove($sApplicationUID, $iDelegation, $aData); /*----------------------------------********---------------------------------*/ + + /*----------------------------------********---------------------------------*/ + $pmGmail = new \ProcessMaker\BusinessModel\Pmgmail(); + $pmGmail->modifyMailToUnpauseCase($aData['APP_UID'], $aData['DEL_INDEX']); + /*----------------------------------********---------------------------------*/ } /* @@ -7328,5 +7338,6 @@ class Cases } return $processList; } + } diff --git a/workflow/engine/classes/class.labelsGmail.php b/workflow/engine/classes/class.labelsGmail.php index 4ec9a240a..aeeab63f9 100644 --- a/workflow/engine/classes/class.labelsGmail.php +++ b/workflow/engine/classes/class.labelsGmail.php @@ -69,7 +69,109 @@ class labelsGmail return $messages; } - function setLabels($caseId, $index, $actualLastIndex, $unassigned=false){ + 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"); + + $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); + $appData = $Pmgmail->getDraftApp($caseId, $index); + + foreach ($appData as $application){ + $appNumber = $application['APP_NUMBER']; + $index = $application['DEL_INDEX']; + $threadUsr = $application['USR_UID']; + $proName = $application['APP_PRO_TITLE']; + $threadStatus = $application['DEL_THREAD_STATUS']; + $appStatus = $application['APP_STATUS']; + } + + //Getting the privious User email + $oUsers = new \Users(); + $usrData = $oUsers->loadDetails($threadUsr); + $mail = $usrData['USR_EMAIL']; + + //The Subject to search the email + $subject = "[PM] " .$proName. " Case: ". $appNumber; + + $pmGoogle = new PMGoogleApi(); + $pmGoogle->setUser($mail); + $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $client = $pmGoogle->serviceClient(); + $service = new Google_Service_Gmail($client); + $labelsIds = $this->getLabelsIds($service); + + if($appStatus == 'DRAFT'){ + $labelsToRemove = $labelsIds['Draft']; + $labelsToSearch = "*-draft"; + $labelsToAdd = $labelsIds['Paused']; + } + + if($appStatus == 'TO_DO'){ + $labelsToRemove = $labelsIds['Inbox']; + $labelsToSearch = "*-inbox"; + $labelsToAdd = $labelsIds['Paused']; + } + + $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)); + } + } + + 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"); + + $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); + $appData = $Pmgmail->getDraftApp($caseId, $index); + + foreach ($appData as $application){ + $appNumber = $application['APP_NUMBER']; + $index = $application['DEL_INDEX']; + $threadUsr = $application['USR_UID']; + $proName = $application['APP_PRO_TITLE']; + $threadStatus = $application['DEL_THREAD_STATUS']; + $appStatus = $application['APP_STATUS']; + } + + //Getting the privious User email + $oUsers = new \Users(); + $usrData = $oUsers->loadDetails($threadUsr); + $mail = $usrData['USR_EMAIL']; + + //The Subject to search the email + $subject = "[PM] " .$proName. " Case: ". $appNumber; + + $pmGoogle = new PMGoogleApi(); + $pmGoogle->setUser($mail); + $pmGoogle->setScope('https://www.googleapis.com/auth/gmail.modify'); + $client = $pmGoogle->serviceClient(); + $service = new Google_Service_Gmail($client); + $labelsIds = $this->getLabelsIds($service); + + if($appStatus == 'DRAFT'){ + $labelsToRemove = $labelsIds['Paused']; + $labelsToSearch = "*-paused"; + $labelsToAdd = $labelsIds['Draft']; + } + + if($appStatus == 'TO_DO'){ + $labelsToRemove = $labelsIds['Paused']; + $labelsToSearch = "*-paused"; + $labelsToAdd = $labelsIds['Inbox']; + } + + $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)); + } + } + + public function setLabels($caseId, $index, $actualLastIndex, $unassigned=false){ //First getting the actual thread data $Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail(); $appData = $Pmgmail->getDraftApp($caseId, $index); @@ -80,9 +182,10 @@ class labelsGmail $threadUsr = $application['USR_UID']; $proName = $application['APP_PRO_TITLE']; $threadStatus = $application['DEL_THREAD_STATUS']; + $appStatus = $application['APP_STATUS']; } - if($threadStatus == 'CLOSED' || $unassigned == true){ + if($threadStatus == 'CLOSED' || $unassigned == true) { //Getting the privious User email $oUsers = new \Users(); @@ -102,44 +205,20 @@ class labelsGmail $client = $pmGoogle->serviceClient(); $service = new Google_Service_Gmail($client); - - //getting all the label's ids of the user's mail - $listlabels = $this->listLabels($service); - - foreach ($listlabels as $label) { - $labId = $label->getId(); - $labName = $label->getName(); - switch($labName){ - case "* Inbox": - $idLabInbox = $labId; - break; - case "* Participated": - $idLabParticipated = $labId; - break; - case "* Unassigned": - $idLabUnassigned = $labId; - break; - case "* Draft": - $idLabDraft = $labId; - break; - case "* Paused": - $idLabPaused = $labId; - break; - } - } + $labelsIds = $this->getLabelsIds($service); if($actualLastIndex == 0){ - $labelsToRemove = $idLabDraft; + $labelsToRemove = $labelsIds['Draft']; $labelsToSearch = "*-draft"; - $labelsToAdd = $idLabParticipated; + $labelsToAdd = $labelsIds['Participated']; } else if ( ($actualLastIndex == -1) && ($unassigned == true) ){ //Unassigned - $labelsToRemove = $idLabUnassigned; + $labelsToRemove = $labelsIds['Unassigned']; $labelsToSearch = "*-unassigned"; - $labelsToAdd = $idLabInbox; + $labelsToAdd = $labelsIds['Inbox']; } else if($actualLastIndex >= 1) { - $labelsToRemove = $idLabInbox; + $labelsToRemove = $labelsIds['Inbox']; $labelsToSearch = "*-inbox"; - $labelsToAdd = $idLabParticipated; + $labelsToAdd = $labelsIds['Participated']; } //Searching the email in the user's mail @@ -207,5 +286,32 @@ class labelsGmail return $count . ' labels successfully deleted.'; } + + private function getLabelsIds($service) { + $result = array(); + $listlabels = $this->listLabels($service); + foreach ($listlabels as $label) { + $labId = $label->getId(); + $labName = $label->getName(); + switch($labName){ + case "* Inbox": + $result['Inbox'] = $labId; + break; + case "* Participated": + $result['Participated'] = $labId; + break; + case "* Unassigned": + $result['Unassigned'] = $labId; + break; + case "* Draft": + $result['Draft'] = $labId; + break; + case "* Paused": + $result['Paused'] = $labId; + break; + } + } + return $result; + } } diff --git a/workflow/engine/methods/cases/derivatedGmail.php b/workflow/engine/methods/cases/derivatedGmail.php index a0b2cfab9..2258257b0 100644 --- a/workflow/engine/methods/cases/derivatedGmail.php +++ b/workflow/engine/methods/cases/derivatedGmail.php @@ -38,7 +38,7 @@ $enablePMGmail = false; G::LoadClass( "pmDrive" ); $pmDrive = new PMDrive(); $enablePMGmail = $pmDrive->getStatusService(); -if( $_SESSION['gmail'] === 1 && !empty($enablePMGmail) && $enablePMGmail==1 ){ +if(key_exists('gmail', $_SESSION) && $_SESSION['gmail'] === 1 && !empty($enablePMGmail) && $enablePMGmail==1 ){ //$_SESSION['gmail'] = 0; $mUrl = '/sys'. $_SESSION['WORKSPACE'] .'/en/neoclassic/cases/cases_Open?APP_UID='.$caseId.'&DEL_INDEX='.$actualIndex.'&action=sent'; } else{ diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Pmgmail.php b/workflow/engine/src/ProcessMaker/BusinessModel/Pmgmail.php index 5e8b085c2..c20eadcac 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Pmgmail.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Pmgmail.php @@ -261,6 +261,19 @@ class Pmgmail { return $response; } + public function modifyMailToPauseCase($appUid, $appDelIndex) + { + require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.labelsGmail.php"); + $oLabels = new \labelsGmail(); + $oResponse = $oLabels->setLabelsToPauseCase($appUid, $appDelIndex); + } + + public function modifyMailToUnpauseCase($appUid, $appDelIndex) + { + require_once (PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.labelsGmail.php"); + $oLabels = new \labelsGmail(); + $oResponse = $oLabels->setLabelsToUnpauseCase($appUid, $appDelIndex); + } }