Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
paint_layout_lookup_value.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/lookup_value/lookup_value.inc';
18 
31 {
32 
33 
41  public static function lookupValueName()
42  {
43  return 'Paint Layout';
44 
45  }//end lookupValueName()
46 
47 
55  public static function namePrefix()
56  {
57  return 'paint_layout';
58 
59  }//end namePrefix()
60 
61 
83  public static function paintInterface(&$owner, &$o, $lookups, $lookup_values, $prefix)
84  {
85  $wa = ($owner->writeAccess('lookups') && $owner->writeAccess('links'));
87 
88  $o->openSection(translate('asset_definitions'));
89 
90  if (!empty($lookup_values)) {
91  $o->openField(translate('summary'));
92  ?>
93  <table class="sq-backend-table">
94  <tr>
95  <td class="sq-backend-table-header"><?php echo translate('url'); ?></td>
96  <td class="sq-backend-table-header"><?php echo 'Effective Paint Layout'; ?></td>
97  </tr>
98  <?php
99  foreach ($lookups as $url) {
100  // need to make sure the URL is printed even if it does not have a layout applied
101  if (empty($lookup_values[$url][$name_prefix.'::system::frontend'])) {
102  $lookup_values[$url][$name_prefix.'::system::frontend'] = Array('value' => '', 'inhd' => '0');
103  }
104 
105  foreach ($lookup_values[$url] as $layout_name => $data) {
106  // determine the type of layout applied and sort
107  $matches = Array();
108  preg_match('/^'.$name_prefix.'::(system|user)::(.*)$/', $layout_name, $matches);
109 
110  // store so we can use these values for individual URL settings later
111  $lookup_values[$url][$layout_name]['type'] = $matches[1];
112  $lookup_values[$url][$layout_name]['code'] = $matches[2];
113 
114  // don't print user defined paint layouts in the asset definitions table
115  if ($matches[1] === 'user') continue;
116 
117  $effective_layout = $data['value'];
118 
119  $override_suffix = '';
120  // get override layout if one exists
121  if (isset($lookup_values[$url.'/'])) {
122  foreach ($lookup_values[$url.'/'] as $override_layout_name => $override_data) {
123  $effective_layout = $override_data['value'];
124  $effective_layout_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($effective_layout));
125  }
126  $suffix = ' - <strong style="font-size:11px"><i>'.translate('override').'</i></strong>';
127  } else {
128  $effective_layout_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($effective_layout));
129  $suffix = self::_getSuffix($data['inhd'], FALSE);
130  }
131 
132  ?>
133  <tr>
134  <td class="sq-backend-table-cell"><?php echo $url; ?></td>
135  <td class="sq-backend-table-cell"><?php echo !empty($effective_layout_info) ? get_asset_tag_line($effective_layout).$suffix : '<i>'.translate('none').'</i>'; ?></td>
136  </tr>
137  <?php
138  }
139  }
140  ?>
141  </table>
142  <?php
143  $o->closeField();
144  }//end if
145 
146  $asset_layout = $GLOBALS['SQ_SYSTEM']->am->getLink($owner->id, SQ_LINK_NOTICE, 'paint_layout_page', FALSE, 'paint_layout::system::frontend');
147  $o->openField(translate('layout'));
148  if ($wa) {
149  asset_finder($prefix.'[asset][paint_layout][value]', empty($asset_layout['minorid']) ? '' : $asset_layout['minorid'], Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', empty($asset_layout) ? Array('clear') : Array('clear', 'reset'));
150  } else {
151  echo empty($asset_layout) ? '<i>'.translate('none').'</i>' : get_asset_tag_line($asset_layout['minorid']);
152  }
153  $o->closeField();
154 
155  $asset_override = $GLOBALS['SQ_SYSTEM']->am->getLink($owner->id, SQ_LINK_NOTICE, 'paint_layout_page', FALSE, 'override::paint_layout::system::frontend');
156  $o->openField(translate('override_layout'));
157  if ($wa) {
158  asset_finder($prefix.'[asset][override::paint_layout][value]', empty($asset_override) ? '' : $asset_override['minorid'], Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', empty($asset_override) ? Array('clear') : Array('clear', 'reset'));
159  } else {
160  echo empty($asset_override) ? '<i>'.translate('none').'</i>' : get_asset_tag_line($asset_override['minorid']);
161  }
162  $design_info = $owner->getDesignLookups();
163  if (empty($design_info) && !empty($lookup_values)) {
164  echo '<br/><span style="color:#FF0000">'.translate('asset_has_no_design').'</span>';
165  }
166 
167  $o->closeField();
168 
169  // close asset definitions section
170  $o->closeSection();
171 
172  // close the paint layout section
173  $o->closeSection();
174 
175  // which lookups have user defined layouts
176  $lookups_user = Array();
177 
178  $o->openSection(translate('url_specific_definitions'));
179  foreach ($lookups as $url) {
180 
181  $o->openSection($url);
182  $o->openField(translate('current'));
183  ?>
184  <table class="sq-backend-table">
185  <tr>
186  <td class="sq-backend-table-header"><?php echo 'Layout'; ?></td>
187  <td class="sq-backend-table-header"><?php echo 'Paint Layout'; ?></td>
188  </tr>
189  <?php
190 
191  if (!isset($lookup_values[$url])) {
192  $lookup_values[$url] = Array(0);
193  }
194 
195  foreach ($lookup_values[$url] as $layout_name => $data) {
196  if ($data['type'] === 'user') {
197  // so we know which lookups have user defined layouts for painting the next section
198  $lookups_user[] = $url;
199  continue;
200  }
201 
202  $layout_name = empty($layout_name) ? 'paint_layout::system::frontend' : $layout_name;
203 
204  $display_name = translate('defined_layout_display', empty($data['type']) ? translate('system') : ucwords($data['type']), ucwords(str_replace('_', ' ', $data['code'])));
205  $display_override_name = translate('defined_override_layout_display', empty($data['type']) ? translate('system') : ucwords($data['type']), ucwords(str_replace('_', ' ', $data['code'])));
206 
207  // to prevent overwriting inherited values unless a valid change was made
208  hidden_field($prefix.'['.$url.']['.$layout_name.'][value][original]', $data['value']);
209 
210  $url_override_layout = 0;
211  $url_override_info = Array();
212  // get override layout if one exists
213  if (isset($lookup_values[$url.'/'])) {
214  foreach ($lookup_values[$url.'/'] as $override_layout_name => $override_data) {
215  $url_override_layout = $override_data['value'];
216  $url_override_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($url_override_layout));
217  }
218  }
219 
220  // do not pre-populate asset finder if the values are the same as the corresponding notice link values
221  if (!empty($asset_layout) && isset($asset_layout['minorid']) && ($asset_layout['minorid'] === $data['value'])) {
222  $layout_value = '0';
223  $url_layout_info = '0';
224  } else {
225  $layout_value = $data['value'];
226  $url_layout_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($data['value']));
227  }
228  if (!empty($asset_override) && isset($asset_override['minorid']) && ($asset_override['minorid'] === $url_override_layout)) {
229  $url_override_layout = '0';
230  $url_override_info = '0';
231  }
232  hidden_field($prefix.'['.$url.'/]['.$layout_name.'][value][original]', $url_override_layout);
233 
234  if ($data['inhd']) {
235  $normal_buttons = empty($layout_value) ? Array('clear') : Array('reset');
236  } else {
237  $normal_buttons = empty($layout_value) ? Array('clear') : Array('clear', 'reset');
238  }
239  $override_buttons = empty($url_override_layout) ? Array('clear') : Array('clear', 'reset');
240 
241  ?>
242  <tr>
243  <td class="sq-backend-table-cell"><?php echo '<i>'.$display_name.'</i>'; echo $wa ? self::_getSuffix($data['inhd'], TRUE) : ''; ?></td>
244  <?php if ($wa) { ?>
245  <td class="sq-backend-table-cell"><?php asset_finder($prefix.'['.$url.']['.$layout_name.'][value]', $layout_value, Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', $normal_buttons);?></td>
246  <?php } else { ?>
247  <td class="sq-backend-table-cell"><?php echo empty($url_layout_info) ? '<i>'.translate('not_customised').'</i>' : get_asset_tag_line($layout_value).self::_getSuffix($data['inhd'], FALSE); ?></td>
248  <?php } ?>
249  </tr>
250  <tr>
251  <td class="sq-backend-table-cell"><?php echo '<i>'.$display_override_name.'</i>'; ?></td>
252  <?php if ($wa) { ?>
253  <td class="sq-backend-table-cell"><?php asset_finder($prefix.'['.$url.'/]['.$layout_name.'][value]', $url_override_layout, Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', $override_buttons);?></td>
254  <?php } else { ?>
255  <td class="sq-backend-table-cell"><?php echo empty($url_override_info) ? '<i>'.translate('not_customised').'</i>': get_asset_tag_line($url_override_layout); ?></td>
256  <?php } ?>
257  </tr>
258  <?php
259 
260  // remove data not needed anymore for painting
261  unset($lookup_values[$url][$layout_name]);
262  if (empty($lookup_values[$url])) {
263  // populate with empty data for painting the user defined layout section
264  $lookup_values[$url][$name_prefix.'::user::'] = Array();
265  }
266 
267  }//end foreach
268 
269  echo '</table>';
270 
271  $o->closeField();
272 
273  // end individual URL section
274  $o->closeSection();
275 
276  }//end foreach
277 
278  // close the URL specific definitions
279  $o->closeSection();
280 
281  $o->openSection(translate('user_defined_layouts'));
282 
283  foreach ($lookups as $url) {
284 
285  $o->openSection($url);
286 
287  $o->openField(translate('current'));
288  if (in_array($url, $lookups_user)) {
289  ?>
290  <table class="sq-backend-table">
291  <tr>
292  <td class="sq-backend-table-header"><?php echo 'Layout code'; ?></td>
293  <td class="sq-backend-table-header"><?php echo 'Paint layout'; ?></td>
294  </tr>
295  <?php
296  foreach ($lookup_values[$url] as $layout_name => $data) {
297  $effective_layout_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($data['value']));
298  $buttons = Array('reset');
299  if (!$data['inhd']) $buttons[] = 'clear';
300  ?>
301  <tr>
302  <?php if ($wa) { ?>
303  <td class="sq-backend-table-cell"><?php echo $data['inhd'] ? $data['code'] : text_box($prefix.'['.$url.']['.$layout_name.'][name]', $data['code'], 20, 30).self::_getSuffix($data['inhd'], TRUE); ?></td>
304  <?php } else { ?>
305  <td class="sq-backend-table-cell"><?php echo $data['code']; ?></td>
306  <?php } ?>
307  <?php if ($wa) { ?>
308  <td class="sq-backend-table-cell"><?php echo $data['inhd'] ? get_asset_tag_line($data['value']).self::_getSuffix($data['inhd'], FALSE) : asset_finder($prefix.'['.$url.']['.$layout_name.'][value]', $data['value'], Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', $buttons);?></td>
309  <?php } else { ?>
310  <td class="sq-backend-table-cell"><?php echo empty($effective_layout_info) ? translate('none') : get_asset_tag_line($data['value']).self::_getSuffix($data['inhd'], FALSE); ?></td>
311  <?php } ?>
312  </tr>
313  <?php
314 
315  }//end foreach
316 
317  // end user defined layouts for this individual URL
318  echo '</table>';
319  } else {
320  // no user defined layouts yet for this URL
321  echo '<i>'.translate('none').'</i>';
322  }
323  $o->closeField();
324 
325  if ($wa) {
326  $o->openSection(translate('create_user_defined_layout'));
327  $o->openField(translate('layout_code'));
328  text_box($prefix.'['.$url.']['.$name_prefix.'::user::][name]', '', 20, 30);
329  $o->openField(translate('new_question'));
330  asset_finder($prefix.'['.$url.']['.$name_prefix.'::user::][value]', 0, Array('paint_layout_page' => 'D'), 'sq_sidenav', FALSE, 'null', Array('clear'));
331  $o->closeField();
332  $o->closeSection();
333  }
334 
335  // end individual URL section
336  $o->closeSection();
337 
338  }//end foreach
339 
340  // close the URL specific definitions
341  $o->closeSection();
342 
343  return $wa;
344 
345  }//end paintInterface()
346 
347 
358  private static function _getSuffix($inhd, $parentheses)
359  {
360  if ($parentheses) {
361  return $inhd ? ' <strong style="font-size:11px"><i>('.translate('inherited').')</i></strong>' : '';
362  } else {
363  return $inhd ? ' - <strong style="font-size:11px"><i>'.translate('inherited').'</i></strong>' : '';
364  }
365 
366  }//end _getSuffix()
367 
368 
390  public static function processInterface(&$owner, &$o, $lookups, &$lookup_values, $prefix)
391  {
392  if (!isset($_POST[$prefix]) || !is_array($_POST[$prefix])) {
393  return FALSE;
394  }
395 
396  $lookup_values = Array();
397  $name_prefix = Paint_Layout_Lookup_Value::namePrefix();
398 
399  foreach ($_POST[$prefix] as $resource => $layouts) {
400 
401  foreach ($layouts as $layout_name => $layout_data) {
402  if ($layout_name === $name_prefix.'::user::') {
403  // creating a new user defined layout
404  if (empty($layout_data['name'])) continue;
405  if (empty($layout_data['value']['assetid'])) continue;
406  $layout_name .= strtolower($layout_data['name']);
407  if (isset($lookup_values[$resource][$layout_name])) {
408  trigger_error('Layout Name "'.$layout_data['name'].'" already in use for "'.$resource.'"', E_USER_WARNING);
409  continue;
410  }
411  } else if (!is_bool(strstr($layout_name, $name_prefix.'::user::'))) {
412  // updating an existing user defined layout
413  // this is the new layout code
414  $new_layout_name = $name_prefix.'::user::'.strtolower($layout_data['name']);
415 
416  // deal with deleted user defined layouts
417  if (empty($layout_data['name'])) {
418  $lookup_values[$resource][$layout_name]['value'] = '';
419  continue;
420  }
421 
422  // remove the previous one if it has changed
423  if ($new_layout_name !== $layout_name) {
424  if (isset($_POST[$prefix][$resource][$new_layout_name])) {
425  trigger_error('Layout Name "'.$new_layout_name.'" already in use for "'.$resource.'"', E_USER_WARNING);
426  continue;
427  }
428  $lookup_values[$resource][$layout_name]['value'] = '';
429  }
430  $layout_name = $new_layout_name;
431  } else if (!is_bool(strstr($layout_name, $name_prefix.'::system::frontend'))) {
432  // url layouts and override layouts fall in here
433 
434  if ($layout_data['value']['assetid'] === $layout_data['value']['original']) {
435  // don't overwrite inherited system::frontend values unless there is a valid change
436  continue;
437  } else {
438  // empty values come through because the asset finder is not pre-populated with values if an asset based layout is defined
439  // if these empty values went through, it would result in updateLookups() being run unnecessarily
440  // we need to make sure that values being deleted are not being caught here inadvertently as well
441  if (preg_match('/.+\/$/', $resource)) {
442  // override layout
443  if (isset($_POST[$prefix]['asset']['override::paint_layout']['value']['assetid']) && (empty($layout_data['value']['assetid']))) {
444  if ($layout_data['value']['original'] === $_POST[$prefix]['asset']['override::paint_layout']['value']['assetid']) {
445  continue;
446  }
447  }
448  } else {
449  // normal layout
450  if (isset($_POST[$prefix]['asset']['paint_layout']['value']['assetid']) && (empty($layout_data['value']['assetid']))) {
451  if ($layout_data['value']['original'] === $_POST[$prefix]['asset']['paint_layout']['value']['assetid']) {
452  continue;
453  }
454  }
455  }
456 
457  }
458  }
459 
460  if (empty($layout_data['value']['assetid'])) {
461  // set to blank string to delete
462  $lookup_values[$resource][$layout_name]['value'] = '';
463  } else {
464  // check to see if the provided assetid is a paint layout
465  $layout = $GLOBALS['SQ_SYSTEM']->am->getAsset($layout_data['value']['assetid']);
466  if (!is_null($layout) && $layout instanceof Paint_Layout_Page) {
467  $lookup_values[$resource][$layout_name]['value'] = $layout_data['value']['assetid'];
468  } else {
469  trigger_error('Assetid provided, #'.$layout_data['value']['assetid'].', is not of asset type Paint Layout Page', E_USER_WARNING);
470  }
471  }
472 
473  }//end foreach
474  }//end foreach
475 
476  return TRUE;
477 
478  }//end processInterface()
479 
480 
481 }//end class
482 
483 ?>