Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_section_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/folder/folder_edit_fns.inc';
18 
31 {
32 
33 
38  function __construct()
39  {
40  parent::__construct();
41  $this->static_screens['details']['force_unlock'] = FALSE;
42 
43  }//end constructor
44 
45 
56  {
57  $write_access = $asset->writeAccess('links');
58  $prefix = $asset->getPrefix();
59 
60  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_field', FALSE, 'major', NULL, TRUE);
61  if (!empty($current_links)) {
62  $assetids = Array();
63  foreach ($current_links as $link) {
64  $assetids[$link['minorid']] = 'details';
65  }
66  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($assetids));
67  $asset_href = $GLOBALS['SQ_SYSTEM']->am->getAssetBackendHref($assetids);
68 
69  // Section description
70  $o->openField(translate('section_description'));
71  $description = $asset->attr('description');
72  if ($write_access) {
73  text_area($prefix.'_description', $description, 30, 5);
74  } else {
75  echo $description;
76  }
77  $o->closeField();
78  // Current Restrictions
79  // Show the dialog for restricting a section by asset type
80  $o->openField(translate('current_restrict'));
81  $restrictions = $asset->attr('restrict');
82  if (!empty($restrictions)) {
83  ?>
84  <table class="sq-backend-table">
85  <tr>
86  <td class="sq-backend-table-header"><?php echo translate('asset_type'); ?></td>
87  <?php
88  if ($write_access) {
89  ?>
90  <td align="center" class="sq-backend-table-header" width="70"><?php echo translate('remove'); ?></td>
91  <?php
92  }
93  ?>
94  </tr>
95  <?php
96  foreach ($restrictions as $asset_type => $inheritance) {
97  ?>
98  <tr>
99  <td class="sq-backend-table-cell">
100  <?php echo ucfirst(str_replace('_', ' ', $asset_type)) ?>
101  </td>
102  <?php
103  if ($write_access) {
104  ?>
105  <td align="center" class="sq-backend-table-cell">
106  <?php check_box($prefix.'_remove_asset_type['.$asset_type.']'); ?>
107  </td>
108  <?php
109  }
110  ?>
111  </tr>
112  <?php
113  }//end foreach()
114  ?>
115  </table>
116 
117  <?php
118  } else {
119  echo translate('no_restriction');
120  }
121  $o->closeField();
122 
123  if ($write_access) {
124  // New Restrictions
125  $o->openField(translate('new_restriction'));
126  asset_type_chooser($prefix.'_add_restriction', TRUE, Array(), NULL, FALSE, TRUE);
127  $o->closeField();
128  }
129 
130  $o->openField(translate('fields'));
131  ?>
132  <table class="sq-backend-table">
133  <tr>
134  <td class="sq-backend-table-header"><b><?php echo translate('field_name'); ?></b></td>
135  <td align="center" width="150" class="sq-backend-table-header"><b><?php echo translate('edit_fields'); ?></b></td>
136  <?php
137  if ($write_access) {
138  ?><td align="center" width="70" class="sq-backend-table-header"><b><?php echo translate('delete_question'); ?></b></td><?php
139  }
140  ?>
141  </tr>
142  <?php
143  foreach ($current_links as $link_data) {
144  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
145  if (is_null($field)) continue;
146  $edit_fns = $field->getEditFns();
147  ?>
148  <tr>
149  <td align="left" class="sq-backend-table-cell"><?php echo get_asset_tag_line($field->id);?></td>
150  <td align="center" class="sq-backend-table-cell">[ <a href="<?php echo $asset_href[$field->id];?>"><?php echo translate('metadata_edit_fields'); ?></a> ]</td>
151  <?php
152  if ($write_access) {
153  ?><td align="center" class="sq-backend-table-cell"><?php
154  check_box($prefix.'_delete_fields['.$link_data['linkid'].']')
155  ?></td><?php
156  }
157  echo '</tr>';
158  }
159  ?></table><?php
160  $o->closeField();
161  }//end if
162 
163  if ($write_access) {
164  $o->openField(translate('new_field_name'));
165 
166  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
167  text_box($prefix.'_new_field', '', 20);
168 
169  $o->closeField();
170 
171  $o->openField(translate('new_field_type'));
172 
173  // now getting available types list
174  $descendant_types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('metadata_field');
175 
176  $contents = Array();
177  foreach ($descendant_types as $type) {
178  // $te[2] - type of the field, which we need
179  $te = explode('_', $type, 3);
180  $contents[$type] = ucwords(str_replace('_', ' ', $te[2])); // capitalizing the first char
181  }
182 
183  combo_box($prefix.'_field_type', $contents, FALSE, 'metadata_field_text');
184 
185  $o->closeField();
186  }
187 
188  return TRUE;
189 
190  }//end paintInlineOptionInterface()
191 
192 
203  {
204  // need to have write access to make any changes
205  if (!$asset->writeAccess('links')) return FALSE;
206  if (!$asset->writeAccess('attributes')) return FALSE;
207 
208  $prefix = $asset->getPrefix();
209  $updated = FALSE;
210 
211  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
212  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
213 
214  // handle description
215  if (isset($_POST[$prefix.'_description'])) {
216  $section_description = trim($_POST[$prefix.'_description']);
217  $asset->setAttrValue('description', $section_description);
218  $asset->saveAttributes();
219  }
220 
221  // handle restrictions
222  // delete
223  $current_restrictions = $asset->attr('restrict');
224  $changes_made = FALSE;
225  if (isset($_POST[$prefix.'_remove_asset_type'])) {
226  foreach($_POST[$prefix.'_remove_asset_type'] as $remove_type => $ahfortheheckofit) {
227  if (array_key_exists($remove_type, $current_restrictions)) {
228  unset($current_restrictions[$remove_type]);
229  $changes_made = TRUE;
230  }
231  }
232 
233  // If something to save, save it, dammit!
234  if ($changes_made) {
235  $asset->setAttrValue('restrict', $current_restrictions);
236  $asset->saveAttributes();
237  }
238  }
239 
240  // add
241  $current_restrictions = $asset->attr('restrict');
242  $changes_made = FALSE;
243  if (isset($_POST[$prefix.'_add_restriction']) && (isset($_POST[$prefix.'_add_restriction']['type_code']))) {
244  foreach($_POST[$prefix.'_add_restriction']['type_code'] as $field => $type_code) {
245  if (!empty($type_code)) {
246  $inheritance = $_POST[$prefix.'_add_restriction']['inherit'][$field];
247  if (!isset($current_restrictions[$type_code])) {
248  $current_restrictions[$type_code] = $inheritance;
249  $changes_made = TRUE;
250  } else if ($current_restrictions[$type_code] !== $inheritance) {
251  $current_restrictions[$type_code] = $inheritance;
252  $changes_made = TRUE;
253  }
254  }
255  }
256 
257  // If something to save, save it, dammit!
258  if ($changes_made) {
259  $asset->setAttrValue('restrict', $current_restrictions);
260  $asset->saveAttributes();
261  }
262  }
263 
264  // delete any fields that need deleting
265  if (isset($_POST[$prefix.'_delete_fields'])) {
266  $delete_fields = $_POST[$prefix.'_delete_fields'];
267  foreach ($delete_fields as $linkid => $on) {
268  if ($asset->deleteLink($linkid)) $updated = TRUE;
269  }
270  }
271 
272  // create a new field?
273  if (isset($_POST[$prefix.'_new_field']) && trim($_POST[$prefix.'_new_field']) != '') {
274  $new_field_name = trim($_POST[$prefix.'_new_field']);
275  $new_field_type = trim($_POST[$prefix.'_field_type']);
276 
277  $GLOBALS['SQ_SYSTEM']->am->includeAsset($new_field_type);
278  $new_field = new $new_field_type();
279 
280  $section_link = Array('asset' => &$asset, 'link_type' => SQ_LINK_TYPE_2, 'sort_order' => -1, 'is_dependant' => 1, 'is_exclusive' => 0);
281  $new_field->setAttrValue('name', $new_field_name);
282  if ($new_field->create($section_link)) $updated = TRUE;
283  }
284 
285  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
286  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
287 
288  return $updated;
289 
290  return TRUE;
291 
292  }//end processInlineOptionInterface()
293 
294 
309  function paintInlineValueInterface(Metadata_Section $asset, Backend_Outputter $o, Array $values, $write_access, $print_cascade_values_option = TRUE, $type_code=NULL)
310  {
311  $restrictions = $asset->attr('restrict');
312  $flag_match_found = FALSE;
313  $res = FALSE;
314 
315  if (!empty($restrictions)) {
316  foreach ($restrictions as $type_code_restricted => $inherit_it) {
317  if ($inherit_it && !$flag_match_found) {
318  $asset_type_with_parents = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($type_code);
319  foreach($asset_type_with_parents as $index => $asset_type) {
320  if (($type_code_restricted == $type_code || $asset_type == $type_code_restricted)&& !$flag_match_found) {
321  $flag_match_found = TRUE;
322  }
323  }
324  } else if (array_key_exists($type_code, $restrictions)){
325  $flag_match_found = TRUE;
326  }
327  }
328  }
329 
330  if (empty($restrictions) || $flag_match_found ) {
331  $o->openSection($asset->name);
332  if ($write_access) {
333  if (isset($GLOBALS['painted_inline_value_interface'][$asset->id])) {
334  // this is the second time we have painted this interface
335  $o->openField(translate('please_note'));
336  echo '<span class="sq-backend-warning"><b>'.translate('metadata_use_editing_interface').'</b></span>';
337  $o->closeField();
338  $write_access = FALSE;
339  } else {
340  // make sure we dont try and paint the same interface twice
341  $GLOBALS['painted_inline_value_interface'][$asset->id] = TRUE;
342  }
343  }
344 
345  $section_description = $asset->attr('description');
346  if (!empty($section_description)) {
347  $o->openField('<i>'.translate('section_description').'</i>');
348  echo '<i>'.$section_description.'</i>';
349  $o->closeField();
350  }
351 
352  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_field', FALSE, 'major', NULL, TRUE);
353  foreach ($current_links as $link) {
354  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
355  if (is_null($field)) continue;
356  $edit_fns = $field->getEditFns();
357  if ($edit_fns->paintInlineValueInterface($field, $o, (isset($values[$field->id])) ? $values[$field->id][0]['value'] : NULL, $write_access, $print_cascade_values_option, $type_code)) {
358  $res = TRUE;
359  }
360  }
361  $o->closeSection();
362  }//end if
363 
364  return $res;
365 
366  }//end paintInlineValueInterface()
367 
368 
380  function processInlineValueInterface(Metadata_Section $asset, Array &$new_values, Array &$section_cascade_value_asset_ids, $type_code=NULL)
381  {
382  $res = FALSE;
383  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_field', FALSE, 'major', NULL, TRUE);
384  foreach ($current_links as $link) {
385  $field_cascade_value = FALSE;
386  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
387  if (is_null($field) || !$field->attr('editable')) {
388  continue;
389  }
390  $edit_fns = $field->getEditFns();
391  $new_value = '';
392  if ($edit_fns->processInlineValueInterface($field, $new_value, $field_cascade_value, $type_code)) {
393  // the process fn returned true, meaning something was submitted
394  // $new_value may be NULL, however, which signifies that we are reverting to default
395  // Evaluating %asset_metadata_% keywords in metadata field will create loop
396  if (strpos($new_value, '%asset_metadata_') !== FALSE) {
397  trigger_localised_error('SYS0344', E_USER_WARNING);
398  $new_value = preg_replace('/%asset_metadata_([^\s]*?)%/', '%metadata_field_$1%', $new_value);
399  }
400 
401  $new_values[$field->id] = Array (
402  0 => Array (
403  'name' => $field->name,
404  'value' => $new_value,
405  ),
406  );
407  if ($field_cascade_value) $section_cascade_value_asset_ids[$field->id] = 1;
408 
409  $res = TRUE;
410  }
411  }
412 
413  return $res;
414 
415  }//end processInlineValueInterface()
416 
417 
428  function requiredFieldsComplete(Metadata_Section $asset, $values, $type_code=NULL)
429  {
430  $inherited_restriction = Array();
431  $restrictions = $asset->attr('restrict');
432 
433  foreach($restrictions as $restriction => $inheritance) {
434  $inherited_types = Array();
435  if ($inheritance) {
436  $inherited_types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants($restriction);
437  foreach($inherited_types as $inherited_type) {
438  if (!array_key_exists($inherited_type, $restrictions)) {
439  $inherited_restriction = array_merge($inherited_restriction, Array($inherited_type));
440  }
441  }
442  }
443  }
444 
445  if(!empty($inherited_restriction)) {
446  $inherited_restriction = array_flip($inherited_restriction);
447  $restrictions = array_merge($restrictions, $inherited_restriction);
448  }
449 
450 
451  if (empty($restrictions) || (!is_null($type_code) && array_key_exists($type_code, $restrictions))) {
452  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_field', FALSE, 'major', NULL, TRUE);
453  foreach ($current_links as $link) {
454  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
455  if (is_null($field)) continue;
456  $edit_fns = $field->getEditFns();
457  if (!$edit_fns->requiredFieldsComplete($field, (isset($values[$field->id])) ? $values[$field->id][0]['value'] : NULL)) {
458  return FALSE;
459  }
460  }
461  }
462 
463  return TRUE;
464 
465  }//end requiredFieldsComplete()
466 
467 
480  function generateMetadata(Metadata_Section $asset, Array &$values, Array &$tag_values, Array &$keywords, $type_code=NULL)
481  {
482  $frontend_option = $asset->attr('frontend');
483  $flag_match_found = FALSE;
484  // if we aren't printing this on the frontend then don't print anything
485 
486  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
487 
488  $restrictions = $asset->attr('restrict');
489  if (!empty($restrictions)) {
490  foreach ($restrictions as $type_code_restricted => $inherit_it) {
491  if ($inherit_it && !$flag_match_found) {
492  $asset_type_with_parents = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($type_code);
493  foreach($asset_type_with_parents as $index => $asset_type) {
494  if (($type_code_restricted == $type_code || $asset_type == $type_code_restricted)&& !$flag_match_found) {
495  $flag_match_found = TRUE;
496  }
497  }
498  } else if (array_key_exists($type_code, $restrictions)){
499  $flag_match_found = TRUE;
500  }
501  }
502  }
503  if (empty($restrictions) || $flag_match_found ) {
504  // Buffer the contents of any META tags
505  ob_start();
506 
507  echo '<!-- '.$mm->escapeMetadata($asset->name).' //-->'."\n";
508  $section_description = $asset->attr('description');
509  if ($asset->attr('desc_frontend') == TRUE && !empty($section_description)) {
510  echo '<!-- '.$mm->escapeMetadata($section_description).' //-->'."\n";
511  }
512 
513  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_field', FALSE, 'major', NULL, TRUE);
514  $valid_fields = Array();
515  foreach ($current_links as $link) {
516  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
517  if (is_null($field)) continue;
518  $valid_fields[] = $field->name;
519 
520  $edit_fns = $field->getEditFns();
521  $tag_values[$field->name] = '';
522 
523 
524  if (!$edit_fns->generateMetadata($field, ((isset($values[$field->id])) ? $values[$field->id][0]['value'] : NULL), $tag_values[$field->name], $keywords)) {
525  ob_end_clean();
526  return FALSE;
527  }
528 
529  }
530 
531  echo "\n";
532 
533  // If printing on front-end, print the META tags, otherwise discard them
534  $contents = ob_get_clean();
535  if ($frontend_option) echo $contents;
536  }//end if
537 
538  return TRUE;
539 
540  }//end generateMetadata()
541 
542 
543 }//end class
544 
545 ?>