1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Traits; |
4
|
|
|
|
5
|
|
|
use Google_Service_Gmail; |
6
|
|
|
|
7
|
|
|
trait HasLabels |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* List the labels in the user's mailbox. |
11
|
|
|
* |
12
|
|
|
* @param $userEmail |
13
|
|
|
* |
14
|
|
|
* @return \Google\Service\Gmail\ListLabelsResponse |
15
|
|
|
*/ |
16
|
|
|
public function labelsList($userEmail) |
17
|
|
|
{ |
18
|
|
|
$service = new Google_Service_Gmail($this); |
|
|
|
|
19
|
|
|
|
20
|
|
|
return $service->users_labels->listUsersLabels($userEmail); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create new label by name. |
25
|
|
|
* |
26
|
|
|
* @param $userEmail |
27
|
|
|
* @param $label |
28
|
|
|
* |
29
|
|
|
* @return \Google\Service\Gmail\Label |
30
|
|
|
*/ |
31
|
|
|
public function createLabel($userEmail, $label) |
32
|
|
|
{ |
33
|
|
|
$service = new Google_Service_Gmail($this); |
|
|
|
|
34
|
|
|
|
35
|
|
|
return $service->users_labels->create($userEmail, $label); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* first or create label in the user's mailbox. |
40
|
|
|
* |
41
|
|
|
* @param $userEmail |
42
|
|
|
* @param $nLabel |
43
|
|
|
* @return \Google\Service\Gmail\Label |
44
|
|
|
*/ |
45
|
|
|
public function firstOrCreateLabel($userEmail, $newLabel) |
46
|
|
|
{ |
47
|
|
|
$labels = $this->labelsList($userEmail); |
48
|
|
|
|
49
|
|
|
foreach ($labels->getLabels() as $existLabel) { |
50
|
|
|
if ($existLabel->getName() == $newLabel->getName()) { |
51
|
|
|
return $existLabel; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$service = new Google_Service_Gmail($this); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $service->users_labels->create($userEmail, $newLabel); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* List the labels in the user's mailbox. |
62
|
|
|
* |
63
|
|
|
* @param $userEmail |
64
|
|
|
* |
65
|
|
|
* @return \Google\Service\Gmail\Label |
66
|
|
|
*/ |
67
|
|
|
public function getLabel($userEmail, $id) |
68
|
|
|
{ |
69
|
|
|
$service = new Google_Service_Gmail($this); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $service->users_labels->get($userEmail, $id); |
72
|
|
|
} |
73
|
|
|
} |