Merged in marcoAntonioNina/processmaker/GI-165-A (pull request #3459)

Change scopes to constants
This commit is contained in:
Dante Loayza
2016-01-06 11:18:47 -04:00
2 changed files with 81 additions and 73 deletions

View File

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

View File

@@ -17,6 +17,7 @@ class PMGoogleApi
const DRIVE_METADATA_READONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly'; const DRIVE_METADATA_READONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly';
const DRIVE_APPDATA = 'https://www.googleapis.com/auth/drive.appdata'; const DRIVE_APPDATA = 'https://www.googleapis.com/auth/drive.appdata';
const DRIVE_PHOTOS_READONLY = 'https://www.googleapis.com/auth/drive.photos.readonly'; 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 $scope = array();
private $serviceAccountEmail; private $serviceAccountEmail;