Merge pull request #15794 from KarinG/master
[civicrm-core.git] / CRM / Mailing / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This class introduces component to the system and provides all the
14 * information about it. It needs to extend CRM_Core_Component_Info
15 * abstract class.
16 *
17 * @package CRM
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 */
20 class CRM_Mailing_Info extends CRM_Core_Component_Info {
21
22 /**
23 * @var string
24 * @inheritDoc
25 */
26 protected $keyword = 'mailing';
27
28 /**
29 * @inheritDoc
30 * @return array
31 */
32 public function getInfo() {
33 return [
34 'name' => 'CiviMail',
35 'translatedName' => ts('CiviMail'),
36 'title' => ts('CiviCRM Mailing Engine'),
37 'search' => 1,
38 'showActivitiesInCore' => 1,
39 ];
40 }
41
42 /**
43 * Get AngularJS modules and their dependencies.
44 *
45 * @return array
46 * list of modules; same format as CRM_Utils_Hook::angularModules(&$angularModules)
47 * @see CRM_Utils_Hook::angularModules
48 */
49 public function getAngularModules() {
50 // load angular files only if valid permissions are granted to the user
51 if (!CRM_Core_Permission::check('access CiviMail')
52 && !CRM_Core_Permission::check('create mailings')
53 && !CRM_Core_Permission::check('schedule mailings')
54 && !CRM_Core_Permission::check('approve mailings')
55 ) {
56 return [];
57 }
58 global $civicrm_root;
59
60 $reportIds = [];
61 $reportTypes = ['detail', 'opened', 'bounce', 'clicks'];
62 foreach ($reportTypes as $report) {
63 $result = civicrm_api3('ReportInstance', 'get', [
64 'sequential' => 1,
65 'report_id' => 'mailing/' . $report,
66 ]);
67 if (!empty($result['values'])) {
68 $reportIds[$report] = $result['values'][0]['id'];
69 }
70 }
71 $result = [];
72 $result['crmMailing'] = include "$civicrm_root/ang/crmMailing.ang.php";
73 $result['crmMailingAB'] = include "$civicrm_root/ang/crmMailingAB.ang.php";
74 $result['crmD3'] = include "$civicrm_root/ang/crmD3.ang.php";
75
76 $config = CRM_Core_Config::singleton();
77 $session = CRM_Core_Session::singleton();
78 $contactID = $session->get('userID');
79
80 // Generic params.
81 $params = [
82 'options' => ['limit' => 0],
83 'sequential' => 1,
84 ];
85 $groupNames = civicrm_api3('Group', 'get', $params + [
86 'is_active' => 1,
87 'check_permissions' => TRUE,
88 'return' => ['title', 'visibility', 'group_type', 'is_hidden'],
89 ]);
90 $headerfooterList = civicrm_api3('MailingComponent', 'get', $params + [
91 'is_active' => 1,
92 'return' => ['name', 'component_type', 'is_default', 'body_html', 'body_text'],
93 ]);
94
95 $emailAdd = civicrm_api3('Email', 'get', [
96 'sequential' => 1,
97 'return' => "email",
98 'contact_id' => $contactID,
99 ]);
100
101 $mesTemplate = civicrm_api3('MessageTemplate', 'get', $params + [
102 'sequential' => 1,
103 'is_active' => 1,
104 'return' => ["id", "msg_title"],
105 'workflow_id' => ['IS NULL' => ""],
106 ]);
107 $mailTokens = civicrm_api3('Mailing', 'gettokens', [
108 'entity' => ['contact', 'mailing'],
109 'sequential' => 1,
110 ]);
111 $fromAddress = civicrm_api3('OptionValue', 'get', $params + [
112 'option_group_id' => "from_email_address",
113 'domain_id' => CRM_Core_Config::domainID(),
114 ]);
115 $enabledLanguages = CRM_Core_I18n::languages(TRUE);
116 $isMultiLingual = (count($enabledLanguages) > 1);
117 // FlexMailer is a refactoring of CiviMail which provides new hooks/APIs/docs. If the sysadmin has opted to enable it, then use that instead of CiviMail.
118 $requiredTokens = defined('CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS') ? Civi\Core\Resolver::singleton()->call(CIVICRM_FLEXMAILER_HACK_REQUIRED_TOKENS, []) : CRM_Utils_Token::getRequiredTokens();
119 CRM_Core_Resources::singleton()
120 ->addSetting([
121 'crmMailing' => [
122 'templateTypes' => CRM_Mailing_BAO_Mailing::getTemplateTypes(),
123 'civiMails' => [],
124 'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents),
125 'groupNames' => [],
126 // @todo this is not used in core. Remove once Mosaico no longer depends on it.
127 'testGroupNames' => $groupNames['values'],
128 'headerfooterList' => $headerfooterList['values'],
129 'mesTemplate' => $mesTemplate['values'],
130 'emailAdd' => $emailAdd['values'],
131 'mailTokens' => $mailTokens['values'],
132 'contactid' => $contactID,
133 'requiredTokens' => $requiredTokens,
134 'enableReplyTo' => (int) Civi::settings()->get('replyTo'),
135 'disableMandatoryTokensCheck' => (int) Civi::settings()->get('disable_mandatory_tokens_check'),
136 'fromAddress' => $fromAddress['values'],
137 'defaultTestEmail' => civicrm_api3('Contact', 'getvalue', [
138 'id' => 'user_contact_id',
139 'return' => 'email',
140 ]),
141 'visibility' => CRM_Utils_Array::makeNonAssociative(CRM_Core_SelectValues::groupVisibility()),
142 'workflowEnabled' => CRM_Mailing_Info::workflowEnabled(),
143 'reportIds' => $reportIds,
144 'enabledLanguages' => $enabledLanguages,
145 'isMultiLingual' => $isMultiLingual,
146 ],
147 ])
148 ->addPermissions([
149 'view all contacts',
150 'edit all contacts',
151 'access CiviMail',
152 'create mailings',
153 'schedule mailings',
154 'approve mailings',
155 'delete in CiviMail',
156 'edit message templates',
157 ]);
158
159 return $result;
160 }
161
162 /**
163 * @return bool
164 */
165 public static function workflowEnabled() {
166 $config = CRM_Core_Config::singleton();
167
168 // early exit, since not true for most
169 if (!$config->userSystem->is_drupal ||
170 !function_exists('module_exists')
171 ) {
172 return FALSE;
173 }
174
175 if (!module_exists('rules')) {
176 return FALSE;
177 }
178
179 $enableWorkflow = Civi::settings()->get('civimail_workflow');
180
181 return ($enableWorkflow &&
182 $config->userSystem->is_drupal
183 ) ? TRUE : FALSE;
184 }
185
186 /**
187 * @inheritDoc
188 * @param bool $getAllUnconditionally
189 * @param bool $descriptions
190 * Whether to return permission descriptions
191 *
192 * @return array
193 */
194 public function getPermissions($getAllUnconditionally = FALSE, $descriptions = FALSE) {
195 $permissions = [
196 'access CiviMail' => [
197 ts('access CiviMail'),
198 ],
199 'access CiviMail subscribe/unsubscribe pages' => [
200 ts('access CiviMail subscribe/unsubscribe pages'),
201 ts('Subscribe/unsubscribe from mailing list group'),
202 ],
203 'delete in CiviMail' => [
204 ts('delete in CiviMail'),
205 ts('Delete Mailing'),
206 ],
207 'view public CiviMail content' => [
208 ts('view public CiviMail content'),
209 ],
210 ];
211
212 if (self::workflowEnabled() || $getAllUnconditionally) {
213 $permissions['create mailings'] = [
214 ts('create mailings'),
215 ];
216 $permissions['schedule mailings'] = [
217 ts('schedule mailings'),
218 ];
219 $permissions['approve mailings'] = [
220 ts('approve mailings'),
221 ];
222 }
223
224 if (!$descriptions) {
225 foreach ($permissions as $name => $attr) {
226 $permissions[$name] = array_shift($attr);
227 }
228 }
229
230 return $permissions;
231 }
232
233 /**
234 * @inheritDoc
235 * @return null
236 */
237 public function getUserDashboardElement() {
238 // no dashboard element for this component
239 return NULL;
240 }
241
242 /**
243 * @return null
244 */
245 public function getUserDashboardObject() {
246 // no dashboard element for this component
247 return NULL;
248 }
249
250 /**
251 * @inheritDoc
252 * @return array
253 */
254 public function registerTab() {
255 return [
256 'title' => ts('Mailings'),
257 'id' => 'mailing',
258 'url' => 'mailing',
259 'weight' => 45,
260 ];
261 }
262
263 /**
264 * @inheritDoc
265 * @return string
266 */
267 public function getIcon() {
268 return 'crm-i fa-envelope-o';
269 }
270
271 /**
272 * @inheritDoc
273 * @return array
274 */
275 public function registerAdvancedSearchPane() {
276 return [
277 'title' => ts('Mailings'),
278 'weight' => 20,
279 ];
280 }
281
282 /**
283 * @inheritDoc
284 * @return null
285 */
286 public function getActivityTypes() {
287 return NULL;
288 }
289
290 /**
291 * add shortcut to Create New.
292 * @param $shortCuts
293 */
294 public function creatNewShortcut(&$shortCuts) {
295 }
296
297 }