Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field_select_edit_fns.inc
1 <?php
18 require_once dirname(__FILE__).'/../../metadata_field/metadata_field_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42 
43  }//end constructor
44 
45 
62  public function paintValueInterface(Metadata_Field_Select $asset, Backend_Outputter $o, $value_str, $write_access, $enable_default=TRUE, $print_cascade_values_option = TRUE)
63  {
64  $prefix = $asset->getPrefix();
65  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
66 
67  $value = '';
68  $value_components = $asset->attr('value_components');
69 
70  $select_options = $asset->getContextedSelectOptions();
71  $is_default = FALSE;
72  if (is_null($value_str)) {
73  $value_str = $asset->getDefaultValue();
74  $is_default = TRUE;
75  }
76 
77  Metadata_Field::decodeValueString($value_str, $value, $value_components);
78 
79  if ($asset->attr('multiple')) {
80  // if there is no value we need a blank array...not an array with one element (a blank string)
81  if ($value == '') {
82  $value = Array();
83  } else {
84  $value = explode('; ', $value);
85  }
86  }
87  // If we're OK to edit, get our selection attribute, and paint it
88  if ($write_access) {
89  $selection = $asset->getSelectionAttribute();
90  $selection->value = $value;
91  if ($enable_default) {
92  $selection->_edit_params['extras'] = 'onclick="document.getElementById(\''.$prefix.'_default\').checked = false;"';
93  if ($is_default) {
94  $selection->_edit_params['extras'] .= ' disabled="disabled"';
95  }
96  echo '<span id="'.$prefix.'_field">';
97  }
98  $selection->paint($prefix, FALSE);
99  if ($enable_default) echo '</span>';
100  } else {
101  if (!((is_array($value) && empty($value)) || (is_string($value) && $value == ''))) {
102  if (!is_array($value)) $value = Array($value);
103  $value_output = '';
104 
105  foreach ($value as $option) {
106  if (isset($select_options[$option])) {
107  $value_output .= $select_options[$option].'; ';
108  }
109  }
110  echo trim($value_output, '; ');
111  }
112  }
113 
114  if ($enable_default) {
115  if ($write_access) {
116  echo ' &nbsp; &nbsp; &nbsp; ';
117 
118  // Work out which options we have to set if we have to
119  $i = 0;
120  $default_keys = explode('; ', $asset->attr('default'));
121  $default_selected = Array();
122 
123  $default_vals = Array();
124  $non_default_vals = Array();
125  foreach ($select_options as $key => $select_option) {
126  if (in_array($key, $default_keys)) {
127  $default_vals[] = "'".addslashes(str_replace(' ', '_', $key))."'";
128  } else {
129  $non_default_vals[] = "'".addslashes(str_replace(' ', '_', $key))."'";
130  }
131  }
132  $default_vals_expr = '['.implode(', ', $default_vals).']';
133  $non_default_vals_expr = '['.implode(', ', $non_default_vals).']';
134  check_box($prefix.'_default', '1', $is_default, "handleDefaultClick(this, '$prefix', $default_vals_expr, $non_default_vals_expr); ");
135  label(translate('use_default'), $prefix.'_default');
136  $o->addJsInclude(sq_web_path('data').'/asset_types/metadata_field_select/js/metadata_field_select.js');
137  } else {
138  if ($is_default) {
139  echo ' <em style="color: #666">('.strtolower(translate('default')).')</em>';
140  }
141  }
142  }
143 
144  if ($write_access && $print_cascade_values_option) {
145  echo '&nbsp;&nbsp;&nbsp;&nbsp;';
146  check_box($prefix.'_cascade_value', '1', FALSE);
147  label(translate('cascade_value'), $prefix.'_cascade_value');
148  }
149 
150  return $write_access;
151 
152  }//end paintValueInterface()
153 
154 
166  public function processInlineValueInterface(Metadata_Field_Select $asset, &$new_value_str, &$field_cascade_value)
167  {
168  if (!$asset->attr('editable')) return FALSE; // nothing for us to do
169 
170  $processed = FALSE;
171 
172  $prefix = $asset->getPrefix();
173  if (!empty($_POST[$prefix.'_default'])) {
174  // use default
175  $new_value_str = NULL;
176  $processed = TRUE;
177  } else {
178  // Default is not checked, so process our select attribute
179  $selection = $asset->getSelectionAttribute();
180  $selection->process($prefix);
181  if ($selection->processed) {
182  // Get the string into proper form for the metadata field function
183  $value = str_replace('|', '; ', $selection->value);
184  $new_value_str = Metadata_Field::encodeValueString(trim($value), Array());
185  $processed = TRUE;
186  }
187  }
188 
189  if (isset($_POST[$prefix.'_cascade_value'])) {
190  $field_cascade_value = TRUE;
191  $processed = TRUE;
192  }
193 
194  return $processed;
195 
196  }//end processInlineValueInterface()
197 
198 
209  public function paintSelection(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
210  {
211  $write_access = $asset->writeAccess('attributes');
212  $is_contextable = (boolean)$asset->attr('is_contextable');
213 
214  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
215  $is_default_context = ($contextid === 0) ? TRUE : FALSE;
216 
217  if ($is_contextable === TRUE) {
218  $select_options = $asset->getContextedSelectOptions();
219  } else {
220  $select_options = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('select_options', 'metadata_field_select', Array($asset->id), 0);
221  $select_options = unserialize($select_options[$asset->id]);
222  }
223 
224  if (!empty($select_options)) {
225  ?>
226  <table class="sq-backend-table">
227  <tr>
228  <td class="sq-backend-table-header"><?php echo translate('option_key'); ?></td>
229  <td class="sq-backend-table-header"><?php echo translate('option_value'); ?></td>
230  <?php
231  if (($write_access === TRUE) && (($is_default_context === TRUE) || ($is_contextable === FALSE))) {
232  ?><td class="sq-backend-table-header"><?php echo translate('delete_question'); ?></td><?php
233  }
234  ?>
235  </tr>
236  <?php
237  $i = 0;
238  // Base this on the default values because then any new
239  // option not already in a modified context can be set to the
240  // default context's value for it
241 
242  foreach ($select_options as $option_key => $option_value) {
243  ?>
244  <tr>
245  <td class="sq-backend-table-cell">
246  <?php
247  if ($write_access === TRUE) {
248  if (($is_default_context === TRUE) || ($is_contextable === FALSE)) {
249  text_box($prefix.'_options[key]['.$i.']', $option_key, 20);
250  } else {
251  echo $option_key;
252  hidden_field($prefix.'_options[key]['.$i.']', $option_key);
253  }
254  } else {
255  echo $option_key;
256  }
257  ?>
258  </td>
259  <td class="sq-backend-table-cell" align="left">
260  <?php
261  if ($write_access) {
262  text_box($prefix.'_options[val]['.$i.']', $option_value, 20);
263  } else {
264  echo $option_value;
265  }
266  ?>
267  </td>
268  <?php
269  if (($write_access === TRUE) && (($is_default_context === TRUE) || ($is_contextable === FALSE))) {
270  ?>
271  <td class="sq-backend-table-cell">
272  <?php check_box($prefix.'_options[del]['.$i.']', $option_key); ?>
273  </td>
274  <?php
275  }
276  ?>
277  </tr>
278  <?php
279  $i++;
280  }//end foreach select_options
281  ?>
282  </table>
283  <?php
284 
285  } else {
286  echo translate('no_select_box_options');
287  }//end if empty
288 
289  // now adding two fields for new entry (key => value)
290  if (($write_access === TRUE) && (($is_default_context === TRUE) || ($is_contextable === FALSE))) {
291  $o->openField(translate('new_option'));
292  echo '<b>'.translate('key').'</b> ';
293  text_box($prefix.'_new_key','');
294  echo ' <b>'.translate('value').'</b> ';
295  text_box($prefix.'_new_val','');
296  $o->closeField();
297  }
298 
299  if (!empty($select_options)) {
300  // displaying default value if any values were actually saved in the array
301  $o->openField(translate('default_value'));
302  $selection = $asset->getSelectionAttribute();
303  $selection->paint($prefix.'_default', !$write_access);
304  $o->closeField();
305  }
306 
307  return TRUE;
308 
309  }//end paintSelection()
310 
311 
322  public function processSelection(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
323  {
324  // need to have write access to make any changes
325  if (!$asset->writeAccess('attributes')) return FALSE;
326  $is_contextable = (boolean)$asset->attr('is_contextable');
327 
328  if ($is_contextable === FALSE) {
329  // If we are not contextable, we might as well be in the default
330  // context... so change to it, and reload our asset
331  $GLOBALS['SQ_SYSTEM']->changeContext(0);
332  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset->id);
333  }
334 
335  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
336 
337  $select_options = Array();
338  if (isset($_POST[$prefix.'_options'])) {
339  $option_changes = $_POST[$prefix.'_options'];
340  foreach ($option_changes['key'] as $i => $key) {
341  if (isset($option_changes['del'][$i])) continue;
342  $select_options[trim($key)] = trim($option_changes['val'][$i]);
343  }
344  }
345 
346  // now get the new submitted values (if any) and add them to array
347  if (array_key_exists($prefix.'_new_key', $_POST) === TRUE) {
348  if (strlen(trim($_POST[$prefix.'_new_key']))>0 && strlen(trim($_POST[$prefix.'_new_val']))>0) {
349  $new_select_key = trim($_POST[$prefix.'_new_key']);
350  $new_select_val = trim($_POST[$prefix.'_new_val']);
351  $select_options[$new_select_key] = $new_select_val;
352  }
353  }
354 
355  // updating asset attribute value
356  $asset->setAttrValue('select_options', $select_options);
357 
358  // saving default value
359  $new_default = '';
360  if (!empty($select_options) && isset($_POST[$prefix.'_default'])) {
361  $new_default = $_POST[$prefix.'_default'];
362  if (is_array($new_default)) {
363  $new_default = implode('; ', $new_default);
364  }
365  }
366  if (!$asset->attr('multiple')) {
367  if (!isset($select_options[$new_default])) {
368  $new_default = '';
369  }
370  }
371  $asset->setAttrValue('default', $new_default);
372  if ($asset->saveAttributes() === TRUE) {
373  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
374  } else {
375  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
376  }
377 
378  if ($is_contextable === FALSE) {
379  // If we are not contextable, we might as well be in the default
380  // context... so change to it, and reload our asset
381  $GLOBALS['SQ_SYSTEM']->restoreContext();
382  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset->id);
383  }
384 
385  // Don't require our own saving of attributes
386  return FALSE;
387 
388  }//end processSelection()
389 
390 
401  public function paintEditParams(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
402  {
403  $selection = $asset->getSelectionAttribute();
404  $selection->paintEditParams($prefix.'_style', $asset->writeAccess('attributes'));
405 
406  }//end paintEditParams()
407 
408 
419  public function processEditParams(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
420  {
421  if (!$asset->writeAccess('attributes')) return FALSE;
422 
423  $selection = $asset->getSelectionAttribute();
424  $edit_params = $selection->processEditParams($prefix.'_style');
425  return $asset->setAttrValue('edit_params', $edit_params);
426 
427  }//end processEditParams()
428 
429 
442  public function generateMetadata(Metadata_Field_Select $asset, $value_str, &$tag_value, Array &$keywords)
443  {
444  if (is_null($value_str)) {
445  $value_str = Metadata_Field::encodeValueString($asset->attr('default'), Array());
446  }
447 
448  // If we are showing the value, then go fetch it
449  if ($asset->attr('visible_part') === 'value') {
450  $value_str = $asset->getValueFromKey($value_str);
451  }
452 
453  return parent::generateMetadata($asset, $value_str, $tag_value, $keywords);
454 
455  }//end generateMetadata()
456 
457 
468  public function paintOptionUpload(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
469  {
470  $write_access = $asset->writeAccess();
471  file_upload($prefix.'_option_upload');
472  return $write_access;
473 
474  }//end paintOptionUpload()
475 
476 
488  {
489  $write_access = $asset->writeAccess();
490  check_box($prefix.'_csv_header', '1');
491  return $write_access;
492 
493  }//end paintOptionUploadHeader()
494 
495 
506  public function processOptionUpload(Metadata_Field_Select $asset, Backend_Outputter $o, $prefix)
507  {
508  if (isset($_FILES[$prefix.'_option_upload'])) {
509  $file = $_FILES[$prefix.'_option_upload'];
510 
511  $header = FALSE;
512  if (isset($_POST[$prefix.'_csv_header'])) {
513  $header = TRUE;
514  }
515 
516  if ($file['error'] === UPLOAD_ERR_OK) {
517  $asset->importOptionsFromCSV($file['tmp_name'], $header);
518  }
519  }
520 
521  }//end processOptionUpload()
522 
523 
524 }//end class
525 
526 ?>