Convert CK Config form to quickform
authorColeman Watts <coleman@civicrm.org>
Thu, 28 May 2020 20:26:47 +0000 (16:26 -0400)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 19 Aug 2020 06:16:45 +0000 (16:16 +1000)
CRM/Admin/Form/CKEditorConfig.php
templates/CRM/Admin/Form/CKEditorConfig.tpl

index fd1e5c8ba93e7f3331707b79661ac33d9a6f8b1b..7a65a9537a618d7f9b02b126e73e58c7276520a6 100644 (file)
@@ -42,23 +42,11 @@ class CRM_Admin_Form_CKEditorConfig extends CRM_Core_Form {
     'filebrowserFlashUploadUrl',
   ];
 
-  public $preset;
-
   /**
-   * Run page.
-   *
-   * @return string
+   * Prepare form
    */
   public function preProcess() {
-    $this->preset = CRM_Utils_Array::value('preset', $_REQUEST, 'default');
-    // If the form was submitted, take appropriate action.
-    if (!empty($_POST['revert'])) {
-      self::deleteConfigFile($this->preset);
-      self::setConfigDefault();
-    }
-    elseif (!empty($_POST['config'])) {
-      $this->save($_POST);
-    }
+    CRM_Utils_Request::retrieve('preset', 'String', $this, FALSE, 'default', 'GET');
 
     $settings = $this->getConfigSettings();
 
@@ -76,15 +64,14 @@ class CRM_Admin_Form_CKEditorConfig extends CRM_Core_Form {
         'settings' => $settings,
       ]);
 
-    $configUrl = self::getConfigUrl($this->preset) ?: self::getConfigUrl('default');
+    $configUrl = self::getConfigUrl($this->get('preset')) ?: self::getConfigUrl('default');
 
-    $this->assign('preset', $this->preset);
+    $this->assign('preset', $this->get('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', $configUrl);
-    $this->assign('revertConfirm', htmlspecialchars(ts('Are you sure you want to revert all changes?', ['escape' => 'js'])));
 
     CRM_Utils_System::appendBreadCrumb([
       [
@@ -92,8 +79,45 @@ class CRM_Admin_Form_CKEditorConfig extends CRM_Core_Form {
         'title' => ts('Display Preferences'),
       ],
     ]);
+  }
 
-    return parent::preProcess();
+  /**
+   * Build form
+   */
+  public function buildQuickForm() {
+    $revertConfirm = json_encode(ts('Are you sure you want to revert all changes?'));
+    $this->addButtons([
+      [
+        'type' => 'next',
+        'name' => ts('Save'),
+      ],
+      [
+        'type' => 'cancel',
+        'name' => ts('Cancel'),
+      ],
+      [
+        'type' => 'refresh',
+        'name' => ts('Revert to Default'),
+        'icon' => 'fa-undo',
+        'js' => ['onclick' => "return confirm($revertConfirm);"],
+      ],
+    ]);
+  }
+
+  /**
+   * Handle form submission
+   */
+  public function postProcess() {
+    if (!empty($_POST[$this->getButtonName('refresh')])) {
+      self::deleteConfigFile($this->get('preset'));
+      self::setConfigDefault();
+    }
+    else {
+      if (!empty($_POST[$this->getButtonName('next')])) {
+        $this->save($_POST);
+      }
+      CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/setting/preferences/display', ['reset' => 1]));
+    }
   }
 
   /**
@@ -126,17 +150,14 @@ class CRM_Admin_Form_CKEditorConfig extends CRM_Core_Form {
           $key = preg_replace('/^config_/', 'config.', $key);
           $setting = "\n\t{$key} = {$val};\n";
           $config = substr_replace($config, $setting, $pos, 0);
-        } catch (CRM_Core_Exception $e) {
-          if (!empty($params['save'])) {
-            CRM_Core_Session::setStatus(ts("Error saving %1.", [1 => $key]), ts('Invalid Value'), 'error');
-          }
+        }
+        catch (CRM_Core_Exception $e) {
+          CRM_Core_Session::setStatus(ts("Error saving %1.", [1 => $key]), ts('Invalid Value'), 'error');
         }
       }
     }
-    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');
-    }
+    self::saveConfigFile($this->get('preset'), $config);
+    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 +215,7 @@ class CRM_Admin_Form_CKEditorConfig extends CRM_Core_Form {
    */
   private function getConfigSettings() {
     $matches = $result = [];
-    $file = self::getConfigFile($this->preset) ?: self::getConfigFile('default');
+    $file = self::getConfigFile($this->get('preset')) ?: self::getConfigFile('default');
     $result['skin'] = 'moono';
     if ($file) {
       $contents = file_get_contents($file);
index ad179df5ab7afbbfb83d2a7fbf01d69c61e41fe7..ae51de9d1815e995e00fa3657cd572b547b4f2a1 100644 (file)
     border-bottom: 0 none;
     padding: 3px 10px 1px !important;
   }
-  .crm-config-option-row span.crm-error:after {
-    font-family: FontAwesome;
-    content: " \f071 Invalid JSON"
-  }
 {/literal}</style>
 {* Force the custom config file to reload by appending a new query string *}
 <script type="text/javascript">
@@ -64,7 +60,7 @@
     {/foreach}
   </ul>
 </div>
-<form method="post" action="{crmURL}" id="toolbarModifierForm">
+<div id="toolbarModifierForm">
   <fieldset>
     <div class="crm-block crm-form-block">
       <label for="skin">{ts}Skin{/ts}</label>
@@ -89,7 +85,6 @@
       </div>
     </div>
 
-
     <div class="crm-block crm-form-block">
       <fieldset>
         <legend>{ts}Advanced Options{/ts}</legend>
       </fieldset>
     </div>
 
-    <div class="crm-submit-buttons">
-      <span class="crm-button">
-        <i class="crm-i fa-wrench" aria-hidden="true"></i> <input type="submit" value="{ts}Save{/ts}" name="save" class="crm-form-submit" accesskey="S"/>
-      </span>
-      <span class="crm-button">
-        <i class="crm-i fa-times" aria-hidden="true"></i> <input type="submit" value="{ts}Revert to Default{/ts}" name="revert" class="crm-form-submit" onclick="return confirm('{$revertConfirm}');"/>
-      </span>
-    </div>
-    <input type="hidden" value="{$preset}" name="preset" />
+    <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
   </fieldset>
-</form>
+</div>
 <script type="text/template" id="config-row-tpl">
   <div class="crm-config-option-row">
     <input class="huge crm-config-option-name" placeholder="{ts}Option{/ts}"/>