Merged in marcoAntonioNina/processmaker/GI-165-A (pull request #3459)
Change scopes to constants
This commit is contained in:
@@ -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
|
||||||
{
|
{
|
||||||
@@ -27,7 +29,8 @@ class labelsGmail
|
|||||||
* @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);
|
||||||
@@ -45,10 +48,12 @@ class labelsGmail
|
|||||||
* @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,10 +77,8 @@ 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);
|
||||||
|
|
||||||
@@ -98,7 +101,7 @@ class labelsGmail
|
|||||||
|
|
||||||
$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);
|
||||||
@@ -119,14 +122,13 @@ class labelsGmail
|
|||||||
$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);
|
||||||
|
|
||||||
@@ -149,7 +151,7 @@ class labelsGmail
|
|||||||
|
|
||||||
$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);
|
||||||
@@ -170,11 +172,13 @@ class labelsGmail
|
|||||||
$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);
|
||||||
@@ -199,13 +203,11 @@ class labelsGmail
|
|||||||
//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);
|
||||||
@@ -215,15 +217,19 @@ class labelsGmail
|
|||||||
$labelsToRemove = $labelsIds['Draft'];
|
$labelsToRemove = $labelsIds['Draft'];
|
||||||
$labelsToSearch = "*-draft";
|
$labelsToSearch = "*-draft";
|
||||||
$labelsToAdd = $labelsIds['Participated'];
|
$labelsToAdd = $labelsIds['Participated'];
|
||||||
} else if ( ($actualLastIndex == -1) && ($unassigned == true) ){ //Unassigned
|
} else {
|
||||||
|
if (($actualLastIndex == -1) && ($unassigned == true)) { //Unassigned
|
||||||
$labelsToRemove = $labelsIds['Unassigned'];
|
$labelsToRemove = $labelsIds['Unassigned'];
|
||||||
$labelsToSearch = "*-unassigned";
|
$labelsToSearch = "*-unassigned";
|
||||||
$labelsToAdd = $labelsIds['Inbox'];
|
$labelsToAdd = $labelsIds['Inbox'];
|
||||||
} else if($actualLastIndex >= 1) {
|
} else {
|
||||||
|
if ($actualLastIndex >= 1) {
|
||||||
$labelsToRemove = $labelsIds['Inbox'];
|
$labelsToRemove = $labelsIds['Inbox'];
|
||||||
$labelsToSearch = "*-inbox";
|
$labelsToSearch = "*-inbox";
|
||||||
$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 . "')";
|
||||||
@@ -232,7 +238,8 @@ class labelsGmail
|
|||||||
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));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +274,7 @@ class labelsGmail
|
|||||||
$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);
|
||||||
@@ -281,7 +288,8 @@ class labelsGmail
|
|||||||
|
|
||||||
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));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
@@ -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,7 +381,8 @@ class labelsGmail
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getLabelsIds($service) {
|
private function getLabelsIds($service)
|
||||||
|
{
|
||||||
$result = array();
|
$result = array();
|
||||||
try {
|
try {
|
||||||
$listlabels = $this->listLabels($service);
|
$listlabels = $this->listLabels($service);
|
||||||
@@ -399,8 +407,7 @@ class labelsGmail
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception $e) {
|
||||||
catch(Exception $e) {
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user