Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
content_type_wysiwyg_edit_fns.inc
1 <?php
18 require_once SQ_FUDGE_PATH.'/wysiwyg/wysiwyg.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/content_type/content_type_edit_fns.inc';
20 
33 {
34 
35 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor
45 
46 
56  protected function _createEditor(Content_Type_WYSIWYG $asset, $prefix)
57  {
58  // allowed plugins (shortened to $ap for easier use below)
59  $ap = explode('|', $GLOBALS['SQ_SYSTEM']->getUserPrefs($asset->type(), 'SQ_WYSIWYG_PLUGINS'));
60  // ignore allowed plugins (shortened to $iap for easier use);
61  $iap = in_array('all', $ap);
62 
63  $html = $asset->attr('html');
64 
65  if (empty($this->_tmp['wysiwg_object'])) {
66 
67  $wysiwyg = new WYSIWYG($prefix, sq_web_path('fudge').'/wysiwyg/');
68 
69  $wysiwyg_style = $GLOBALS['SQ_SYSTEM']->getUserPrefs($asset->type(), 'SQ_WYSIWYG_STYLE');
70  $wysiwyg->set_body_type($wysiwyg_style['body_type']);
71  $wysiwyg->set_init_onload(FALSE);
72  $wysiwyg->set_show_status_bar(FALSE);
73  $size_unit = '';
74  if (is_numeric($wysiwyg_style['width']) && isset($wysiwyg_style['units']['width'])) {
75  $size_unit = $wysiwyg_style['units']['width'];
76  }
77  $wysiwyg->set_width($wysiwyg_style['width'].$size_unit);
78  $size_unit = '';
79  if (is_numeric($wysiwyg_style['height']) && isset($wysiwyg_style['units']['height'])) {
80  $size_unit = $wysiwyg_style['units']['height'];
81  }
82  $wysiwyg->set_height($wysiwyg_style['height'].$size_unit);
83 
84  $base_php_self = str_replace('/'.SQ_CONF_BACKEND_SUFFIX, '', $_SERVER['PHP_SELF']);
85  $base_php_self = str_replace('/'.SQ_CONF_LIMBO_SUFFIX, '', $base_php_self);
86  $base_php_self = rtrim($base_php_self, '/ ');
87 
88  $wysiwyg->add_relative_href_check('(http[s]?://'.$_SERVER['HTTP_HOST'].')'.$_SERVER['PHP_SELF'], '$1'.$base_php_self);
89  $wysiwyg->add_relative_href_check('http[s]?://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?(\?a=[0-9]+)', './$1');
90  $wysiwyg->add_relative_href_check('([^.][^/])(\?a=[0-9]+)', '$1./$2');
91  $wysiwyg->add_relative_href_check('http[s]?://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?%([a-zA-Z_\-0-9]+)%', '%$1%');
92  $wysiwyg->add_absolute_url_check('\./\?(a=[0-9]+)', 'http://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?$1');
93 
94  // get the keyword replacements
95  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
96  $keywords = Array();
97  $keyword_vars = Array('keywords' => &$keywords);
98 
99  $em->broadcastEvent($asset, 'requestKeywords', $keyword_vars);
100 
101  $wysiwyg->open_group();
102 
103  if ($iap || in_array('format_text', $ap)) {
104  $wysiwyg->add_plugin('format_text');
105  }
106  if ($iap || in_array('replace_text', $ap)) {
107  $wysiwyg->add_plugin('replace_text');
108  }
109  if ($iap || in_array('search_and_replace', $ap)) {
110  $wysiwyg->add_plugin('search_and_replace');
111  }
112  if ($iap || in_array('format_font', $ap)) {
113  $wysiwyg->add_plugin('format_font');
114  }
115  if ($iap || in_array('general', $ap)) {
116  $wysiwyg->add_plugin('general');
117  }
118  if ($iap || in_array('s_script', $ap)) {
119  $wysiwyg->add_plugin('s_script');
120  }
121  $wysiwyg->close_group();
122 
123  $wysiwyg->open_group();
124  if ($iap || in_array('matrix_apply_style', $ap)) {
125  $wysiwyg->add_plugin('matrix_apply_style');
126  }
127  $wysiwyg->close_group();
128 
129  if ($iap || in_array('justify_text', $ap)) {
130  $wysiwyg->add_plugin('justify_text');
131  }
132  if (($iap || in_array('keyword_replace', $ap)) && !empty($keywords)) {
133  $wysiwyg->add_plugin('keyword_replace');
134  $keyword_replace =& $wysiwyg->get_plugin('keyword_replace');
135  $keyword_replace->keyword_tag = '%';
136  $keyword_replace->keywords = $keywords;
137  }
138 
139  // snippet global keyword dropdown
140  $snippets = $GLOBALS['SQ_SYSTEM']->am->getSnippetKeywords();
141  if (($iap || in_array('snippet_keyword_replace', $ap)) && !empty($snippets)) {
142  $wysiwyg->add_plugin('snippet_keyword_replace');
143  $snippet_keyword_replace =& $wysiwyg->get_plugin('snippet_keyword_replace');
144  $snippet_keyword_replace->keyword_tag = '%';
145  $snippet_keyword_replace->keywords = $snippets;
146  }
147 
148  if ($iap || in_array('history', $ap)) {
149  $wysiwyg->add_plugin('history');
150  }
151  if ($iap || in_array('indenting', $ap)) {
152  $wysiwyg->add_plugin('indenting');
153  }
154  if ($iap || in_array('listing', $ap)) {
155  $wysiwyg->add_plugin('listing');
156  }
157  if ($iap || in_array('table_editing', $ap)) {
158  $wysiwyg->add_plugin('table_editing');
159  }
160  if ($iap || in_array('select_color', $ap)) {
161  $wysiwyg->add_plugin('select_color');
162  }
163 
164  $wysiwyg->open_group();
165  if ($iap || in_array('matrix_insert_link', $ap) || in_array('insert_link', $ap)) {
166  $wysiwyg->add_plugin('matrix_insert_link');
167  }
168  if ($iap || in_array('matrix_insert_quick_link', $ap)) {
169  $wysiwyg->add_plugin('matrix_insert_quick_link');
170  }
171  if ($iap || in_array('matrix_insert_anchor', $ap) || in_array('insert_anchor', $ap)) {
172  $wysiwyg->add_plugin('matrix_insert_anchor');
173  }
174  if ($iap || in_array('matrix_insert_image', $ap) || in_array('insert_image', $ap)) {
175  $wysiwyg->add_plugin('matrix_insert_image');
176  }
177  if ($iap || in_array('matrix_embed_movie', $ap)) {
178  $wysiwyg->add_plugin('matrix_embed_movie');
179  }
180  if ($iap || in_array('matrix_embed_youtube', $ap)) {
181  $wysiwyg->add_plugin('matrix_embed_youtube');
182  }
183  $wysiwyg->close_group();
184 
185  $wysiwyg->open_group();
186  if ($iap || in_array('matrix_accessibility', $ap)) {
187  $wysiwyg->add_plugin('matrix_accessibility');
188  }
189  $wysiwyg->close_group();
190 
191  // html_tidy needs to be last so other plugins can set options
192  if ($iap || in_array('html_tidy', $ap)) {
193  $wysiwyg->add_plugin('html_tidy');
194  }
195 
196  if ($iap || in_array('matrix_expand_keywords', $ap)) {
197  $wysiwyg->add_plugin('matrix_expand_keywords');
198  }
199 
200  $wysiwyg->open_group();
201  if ($iap || in_array('special_chars', $ap)) {
202  $wysiwyg->add_plugin('special_chars');
203  }
204  $wysiwyg->close_group();
205 
206  $wysiwyg->open_group();
207  if ($iap || in_array('choose_language', $ap)) {
208  $wysiwyg->add_plugin('choose_language');
209  }
210  $wysiwyg->close_group();
211 
212  $wysiwyg->open_group();
213  if ($iap || in_array('definition_list', $ap)) {
214  $wysiwyg->add_plugin('definition_list');
215  }
216  $wysiwyg->close_group();
217 
218  $wysiwyg->open_group();
219  if ($iap || in_array('misc_accessibility', $ap)) {
220  $wysiwyg->add_plugin('misc_accessibility');
221  }
222  if ($iap || in_array('quotation', $ap)) {
223  $wysiwyg->add_plugin('quotation');
224  }
225  $wysiwyg->close_group();
226 
227  $wysiwyg->open_group();
228  if ($iap || in_array('visual_aid', $ap)) {
229  $wysiwyg->add_plugin('visual_aid');
230  }
231  $wysiwyg->close_group();
232 
233  $wysiwyg->open_group();
234  if ($iap || in_array('spell_checker', $ap)) {
235  $wysiwyg->add_plugin('spell_checker');
236  }
237  if ($iap || in_array('text_indicator', $ap)) {
238  $wysiwyg->add_plugin('text_indicator');
239  }
240  if ($iap || in_array('view_source', $ap)) {
241  $wysiwyg->add_plugin('view_source');
242  }
243  $wysiwyg->close_group();
244 
245  $this->_tmp['wysiwg_object'] =& $wysiwyg;
246 
247  }//end if WYSIWYG object doesn't already exist
248 
249  // contents must be set AFTER all plugins are loaded
250  $this->_tmp['wysiwg_object']->set_contents($html);
251 
252  // set dir attribute to include in wysiwyg iframe
253  if(!empty($this->_tmp['dir_attr']))
254  $this->_tmp['wysiwg_object']->set_dir_attr($this->_tmp['dir_attr']);
255 
256  return $this->_tmp['wysiwg_object'];
257 
258  }//end _createEditor()
259 
260 
270  public function paintBackend(Content_Type_WYSIWYG $asset, $prefix)
271  {
272  if ($asset->writeAccess('attributes')) {
273  $wysiwyg = $this->_createEditor($asset, $prefix);
274  ?>
275  <div style="display:none;" id="<?php echo $prefix?>_wysiwyg_div"><?php echo $wysiwyg->paint(); ?></div>
276  <div id="<?php echo $prefix?>_contents_div" onclick="editDiv=document.getElementById('<?php echo $prefix ?>_wysiwyg_div'); if (editDiv.style.display == 'none') {switchEditingMode('<?php echo $prefix ?>_contents_div','<?php echo $prefix ?>_wysiwyg_div', editor_<?php echo $prefix ?>);}"><?php echo $wysiwyg->contents ?></div>
277  <?php
278  }
279  return TRUE;
280 
281  }//end paintBackend()
282 
283 
293  public function paintBackendIcons(Content_Type_WYSIWYG $asset, $prefix)
294  {
295  Bodycopy_Edit_Fns::printBodycopyIcon("switchEditingMode('".$prefix."_contents_div','".$prefix."_wysiwyg_div', editor_".$prefix.');', 'Edit Contents', 'Switch between preview and edit mode', 'edit_mode', '[Edit Contents]');
296 
297  }//end paintBackendIcons()
298 
299 
309  public function paintBackendStatusIcons(Content_Type_WYSIWYG $asset, $prefix)
310  {
311  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
312 
313  // prints the status of the last run of htmltidy tool as a tag icon
314  if (!SQ_TOOL_HTML_TIDY_ENABLED || !file_exists(SQ_TOOL_HTML_TIDY_PATH)) {
315  // tool is not enabled
316  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Status', translate('wysiwyg_htmltidy_status_disabled'), 'htmltidy_disabled', '[HTMLTidy Status]');
317  } else {
318 
319  $status = $asset->attr('htmltidy_status');
320  $status_msg = translate('wysiwyg_htmltidy_status_wait');
321  $image_filename = 'htmltidy_'.$status;
322  switch ($status) {
323  case 'pass':
324  $status_msg = translate('wysiwyg_htmltidy_status_pass');
325  break;
326  case 'fail':
327  $status_msg = translate('wysiwyg_htmltidy_status_fail');
328  break;
329  case 'wait':
330  $status_msg = translate('wysiwyg_htmltidy_status_wait');
331  break;
332  }//end switch
333  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Status', $status_msg, $image_filename, '[HTMLTidy Status]');
334 
335  // Do not remove the line below, they are 'new line' seperated for a reason, that is how htmltidy_errors need to be seperated so it does not break the javascript underneath in printBodycopyIcon() call.
336  $res = (str_replace('
337 ', '<br />', htmlentities($asset->attr('htmltidy_errors'), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET)));
338 
339  $htmltidy_accessibility_filename = 'accessibility_'.(empty($res) ? 'pass' : 'fail');
340  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Accessibility', !empty($res) ? $res : translate('wysiwyg_htmltidy_accessibility_no_warning'), $htmltidy_accessibility_filename, '[HTMLTidy Status]');
341 
342  }
343 
344  }//end paintBackendStatusIcons()
345 
346 
357  public function paint(Content_Type_WYSIWYG $asset, $editing=FALSE, $generating=FALSE)
358  {
359  $html = $asset->attr('html');
360  $html = preg_replace('|(<htmlarea_special[^>]*?>)(.*?)(</htmlarea_special>)|si', '$2', $html);
361  if ($generating) {
362  // escape dangerous content
363  require_once SQ_FUDGE_PATH.'/general/text.inc';
364  escape_php($html);
365  echo $html;
366  return TRUE;
367  }
368 
369  if ($editing) {
370  // We are in the edit interface, so print the interface and content
371  $this->_paintInterface($html, $asset);
372  } else {
373  // Just print content
374  echo $html;
375  }//end if
376 
377  }//end paint()
378 
379 
390  public function processBackend(Array $link, Content_Type_WYSIWYG $asset, $prefix)
391  {
392  if (!$asset->writeAccess('attributes')) return FALSE;
393 
394  $wysiwyg = $this->_createEditor($asset, $prefix);
395  $html = $wysiwyg->process();
396  if ($html === FALSE) return FALSE;
397 
398  if (!isset($_POST['ees_content_no_filter'])) {
399  // grab and store the last run status of htmltidy plugin
400  // to be used when we print the corresponding status icon
401  $html_tidy =& $wysiwyg->get_plugin('html_tidy');
402  if (!is_null($html_tidy)) {
403  $asset->setAttrValue('htmltidy_status', $html_tidy->htmltidy_status);
404  $asset->setAttrValue('htmltidy_errors', $html_tidy->htmltidy_errors);
405  }//end if
406  }
407 
408  return $asset->setAttrValue('html', $html);
409 
410  }//end processBackend()
411 
412 
421  public function setDirAttr($dir_attribute)
422  {
423  $this->_tmp['dir_attr'] = $dir_attribute;
424 
425  }//end setDirAttr()
426 
427 
428 }//end class
429 
430 ?>