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
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
{
@@ -27,7 +29,8 @@ class labelsGmail
* @param array $labelsToAdd Array of Labels to add.
* @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->setAddLabelIds($labelsToAdd);
$mods->setRemoveLabelIds($labelsToRemove);
@@ -45,10 +48,12 @@ class labelsGmail
* @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.
*
* @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
} else {
if (($actualLastIndex == -1) && ($unassigned == true)) { //Unassigned
$labelsToRemove = $labelsIds['Unassigned'];
$labelsToSearch = "*-unassigned";
$labelsToAdd = $labelsIds['Inbox'];
} else if($actualLastIndex >= 1) {
} 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));
}
}
@@ -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();
$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;

View File

@@ -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;