Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
layouts.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
39 function paintLayouts(&$owner, &$o, &$ei, $keywords=NULL)
40 {
41  $root = $ei->findScreen('static_screen_layouts');
42  $prefix = $owner->getPrefix();
43  $write_access = $owner->writeAccess('attributes');
44 
45  $lookups = $owner->getLookups('url');
46  $lookup_values = $owner->getLookupValues();
47 
48  $return_lookup = FALSE;
49 
50  $current_value = Array();
51  $current_value = _formatLayoutCode($lookup_values);
52 
53  foreach ($root->section as $section) {
54  $o->openSection((is_null($section['display_name'])) ? $section['name'] : $section['display_name']);
55 
56  // if we are in limbo and this section is not to be printed in limbo - dont print it
57  if (is_null($keywords)) {
58  if (SQ_IN_LIMBO && ((string) $section['name'] === 'limbo_access' || (int) $section['limbo_access'] === 0)) {
59  continue;
60  }
61  }
62 
63  if (!is_null($keywords) && isset($keywords['sections'][strtolower($section->attributes()->name)])) {
64  ob_start();
65  }
66 
67  foreach ($section->children() as $field) {
68  // should we print the note for the field or not
69  $print_note = TRUE;
70 
71  $child = strtolower((string) $field['code']);
72  switch ($child) {
73  // Show the Add Layout
74  case 'add_layout' :
75  if ($owner->adminAccess('settings')) {
76  $return_lookup = TRUE;
77 
78  // Allow changes here
79  $o->openField('');
80  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
81  asset_type_chooser($prefix.'_add_layouts', TRUE, Array());
82 
83  $o->closeField();
84  } else {
85  $return_lookup = TRUE;
86  }
87  break;
88 
89  // Show the edit layout
90  case 'edit_layouts' :
91  if (!is_null($current_value)) {
92  foreach ($current_value as $type_code => $values) {
93  // should we print the note for the field or not
94  $print_note = TRUE;
95 
96  $uc_type_code = ucwords(str_replace('_', ' ', $type_code));
97  $o->openSection($uc_type_code);
98 
99  // Show the fields
100  $return_lookup = TRUE;
101 
102  // Show the extra controls like delete this layout
103  if ($write_access) {
104  $o->openField('');
105  check_box($prefix.'_remove['.$type_code.']');
106  label(translate('remove_customisations_for', $uc_type_code), $prefix.'_remove['.$type_code.']');
107  $o->closeField();
108  }
109 
110  // Show the defined screens
111  $defined_screens = 0;
112  foreach ($values as $screen_key => $assetid) {
113  if ($screen_key != 'noscreen') {
114  $o->openField('');
115  ?>
116  <table class="sq-backend-table">
117  <tr>
118  <td class="sq-backend-table-header" width="33%"><?php echo translate('screen_name'); ?></td>
119  <td class="sq-backend-table-header" width="33%"><?php echo translate('layout'); ?></td>
120  <?php if ($write_access) {
121  ?>
122  <td align="center" class="sq-backend-table-header" width="33%"><?php echo translate('remove'); ?></td>
123  <?php }
124  ?>
125  </tr>
126  <tr>
127  <td class="sq-backend-table-cell">
128  <?php echo ucfirst($screen_key) ?>
129  </td>
130  <td class="sq-backend-table-cell">
131  <?php
132  if ($write_access) {
133  asset_finder($prefix.'_find['.$type_code.'@'.$screen_key.']', $assetid, Array('layout' => 'I'));
134  } else {
135  if (empty($assetid)) {
136  echo '<em>'.translate('none').'</em>';
137  } else {
138  echo get_asset_tag_line($assetid);
139  }
140  }
141  ?>
142  </td>
143  <?php if ($write_access) {
144  ?>
145  <td align="center" class="sq-backend-table-cell">
146  <?php check_box($prefix.'_remove_screen['.$type_code.'@'.$screen_key.']'); ?>
147  </td>
148  <?php }
149  ?>
150  </tr>
151  </table>
152  <?php
153  $defined_screens++;
154  $o->closeField();
155  }//end if
156  }//end foreach
157  // No screens so show message
158  if (!($defined_screens > 0)) {
159  if (!$owner->adminAccess('settings')) {
160  $o->openField('&nbsp;');
161  echo 'No layouts defined for any of '.$uc_type_code."'s screens";
162  $o->closeField();
163  }
164  }
165  // Give the option to add more screens
166  if ($write_access) {
167  $o->openField('');
168  if ($write_access) {
169  ?>
170  <br />
171  <?php
172  echo translate('add_layout');
173  echo ': ';
174 
175  $ei = new Asset_Edit_Interface($type_code);
176 
177  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
178  $as = new $type_code();
179  $ef = $as->getEditFns();
180 
181  $screens = Array(''); // first option is blank
182  foreach (array_keys($ef->static_screens) as $screen) {
183  $screens[] = $screen;
184  }
185  foreach (array_keys($ei->getScreens()) as $screen) {
186  $screens[] = $screen;
187  }
188 
189  // remove any screens that have already been defined
190  $screens = array_unique(array_diff($screens, array_keys($values)));
191 
192  $uc_screens = Array();
193  foreach ($screens as $screen) {
194  $uc_screens[$screen] = ucfirst($screen);
195  }
196  // print a box to add more screens
197  combo_box($prefix.'_screens['.$type_code.']', $uc_screens, FALSE, Array());
198  }
199 
200  $o->closeField();
201  }//end if
202  $o->closeSection();
203 
204  }//end foreach
205  }//end if
206  break;
207  }//end switch
208  }//end foreach
209 
210  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string) $field['code'])])) {
211  ob_start();
212  }
213  // before printing the note, we make sure that the note element exists and is not empty
214  if ($print_note && (!is_null($field->note)) && (strlen((string)$field->note) > 0)) {
215  $o->openField('&nbsp;');
216  $o->note((string) $field->note);
217  $o->closeField();
218  }
219  $o->closeSection();
220  }//end foreach
221 
222  return $return_lookup;
223 
224 }//end paintLayouts()
225 
226 
237 function processLayouts(&$owner, &$o, &$ei)
238 {
239  $prefix = $owner->getPrefix();
240  $write_access = $owner->writeAccess('lookups');
241 
242  // We don't have write access so exit
243  if (!$write_access) return FALSE;
244 
245  // Grab the info we need
246  $layouts = $owner->getLookupValues();
247  $urls = Array();
248 
249  if (!empty($layouts)) {
250  $urls = array_keys($layouts);
251  } else {
252  $all_urls = $owner->getURLs();
253 
254  // Grab the address innards of the URL and store this only
255  foreach ($all_urls as $url) {
256  $urls[] = $url['url'];
257  }
258  }
259 
260  // If url is not warn user and stop
261  if (empty($urls)) {
262  trigger_localised_error('SYS0330', E_USER_WARNING);
263  }
264 
265  // Process Adding a new layout of type code xx
266  if (isset($_POST[$prefix.'_add_layouts']) && !empty($_POST[$prefix.'_add_layouts'])) {
267  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
268  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
269 
270  // Format the current layouts to a design we like
271  $layout_schemas = Array();
272  foreach ($layouts as $layout_designs) {
273  $layout_schemas[] = key($layout_designs);
274  }
275 
276  // Remember last postings so we don't too many of the same
277  $prev_post = Array();
278 
279  foreach ($_POST[$prefix.'_add_layouts'] as $type_code) {
280  // Check they are not trying to add multiple entries of the same page
281  if (in_array($type_code, $prev_post)) {
282  continue;
283  } else {
284  $prev_post[] = $type_code;
285  }
286 
287  // layouts::type_code::screen where is not defined screen will default to noscreen with asset of 0
288  if (!empty($type_code)) {
289  $layout_schema = 'layout::'.$type_code.'::noscreen';
290  foreach ($urls as $url) {
291  $layouts[$url][$layout_schema] = Array();
292  $layouts[$url][$layout_schema]['value'] = 0;
293  }
294  }
295  }//end foreach
296 
297  if (!$owner->setLookupValues($layouts)) {
298  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
299  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
300  return FALSE;
301  }
302  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
303  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
304  }//end if
305 
306  // Process Editing layout screens
307  // Add the asset to the screen
308  if (isset($_POST[$prefix.'_find']) && !empty($_POST[$prefix.'_find'])) {
309  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
310  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
311  foreach ($_POST[$prefix.'_find'] as $key => $value) {
312  $details = explode('@', $key);
313 
314  // Make sure an asset was selected
315  if (!isset($value['assetid']) || empty($value['assetid'])) {
316  $assetid = 0;
317  } else {
318  $assetid = $value['assetid'];
319  }
320 
321  // Format the values for lookup values
322  $lookup = 'layout::'.$details[0].'::'.$details[1];
323 
324  // If an asset is already set for here delete it so we can re add it later
325  foreach ($urls as $url) {
326  if (in_array($lookup, $layouts[$url])) {
327  unset($layouts[$url][$lookup]);
328  }
329 
330  // Make out changes
331  if (!isset($layouts[$url][$lookup])) {
332  $layouts[$url][$lookup] = Array();
333  }
334  $layouts[$url][$lookup]['value'] = $assetid;
335  }
336  }//end foreach
337 
338  if (!$owner->setLookupValues($layouts)) {
339  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
340  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
341  return FALSE;
342  }
343  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
344  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
345  }//end if
346 
347  // Add the screen to the type code
348  if (isset($_POST[$prefix.'_screens']) && !empty($_POST[$prefix.'_screens'])) {
349  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
350  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
351  foreach ($_POST[$prefix.'_screens'] as $type_code => $screen) {
352  // Make sure something was selected
353  if (empty($screen)) continue;
354 
355  // Format the values for lookup values
356  $lookup = 'layout::'.$type_code.'::'.$screen;
357 
358  foreach ($urls as $url) {
359  // Check for duplication
360  if (in_array($lookup, $layouts[$url])) continue;
361 
362  $layouts[$url][$lookup] = Array();
363  $layouts[$url][$lookup]['value'] = 0;
364  if (!empty($screen)) {
365  // Delete the no screen entry if setting a screen
366  unset($layouts[$url]['layout::'.$type_code.'::noscreen']);
367  }
368  }
369  }
370 
371  if (!$owner->setLookupValues($layouts)) {
372  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
373  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
374  return FALSE;
375  }
376  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
377  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
378  }//end if
379 
380  // Remove a screen from a type code
381  if (isset($_POST[$prefix.'_remove_screen']) && !empty($_POST[$prefix.'_remove_screen'])) {
382  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
383  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
384  foreach ($_POST[$prefix.'_remove_screen'] as $key => $value) {
385  $details = explode('@', $key);
386 
387  foreach ($urls as $url) {
388  if ($value) {
389  unset($layouts[$url]['layout::'.$details[0].'::'.$details[1]]);
390  }
391 
392  // Check for another entry, if none found we need to add a noscreen entry
393  // Otherwise we lose our type code
394  $another_entry = 0;
395  foreach ($layouts[$url] as $looking_key => $looking_value) {
396  $search_string = '/^layout::'.$details[0].'::/i';
397  if (preg_match($search_string, $looking_key)) {
398  $another_entry++;
399  }
400  }
401  if (!$another_entry) {
402  $layouts[$url]['layout::'.$details[0].'::noscreen'] = Array();
403  $layouts[$url]['layout::'.$details[0].'::noscreen']['value'] = 0;
404  }
405  }
406  }
407 
408  if (!$owner->setLookupValues($layouts)) {
409  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
410  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
411  return FALSE;
412  }
413  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
414  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
415  }//end if
416 
417  // Remove a type code from layouts
418  if (isset($_POST[$prefix.'_remove']) && !empty($_POST[$prefix.'_remove'])) {
419  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
420  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
421 
422  foreach ($_POST[$prefix.'_remove'] as $type_code => $value) {
423  if ($value) {
424  foreach ($urls as $url) {
425  foreach ($layouts[$url] as $lookup_key => $lookup_value) {
426  $search_string = '/^layout::'.$type_code.'::/i';
427  if (preg_match($search_string, $lookup_key)) {
428  unset($layouts[$url][$lookup_key]);
429  }
430  }
431  }
432  }
433  }
434 
435  if (!$owner->setLookupValues($layouts)) {
436  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
437  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
438  return FALSE;
439  }
440  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
441  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
442  }//end if
443 
444  return TRUE;
445 
446 }//end processLayouts()
447 
448 
457 function _formatLayoutCode($lookup_values)
458 {
459  $return_value = Array();
460 
461  // Return nothing if empty
462  if (empty($lookup_values)) return $return_value;
463 
464  foreach ($lookup_values as $item) {
465  // Only grab the layouts only
466  foreach ($item as $lookup => $value) {
467  if (preg_match('/^layout::/i', $lookup)) {
468  $layout_format = explode('::', $lookup);
469  if (!isset($return_value[$layout_format[1]])) {
470  $return_value[$layout_format[1]] = Array();
471  }
472  $return_value[$layout_format[1]][$layout_format[2]] = $value['value'];
473  }
474  }
475  }
476 
477  return $return_value;
478 
479 }//end _formatLayoutCode()
480 
481 
482 ?>