Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
content_type_markdown_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/content_type/content_type_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42 
43  }//end constructor
44 
45 
55  function paintBackend(&$asset, $prefix)
56  {
57  if ($asset->writeAccess('attributes')) {
58  $size = $GLOBALS['SQ_SYSTEM']->getUserPrefs($asset->type(), 'SQ_MARKDOWN_SIZE');
59  $markdown = $asset->attr('markdown');
60  $markdownify_not_configured_message = '';
61  if ($markdown == '') {
62  $html = $asset->attr('html');
63  if (trim($html) != '') {
64  $is_markdownified = FALSE;
65  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
66  require_once SQ_FUDGE_PATH.'/general/text.inc';
67  if (SQ_TOOL_MARKDOWNIFY_ENABLED && is_file(SQ_TOOL_MARKDOWNIFY_PATH)) {
68  require_once SQ_TOOL_MARKDOWNIFY_PATH;
69  $class_name = basename(SQ_TOOL_MARKDOWNIFY_PATH, '.php');
70  if (method_exists($class_name, 'parseString')) {
71  $markdownify = new $class_name;
72 
73  // Keep the html 'pure' only make changes on safe_html
74  $safe_html = $html;
75 
76  // Find the keywords and make them 'markdown-proof'
77  $keywords = retrieve_keywords_replacements($html);
78  foreach ($keywords as $keyword) {
79  $safe_keyword = '%'.str_replace('_', '[MY3SEP]', $keyword).'%';
80  $safe_html = str_replace('%'.$keyword.'%', $safe_keyword, $safe_html);
81  }//end foreach
82 
83  // Convert HTML to Markdown
84  $markdown = $markdownify->parseString($safe_html);
85 
86  // Change the safe keywords back to normal keywords
87  foreach ($keywords as $keyword) {
88  $safe_keyword = '%'.str_replace('_', '[MY3SEP]', $keyword).'%';
89  $markdown = str_replace($safe_keyword, '%'.$keyword.'%', $markdown);
90  }//end foreach
91  $is_markdownified = TRUE;
92  }
93  }
94 
95  //if can not markdownify, keep the HTML value
96  if (!$is_markdownified) {
97  $markdown = $html;
98  $markdownify_not_configured_message = translate('cms_content_type_markdown_markdownify_tool_not_configured');
99  }
100 
101  }//end if (trim($html) != '')
102  }//end if ($markdown == '')
103 
104 
105  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
106  $keywords = Array();
107  $prepared_keywords = Array();
108  $keyword_vars = Array('keywords' => &$keywords);
109  $em->broadcastEvent($asset, 'requestKeywords', $keyword_vars);
110  if (!empty($keywords)) {
111  foreach ($keywords as $key => $name) {
112  $prepared_keywords['%'.$key.'%'] = $name;
113  }
114  keyword_selector($prepared_keywords, $prefix.'_markdown');
115  }
116 
117  //print a notice if markdownify is not configured when it is needed
118  if ($markdownify_not_configured_message != '') {
119  echo '<span class="sq-backend-warning">'.$markdownify_not_configured_message.'</span><br />';
120  }
121 
122  //print a notice if markdown is not configured
123  $markdown_configured = FALSE;
124  if (SQ_TOOL_MARKDOWN_ENABLED && is_file(SQ_TOOL_MARKDOWN_PATH)) {
125  require_once SQ_TOOL_MARKDOWN_PATH;
126  if (function_exists('Markdown')) {
127  $markdown_configured = TRUE;
128  }
129  }
130  if (!$markdown_configured) {
131  echo '<span class="sq-backend-warning">'.translate('cms_content_type_markdown_markdown_tool_not_configured').'</span><br />';
132  }
133 
134  //print editor
135  text_area($prefix.'_markdown', $markdown, '', '', 0, 'style="width: '.$size['width'].'; height: '.$size['height'].'; font-family: \'Courier New\', Courier, monospace; display: block"');
136  }
137  return TRUE;
138 
139  }//end paintBackend()
140 
141 
152  function processBackend($link, &$asset, $prefix)
153  {
154  if ($asset->writeAccess('attributes')) {
155  if (isset($_POST[$prefix.'_markdown'])) {
156  $markdown = $_POST[$prefix.'_markdown'];
157  $current_markdown = $asset->attr('markdown');
158  if ($markdown != $current_markdown) {
159  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
160  require_once SQ_FUDGE_PATH.'/general/text.inc';
161  if (SQ_TOOL_MARKDOWN_ENABLED && is_file(SQ_TOOL_MARKDOWN_PATH)) {
162  require_once SQ_TOOL_MARKDOWN_PATH;
163  if (function_exists('Markdown')) {
164  // Keep the markdown 'pure' only make changes on safe_markdown
165  $safe_markdown = $markdown;
166 
167  // Find the keywords and make them 'markdown-proof'
168  $keywords = retrieve_keywords_replacements($markdown);
169  foreach ($keywords as $keyword) {
170  $safe_keyword = '%'.str_replace('_', '[MY3SEP]', $keyword).'%';
171  $safe_markdown = str_replace('%'.$keyword.'%', $safe_keyword, $safe_markdown);
172  }//end foreach
173 
174  // Convert Markdown to HTML
175  $html = Markdown($safe_markdown);
176 
177  // Change the safe keywords back to normal keywords
178  foreach ($keywords as $keyword) {
179  $safe_keyword = '%'.str_replace('_', '[MY3SEP]', $keyword).'%';
180  $html = str_replace($safe_keyword, '%'.$keyword.'%', $html);
181  }//end foreach
182 
183  // Save the changes
184  if ($asset->setAttrValue('markdown', $markdown) && $asset->setAttrValue('html', $html)) {
185  return TRUE;
186  }
187  }
188  } else {
189  trigger_localised_error('CMS0107', E_USER_WARNING);
190  }
191 
192  }//end if
193  }
194  }
195  return FALSE;
196 
197  }//end processBackend()
198 
199 
200 }//end class
201 
202 ?>