Skip to content

Commit

Permalink
Merge pull request #529 from krishgopi/2.x
Browse files Browse the repository at this point in the history
#410 - Multisite support for mosaico
  • Loading branch information
mattwire authored Jul 8, 2022
2 parents 516586c + c40edfa commit a76d59e
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 5 deletions.
32 changes: 31 additions & 1 deletion CRM/Mosaico/DAO/MosaicoTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from uk.co.vedaconsulting.mosaico/xml/schema/CRM/Mosaico/MosaicoTemplate.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0802c4ee4037ee1644860bb85baa890f)
* (GenCodeChecksum:16450473094763338ba3f855ec5804e0)
*/
use CRM_Mosaico_ExtensionUtil as E;

Expand Down Expand Up @@ -87,6 +87,13 @@ class CRM_Mosaico_DAO_MosaicoTemplate extends CRM_Core_DAO {
*/
public $category_id;

/**
* Domain ID this message template belongs to.
*
* @var int
*/
public $domain_id;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -115,6 +122,7 @@ public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'msg_tpl_id', 'civicrm_msg_template', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'domain_id', 'civicrm_domain', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
Expand Down Expand Up @@ -254,6 +262,28 @@ public static function &fields() {
],
'add' => NULL,
],
'domain_id' => [
'name' => 'domain_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Domain ID'),
'description' => ts('Domain ID this message template belongs to.'),
'required' => FALSE,
'where' => 'civicrm_mosaico_template.domain_id',
'table_name' => 'civicrm_mosaico_template',
'entity' => 'MosaicoTemplate',
'bao' => 'CRM_Mosaico_DAO_MosaicoTemplate',
'localizable' => 0,
'FKClassName' => 'CRM_Core_DAO_Domain',
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'table' => 'civicrm_domain',
'keyColumn' => 'id',
'labelColumn' => 'name',
],
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mosaico/Page/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function createMosaicoConfig() {
]);

$config = [
'imgProcessorBackend' => $this->getUrl('civicrm/mosaico/img', NULL, TRUE),
'imgProcessorBackend' => $this->getUrl('civicrm/mosaico/img', NULL, FALSE),
'imgPlaceholderUrl' => $this->getUrl('civicrm/mosaico/img/placeholder', NULL, FALSE),
'emailProcessorBackend' => 'unused-emailProcessorBackend',
'titleToken' => 'MOSAICO Responsive Email Designer',
Expand Down
25 changes: 25 additions & 0 deletions CRM/Mosaico/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ public function upgrade_4706() {
return TRUE;
}

/**
* Add domain_id to the civicrm_mosaico_template table.
*/
public function upgrade_4707() {
$this->ctx->log->info('Applying update 4707');

CRM_Core_DAO::executeQuery('
ALTER TABLE civicrm_mosaico_template
ADD COLUMN `domain_id` int unsigned NULL COMMENT \'FK from civicrm_domain.\'
');

CRM_Core_DAO::executeQuery('
ALTER TABLE civicrm_mosaico_template
ADD CONSTRAINT FK_civicrm_mosaico_template_domain_id
FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
ON DELETE SET NULL
');

// Update existing templates with default domain ID
$domainID = CRM_Core_Config::domainID();
CRM_Core_DAO::executeQuery("UPDATE civicrm_mosaico_template SET domain_id = {$domainID} WHERE domain_id IS NULL");

return TRUE;
}


/**
* Example: Run an external SQL script.
Expand Down
64 changes: 62 additions & 2 deletions api/v3/MosaicoTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ function _civicrm_api3_mosaico_template_create_spec(&$spec) {
* @throws API_Exception
*/
function civicrm_api3_mosaico_template_create($params) {
// Add current domain id to the template while creating
if(empty($params['domain_id']) && empty($params['id'])){
$params['domain_id'] = CRM_Core_Config::domainID();
}

if (isset($params['metadata'])) {
$metaData = json_decode($params['metadata'], TRUE);
$baseTemplateURL = $metaData['template'];

// Check if domain name exists in the URL and remove it. Storing the relative path to get the templates work with multi domain sites.
if (_civicrm_api3_mosaico_template_getDomainFrom($baseTemplateURL)) {
$urlParts = parse_url($baseTemplateURL);
$templatePath = trim($urlParts['path'], '/');
} else {
$templatePath = $baseTemplateURL;
}

$metaData['template'] = $templatePath;
$params['metadata'] = json_encode($metaData);
}
return _civicrm_api3_basic_create('CRM_Mosaico_BAO_MosaicoTemplate', $params);
}

Expand All @@ -42,7 +62,32 @@ function civicrm_api3_mosaico_template_delete($params) {
* @throws API_Exception
*/
function civicrm_api3_mosaico_template_get($params) {
return _civicrm_api3_basic_get('CRM_Mosaico_BAO_MosaicoTemplate', $params);
// Added the current domain id to the template while retrieving
if(empty($params['domain_id']) && empty($params['id'])){
$params['domain_id'] = CRM_Core_Config::domainID();
}

$getresult = _civicrm_api3_basic_get('CRM_Mosaico_BAO_MosaicoTemplate', $params);
// Check if domain name exists in the URL and append current domain with it. Returning the URL with current domain to get the templates work with multi domain sites.
if ( !empty($getresult['values']) && isset($getresult['values'][0]['metadata']) ) {
$metaData = json_decode($getresult['values'][0]['metadata'], TRUE);
$baseTemplateURL = $metaData['template'];

$baseURL = CRM_Utils_System::baseURL();

if (_civicrm_api3_mosaico_template_getDomainFrom($baseTemplateURL)) {
$urlParts = parse_url($baseTemplateURL);
$templatePath = $urlParts['path'];
$currentURL = CRM_Utils_System::baseURL() . $templatePath;
} else {
$currentURL = CRM_Utils_System::baseURL() . $baseTemplateURL;
}

$metaData['template'] = $currentURL;
$getresult['values'][0]['metadata'] = json_encode($metaData);
}

return $getresult;
}

/**
Expand Down Expand Up @@ -130,4 +175,19 @@ function civicrm_api3_mosaico_template_replaceurls($params) {

CRM_Mosaico_BAO_MosaicoTemplate::replaceUrls($params['from_url'], $params['to_url']);
return _civicrm_api3_basic_get('CRM_Mosaico_BAO_MosaicoTemplate', ['return' => ['id', 'title']]);
}
}

/**
* Function to check if a URL has domain name
*
* @param array $url
* @return boolean
*/
function _civicrm_api3_mosaico_template_getDomainFrom($url) {
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ If you move CiviCRM to a new domain, you must update the template paths using th
```
cv api MosaicoTemplate.replaceurls from_url="http://old.server.org" to_url="https://new.server.org"
```
## Multisite Support - Restrict mosaico templates & image gallery to domain specific
- Mosaico templates will be restricted to each domain in multi sites
- You need to ensure that you have separate image upload directory set in Directory settings & Resource URLs settings, so that each site would have unique set of images in their gallery while editing a mosaico template.
eg: [civicrm.files]/persist/contribute/site2
4 changes: 3 additions & 1 deletion sql/auto_install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ CREATE TABLE `civicrm_mosaico_template` (
`metadata` longtext COMMENT 'Mosaico metadata (JSON)',
`content` longtext COMMENT 'Mosaico content (JSON)',
`msg_tpl_id` int unsigned NULL COMMENT 'FK to civicrm_msg_template.',
`category_id` int(10) UNSIGNED DEFAULT NULL COMMENT 'ID of the category this mailing template is currently belongs. Foreign key to civicrm_option_value.'
`category_id` int(10) UNSIGNED DEFAULT NULL COMMENT 'ID of the category this mailing template is currently belongs. Foreign key to civicrm_option_value.',
`domain_id` int unsigned NULL COMMENT 'FK to civicrm_domain.'
,
PRIMARY KEY (`id`)


, CONSTRAINT FK_civicrm_mosaico_template_msg_tpl_id FOREIGN KEY (`msg_tpl_id`) REFERENCES `civicrm_msg_template`(`id`) ON DELETE SET NULL
, CONSTRAINT FK_civicrm_mosaico_template_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB ;


22 changes: 22 additions & 0 deletions xml/schema/CRM/Mosaico/MosaicoTemplate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,26 @@
</html>
</field>

<field>
<name>domain_id</name>
<type>int unsigned</type>
<title>Domain ID</title>
<required>false</required>
<comment>Domain ID this message template belongs to.</comment>
<pseudoconstant>
<table>civicrm_domain</table>
<keyColumn>id</keyColumn>
<labelColumn>name</labelColumn>
</pseudoconstant>
<html>
<type>Select</type>
</html>
</field>
<foreignKey>
<name>domain_id</name>
<table>civicrm_domain</table>
<key>id</key>
<onDelete>SET NULL</onDelete>
</foreignKey>

</table>

0 comments on commit a76d59e

Please sign in to comment.