Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field_select.inc
1 <?php
17 require_once dirname(__FILE__).'/../../metadata_field/metadata_field.inc';
18 require_once SQ_FUDGE_PATH.'/csv/csv.inc';
19 
20 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
63  public function setAttrValue($name, $value)
64  {
65  if ($name === 'select_options') {
67  }
68  return parent::setAttrValue($name, $value);
69 
70  }//end setAttrValue()
71 
72 
79  public function getSelectionAttribute()
80  {
81  require_once SQ_ATTRIBUTES_PATH.'/selection/selection.inc';
82 
83  $selection = new Asset_Attribute_Selection();
84  $edit_params = $this->attr('edit_params');
85  if (!empty($edit_params)) {
86  $selection->_edit_params = array_merge($selection->_edit_params, $edit_params);
87  }
88 
89  $selection->_params['options'] = $this->getContextedSelectOptions();
90  $selection->_params['multiple'] = $this->attr('multiple');
91  $selection->_params['allow_empty'] = $this->attr('allow_empty');
92 
93  $default = $this->attr('default');
94  if ($this->attr('multiple')) {
95  $default = explode('; ', $default);
96  }
97  if ($selection->validateValue($default)) {
98  $selection->setValue($default);
99  }
100 
101  return $selection;
102 
103  }//end getSelectionAttribute()
104 
105 
118  public function getMetadataValue(Asset $asset, $value_str)
119  {
120  // If we are showing the value, then pass it up
121  if ($this->attr('visible_part') === 'value') {
122  $value_str = $this->getValueFromKey($value_str);
123  }
124  return str_replace('\\;', ';', parent::getMetadataValue($asset, $value_str));
125 
126  }//end getMetadataValue()
127 
128 
138  public function getValueFromKey($option_str)
139  {
140  $option_keys = explode('\\; ', $option_str);
141  if (empty($option_keys)) {
142  return $option_str;
143  }
144 
145  $value_str = '';
146  $options = $this->getContextedSelectOptions();
147 
148  foreach ($option_keys as $key) {
149  $key = str_replace('\\;', ';', $key);
150  $key = str_replace('\\=', '=', $key);
151  $key = str_replace('\\\\', '\\', $key);
152  if (isset($options[$key])) {
153  $value_str .= $options[$key].'; ';
154  }
155  }
156  if (strlen($value_str) == 0) {
157  // no corresponding values found
158  return $option_str;
159  }
160 
161  // trim the last separator
162  $value_str = substr($value_str, 0, -2);
163  $value_str = Metadata_Field::encodeValueString($value_str, Array());
164 
165  return $value_str;
166 
167  }//end getValueFromKey()
168 
169 
185  public function getContextedSelectOptions()
186  {
187  // Create the cache array if it doesn't exist yet
188  if (array_key_exists('cached_select_options', $this->_tmp) === FALSE) {
189  $this->_tmp['cached_select_options'] = Array();
190  }
191 
192  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
193 
194  if ($contextid === 0) {
195  // Don't really need to cache the select options in the default
196  // context, because it is it's our own law to itself
197  return $this->attr('select_options');
198  } else {
199  // Alternate context
200  if (array_key_exists($contextid, $this->_tmp['cached_select_options']) === FALSE) {
201  // Get the base
202  $select_options = $this->attr('select_options');
203 
204  // Get ourselves in default context form, and get the current
205  // default keys and values
206  $default_options = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('select_options', 'metadata_field_select', Array($this->id), 0);
207  $default_options = unserialize($default_options[$this->id]);
208 
209  // Banish any keys that don't exist in the default version
210  foreach ($select_options as $select_key => $select_value) {
211  if (array_key_exists($select_key, $default_options) === FALSE) {
212  unset($select_options[$select_key]);
213  }
214  }
215 
216  // Add any keys that don't exist in the contexted version,
217  // with the value added from the default context
218  foreach ($default_options as $default_key => &$default_value) {
219  if (array_key_exists($default_key, $select_options) === FALSE) {
220  $select_options[$default_key] = $default_value;
221  }
222  }
223 
224  $this->_tmp['cached_select_options'][$contextid] = $select_options;
225  }
226 
227  return $this->_tmp['cached_select_options'][$contextid];
228  }
229 
230  }//end getContextedSelectOptions()
231 
232 
244  {
245  // Create the cache array if it doesn't exist yet
246  if (array_key_exists('cached_select_options', $this->_tmp) === TRUE) {
247  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
248 
249  if ($contextid === 0) {
250  $this->_tmp['cached_select_options'] = Array();
251  } else {
252  if (array_key_exists($contextid, $this->_tmp['cached_select_options']) === TRUE) {
253  unset($this->_tmp['cached_select_options'][$contextid]);
254  }
255  }
256  }
257 
258  }//end _clearContextedSelectOptionsCache()
259 
260 
270  public function getKeyFromValue($option_str)
271  {
272  $option_keys = explode('\\; ', $option_str);
273  if (empty($option_keys)) return $option_keys;
274 
275  $value_str = '';
276  $options = $this->attr('select_options');
277 
278  foreach ($option_keys as $key) {
279  foreach($options as $index => $element) {
280  if ($index === $key) {
281  $value_str .= $element.'\\; ';
282  }
283  }
284  }
285  if (strlen($value_str) == 0) {
286  // no corresponding values found
287  return $option_str;
288  }
289 
290  // trim the last separator
291  $value_str = substr($value_str, 0, -3);
292 
293  return $value_str;
294 
295  }//end getKeyFromValue()
296 
297 
319  public function getAdditionalKeywordReplacement($host_assetid, $additional_keyword, $contextid=NULL)
320  {
321  $replacement = NULL;
322 
323  if ($contextid === NULL) {
324  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
325  }
326 
327  switch ($additional_keyword) {
328  case 'key':
329  case 'value':
330  // Go directly to the database, and get what it has (which
331  // will be the key)
332  // We need to narrow the host asset ID where it is because
333  // if a default value needs to be used, the custom values table
334  // will be all nulls - so we can't narrow by host asset ID by then
335 
336  // We need to get custom values from the metadata value table,
337  // then default values from the default value table where they
338  // don't exist in the custom value table.
339  $sql = 'SELECT value FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_val ';
340  $where = 'assetid = :host_assetid AND fieldid = :fieldid AND contextid = :contextid';
341  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where);
342  $sql .= $where;
343 
344  // Now build the default values query (and sub-query).
345  $sub_sql = 'SELECT fieldid, contextid FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_val WHERE assetid = :host_assetid_1 ';
346  $sub_where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause('', 'ast_mdata_val');
347 
348  $union_sql = 'SELECT default_val FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_dflt_val';
349  $union_where = '%tuple_keyword%(assetid, contextid) NOT IN ('.$sub_sql.$sub_where.') AND assetid = :fieldid_1 AND contextid = :contextid_1';
350  $union_where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($union_where, 'ast_mdata_dflt_val');
351 
352  // Oracle uses '(field, field)' for tuples, PgSQL uses 'ROW(field, field)'.
353  if (MatrixDAL::getDbType() === 'oci') {
354  $union_where = str_replace('%tuple_keyword%', '', $union_where);
355  } else {
356  $union_where = str_replace('%tuple_keyword%', 'ROW', $union_where);
357  }
358  $union_sql .= $union_where;
359  $sql = '('.$sql.') UNION ALL ('.$union_sql.')';
360 
361  // Go go go!
362  $bind_vars = Array(
363  'host_assetid' => (string)$host_assetid,
364  'fieldid' => (string)$this->id,
365  'contextid' => (int)$contextid,
366  'host_assetid_1' => (string)$host_assetid,
367  'fieldid_1' => (string)$this->id,
368  'contextid_1' => (int)$contextid,
369  );
370 
371  $query = MatrixDAL::preparePdoQuery($sql);
372  foreach ($bind_vars as $bind_var => $bind_value) {
373  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
374  }
375 
376  $key = MatrixDAL::executePdoOne($query);
377 
378  // Does the metadata field value (or a default value) exist?
379  if ($key !== FALSE) {
380  if ($additional_keyword === 'key') {
381  $replacement = $key;
382  } else if ($additional_keyword === 'value') {
383  // Turn it into a value
384  $replacement = $this->getValueFromKey($key);
385  }
386  }
387 
388  break;
389 
390  }//end switch
391 
392  if (is_null($replacement)){
393  return $replacement;
394  } else {
395  $value='';
396  $value_components = $this->attr('value_components');
397  $this->decodeValueString($replacement, $value, $value_components);
398  return $value;
399  }
400 
401  }//end getAdditionalKeywordReplacement()
402 
403 
416  public function importOptionsFromCSV($filename, $header=FALSE)
417  {
418  $csv = new CSV($filename);
419 
420  $start_line = 1;
421  if ($header == TRUE) {
422  $start_line = 2;
423  }
424  $csv->import($start_line);
425 
426  $values = $csv->values;
427  $options = Array();
428  $defaults = Array();
429 
430  if (!empty($values)) {
431  // We will assume that the number of columns in the first row
432  // is representative of the entire list
433  $num_columns = count($values[0]);
434  $index = 0;
435 
436  foreach ($values as $row) {
437  // if the very first bit for us is not populated
439  if (trim($row[0]) === '') continue;
440  if ($num_columns === 1) {
441  // If we haven't provided a first column, then key and value
442  // are the same
443  $value = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
444  $options[$index] = $value;
445  $index++;
446  } else {
447  $key = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
448  $value = htmlentities($row[1], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
449  $options[$key] = $value;
450 
451  if ($num_columns === 3) {
452  if ($row[2] == 1) {
453  $defaults[] = $key;
454  }
455  }
456  }
457  }
458 
459  $this->setAttrValue('select_options', $options);
460  if ($num_columns === 3) {
461  if (!$this->attr('multiple')) {
462  if (count($defaults) > 1) {
463  trigger_localised_error('CORE0309', E_USER_NOTICE);
464  }
465  if (count($defaults) > 0) {
466  $defaults = Array($defaults[0]);
467  } else {
468  $defaults = Array();
469  }
470  }
471 
472  $this->setAttrValue('default', implode('; ', $defaults));
473  } else {
474  // What happens if it gets overwritten?
475  $current_default = $this->attr('default');
476  if (!is_array($current_default)) {
477  $current_default = Array($current_default);
478  }
479  foreach ($current_default as $default_key) {
480  // Remove any default options that are not choices in this
481  // particular situation
482  if (!isset($options[$default_key])) {
483  unset($current_default[$default_key]);
484  }
485  }
486  $this->setAttrValue('default', implode('; ', $current_default));
487  }
488  $this->saveAttributes();
489  }
490 
491  }//end importOptionsFromCSV()
492 
493 
494 }//end class
495 
496 ?>