Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
paint_layout_page.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page_templates/page_standard/page_standard.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/paint_layout/paint_layout_bodycopy/paint_layout_bodycopy.inc';
19 
20 define('SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE', 'paint_layout_bodycopy');
21 define('SQ_PAINT_LAYOUT_REQUEST_VAR_ASSET', 'sqpa');
22 define('SQ_PAINT_LAYOUT_DEFAULT_FORMAT_NAME', 'default_format');
23 
24 
41 {
42 
43 
50  function __construct($assetid=0)
51  {
52  $this->_ser_attrs = TRUE;
53  parent::__construct($assetid);
54 
55  }//end constructor
56 
57 
68  protected function _createAdditional(Array &$link)
69  {
70  if (!parent::_createAdditional($link, Array('content' => '%paint_layout%'))) return FALSE;
71 
72  // add a bodycopy to this page when creating
73  $GLOBALS['SQ_SYSTEM']->am->includeAsset(SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE);
74 
75  // 1. create type formats folder
76  $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
77 
78  $sub_assets= Array(
79  'type_formats' => 'folder',
80  );
81 
82  $type_formats = NULL;
83  foreach ($sub_assets as $name => $type) {
84  $asset = new $type();
85  $copy_link = Array('asset' => &$this, 'value' => $name ,'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
86 
87  $asset->setAttrValue('name', ucwords(str_replace('_',' ', $name)));
88  if (!$asset->create($copy_link)) return FALSE;
89 
90  if ($name == 'type_formats') $type_formats = $asset;
91 
92  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
93  unset($asset);
94  }
95 
96  // 2. create default format bodycopy
97  $type_format_asset_type_code = SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE;
98  $asset = new $type_format_asset_type_code();
99 
100  $copy_link = Array(
101  'asset' => &$type_formats,
102  'value' => SQ_PAINT_LAYOUT_DEFAULT_FORMAT_NAME,
103  'link_type' => SQ_LINK_TYPE_2,
104  'is_dependant' => 1,
105  'is_exclusive' => 1,
106  );
107 
108  $asset->setAttrValue('name', 'Default Format');
109 
110  return $asset->create($copy_link, Array('content' => '%asset_contents%'));
111 
112  }//end _createAdditional()
113 
114 
128  public function setAttrValue($name, $value)
129  {
130  if ($name == 'types' && !empty($value)) {
131  // because of way that we use the 'types' attribute in _getBodycopyFromAssetTypeCode() we need to make sure
132  // that all the types that inherit from other types are at the start of the list
133  // otherwise if we have 'page' and 'page_standard' in the list in that order, and we are viewing a page_standard asset
134  // it will pick the 'page' format bodycopy because 'page_standard' inherits from 'page'
135  $orig_value = $value;
136  $value = Array();
137  $levels = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo(array_keys($orig_value), 'lvl');
138  arsort($levels);
139  foreach ($levels as $type_code => $lvl) {
140  $value[$type_code] = $orig_value[$type_code];
141  }
142 
143  }
144 
145  return parent::setAttrValue($name, $value);
146 
147  }//end setAttrValue()
148 
149 
156  public function printBody()
157  {
158  $assetid = $this->_getAssetToPaint();
159  $this->paintAssetFromId($assetid);
160 
161  }//end printBody()
162 
163 
174  public function onRequestKeywords(Asset $broadcaster, Array $vars=Array())
175  {
176  if (!isset($vars['keywords'])) return;
177 
178  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($broadcaster->id);
179 
180  // if we're being handled here (instead of one of our extensions),
181  // it must be from one of our format bodycopies, or something else altogether,
182  // so we'll try to find out which of the format types it is.
183  $folder = $this->getFolder();
184  if (is_null($folder)) return;
185 
186  if (isset($parents[$folder->id])) {
187  $type_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2, SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE);
188 
189  foreach ($type_links as $link_info) {
190  if (isset($parents[$link_info['minorid']])) {
191  $type_code = $link_info['value'];
192  if ($type_code == SQ_PAINT_LAYOUT_DEFAULT_FORMAT_NAME) {
193  $type_code = '';
194  }
195 
196  $type_format = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_info['minorid']);
197  if (!empty($type_format)) {
198  $type_format->getKeywordsDescription($vars['keywords'], $type_code);
199  }
200 
201  break;
202  }
203  }
204 
205  } else {
206  // must be our page contents bodycopy
207  $vars['keywords']['paint_layout'] = 'Paint Layout';
208  }
209 
210  }//end onRequestKeywords()
211 
212 
219  protected function _getAssetToPaint()
220  {
221  $ret = 0;
222 
223  if (!empty($_REQUEST[SQ_PAINT_LAYOUT_REQUEST_VAR_ASSET])) {
224  $ret = $_REQUEST[SQ_PAINT_LAYOUT_REQUEST_VAR_ASSET];
225  }
226 
227  return $ret;
228 
229  }//end _getAssetToPaint()
230 
231 
240  public function paintAssetFromId($assetid=0)
241  {
242  if (!empty($assetid)) {
243  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
244  $this->paint($asset);
245  }
246 
247 
248  }//end paintAssetFromId()
249 
250 
259  public function paint(Asset $asset)
260  {
261  if (!empty($asset)) {
262  $bodycopy = $this->getBodycopy();
263  if (is_null($bodycopy) || !$bodycopy->readAccess()) {
264  $asset->printBody();
265  } else {
266  $keywords = $bodycopy->getKeywords();
267  $replacements = $this->_getKeywordReplacements($keywords, $asset);
268  $bodycopy->setKeywordReplacements($replacements);
269  $bodycopy->printBody();
270  }
271  }
272 
273  }//end paint()
274 
275 
285  protected function &_getKeywordReplacements($keywords, &$asset)
286  {
287  $ret = Array();
288 
289  if (in_array('paint_layout', $keywords)) {
290  $asset_type_code = $asset->type();
291  $bodycopy = $this->_getBodycopyFromAssetTypeCode($asset_type_code);
292 
293  $ret['paint_layout'] = $bodycopy->paintAsset($asset);
294  }
295 
296  return $ret;
297 
298  }//end _getKeywordReplacements()
299 
300 
310  protected function _getBodycopyFromAssetTypeCode($asset_type_code)
311  {
312  $ret = NULL;
313 
314  $supported_types = $this->attr('types');
315 
316  $format_type = '';
317  foreach ($supported_types as $supported_type => $inherited) {
318  $supported_type_and_children = ($inherited) ? $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants($supported_type, TRUE) : Array($supported_type);
319  if (in_array($asset_type_code, $supported_type_and_children)) {
320  $format_type = $supported_type;
321  break;
322  }
323  }
324 
325  $folder = $this->getFolder();
326 
327  if (!empty($format_type)) {
328  $format_link = $GLOBALS['SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2, SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE, TRUE, $format_type);
329  } else {
330  // not a customized type, so let's take the default format
331  $format_link = $GLOBALS['SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2, SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE, TRUE, SQ_PAINT_LAYOUT_DEFAULT_FORMAT_NAME);
332  }
333 
334  if (!empty($format_link)) {
335  $ret = $GLOBALS['SQ_SYSTEM']->am->getAsset($format_link['minorid'], $format_link['minor_type_code']);
336  }
337 
338  return $ret;
339 
340  }//end _getBodycopyFromAssetTypeCode()
341 
342 
351  public function getFolder($type='type_formats')
352  {
353  $null = NULL;
354  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, $type);
355  if (empty($link)) return $null;
356 
357  $folder = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
358  if (is_null($folder)) return $null;
359 
360  return $folder;
361 
362  }//end getFolder()
363 
364 
375  public function getFormats($type='type_formats')
376  {
377  if (!isset($this->_tmp['formats'][$type])) {
378  $folder = $this->getFolder($type);
379  $format_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2, SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE, TRUE);
380  if (empty($format_links)) return Array();
381 
382  $formats = Array();
383  foreach ($format_links as $link_data) {
384  $formats[$link_data['minorid']] = $link_data['value'];
385  }
386  $this->_tmp['formats'][$type] = $formats;
387  }
388  return $this->_tmp['formats'][$type];
389 
390  }//end getFormats()
391 
392 
403  public function createTypeFormatBodycopy($type)
404  {
405  $ret = FALSE;
406 
407  // create new bodycopy for that type
408  $folder = $this->getFolder('type_formats');
409  $new_type = $type;
410  // check that this type isnt already customised
411  $current_link = $GLOBALS['SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2, SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE, TRUE, $new_type);
412  if (empty($current_link)) {
413  // create a new format bodycopy for this type
414  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
415  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
416  $GLOBALS['SQ_SYSTEM']->am->includeAsset(SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE);
417 
418  $copy_link = Array('asset' => &$folder, 'value' => $new_type, 'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
419 
420  $type_format_asset_type_code = SQ_PAINT_LAYOUT_FORMAT_ASSET_TYPE;
421  $bodycopy = new $type_format_asset_type_code();
422  $bodycopy->setAttrValue('name', $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($new_type, 'name').' Format');
423  $bodycopy->setAttrValue('associated_asset_type', $type);
424 
425  if ($bodycopy->create($copy_link)) {
426  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
427  $ret = TRUE;
428  } else {
429  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
430  }
431  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
432  } else {
433  $ret = TRUE;
434  }
435 
436  return $ret;
437 
438  }//end createTypeFormatBodycopy()
439 
440 
441 }//end class
442 
443 ?>