-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaico.php
379 lines (338 loc) · 12.3 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
require_once 'mosaico.civix.php';
/**
* 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_xmlMenu().
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function mosaico_civicrm_xmlMenu(&$files) {
_mosaico_civix_civicrm_xmlMenu($files);
}
/**
* 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_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function mosaico_civicrm_postInstall() {
_mosaico_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function mosaico_civicrm_uninstall() {
_mosaico_civix_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();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function mosaico_civicrm_disable() {
_mosaico_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed
* Based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function mosaico_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _mosaico_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function mosaico_civicrm_managed(&$entities) {
_mosaico_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function mosaico_civicrm_caseTypes(&$caseTypes) {
_mosaico_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function mosaico_civicrm_angularModules(&$angularModules) {
_mosaico_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function mosaico_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_mosaico_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Functions below this ship commented out. Uncomment as required.
*
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
function mosaico_civicrm_preProcess($formName, &$form) {
}
*/
function mosaico_civicrm_navigationMenu(&$params){
$parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Mailings', 'id', 'name');
//$msgTpls = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Message Templates', 'id', 'name');
$maxId = max(array_keys($params));
$msgTplMaxId = empty($msgTpls) ? $maxId+10 : $msgTpls;
$params[$parentId]['child'][$msgTplMaxId] = array(
'attributes' => array(
'label' => ts('Message Template Builder'),
'name' => 'Message_Template_Builder',
'url' => CRM_Utils_System::url('civicrm/mosaico/index', 'reset=1', TRUE),
'active' => 1,
'parentID' => $parentId,
'operator' => NULL,
'navID' => $msgTplMaxId,
'permission'=> 'access CiviCRM Mosaico',
),
);
_mosaico_civix_insert_navigation_menu($params, 'Mailings', array(
'label' => ts('Mosaico Templates', array('domain' => 'org.civicrm.styleguide')),
'name' => 'mosaico_templates',
'permission' => 'access CiviCRM Mosaico',
'child' => array(),
'operator' => 'OR',
'separator' => 0,
'url' => CRM_Utils_System::url('civicrm/a/', NULL, TRUE, '/mosaico-template'),
));
_mosaico_civix_navigationMenu($menu);
}
function mosaico_civicrm_pageRun(&$page){
$pageName = $page->getVar('_name');
if ($pageName == 'Civi\Angular\Page\Main') {
CRM_Core_Resources::singleton()->addScriptFile('uk.co.vedaconsulting.mosaico', 'js/crmMailingCustom.js', 800);
}
if ($pageName == 'CRM_Admin_Page_MessageTemplates') {
$activeTab = CRM_Utils_Request::retrieve('activeTab', 'String', $form, false, null, 'REQUEST');
$resultArray= $exsitingTemplates = array();
$smarty = CRM_Core_Smarty::singleton();
$tableName = CRM_Mosaico_DAO_MessageTemplate::getTableName();
$dao = CRM_Core_DAO::executeQuery("SELECT mosaico.*, cmt.msg_title, cmt.msg_subject, cmt.is_active
FROM {$tableName} mosaico
JOIN civicrm_msg_template cmt ON (cmt.id = mosaico.msg_tpl_id)
");
while ($dao->fetch()) {
$resultArray[$dao->id] = $dao->toArray();
$editURL= CRM_Utils_System::url('civicrm/mosaico/editor', 'snippet=2', FALSE, $dao->hash_key);
$delURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=delete&id='.$dao->msg_tpl_id);
$enableDisableText = $dao->is_active ? 'Disable' : 'Enable';
$action = sprintf('<span>
<a href="%s" class="action-item crm-hover-button" title="Edit this message template" >Edit</a>
<a href="#" class="action-item crm-hover-button crm-enable-disable" title="Disable this message template" >%s</a>
<a href="%s" class="action-item crm-hover-button small-popup" title="Delete this message template" >Delete</a>
<a href="#" class="action-item crm-hover-button copy-template" value = "%s" title="Copy this message template">Copy</a>
</span>', $editURL, $enableDisableText, $delURL, $dao->msg_tpl_id);
$resultArray[$dao->id]['action'] = $action;
//all tempalte values which are exist in Mosaico tempalte and related hash key
$exsitingTemplates[$dao->msg_tpl_id] = $dao->hash_key;
}
//To inject new action link with default actions.
//row already assinged in smarty, so get template variable , and append action and assigned new row values to tpl.
$rows = $smarty->get_template_vars('rows');
foreach ($rows['userTemplates'] as $key => $value) {
$editURL = '#';
$editableClassName = "edit_msg_tpl_to_mosaico";
$editableLinkName = "Import in Mosaico";
//url for existing mosaico templates. otherwise we create dummy with new hashkey and link to open up in mosaico editor.
//and using classname to allow edit only if not exist in mosaico template.
if (array_key_exists($key, $exsitingTemplates)) {
$editURL = CRM_Utils_System::url('civicrm/mosaico/editor', 'snippet=2', FALSE, $exsitingTemplates[$key]);
$editableClassName = NULL;
$editableLinkName = "Edit In Mosaico";
}
$action = sprintf('<span>
<a href="%s" class="action-item crm-hover-button %s" value="xx" title="Open in Mosaico" >%s</a>
</span>', $editURL, $editableClassName, $editableLinkName);
if (defined('CIVICRM_MOSAICO_IMPORT') && CIVICRM_MOSAICO_IMPORT == 1) {
//MV: allow open msg template in mosaico editor.
$rows['userTemplates'][$key]['action'] .= $action;
}
}
$smarty->assign('rows', $rows);
$smarty->assign('mosaicoTemplates', $resultArray);
$smarty->assign('selectedChild', $activeTab);
// From civi 4.7, no more tinymce, so if only civi version is less than 4.7 show tinymce.
//https://civicrm.org/blog/colemanw/big-changes-to-wysiwyg-editing-in-47
$showTinymceTpl = FALSE;
$currentVer = CRM_Core_BAO_Domain::version(TRUE);
if (version_compare($currentVer, '4.7') < 0) {
$showTinymceTpl = TRUE;
}
$smarty->assign('showTinymceTpl', $showTinymceTpl);
}
}
/**
* Implements hook_civicrm_check.
*
* @param array $messages
* List of CRM_Utils_Check_Message objects.
*/
function mosaico_civicrm_check(&$messages) {
//Make sure the ImageMagick library is loaded.
if( !(extension_loaded('imagick') || class_exists("Imagick"))){
$messages[] = new CRM_Utils_Check_Message(
'mosaico_imagick',
ts('the ImageMagick library is not installed. The Email Template Builder extension will not work without it.'),
ts('ImageMagick not installed'),
\Psr\Log\LogLevel::CRITICAL
);
}
if (CRM_Mailing_Info::workflowEnabled()) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_workflow',
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.'),
ts('Advanced CiviMail workflows unsupported'),
\Psr\Log\LogLevel::CRITICAL
);
}
if (!CRM_Extension_System::singleton()->getMapper()->isActiveModule('bootstrapcivicrm')) {
$messages[] = new CRM_Utils_Check_Message(
'mosaico_bootstrap',
ts('Mosaico uses Bootstrap CSS. Please install the extension "org.civicrm.bootstrapcivicrm".'),
ts('Bootstrap required'),
\Psr\Log\LogLevel::CRITICAL
);
}
}
/**
* Implementation of hook_civicrm_permission
*
* @param array $permissions
* @return void
*/
function mosaico_civicrm_permission(&$permissions) {
$prefix = ts('CiviMail Mosaico') . ': '; // name of extension or module
$permissions += array(
'access CiviCRM Mosaico' => $prefix . ts('access CiviCRM Mosaico'),
);
}
/**
* Implementation of hook_civicrm_alterMailContent
*
* @param array $permissions
* @return void
*/
function mosaico_civicrm_alterMailContent(&$content)
{
/**
* create absolute urls for Mosaico/imagemagick images when sending an email in CiviMail
* convert string below into just the absolute url with addition of static directory where correctly sized image is stored
* Mosaico image urls are in this format:
* img?src=BASE_URL+UPLOADS_URL+imagename+imagemagickparams
*/
$mosaico_config = CRM_Mosaico_Utils::getConfig();
$mosaico_image_upload_dir = rawurlencode($mosaico_config['BASE_URL'].$mosaico_config['UPLOADS_URL']);
$content = preg_replace_callback(
"/src=\"h.+img\?src=(".$mosaico_image_upload_dir.")(.+)&.*\"/U",
function($matches){
return "src=\"" . rawurldecode($matches[1]) . "static/" . rawurldecode($matches[2]) . "\"";
},
$content
);
}
/**
* Implements hook_civicrm_mailingTemplateTypes().
*
* @throws \CRM_Core_Exception
*/
function mosaico_civicrm_mailingTemplateTypes(&$types) {
$messages = array();
mosaico_civicrm_check($messages);
$editorUrl = empty($messages)
? '~/crmMosaico/EditMailingCtrl/mosaico.html'
: '~/crmMosaico/requirements.html';
$types[] = array(
'name' => 'mosaico',
'editorUrl' => $editorUrl,
'weight' => -10,
);
}
/**
* Implements hook_civicrm_entityTypes().
*/
function mosaico_civicrm_entityTypes(&$entityTypes) {
$entityTypes[] = array(
'name' => 'MosaicoTemplate',
'class' => 'CRM_Mosaico_DAO_MosaicoTemplate',
'table' => 'civicrm_mosaico_template',
);
}
/**
* Implements hook_civicrm_pre().
*
* @param string $op
* @param string $objectName
* @param int|null $id
* @param array|object $params
*/
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;
}
}
}