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