Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_area/design_area.inc';
19 
31 class Design extends Design_Area
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
58  protected function _createAdditional(Array &$link)
59  {
60  if (!parent::_createAdditional($link)) return FALSE;
61 
62 
63  // create an empty parse file
64  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
65  if (!is_dir($this->data_path)) {
66  create_directory($this->data_path);
67  }
68  $parse_file = $this->data_path.'/parse.txt';
69  touch($parse_file);
70  if (!$fv->add($this->data_path_suffix, $parse_file, '')) {
71  trigger_localised_error('CORE1057', E_USER_WARNING);
72  }
73  if (!$fv->checkOut($this->data_path_suffix.'/parse.txt', $this->data_path)) {
74  trigger_localised_error('CORE0158', E_USER_WARNING);
75  return FALSE;
76  }
77 
78  return $this->makeAndSaveInitialWebPath(strtolower($this->_getName()), $link);
79 
80  }//end _createAdditional()
81 
82 
105  public function cloneComponentsAdditional(Asset $clone, Array $components)
106  {
107  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
108 
109  // Clone the parse file
110  // ONLY do this for the design to design clone and if it will not be morphed to a customisation
111  if (($this->type() == 'design' || $this->type() == 'design_css') && !isset($this->_tmp['custom'])) {
112  $orig_parse = $this->data_path.'/parse.txt';
113  $clone_parse = $clone->data_path.'/parse.txt';
114 
115  // Check the path exists and is accessible (and attempt to create if possible)
116  if (!is_dir($clone->data_path) || !is_writable($clone->data_path)) {
117  if (!create_directory($clone->data_path)) {
118  return FALSE;
119  }
120  }
121 
122 
123 
124 
125  // Copy the parse file across first, then process it, to make sure
126  if (copy($orig_parse, $clone_parse)) {
127  $edit_fns = $clone->getEditFns();
128 
129  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
130 
131  // tell the asset to update the customisation at the end of the interface processing
132  $clone->_tmp['update_customisations'] = TRUE;
133  $clone->_tmp['generate_design'] = TRUE;
134  $changes = $edit_fns->parseAndProcessFile($clone);
135 
136  // attempt to add the parse file to the repository
137  if (!$fv->add($clone->data_path_suffix, $clone_parse, '')) {
138  trigger_localised_error('CORE1057', E_USER_WARNING);
139  }
140 
141  // make sure we have the latest version of our file
142  if (!$fv->checkOut($clone->data_path_suffix.'/parse.txt', $clone->data_path)) {
143  trigger_localised_error('CORE0158', E_USER_WARNING);
144  return FALSE;
145  }
146 
147  if (!$clone->generateDesignFile()){
148  return FALSE;
149  }
150 
151  }//end if
152  }//end if
153 
154  return parent::cloneComponentsAdditional($clone, $components);
155 
156  }//end cloneComponentsAdditional()
157 
158 
165  public function cloneDependantAdditional(Asset $clone)
166  {
167  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
168  $return_value = TRUE;
169  $value = TRUE;
170 
171  // Load the web paths for the design's components
172  $components = $GLOBALS['SQ_SYSTEM']->am->getChildren($clone->id);
173  foreach ($components as $componentid => $component) {
174  // set web path for components
175  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($componentid);
176 
177  $initial_path = strtolower($asset->name);
178  $valid_paths = make_valid_web_paths(Array($initial_path));
179  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($clone, $valid_paths, $asset->id, TRUE);
180  $GLOBALS['SQ_SYSTEM']->acquireLock($asset->id, 'all');
181  $return_value = $asset->saveWebPaths($good_paths);
182  $value = (!$return_value) ? FALSE : TRUE;
183 
184  $return_value = $asset->updateLookups(FALSE);
185  $value = (!$return_value) ? FALSE : TRUE;
186  $GLOBALS['SQ_SYSTEM']->releaseLock($asset->id, 'all');
187  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
188  }//end foreach
189 
190  return $value;
191 
192  }//end cloneDependantAdditional()
193 
194 
202  public function _getAllowedLinks()
203  {
204  return Array(
205  SQ_LINK_TYPE_1 => Array(
206  'design_css' => Array('card' => 'M', 'exclusive' => FALSE),
207  ),
208  SQ_LINK_TYPE_2 => Array(
209  'design_customisation' => Array('card' => 'M', 'exclusive' => FALSE),
210  'design_css' => Array('card' => 'M', 'exclusive' => FALSE),
211  'file' => Array('card' => 'M', 'exclusive' => FALSE),
212  ),
213  SQ_LINK_TYPE_3 => Array(
214  'design_area' => Array('card' => 'M', 'exclusive' => FALSE),
215  ),
216  SQ_LINK_NOTICE => Array(),
217  );
218 
219  }//end _getAllowedLinks()
220 
221 
229  public function printFrontend()
230  {
231 
232  }//end printFrontend()
233 
234 
246  public function moveLinkPos($linkid, $sort_order=-1)
247  {
248  trigger_localised_error('CORE0168', E_USER_WARNING);
249  return FALSE;
250 
251  }//end moveLinkPos()
252 
253 
262  public function linksUpdated()
263  {
264  parent::linksUpdated();
265  if (isset($this->_tmp['design_areas'])) {
266  foreach (array_keys($this->_tmp['design_areas']) as $id_name) {
267  unset($this->_tmp['design_areas'][$id_name]);
268  }
269  }
270 
271  }//end linksUpdated()
272 
273 
294  public function prepareLink(Asset $asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
295  {
296  // if this is a file then we need to make it a dependant link
297  $changed = FALSE;
298  if ($side_of_link == 'major' && ($asset instanceof File)) {
299  if ($dependant != '1') {
300  $dependant = '1';
301  $changed = TRUE;
302  }
303  // In order to use mysource_files urls, we need to type 2 link files below us
304  if ($link_type == SQ_LINK_TYPE_1) {
305  $link_type = SQ_LINK_TYPE_2;
306  $changed = TRUE;
307  }
308  }
309 
310  return $changed;
311 
312  }//end prepareLink()
313 
314 
332  public function createLink(Asset $minor, $link_type, $value='', $sort_order=NULL, $dependant='0', $exclusive='0')
333  {
334  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
335  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
336 
337  $linkid = parent::createLink($minor, $link_type, $value, $sort_order, $dependant, $exclusive);
338  if (!$linkid) {
339  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
340  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
341  return FALSE;
342  }
343 
344  // special processing if we are creating a TYPE_2 linked file (inherited by customisations)
345  if (($minor instanceof File)) {
346 
347  $am =& $GLOBALS['SQ_SYSTEM']->am; // this need ref!
348 
349  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
350  foreach ($customisation_links as $link) {
351  $customisation = $am->getAsset($link['minorid'], $link['minor_type_code']);
352  if (is_null($customisation)) continue;
353 
354  $file_link_ids = Array();
355  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($customisation->id, SQ_LINK_TYPE_2, 'file', FALSE);
356  foreach ($file_links as $link) {
357  $file_link_ids[] = $link['minorid'];
358  }
359  $file_link_info = $am->getAssetInfo(array_unique($file_link_ids));
360 
361  // is there already a direct file with the same name
362  $insert_link = TRUE;
363 
364  // we need to make it a valid web path to compare properly
365  $new_file_name = make_valid_web_paths(Array($minor->attr('name')));
366  $new_file_name = array_shift($new_file_name);
367 
368  foreach ($file_link_info as $info) {
369  if ($info['name'] == $new_file_name) {
370  $insert_link = FALSE;
371  }
372  }
373 
374  if ($insert_link) {
375  if (!$customisation->createLink($minor, SQ_LINK_TYPE_3, '', NULL, 1)) {
376  trigger_localised_error('CORE0162', E_USER_WARNING);
377  $am->forgetAsset($customisation);
378  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
379  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
380  return FALSE;
381  }
382  }
383 
384  $am->forgetAsset($customisation);
385 
386  }//end foreach customisation
387 
388  }//end if is_a file
389 
390  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
391  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
392  return $linkid;
393 
394  }//end createLink()
395 
396 
405  public function deleteLink($linkid)
406  {
407  // first, we should try and find the link
408  $delete_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
409  if (empty($delete_link)) {
410  trigger_localised_error('CORE0155', E_USER_WARNING, $linkid);
411  return FALSE;
412  }
413 
414  $am =& $GLOBALS['SQ_SYSTEM']->am;
415  $minor = $am->getAsset($delete_link['minorid'], $delete_link['minor_type_code']);
416 
417  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
418  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
419 
420  // special processing if we are deleting a TYPE_2 linked file (a file overridding a design file)
421  if ($minor instanceof File) {
422 
423  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
424  foreach ($customisation_links as $link) {
425  $customisation = $am->getAsset($link['minorid'], $link['minor_type_code']);
426  if (is_null($customisation)) continue;
427 
428  $file_link_ids = Array();
429  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($customisation->id, SQ_LINK_TYPE_3, 'file', FALSE);
430 
431  foreach ($file_links as $link) {
432  if ($link['minorid'] == $delete_link['minorid']) {
433  // found the link to delete
434  if (!$customisation->deleteLink($link['linkid'])) {
435  trigger_localised_error('CORE1065', E_USER_WARNING);
436  $am->forgetAsset($customisation);
437  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
438  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
439  return FALSE;
440  }
441  }
442  }// end foreach $file_links
443 
444  $am->forgetAsset($customisation);
445 
446  }//end foreach customisation
447  } //end is_a file
448 
449  if (!parent::deleteLink($linkid)) {
450  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
451  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
452  return FALSE;
453  }
454 
455  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
456  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
457  return TRUE;
458 
459  }//end deleteLink()
460 
461 
470  public function getAssociatedFile($filename)
471  {
472  $file_link_ids = Array();
473  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
474  foreach ($file_links as $link) {
475  $file_link_ids[] = $link['minorid'];
476  }
477  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($file_link_ids));
478 
479  foreach ($file_link_info as $assetid => $file_info) {
480  if ($file_info['name'] == $filename) {
481  return $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $file_info['type_code']);
482  }
483  }
484 
485  $null = NULL; // return by ref
486  return $null;
487 
488  }//end getAssociatedFile()
489 
490 
497  public function getStylesheets()
498  {
499 
500  // first get all files which end with .css
501  $file_link_ids = Array();
502  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
503  foreach ($file_links as $link) {
504  $file_link_ids[] = $link['minorid'];
505  }
506  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($file_link_ids));
507 
508  $return_values = Array();
509  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
510  foreach ($file_link_info as $assetid => $file_info) {
511  if (get_file_type($file_info['name']) == 'css') {
512  $return_values[$assetid] = $file_info['name'];
513  }
514  }
515 
516  return $return_values;
517 
518  }//end getStylesheets()
519 
520 
530  public function getDesignAreaLink($id_name='')
531  {
532  if ($id_name) {
533  return @$GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_3, 'design_area', FALSE, $id_name);
534  } else {
535  // we have to remove the real CSS links, as they are not true design areas
536  $all_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_3, 'design_area', FALSE);
537  $return_links = Array();
538  foreach ($all_links as $link) {
539  if ($link['minor_type_code'] != 'design_css') {
540  $return_links[] = $link;
541  }
542  }
543  return $return_links;
544  }
545 
546  }//end getDesignAreaLink()
547 
548 
557  public function getDesignArea($id_name)
558  {
559  if (empty($this->_tmp['design_areas'][$id_name])) {
560  if (!isset($this->_tmp['design_areas'])) {
561  $this->_tmp['design_areas'] = Array();
562  }
563  $link = $this->getDesignAreaLink($id_name);
564 
565  if (empty($link)) {
566  $this->_tmp['design_areas'][$id_name] = NULL;
567  } else {
568  $this->_tmp['design_areas'][$id_name] = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
569  }
570  }
571  return $this->_tmp['design_areas'][$id_name];
572 
573  }//end getDesignArea()
574 
575 
582  public function getParentDesign()
583  {
584  return $this;
585 
586  }//end getParentDesign()
587 
588 
598  public function processBackend(Backend_Outputter $o, Array &$link)
599  {
600  switch ($_POST['asset_action']) {
601  case 'edit' :
602  if (parent::processBackend($o, $link)) {
603  // regenerate the design (if necessary) and update customisations in HIPO
604  if (isset($this->_tmp['generate_design']) && $this->_tmp['generate_design']) {
605  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
606  $vars = Array(
607  'assetid' =>$this->id,
608  );
609  $hh->queueHipo('hipo_job_regenerate_design', $vars);
610 
611  }
612 
613  return TRUE;
614 
615  }// end if
616 
617  return FALSE;
618 
619  default :
620  return parent::processBackend($o, $link);
621 
622  }//end switch
623 
624  }//end processBackend()
625 
626 
633  public function updateCustomisations()
634  {
635  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
636  $am = $GLOBALS['SQ_SYSTEM']->am;
637  foreach ($customisation_links as $link) {
638  $customisation = $am->getAsset($link['minorid'], $link['minor_type_code']);
639  if (is_null($customisation)) continue;
640  if ($acquired = $GLOBALS['SQ_SYSTEM']->am->acquireLock($customisation->id, 'all')) {
641  $customisation->updateFromParent($this);
642  if ($acquired != 2) {
643  $GLOBALS['SQ_SYSTEM']->am->releaseLock($customisation->id, 'all');
644  }
645  }
646  }// end foreach
647 
648  }//end updateCustomisations()
649 
650 
660  public function printVar($var, $args)
661  {
662 
663  switch ($var) {
664  case 'html_header' :
665 echo '<!--
666  Running Squiz Matrix
667  Developed by Squiz - http://www.squiz.net
668  Squiz, Squiz Matrix, MySource, MySource Matrix and Squiz.net are registered Trademarks of Squiz Pty Ltd
669  Page generated: <?php echo date(\'d F Y H:i:s\')."\n"; ?>
670 -->
671 
672 <?php
673 if (!empty($SQ_PRIMARY_URL)) {
674  ?>
675  <script type="text/javascript" src="<?php echo sq_web_path(\'lib\'); ?>/js/JsHttpConnector.js"></script>
676  <script type="text/javascript" src="<?php echo $SQ_PRIMARY_URL; ?>/__lib/session/session.php?in_primary=<?php echo (sq_web_path(\'root_url\') == $SQ_PRIMARY_URL) ? 1 : 0; ?>&site_network=<?php echo $SQ_SITE_NETWORK->id; ?>"></script>
677  <?php
678  if (sq_web_path(\'root_url\') != $SQ_PRIMARY_URL) {
679  ?>
680  <script type="text/javascript">
681  start_session_handler("<?php echo sq_web_path(\'lib\'); ?>/session/session.php?site_network=<?php echo $SQ_SITE_NETWORK->id; ?>");
682  </script>
683  <?php
684  }
685 }//end if primary URL available
686 
687 
688 if (SQ_IN_LIMBO && !is_null($GLOBALS[\'SQ_SYSTEM\']->backend) && !is_null($GLOBALS[\'SQ_SYSTEM\']->backend->out)) $GLOBALS[\'SQ_SYSTEM\']->backend->out->_paintHeader();
689 if (SQ_ROLLBACK_VIEW) $GLOBALS[\'SQ_SYSTEM\']->printRollbackWarning();
690 ?>
691 
692 ';
693  break;
694 
695  case 'css_header' :
696 echo '/*
697  <?php echo SQ_CONF_SYSTEM_NAME."\n"; ?>
698  Content (c) Copyright <?php echo SQ_CONF_SYSTEM_OWNER."\n"; ?>
699  Running <?php echo SQ_SYSTEM_LONG_NAME; ?> - <?php echo SQ_SYSTEM_URL."\n"; ?>
700  Developed by Squiz - http://www.squiz.net
701  (c) Copyright <?php echo date(\'Y\') ?>. All rights reserved.
702  Page generated: <?php echo date(\'d F Y H:i:s\')."\n"; ?>
703  Warranty key: <?php echo (defined("SQ_LICENCE_WARRANTY_KEY") ? SQ_LICENCE_WARRANTY_KEY : "None")."\n"; ?>
704 */
705 
706 ';
707  break;
708 
709  case 'html_footer' :
710  break;
711 
712  case 'content_type' :
713  if (!empty($args['content_type'])) {
714  echo '<', '?php header('.$this->_safeStringLiteral('Content-type: '.$args['content_type']).'); ?', '>';
715  }
716  break;
717 
718  case 'pragma' :
719  if (!empty($args['pragma'])) {
720  echo '<', '?php header('.$this->_safeStringLiteral('Pragma: '.$args['pragma']).'); ?', '>';
721  }
722  break;
723 
724  case 'content_attachment' :
725  if (!empty($args['extension'])) {
726  echo '<', '?php $asset_name = htmlspecialchars($ASSET->name).\'.'.$args['extension'].'\'; ?'.'>';
727  echo '<'.'?php header("Content-Disposition: attachment; filename=$asset_name"); ?'.'>';
728  }
729  break;
730 
731  case 'site_link' :
732  if (!empty($args['full_url']) && in_array($args['full_url'], Array('yes', 'true', '1'))) {
733  $res_exp = '$ASSET_LINEAGE[0][\'protocol\'].\'://\'.$ASSET_LINEAGE[0][\'url\']';
734  } else {
735  $res_exp = 'substr($ASSET_LINEAGE[0][\'url\'], strpos($ASSET_LINEAGE[0][\'url\'], \'/\'))';
736  }
737 
738  $aqs = (!empty($args['append_query_string']) && in_array($args['append_query_string'], Array('yes', 'true', '1')));
739  $sqs = (!empty($args['start_query_string']) && in_array($args['start_query_string'], Array('yes', 'true', '1')));
740 
741  if ($aqs) {
742  $res_exp .= '.((!empty($_SERVER[\'QUERY_STRING\'])) ? \'?\'.$_SERVER[\'QUERY_STRING\'] : \'\')';
743  }
744  if ($sqs) {
745  if (!$aqs) {
746  $res_exp .= '.\'?\'';
747  } else {
748  $res_exp .= '.((!empty($_SERVER[\'QUERY_STRING\'])) ? \'&\' : \'?\');';
749  }
750  }
751  echo '<', '?php echo '.htmlentities($res_exp, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'; ?', '>';
752  break;
753 
754  case 'site_name' :
755  echo '<', '?php echo htmlspecialchars($ASSET_LINEAGE[0][\'short_name\']); ?', '>';
756  break;
757 
758  case 'assetid' :
759  echo '<', '?php echo ', $this->_escapeVar('$ASSET->id', $args), '; ?', '>';
760  break;
761 
762  case 'asset_name' :
763  $asset_name = $this->_escapeVar('htmlspecialchars($ASSET->name)', $args);
764  echo '<', '?php echo ', $asset_name, '; ?', '>';
765  break;
766 
767  case 'asset_short_name' :
768  $asset_short_name = $this->_escapeVar('htmlspecialchars($ASSET->short_name)', $args);
769  echo '<', '?php echo ', $asset_short_name, '; ?', '>';
770  break;
771 
772  case 'asset_link' :
773  $link_fn = 'getHref';
774  if (!empty($args['full_url'])) {
775  switch ($args['full_url']) {
776  case 'yes' :
777  case 'true' :
778  case '1' :
779  $link_fn = 'getUrl';
780  break;
781  }
782  }
783 
784  $aqs = FALSE;
785  if (!empty($args['append_query_string'])) {
786  switch ($args['append_query_string']) {
787  case 'yes' :
788  case 'true' :
789  case '1' :
790  $aqs = TRUE;
791  break;
792  }
793  }
794 
795  $sqs = FALSE;
796  if (!empty($args['start_query_string'])) {
797  switch ($args['start_query_string']) {
798  case 'yes' :
799  case 'true' :
800  case '1' :
801  $sqs = TRUE;
802  break;
803  }
804  }
805 
806  $cqs = FALSE;
807  if (!empty($args['clean_query_string'])) {
808  switch ($args['clean_query_string']) {
809  case 'yes' :
810  case 'true' :
811  case '1' :
812  $cqs = TRUE;
813  break;
814  }
815  }
816 
817  $additional = '$query_string = $_SERVER[\'QUERY_STRING\'];';
818  if ($cqs) {
819  $additional .= 'if (!empty($query_string)) $query_string = preg_replace("/[?&]?SQ_[^&]+/",\'\',$query_string);';
820  }
821 
822 
823  if ($aqs) {
824  $additional .= 'echo htmlentities((!empty($query_string)) ? \'?\'.$query_string : \'\');'."\n";
825  }
826  if ($sqs) {
827  if (!$aqs) {
828  $additional .= 'echo \'?\';';
829  } else {
830  $additional .= 'echo htmlentities((!empty($query_string)) ? \'&\' : \'?\');';
831  }
832  $additional .= "\n";
833  }
834  echo '<', '?php echo htmlentities(', $this->_escapeVar('$ASSET->'.$link_fn.'()', $args), ");\n", $additional, ' ?', '>';
835  break;
836 
837  case 'current_asset' :
838  case 'current_user' :
839  if ($var == 'current_user') {
840  echo '<'.'?php
841  if (!$GLOBALS["SQ_SYSTEM"]->userPublic()) {
842  ?', '>';
843  $base = '$GLOBALS["SQ_SYSTEM"]->user';
844  } else {
845  $base = '$ASSET';
846  }
847 
848  if (!empty($args['keyword'])) {
849  $args['keyword'] = trim($args['keyword'], '% ');
850  echo '<'.'?php
851  $keyword_val = '.$base.'->getKeywordReplacement('.$this->_safeStringLiteral($args['keyword']).');
852  if ($keyword_val != '.$this->_safeStringLiteral('%'.$args['keyword'].'%').') {
853  echo $keyword_val;
854  }
855  ?', '>';
856  } else {
857  if (empty($args['attr'])) $args['attr'] = 'name';
858  switch ($args['attr']) {
859  case 'name' :
860  case 'id' :
861  echo '<'.'?php
862  echo '.$base.'->'.$args['attr'].';
863  ?', '>';
864  break;
865  case 'password':
866  // not allowed
867  break;
868  default :
869  $attr_name = $this->_safeStringLiteral($args['attr']);
870  echo '<'.'?php
871  echo @'.$base.'->attr('.$attr_name.');
872  ?', '>';
873  break;
874 
875  }// end switch
876  }
877 
878  if ($var == 'current_user') {
879  if (!empty($args['default'])) {
880  $default = $this->_safeStringLiteral($args['default']);
881  echo '<'.'?php
882  } else {
883  echo '.$default.';
884  ?', '>';
885  }
886 
887  echo '<'.'?php
888  }//end if not public user
889  ?', '>';
890  }
891 
892  break;
893 
894  case 'file_path' :
895  echo '<','?php echo $FILE_URLS['.$this->_safeStringLiteral($args['filename']).'];?', '>';
896  break;
897 
898  case 'css_path' :
899  echo '<','?php echo $CSS_URLS['.$this->_safeStringLiteral($args['filename']).'];?', '>';
900  break;
901 
902  case 'asset_thumbnail' :
903  echo '<', '?php ';
904  echo '$thumbnail = NULL;'."\n";
905  echo 'if ($ASSET->useSystemVersion()) {'."\n";
906  echo ' $notice_links = unserialize(file_to_string($ASSET->data_path.\'/.sq_notice_links\'));'."\n";
907  echo ' foreach ($notice_links as $link) {'."\n";
908  echo ' if ($link[\'value\'] == \'thumbnail\') {'."\n";
909  echo ' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'."\n";
910  echo ' break;'."\n";
911  echo ' }'."\n";
912  echo ' }'."\n";
913  echo '} else {'."\n";
914  echo ' $link = $GLOBALS[\'SQ_SYSTEM\']->am->getLink('.$this->_escapeVar('$ASSET->id', $args).', SQ_LINK_NOTICE, \'image\', FALSE, \'thumbnail\');'."\n";
915  echo ' if (!empty($link)) {'."\n";
916  echo ' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'."\n";
917  echo ' }'."\n";
918  echo '}'."\n";
919  echo 'if ($thumbnail) echo $thumbnail->printImageTag(\'\', \'\', TRUE);'."\n";
920  echo '?', '>';
921  break;
922 
923  case 'asset_thumbnail_url' :
924  echo '<', '?php ';
925  echo '$thumbnail = NULL;'."\n";
926  echo 'if ($ASSET->useSystemVersion()) {'."\n";
927  echo ' $notice_links = unserialize(file_to_string($ASSET->data_path.\'/.sq_notice_links\'));'."\n";
928  echo ' foreach ($notice_links as $link) {'."\n";
929  echo ' if ($link[\'value\'] == \'thumbnail\') {'."\n";
930  echo ' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'."\n";
931  echo ' break;'."\n";
932  echo ' }'."\n";
933  echo ' }'."\n";
934  echo '} else {'."\n";
935  echo ' $link = $GLOBALS[\'SQ_SYSTEM\']->am->getLink('.$this->_escapeVar('$ASSET->id', $args).', SQ_LINK_NOTICE, \'image\', FALSE, \'thumbnail\');'."\n;";
936  echo ' if (!empty($link)) {'."\n";
937  echo ' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'."\n";
938  echo ' }'."\n";
939  echo '}'."\n";
940  echo 'if ($thumbnail) echo $thumbnail->getURL('.$this->_escapeVar('$ASSET->getUrl()', $args).');'."\n";
941  echo '?', '>';
942  break;
943 
944  case (preg_match('/^asset_/i', $var) ? $var : !$var):
945  echo '<', '?php ';
946  echo '$keyword = \'',$var, '\';';
947  echo '$replacement = '.$this->_escapeVar('$ASSET->getKeywordReplacement($keyword)', $args).';';
948  echo 'if ($replacement == $keyword) {';
949  echo ' echo \'\';';
950  echo '} else {';
951  echo ' echo $replacement;';
952  echo '}';
953  echo '?', '>';
954  break;
955 
956  default :
957  parent::printVar($var, $args);
958 
959  }//end switch
960 
961  }//end printVar()
962 
963 
978  public function paint(Asset $ASSET, Array $FILE_URLS=Array(), Array $CSS_URLS=Array())
979  {
980  // start performance mode timer
981  $GLOBALS['SQ_SYSTEM']->pm->startTimer($this);
982 
983  // generate an array of all the URLs we are going to need for the files in this design
984  $file_link_ids = Array();
985  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'file', FALSE);
986  foreach ($file_links as $link) {
987  $file_link_ids[] = $link['minorid'];
988  }
989 
990  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($file_link_ids));
991  $file_link_urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($file_link_ids));
992 
993  $system_root_url = sq_web_path('root_url');
994  foreach ($file_link_info as $minorid => $file_info) {
995  $file_url = $file_link_urls[$minorid];
996  if ($file_url == '') {
997  $file_url = $system_root_url.'/?a='.$minorid;
998  } else if ($this->attr('static_url_versioning')){
999  $served_by_apache = strpos($file_url, '/__data/') !== FALSE || (SQ_CONF_STATIC_ROOT_URL && strpos($file_url, SQ_CONF_STATIC_ROOT_URL.'/') !== FALSE);
1000  if ($served_by_apache) $file_url .= '?v='.$file_info['version'];
1001  }
1002  $FILE_URLS[$file_info['name']] = $file_url;
1003  }
1004 
1005  $DATA_PATH = sq_web_path('data'); // this works because we know that all design areas are not going to be restricted
1006  $LIB_PATH = sq_web_path('lib');
1007 
1008  // work out where the design file is
1009  if (SQ_ROLLBACK_VIEW) {
1010  // get an older version of our design file
1011  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
1012  $rep_file = $this->data_path_suffix.'/design_file.php';
1013  $then = iso8601_ts($_SESSION['sq_rollback_view']['rollback_time']);
1014  $info = $fv->_checkOutCheck($rep_file, NULL, $then);
1015  $file_path = $info['source_file'];
1016  } else {
1017  $file_path = $this->data_path.'/design_file.php';
1018  if (!file_exists($file_path)) {
1019 
1020  // If the design file does not exist, find from the parent design asset
1021  $arr['majorid'] = $this->id;
1022  do {
1023  $arr = $GLOBALS['SQ_SYSTEM']->am->getLink($arr['majorid'], NULL, 'design', FALSE, NULL,'minor', FALSE);
1024  if (empty($arr)) {
1025  trigger_localised_error('CORE0259', E_USER_WARNING, $ASSET->id);
1026  break;
1027  }
1028  $design = $GLOBALS['SQ_SYSTEM']->am->getAsset($arr['majorid']);
1029  $file_path = dirname(dirname($design->data_path)).'/'.get_asset_hash($design->id).'/'.$arr['majorid'].'/design_file.php';
1030  } while(!file_exists($file_path));
1031  }
1032  }
1033 
1034  // Now we just include the design file to get it to print
1035  // else
1036  require $file_path;
1037 
1038  // start performance mode timer
1039  $GLOBALS['SQ_SYSTEM']->pm->stopTimer($this);
1040  }//end paint()
1041 
1042 
1051  public function generateDesignFile($do_customisations=TRUE)
1052  {
1053  $this->_setupVarReferences();
1054  $pre_str = $this->_getDesignFileHeader();
1055 
1056  $str = '';
1057  $GLOBALS['DESIGN_BEING_GENERATED'] =& $this;
1058  ob_start();
1059  foreach ($this->attr('contents') as $item) {
1060  if ($item['_type'] == 'HTML') {
1061  // just some HTML content to echo out
1062  echo $item['contents'];
1063  } else if ($item['_type'] == 'TAG') {
1064  if ($item['operation'] == 'area') {
1065  // defining a design area
1066  $da = $this->getDesignArea($item['attributes']['id_name']);
1067  if (is_null($da)) {
1068  trigger_localised_error('CORE0234', E_USER_WARNING, $item['attributes']['id_name']);
1069  } else {
1070  $da->printAreaCode();
1071  }
1072  } else if ($item['operation'] == 'print') {
1073  if ((!isset($item['attributes']['id_name'])) || ($item['attributes']['id_name'] == '__global__')) {
1074  // printing a global var
1075  $args = $item['attributes'];
1076  unset($args['id_name']);
1077  unset($args['var']);
1078  $this->printVar($item['attributes']['var'], $args);
1079  } else {
1080  $da = $this->getDesignArea($item['attributes']['id_name']);
1081  if (is_null($da)) {
1082  trigger_localised_error('CORE0153', E_USER_NOTICE, $item['attributes']['id_name']);
1083  } else {
1084  if (isset($item['attributes']['var'])) {
1085  // printing a var from a certain design area
1086  $args = $item['attributes'];
1087  unset($args['id_name']);
1088  unset($args['var']);
1089  $da->printVar($item['attributes']['var'], $args);
1090  } else {
1091  // printing a whole design area (which should already have been defined)
1092  $da->printPaintingCode();
1093  }
1094  }
1095  }
1096  }
1097  }//end if Item = TAG
1098  }//end foreach contents
1099  $str = str_replace('?'.'><'.'?php', '', ob_get_contents());
1100  unset($GLOBALS['DESIGN_BEING_GENERATED']);
1101 
1102  ob_end_clean();
1103  if (empty($str)) return FALSE;
1104 
1105  if (!$this->_writeDesignFile($pre_str."\n".$str)) {
1106  return FALSE;
1107  }
1108 
1109 
1110  if ($do_customisations) {
1111  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
1112  $am =& $GLOBALS['SQ_SYSTEM']->am;
1113  foreach ($customisation_links as $link) {
1114  $customisation = $am->getAsset($link['minorid'], $link['minor_type_code']);
1115  if (is_null($customisation)) continue;
1116  $customisation->generateDesignFile(TRUE);
1117  }
1118  }
1119 
1120  return TRUE;
1121 
1122  }//end generateDesignFile()
1123 
1124 
1131  protected function _getDesignFileHeader()
1132  {
1133  // we are checking to see if there is a global $ASSET_LINEAGE here
1134  // because we need other assets to be able to modify the lineage, for
1135  // instance 404 pages.
1136  return '<?php
1137  global $ASSET_LINEAGE;
1138  $am =& $GLOBALS[\'SQ_SYSTEM\']->am;
1139  if (!isset($ASSET_LINEAGE) || empty($ASSET_LINEAGE)) $ASSET_LINEAGE = @$am->getLineageFromURL(null, null);
1140  if (empty($ASSET_LINEAGE)) {
1141  $asset_url = $ASSET->getURL();
1142  $protocol_pos = strpos($asset_url, "://");
1143  if ($protocol_pos !== FALSE) {
1144  $asset_url = substr($asset_url, $protocol_pos + 3);
1145  }
1146 
1147  $ASSET_LINEAGE = @$am->getLineageFromURL(null, $asset_url);
1148  }
1149 
1150  global $SQ_SITE_NETWORK, $SQ_PRIMARY_URL;
1151  $SQ_PRIMARY_URL = \'\';
1152  $SQ_SITE_NETWORK = NULL;
1153 
1154  if (isset($ASSET_LINEAGE[0])) {
1155  $site = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($ASSET_LINEAGE[0][\'assetid\'], $ASSET_LINEAGE[0][\'type_code\']);
1156  if (!is_null($site)) {
1157  $SQ_SITE_NETWORK = $site->getSiteNetwork();
1158  if (!is_null($SQ_SITE_NETWORK)) {
1159  $SQ_PRIMARY_URL = $SQ_SITE_NETWORK->getPrimaryURL();
1160  if (!empty($SQ_PRIMARY_URL)) {
1161  if (sq_web_path(\'root_url\') != $SQ_PRIMARY_URL) {
1162  require_once SQ_LIB_PATH.\'/session/pre_session.php\';
1163  }
1164  }
1165  }
1166  }
1167  }
1168 
1169  if (!defined(\'SQ_LIMBO_EDITING\')) define(\'SQ_LIMBO_EDITING\', (SQ_IN_LIMBO && (!isset($_REQUEST[\'ignore_frames\']) || !$_REQUEST[\'ignore_frames\'])));
1170  ?>';
1171 
1172  }//end _getDesignFileHeader()
1173 
1174 
1183  protected function _writeDesignFile($str)
1184  {
1185  $str = preg_replace('|\?>([\s]+)?<\?php|', '', $str);
1186  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
1187  create_directory($this->data_path);
1188  $file = $this->data_path.'/design_file.php';
1189  $new_file = !file_exists($file);
1190  if (!string_to_file($str, $file)) {
1191  return FALSE;
1192  }
1193 
1195 
1196  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
1197 
1198  if ($new_file) {
1199  // attempt to add the file to the repository
1200  if (!$fv->add($this->data_path_suffix, $file, '')) {
1201  trigger_localised_error('CORE0156', E_USER_WARNING);
1202  return FALSE;
1203  }
1204  } else {
1205  // updating existing file
1206  $file_status = $fv->upToDate($file);
1207  if (FUDGE_FV_MODIFIED & $file_status) {
1208  if (!$fv->commit($file, '')) {
1209  trigger_localised_error('CORE0159', E_USER_WARNING);
1210  return FALSE;
1211  }
1212  }
1213  }
1214 
1215  // make sure we have the latest version of our file
1216  if (!$fv->checkOut($this->data_path_suffix.'/design_file.php', $this->data_path)) {
1217  trigger_localised_error('CORE0031', E_USER_WARNING);
1218  return FALSE;
1219  }
1220 
1221  return TRUE;
1222 
1223  }//end _writeDesignFile()
1224 
1225 
1232  protected function _setupVarReferences()
1233  {
1234  $design_area_links = $this->getDesignAreaLink();
1235  $var_ref_values = Array();
1236  $da_refs = Array(); // REMOVE THIS SOON
1237  foreach ($design_area_links as $link) {
1238  $da = $this->getDesignArea($link['value']);
1239  if (is_null($da)) continue;
1240 
1241  $da_var_refs = $da->getVarReferences();
1242  for (reset($da_var_refs); NULL !== ($id_name = key($da_var_refs)); next($da_var_refs)) {
1243  $var_da = $this->getDesignArea($id_name);
1244  if (is_null($var_da)) continue;
1245  if (!isset($var_ref_values[$id_name])) {
1246  $var_ref_values[$id_name] = Array();
1247  }
1248  foreach ($da_var_refs[$id_name] as $var) {
1249  // does this var want the whole design area ?
1250  if ($var === '') {
1251  $da_refs[] = $id_name; // REMOVE THIS SOON
1252  // else just a normal var (make sure we don't have it already)
1253  } else if (!isset($var_ref_values[$id_name][$var])) {
1254  $var_ref_values[$id_name][$var] = $var_da->attr($var);
1255 
1256  }//end if
1257 
1258  }//end foreach
1259 
1260  }//end foreach
1261 
1262  }//end foreach
1263 
1264  // OK, now that we have all the var reference values we can just set them for all the DA's
1265  foreach ($design_area_links as $link) {
1266  $da = $this->getDesignArea($link['value']);
1267  if (is_null($da)) continue;
1268  $da->setVarReferenceValues($var_ref_values);
1269  }
1270 
1271  }//end _setupVarReferences()
1272 
1273 
1274 }//end class
1275 ?>