-
Notifications
You must be signed in to change notification settings - Fork 75
/
mosaico.php
342 lines (311 loc) · 13 KB
/
mosaico.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
require_once 'mosaico.civix.php';
use CRM_Mosaico_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function mosaico_civicrm_config(&$config) {
_mosaico_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function mosaico_civicrm_install() {
_mosaico_civix_civicrm_install();
$schema = new CRM_Logging_Schema();
$schema->fixSchemaDifferences();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function mosaico_civicrm_uninstall() {
$schema = new CRM_Logging_Schema();
$schema->fixSchemaDifferences();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function mosaico_civicrm_enable() {
_mosaico_civix_civicrm_enable();
}
function mosaico_civicrm_alterAngular(\Civi\Angular\Manager $angular) {
$changeSet = \Civi\Angular\ChangeSet::create('mosaico_subject_list')
->alterHtml('~/crmMailing/BlockMailing.html', function (phpQueryObject $doc) {
$field = $doc->find('[name=subject]')->parent('[crm-ui-field]');
$field->html('<crm-mosaico-subject-list crm-mailing="mailing"/>');
$field->after('<div crm-ui-field="{name: \'subform.dist\', title: ts(\'Distribution\')}" ng-if="mailing.template_options.variants"><crm-mosaico-distribution crm-mailing="mailing" /></div>');
});
$angular->add($changeSet);
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
* Grant permissions for mosaico_base_template and mosaico_template based on
* those granted for message_template.
*
* If no permissions are defined on message_template then we do not grant any
* permissions.
*
* @link https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_alterAPIPermissions/
*/
function mosaico_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
if (isset($permissions['message_template'])) {
// Permissions are defined for message_template, so use those to grant our
// new permissions.
$permissions['mosaico_base_template'] = $permissions['message_template'];
$permissions['mosaico_template'] = $permissions['message_template'];
if (isset($permissions['message_template']['create'])) {
$permissions['mosaico_template']['clone'] = $permissions['message_template']['create'];
}
}
}
/**
* Implements hook_civicrm_check().
*/
function mosaico_civicrm_check(&$messages) {
//Make sure the ImageMagick library is loaded.
try {
Civi::service('mosaico_graphics');
}
catch (CRM_Mosaico_Graphics_Exception $e) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_graphics',
E::ts('Mosaico requires a graphics driver such as PHP-ImageMagick or PHP-GD. For more information, see <a href="%1">Mosaico Settings</a>.', [
1 => \CRM_Utils_System::url('civicrm/admin/setting/mosaico', 'reset=1'),
])
. "<p><em>" . E::ts("Error: %1", [1 => $e->getMessage()]) . "</em></p>",
E::ts('Graphics driver not available'),
\Psr\Log\LogLevel::CRITICAL,
'fa-chain-broken'
);
}
if (!extension_loaded('fileinfo')) {
$messages[] = new CRM_Utils_Check_Message('mosaico_fileinfo', E::ts('May experience mosaico template or thumbnail loading issues (404 errors).'), E::ts('PHP extension Fileinfo not loaded or enabled'));
}
if (!file_exists(E::path('packages/mosaico/dist/rs/mosaico.min.js')) || !file_exists(E::path('packages/mosaico/dist/rs/mosaico-libs-and-tinymce.min.js'))) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_packages',
E::ts('Mosaico requires dependencies in its "packages" folder. Please consult the README.md for current installation instructions.'),
E::ts('Mosaico: Packages are missing'),
\Psr\Log\LogLevel::CRITICAL,
'fa-chain-broken'
);
}
if (CRM_Mailing_Info::workflowEnabled()) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_workflow',
E::ts('CiviMail is configured to support advanced workflows. This is currently incompatible with the Mosaico mailer. Navigate to "Administer => CiviMail => CiviMail Component Settings" to disable it.'),
E::ts('Advanced CiviMail workflows unsupported'),
\Psr\Log\LogLevel::CRITICAL,
'fa-chain-broken'
);
}
if (!CRM_Extension_System::singleton()->getMapper()->isActiveModule('flexmailer')) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_flexmailer',
E::ts('Mosaico uses FlexMailer for delivery. Please install the extension "org.civicrm.flexmailer".'),
E::ts('FlexMailer required'),
\Psr\Log\LogLevel::CRITICAL,
'fa-chain-broken'
);
}
else {
$RECOMMENDED_FLEXMAILER = '1.1.1';
$fmInfo = CRM_Extension_System::singleton()->getMapper()->keyToInfo('org.civicrm.flexmailer');
if (version_compare($fmInfo->version, $RECOMMENDED_FLEXMAILER, '<')) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_flexmailer_ver',
E::ts('The extension %1 expects %2 version <code>%3</code> or newer. Found version <code>%4</code>.', [
1 => 'Mosaico',
2 => 'FlexMailer',
3 => $RECOMMENDED_FLEXMAILER,
4 => $fmInfo->version,
]),
E::ts('Outdated dependency'),
\Psr\Log\LogLevel::WARNING
);
}
}
if (!empty($mConfig['BASE_URL'])) {
// detect incorrect image upload url. (Note: Since v4.4.4, CRM_Utils_Check_Security has installed index.html placeholder.)
$handle = curl_init($mConfig['BASE_URL'] . '/index.html');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($httpCode == 404) {
$messages[] = new CRM_Utils_Check_Message('mosaico_base_url', E::ts('BASE_URL seems incorrect - %1. Images when uploaded, may not appear correctly as thumbnails. Make sure "Image Upload URL" is configured correctly with Administer » System Settings » Resouce URLs.', [1 => $mConfig['BASE_URL']]), E::ts('Incorrect image upload url'));
}
}
$oldTplCount = CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_mosaico_msg_template');
if ($oldTplCount > 0) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_migrate_1x',
E::ts('Found %1 template(s) from CiviCRM-Mosaico v1.x. Use the <a href="%2">Migration Assistant</a> to load them in v2.x.', [
1 => $oldTplCount,
2 => CRM_Utils_System::url('civicrm/admin/mosaico/migrate', 'reset=1'),
]),
E::ts('Mosaico: Migrate templates (1.x => 2.x)'),
\Psr\Log\LogLevel::WARNING
);
}
_mosaico_civicrm_check_dirs($messages);
}
function _mosaico_civicrm_check_dirs(&$messages) {
$mConfig = CRM_Mosaico_Utils::getConfig();
// Check if UPLOADS directory exists and create it if it doesn't
if (!is_dir($mConfig['BASE_DIR'] . $mConfig['UPLOADS_DIR'])) {
if (!mkdir($mConfig['BASE_DIR'] . $mConfig['UPLOADS_DIR'], 0775, TRUE)) {
$messages[] = new CRM_Utils_Check_Message('mosaico_uploads_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['UPLOADS_DIR']]), E::ts('UPLOADS_DIR not writable or configured'));
}
}
elseif (!is_writable($mConfig['BASE_DIR'] . $mConfig['UPLOADS_DIR'])) {
$messages[] = new CRM_Utils_Check_Message('mosaico_uploads_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['UPLOADS_DIR']]), E::ts('UPLOADS_DIR not writable or configured'));
}
// Check if uploads/STATIC directory exists and create it if it doesn't
if (!is_dir($mConfig['BASE_DIR'] . $mConfig['STATIC_DIR'])) {
if (!mkdir($mConfig['BASE_DIR'] . $mConfig['STATIC_DIR'], 0775, TRUE)) {
$messages[] = new CRM_Utils_Check_Message('mosaico_static_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['STATIC_DIR']]), E::ts('STATIC_DIR not writable or configured'));
}
}
elseif (!is_writable($mConfig['BASE_DIR'] . $mConfig['STATIC_DIR'])) {
$messages[] = new CRM_Utils_Check_Message('mosaico_static_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['STATIC_DIR']]), E::ts('STATIC_DIR not writable or configured'));
}
// Check if uploads/THUMBNAILS directory exists and create it if it doesn't
if (!is_dir($mConfig['BASE_DIR'] . $mConfig['THUMBNAILS_DIR'])) {
if (!mkdir($mConfig['BASE_DIR'] . $mConfig['THUMBNAILS_DIR'], 0775, TRUE)) {
$messages[] = new CRM_Utils_Check_Message('mosaico_thumbnails_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['THUMBNAILS_DIR']]), E::ts('THUMBNAILS_DIR not writable or configured'));
}
}
elseif (!is_writable($mConfig['BASE_DIR'] . $mConfig['THUMBNAILS_DIR'])) {
$messages[] = new CRM_Utils_Check_Message('mosaico_thumbnails_dir', E::ts('%1 not writable or configured.', [1 => $mConfig['BASE_DIR'] . $mConfig['THUMBNAILS_DIR']]), E::ts('THUMBNAILS_DIR not writable or configured'));
}
}
/**
* Convert dyanmic-y image URLs to static-y URLs.
*
* This is analogous to alterMailContent, but we only apply to Mosaico mailings.
*
* @param $content
* This parameter seems a bit confused.
* @see CRM_Mosaico_MosaicoComposer
*/
function _mosaico_civicrm_alterMailContent(&$content) {
// Mosaico templates have a few of their own tokens which are named differently from
// CiviMail tokens. By treating these as aliases, we can get more compatibility between
// Civi's delivery system and upstream Mosaico templates.
$tokenAliases = [
// '[profile_link]' => 'FIXME',
'[show_link]' => '{mailing.viewUrl}&cid={contact.contact_id}&{contact.checksum}',
'[subject]' => '{mailing.subject}',
'[unsubscribe_link]' => '{action.unsubscribeUrl}',
];
$content = str_replace(array_keys($tokenAliases), array_values($tokenAliases), $content);
// Some existing and customized templates have awkward HTML <TITLE>s, which show up when viewing the mailing the browser.
$content = preg_replace(';(\<head.*\<title\>\s*)TITLE(\s*\</title\>.*\</head\>);ms', '\\1{mailing.subject}\\2', $content, 1);
}
/**
* Implements hook_civicrm_mailingTemplateTypes().
*
* @throws \CRM_Core_Exception
*/
function mosaico_civicrm_mailingTemplateTypes(&$types) {
$messages = [];
mosaico_civicrm_check($messages);
foreach (array_keys($messages) as $key) {
if ($messages[$key]->getLevel() <= 4) {
unset($messages[$key]);
}
}
$editorUrl = empty($messages) ?
CRM_Mosaico_Utils::getLayoutPath() :
'~/crmMosaico/requirements.html';
$types[] = [
'name' => 'mosaico',
'editorUrl' => $editorUrl,
'weight' => -10,
];
}
/**
* Implements hook_civicrm_entityTypes().
*/
function mosaico_civicrm_entityTypes(&$entityTypes) {
// _mosaico_civix_civicrm_entityTypes($entityTypes);
$entityTypes[] = [
'name' => 'MosaicoTemplate',
'class' => 'CRM_Mosaico_DAO_MosaicoTemplate',
'table' => 'civicrm_mosaico_template',
];
}
/**
* Implements hook_civicrm_pre().
*/
function mosaico_civicrm_pre($op, $objectName, $id, &$params) {
if ($objectName === 'Mailing' && $op === 'create') {
if (isset($params['template_type']) && $params['template_type'] === 'mosaico') {
$params['header_id'] = 'null';
$params['footer_id'] = 'null';
}
}
}
/**
* Implements hook_civicrm_container().
*/
function mosaico_civicrm_container(\Symfony\Component\DependencyInjection\ContainerBuilder $container) {
$container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__));
require_once 'CRM/Mosaico/Services.php';
CRM_Mosaico_Services::registerServices($container);
}
/**
* Implements hook_civicrm_searchTasks().
*/
function mosaico_civicrm_searchTasks($objectName, &$tasks) {
if ($objectName == 'contact') {
if (CRM_Core_Permission::access('CiviMail')) {
$tasks[] = [
'title' => E::ts('Email - schedule/send via CiviMail (traditional)'),
'class' => 'CRM_Mosaico_Form_Task_AdhocMailingTraditional',
];
}
}
}
/**
* Listen to prepare event and a mailing wrapper for the
* Mailing.submit and Mailing.send_test api actions.
*
* @param \Civi\API\Event\PrepareEvent $event
*/
function mosaico_wrapMailingApi($event) {
$a = $event->getApiRequest();
switch ($a['entity'] . '.' . $a['action']) {
case 'Mailing.submit':
if (is_numeric($a['params']['id'])) {
$abMux = \Civi::service('mosaico_ab_demux');
$event->wrapApi([$abMux, 'onSubmitMailing']);
}
break;
case 'Mailing.send_test':
if (is_numeric($a['params']['mailing_id'])) {
$abMux = \Civi::service('mosaico_ab_demux');
$event->wrapApi([$abMux, 'onSendTestMailing']);
}
break;
}
}
/**
* Implements hook_civicrm_apiWrappers().
*/
function mosaico_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if ($apiRequest['entity'] == 'MosaicoTemplate' && $apiRequest['action'] == 'get') {
if ($apiRequest['version'] == '4') {
$wrappers[] = new CRM_Mosaico_API4Wrappers_MosaicoTemplate();
}
}
}