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