Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_field.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Metadata_Field extends Asset
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _preCreateCheck(Array &$link)
60  {
61  if (!parent::_preCreateCheck($link)) return FALSE;
62 
63  $name = trim($this->attr('name'));
64  if ($name == '') {
65  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
66  return FALSE;
67  }
68 
69  return TRUE;
70 
71  }//end _preCreateCheck()
72 
73 
83  protected function _getName($short_name=FALSE)
84  {
85  return $this->attr('name');
86 
87  }//end _getName()
88 
89 
97  public function _getAllowedLinks()
98  {
99  return Array(
100  SQ_LINK_TYPE_1 => Array(),
101  SQ_LINK_TYPE_2 => Array(),
102  SQ_LINK_TYPE_3 => Array(),
103  SQ_LINK_NOTICE => Array(),
104  );
105 
106  }//end _getAllowedLinks()
107 
108 
115  public function getDefaultValue()
116  {
117  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
118  return $mm->getMetadataFieldDefaultValue($this->id);
119 
120  }//end getDefaultValue()
121 
122 
133  public function getMetadataValue(Asset $asset, $value_str)
134  {
135  require_once SQ_FUDGE_PATH.'/general/text.inc';
136  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
137 
138  // get the default value
139  if (is_null($value_str)) {
140  $value_str = $this->getDefaultValue();
141  }
142 
143  // if there are no components that we know about and we are not being displayed on the frontend,
144  // then just unescape the value string and return that
145  if (!$this->attr('value_components') && $this->attr('frontend') == 'hide') {
146  Metadata_Field::decodeValueString($value_str, $value='', $components=Array());
147  return $value;
148  }
149 
150  $used_keywords = retrieve_keywords_replacements($value_str, '.');
151 
152  $used_keywords = $mm->generateKeywordReplacements($asset, $used_keywords, FALSE);
153 
154  return replace_keywords($value_str, $used_keywords);
155 
156  }//end getMetadataValue()
157 
158 
184  public static function encodeValueString($value, $components)
185  {
186  // NOTE: need to have the 4 slashes == 2 slashes in regex == 1 slash literal being matched/printed
187  $regex = '/([;=\\\\])/';
188  $replace = '\\\\$1';
189 
190  $str = preg_replace($regex, $replace, $value);
191  foreach ($components as $k => $v) {
192  $str .= '; '.preg_replace($regex, $replace, $k).'='.preg_replace($regex, $replace, $v);
193  }// end foreach
194 
195  if (!empty($components)) $str .= ';';
196  return $str;
197 
198  }//end encodeValueString()
199 
200 
231  public static function decodeValueString($str, &$value, &$components)
232  {
233 
234  // First escape % characters
235  $str = str_replace('%', '%'.ord('%').'%', $str);
236  // Next change \ escaped characters to ^d^ where d is the character's ascii code
237  $str = preg_replace('/\\\\([;=\\\\])/e', '"%".ord("\\1")."%"', $str);
238 
239  $value = '';
240  $check_component = !empty($components);
241  $parts = explode(';', $str);
242  foreach ($parts as $part) {
243  if ($part == '') continue;
244  $pair = explode('=', $part, 2);
245 
246  // strip whitespace from name string
247  $pair[0] = preg_replace('/^\s*(\S+)\s*$/', '$1', $pair[0]);
248 
249  // trim it
250  $pair[0] = trim($pair[0]);
251 
252  // convert % escaped characters back
253  $pair[0] = preg_replace('/%(\d+)%/e', 'chr($1)', $pair[0]);
254 
255  if (isset($pair[1])) {
256  $pair[1] = preg_replace('/%(\d+)%/e', 'chr($1)', $pair[1]);
257  }
258 
259  if (!isset($pair[1])) {
260  if ($value != '') $value .= '; ';
261  $value .= $pair[0];
262  // if the array wasn't empty, then we need to make sure that the component is already in the array
263  } else if (!$check_component || isset($components[$pair[0]])) {
264  $components[$pair[0]] = $pair[1];
265  }
266 
267  }// end foreach
268 
269  }//end decodeValueString()
270 
271 
284  public function saveAttributes($dont_run_updated=FALSE)
285  {
286  // We basically dont allow single or double quotation mark in the field name.
287  $field_name_original = $this->attr('name');
288  $field_name = str_replace('\'', '', $field_name_original);
289  $field_name = str_replace('\"', '', $field_name);
290  if ($field_name != $field_name_original) {
291  $this->setAttrValue('name', $field_name);
292  }//end if
293 
294  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
295  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
296 
297  $default_val = self::encodeValueString($this->attr('default'), $this->attr('value_components'));
298 
299  // Only re-save the default value in the default metadata table if
300  // the default value has actually changed
301  if ((array_key_exists('vars_set', $this->_tmp) === TRUE) && (array_key_exists('default', $this->_tmp['vars_set']) === TRUE)) {
302  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
303 
304  if (!$mm->setMetadataFieldDefaultValue($this->id, $default_val)) {
305  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
306  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
307  return FALSE;
308  }
309  }
310 
311  if (!parent::saveAttributes($dont_run_updated)) {
312  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
313  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
314  return FALSE;
315  }
316 
317  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
318  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
319 
320  return TRUE;
321 
322  }//end saveAttributes()
323 
324 
336  public function delete($release_lock=TRUE)
337  {
338  if (parent::delete($release_lock)) {
339  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
340  $db = MatrixDAL::getDb();
341 
342  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
343 
344  // First remove any values assets have for this field
345  try {
346  $sql = 'DELETE FROM sq_ast_mdata_val WHERE ';
347  $sql .= 'fieldid = :fieldid';
348 
349  $query = MatrixDAL::preparePdoQuery($sql);
350  MatrixDAL::bindValueToPdo($query, 'fieldid', $this->id);
351  MatrixDAL::executePdoOne($query);
352  } catch (Exception $e) {
353  throw new Exception('Unable to delete metadata value for fieldid #'.$this->id.' due to the following database error:'.$e->getMessage());
354  }//end try catch
355 
356 
357  // Now we have to delete the default value information
358  try {
359  $sql = 'DELETE FROM sq_ast_mdata_dflt_val WHERE ';
360  $sql .= 'assetid = :assetid';
361 
362  $query = MatrixDAL::preparePdoQuery($sql);
363  MatrixDAL::bindValueToPdo($query, 'assetid', $this->id);
364  MatrixDAL::executePdoOne($query);
365  } catch (Exception $e) {
366  throw new Exception('Unable to delete metadata default value for assetid #'.$this->id.' due to the following database error:'.$e->getMessage());
367  }//end try catch
368 
369  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
370  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
371 
372  return TRUE;
373  }
374  return FALSE;
375 
376  }//end delete()
377 
378 
395  public function getAdditionalKeywordReplacement($additional_keyword)
396  {
397  return NULL;
398 
399  }//end getAdditionalKeywordReplacement()
400 
401 
402 }//end class
403 
404 ?>