Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
asset_attribute.inc
1 <?php
29 {
30 
35  var $id = 0;
36 
41  var $name = '';
42 
47  var $uniq = FALSE;
48 
53  var $description = '';
54 
59  var $_params = Array();
60 
65  var $_default_value = NULL;
66 
71  var $_edit_params = Array();
72 
77  var $value = NULL;
78 
83  var $processed = FALSE;
84 
89  var $is_admin = FALSE;
90 
91 
100  function Asset_Attribute($attributeid=0, $value=NULL, $default_params=Array())
101  {
102  $this->MySource_Object();
103 
104  if ($attributeid) {
105  $db = DAL::getDb();
106 
107  $sql = 'SELECT attrid, name, type, uniq, description, is_admin, parameters_val, default_val
108  FROM sq_ast_attr
109  WHERE attrid = :attrid';
110 
111  try {
112  $query = MatrixDAL::preparePdoQuery($sql);
113  MatrixDAL::bindValueToPdo($query, 'attrid', $attributeid);
114  $result = DAL::executePdoAll($query);
115  if (empty($result)) {
116  return;
117  } else {
118  $row = $result[0];
119  }
120  unset($result);
121  } catch (Exception $e) {
122  throw new Exception('Unable to get information for attribute ID '.$attributeid.' due to database error: '.$e->getMessage());
123  }
124 
125  $this->id = $row['attrid'];
126  $this->name = $row['name'];
127  $type = $row['type'];
128  $this->uniq = $row['uniq'];
129  $this->description = $row['description'];
130  $this->is_admin = $row['is_admin'];
131  $parameters = $row['parameters_val'];
132  $this->_default_value = $row['default_val'];
133 
134  // make sure the asset we are loading is of the same type as our class
135  if ($type != $this->type()) {
136  throw new Exception('Attribute type mismatch. Attribute ID '.$attributeid.' was created as a "'.$this->type().'" attribute but the database believes it is a "'.$type.'" attribute.');
137  //$this->id = NULL;
138  return;
139  }
140 
141  $this->_params = ($parameters) ? unserialize($parameters) : Array();
142  unset($result);
143 
144  }
145 
146  $this->value = $value;
147 
148  // now just make sure some default values are set
149  foreach ($default_params as $name => $default) {
150  if (!isset($this->_params[$name])) {
151  $this->_params[$name] = $default;
152  }
153  }
154 
155  }//end constructor
156 
157 
169  function &loadAttribute($attributeid=0, $value=NULL, $default_params=Array())
170  {
171  if ($attributeid == 0) {
172  $empty_attr = new Asset_Attribute(0, $value, $default_params);
173  return $empty_attr;
174  }
175 
176  $db = DAL::getDb();
177 
178  $sql = 'SELECT type FROM sq_ast_attr
179  WHERE attrid = :attrid';
180  try {
181  $query = MatrixDAL::preparePdoQuery($sql);
182  MatrixDAL::bindValueToPdo($query, 'attrid', $attributeid);
183  $type = DAL::executePdoOne($query);
184  } catch (Exception $e) {
185  throw new Exception('Unable to load attribute ID #'.$attributeid.' due to database error: '.$e->getMessage());
186  }
187 
188  $class = 'Asset_Attribute_'.$type;
189  require_once SQ_ATTRIBUTES_PATH.'/'.$type.'/'.$type.'.inc';
190  $attr = new $class($attributeid, $value, $default_params);
191 
192  return $attr;
193 
194  }//end loadAttribute()
195 
196 
203  function type()
204  {
205  // strip the 'Asset_Attribute_' off the front
206  // And lower-case it - PHP 5 uses the case the class was declared as
207  return strtolower(substr(get_class($this), 16));
208 
209  }//end type()
210 
211 
221  function paint($prefix, $read_only=FALSE)
222  {
223  echo translate('interface_not_created', ucwords(str_replace('_', ' ', $this->type())));
224 
225  }//end paint()
226 
227 
239  function paintEditParams($prefix, $write_access=FALSE)
240  {
241  return TRUE;
242 
243  }//end paintEditParams()
244 
245 
256  function processEditParams($prefix)
257  {
258  return TRUE;
259 
260  }//end processEditParams()
261 
262 
271  function process($prefix)
272  {
273  $this->processed = FALSE;
274 
275  }//end process()
276 
277 
290  function validateValue(&$value)
291  {
292  return TRUE;
293 
294  }//end validateValue()
295 
296 
305  function setValue(&$value)
306  {
307  if (!$this->validateValue($value)) {
308  trigger_localised_error('SYS0073', E_USER_WARNING, $value, $this->type());
309  return FALSE;
310  }
311  $this->value = $value;
312  return TRUE;
313 
314  }//end setValue()
315 
316 
326  function setEditParams(&$node)
327  {
328 
329  if (strtolower($node->getName()) != strtolower($this->type())) {
330  trigger_localised_error('SYS0300', E_USER_WARNING, $node->getName(), $this->type(), '<pre>'.htmlspecialchars($node->asXml()).'</pre>');
331  return FALSE;
332  }
333  return TRUE;
334 
335  }//end setEditParams()
336 
337 
350  function setEditParam($field, $value)
351  {
352  if (isset($this->_edit_params[$field])) {
353  $this->_edit_params[$field] = $value;
354  return TRUE;
355  } else {
356  trigger_localised_error('SYS0258', E_USER_NOTICE, $field, $this->type());
357  return FALSE;
358  }
359 
360  }//end setEditParam()
361 
362 
375  function setParam($field, $value)
376  {
377  if (isset($this->_params[$field])) {
378  $this->_params[$field] = $value;
379  return TRUE;
380  } else {
381  trigger_localised_error('SYS0258', E_USER_NOTICE, $field, $this->type());
382  return FALSE;
383  }
384 
385  }//end setParam()
386 
387 
394  function getContent()
395  {
396  return $this->value;
397 
398  }//end getContent()
399 
400 
409  function getKeywordValue($format='')
410  {
411  if ($format !== '') return ''; // no formats supported by default
412  return $this->value;
413 
414  }//end getKeywordValue()
415 
416 
423  function exportToXML()
424  {
425  $xml='';
426 
427  $tag_attributes = 'name="'.$this->name.'" ';
428  $tag_attributes .= 'id="'.$this->id.'" ';
429  $tag_attributes .= 'uniq="'.$this->uniq.'" ';
430  $tag_attributes .= 'description="'.$this->description.'" ';
431  $tag_attributes .= 'default_value="'.$this->_default_value.'" ';
432  $tag_attributes .= 'processed="'.$this->processed.'" ';
433  $tag_attributes .= 'is_admin="'.$this->is_admin.'" ';
434 
435  $open_tag = '<'.$this->type().' '.$tag_attributes.'>';
436  $close_tag = '</'.$this->type().'>';
437 
438  $contents = $this->exportContentsToXML();
439 
440  $xml = $open_tag.$contents.$close_tag;
441 
442  return $xml;
443 
444  }//end exportToXML()
445 
446 
456  {
457  $xml = '<value><![CDATA['.$this->getContent().']]></value>';
458  return $xml;
459 
460  }//end exportContentsToXML()
461 
462 
463 }//end class
464 
465 ?>