Commit | Line | Data |
---|---|---|
7266e09b CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
7266e09b | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
7266e09b CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
7266e09b CW |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * Page for configuring CKEditor options. |
7266e09b CW |
36 | * |
37 | * Note that while this is implemented as a CRM_Core_Page, it is actually a form. | |
47b59d65 | 38 | * Because the form needs to be submitted and refreshed via javascript, it seemed like |
7266e09b CW |
39 | * Quickform and CRM_Core_Form/Controller might get in the way. |
40 | */ | |
41 | class CRM_Admin_Page_CKEditorConfig extends CRM_Core_Page { | |
42 | ||
7ad5ae6a | 43 | const CONFIG_FILEPATH = '[civicrm.files]/persist/crm-ckeditor-'; |
7266e09b CW |
44 | |
45 | /** | |
3b7ceeb2 | 46 | * Settings that cannot be configured in "advanced options" |
7266e09b CW |
47 | * |
48 | * @var array | |
49 | */ | |
3b7ceeb2 CW |
50 | public $blackList = array( |
51 | 'on', | |
52 | 'skin', | |
53 | 'extraPlugins', | |
54 | 'toolbarGroups', | |
55 | 'removeButtons', | |
56 | 'filebrowserBrowseUrl', | |
57 | 'filebrowserImageBrowseUrl', | |
58 | 'filebrowserFlashBrowseUrl', | |
59 | 'filebrowserUploadUrl', | |
60 | 'filebrowserImageUploadUrl', | |
61 | 'filebrowserFlashUploadUrl', | |
7266e09b CW |
62 | ); |
63 | ||
7ad5ae6a CW |
64 | public $preset; |
65 | ||
7266e09b | 66 | /** |
ce064e4f | 67 | * Run page. |
68 | * | |
7266e09b CW |
69 | * @return string |
70 | */ | |
71 | public function run() { | |
7ad5ae6a CW |
72 | $this->preset = CRM_Utils_Array::value('preset', $_REQUEST, 'default'); |
73 | ||
7266e09b CW |
74 | // If the form was submitted, take appropriate action. |
75 | if (!empty($_POST['revert'])) { | |
7ad5ae6a | 76 | self::deleteConfigFile($this->preset); |
7266e09b CW |
77 | } |
78 | elseif (!empty($_POST['config'])) { | |
79 | $this->save($_POST); | |
80 | } | |
81 | ||
3b7ceeb2 CW |
82 | $settings = $this->getConfigSettings(); |
83 | ||
7266e09b | 84 | CRM_Core_Resources::singleton() |
68e7ff1c | 85 | ->addScriptFile('civicrm', 'bower_components/ckeditor/ckeditor.js', 0, 'page-header') |
7266e09b CW |
86 | ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/fulltoolbareditor.js', 1) |
87 | ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/abstracttoolbarmodifier.js', 2) | |
88 | ->addScriptFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/js/toolbarmodifier.js', 3) | |
89 | ->addScriptFile('civicrm', 'js/wysiwyg/admin.ckeditor-configurator.js', 10) | |
90 | ->addStyleFile('civicrm', 'bower_components/ckeditor/samples/toolbarconfigurator/css/fontello.css') | |
91 | ->addStyleFile('civicrm', 'bower_components/ckeditor/samples/css/samples.css') | |
92 | ->addVars('ckConfig', array( | |
93 | 'plugins' => array_values($this->getCKPlugins()), | |
3b7ceeb2 CW |
94 | 'blacklist' => $this->blackList, |
95 | 'settings' => $settings, | |
7266e09b CW |
96 | )); |
97 | ||
7ad5ae6a CW |
98 | $configUrl = self::getConfigUrl($this->preset); |
99 | if (!$configUrl) { | |
100 | $configUrl = self::getConfigUrl('default'); | |
101 | } | |
102 | ||
103 | $this->assign('preset', $this->preset); | |
104 | $this->assign('presets', CRM_Core_OptionGroup::values('wysiwyg_presets', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name')); | |
7266e09b | 105 | $this->assign('skins', $this->getCKSkins()); |
3b7ceeb2 CW |
106 | $this->assign('skin', CRM_Utils_Array::value('skin', $settings)); |
107 | $this->assign('extraPlugins', CRM_Utils_Array::value('extraPlugins', $settings)); | |
7ad5ae6a | 108 | $this->assign('configUrl', $configUrl); |
7266e09b CW |
109 | $this->assign('revertConfirm', htmlspecialchars(ts('Are you sure you want to revert all changes?', array('escape' => 'js')))); |
110 | ||
111 | CRM_Utils_System::appendBreadCrumb(array(array( | |
112 | 'url' => CRM_Utils_System::url('civicrm/admin/setting/preferences/display', 'reset=1'), | |
113 | 'title' => ts('Display Preferences'), | |
114 | ))); | |
115 | ||
116 | return parent::run(); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Generate the config js file based on posted data. | |
121 | * | |
122 | * @param array $params | |
123 | */ | |
124 | public function save($params) { | |
125 | $config = "/**\n" | |
126 | . " * CKEditor config file auto-generated by CiviCRM.\n" | |
127 | . " *\n" | |
128 | . " * Note: This file will be overwritten if settings are modified at:\n" | |
129 | . " * @link " . CRM_Utils_System::url(CRM_Utils_System::currentPath(), NULL, TRUE, NULL, FALSE) . "\n" | |
130 | . " */\n\n" | |
131 | // Standardize line-endings | |
132 | . preg_replace('~\R~u', "\n", $params['config']); | |
133 | ||
3b7ceeb2 CW |
134 | // Use all params starting with config_ |
135 | foreach ($params as $key => $val) { | |
136 | $val = trim($val); | |
137 | if (strpos($key, 'config_') === 0 && strlen($val)) { | |
b3e9532a | 138 | if ($val != 'true' && $val != 'false' && $val != 'null' && $val[0] != '{' && $val[0] != '[' && !is_numeric($val)) { |
3b7ceeb2 CW |
139 | $val = json_encode($val); |
140 | } | |
7266e09b | 141 | $pos = strrpos($config, '};'); |
3b7ceeb2 CW |
142 | $key = preg_replace('/^config_/', 'config.', $key); |
143 | $setting = "\n\t{$key} = {$val};\n"; | |
7266e09b CW |
144 | $config = substr_replace($config, $setting, $pos, 0); |
145 | } | |
146 | } | |
7ad5ae6a | 147 | self::saveConfigFile($this->preset, $config); |
7266e09b CW |
148 | if (!empty($params['save'])) { |
149 | CRM_Core_Session::setStatus(ts("You may need to clear your browser's cache to see the changes in CiviCRM."), ts('CKEditor Saved'), 'success'); | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
47b59d65 | 154 | * Get available CKEditor plugin list. |
ce064e4f | 155 | * |
7266e09b CW |
156 | * @return array |
157 | */ | |
158 | private function getCKPlugins() { | |
159 | $plugins = array(); | |
47b59d65 | 160 | $pluginDir = Civi::paths()->getPath('[civicrm.root]/bower_components/ckeditor/plugins'); |
7266e09b CW |
161 | |
162 | foreach (glob($pluginDir . '/*', GLOB_ONLYDIR) as $dir) { | |
163 | $dir = rtrim(str_replace('\\', '/', $dir), '/'); | |
164 | $name = substr($dir, strrpos($dir, '/') + 1); | |
165 | $dir = CRM_Utils_file::addTrailingSlash($dir, '/'); | |
166 | if (is_file($dir . 'plugin.js')) { | |
167 | $plugins[$name] = array( | |
168 | 'id' => $name, | |
169 | 'text' => ucfirst($name), | |
170 | 'icon' => NULL, | |
171 | ); | |
172 | if (is_dir($dir . "icons")) { | |
173 | if (is_file($dir . "icons/$name.png")) { | |
174 | $plugins[$name]['icon'] = "bower_components/ckeditor/plugins/$name/icons/$name.png"; | |
175 | } | |
176 | elseif (glob($dir . "icons/*.png")) { | |
177 | $icon = CRM_Utils_Array::first(glob($dir . "icons/*.png")); | |
178 | $icon = rtrim(str_replace('\\', '/', $icon), '/'); | |
179 | $plugins[$name]['icon'] = "bower_components/ckeditor/plugins/$name/icons/" . substr($icon, strrpos($icon, '/') + 1); | |
180 | } | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | return $plugins; | |
186 | } | |
187 | ||
188 | /** | |
47b59d65 | 189 | * Get available CKEditor skins. |
ce064e4f | 190 | * |
7266e09b CW |
191 | * @return array |
192 | */ | |
193 | private function getCKSkins() { | |
194 | $skins = array(); | |
47b59d65 | 195 | $skinDir = Civi::paths()->getPath('[civicrm.root]/bower_components/ckeditor/skins'); |
7266e09b CW |
196 | foreach (glob($skinDir . '/*', GLOB_ONLYDIR) as $dir) { |
197 | $dir = rtrim(str_replace('\\', '/', $dir), '/'); | |
198 | $skins[] = substr($dir, strrpos($dir, '/') + 1); | |
199 | } | |
200 | return $skins; | |
201 | } | |
202 | ||
203 | /** | |
3b7ceeb2 | 204 | * @return array |
7266e09b | 205 | */ |
3b7ceeb2 CW |
206 | private function getConfigSettings() { |
207 | $matches = $result = array(); | |
7ad5ae6a | 208 | $file = self::getConfigFile($this->preset); |
b3e9532a CW |
209 | if (!$file) { |
210 | $file = self::getConfigFile('default'); | |
211 | } | |
3b7ceeb2 | 212 | $result['skin'] = 'moono'; |
7266e09b CW |
213 | if ($file) { |
214 | $contents = file_get_contents($file); | |
3b7ceeb2 CW |
215 | preg_match_all("/\sconfig\.(\w+)\s?=\s?([^;]*);/", $contents, $matches); |
216 | foreach ($matches[1] as $i => $match) { | |
217 | $result[$match] = trim($matches[2][$i], ' "\''); | |
7266e09b CW |
218 | } |
219 | } | |
3b7ceeb2 | 220 | return $result; |
7266e09b CW |
221 | } |
222 | ||
223 | /** | |
7ad5ae6a CW |
224 | * @param string $preset |
225 | * Omit to get an array of all presets | |
226 | * @return array|null|string | |
7266e09b | 227 | */ |
7ad5ae6a CW |
228 | public static function getConfigUrl($preset = NULL) { |
229 | $items = array(); | |
230 | $presets = CRM_Core_OptionGroup::values('wysiwyg_presets', FALSE, FALSE, FALSE, NULL, 'name'); | |
231 | foreach ($presets as $key => $name) { | |
232 | if (self::getConfigFile($name)) { | |
233 | $items[$name] = Civi::paths()->getUrl(self::CONFIG_FILEPATH . $name . '.js', 'absolute'); | |
234 | } | |
7266e09b | 235 | } |
7ad5ae6a | 236 | return $preset ? CRM_Utils_Array::value($preset, $items) : $items; |
7266e09b CW |
237 | } |
238 | ||
239 | /** | |
7ad5ae6a | 240 | * @param string $preset |
7266e09b CW |
241 | * |
242 | * @return null|string | |
243 | */ | |
7ad5ae6a CW |
244 | public static function getConfigFile($preset = 'default') { |
245 | $fileName = Civi::paths()->getPath(self::CONFIG_FILEPATH . $preset . '.js'); | |
246 | return is_file($fileName) ? $fileName : NULL; | |
7266e09b CW |
247 | } |
248 | ||
249 | /** | |
250 | * @param string $contents | |
251 | */ | |
7ad5ae6a CW |
252 | public static function saveConfigFile($preset, $contents) { |
253 | $file = Civi::paths()->getPath(self::CONFIG_FILEPATH . $preset . '.js'); | |
7266e09b CW |
254 | file_put_contents($file, $contents); |
255 | } | |
256 | ||
257 | /** | |
ce064e4f | 258 | * Delete config file. |
7266e09b | 259 | */ |
7ad5ae6a CW |
260 | public static function deleteConfigFile($preset) { |
261 | $file = self::getConfigFile($preset); | |
7266e09b CW |
262 | if ($file) { |
263 | unlink($file); | |
264 | } | |
265 | } | |
266 | ||
267 | } |