Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
layout_manager.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 
30 class Layout_Manager extends Asset
31 {
32 
33 
40  function Layout_Manager($assetid=0)
41  {
42  $this->_ser_attrs = TRUE;
43  $this->Asset($assetid);
44 
45  }//end constructor
46 
47 
59  public function create(Array &$link)
60  {
61  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
62  if (!system_asset_fns_create_pre_check($this)) {
63  return FALSE;
64  }
65  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
66  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
67 
68  if ($linkid = parent::create($link)) {
69  if (!system_asset_fns_create_cleanup($this)) {
70  $linkid = FALSE;
71  }
72  }
73 
74  if ($linkid) {
75  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
76  } else {
77  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
78  }
79 
80  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
81  return $linkid;
82 
83  }//end create()
84 
85 
94  protected function _getName($short_name=FALSE)
95  {
96  return $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
97 
98  }//end _getName()
99 
100 
107  public function canDelete()
108  {
109  return FALSE;
110 
111  }//end canDelete()
112 
113 
120  public function canClone()
121  {
122  return FALSE;
123 
124  }//end canClone()
125 
126 
144  public function getLayout($asset, $screen='')
145  {
146  $null = NULL; // for returning a reference
147 
148  // layouts should only be used on the frontend
149  if (!SQ_IN_LIMBO) return $null;
150 
151  if (is_null($asset)) return $null;
152 
153  // Go and find if there is a layout on this asset, first.
154  // Layouts defined by the Layouts screen on the asset
155  $layout = $this->_getAssetLayout($asset, $screen);
156 
157  if (!is_null($layout)) return $layout;
158 
159  // If no Asset Layout found, continue to use Layout Manager for a layout
160  $type_code = $asset->type();
161  $layouts = $this->attr('layouts');
162 
163  // if there are no layouts directly applied for this type code will
164  // will check the type ancestors to look for one
165  if (!isset($layouts[$type_code]) || (!empty($screen) && !isset($layouts[$type_code][$screen]))) {
166  if (NULL === ($type_code = $this->getAncestorLayoutTypeCode($type_code, $screen))) {
167  return $null;
168  }
169  }
170 
171  // no layouts defined for this type_code's screens
172  if (empty($layouts[$type_code])) return $null;
173 
174  // if they have not specified a screen, return an array of references to all the screen layouts
175  if ($screen == '') {
176  $layout = Array();
177  foreach ($layouts[$type_code] as $screen => $assetid) {
178  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'layout');
179  if (is_null($asset)) continue;
180  $layout[$screen] = $asset;
181  }
182  if (empty($layout)) return $null;
183  } else {
184  if (!isset($layouts[$type_code][$screen])) {
185  return $null;
186  }
187  $layout = NULL;
188  if (!empty($layouts[$type_code][$screen])) {
189  $layout = $GLOBALS['SQ_SYSTEM']->am->getAsset($layouts[$type_code][$screen], 'layout');
190  }
191  return $layout;
192  }
193  return $layout;
194 
195  }//end getLayout()
196 
197 
207  public function getAncestorLayoutTypeCode($type_code, $screen='')
208  {
209  $layouts = $this->attr('layouts');
210  $ancestor_type_code = '';
211 
212  $ancestors = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($type_code, FALSE);
213  foreach ($ancestors as $type) {
214  if (isset($layouts[$type])) {
215  if (!empty($screen) && !isset($layouts[$type][$screen])) {
216  continue;
217  }
218  $ancestor_type_code = $type;
219  break;
220  }
221  }
222  return (!empty($ancestor_type_code)) ? $ancestor_type_code : NULL;
223 
224  }//end getAncestorLayoutTypeCode()
225 
226 
236  public function hasLayout($asset, $screen='details')
237  {
238  // Check for errors first
239  if (is_null($asset)) return FALSE;
240 
241  // Check for a valid asset layout set by the Layouts screen of the asset
242  $layout = $this->_hasAssetLayout($asset, $screen);
243 
244  // If a layout found, return, otherwise continue
245  if ($layout) return TRUE;
246 
247  $type_code = $asset->type();
248  $layouts = $this->attr('layouts');
249  if (!isset($layouts[$type_code][$screen])) {
250  // if there is no screen defined for this type_code, check to see if there is
251  // a layout defined for its ancestor
252  if (NULL === ($type_code = $this->getAncestorLayoutTypeCode($type_code))) {
253  return FALSE;
254  }
255  if (!isset($layouts[$type_code][$screen])) {
256  return FALSE;
257  }
258  }
259  return TRUE;
260 
261  }//end hasLayout()
262 
263 
281  public function _getAssetLayout($asset, $screen='')
282  {
283  $layout_def = 'layout::'.$asset->type().'::'.$screen;
284  $layouts = $asset->getLookupValues(TRUE, $layout_def);
285  $url = preg_replace('|^https?://|', '', $asset->getURL());
286 
287  if (!empty($layouts)) {
288  $layout = NULL;
289  if (isset($layouts[$url][$layout_def]['value']) && !empty($layouts[$url][$layout_def]['value'])) {
290  $layout = $GLOBALS['SQ_SYSTEM']->am->getAsset($layouts[$url][$layout_def]['value']);
291  }//end if
292 
293  if (is_null($layout)) {
294  return NULL;
295  } else {
296  return $layout;
297  }
298  }//end if
299 
300  return NULL;
301 
302  }//end _getAssetLayout()
303 
304 
314  public function _hasAssetLayout($asset, $screen='details')
315  {
316  $layout = $asset->getLookupValues(TRUE, 'layout::'.$asset->type().'::'.$screen);
317 
318  if (!empty($layout)) return TRUE;
319 
320  return FALSE;
321 
322  }//end _hasAssetLayout()
323 
324 
325 }//end class
326 ?>