Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
content_type_snippet_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/content_type/content_type_edit_fns.inc';
19 
34 {
35 
36 
41  function __construct()
42  {
43  parent::__construct();
44 
45  }//end constructor
46 
47 
59  function paint(&$asset, $editing=FALSE, $generating=FALSE)
60  {
61  $snippet_asset = $asset->getCurrentSnippet();
62  if ($generating) {
63  $this->paintGenerated($asset, $snippet_asset);
64  } else {
65  if (!is_null($snippet_asset)) {
66  $snippet_asset->printBody();
67  }
68  }
69 
70  return TRUE;
71 
72  }//end paint()
73 
74 
84  function paintGenerated(&$asset, &$snippet_asset)
85  {
86  // this is used when generating content_file.php
87  echo '<'.'?php'."\n";
88  if (!is_null($snippet_asset)) {
89  ?>
90  $snippet_assetid = '<?php echo $snippet_asset->id; ?>';
91  $snippet_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($snippet_assetid);
92  $snippet_asset->printBody();
93  <?php
94  }
95  echo '?'.'>'."\n";
96 
97  }//end paintGenerated()
98 
99 
109  function paintBackend(&$asset, $prefix)
110  {
111  if ($asset->writeAccess('attributes')) {
112 
113  // no access, do not change anything
114  $snippets = $asset->getSnippets();
115  if (empty($snippets)) {
116  echo translate('cms_ct_snippet_no_access');
117  return TRUE;
118  }
119 
120  // get default values
121  $source_safe = FALSE;
122  $current_view = $asset->attr('view_style');
123  $current_snippet = $asset->getCurrentSnippet();
124  $current_id = (is_null($current_snippet)) ? 0 : $current_snippet->id;
125  $current_content = translate('cms_ct_snippet_select_one');
126  if ($current_id) {
127  if (!isset($snippets[$current_id])) {
128  // no read access or snippet deleted
129  $current_content = translate('cms_ct_snippet_warning', get_asset_tag_line($current_id));
130  } else {
131  $current_content = $snippets[$current_id]['content'];
132  $source_safe = TRUE;
133  }
134  }
135 
136  // prepare the snippet and view style selection
137  $viewstyle = Array(
138  'source' => 'View source',
139  'html' => 'View HTML',
140  );
141  $dropdown = Array(0 => '--');
142  foreach ($snippets as $id => $info) {
143  $dropdown[$id] = $id.': '.$info['name'];
144  // print the content for quick switching later
145  hidden_field($prefix.'_hidden_source_'.$id, $info['type'] == 'content_type_nest_content' ? $info['content'] : nl2br(htmlspecialchars($info['content'])));
146  hidden_field($prefix.'_hidden_html_'.$id, $info['content']);
147  }
148 
149  // print the interface
150  // and the javascript for instant preview
151  $switchSnippet = 'onChange="switchSnippet(this.value)"';
152  $switchView = 'onChange="switchView(this.value)"';
153  ?>
154  <table class="sq-backend-table">
155  <tr>
156  <td class="sq-backend-table-cell-alt">
157  <?php echo combo_box($prefix.'_dropdown', $dropdown, FALSE, $current_id, 0, $switchSnippet); ?>
158  <?php echo combo_box($prefix.'_viewstyle', $viewstyle, FALSE, $current_view, 0, $switchView); ?>
159  </td>
160  </tr>
161  <tr>
162  <td>
163  <span id="<?php echo $prefix.'_displayarea'; ?>" name="<?php echo $prefix.'_displayarea'; ?>">
164  <?php
165  if ($current_view == 'source' && $source_safe) {
166  echo $snippets[$current_id]['type'] == 'content_type_nest_content' ? $current_content : nl2br(htmlspecialchars($current_content));
167  } else {
168  echo $current_content;
169  }
170  ?>
171  </span>
172  </td>
173  </tr>
174  </table>
175  <script type="text/javascript">
176  function switchSnippet(id) {
177  var span = document.getElementById('<?php echo $prefix.'_displayarea'; ?>');
178  var style = document.getElementById('<?php echo $prefix.'_viewstyle'; ?>');
179  if (id == 0) {
180  span.innerHTML = '<?php echo translate('cms_ct_snippet_select_one'); ?>';
181  return;
182  }
183  var snippet = document.getElementById(('<?php echo $prefix; ?>_hidden_' + style.value + '_' + id));
184  span.innerHTML = snippet.value;
185  }
186  function switchView(style) {
187  var span = document.getElementById('<?php echo $prefix.'_displayarea'; ?>');
188  var dropdown = document.getElementById('<?php echo $prefix.'_dropdown'; ?>');
189  if (dropdown.value == 0) {
190  return;
191  }
192  var snippet = document.getElementById(('<?php echo $prefix; ?>_hidden_' + style + '_' + dropdown.value));
193  span.innerHTML = snippet.value;
194  }
195  </script>
196  <?php
197  }//end if
198 
199  return TRUE;
200 
201  }//end paintBackend()
202 
203 
214  function processBackend($link, &$asset, $prefix)
215  {
216  if ($asset->writeAccess('attributes')) {
217 
218  // update view style and selected snippet
219  if (isset($_POST[$prefix.'_viewstyle'])) {
220  $asset->setAttrValue('view_style', $_POST[$prefix.'_viewstyle']);
221  $asset->saveAttributes();
222  }
223  $current_snippet = $asset->getCurrentSnippet();
224  $current_id = (is_null($current_snippet)) ? 0 : $current_snippet->id;
225  $new_id = (isset($_POST[$prefix.'_dropdown'])) ? $_POST[$prefix.'_dropdown'] : 0;
226 
227  if (!$new_id && (!$current_id || !$current_snippet->readAccess())) {
228  // no new id set, and does not have access to current snippet
229  // prevent accidental overwrite
230  return FALSE;
231  }
232 
233  if ($new_id) {
234  // user selected a different snippet
235  if ($new_id != $current_id) {
236  if ($current_id) {
237  // delete old snippet link
238  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, '', TRUE, 'snippet_asset');
239  if (!$asset->deleteLink($link['linkid'])) {
240  return FALSE;
241  }
242  }
243  $new_snippet = $GLOBALS['SQ_SYSTEM']->am->getAsset($new_id);
244  // create new snippet link
245  if (!$asset->createLink($new_snippet, SQ_LINK_NOTICE, 'snippet_asset')) {
246  return FALSE;
247  }
248  // special initial case, current = 0
249  if (!$current_id && $new_id) {
250  $asset->linksUpdated();
251  }
252  }//end if
253 
254  } else if ($current_id) {
255  // user unselected a snippet
256  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, '', TRUE, 'snippet_asset');
257  if (!$asset->deleteLink($link['linkid'])) {
258  return FALSE;
259  }
260  }
261 
262  // update content.php file
263  $bc_container_link = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE, 'minor');
264  if (!empty($bc_container_link)) {
265  $container = $GLOBALS['SQ_SYSTEM']->am->getAsset($bc_container_link[0]['majorid']);
266  $edit_fns = $container->getEditFns();
267  $edit_fns->generateContentFile($container);
268  }
269 
270  }//end if
271  return FALSE;
272 
273  }//end processBackend()
274 
275 
276 }//end class
277 
278 ?>