Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bodycopy_container.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
60  public function create(Array &$link, Array $bc_data=Array())
61  {
62  // store this data for later use during the create process in _createAdditional()
63  $this->_tmp['bodycopy_create_data'] = $bc_data;
64 
65  return parent::create($link);
66 
67  }//end create()
68 
69 
80  protected function _preCreateCheck(Array &$link)
81  {
82  if (!parent::_preCreateCheck($link)) return FALSE;
83 
84  $name = trim($this->attr('name'));
85  if ($name == '') {
86  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
87  return FALSE;
88  }
89 
90  return TRUE;
91 
92  }//end _preCreateCheck()
93 
94 
101  public function lockTypes()
102  {
103  $lock_types = parent::lockTypes();
104  $lock_types['content'] = ($lock_types['attributes'] | $lock_types['links']);
105  return $lock_types;
106 
107  }//end lockTypes()
108 
109 
119  public function paintBackend(Backend_Outputter $o)
120  {
121  if (!isset($_REQUEST['asset_ei_screen'])) {
122  return parent::paintBackend($o);
123  }
124 
125  switch ($_REQUEST['asset_ei_screen']) {
126  case 'show_popup' :
127  // Find out where the popups are located. (under bodycopy)
128  $type_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo('bodycopy');
129 
130  $include_file = trim($_REQUEST['popup_file']);
131  $bodycopy_popup_dir = SQ_SYSTEM_ROOT.'/'.$type_info['dir'].'/popups/';
132  $dir_listing = scandir($bodycopy_popup_dir);
133 
134  if (in_array($include_file, $dir_listing)) {
135  require_once($bodycopy_popup_dir.$include_file);
136  }
137 
138  exit(0);
139  default :
140  return parent::paintBackend($o);
141 
142  }//end switch
143 
144  }//end paintBackend()
145 
146 
155  public function saveSystemVersion()
156  {
157  if (!parent::saveSystemVersion()) return FALSE;
158  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
159 
160  // Bug #5460: New DIVs created in Safe Edit showing on front end before going Live.
161  // There is no content yet but the DIV markup shows up and can cause issues if CSS is applied.
162  // So lets not copy the content file over to .sq_system directory.
163  if (!isset($this->_tmp['skip_save_system_version_content_file'])) {
164  // save a version of the container as it appears now
165  $edit = $this->getEditFns();
166  if (!$edit->generateContentFile($this)) {
167  trigger_localised_error('CORE0047', E_USER_WARNING, $this->name);
168  return FALSE;
169  }
170 
171  $contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
172  foreach ($contexts as $contextid => $context_data) {
173  $filename_suffix = ((int)$contextid === 0) ? '' : '.'.$contextid;
174  if (file_exists($this->data_path.'/content_file'.$filename_suffix.'.php') === TRUE) {
175  $copy_usv = copy_file($this->data_path.'/content_file'.$filename_suffix.'.php', $this->data_path.'/.sq_system/.content_file'.$filename_suffix.'.php');
176 
177  }
178  }
179  } else {
180  unset($this->_tmp['skip_save_system_version_content_file']);
181  }
182 
183  // save the current notice links
184  $replace_assetids = Array();
185  $notice_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
186 
187  foreach ($notice_links as $link) {
188  $replace_assetids[] = $link['minorid'];
189  }
190 
191  // save the links we currently use (./?a=xx)
192  if (!string_to_file(serialize($replace_assetids), $this->data_path.'/.sq_system/.notice_links')) {
193  trigger_localised_error('CORE0048', E_USER_WARNING, $this->name);
194  return FALSE;
195  }
196 
197  return TRUE;
198 
199  }//end saveSystemVersion()
200 
201 
211  public function processStatusChange($new_status, $update_parent=TRUE)
212  {
213  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
214  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
215 
216  $old_status = $this->status;
217  if (!parent::processStatusChange($new_status, $update_parent)) {
218  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
219  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
220  return FALSE;
221  }
222 
223  // if we have just gone live from safe editing, we need to
224  // create our notice links again so we dont have leftovers from
225  // when we were safe editing and thus not deleting links we removed
226  // Same thing goes cancelling a safe edit (going back to live)
227  if ($this->status == SQ_STATUS_LIVE && ($old_status & SQ_SC_STATUS_SAFE_EDITING)) {
228  $edit = $this->getEditFns();
229  if (!$edit->createContentLinks($this)) {
230  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
231  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
232  return FALSE;
233  }
234  }
235 
236  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
237  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
238  return TRUE;
239 
240  }//end processStatusChange()
241 
242 
263  public function prepareLink(Asset $asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
264  {
265  // if this is a content type then we need to make it a dependant link
266  if ($side_of_link == 'major' && ($asset instanceof Content_Type) && $dependant != '1' && ($link_type & SQ_SC_LINK_SIGNIFICANT)) {
267  $dependant = '1';
268  return TRUE;
269  }
270 
271  return FALSE;
272 
273  }//end prepareLink()
274 
275 
284  public function deleteLink($linkid)
285  {
286  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
287  if (!parent::deleteLink($linkid)) return FALSE;
288 
289  // link has been deleted, check urls to see if this link
290  // was a HREF or SRC etc and get rid of them
291  ob_start();
292  $edit_fns = $this->getEditFns();
293  $edit_fns->paint($this, TRUE);
294  $output = ob_get_contents();
295  ob_end_clean();
296 
297  // NOTE: don't need to worry about shadow assets here because we never link to them anyway
298  $e = '/\\.\\/\\?a=([0-9]+)/';
299  $matches = Array();
300  preg_match_all($e, $output, $matches);
301  $replace_assetids = $matches[1];
302 
303  return TRUE;
304 
305  }//end deleteLink()
306 
307 
315  public function _getAllowedLinks()
316  {
317  return Array(
318  SQ_LINK_TYPE_2 => Array(
319  'content_type' => Array(
320  'card' => 'M',
321  'exclusive' => FALSE,
322  ),
323  ),
324  SQ_LINK_NOTICE => Array(
325  'asset' => Array(
326  'card' => 'M',
327  'exclusive' => FALSE,
328  ),
329  ),
330  );
331 
332  }//end _getAllowedLinks()
333 
334 
343  public function describeLink($linkid)
344  {
345  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
346  if ($link['link_type'] == SQ_LINK_NOTICE) {
347  return 'Linked inside the content as a URL, image, or embedded movie';
348  }
349  return parent::describeLink($linkid);
350 
351  }//end describeLink()
352 
353 
364  public function printBody($keyword_replacements=NULL)
365  {
366  if (!$this->readAccess()) return;
367 
368  // if we don't have any keyword replacements passed as an argument,
369  // check if we really don't have any keywords in the contents because in some rare cases,
370  // such as an asset listing that lists bodycopies and use %asset_contents% in the type format,
371  // this function is called without an array that contains keyword replacements which makes all the
372  // keywords disappear from the contents printed on the frontend
373  if (is_null($keyword_replacements)) {
374  $keyword_replacements = Array();
375  // find if we have any keywords in the content
376  ob_start();
377  $edit = $this->getEditFns();
378  $edit->paint($this, FALSE, TRUE);
379  $temp_raw_content = ob_get_contents();
380  ob_end_clean();
381  $keywords = retrieve_keywords_replacements($temp_raw_content);
382 
383  // if we have keywords, attempt to get replacement values for them
384  if (!empty($keywords)) {
385  $dependant_parent_assetids = $GLOBALS['SQ_SYSTEM']->am->getDependantParents($this->id, '', FALSE, FALSE);
386 
387  // if we know the one and only dependant parent that itself is not a dependant
388  // then we can replace both global and asset keywords
389  if (count($dependant_parent_assetids) === 1) {
390  $dependant_parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset(array_pop($dependant_parent_assetids));
391  foreach ($keywords as $keyword) {
392  $keyword_replacements[$keyword] = $dependant_parent_asset->getKeywordReplacement($keyword);
393  }
394  } else if (count($dependant_parent_assetids) > 1) {
395  // if we found multiple dependant parents (by new linking Content DIV, etc) then
396  // we can have global keywords later on by passing the keywords as they are but
397  // not asset keyword as we don't know which asset to invoke getKeywordReplacement on.
398  foreach ($keywords as $keyword) {
399  if (0 === strpos($keyword, 'globals_')) {
400  $keyword_replacements[$keyword] = "%$keyword%";
401  }
402  }
403  }
404  }
405  }
406 
407  // Escape the double quotes in the keyword
408  foreach($keyword_replacements as $keyword => $val) {
409  $safe_keyword = htmlentities($keyword);
410  if ($safe_keyword != $keyword) {
411  $keyword_replacements[$safe_keyword] = $val;
412  unset($keyword_replacements[$keyword]);
413  }
414  }
415 
416  $contextid = (int)$GLOBALS['SQ_SYSTEM']->getContextId();
417  $context_suffix = ($contextid === 0) ? '' : ('.'.$contextid);
418  $content_file_name = 'content_file'.$context_suffix.'.php';
419  $default_content_file_name = 'content_file.php';
420 
421  if ($this->useSystemVersion()) {
422  $safe_edit_file_name = $this->data_path.'/.'.$content_file_name;
423  if (file_exists($safe_edit_file_name) === FALSE) {
424  $safe_edit_file_name = $this->data_path.'/.'.$default_content_file_name;
425  }
426  if (file_exists($safe_edit_file_name) === TRUE) {
427  // create an array of urls that the content file is
428  // going to try and use to work out file urls, link hrefs etc
429  $sq_asset_url_list = Array();
430 
431  $notice_links = unserialize(file_to_string($this->data_path.'/.notice_links'));
432  $get_href_ids = $this->attr('shadow_links');
433 
434  foreach ($notice_links as $link_assetid) {
435  $get_href_ids[] = $link_assetid;
436  }
437 
438  $sq_asset_url_list = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($get_href_ids));
439  for (reset($sq_asset_url_list); NULL !== ($minorid = key($sq_asset_url_list)); next($sq_asset_url_list)) {
440  if ($sq_asset_url_list[$minorid] == '') {
441  $sq_asset_url_list[$minorid] = './?a='.$minorid;
442  }
443  }
444 
445  ob_start();
446  include($safe_edit_file_name);
447  $content = ob_get_contents();
448  ob_end_clean();
449  replace_keywords($content, $this->keyword_replacements);
450 
451  echo $content;
452  }
453  } else {
454 
455  if (!isset($this->_tmp['asset_url_list'])) {
456 
457  // create an array of urls that the content file is
458  // going to try and use to work out file urls, link hrefs etc
459 
460  $notice_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE);
461  $get_href_ids = $this->attr('shadow_links');
462 
463  foreach ($notice_links as $link) {
464  $get_href_ids[] = $link['minorid'];
465  }
466 
467  $this->_tmp['asset_url_list'] = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($get_href_ids));
468  for (reset($this->_tmp['asset_url_list']); NULL !== ($minorid = key($this->_tmp['asset_url_list'])); next($this->_tmp['asset_url_list'])) {
469  if ($this->_tmp['asset_url_list'][$minorid] == '') {
470  $this->_tmp['asset_url_list'][$minorid] = './?a='.$minorid;
471  }
472  }
473 
474  }
475 
476  $sq_asset_url_list =& $this->_tmp['asset_url_list'];
477 
478  if (SQ_ROLLBACK_VIEW) {
479  $edit = $this->getEditFns();
480  ob_start();
481  $edit->paint($this, FALSE, FALSE);
482  $output = ob_get_contents();
483  ob_end_clean();
484 
485  // replace ./?a=xxx style links within the content
486  foreach ($sq_asset_url_list as $assetid => $href) {
487  if (strpos($assetid, ':') !== FALSE) {
488  $s = './?a='.$assetid.'$';
489  $output = str_replace($s, $href, $output);
490  } else {
491  $e = "!(/?\./\\?a=$assetid)([^0-9:])!";
492  $output = preg_replace($e, $href.'\\2', $output);
493  }
494  }
495 
496  // replace keywords in the content
497  replace_keywords($output, $keyword_replacements);
498 
499  echo $output;
500  return;
501  }
502 
503  if (!is_file($this->data_path.'/'.$content_file_name)) {
504  // generate the content file
505  $edit = $this->getEditFns();
506  $edit->generateContentFile($this);
507  }
508 
509  if (!is_file($this->data_path.'/'.$content_file_name)) {
510  trigger_localised_error('CORE0009', E_USER_WARNING, $this->name);
511  } else {
512  include($this->data_path.'/'.$content_file_name);
513  }
514  }//end else
515 
516  }//end printBody()
517 
518 
529  protected function _getName($short_name=FALSE, $contextid=NULL)
530  {
531  // No context specified, using the current context
532  if ($contextid === NULL) {
533  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
534  }//end if
535 
536  // Obtain the attribute value for Name from the specified Context
537  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
538  if (empty($values) === TRUE) {
539  return parent::_getName($short_name, $contextid);
540  } else {
541  return $values[$this->id];
542  }
543 
544  }//end _getName()
545 
546 
553  public function isEmpty()
554  {
555  $am = $GLOBALS['SQ_SYSTEM']->am;
556  $content_types = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'content_type', FALSE);
557  foreach ($content_types as $type) {
558  $ct = $am->getAsset($type['minorid'], $type['minor_type_code']);
559  if (!$ct->isEmpty()) return FALSE;
560  }
561  return TRUE;
562 
563  }//end isEmpty()
564 
565 
566 }//end class
567 
568 ?>