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