Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bodycopy.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 
30 class Bodycopy extends Asset
31 {
32 
37  public $keyword_replacements = Array();
38 
39 
46  function __construct($assetid=0)
47  {
48  parent::__construct($assetid);
49 
50  }//end constructor
51 
52 
59  public function lockTypes()
60  {
61  $lock_types = parent::lockTypes();
62  $lock_types['content'] = ($lock_types['attributes'] | $lock_types['links']);
63  return $lock_types;
64 
65  }//end lockTypes()
66 
67 
80  public function create(Array &$link, $bc_data=Array())
81  {
82  // store this data for later use during the create process in _createAdditional()
83  $this->_tmp['bodycopy_create_data'] = $bc_data;
84 
85  return parent::create($link);
86 
87  }//end create()
88 
89 
100  protected function _preCreateCheck(Array $link)
101  {
102  if (!parent::_preCreateCheck($link)) return FALSE;
103 
104  $name = trim($this->attr('name'));
105  if ($name == '') {
106  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
107  return FALSE;
108  }
109 
110  return TRUE;
111 
112  }//end _preCreateCheck()
113 
114 
125  protected function _createAdditional(Array &$link)
126  {
127  // grab the bodycopy data that we stored in the create() function
128  $bc_data = $this->_tmp['bodycopy_create_data'];
129  unset($this->_tmp['bodycopy_create_data']);
130 
131  if (!parent::_createAdditional($link)) return FALSE;
132 
133  // create a DIV
134  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy_div');
135  $bodycopy_div = new Bodycopy_DIV();
136  $copy_link = Array('asset' => &$this, 'link_type' => SQ_LINK_TYPE_2, 'value' => '', 'sort_order' => 1, 'is_dependant' => 1);
137  $bodycopy_div->setAttrValue('name', 'Content DIV');
138  $create_success = $bodycopy_div->create($copy_link, $bc_data);
139  return $create_success;
140 
141  }//end _createAdditional()
142 
143 
164  public function prepareLink(Asset $asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
165  {
166  // if this is a bodycopy container then we need to make it a dependant link
167  if ($side_of_link == 'major' && ($asset instanceof Bodycopy_Container) && $dependant != '1') {
168  $dependant = '1';
169  $link_type = SQ_LINK_TYPE_2;
170  return TRUE;
171  }
172 
173  return FALSE;
174 
175  }//end prepareLink()
176 
177 
185  public function _getAllowedLinks()
186  {
187  return Array(
188  SQ_LINK_TYPE_2 => Array(
189  'bodycopy_container' => Array(
190  'card' => 'M',
191  'exclusive' => FALSE,
192  ),
193  ),
194  );
195 
196  }//end _getAllowedLinks()
197 
198 
208  function processStatusChange($new_status, $update_parents=TRUE)
209  {
210  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
211 
212  // make sure our data directories exists
213  if (!create_directory($this->data_path)) {
214  trigger_localised_error('CORE0049', E_USER_WARNING, $this->name);
215  return FALSE;
216  }
217 
218  switch ($new_status) {
219  case SQ_STATUS_EDITING:
220  // Going to safe edit
221  if ($this->status == SQ_STATUS_LIVE) {
222  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
223  file_put_contents($this->data_path.'/.safe_edit_before', serialize($containers));
224  file_put_contents($this->data_path.'/.safe_edit_after', serialize($containers));
225  }//end if
226  break;
227  case SQ_STATUS_LIVE:
228  if ($this->status == SQ_STATUS_EDITING) {
229  // Cancel safe edit
230  $before_safe_edit = Array();
231  $after_safe_edit = Array();
232  $before_safe_edit_string = file_to_string($this->data_path.'/.safe_edit_before');
233  $after_safe_edit_string = file_to_string($this->data_path.'/.safe_edit_after');
234 
235  if (!empty($before_safe_edit_string)) {
236  $before_safe_edit = @unserialize($before_safe_edit_string);
237  }//end if
238  if (!empty($after_safe_edit_string)) {
239  $after_safe_edit = @unserialize($after_safe_edit_string);
240  }//end if
241 
242  // Remove DIVs if they did not exist before going back to Safe Edit
243  if (!empty($after_safe_edit)) {
244  foreach ($after_safe_edit as $active_link) {
245  if (!$this->_compareLink($active_link, $before_safe_edit)) {
246  // The link didn't exist, move to trash
247  if (isset($active_link['linkid'])) {
248  // Drop to a run level to allow the links to be shuffled out without a fuss
249  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
250  $this->deleteLink($active_link['linkid']);
251  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
252  }//end if
253  }//end if
254  }//end foreach
255  }//end if
256 
257  // Coming out of safe edit remove safe edit files
258  $this->_cleanupSafeEdit();
259  }//end if
260 
261  if ($this->status == SQ_STATUS_EDITING_APPROVED) {
262  // Changes approved
263  $this->_cleanupSafeEdit();
264  }//end if
265  break;
266  }//end switch
267 
268  return parent::processStatusChange($new_status, $update_parents);
269 
270  }//end processStatusChange()
271 
272 
281  public function paintBackend(Backend_Outputter $o)
282  {
283  if (!isset($_REQUEST['asset_ei_screen'])) {
284  return parent::paintBackend($o);
285  }
286  switch ($_REQUEST['asset_ei_screen']) {
287  case 'show_popup' :
288  $include_file = trim($_REQUEST['popup_file']);
289  $include_dir = dirname(__FILE__).'/popups/';
290  $dir_listing = scandir($include_dir);
291 
292  if (in_array($include_file, $dir_listing)) {
293  require_once($include_dir.$include_file);
294  }
295  exit();
296  default :
297  return parent::paintBackend($o);
298 
299  }//end switch
300 
301  }//end paintBackend()
302 
303 
310  public function printFrontend()
311  {
312  if (!isset($_REQUEST['asset_ei_screen'])) {
313  return parent::printFrontend();
314  }
315  switch ($_REQUEST['asset_ei_screen']) {
316  case 'show_popup' :
317  $include_file = trim($_REQUEST['popup_file']);
318  $include_dir = dirname(__FILE__).'/popups/';
319  $dir_listing = scandir($include_dir);
320 
321  if (in_array($include_file, $dir_listing)) {
322  require_once($include_dir.$include_file);
323  }
324  exit();
325  default :
326  return parent::printFrontend();
327 
328  }//end switch
329 
330  }//end printFrontend()
331 
332 
339  public function printBody()
340  {
341  // Bodycopy no longer handles the use of system version file, such uses is now being handled by containers.
342  // But For the sake of the upgrading, check for existing file in the bodycopy data dir.
343 
344  if (is_file($this->data_path.'/.content_file.php') && $this->useSystemVersion()) {
345  // create an array of urls that the content file is
346  // going to try and use to work out file urls, link hrefs etc
347  $sq_asset_url_list = Array();
348 
349  $notice_links = unserialize(file_to_string($this->data_path.'/.notice_links'));
350 
351  $sq_asset_url_list = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($notice_links));
352  for (reset($sq_asset_url_list); NULL !== ($minorid = key($sq_asset_url_list)); next($sq_asset_url_list)) {
353  if ($sq_asset_url_list[$minorid] == '') {
354  $sq_asset_url_list[$minorid] = './?a='.$minorid;
355  }
356  }
357  ob_start();
358  include($this->data_path.'/.content_file.php');
359  $content = ob_get_contents();
360  ob_end_clean();
361  replace_keywords($content, $this->keyword_replacements);
362 
363  echo $content;
364 
365  } else {
366  // get all the containers linked to this bodycopy
367  $containers =& $this->getContainers();
368 
369  // paint each container
370  foreach (array_keys($containers) as $i) {
371  $container =& $containers[$i];
372  $container->printBody($this->keyword_replacements);
373  }
374  }
375 
376  return TRUE;
377 
378  }//end printBody()
379 
380 
391  public function _getName($short_name=FALSE, $contextid=NULL)
392  {
393  // No context specified, using the current context
394  if ($contextid === NULL) {
395  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
396  }//end if
397 
398  // Obtain the attribute value for Name from the specified context
399  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
400  if (empty($values) === TRUE) {
401  return parent::_getName($short_name, $contextid);
402  } else {
403  return $values[$this->id];
404  }
405 
406  }//end _getName()
407 
408 
417  public function &getContainers()
418  {
419  if (SQ_IN_BACKEND || SQ_IN_LIMBO || !isset($this->_tmp['containers'])) {
420  $this->_tmp['containers'] = Array();
421  $container_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
422  foreach ($container_links as $container_data) {
423  $this->_tmp['containers'][] = $GLOBALS['SQ_SYSTEM']->am->getAsset($container_data['minorid'], $container_data['minor_type_code']);
424  }
425  }
426  return $this->_tmp['containers'];
427 
428  }//end getContainers()
429 
430 
437  public function isEmpty()
438  {
439  $am =& $GLOBALS['SQ_SYSTEM']->am;
440  $containers = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'bodycopy_container', FALSE);
441  foreach ($containers as $container_data) {
442  $container = $am->getAsset($container_data['minorid'], $container_data['minor_type_code']);
443  if (!$container->isEmpty()) return FALSE;
444  }
445  return TRUE;
446 
447  }//end isEmpty()
448 
449 
456  public function getKeywords()
457  {
458  require_once SQ_FUDGE_PATH.'/general/text.inc';
459  $content = $this->getRawBodycopyContent();
460  $keywords = retrieve_keywords_replacements($content);
461  return $keywords;
462 
463  }//end getKeywords()
464 
465 
472  public function getContent()
473  {
474  return $this->getRawBodycopyContent();
475 
476  }//end getContent()
477 
478 
485  public function getRawBodycopyContent()
486  {
487  $containers =& $this->getContainers();
488  $contents = '';
489 
490  ob_start();
491  foreach (array_keys($containers) as $i) {
492  $container =& $containers[$i]; // need this by reference
493  $edit = $container->getEditFns();
494  $edit->paint($container, FALSE, TRUE);
495  }
496  $contents = ob_get_contents();
497  ob_end_clean();
498 
499  return $contents;
500 
501  }//end getRawBodycopyContent()
502 
503 
512  public function setKeywordReplacements($replacements=Array())
513  {
514  if (!is_array($replacements)) return FALSE;
515  if (empty($replacements)) return FALSE;
516 
517  $this->keyword_replacements = $replacements;
518  return TRUE;
519 
520  }//end setKeywordReplacements()
521 
522 
532  function _compareLink($current_link, $before_links)
533  {
534  if (!isset($current_link['minorid'])) {
535  // Not enough information to proceed
536  return FALSE;
537  }//end if
538 
539  foreach ($before_links as $before_link) {
540  if (isset($before_link['minorid'])) {
541  if ($before_link['minorid'] == $current_link['minorid']) {
542  // It exists!
543  return TRUE;
544  }//end if
545  }//end if
546  }//end foreach
547 
548  // Didn't find one, report that...
549  return FALSE;
550 
551  }//end _compareLink()
552 
553 
560  function _cleanupSafeEdit()
561  {
562  if (file_exists($this->data_path.'/.safe_edit_after')) {
563  unlink($this->data_path.'/.safe_edit_after');
564  }//end if
565  if (file_exists($this->data_path.'/.safe_edit_before')) {
566  unlink($this->data_path.'/.safe_edit_before');
567  }//end if
568 
569  }//end _cleanupSafeEdit()
570 
571 
572 }//end class
573 
574 ?>