Framework  3.9
abstract_data_item.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
37 class DataItemException extends Exception {}
38 class DataNotFoundException extends Exception {}
39 
40 require_once realpath(dirname(__FILE__)."/transaction.inc");
41 require_once realpath(dirname(__FILE__)."/data_type_renderers/abstract_type_renderer.inc");
42 
51 {
52  var $fields = array();
53  var $type = "ExclusionFilter";
54 
55  function ExclusionFilter()
56  {
57  $args = func_get_args();
58  for($i = 0; $i < count($args); ++$i)
59  {
60  $this->fields[$args[$i]] = true;
61  }
62  }
63 
64  function isExcluded($field)
65  {
66  return (array_key_exists($field, $this->fields));
67  }
68 
69  function add()
70  {
71  $args = func_get_args();
72  for($i = 0; $i < count($args); ++$i)
73  {
74  $this->fields[$args[$i]] = true;
75  }
76  }
77 
78  function remove()
79  {
80  $args = func_get_args();
81  for($i = 0; $i < count($args); ++$i)
82  {
83  unset($this->fields[$args[$i]]);
84  }
85  }
86 
87  function includeField($field)
88  {
89  $this->remove($field);
90  }
91 
92  function excludeField($field)
93  {
94  $this->add($field);
95  }
96 }
97 
104 {
105  var $fields = array();
106  var $type = "InclusionFilter";
107 
108  function InclusionFilter()
109  {
110  $args = func_get_args();
111  for($i = 0; $i < count($args); ++$i)
112  {
113  $this->fields[$args[$i]] = true;
114  }
115  }
116 
117  function isExcluded($field)
118  {
119  return !(array_key_exists($field, $this->fields));
120  }
121 
122  function add()
123  {
124  $args = func_get_args();
125  for($i = 0; $i < count($args); ++$i)
126  {
127  $this->fields[$args[$i]] = true;
128  }
129  }
130 
131  function remove()
132  {
133  $args = func_get_args();
134  for($i = 0; $i < count($args); ++$i)
135  {
136  unset($this->fields[$args[$i]]);
137  }
138  }
139 
140  function includeField($field)
141  {
142  $this->add($field);
143  }
144 
145  function excludeField($field)
146  {
147  $this->remove($field);
148  }
149 }
150 
151 define("SerializeRelations", 1);
152 define("SerializeDirectRelations", 2);
153 define("ProtectHTML", 4);
154 
160 abstract class AbstractDataItem
161 {
162  var $filter;
163  var $_options = 0;
164  var $_tx = null;
165 
166  var $_decorations = null;
167 
173  function decorate($name, $value)
174  {
175  if (!$this->_decorations)
176  {
177  $this->_decorations = array();
178  }
179 
180  $this->_decorations[$name] = $value;
181  }
182 
187  function getDecoration($name)
188  {
189  return $this->_decorations[$name];
190  }
191 
200  abstract function cast($class);
201 
206  function setOption($opt)
207  {
208  $this->_options |= $opt;
209  }
210 
215  function clearOptions($opt)
216  {
217  $this->_options |= $opt;
218  }
219 
225  function getConnection()
226  {
227  $tx = $this->getTransaction();
228 
229  if ($tx)
230  {
231  return $tx->getConnection();
232  }
233  else
234  {
236  }
237  }
238 
243  abstract function joinTransaction($tx);
244 
248  abstract function getTransaction();
249 
256  {
257  return "";
258  }
259 
268  abstract function populate($line);
269 
273  abstract function getFields();
274 
279  abstract function getFieldList($alias = "");
280 
285  abstract function getFieldArray();
286 
294  abstract function hasField($field);
295 
302  abstract function hasRelation($relation);
303 
307  abstract function getPrimaryKey();
308 
313  abstract function getPrimaryKeyList();
314 
320  abstract function get($field);
321 
326  {
327  return $this->get($this->getPrimaryKey());
328  }
329 
335  abstract function set($field, $value);
336 
342  abstract function getType($field);
343 
350  abstract function getHiddenFields();
351 
355  abstract function getFilter();
356 
361  abstract function setFilter($filter);
362 
367  abstract function getFieldAliases();
368 
372  abstract function getFieldAnnotations();
373 
379  abstract function load($id);
380 
384  abstract function save();
385 
389  abstract function select();
390 
394  abstract function exists($constraint = "");
395 
399  abstract function update();
400 
404  abstract function delete($constraint = "");
405 
409  abstract function deleteAll();
410 
414  function cacheLookup($id)
415  {
416  return false;
417  }
418 
426  function quoteFieldValue($field, $type = null)
427  {
428  if (!$type) $type = $this->getType($field);
429 
430  $val = $this->get($field);
431 
432  return $this->quoteValue($val, $type);
433  }
434 
435  function quoteValue($val, $type)
436  {
437  $query = "";
438 
439  $db = $this->getConnection();
440 
441  switch($type)
442  {
443  case Number:
444  case Currency:
445 
446  if ($val == "" || !is_numeric($val))
447  {
448  $query .= "0";
449  }
450  else
451  {
452  $query .= $val;
453  }
454  break;
455 
456  case Date:
457  case DateTime:
458 
459  $query .= ($val) ? $db->quote($this->reformatToSQLDate($val)) : "NULL";
460  break;
461 
462  case Timestamp:
463 
464  $this->__timestamp = date("Y-m-d H:i:s");
465  $query .= $db->quote($this->__timestamp);
466  break;
467 
468  case Boolean:
469 
470  $query .= ($val) ? "1" : "0";
471  break;
472 
473  case String:
474  case Text:
475  case TimeZone:
476 
477  default:
478 
479  $query .= $db->quote($val);
480  break;
481  }
482 
483  return $query;
484  }
485 
486  /*
487  * Call data type renderer to get default format if no
488  * template or template-reqested format.
489  *
490  * JDG 5/28/2011
491  */
492  function formatFieldValue($field, $template = "")
493  {
494  $type = $this->getType($field);
495  $data = $this->get($field);
496 
497  $val = "";
498 
499  $renderer = DataItem::$dataTypeRendererMap[$type];
500  if($renderer)
501  {
502  $formatter = array($renderer, "format");
503  $val = call_user_func($formatter, $data, $template);
504  }
505  else
506  {
507  $val = $data;
508  }
509  return $val;
510  }
511 
515  function formatFieldForXML($field)
516  {
517  $type = $this->getType($field);
518  $data = $this->get($field);
519 
520  $v = "";
521  switch($type)
522  {
523  case HTML:
524 
525  if ($this->_options & ProtectHTML)
526  {
527  $v = str_replace( array("<", ">", "&"), array("[_[OPENTAG]_]", "[_[CLOSETAG]_]", "[_[AMPERSAND]_]"), $data);
528  break;
529  }
530  else
531  {
532  $v = htmlentities($data, ENT_COMPAT | ENT_XML1);
533  break;
534  }
535 
536  case Number:
537  case Date:
538  case Timestamp:
539  case String:
540  case Text:
541  default:
542 
543  $v = htmlentities($data, ENT_COMPAT | ENT_XML1);
544  break;
545  }
546 
547  return $v;
548 
549  }
550 
556  function reformatToSQLDate($date)
557  {
558  trace($date, 3);
559  $fields = array();
560  if (preg_match("|^(\\d+)[\-/](\\d+)[\-/](\\d{4})$|", $date, $fields))
561  {
562  $date = $fields[3]."-".$fields[1]."-".$fields[2];
563  }
564  else if (preg_match("|^(\\d+)/(\\d+)/(\\d{4})\\s+(\\d+:\\d+:\\d+)|", $date, $fields))
565  {
566  $date = $fields[3]."-".$fields[1]."-".$fields[2]." ".$fields[4];
567  }
568 
569  return $date;
570  }
571 
572  function reformatFromSQLDate($date)
573  {
574  $fields = array();
575  if (preg_match("|^(\\d{4})-(\\d\\d)-(\\d\\d)$|", $date, $fields))
576  {
577  $date = $fields[2]."/".$fields[3]."/".$fields[1];
578  }
579  else if (preg_match("|(\\d{4})-(\\d\\d)-(\\d\\d)\\s+(\\d+:\\d+:\\d+)|", $date, $fields))
580  {
581  $date = $fields[2]."/".$fields[3]."/".$fields[1]." ".$fields[4];
582  }
583 
584  if ($date == "00/00/0000") $date = "";
585 
586  return $date;
587  }
588 
592  abstract function insert();
593 
602  abstract function distinctValues($field, $sorted = false, $constraint = "");
603 
607  abstract function fromGET();
608 
615  abstract function fromPOST();
616 
621  abstract function fromREQUEST();
622 
630  abstract function compare($to);
631 
638  abstract function copy($from);
639 
645  abstract function toXML($indent = 0, $path = null);
646 
651  abstract function fromXML($node);
652 
657  function fromJSON($json)
658  {
659  if (is_object($json))
660  {
661  $fields = $this->getFields();
662  foreach($fields as $field => $type)
663  {
664  $this->set($field, $json->$field);
665  }
666  }
667  else
668  {
669  $obj = json_decode($json, true);
670  $fields = $this->getFields();
671  foreach($fields as $field => $type)
672  {
673  $this->set($field, $obj[$field]);
674  }
675  }
676  }
677 
678  abstract function format($template = "", $separator = ", ");
679 
680  abstract function prettifyFieldName($field);
681 
682  function prettifyClassName($plural = false)
683  {
684  $c = ($this->pretty_class_name) ? $this->pretty_class_name : get_class($this);
685  $c = preg_replace(array("/([a-z])([A-Z0-9])/",
686  "/_/"),
687  array("$1 $2",
688  " "),
689  $c);
690  $c = ucwords($c);
691 
692  if ($plural)
693  {
694  $c = pluralize($c);
695  }
696 
697  return $c;
698  }
699 
708  abstract function relateTo($target, $field = "");
709 }?>
Abstract base class for all DataItem implementations.
cacheLookup($id)
Populate the object from the local cache if the object is marked as "cacheLocal".
fromREQUEST()
Automatically populate the object based on parameters in either the $_GET or $_POST collection,...
fromGET()
Automatically populate the object based on parameters in the $_GET collection.
getFields()
Retrieve the field type list for this object.
getFieldList($alias="")
Returns a comma-separated list of the fields for this object (applying the assigned filter if there i...
relateTo($target, $field="")
Link this object to the specified target by setting corresponding field to the value of the target's ...
getConnection()
Retrieves a connection to the database.
formatFieldValue($field, $template="")
getPrimaryKeyList()
Retrieves a list of all the primary keys used for an object as an array.
fromPOST()
Automatically populate the object based on parameters in the $_POST collection.
format($template="", $separator=", ")
joinTransaction($tx)
Join the DataItem to the specified DataTransaction.
hasRelation($relation)
Returns true if this DataItem contains a relation with the specified name.
prettifyFieldName($field)
decorate($name, $value)
Adds a decoration to the DataItem.
hasField($field)
Returns true if this DataItem contains a field with the specified name and that field is not excluded...
formatFieldForXML($field)
Format field for XML output.
setFilter($filter)
Sets the filter on this object.
insert()
Insert a new row in the database to store this object.
populate($line)
Populates the object using the supplied associative array (field -> value).
getTransaction()
Retrieves the current DataTransaction.
getType($field)
Retrieves the data type of the specified field.
update()
Update the row in the database that corresponds to this object.
getPrimaryKey()
Retrieves the primary key field name.
prettifyClassName($plural=false)
save()
Store the object in the database.
getFieldArray()
Return an array of field names for this object filtered by any active filter.
fromXML($node)
Populates the object from the specified XML node.
deleteAll()
Delete all the rows in the database that correspond to this class.
getFieldAliases()
Retrieve the list of field aliases.
getIdentityConstraint()
Subclasses can override this function to provide additional identity constraints to be used when quer...
$_tx
Tracks the current database transaction for each DataItem.
compare($to)
Compare this object to another object.
fromJSON($json)
Populate from the supplied JSON object.
toXML($indent=0, $path=null)
Generates an XML representation of the object.
reformatToSQLDate($date)
Reformats the specified date to be in a format used by the database.
cast($class)
Cast this object to another class.
getFieldAnnotations()
Retrieve the list of field annotations.
exists($constraint="")
Check whether the object exists in the database.
distinctValues($field, $sorted=false, $constraint="")
Retrieves the distinct values in the database for the specified field across the specified set of rec...
getFilter()
Returns the filter set on this object.
getDecoration($name)
Retrieve the value of a decoration on the current DataItem.
clearOptions($opt)
Clear the specified behavior option flag.
copy($from)
Copies values from another object, field by field.
getHiddenFields()
Retrieves the list of hidden fields.
select()
Select the object from the database, based on the value of the primary key field.
setOption($opt)
Set a behavior option flag.
quoteFieldValue($field, $type=null)
Returns the properly quoted value of the specified field.
load($id)
Load the object with the specified primary key.
getPrimaryKeyValue()
Retrieves the primary key value.
static getConnection()
Retrieves a reference to the global database connection.
static $dataTypeRendererMap
Definition: data_item.inc:70
Used to place a filter on the contents of a DataItem-derived object.
Used to place a filter on the contents of a DataItem-derived object.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1428
const ProtectHTML