1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Enalean, 2011. All Rights Reserved. |
4
|
|
|
* |
5
|
|
|
* This file is a part of Tuleap. |
6
|
|
|
* |
7
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
require_once 'common/mail/Codendi_Mail_Interface.class.php'; |
22
|
|
|
require_once 'common/mail/Mail.class.php'; |
23
|
|
|
require_once 'common/mail/Codendi_Mail.class.php'; |
24
|
|
|
require_once 'common/include/Tuleap_Template.class.php'; |
25
|
|
|
|
26
|
|
|
require_once 'common/user/UserManager.class.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Mail manager is the key interface to send emails in the platform |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
class MailManager { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prepare the mail according to user preferences |
36
|
|
|
* |
37
|
|
|
* @param PFUser $user The user to whom send the mail |
38
|
|
|
* |
39
|
|
|
* @return Mail |
40
|
|
|
*/ |
41
|
|
|
public function getMailForUser(PFUser $user) { |
42
|
|
|
$mail = $this->getMailByType($this->getMailPreferencesByUser($user)); |
43
|
|
|
$mail->setToUser(array($user)); |
44
|
|
|
return $mail; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return a mail object depending of the requested format |
49
|
|
|
* |
50
|
|
|
* @param String $type Type of mail (text or html) |
51
|
|
|
* |
52
|
|
|
* @return Mail |
53
|
|
|
*/ |
54
|
|
|
public function getMailByType($type = null) { |
55
|
|
|
$mail = new Codendi_Mail(); |
|
|
|
|
56
|
|
|
if ($type == Codendi_Mail_Interface::FORMAT_TEXT) { |
57
|
|
|
$mail = new Mail(); |
58
|
|
|
} |
59
|
|
|
$mail->setFrom($this->getConfig('sys_noreply')); |
60
|
|
|
return $mail; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return users corresponding to email addresses mapped according to their |
65
|
|
|
* preferences. |
66
|
|
|
* |
67
|
|
|
* @deprecated |
68
|
|
|
* |
69
|
|
|
* @param Array $addresses A set of addresses |
70
|
|
|
* |
71
|
|
|
* @return Array of Array of User |
72
|
|
|
*/ |
73
|
|
|
public function getMailPreferencesByEmail($addresses) { |
74
|
|
|
$default = Codendi_Mail_Interface::FORMAT_HTML; |
75
|
|
|
$res = array('html' => array(), 'text' => array()); |
76
|
|
|
$um = $this->getUserManager(); |
77
|
|
|
foreach ($addresses as $address) { |
78
|
|
|
$users = $um->getAllUsersByEmail($address); |
79
|
|
|
$pref = $default; |
80
|
|
|
if (count($users) > 0) { |
81
|
|
|
foreach ($users as $user) { |
82
|
|
|
$pref_user = $this->getMailPreferencesByUser($user); |
83
|
|
|
$user_status = $user->getStatus(); |
84
|
|
|
if ($pref_user != $default && ($user_status == 'A' || $user_status == 'R')) { |
85
|
|
|
$pref = $pref_user; |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
$user = new PFUser(array('user_id' => 0, 'language_id' => $this->getConfig('sys_lang'))); |
91
|
|
|
$user->setEmail($address); |
92
|
|
|
} |
93
|
|
|
$res[$pref][] = $user; |
94
|
|
|
} |
95
|
|
|
return $res; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns whether the user wants an HTML or a Text notification |
100
|
|
|
* |
101
|
|
|
* @param PFUser $user |
102
|
|
|
* |
103
|
|
|
* @return String |
104
|
|
|
*/ |
105
|
|
|
public function getMailPreferencesByUser(PFUser $user) { |
106
|
|
|
if ($user->getPreference(Codendi_Mail_Interface::PREF_FORMAT) == Codendi_Mail_Interface::FORMAT_TEXT) { |
107
|
|
|
return Codendi_Mail_Interface::FORMAT_TEXT; |
108
|
|
|
} |
109
|
|
|
return Codendi_Mail_Interface::FORMAT_HTML; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns all possible mail formats |
114
|
|
|
* |
115
|
|
|
* @return Array |
116
|
|
|
*/ |
117
|
|
|
public function getAllMailFormats() { |
118
|
|
|
return array(Codendi_Mail_Interface::FORMAT_TEXT, Codendi_Mail_Interface::FORMAT_HTML); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Wrapper for configuration access |
123
|
|
|
* |
124
|
|
|
* @param String $var |
125
|
|
|
* |
126
|
|
|
* @return String |
127
|
|
|
*/ |
128
|
|
|
protected function getConfig($var) { |
129
|
|
|
return ForgeConfig::get($var); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Wrapper for UserManager |
134
|
|
|
* |
135
|
|
|
* @return UserManager |
136
|
|
|
*/ |
137
|
|
|
protected function getUserManager() { |
138
|
|
|
return UserManager::instance(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
?> |
143
|
|
|
|