From 7ad5ae6a418b74a513bf7fc71d8025f787fd52db Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 20 Nov 2016 21:03:43 -0500 Subject: [PATCH] CRM-19649 - Multiple wysiwyg presets --- CRM/Admin/Form/MessageTemplates.php | 2 +- CRM/Admin/Page/CKEditorConfig.php | 54 +++++++----- CRM/Core/Form.php | 4 + CRM/Core/Resources.php | 5 +- CRM/Event/Form/ManageEvent/EventInfo.php | 2 +- CRM/Event/Form/ManageEvent/Registration.php | 10 +-- CRM/Upgrade/Incremental/php/FourSeven.php | 47 +++++++++++ ang/crmMailing/BodyHtml.html | 2 +- js/wysiwyg/crm.ckeditor.js | 12 +-- templates/CRM/Admin/Page/CKEditorConfig.tpl | 92 +++++++++++++-------- xml/templates/civicrm_data.tpl | 7 ++ 11 files changed, 166 insertions(+), 71 deletions(-) diff --git a/CRM/Admin/Form/MessageTemplates.php b/CRM/Admin/Form/MessageTemplates.php index 964dc953e0..9fcb640609 100644 --- a/CRM/Admin/Form/MessageTemplates.php +++ b/CRM/Admin/Form/MessageTemplates.php @@ -205,7 +205,7 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { 'cols' => '80', 'rows' => '8', 'onkeyup' => "return verify(this)", - 'class' => 'crm-wysiwyg-fullpage', + 'preset' => 'civimail', ) ); } diff --git a/CRM/Admin/Page/CKEditorConfig.php b/CRM/Admin/Page/CKEditorConfig.php index 5413fbf80b..1b42dee2b4 100644 --- a/CRM/Admin/Page/CKEditorConfig.php +++ b/CRM/Admin/Page/CKEditorConfig.php @@ -40,7 +40,7 @@ */ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { - const CONFIG_FILENAME = '[civicrm.files]/persist/crm-ckeditor-config.js'; + const CONFIG_FILEPATH = '[civicrm.files]/persist/crm-ckeditor-'; /** * Settings that cannot be configured in "advanced options" @@ -61,15 +61,19 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { 'filebrowserFlashUploadUrl', ); + public $preset; + /** * Run page. * * @return string */ public function run() { + $this->preset = CRM_Utils_Array::value('preset', $_REQUEST, 'default'); + // If the form was submitted, take appropriate action. if (!empty($_POST['revert'])) { - self::deleteConfigFile(); + self::deleteConfigFile($this->preset); } elseif (!empty($_POST['config'])) { $this->save($_POST); @@ -91,10 +95,17 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { 'settings' => $settings, )); + $configUrl = self::getConfigUrl($this->preset); + if (!$configUrl) { + $configUrl = self::getConfigUrl('default'); + } + + $this->assign('preset', $this->preset); + $this->assign('presets', CRM_Core_OptionGroup::values('wysiwyg_presets', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name')); $this->assign('skins', $this->getCKSkins()); $this->assign('skin', CRM_Utils_Array::value('skin', $settings)); $this->assign('extraPlugins', CRM_Utils_Array::value('extraPlugins', $settings)); - $this->assign('configUrl', self::getConfigUrl()); + $this->assign('configUrl', $configUrl); $this->assign('revertConfirm', htmlspecialchars(ts('Are you sure you want to revert all changes?', array('escape' => 'js')))); CRM_Utils_System::appendBreadCrumb(array(array( @@ -133,7 +144,7 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { $config = substr_replace($config, $setting, $pos, 0); } } - self::saveConfigFile($config); + self::saveConfigFile($this->preset, $config); if (!empty($params['save'])) { CRM_Core_Session::setStatus(ts("You may need to clear your browser's cache to see the changes in CiviCRM."), ts('CKEditor Saved'), 'success'); } @@ -194,7 +205,7 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { */ private function getConfigSettings() { $matches = $result = array(); - $file = self::getConfigFile(); + $file = self::getConfigFile($this->preset); $result['skin'] = 'moono'; if ($file) { $contents = file_get_contents($file); @@ -207,39 +218,44 @@ class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { } /** - * @return null|string + * @param string $preset + * Omit to get an array of all presets + * @return array|null|string */ - public static function getConfigUrl() { - if (self::getConfigFile()) { - return Civi::paths()->getUrl(self::CONFIG_FILENAME, 'absolute'); + public static function getConfigUrl($preset = NULL) { + $items = array(); + $presets = CRM_Core_OptionGroup::values('wysiwyg_presets', FALSE, FALSE, FALSE, NULL, 'name'); + foreach ($presets as $key => $name) { + if (self::getConfigFile($name)) { + $items[$name] = Civi::paths()->getUrl(self::CONFIG_FILEPATH . $name . '.js', 'absolute'); + } } - return NULL; + return $preset ? CRM_Utils_Array::value($preset, $items) : $items; } /** - * @param bool $checkIfFileExists - * If false, this fn will return fileName even if it doesn't exist + * @param string $preset * * @return null|string */ - public static function getConfigFile($checkIfFileExists = TRUE) { - $fileName = Civi::paths()->getPath(self::CONFIG_FILENAME); - return !$checkIfFileExists || is_file($fileName) ? $fileName : NULL; + public static function getConfigFile($preset = 'default') { + $fileName = Civi::paths()->getPath(self::CONFIG_FILEPATH . $preset . '.js'); + return is_file($fileName) ? $fileName : NULL; } /** * @param string $contents */ - public static function saveConfigFile($contents) { - $file = self::getConfigFile(FALSE); + public static function saveConfigFile($preset, $contents) { + $file = Civi::paths()->getPath(self::CONFIG_FILEPATH . $preset . '.js'); file_put_contents($file, $contents); } /** * Delete config file. */ - public static function deleteConfigFile() { - $file = self::getConfigFile(); + public static function deleteConfigFile($preset) { + $file = self::getConfigFile($preset); if ($file) { unlink($file); } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 2204af696e..c694a85252 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -348,6 +348,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { if ($type == 'wysiwyg' || in_array($type, self::$html5Types)) { $attributes = ($attributes ? $attributes : array()) + array('class' => ''); $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type"); + if ($type == 'wysiwyg' && isset($attributes['preset'])) { + $attributes['data-preset'] = $attributes['preset']; + unset($attributes['preset']); + } $type = $type == 'wysiwyg' ? 'textarea' : 'text'; } // @see http://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index fef18a071c..92d73bd6dd 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -728,10 +728,7 @@ class CRM_Core_Resources { $editor = Civi::settings()->get('editor_id'); if ($editor == "CKEditor") { $items[] = "js/wysiwyg/crm.ckeditor.js"; - $ckConfig = CRM_Admin_Page_CKEditorConfig::getConfigUrl(); - if ($ckConfig) { - $items[] = array('config' => array('CKEditorCustomConfig' => $ckConfig)); - } + $items[] = array('config' => array('CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig::getConfigUrl())); } // These scripts are only needed by back-office users diff --git a/CRM/Event/Form/ManageEvent/EventInfo.php b/CRM/Event/Form/ManageEvent/EventInfo.php index bf9b787276..b81625341c 100644 --- a/CRM/Event/Form/ManageEvent/EventInfo.php +++ b/CRM/Event/Form/ManageEvent/EventInfo.php @@ -172,7 +172,7 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent { $this->addSelect('participant_listing_id', array('placeholder' => ts('Disabled'), 'option_url' => NULL)); $this->add('textarea', 'summary', ts('Event Summary'), $attributes['summary']); - $this->add('wysiwyg', 'description', ts('Complete Description'), $attributes['event_description']); + $this->add('wysiwyg', 'description', ts('Complete Description'), $attributes['event_description'] + array('preset' => 'civievent')); $this->addElement('checkbox', 'is_public', ts('Public Event')); $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?')); $this->addElement('checkbox', 'is_map', ts('Include Map to Event Location')); diff --git a/CRM/Event/Form/ManageEvent/Registration.php b/CRM/Event/Form/ManageEvent/Registration.php index b114488f3b..f37f74fc84 100644 --- a/CRM/Event/Form/ManageEvent/Registration.php +++ b/CRM/Event/Form/ManageEvent/Registration.php @@ -316,7 +316,7 @@ class CRM_Event_Form_ManageEvent_Registration extends CRM_Event_Form_ManageEvent * */ public function buildRegistrationBlock(&$form) { - $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'intro_text') + array('class' => 'collapsed'); + $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'intro_text') + array('class' => 'collapsed', 'preset' => 'civievent'); $form->add('wysiwyg', 'intro_text', ts('Introductory Text'), $attributes); $form->add('wysiwyg', 'footer_text', ts('Footer Text'), $attributes); @@ -406,8 +406,8 @@ class CRM_Event_Form_ManageEvent_Registration extends CRM_Event_Form_ManageEvent $form->addYesNo('is_confirm_enabled', ts('Use a confirmation screen?'), NULL, NULL, array('onclick' => "return showHideByValue('is_confirm_enabled','','confirm_screen_settings','block','radio',false);")); } $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']); - $form->add('wysiwyg', 'confirm_text', ts('Introductory Text'), $attributes['confirm_text'] + array('class' => 'collapsed')); - $form->add('wysiwyg', 'confirm_footer_text', ts('Footer Text'), $attributes['confirm_text'] + array('class' => 'collapsed')); + $form->add('wysiwyg', 'confirm_text', ts('Introductory Text'), $attributes['confirm_text'] + array('class' => 'collapsed', 'preset' => 'civievent')); + $form->add('wysiwyg', 'confirm_footer_text', ts('Footer Text'), $attributes['confirm_text'] + array('class' => 'collapsed', 'preset' => 'civievent')); } /** @@ -436,8 +436,8 @@ class CRM_Event_Form_ManageEvent_Registration extends CRM_Event_Form_ManageEvent public function buildThankYouBlock(&$form) { $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']); - $form->add('wysiwyg', 'thankyou_text', ts('Introductory Text'), $attributes['thankyou_text'] + array('class' => 'collapsed')); - $form->add('wysiwyg', 'thankyou_footer_text', ts('Footer Text'), $attributes['thankyou_text'] + array('class' => 'collapsed')); + $form->add('wysiwyg', 'thankyou_text', ts('Introductory Text'), $attributes['thankyou_text'] + array('class' => 'collapsed', 'preset' => 'civievent')); + $form->add('wysiwyg', 'thankyou_footer_text', ts('Footer Text'), $attributes['thankyou_text'] + array('class' => 'collapsed', 'preset' => 'civievent')); } /** diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index bd6c296c89..2e327bc813 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -257,6 +257,15 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base $this->addTask('Add column to allow for payment processors to set what card types are accepted', 'addAcceptedCardTypesField'); } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_4_7_14($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + $this->addTask('Add WYSIWYG Editor Presets', 'addWysiwygPresets'); + } /* * Important! All upgrade functions MUST add a 'runSql' task. @@ -882,4 +891,42 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_ return TRUE; } + /** + * CRM-19372 Add field to store accepted credit credit cards for a payment processor. + * @return bool + */ + public static function addWysiwygPresets() { + CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array( + 'name' => 'wysiwyg_presets', + 'title' => ts('WYSIWYG Editor Presets'), + 'is_reserved' => 1, + )); + $values = array( + 'default' => array('label' => ts('Default'), 'is_default' => 1), + 'civimail' => array('label' => ts('CiviMail'), 'component_id' => 'CiviMail'), + 'civievent' => array('label' => ts('CiviEvent'), 'component_id' => 'CiviEvent'), + ); + foreach ($values as $name => $value) { + civicrm_api3('OptionValue', 'create', $value + array( + 'options' => array('match' => array('name', 'option_group_id')), + 'name' => $name, + 'option_group_id' => 'wysiwyg_presets', + )); + } + $fileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-config.js'); + if (file_exists($fileName)) { + $config = file_get_contents($fileName); + $pos = strrpos($config, '};'); + $setting = "\n\tconfig.allowedContent = true;\n"; + $config = substr_replace($config, $setting, $pos, 0); + unlink($fileName); + } + else { + $config = "CKEDITOR.editorConfig = function( config ) {\n\tconfig.allowedContent = true;\n};\n"; + } + $newFileName = Civi::paths()->getPath('[civicrm.files]/persist/crm-ckeditor-default.js'); + file_put_contents($newFileName, $config); + return TRUE; + } + } diff --git a/ang/crmMailing/BodyHtml.html b/ang/crmMailing/BodyHtml.html index 730beecdd8..e3de42005c 100644 --- a/ang/crmMailing/BodyHtml.html +++ b/ang/crmMailing/BodyHtml.html @@ -14,7 +14,7 @@ Required vars: mailing crm-ui-insert-rx="insert:body_html" ng-model="mailing.body_html" ng-blur="checkTokens(mailing, 'body_html', 'insert:body_html')" - class="crm-wysiwyg-fullpage" + data-preset="civimail" >