Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 
32 {
33 
34 
41  function __construct()
42  {
43  parent::__construct();
44  $this->static_screens['details']['force_unlock'] = FALSE;
45 
46  }//end constructor
47 
48 
60  {
61  ob_start();
62  ?><a href="#" onClick="Javascript: var win = window.open('<?php echo sq_web_path('data')?>/asset_types/metadata_field/popups/keywords.php?assetid=<?php echo $asset->id; ?>&all=1', 'keywords', 'toolbar=0,menubar=0,location=0,status=0,scrollbars=1,resizable=1,width=580,height=520'); return false;">click here </a></b><?php
63  $replacements_link = ob_get_contents();
64  ob_end_clean();
65  $o->note(translate('metadata_keyword_replacements_usage', $replacements_link));
66 
67  }//end paintKeywordInstructions()
68 
69 
84  {
85  return FALSE;
86 
87  }//end processKeywordInstructions()
88 
89 
101  {
102  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
103  $is_default_context = ($contextid === 0) ? TRUE : FALSE;
104  $is_contextable = $asset->attr('is_contextable');
105  $can_change_keys = ($is_contextable === FALSE) || ($is_default_context = TRUE);
106 
107  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
108  $write_access_perm = $asset->writeAccess();
109  $write_access = $asset->writeAccess('attributes');
110 
111  if ($write_access_perm === TRUE) {
112  if ($can_change_keys === TRUE) {
113  if ($write_access === TRUE) {
114  text_box($prefix.'_new_vc', '', 30);
115  } else {
116  // Would have permission if we had the asset locked
117  echo '<i>'.translate('core_lock_asset_to_create_value_component').'</i>';
118  }
119  } else {
120  // Can't add/delete components in an alternate context
121  echo '<em>'.translate('core_cannot_create_value_component_in_alternate_context').'</em>';
122  }
123  } else {
124  // Would not have permission to do it even if able to be locked
125  echo '<i>'.translate('core_cannot_create_value_component').'</i>';
126  }
127  $o->closeField();
128 
129  // Get the value components for the current context (if contextable)
130  // or the default context (if not)
131  $default_vc = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('value_components', 'metadata_field', Array($asset->id), 0);
132  $default_vc = unserialize($default_vc[$asset->id]);
133 
134  if ($can_change_keys === FALSE) {
135  $current_vc = $asset->attr('value_components');
136  } else {
137  $current_vc = $default_vc;
138  }
139 
140  if (!empty($current_vc)) {
141  $o->openField(translate('current'));
142  $num_per_row = 3;
143  $num_done = 0;
144  ?>
145  <table class="sq-backend-table">
146  <tr>
147  <?php
148  foreach ($default_vc as $vc_name => $default_vc_value) {
149  $vc_value = array_get_index($current_vc, $vc_name, $default_vc_value);
150 
151  if ($num_done >= $num_per_row) {
152  $num_done = 0;
153  echo '</tr><tr>';
154  }
155 
156  echo '<td width="150" valign="top" class="sq-backend-table-cell">';
157  if ($write_access === TRUE) {
158  if ($can_change_keys === FALSE) {
159  // If in alternate context, don't let the user edit the field name
160  // but do send it to the process function, because it needs to be
161  // saved to this context on commit
162  echo '<b>'.$vc_name.'</b>';
163  hidden_field($prefix.'_vc_name['.$vc_name.']', $vc_name);
164  } else {
165  text_box($prefix.'_vc_name['.$vc_name.']', $vc_name, 30);
166  }
167  } else {
168  echo '<b>'.$vc_name.'</b>';
169  }
170  echo '<br />';
171  if ($write_access) {
172  text_area($prefix.'_vc_value['.$vc_name.']', $vc_value, 30, 4);
173  } else {
174  echo $vc_value;
175  }
176  if ($write_access) {
177  if ($can_change_keys === TRUE) {
178  echo '<br />';
179  check_box($prefix.'_vc_delete['.$vc_name.']');
180  label(translate('delete_question'), $prefix.'_vc_delete['.$vc_name.']');
181  }
182  }
183  echo '</td>';
184 
185  $num_done++;
186  }
187  if ($num_done && $num_done != $num_per_row) {
188  for ($i = ($num_per_row - $num_done); $i > 0; $i--) {
189  echo'<td width="200" class="sq-backend-table-cell">&nbsp;</td>';
190  }
191  }
192  ?>
193  </tr>
194  </table>
195  <?php
196  }//end if
197 
198  return TRUE;
199 
200  }//end paintValueComponents()
201 
202 
214  {
215  // need to have write access to make any changes
216  if (!$asset->writeAccess('attributes')) return FALSE;
217  $is_contextable = (boolean)$asset->attr('is_contextable');
218  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
219 
220  if (($is_contextable === FALSE) && ($contextid !== 0)) {
221  $GLOBALS['SQ_SYSTEM']->changeContext(0);
222  $contexted_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset->id);
223  } else {
224  $contexted_asset = $asset;
225  }
226 
227  $current_vc = $contexted_asset->attr('value_components');
228  $updated = FALSE;
229 
230  // edit the existing value components first
231  foreach ($current_vc as $vc_name => $vc_value) {
232  if (isset($_POST[$prefix.'_vc_delete'][$vc_name]) && $_POST[$prefix.'_vc_delete'][$vc_name]) {
233  unset($current_vc[$vc_name]);
234  $updated = TRUE;
235  continue;
236  }
237 
238  if (isset($_POST[$prefix.'_vc_name'][$vc_name])) {
239  $new_vc_name = trim($_POST[$prefix.'_vc_name'][$vc_name]);
240  }
241  if (isset($_POST[$prefix.'_vc_value'][$vc_name])) {
242  $new_vc_value = trim($_POST[$prefix.'_vc_value'][$vc_name]);
243  }
244  if (empty($new_vc_name)) continue;
245 
246  // change the default value of a value component
247  if ($vc_value != $new_vc_value) {
248  $current_vc[$vc_name] = $new_vc_value;
249  $updated = TRUE;
250  }
251 
252  // change the name of a value component
253  if ($vc_name != $new_vc_name) {
254  $current_vc[$new_vc_name] = $current_vc[$vc_name];
255  unset($current_vc[$vc_name]);
256  $updated = TRUE;
257  }
258  }//end foreach
259 
260  // create a new value component if required
261  if (isset($_POST[$prefix.'_new_vc']) && trim($_POST[$prefix.'_new_vc']) != '') {
262  $new_vc = trim($_POST[$prefix.'_new_vc']);
263  if (isset($current_vc[$new_vc])) {
264  trigger_localised_error('CORE0206', E_USER_WARNING, $contexted_asset->name);
265  } else if (preg_match('/^[1-9][0-9]+$/', $new_vc)) {
266  // we are not allowing just numbers because we want to use array_merge()
267  // and if there is only numbers it thinks the elements are in from an indexed array and it all goes bad.
268  trigger_localised_error('CORE0207', E_USER_WARNING);
269  } else {
270  $current_vc[$new_vc] = '';
271  $updated = TRUE;
272  }
273  }//end if
274 
275  if ($updated) {
276  $contexted_asset->setAttrValue('value_components', $current_vc);
277  }
278 
279  if (($is_contextable === FALSE) && ($contextid !== 0)) {
280  // Commit the contexted asset; we don't need to commit the one
281  // in the current context in this instance
282  if ($updated) {
283  $contexted_asset->saveAttributes();
284  $updated = FALSE;
285  }
286 
287  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($contexted_asset);
288  $GLOBALS['SQ_SYSTEM']->restoreContext();
289  }
290 
291  return $updated;
292 
293  }//end processValueComponents()
294 
295 
309  function paintInlineValueInterface(Metadata_Field $asset, Backend_Outputter $o, $value_str, $write_access, $print_cascade_values_option = TRUE, $type_code=NULL)
310  {
311  $complete = $this->requiredFieldsComplete($asset, $value_str);
312  // set field header to friendly_name if it is not empty
313  $field_name = $asset->attr('friendly_name');
314  if (!$field_name) $field_name = $asset->name;
315 
316  if ($asset->attr('required')) {
317  $field_name .= ' <span class="sq-backend-warning">*</span>';
318  if (!$complete && $write_access) {
319  $field_name .= '<br /><span class="sq-backend-warning">'.translate('currently_empty').'</span>';
320  }
321  }
322 
323  // use description as the note for this field
324  $o->openField($field_name, '', $asset->attr('description'));
325  $writable = $write_access && $asset->attr('editable');
326  $res = $this->paintValueInterface($asset, $o, $value_str, $writable, TRUE, $print_cascade_values_option, $type_code);
327  if (!$write_access && $asset->attr('required') && !$complete) {
328  echo '<b class="sq-backend-warning">['.strtoupper(translate('empty')).']</b>';
329  }
330  $o->closeField();
331  return $res;
332 
333  }//end paintInlineValueInterface()
334 
335 
351  function paintValueInterface(Metadata_Field $asset, Backend_Outputter $o, $value_str, $write_access, $enable_default=TRUE, $print_cascade_values_option = TRUE, $type_code=NULL)
352  {
353  return FALSE;
354 
355  }//end paintValueInterface()
356 
357 
372  function processInlineValueInterface(Metadata_Field $asset, &$new_value_str, &$field_cascade_value)
373  {
374  return FALSE;
375 
376  }//end processInlineValueInterface()
377 
378 
389  function requiredFieldsComplete(Metadata_Field $asset, $value_str)
390  {
391  // if this is not a required field then of course it doesnt break the rule
392  if (!$asset->attr('required')) return TRUE;
393 
394  if (is_null($value_str)) {
395  $value = $asset->attr('default');
396  $value_components = $asset->attr('value_components');
397  } else {
398  $value='';
399  $value_components = $asset->attr('value_components');
400  Metadata_Field::decodeValueString($value_str, $value, $value_components);
401  }
402 
403  if (trim($value) != '') return TRUE;
404 
405  // now check value components to see if they are complete
406  foreach ($value_components as $vc_name => $vc_value) {
407  if (trim($vc_value) != '') return TRUE;
408  }
409 
410  return FALSE;
411 
412  }//end requiredFieldsComplete()
413 
414 
427  function generateMetadata(Metadata_Field $asset, $value_str, &$tag_value, Array &$keywords)
428  {
429  // No name means we can't reference a value from the metadata array,
430  // so we would be writing meta tags with PHP errors in them.
431  if (trim($asset->name) === '') {
432  return TRUE;
433  }
434 
435  require_once SQ_FUDGE_PATH.'/general/text.inc';
436  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
437 
438  $frontend_option = $asset->attr('frontend');
439  // if we aren't printing this on the frontend then don't print anything
440 
441  if (is_null($value_str)) {
442  $value = $asset->attr('default');
443  $value_components = $asset->attr('value_components');
444  $value_str = Metadata_Field::encodeValueString($value, $value_components);
445  } else {
446  $value='';
447  $value_components = $asset->attr('value_components');
448  Metadata_Field::decodeValueString($value_str, $value, $value_components);
449  }
450 
451  $keywords = array_merge($keywords, retrieve_keywords_replacements($value_str, '.'));
452 
453  $empty_vc = TRUE;
454 
455  foreach ($value_components as $vc_name => $vc_value) {
456  // if any value component is not empty then the whole thing is not empty
457  if ($vc_value != '') $empty_vc = FALSE;
458  }
459 
460  // if we are empty and it must be filled to be printed, then don't print anything
461  if (($frontend_option == 'fill') && $value == '' && $empty_vc) {
462  return TRUE;
463  }
464 
465  $tag_value = $value_str;
466 
467  if ($frontend_option == 'hide') return TRUE;
468 
469  echo '<meta name="'.$mm->escapeMetadata($asset->name).'"';
470  if (trim($asset->attr('language')) != '') {
471  echo ' lang="'.$mm->escapeMetadata($asset->attr('language')).'"';
472  }
473  if (trim($asset->attr('scheme')) != '') {
474  echo ' scheme="'.$mm->escapeMetadata($asset->attr('scheme')).'"';
475  }
476 
477  echo ' content="<?php echo $mm->escapeMetadata($metadata_values[\''.$mm->escapeMetadata(addslashes($asset->name), TRUE).'\']); foreach ($metadata_value_components[\''.$mm->escapeMetadata(addslashes($asset->name), TRUE).'\'] as $k => $v) {echo \'; \'.$k.\'=\'.$v; } ?>" />'."\n";
478 
479 
480  return TRUE;
481 
482  }//end generateMetadata()
483 
484 
493  public function canUsePlainDefault(Metadata_Field $asset)
494  {
495  return !($this->requiresCustomDefault($asset));
496 
497  }//end isContextableField()
498 
499 
512  public function requiresCustomDefault(Metadata_Field $asset)
513  {
514  if (($asset->attr('is_contextable') === FALSE) && ($GLOBALS['SQ_SYSTEM']->getContextId() !== 0)) {
515  return TRUE;
516  } else {
517  return FALSE;
518  }
519 
520  }//end isNotContextableField()
521 
522 }//end class
523 
524 ?>