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80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php


class labelsGmail
{
    function listLabels($service)
    {
        $labels = array();
        try {
            $labelsResponse = $service->users_labels->listUsersLabels('me');
            if ($labelsResponse->getLabels()) {
                $labels = array_merge($labels, $labelsResponse->getLabels());
            }
        } catch (Exception $e) {
            print G::LoadTranslation("ID_PMGMAIL_GENERAL_ERROR") . G::getErrorMessage($e);
            throw ($e);
        }
        return $labels;
    }

    /**
     * Modify the Labels a Message is associated with.
     *
     * @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               $messageId      ID of Message to modify.
     * @param  array                $labelsToAdd    Array of Labels to add.
     * @param  array                $labelsToRemove Array of Labels to remove.
     */
    function modifyMessage($service, $userId, $messageId, $labelsToAdd, $labelsToRemove)
    {
        $mods = new Google_Service_Gmail_ModifyMessageRequest();
        $mods->setAddLabelIds($labelsToAdd);
        $mods->setRemoveLabelIds($labelsToRemove);
        try {
            $message = $service->users_messages->modify($userId, $messageId, $mods);
        } catch (Exception $e) {
            print G::LoadTranslation("ID_PMGMAIL_GENERAL_ERROR") . G::getErrorMessage($e);
            throw ($e);
        }
    }

    /**
     * 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.
     *
     * @return array Array of Messages.
     */
    function listMessages($service, $userId, $query, $labels)
    {
        $pageToken = null;
        $messages = array();
        $opt_param = array();
        do {
            try {
                if ($pageToken) {
                    $opt_param['pageToken'] = $pageToken;
                }
                $opt_param['labelIds'] = $labels;
                $opt_param['q'] = $query;
                $opt_param['maxResults'] = 3;
                $messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
                if ($messagesResponse->getMessages()) {
                    $messages = array_merge($messages, $messagesResponse->getMessages());
                }
            } catch (Exception $e) {
                print G::LoadTranslation("ID_PMGMAIL_GENERAL_ERROR") . G::getErrorMessage($e);
                throw ($e);
            }
        } while ($pageToken);

        return $messages;
    }

    public function setLabelsToPauseCase($caseId, $index)
    {
        $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 . " (" . $index . ") Case: " . $appNumber;

        $pmGoogle = new PmGoogleApi();
        $pmGoogle->setUser($mail);
        $pmGoogle->setScope(PmGoogleApi::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)
    {
        $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 . " (" . $index . ") Case: " . $appNumber;

        $pmGoogle = new PmGoogleApi();
        $pmGoogle->setUser($mail);
        $pmGoogle->setScope(PmGoogleApi::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);

        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'];
            $tasUid = $application['TAS_UID'];
        }

        if ($threadStatus == 'CLOSED' || $unassigned == true) {
            //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 . " (" . $index . ") Case: " . $appNumber;

            $pmGoogle = new PmGoogleApi();

            $pmGoogle->setUser($mail);

            $pmGoogle->setScope(PmGoogleApi::GMAIL_MODIFY);
            $client = $pmGoogle->serviceClient();

            $service = new Google_Service_Gmail($client);
            $labelsIds = $this->getLabelsIds($service);

            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'];
                    }
                }
            }

            //Searching the email in the user's mail
            $q = "subject:('" . preg_quote($subject, '-') . "') label:('" . $labelsToSearch . "')";
            $messageList = $this->listMessages($service, $mail, $q, $labelsToRemove);

            //if there isn't any message at draft, and lasindex is zero, is a subprocess
            //and we must search in inbox:
            if ($actualLastIndex ===  0 && count($messageList) ===0) {
                $labelsToRemove = $labelsIds['Inbox'];
                $labelsToSearch = "*-inbox";
                $labelsToAdd = $labelsIds['Participated'];
                $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));

            }

            //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");
                $oTaskUsers = new \TaskUser();
                $taskUsers = $oTaskUsers->getAllUsersTask($tasUid);
                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
                    if ($nextMail !== $mail) {
                        $this->changeLabelsOfUnassigned($appData[0], $nextMail);
                    }
                }
            }
        }
    }

    function changeLabelsOfUnassigned($application, $mail)
    {
        $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'];
        $tasUid = $application['TAS_UID'];

        //The Subject to search the email
        $subject = "[PM] " . $proName . " (" . $index . ") Case: " . $appNumber;
        $pmGoogle = new PmGoogleApi();
        $pmGoogle->setUser($mail);
        $pmGoogle->setScope(PmGoogleApi::GMAIL_MODIFY);
        $client = $pmGoogle->serviceClient();
        $service = new Google_Service_Gmail($client);
        $labelsIds = $this->getLabelsIds($service);
        $labelsToRemove = $labelsIds['Unassigned'];
        $labelsToSearch = "*-unassigned";
        $labelsToAdd = $labelsIds['Participated'];

        //Searching the email in the user's mail
        $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));

        }
    }


    /**
     * 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.
     */
    public function deleteLabel($service, $user, $labelId)
    {
        try {
            $service->users_labels->delete($user, $labelId);
        } catch (Exception $e) {
            error_log(G::LoadTranslation("ID_PMGMAIL_GENERAL_ERROR") . $e->getMessage());
        }
    }

    /**
     * Delete PMGmail integration labels getting the list of labels in an email account.
     *
     * @param string $mail User mail adress.
     *
     */
    public function deletePMGmailLabels($mail)
    {
        $pmGoogle = new PmGoogleApi();

        $pmGoogle->setUser($mail);

        $pmGoogle->setScope(PmGoogleApi::GMAIL_MODIFY);
        $client = $pmGoogle->serviceClient();

        $service = new Google_Service_Gmail($client);
        $count = 0;
        $listlabels = $this->listLabels($service);
        foreach ($listlabels as $label) {
            if ($label->getName() == '* Inbox' ||
                $label->getName() == '* Participated' ||
                $label->getName() == '* Unassigned' ||
                $label->getName() == '* Draft' ||
                $label->getName() == '* Inbox' ||
                $label->getName() == '* --- ProcessMaker ---' ||
                $label->getName() == '* Paused'
            ) {
                $oresp = $this->deleteLabel($service, 'me', $label->getId());
                $count++;
            }
        }
        return $count . ' labels successfully deleted.';
    }

    public function addRelabelingToQueue($caseId, $index, $actualLastIndex, $unassigned = false)
    {
        $labelingQueue = new GmailRelabeling();
        $labelingQueue->setCreateDate(date('Y-m-d H:i:s'));
        $labelingQueue->setLabelingUid(G::generateUniqueID());
        $labelingQueue->setAppUid($caseId);
        $labelingQueue->setDelIndex($index);
        $labelingQueue->setCurrentLastIndex($actualLastIndex);
        $labelingQueue->setUnassigned(($unassigned === true) ? 1 : 0);
        $labelingQueue->setStatus('pending');
        $labelingQueue->save();
    }

    public function processPendingRelabelingInQueue()
    {
        $c = new \Criteria('workflow');
        $c->add(\GmailRelabelingPeer::STATUS, 'pending');
        $c->addAscendingOrderByColumn('CREATE_DATE');
        $list = \GmailRelabelingPeer::doSelect($c);
        foreach ($list as $task) {
            try {
                $oResponse = $this->setLabels($task->getAppUid(),
                    $task->getDelIndex(),
                    $task->getCurrentLastIndex(),
                    ($task->getUnassigned() === 1) ? true : false
                );
                $task->setStatus('completed');
            } catch (exception $e) {
                $task->setMsgError($e->getMessage());
                $task->setStatus('pending');
            }
            $task->save();
        }
    }

    private function getLabelsIds($service)
    {
        $result = array();
        try {
            $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;
                }
            }
        } catch (Exception $e) {
            throw $e;
        }
        return $result;
    }
}