Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field_thesaurus_edit_fns.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/assets/metadata/metadata_field/metadata_field_edit_fns.inc';
18 
31 {
32 
33 
38  function __construct()
39  {
40  parent::__construct();
41 
42  }//end constructor
43 
44 
56  {
57  // get root links
58  $root_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, '', FALSE, 'major', 'root');
59  $root_asset_ids = Array();
60  foreach ($root_links as $root_link) {
61  $root_asset_ids[$root_link['minorid']] = $root_link['minor_type_code'];
62  }
63 
64  if ($asset->writeAccess('links')) {
65  multiple_asset_finder($prefix.'_root_nodes', array_keys($root_asset_ids));
66  } else {
67  $infos = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($root_asset_ids));
68  ?>
69  <table class="sq-backend-table">
70  <tr>
71  <td class="sq-backend-table-header"><?php echo translate('id') ?></td>
72  <td class="sq-backend-table-header"><?php echo translate('name') ?></td>
73  <td class="sq-backend-table-header"><?php echo translate('type') ?></td>
74  </tr>
75  <?php
76  foreach ($infos as $assetid => $info) {
77  ?>
78 
79  <tr>
80  <td class="sq-backend-table-cell"><?php echo $assetid ?></td>
81  <td class="sq-backend-table-cell"><?php echo $info['name'] ?></td>
82  <td class="sq-backend-table-cell"><?php echo ucwords(str_replace('_',' ', $info['type_code'])) ?></td>
83  </tr>
84  <?php
85  }
86  ?>
87  </table>
88  <?php
89  }
90  return TRUE;
91 
92  }//end paintRootNode()
93 
94 
111  function paintValueInterface(Metadata_Field_Thesaurus $asset, Backend_Outputter $o, $value_str, $write_access, $enable_default=TRUE, $print_cascade_values_option = TRUE)
112  {
113  $thesaurus = $asset->getThesaurusRef();
114 
115  if ($thesaurus == FALSE) {
116  echo translate('source_thesaurus_not_set');
117  return;
118  }//end if
119 
120  $prefix = $asset->getPrefix();
121  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
122 
123  $is_default = FALSE;
124  $default = $asset->attr('default');
125  if (is_null($value_str)) {
126  $is_default = TRUE;
127  $value = $default;
128  $value_components = $asset->attr('value_components');
129  } else {
130  $value='';
131  $value_components = $asset->attr('value_components');
132  Metadata_Field::decodeValueString($value_str, $value, $value_components);
133  }
134 
135 
136  if (empty($value)) {
137  $values = Array();
138  } else if (!is_array($value)) {
139  $values = explode(',', $value);
140  }
141 
142  if (!is_array($values)) $values = Array($values);
143 
144  $term_assetids = Array();
145 
146  foreach ($values as $term) {
147  $term = str_replace('&#44;', ',', $term);
148  $termid = $thesaurus->getTermIdByName($term);
149  // Bug #3123
150  // If the thesaurus field has changed, this produces errors on the backend
151  // Only deal with valid terms
152  if (!empty($termid)) {
153  $term_assetids[$termid] = $thesaurus->id.':'.$termid;
154  }
155  $term_info[$termid] = $term;
156  }
157 
158  if (isset($asset->_tmp['error'])) {
159  echo '<ul>';
160  foreach ($asset->_tmp['error'] as $message) {
161  echo '<li class="sq-backend-warning">'.$message.'</li>';
162  }
163  echo '</ul>';
164  }
165 
166  if ($write_access) {
167 
168  if ($enable_default) {
169 
170  ?>
171  <script type="text/javascript">
172  //<![CDATA[
173  function toggleManualInterface(div_1, div_2)
174  {
175  if (document.getElementById(div_1).style.display == 'block') {
176  document.getElementById(div_1).style.display = 'none';
177  document.getElementById(div_2).style.display = 'block';
178  } else {
179  document.getElementById(div_2).style.display = 'none';
180  document.getElementById(div_1).style.display = 'block';
181  }
182  }
183  //]]>
184  </script>
185  <?php
186 
187  // use-default-or-not chooser
188  check_box($prefix.'_default', '1', $is_default, 'toggleManualInterface(\''.$prefix.'_interface\',\''.$prefix.'_defaults\')');
189  label(translate('thesaurus_use_default_value'), $prefix.'_default');
190  if ($is_default) {
191  echo ' '.translate('thesaurus_default_values_note');
192  }
193  echo '<br />';
194 
195  // non-default-value chooser
196  echo '<div id="'.$prefix.'_interface" style="padding: 5px 0px; display:'.($is_default ? 'none' : 'block').'">';
197  multiple_asset_finder($prefix.'_thes_term', $term_assetids, Array('thesaurus_term'=>'D'));
198  hidden_field($prefix.'_terms_submitted', 1);
199  echo '<div style="margin-top: 3px">';
200  echo translate('thesaurus_meta_thesaurus_note', get_asset_tag_line($thesaurus->id, 'details'));
201  echo '</div>';
202  echo '</div>';
203 
204  // default value display
205  echo '<div id="'.$prefix.'_defaults" style="display:'.($is_default ? 'block' : 'none').'">';
206  echo $default;
207  echo '</div>';
208 
209  } else {
210  multiple_asset_finder($prefix.'_thes_term', $term_assetids, Array('thesaurus_term'=>'D'));
211  hidden_field($prefix.'_terms_submitted', 1);
212  }
213 
214  if ($print_cascade_values_option) {
215  check_box($prefix.'_cascade_value', '1', FALSE);
216  label(translate('cascade_value'), $prefix.'_cascade_value');
217  }
218 
219  } else {
220  if (empty($values)) {
221  echo translate('thesaurus_no_values');
222  } else {
223  echo translate($is_default ? 'thesaurus_using_default_values' : 'thesaurus_using_non_default_values');
224  ?>
225  <table class="sq-backend-table" border="1" style="margin-left: 0px">
226  <tr>
227  <th style="width: 30%; white-space: none;"><?php echo translate('thesaurus_term'); ?></th>
228  <th><?php echo translate('thesaurus_lineages'); ?></th>
229  </tr>
230  <?php
231  foreach ($term_info as $term_id => $term_name) {
232  // Bug #3123
233  // If the thesaurus field has changed, this produces errors on the backend
234  // Only deal with valid terms
235  if (!empty($term_id)) {
236  ?>
237  <tr>
238  <td><?php echo get_asset_tag_line($term_assetids[$term_id], 'details') ?></td>
239  <td>
240  <?php
241  $lineages = $thesaurus->getLineagesForTerm($term_id);
242  if (!$lineages) continue;
243  foreach ($lineages as $lineage) {
244  foreach ($lineage as $ancestor) {
245  echo ' / ';
246  if (isset($ancestor['relation'])) {
247  echo '('.$ancestor['relation'].') ';
248  }
249  echo $ancestor['term'];
250  }
251  echo '<br />';
252  }
253  ?>
254  </td>
255  </tr>
256  <?php
257  }//end if
258  }//end foreach
259  ?>
260  </table>
261  <?php
262  }//end else
263  }//end else
264  return $write_access;
265 
266  }//end paintValueInterface()
267 
268 
280  function processInlineValueInterface(Metadata_Field_Thesaurus $asset, &$new_value_str, &$field_cascade_value)
281  {
282  if (!$asset->attr('editable')) return FALSE; // nothing for us to do
283 
284  $processed = FALSE;
285 
286  $prefix = $asset->getPrefix();
287  $thesaurus = $asset->getThesaurusRef();
288  $value = Array();
289 
290  if (!empty($_POST[$prefix.'_default'])) {
291  // use default
292  $new_value_str = NULL;
293  $processed = TRUE;
294  } else {
295  if (!empty($_REQUEST[$prefix.'_terms_submitted'])) {
296  $new_terms = array_get_index($_REQUEST, $prefix.'_thes_term', Array());
297  foreach ($new_terms as $one_term) {
298  if (empty($one_term['assetid'])) continue;
299  $term_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($one_term['assetid']);
300  $term_name = $term_asset->attr('name');
301  if ($term_asset->_thesaurus->id != $thesaurus->id) {
302  $asset->_tmp['error'][] = translate('thesaurus_meta_wrong_thesaurus', $term_name, $term_asset->_thesaurus->name, $term_asset->_thesaurus->id);
303  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($term_asset);
304  continue;
305  }
306  $term_name = str_replace(',', '&#44;', $term_name);
307  $value[] = $term_name;
308  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($term_asset);
309  }
310 
311  $old_count = count($value);
312  $value = array_unique($value);
313  if ($old_count != count($value)) {
314  $asset->_tmp['error'][] = translate('thesaurus_term_duplicate_removed');
315  }
316 
317  $value = implode(',', $value);
318  $new_value_str = Metadata_Field::encodeValueString($value, Array());
319 
320  $processed = TRUE;
321  }
322  }
323 
324  if (isset($_POST[$prefix.'_cascade_value'])) {
325  $field_cascade_value = TRUE;
326  $processed = TRUE;
327  }
328 
329  return $processed;
330 
331  }//end processInlineValueInterface()
332 
333 
345  {
346  $write_access = $asset->writeAccess('attributes');
347 
348  if ((boolean)$asset->attr('is_contextable') === TRUE) {
349  $default = $asset->attr('default');
350  } else {
351  $default = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('default', 'metadata_field', Array($asset->id), 0);
352  $default = $default[$asset->id];
353  }
354 
355  if (!$write_access) {
356  echo $default;
357  return TRUE;
358  }
359 
360  $thesaurus = $asset->getThesaurusRef();
361 
362  if (empty($default)) {
363  $values = Array();
364  } else if (!is_array($default)) {
365  $values = explode(',', $default);
366  }
367 
368  if (!is_array($values)) $values = Array($values);
369 
370  $term_assetids = Array();
371 
372  foreach ($values as $term) {
373  $termid = $thesaurus->getTermIdByName($term);
374  $term_assetids[] = $thesaurus->id.':'.$termid;
375  $term_info[$termid] = $term;
376  }
377 
378  multiple_asset_finder($prefix.'_default_term', $term_assetids, Array('thesaurus_term'=>'D'));
379 
380  return TRUE;
381 
382  }//end paintDefault()
383 
384 
396  {
397  if (!$asset->writeAccess('attributes')) return FALSE;
398 
399  $prefix = $asset->getPrefix();
400  $thesaurus = $asset->getThesaurusRef();
401  $is_contextable = (boolean)$asset->attr('is_contextable');
402  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
403  $assetid = $asset->id;
404  $value = Array();
405 
406  $new_terms = array_get_index($_REQUEST, $prefix.'_default_term', Array());
407  foreach ($new_terms as $one_term) {
408  if (empty($one_term['assetid'])) continue;
409  $term_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($one_term['assetid']);
410  $term_name = $term_asset->attr('name');
411  if ($term_asset->_thesaurus->id != $thesaurus->id) {
412  $asset->_tmp['error'][] = translate('thesaurus_meta_wrong_thesaurus', $term_name, $term_asset->_thesaurus->name, $term_asset->_thesaurus->id);
413  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($term_asset);
414  continue;
415  }
416  $value[] = $term_name;
417  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($term_asset);
418  }
419 
420  $old_count = count($value);
421  $value = array_unique($value);
422  if ($old_count != count($value)) {
423  $asset->_tmp['error'][] = translate('thesaurus_term_duplicate_removed');
424  }
425  $value = implode(',', $value);
426 
427  $default = Metadata_Field::encodeValueString($value, Array());
428 
429  if ($is_contextable === FALSE) {
430  if ($contextid !== 0) {
431  $GLOBALS['SQ_SYSTEM']->changeContext(0);
432  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
433  }
434  }
435 
436  $asset->setAttrValue('default', $default);
437  $asset->saveAttributes();
438 
439  if ($is_contextable === FALSE) {
440  if ($contextid !== 0) {
441  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
442  $GLOBALS['SQ_SYSTEM']->restoreContext();
443  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
444  }
445  }
446 
447  // No need to save attrs again
448  return FALSE;
449 
450  }//end processDefault()
451 
452 
453 }//end class
454 
455 ?>