Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Design_Area extends Asset
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE; // we have serialise type vars
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _preCreateCheck(Array &$link)
60  {
61  if (!parent::_preCreateCheck($link)) return FALSE;
62 
63  $id_name = trim($this->attr('id_name'));
64  if ($id_name == '') {
65  trigger_localised_error('CORE0187', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
66  return FALSE;
67  }
68 
69  return TRUE;
70 
71  }//end _preCreateCheck()
72 
73 
88  public function _updated($update_parents=TRUE)
89  {
90  // don't update the parent asset when this asset is updated as it starts a recusion
91  // that takes way too long
92  return parent::_updated(FALSE);
93 
94  }//end _updated()
95 
96 
103  public function lockTypes()
104  {
105  $lock_types = parent::lockTypes();
106  $lock_types['parsing'] = $lock_types['all'];
107  return $lock_types;
108 
109  }//end lockTypes()
110 
111 
121  protected function _getName($short_name=FALSE)
122  {
123  $name = $this->attr('id_name');
124  // if the attribute is empty, let the parent take care of setting the name
125  if ($name == '') $name = parent::_getName($short_name);
126 
127  return $name;
128 
129  }//end _getName()
130 
131 
141  public function processBackend(Backend_Outputter $o, Array &$link)
142  {
143  if (parent::processBackend($o, $link)) {
144  $designs = $this->getDesigns();
145  for ($i = 0, $max = count($designs); $i < $max; $i++) {
146  $designs[$i]->generateDesignFile(FALSE);
147  }//end for
148 
149  return TRUE;
150 
151  } else {
152 
153  return FALSE;
154 
155  }//end if
156 
157  }//end processBackend()
158 
159 
166  public function &getDesigns()
167  {
168  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_3, 'design', FALSE, 'minor');
169  $designs = Array();
170  foreach ($links as $link) {
171  $designs[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['majorid'], $link['major_type_code'], TRUE);
172  }
173  return $designs;
174 
175  }//end getDesigns()
176 
177 
184  public function getVarReferences()
185  {
186  $var_refs = Array();
187  foreach ($this->vars['var_references']['value'] as $var_name => $info) {
188  if (!isset($var_refs[$info['id_name']])) {
189  $var_refs[$info['id_name']] = Array();
190  }
191  $var_refs[$info['id_name']][] = $info['var'];
192  }//end foreach
193 
194  return $var_refs;
195 
196  }//end getVarReferences()
197 
198 
207  public function setVarReferenceValues(Array $var_ref_values)
208  {
209  if (!isset($this->_tmp['var_reference_values'])) {
210  $this->_tmp['var_reference_values'] = Array();
211  }
212  foreach ($this->vars['var_references']['value'] as $var_name => $info) {
213  if (isset($var_ref_values[$info['id_name']][$info['var']])) {
214  $this->_tmp['var_reference_values'][$var_name] = $var_ref_values[$info['id_name']][$info['var']];
215  }
216  }//end foreach
217 
218  }//end setVarReferenceValues()
219 
220 
229  public function attr($name)
230  {
231  if (isset($this->_tmp['var_reference_values'][$name])) {
232  return $this->_tmp['var_reference_values'][$name];
233  } else {
234  return parent::attr($name);
235  }
236 
237  }//end attr()
238 
239 
249  public function getProtectedAttrs()
250  {
251  return $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes('design_area', Array('name'));
252 
253  }//end getProtectedAttrs()
254 
255 
256 //-- PARSE FILE GENERATION --//
257 
258 
268  public function printVar($var, Array $args)
269  {
270  echo $this->_escapeVar($this->attr($var), $args);
271 
272  }//end printVar()
273 
274 
284  protected function _escapeVar($value, Array $args)
285  {
286  if (empty($args['escape']) || strtolower($args['escape']) == 'no') {
287  return $value;
288  } else {
289  return 'addslashes('.$value.')';
290  }
291 
292  }//end _escapeVar()
293 
294 
305  protected function _safeStringLiteral($string)
306  {
307  return var_export((string)$string, TRUE);
308 
309  }//end _safeStringLiteral()
310 
311 
320  protected function _canBeCached()
321  {
322  return TRUE;
323 
324  }//end _canBeCached()
325 
326 
335  protected function _canInit()
336  {
337  return TRUE;
338 
339  }//end _canInit()
340 
341 
350  public function printInitCode()
351  {
352  $design =& $GLOBALS['DESIGN_BEING_GENERATED'];
353  $instance_id = '_'.$this->id.'_'.$design->id;
354  if ($this->attr('cache') && $this->_canBeCached()) {
355  echo '
356  <?php
357  $cm = $GLOBALS[\'SQ_SYSTEM\']->am->getSystemAsset(\'cache_manager\');
358  $asset_contents'.$instance_id.' = $cm->loadFromCache('.$this->id.', \''.$this->type().'\', \''.$design->id.'\', !'.$this->attr('cache_globally').');
359 
360  if ($asset_contents'.$instance_id.' === FALSE) {
361  ?>
362  ';
363  }
364  $this->_printInitCode();
365 
366  if ($this->attr('cache') && $this->_canBeCached()) {
367  echo '
368  <?php
369  }
370  ?>
371  ';
372  }
373 
374  }//end printInitCode()
375 
376 
385  protected function _printInitCode()
386  {
387 
388  }//end _printInitCode()
389 
390 
401  public function printPaintingCode()
402  {
403  if (!array_get_index($this->_tmp, 'ready_to_paint')) {
404  return;
405  }
406  $design =& $GLOBALS['DESIGN_BEING_GENERATED'];
407  $instance_id = '_'.$this->id.'_'.$design->id;
408  if ($this->attr('cache') && $this->_canBeCached()) {
409  echo '
410  <?php
411  if ($asset_contents'.$instance_id.' === FALSE) {
412  ob_start();
413  ?>
414  ';
415  }
416 
417  $this->_printPaintingCode();
418 
419  if ($this->attr('cache') && $this->_canBeCached()) {
420  echo '
421  <?php
422  $asset_contents'.$instance_id.' = ob_get_contents();
423  ob_end_clean();
424  $cm = $GLOBALS[\'SQ_SYSTEM\']->am->getSystemAsset(\'cache_manager\');
425  $cm->saveToCache('.$this->id.', \''.$this->type().'\', \''.$design->id.'\', $asset_contents'.$instance_id.', !'.$this->attr('cache_globally').');
426  }
427  echo $asset_contents'.$instance_id.';
428  ?>
429  ';
430  }
431 
432  }//end printPaintingCode()
433 
434 
444  protected function _printPaintingCode()
445  {
446  $contents = $this->attr('contents');
447  $current_element = NULL;
448 
449  // remove any leading or trailing whitespace elements from the contents
450  // first, the leaders
451  if (count($contents) > 0) {
452  $current_element = array_shift($contents);
453  while(($current_element['_type'] == 'HTML') && (trim($current_element['contents']) == '')) {
454  if (count($contents) == 0) {
455  $current_element = NULL;
456  break;
457  }
458  $current_element = array_shift($contents);
459  }
460  }
461 
462  if (!is_null($current_element)) {
463  array_unshift($contents, $current_element);
464  }
465 
466  // now the trailers...
467  if (count($contents) > 0) {
468  $current_element = array_pop($contents);
469  while(($current_element['_type'] == 'HTML') && (trim($current_element['contents']) == '')) {
470  if (count($contents) == 0) {
471  $current_element = NULL;
472  break;
473  }
474  $current_element = array_pop($contents);
475  }
476  }
477 
478  if (!is_null($current_element)) {
479  array_push($contents, $current_element);
480  }
481 
482  // Now print out what we have left
483  foreach ($contents as $item) {
484  $this->_printContentItemCode($item);
485  }
486 
487  }//end _printPaintingCode()
488 
489 
498  protected function _printContentItemCode(Array $item)
499  {
500  if ($item['_type'] == 'HTML') {
501  // just some HTML content to echo out
502  echo $item['contents'];
503  } else if ($item['_type'] == 'TAG') {
504  if ($item['operation'] == 'print') {
505  $design =& $GLOBALS['DESIGN_BEING_GENERATED'];
506  if (empty($item['attributes']['id_name'])) {
507  $args = $item['attributes'];
508  unset($args['id_name']);
509  unset($args['var']);
510  $this->printVar($item['attributes']['var'], $args);
511  } else {
512  if ($item['attributes']['id_name'] == '__global__') {
513  // printing a global var
514  $args = $item['attributes'];
515  unset($args['id_name']);
516  unset($args['var']);
517  $design->printVar($item['attributes']['var'], $args);
518  } else {
519  // referencing another design area (which should already have been defined)
520  $da = $design->getDesignArea($item['attributes']['id_name']);
521  if (is_null($da)) {
522  trigger_localised_error('CORE0153', E_USER_NOTICE, $item['attributes']['id_name']);
523  } else {
524  if (isset($item['attributes']['var'])) {
525  // printing a particular var from the design area
526  $args = $item['attributes'];
527  unset($args['id_name']);
528  unset($args['var']);
529  $da->printVar($item['attributes']['var'], $args);
530  } else {
531  // printing the whole other design area
532  $da->printPaintingCode();
533  }
534  }
535  }
536  }
537  }//end if operation
538  }//end if type = TAG
539 
540  }//end _printContentItemCode()
541 
542 
552  public function printAreaCode()
553  {
554  if ($this->_canInit()) {
555  $this->printInitCode();
556  $this->_tmp['ready_to_paint'] = TRUE;
557  if ($this->attr('print')) $this->printPaintingCode();
558  }
559 
560  }//end printAreaCode()
561 
562 
563 }//end class
564 ?>