Merge pull request #18749 from totten/master-crmMailing-factory
[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 CRM_Core_Resources::singleton()
163 ->addPermissions([
164 'view all contacts',
165 'edit all contacts',
166 'access CiviMail',
167 'create mailings',
168 'schedule mailings',
169 'approve mailings',
170 'delete in CiviMail',
171 'edit message templates',
172 ]);
173
174 return $result;
175 }
176
177 /**
178 * @return bool
179 */
180 public static function workflowEnabled() {
181 $config = CRM_Core_Config::singleton();
182
183 // early exit, since not true for most
184 if (!$config->userSystem->is_drupal ||
185 !function_exists('module_exists')
186 ) {
187 return FALSE;
188 }
189
190 if (!module_exists('rules')) {
191 return FALSE;
192 }
193
194 $enableWorkflow = Civi::settings()->get('civimail_workflow');
195
196 return $enableWorkflow && $config->userSystem->is_drupal;
197 }
198
199 /**
200 * @inheritDoc
201 * @param bool $getAllUnconditionally
202 * @param bool $descriptions
203 * Whether to return permission descriptions
204 *
205 * @return array
206 */
207 public function getPermissions($getAllUnconditionally = FALSE, $descriptions = FALSE) {
208 $permissions = [
209 'access CiviMail' => [
210 ts('access CiviMail'),
211 ],
212 'access CiviMail subscribe/unsubscribe pages' => [
213 ts('access CiviMail subscribe/unsubscribe pages'),
214 ts('Subscribe/unsubscribe from mailing list group'),
215 ],
216 'delete in CiviMail' => [
217 ts('delete in CiviMail'),
218 ts('Delete Mailing'),
219 ],
220 'view public CiviMail content' => [
221 ts('view public CiviMail content'),
222 ],
223 ];
224
225 if (self::workflowEnabled() || $getAllUnconditionally) {
226 $permissions['create mailings'] = [
227 ts('create mailings'),
228 ];
229 $permissions['schedule mailings'] = [
230 ts('schedule mailings'),
231 ];
232 $permissions['approve mailings'] = [
233 ts('approve mailings'),
234 ];
235 }
236
237 if (!$descriptions) {
238 foreach ($permissions as $name => $attr) {
239 $permissions[$name] = array_shift($attr);
240 }
241 }
242
243 return $permissions;
244 }
245
246 /**
247 * @inheritDoc
248 * @return null
249 */
250 public function getUserDashboardElement() {
251 // no dashboard element for this component
252 return NULL;
253 }
254
255 /**
256 * @return null
257 */
258 public function getUserDashboardObject() {
259 // no dashboard element for this component
260 return NULL;
261 }
262
263 /**
264 * @inheritDoc
265 * @return array
266 */
267 public function registerTab() {
268 return [
269 'title' => ts('Mailings'),
270 'id' => 'mailing',
271 'url' => 'mailing',
272 'weight' => 45,
273 ];
274 }
275
276 /**
277 * @inheritDoc
278 * @return string
279 */
280 public function getIcon() {
281 return 'crm-i fa-envelope-o';
282 }
283
284 /**
285 * @inheritDoc
286 * @return array
287 */
288 public function registerAdvancedSearchPane() {
289 return [
290 'title' => ts('Mailings'),
291 'weight' => 20,
292 ];
293 }
294
295 /**
296 * @inheritDoc
297 * @return null
298 */
299 public function getActivityTypes() {
300 return NULL;
301 }
302
303 /**
304 * add shortcut to Create New.
305 * @param $shortCuts
306 */
307 public function creatNewShortcut(&$shortCuts) {
308 }
309
310 }