Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bodycopy_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/var_serialise/var_serialise.inc';
20 
21 
34 {
35 
36 
41  function __construct()
42  {
43  parent::__construct();
44 
45  unset($this->static_screens['workflow']);
46  unset($this->static_screens['metadataSchemas']);
47  unset($this->static_screens['metadata']);
48  unset($this->static_screens['linking']);
49  unset($this->static_screens['settings']);
50  unset($this->static_screens['tagging']);
51 
52  }//end constructor
53 
54 
64  protected function _saveCssIds(Backend_Outputter $o, $mute_errors = FALSE)
65  {
66  $am =& $GLOBALS['SQ_SYSTEM']->am;
67 
68  if (isset($GLOBALS['sq_preview_url']) && !empty($GLOBALS['sq_preview_url'])) {
69  $url = $GLOBALS['sq_preview_url'];
70  $url = preg_replace('|^http[s]?://|', '', $url);
71 
72  $designid = $am->getValueFromURL($url, 'design::%frontend%', TRUE);
73 
74  if ($designid) {
75  $design = $am->getAsset($designid);
76  $stylesheets = $design->getStylesheets();
77 
78  // save the ids of all the css files into a global var so the wysiwyg style
79  // drop down plugin can access this information without another querey
80  // we can also limit the stylesheets that are available to it here if we want
81  $GLOBALS['sq_bodycopy_cssids'] = array_keys($stylesheets);
82 
83  $am->forgetAsset($design);
84  unset($design);
85 
86  } else {
87  if (!$mute_errors) {
88  $o->note(translate('bodycopy_asset_has_no_design', $GLOBALS['sq_preview_url']));
89  }
90  }
91  } else {
92  if (!$mute_errors) {
93  $o->note(translate('bodycopy_asset_has_no_preview'));
94  }
95  }
96 
97  }//end _saveCssIds()
98 
99 
110  public function paintBodycopy(Bodycopy $asset, Backend_Outputter $o, $prefix)
111  {
112  $am =& $GLOBALS['SQ_SYSTEM']->am;
113  $write_access = $asset->writeAccess('content');
114  // variable to determine whether or not to allow commit
115  $print_commit_button = $write_access;
116 
117  // save css ids into global var
118  $this->_saveCssIds($o);
119 
120  // get all the containers linked to this bodycopy
121  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
122  if (!$write_access) {
123  ob_start();
124  foreach ($containers as $container_data) {
125  $container = $am->getAsset($container_data['minorid'], $container_data['minor_type_code']);
126  if ($container->useSystemVersion()) {
127  $keyword_replacements = Array(); //Do not want to replace keywords
128  $container->printBody($keyword_replacements);
129  } else {
130  $container_edit = $container->getEditFns();
131  $container_edit->paintContainer($container, $o, $prefix);
132  }
133  }
134  $html = ob_get_clean();
135  $html = preg_replace('|<script\b[^>]*>(.*?)</script>|is', '', $html);
136  echo $html;
137  return FALSE;
138  }
139 
140  // the initial width and height to have the layer display created at;
141  $init_layer_width = 500;
142  $init_layer_height = 500;
143 
144  // all available content types
145  $content_types = Array();
146  require_once SQ_CORE_PACKAGE_PATH.'/content_type/content_type.inc';
148  foreach ($types as $type => $type_data) {
149  $content_types[$type] = Array('name' => str_replace(' Content Type', '', $type_data['name']));
150  }
151 
152  ?>
153  <input type="hidden" name="bodycopy_action" value="">
154  <input type="hidden" name="bodycopy_name" value="">
155  <input type="hidden" name="bodycopy_data" value="">
156 
157  <script type="text/javascript">
158 
159  // php vars
160  var backendHref = "<?php echo $asset->getBackendHref('show_popup');?>";
161  var init_layer_width = <?php echo $init_layer_width?>;
162  var init_layer_height = <?php echo $init_layer_height?>;
163  var bodycopy_types = '<?php echo var_serialise($content_types, TRUE)?>';
164  var _prefix = "<?php echo $prefix; ?>";
165  var asset_id = "<?php echo $asset->id; ?>";
166  </script>
167 
168  <script type="text/javascript" src="<?php echo sq_web_path('data').'/asset_types/bodycopy/js/bodycopy_edit_fns.js'?>"></script>
169  <script type="text/javascript" src="<?php echo sq_web_path('fudge').'/var_serialise/var_serialise.js'?>" ></script>
170  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/js/detect.js';?>"></script>
171  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/js/layer_handler.js';?>"></script>
172  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/js/general.js';?>"></script>
173  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/js/debug.js';?>"></script>
174  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/html_form/html_form.js';?>"></script>
175 
176 
177  <?php
178  // print JS functions for each of the container types we have installed
179  $container_types = $am->getTypeDescendants('bodycopy_container');
180  foreach ($container_types as $type) {
181  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type);
182  $type_asset = new $type();
183  $edit_fns = $type_asset->getEditFns();
184  $edit_fns->paintGenericBackend($asset, $o, $prefix);
185  }
186 
187  $container_type_options = Array();
188  $default = '';
189  foreach ($container_types as $type) {
190  $code = strtolower(substr($type,9));
191  $container_type_options[$code] = ucwords(str_replace('_', ' ', substr($type,9)));
192  if (empty($default)) $default = $code;
193  }
194  asort($container_type_options);
195  echo hidden_field($prefix.'_insert_container_type', $default);
196  ?>
197 
198  <style type="text/css">
199  #bodycopyPopupDiv {
200  color: #ffffff;
201  position: absolute;
202  left: 100;
203  top: 120;
204  width: <?php echo $init_layer_width?>;
205  height: <?php echo $init_layer_height?>;
206  clip: rect(0,600,600,0);
207  visibility: hidden;
208  }
209  #bodycopyToolTipDiv {
210  position: absolute;
211  width: 1px;
212  height: 1px;
213  visibility: hidden;
214  }
215  </style>
216 
217 
218  <!-- Begin Popup -->
219  <div id="bodycopyPopupDiv" style="z-index:1001">
220  <iframe name="bodycopyFrame" id="bodycopyFrame" scrolling="no" width="<?php echo $init_layer_width?>" height="<?php echo $init_layer_height?>" marginwidth=0 marginheight=0 frameborder="no"></iframe>
221  </div>
222  <!-- End Popup -->
223 
224  <table border="0" cellspacing="0" cellpadding="0" class="bodytext">
225 
226  <?php
227 
228  // require library functions needed for drawing the bodycopy
229  $type_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo('bodycopy');
230 
231  // for each container, print it's editing interface
232  $i = 0;
233  foreach ($containers as $container_data) {
234  $container = $am->getAsset($container_data['minorid'], $container_data['minor_type_code']);
235  $container_edit = $container->getEditFns();
236 
237  if ($write_access) {
238  ?>
239  <tr>
240  <td>
241  <input type="hidden" name="bodycopy_saved[<?php echo $prefix?>][<?php echo $container->id?>]" value="">
242  <table border="0" cellspacing="0" cellpadding="0">
243  <tr>
244  <?php
245  if ( $GLOBALS['SQ_SYSTEM']->getUserPrefs('bodycopy_container', 'SQ_DIV_DISABLE_CREATE') === 'yes'){
246  ?>
247  <td colspan="2"><span class="sq-backend-warning"><?php echo translate('bodycopy_pref_cannot_insert_new'); ?></span></td>
248  <?php
249  }
250  else {
251  ?>
252  <td>
253  <?php
254  Bodycopy_Edit_Fns::printBodycopyIcon("document.main_form.{$prefix}_insert_container_type.value = document.main_form.{$prefix}_insert_container_type_before_{$i}.value; bodycopy_insert_container('$prefix', $i, true)", translate('insert_container'), translate('select_container_type'), 'create_container', '['.translate('insert').']');
255  ?>
256  </td>
257  <td class="sq-backend-data" valign="top">
258  <?php
259  echo '&nbsp;&nbsp;'.translate('create_new').' ';
260  echo combo_box($prefix.'_insert_container_type_before_'.$i, $container_type_options, FALSE, '');
261  ?>
262  </td>
263  <?php
264  }
265  ?>
266  </tr>
267  </table>
268  </td>
269  </tr>
270  <tr>
271  <td>&nbsp;</td>
272  </tr>
273  <?php
274  }//end if write access
275 
276  ?>
277  <tr>
278  <td><?php
279 
280  if ($container_edit->paintContainer($container, $o, $prefix) == TRUE) {
281  $print_commit_button = TRUE;
282  }
283  ?></td>
284  </tr>
285  <tr>
286  <td>&nbsp;</td>
287  </tr>
288  <?php
289 
290  $i++;
291  }//end foreach container
292 
293  if ($write_access) {
294  ?>
295  <tr>
296  <td>
297  <table border="0" cellspacing="0" cellpadding="0">
298  <tr>
299  <?php
300  if ( $GLOBALS['SQ_SYSTEM']->getUserPrefs('bodycopy_container', 'SQ_DIV_DISABLE_CREATE') === 'yes'){
301  ?>
302  <td colspan="2"><span class="sq-backend-warning"><?php echo translate('bodycopy_pref_cannot_insert_new'); ?></span></td>
303  <?php
304  }
305  else {
306  ?>
307  <td>
308  <?php
309  Bodycopy_Edit_Fns::printBodycopyIcon("document.main_form.{$prefix}_insert_container_type.value = document.main_form.{$prefix}_insert_container_type_after_{$i}.value; bodycopy_insert_container('$prefix', $i, false)", translate('insert_container'), translate('select_container_type'), 'create_container', '['.translate('insert').']');
310  ?>
311  </td>
312  <td class="sq-backend-data" valign="top">
313  <?php
314  echo '&nbsp;&nbsp;'.translate('create_new').' ';
315  echo combo_box($prefix.'_insert_container_type_after_'.$i, $container_type_options, FALSE, '');
316  ?>
317  </td>
318  <?php
319  }
320  ?>
321  </tr>
322  </table>
323  </td>
324  </tr>
325  <?php
326  }//end if write access
327 
328  ?>
329  </table>
330 
331  <?php
332 
333  return $print_commit_button;
334 
335  }//end paintBodycopy()
336 
337 
348  public function processBodycopy(Bodycopy $asset, Backend_Outputter $o, $prefix)
349  {
350  $am =& $GLOBALS['SQ_SYSTEM']->am;
351 
352  $updated = FALSE;
353 
354  // save css ids into global var
355  // we dont want note to be displayed so we mute errors
356  $this->_saveCssIds($o,TRUE);
357 
358  // do some thing for the first time we process a bodycopy on this page
359  if (empty($this->_tmp['update_initialised'])) {
360  $bc_action = (empty($_POST['bodycopy_action'])) ? '' : $_POST['bodycopy_action'];
361  $bc_name = (empty($_POST['bodycopy_name'])) ? '' : $_POST['bodycopy_name'];
362  $bc_data = (empty($_POST['bodycopy_data'])) ? Array() : $_POST['bodycopy_data'];
363  $bc_saved = (empty($_POST['bodycopy_saved'])) ? Array() : $_POST['bodycopy_saved'];
364 
365  if (empty($bc_data) && empty($bc_saved)) {
366  return FALSE;
367  }
368 
369  // if there is some bodycopy data, deserialise it into the arrays
370  // so that everything else can use it
371  if ($bc_data) $bc_data = var_unserialise($bc_data);
372 
373  // unserialize the data for a bodycopy.
374  for (reset($bc_saved); NULL !== ($k = key($bc_saved)); next($bc_saved)) {
375  $containers =& $bc_saved[$k];
376  for (reset($containers); NULL !== ($containersid = key($containers)); next($containers)) {
377  $containers[$containersid] = var_unserialise($containers[$containersid]);
378  }
379  }
380 
381  unset($containers);
382 
383  $this->_tmp['update_initialised'] = TRUE;
384  }
385 
386  // get all the containers linked to this bodycopy
387  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
388 
389  // process each of the containers
390  foreach ($containers as $container_data) {
391  $container = $am->getAsset($container_data['minorid'], $container_data['minor_type_code']);
392  $container_edit = $container->getEditFns();
393  if ($container_edit->processContainer($container, $o, $prefix, $bc_action, $bc_name, $bc_data, $bc_saved)) {
394  if ($container->saveAttributes()) $updated = TRUE;
395  }
396  }
397 
398  // only update if we are the bodycopy that submitted the form
399  if ($bc_name == $prefix || isset($bc_saved[$prefix])) {
400  // perform some bodycopy level actions
401  if ($bc_name == $prefix) {
402  switch ($bc_action) {
403  case 'insert_container' :
404  if ($this->insertContainer($asset, $bc_data['container_type'], $bc_data['containerid'], $bc_data)) {
405  $updated = TRUE;
406  }
407  break;
408 
409  case 'delete_container' :
410  if ($this->deleteContainer($asset, $bc_data['containerid'])) {
411  $asset->_updated();
412  $updated = TRUE;
413  }
414  break;
415  }//end switch
416  }
417  }
418 
419  return $updated;
420 
421  }//end processBodycopy()
422 
423 
435  public function insertContainer(Bodycopy $asset, $type, $containerid, Array $bc_data)
436  {
437  // Save the current setup of links, if it has not been saved already
438  if ($asset->status & SQ_SC_STATUS_SAFE_EDITING) {
439  if (!file_exists($asset->data_path.'/.safe_edit_before')) {
440  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
441  file_put_contents($asset->data_path.'/.safe_edit_before', serialize($containers));
442  }//end if
443  }//end if
444 
445  // get all the containers linked to this bodycopy
446  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
447  $num_containers = count($containers);
448 
449  $containerid = intval($containerid);
450 
451  // if the containerid is greater than the number of containers we have, set it to the last one
452  if ($containerid > $num_containers - 1) {
453  $containerid = $num_containers - 1;
454  } else if ($containerid < 0) {
455  $divid = 0;
456  }
457 
458  // if they want to insert after the passed containerid what
459  // they really mean is to insert before the next containerid
460  if (!$bc_data['before']) $containerid++;
461 
462  // now add the container
463  $copy_link = Array('asset' => &$asset, 'link_type' => SQ_LINK_TYPE_2, 'value' => '', 'sort_order' => $containerid, 'is_dependant' => 1);
464 
465  $asset_type = 'bodycopy_'.$type;
466  $GLOBALS['SQ_SYSTEM']->am->includeAsset($asset_type);
467  $new_container = new $asset_type();
468  // Bug #5460 New DIVs created in Safe Edit showing on front end before going Live. See Bodycopy_Container::saveSystemVersion().
469  $new_container->_tmp['skip_save_system_version_content_file'] = TRUE;
470 
471  $new_container->setAttrValue('name', 'New '.ucfirst($type));
472  $new_container->setAttrValue('attributes', $bc_data['attributes']);
473  $retval = (bool) $new_container->create($copy_link, $bc_data);
474  if ($retval && (!isset($bc_data['attributes']['identifier']) || trim($bc_data['attributes']['identifier']) == '')) {
475  // try and update the name to be unique - but dont stress if we couldn't update it
476  $new_container->setAttrValue('name', 'New '.ucfirst($type).' '.$new_container->id);
477  $new_container->saveAttributes();
478  }
479 
480  // Tweaking this asset is under safe edit and has a workflow on it
481  if ($asset->status & SQ_SC_STATUS_SAFE_EDITING) {
482  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
483  $schemas = $wfm->getSchemas($asset->id, TRUE);
484  if (!empty($schemas) && $new_container->status != SQ_STATUS_EDITING) {
485  // Drop to a run level to force the status change to SQ_STATUS_EDITING (saveSystemVersion() so it can go back to LIVE)
486  $GLOBALS['SQ_SYSTEM']->setRunLevel(191);
487  $new_container->saveSystemVersion();
488  $new_container->processStatusChange(SQ_STATUS_EDITING, FALSE);
489  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($new_container->id);
490  if (!empty($children)) {
491  foreach (array_keys($children) as $child_assetid) {
492  $child = $GLOBALS['SQ_SYSTEM']->am->getAsset($child_assetid, '', TRUE);
493  if (!is_null($child)) {
494  $child->saveSystemVersion();
495  $child->processStatusChange(SQ_STATUS_EDITING, FALSE);
496  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($child);
497  }//end if
498  }//end foreach
499  }//end if
500  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
501  }//end if
502  }//end if
503 
504  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($new_container);
505 
506  // Save the current setup of links
507  if ($asset->status & SQ_SC_STATUS_SAFE_EDITING) {
508  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
509  file_put_contents($asset->data_path.'/.safe_edit_after', serialize($containers));
510  }//end if
511 
512  return $retval;
513 
514  }//end insertContainer()
515 
516 
526  public function deleteContainer(Bodycopy $asset, $containerid)
527  {
528  $am =& $GLOBALS['SQ_SYSTEM']->am;
529 
530  // get all the containers linked to this bodycopy
531  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
532  foreach ($containers as $container_data) {
533  if ($container_data['minorid'] == $containerid) {
534  // found the container to delete
535  return $asset->deleteLink($container_data['linkid']);
536  }
537  }
538 
539  return FALSE;
540 
541  }//end deleteContainer()
542 
543 
559  public static function printBodycopyIcon($onclick, $heading, $desc, $icon, $alt='', $extra='', $width='16', $height='16')
560  {
561  $path = sq_web_path('data').'/asset_types/bodycopy/images/icons/'.$icon.'.png';
562  $extra .= ' onmouseover="if (bodycopy_initialised) tooltip.show(this, \''.addslashes(htmlentities($desc, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET)).'\', \''.addslashes(htmlentities($heading, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET)).'\')"';
563  $extra .= ' onmouseout="if (bodycopy_initialised) tooltip.hide()"';
564  $extra .= ' onclick="'.$onclick.'"';
565  $extra .= ' align="absmiddle" ';
566  if (!empty($onclick)) {
567  $extra .= ' style="cursor:pointer;"';
568  }
569  sq_print_icon($path, $width, $height, $alt, '', $extra);
570 
571  }//end printBodycopyIcon()
572 
573 
574 }//end class
575 
576 
577 ?>