Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area_menu_type_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_area/design_area_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42 
43  }//end constructor
44 
45 
55  public function _processContents(Design_Area_Menu_Type $asset, Array &$contents)
56  {
57  $asset->setAttrValue('settings', Array());
58  if (!$this->_setVariables($asset, $contents)) {
59  return FALSE;
60  }
61  $settings = $asset->attr('settings');
62  // Now just make sure that current, hierarchy and normal entries all exist for the settings
63  for (reset($settings); NULL !== ($name = key($settings)); next($settings)) {
64  if (!isset($settings[$name]['normal'])) {
65  trigger_localised_error('CORE0172', E_USER_WARNING, $name, $asset->name);
66  return FALSE;
67  }
68 
69  if (!isset($settings[$name]['hierarchy'])) {
70  $settings[$name]['hierarchy'] = (isset($settings[$name]['current'])) ? $settings[$name]['current'] : $settings[$name]['normal'];
71  }
72 
73  if (!isset($settings[$name]['current'])) {
74  $settings[$name]['current'] = (isset($settings[$name]['hierarchy'])) ? $settings[$name]['hierarchy'] : $settings[$name]['normal'];
75  }
76 
77  }// end foreach
78 
79  if (!$asset->setAttrValue('settings', $settings)) {
80  return FALSE;
81  }
82 
83  $sub_menu_processed = FALSE;
84  $asset_index = -1;
85  $divider_index = -1;
86  $sub_index = -1;
87  // see if our asset element has any sub's in it
88  foreach ($contents as $index => $element) {
89  if ($element['_type'] != 'TAG') continue;
90  switch ($element['operation']) {
91  case 'asset' :
92  $asset_index = $index;
93 
94  // if there is nothing to do get out of here :)
95  if ($element['self_terminating'] || empty($element['contents'])) {
96  break;
97  }
98 
99  foreach ($element['contents'] as $sub_element_index => $sub_element) {
100  if ($sub_element['_type'] != 'TAG' || $sub_element['operation'] != 'sub') {
101  continue;
102  }
103 
104  $sub_index = $sub_element_index;
105 
106  // right so we have a sub menu, let's check it out
107 
108  // if there is a name and it's not the the same type as the current design area
109  if (empty($sub_element['attributes']['design_area'])) {
110  trigger_localised_error('CORE0144', E_USER_WARNING);
111  return FALSE;
112  }//end if
113 
114  $design_area = 'design_area_'.strtolower($sub_element['attributes']['design_area']);
115 
116  // first let's make sure that we are actually going to create a menu section type
117  $design_area_parent_types = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($design_area);
118  if (!in_array('design_area_menu_type', $design_area_parent_types)) {
119  trigger_localised_error('CORE0170', E_USER_WARNING, $asset->name);
120  return FALSE;
121  }
122 
123  if (!$this->_processSubMenu($asset, $sub_element['contents'], $design_area)) {
124  return FALSE;
125  }
126  $sub_menu_processed = TRUE;
127 
128  break;
129 
130  }//end foreach
131  break;
132 
133  case 'divider' :
134  $divider_index = $index;
135  break;
136 
137  }//end switch
138 
139  }//end foreach
140 
141  // OK, so we have a working sub menu, add it in
142  if ($sub_menu_processed && $asset_index >= 0 && $sub_index >= 0) {
143  // Sub menu contents used to be cleared here, which was causing probs with customisations.
144  // This remains empty for the moment, but any alterations to sub-menu attributes should
145  // be done here.
146  // otherwise remove any reference to it
147  } else {
148  $link = $asset->getSubMenuLink();
149  if (!empty($link)) {
150  $asset->deleteLink($link['linkid']);
151  }
152  }// end if
153 
154  // OK, so we have some asset contents, so extract it
155  if ($asset_index >= 0) {
156 
157  // if there is no contents, then see if there is a default
158  if (empty($contents[$asset_index]['contents'])) {
159  $contents[$asset_index]['contents'] = $this->_getDefaultContents($asset, 'asset');
160  }
161 
162  if (!$asset->setAttrValue('asset_contents', $contents[$asset_index]['contents'])) {
163  return FALSE;
164  }
165 
166  // Now what we do is morph it so that this element doesn't contain all the un-necessary contents stuff
167  $contents[$asset_index] = Array(
168  '_type' => 'TAG',
169  'attributes' => Array (),
170  'operation' => 'asset',
171  'self_terminating' => 1,
172  );
173 
174  }// end if
175 
176  // OK, so we have some divider contents, so extract it
177  if ($divider_index >= 0) {
178 
179  // if there is no contents, then see if there is a default
180  if (empty($contents[$divider_index]['contents'])) {
181  $contents[$divider_index]['contents'] = $this->_getDefaultContents($asset, 'divider');
182  }
183 
184  if (!$asset->setAttrValue('divider_contents', $contents[$divider_index]['contents'])) {
185  return FALSE;
186  }
187 
188  // Now we want to remove the divider section
189  array_splice($contents, $divider_index, 1);
190 
191  }// end if
192 
193  $GLOBALS['SQ_PROCESSED_DESIGN_AREAS'][$asset->attr('id_name')] = TRUE;
194 
195  $asset->setAttrValue('contents', $contents);
196  $asset->saveAttributes();
197  return TRUE;
198 
199  }//end _processContents()
200 
201 
214  protected function _processSubMenu(Design_Area_Menu_Type $asset, Array &$contents, $design_area)
215  {
216  // let's see if we already have a sub menu
217  $sub = $asset->getSubMenu();
218 
219  // nup ? create one now
220  if (empty($sub) || is_null($sub)) {
221 
222  // get the parent
223  $design_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_3, 'design', FALSE, 'minor', $asset->attr('id_name'));
224 
225  if (empty($design_links)) return FALSE;
226  $designs = Array();
227  foreach ($design_links as $design_link) {
228  $design_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($design_link['majorid']);
229  if (!is_null($design_asset)) {
230  $designs[] = $design_asset;
231  }
232  }
233 
234  if (is_null($designs)) return FALSE;
235 
236  $GLOBALS['SQ_SYSTEM']->am->includeAsset($design_area);
237  $sub = new $design_area();
238 
239  $sub->setAttrValue('id_name', $asset->attr('id_name').'__sub_menu');
240  $sub->setAttrValue('contents', $contents);
241 
242  $linkids = Array();
243  $created = FALSE;
244  // create link to design and customisation.
245  foreach ($designs as $design) {
246  $sub_link = Array('asset' => &$design, 'link_type' => SQ_LINK_TYPE_3, 'value' => $asset->attr('id_name').'__sub_menu', 'is_dependant' => 1);
247  // Create the asset and link it to one of the parents.
248  if (!$created) {
249  $linkid = $sub->create($sub_link);
250  if (!$linkid) return FALSE;
251  $created = TRUE;
252  } else {
253  // Create link only now
254  $linkid = $GLOBALS['SQ_SYSTEM']->am->createAssetLink($design, $sub, SQ_LINK_TYPE_3, $asset->attr('id_name').'__sub_menu', NULL, 1);
255  if (!$linkid) return FALSE;
256  }
257 
258  $linkids[] = $linkid;
259  }
260 
261  // Create SQ_LINK_NOTICE to its Upper level menu
262  $createAssetLink = $GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $sub, SQ_LINK_NOTICE, 'sub_menu');
263  if (!$createAssetLink) return FALSE;
264 
265  }//end if
266 
267  // if the current sub is a different type of menu type then morph it to the correct one
268  if ($design_area != $sub->type()) {
269 
270  // first up
271  if (!$sub->morph('design_area_menu_type')) {
272  return FALSE;
273  }
274  // then down
275  if (!$sub->morph($design_area)) return FALSE;
276  if (!$sub->setAttrValue('id_name', $asset->attr('id_name').'__sub_menu')) {
277  return FALSE;
278  }
279  }
280 
281  $edit_fns = $sub->getEditFns();
282 
283  if (!$edit_fns->_processContents($sub, $contents)) {
284  return FALSE;
285  }
286 
287  // We have to set this attribute here between the _processContents function above to make sure this attribute
288  // is not reverted to the default empty array.
289  $unwanted_asset_types_parents = $asset->attr('unwanted_asset_types');
290  if (!empty($unwanted_asset_types_parents)) {
291  $sub->setAttrValue('unwanted_asset_types', $unwanted_asset_types_parents);
292  }//end if
293 
294  if (!$sub->saveAttributes()) {
295  return FALSE;
296  }
297 
298  $asset->saveAttributes();
299 
300  $wanted_asset_types_parents = $asset->attr('wanted_asset_types');
301  if (!empty($wanted_asset_types_parents)) {
302  $sub->setAttrValue('wanted_asset_types', $wanted_asset_types_parents);
303  }
304 
305  if (!$sub->saveAttributes()) {
306  return FALSE;
307  }
308 
309  $asset->saveAttributes();
310 
311 
312  return TRUE;
313 
314  }//end _processSubMenu()
315 
316 
327  function paintLevel(Design_Area_Menu_Type $asset, Backend_Outputter $o, $prefix)
328  {
329  $write_access = $asset->writeAccess('attributes');
330  $current_level = $asset->attr('level');
331  $special_levels = Array(
332  '' => '-- '.strtolower(translate('custom')).' --',
333  '-2' => translate('children'),
334  '-1' => translate('current'),
335  '0' => translate('top'),
336  '1' => translate('sub'),
337  );
338 
339  if ($write_access) {
340  combo_box($prefix.'_special_level', $special_levels, FALSE, $asset->attr('level'));
341  echo ' '.strtolower(translate('or')).' ';
342  text_box($prefix.'_level', (!isset($special_levels[$current_level])) ? $current_level : '');
343  } else {
344  if (isset($special_levels[$current_level])) {
345  echo $special_levels[$current_level];
346  } else {
347  echo $current_level;
348  }
349  }
350 
351  return $write_access;
352 
353  }//end paintLevel()
354 
355 
367  {
368  $special_level = (int) (isset($_POST[$prefix.'_special_level'])) ? $_POST[$prefix.'_special_level'] : '';
369  $custom_level = (int) (isset($_POST[$prefix.'_level'])) ? $_POST[$prefix.'_level'] : '';
370  $level = (!empty($custom_level)) ? $custom_level : $special_level;
371  return $asset->setAttrValue('level', $level);
372 
373  }//end processLevel()
374 
375 
386  public function paintExcludedTypes(Design_Area_Menu_Type $asset, Backend_Outputter $o, $prefix)
387  {
388  $excluded_types = $asset->attr('unwanted_asset_types');
389  if ($asset->writeAccess('attributes')) {
390  asset_type_chooser($prefix.'_excluded_types', TRUE, $excluded_types);
391  } else {
392  if (empty($excluded_types)) {
393  echo 'No types are excluded';
394  } else {
395  ?>
396  <ul>
397  <?php
398  foreach ($GLOBALS['SQ_SYSTEM']->am->getTypeInfo($excluded_types, 'name') as $type_name) {
399  ?>
400  <li><?php echo $type_name; ?></li>
401  <?php
402  }
403  ?>
404  </ul>
405  <?php
406  }
407  }
408 
409  }//end paintExcludedTypes()
410 
411 
422  public function processExcludedTypes(Design_Area_Menu_Type $asset, Backend_Outputter $o, $prefix)
423  {
424  if ($asset->writeAccess('attributes')) {
425  // bug #2712: asset chooser returns an array of empty strings
426  // instead of an empty array
427  $exclude_list = Array();
428  foreach ($_POST[$prefix.'_excluded_types'] as $type) {
429  if ($type != '') $exclude_list[] = $type;
430  }
431  return $asset->setAttrValue('unwanted_asset_types', $exclude_list);
432  } else {
433  return FALSE;
434  }
435 
436  }//end processExcludedTypes()
437 
438 
449  public function paintIncludedTypes(Design_Area_Menu_Type $asset, Backend_Outputter $o, $prefix)
450  {
451  $included_types = $asset->attr('wanted_asset_types');
452  if ($asset->writeAccess('attributes')) {
453  asset_type_chooser($prefix.'_included_types', TRUE, $included_types);
454  } else {
455  if (empty($included_types)) {
456  echo 'All types are included';
457  } else {
458  ?>
459  <ul>
460  <?php
461  foreach ($GLOBALS['SQ_SYSTEM']->am->getTypeInfo($included_types, 'name') as $type_name) {
462  ?>
463  <li><?php echo $type_name; ?></li>
464  <?php
465  }
466  ?>
467  </ul>
468  <?php
469  }
470  }
471 
472  }//end paintIncludedTypes()
473 
474 
485  public function processIncludedTypes(Design_Area_Menu_Type $asset, Backend_Outputter $o, $prefix)
486  {
487  if ($asset->writeAccess('attributes')) {
488 
489  $include_list = Array();
490  foreach ($_POST[$prefix.'_included_types'] as $type) {
491  if ($type != '') $include_list[] = $type;
492  }
493  return $asset->setAttrValue('wanted_asset_types', $include_list);
494  } else {
495  return FALSE;
496  }
497 
498  }//end processIncludedTypes()
499 
500 }//end class
501 ?>