Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field_hierarchy.inc
1 <?php
18 require_once dirname(__FILE__).'/../../metadata_field/metadata_field.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
54  public function getHierarchyAttribute()
55  {
56  require_once SQ_ATTRIBUTES_PATH.'/selection/selection.inc';
57 
58  $selection = new Asset_Attribute_Selection();
59  $edit_params = $this->attr('edit_params');
60  if (!empty($edit_params)) {
61  $selection->_edit_params = array_merge($selection->_edit_params, $edit_params);
62  }
63 
64  $selection->_params['options'] = $this->getContextedHierarchyOptions();
65  $selection->_params['multiple'] = $this->attr('multiple');
66  $selection->_params['allow_empty'] = $this->attr('allow_empty');
67  $selection->_params['sort'] = $this->attr('hier_sort');
68  $selection->_params['structure'] = $this->attr('hier_structure');
69 
70  $default = explode('; ', $this->attr('default'));
71  if ($selection->validateValue($default)) {
72  $selection->setValue($default);
73  $selection->_default_value = $default;
74  }
75 
76  return $selection;
77 
78  }//end getHierarchyAttribute()
79 
80 
93  public function getMetadataValue(Asset $asset, $value_str)
94  {
95 
96  // If we are showing the value, then pass it up
97  if ($this->attr('visible_part') === 'value') {
98  $value_str = $this->getValueFromKey($value_str);
99  }
100  return str_replace('\\;', ';', parent::getMetadataValue($asset, $value_str));
101 
102  }//end getMetadataValue()
103 
104 
114  public function getValueFromKey($option_str)
115  {
116  $option_keys = explode('; ', $option_str);
117  if (empty($option_keys)) return $option_str;
118 
119  $value_str = '';
120  $options = $this->getContextedHierarchyOptions();
121 
122  foreach ($option_keys as $key) {
123  $key = str_replace('\\', '', $key);
124  if (isset($options[$key])) {
125  $value_str .= $options[$key].'; ';
126  }
127  }
128  if (strlen($value_str) == 0) {
129  // no corresponding values found
130  return $option_str;
131  }
132 
133  // trim the last separator
134  $value_str = substr($value_str, 0, -2);
135  $value_str = Metadata_Field::encodeValueString($value_str, Array());
136 
137  return $value_str;
138 
139  }//end getValueFromKey()
140 
141 
158  {
159  // Create the cache array if it doesn't exist yet
160  if (array_key_exists('cached_hierarchy_options', $this->_tmp) === FALSE) {
161  $this->_tmp['cached_hierarchy_options'] = Array();
162  }
163 
164  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
165 
166  if ($contextid === 0) {
167  // Don't really need to cache the hierarchy options in the default
168  // context, because it is it's our own law to itself
169  return $this->attr('hierarchy_options');
170  } else {
171  // Alternate context
172  if (array_key_exists($contextid, $this->_tmp['cached_hierarchy_options']) === FALSE) {
173  // Get the base
174  $hierarchy_options = $this->attr('hierarchy_options');
175 
176  // Get ourselves in default context form, and get the current
177  // default keys and values
178  $default_options = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('hierarchy_options', 'metadata_field_hierarchy', Array($this->id), 0);
179  $default_options = unserialize($default_options[$this->id]);
180 
181  // Banish any keys that don't exist in the default version
182  foreach ($hierarchy_options as $hierarchy_key => $hierarchy_value) {
183  if (array_key_exists($hierarchy_key, $default_options) === FALSE) {
184  unset($hierarchy_options[$hierarchy_key]);
185  }
186  }
187 
188  // Add any keys that don't exist in the contexted version,
189  // with the value added from the default context
190  foreach ($default_options as $default_key => &$default_value) {
191  if (array_key_exists($default_key, $hierarchy_options) === FALSE) {
192  $hierarchy_options[$default_key] = $default_value;
193  }
194  }
195 
196  $this->_tmp['cached_hierarchy_options'][$contextid] = $hierarchy_options;
197  }
198 
199  return $this->_tmp['cached_hierarchy_options'][$contextid];
200  }
201 
202  }//end getContextedHierarchyOptions()
203 
204 
216  {
217  // Create the cache array if it doesn't exist yet
218  if (array_key_exists('cached_hierarchy_options', $this->_tmp) === TRUE) {
219  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
220 
221  if ($contextid === 0) {
222  $this->_tmp['cached_hierarchy_options'] = Array();
223  } else {
224  if (array_key_exists($contextid, $this->_tmp['cached_hierarchy_options']) === TRUE) {
225  unset($this->_tmp['cached_hierarchy_options'][$contextid]);
226  }
227  }
228  }
229 
230  }//end _clearContextedHierarchyOptionsCache()
231 
232 
242  public function getKeyFromValue($option_str)
243  {
244  $option_keys = explode('; ', $option_str);
245  if (empty($option_keys)) return $option_keys;
246 
247  $value_str = '';
248  $options = $this->attr('hierarchy_options');
249 
250  foreach ($option_keys as $key) {
251  $key = str_replace('\\', '', $key);
252  foreach($options as $index => $element) {
253  if ($index === $key) {
254  $value_str .= $element.'\\; ';
255  }
256  }
257  }
258  if (strlen($value_str) == 0) {
259  // no corresponding values found
260  return $option_str;
261  }
262 
263  // trim the last separator
264  $value_str = substr($value_str, 0, -3);
265 
266  return $value_str;
267 
268  }//end getKeyFromValue()
269 
270 
292  public function getAdditionalKeywordReplacement($host_assetid, $additional_keyword, $contextid=NULL)
293  {
294  $replacement = NULL;
295 
296  if ($contextid === NULL) {
297  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
298  }
299 
300  switch ($additional_keyword) {
301  case 'key':
302  case 'value':
303  // Go directly to the database, and get what it has (which
304  // will be the key)
305  // We need to narrow the host asset ID where it is because
306  // if a default value needs to be used, the custom values table
307  // will be all nulls - so we can't narrow by host asset ID by then
308 
309  // We need to get custom values from the metadata value table,
310  // then default values from the default value table where they
311  // don't exist in the custom value table.
312  $sql = 'SELECT value FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_val ';
313  $where = 'assetid = :host_assetid AND fieldid = :fieldid AND contextid = :contextid';
314  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where);
315  $sql .= $where;
316 
317  // Now build the default values query (and sub-query).
318  $sub_sql = 'SELECT fieldid, contextid FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_val ';
319  $sub_where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause('', 'ast_mdata_val');
320 
321  $union_sql = 'SELECT default_val FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_mdata_dflt_val';
322  $union_where = '%tuple_keyword%(assetid, contextid) NOT IN ('.$sub_sql.$sub_where.') AND assetid = :fieldid_1 AND contextid = :contextid_1';
323  $union_where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($union_where, 'ast_mdata_dflt_val');
324 
325  // Oracle uses '(field, field)' for tuples, PgSQL uses 'ROW(field, field)'.
326  if (MatrixDAL::getDbType() === 'oci') {
327  $union_where = str_replace('%tuple_keyword%', '', $union_where);
328  } else {
329  $union_where = str_replace('%tuple_keyword%', 'ROW', $union_where);
330  }
331  $union_sql .= $union_where;
332  $sql = '('.$sql.') UNION ALL ('.$union_sql.')';
333 
334  // Go go go!
335  $bind_vars = Array(
336  'host_assetid' => (string)$host_assetid,
337  'fieldid' => (string)$this->id,
338  'contextid' => (int)$contextid,
339  'fieldid_1' => (string)$this->id,
340  'contextid_1' => (int)$contextid,
341  );
342 
343  $query = MatrixDAL::preparePdoQuery($sql);
344  foreach ($bind_vars as $bind_var => $bind_value) {
345  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
346  }
347 
348  $key = MatrixDAL::executePdoOne($query);
349 
350  // Does the metadata field value (or a default value) exist?
351  if ($key !== FALSE) {
352  if ($additional_keyword === 'key') {
353  $replacement = $key;
354  } else if ($additional_keyword === 'value') {
355  // Turn it into a value
356  $replacement = $this->getValueFromKey($key);
357  }
358  }
359 
360  break;
361 
362  }//end switch
363 
364  if (is_null($replacement)){
365  return $replacement;
366  } else {
367  $value='';
368  $value_components = $this->attr('value_components');
369  $this->decodeValueString($replacement, $value, $value_components);
370  return $value;
371  }
372 
373  }//end getAdditionalKeywordReplacement()
374 
375 
376 }//end class
377 
378 ?>