Fakoli Framework
abstract_data_item.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
34 class DataItemException extends Exception {}
35 class DataNotFoundException extends Exception {}
36 
37 require_once realpath(dirname(__FILE__)."/transaction.inc");
38 require_once realpath(dirname(__FILE__)."/data_type_renderers/abstract_type_renderer.inc");
39 
48 {
49  var $fields = array();
50  var $type = "ExclusionFilter";
51 
52  function ExclusionFilter()
53  {
54  $args = func_get_args();
55  for($i = 0; $i < count($args); ++$i)
56  {
57  $this->fields[$args[$i]] = true;
58  }
59  }
60 
61  function isExcluded($field)
62  {
63  return (array_key_exists($field, $this->fields));
64  }
65 
66  function add()
67  {
68  $args = func_get_args();
69  for($i = 0; $i < count($args); ++$i)
70  {
71  $this->fields[$args[$i]] = true;
72  }
73  }
74 
75  function remove()
76  {
77  $args = func_get_args();
78  for($i = 0; $i < count($args); ++$i)
79  {
80  unset($this->fields[$args[$i]]);
81  }
82  }
83 
84  function includeField($field)
85  {
86  $this->remove($field);
87  }
88 
89  function excludeField($field)
90  {
91  $this->add($field);
92  }
93 }
94 
101 {
102  var $fields = array();
103  var $type = "InclusionFilter";
104 
105  function InclusionFilter()
106  {
107  $args = func_get_args();
108  for($i = 0; $i < count($args); ++$i)
109  {
110  $this->fields[$args[$i]] = true;
111  }
112  }
113 
114  function isExcluded($field)
115  {
116  return !(array_key_exists($field, $this->fields));
117  }
118 
119  function add()
120  {
121  $args = func_get_args();
122  for($i = 0; $i < count($args); ++$i)
123  {
124  $this->fields[$args[$i]] = true;
125  }
126  }
127 
128  function remove()
129  {
130  $args = func_get_args();
131  for($i = 0; $i < count($args); ++$i)
132  {
133  unset($this->fields[$args[$i]]);
134  }
135  }
136 
137  function includeField($field)
138  {
139  $this->add($field);
140  }
141 
142  function excludeField($field)
143  {
144  $this->remove($field);
145  }
146 }
147 
148 define("SerializeRelations", 1);
149 define("SerializeDirectRelations", 2);
150 define("ProtectHTML", 4);
151 
157 abstract class AbstractDataItem
158 {
159  var $filter;
160  var $_options = 0;
161  var $_tx = null;
162 
163  var $_decorations = null;
164 
170  function decorate($name, $value)
171  {
172  if (!$this->_decorations)
173  {
174  $this->_decorations = array();
175  }
176 
177  $this->_decorations[$name] = $value;
178  }
179 
184  function getDecoration($name)
185  {
186  return $this->_decorations[$name];
187  }
188 
197  abstract function cast($class);
198 
203  function setOption($opt)
204  {
205  $this->_options |= $opt;
206  }
207 
212  function clearOptions($opt)
213  {
214  $this->_options |= $opt;
215  }
216 
222  function getConnection()
223  {
224  $tx = $this->getTransaction();
225 
226  if ($tx)
227  {
228  return $tx->getConnection();
229  }
230  else
231  {
233  }
234  }
235 
240  abstract function joinTransaction($tx);
241 
245  abstract function getTransaction();
246 
253  {
254  return "";
255  }
256 
265  abstract function populate($line);
266 
270  abstract function getFields();
271 
276  abstract function getFieldList($alias = "");
277 
282  abstract function getFieldArray();
283 
291  abstract function hasField($field);
292 
299  abstract function hasRelation($relation);
300 
304  abstract function getPrimaryKey();
305 
310  abstract function getPrimaryKeyList();
311 
317  abstract function get($field);
318 
323  {
324  return $this->get($this->getPrimaryKey());
325  }
326 
332  abstract function set($field, $value);
333 
339  abstract function getType($field);
340 
347  abstract function getHiddenFields();
348 
352  abstract function getFilter();
353 
358  abstract function setFilter($filter);
359 
364  abstract function getFieldAliases();
365 
369  abstract function getFieldAnnotations();
370 
376  abstract function load($id);
377 
381  abstract function save();
382 
386  abstract function select();
387 
391  abstract function exists($constraint = "");
392 
396  abstract function update();
397 
401  abstract function delete($constraint = "");
402 
406  abstract function deleteAll();
407 
411  function cacheLookup($id)
412  {
413  return false;
414  }
415 
423  function quoteFieldValue($field, $type)
424  {
425  if (!$type) $type = $this->getType($field);
426 
427  $val = $this->get($field);
428 
429  return $this->quoteValue($val, $type);
430  }
431 
432  function quoteValue($val, $type)
433  {
434  $query = "";
435 
436  $db = $this->getConnection();
437 
438  switch($type)
439  {
440  case Number:
441  case Currency:
442 
443  if ($val == "" || !is_numeric($val))
444  {
445  $query .= "0";
446  }
447  else
448  {
449  $query .= $val;
450  }
451  break;
452 
453  case Date:
454  case DateTime:
455 
456  $query .= ($val) ? $db->quote($this->reformatToSQLDate($val)) : "NULL";
457  break;
458 
459  case Timestamp:
460 
461  $this->__timestamp = date("Y-m-d H:i:s");
462  $query .= $db->quote($this->__timestamp);
463  break;
464 
465  case Boolean:
466 
467  $query .= ($val) ? "1" : "0";
468  break;
469 
470  case String:
471  case Text:
472  case TimeZone:
473 
474  default:
475 
476  $query .= $db->quote($val);
477  break;
478  }
479 
480  return $query;
481  }
482 
483  /*
484  * Call data type renderer to get default format if no
485  * template or template-reqested format.
486  *
487  * JDG 5/28/2011
488  */
489  function formatFieldValue($field, $template = "")
490  {
491  $type = $this->getType($field);
492  $data = $this->get($field);
493 
494  $val = "";
495 
496  $renderer = DataItem::$dataTypeRendererMap[$type];
497  if($renderer)
498  {
499  $formatter = array($renderer, "format");
500  $val = call_user_func($formatter, $data, $template);
501  }
502  else
503  {
504  $val = $data;
505  }
506  return $val;
507  }
508 
512  function formatFieldForXML($field)
513  {
514  $type = $this->getType($field);
515  $data = $this->get($field);
516 
517  $v = "";
518  switch($type)
519  {
520  case HTML:
521 
522  if ($this->_options & ProtectHTML)
523  {
524  $v = str_replace( array("<", ">", "&"), array("[_[OPENTAG]_]", "[_[CLOSETAG]_]", "[_[AMPERSAND]_]"), $data);
525  break;
526  }
527  else
528  {
529  $v = htmlentities($data, ENT_COMPAT | ENT_XML1);
530  break;
531  }
532 
533  case Number:
534  case Date:
535  case Timestamp:
536  case String:
537  case Text:
538  default:
539 
540  $v = htmlentities($data, ENT_COMPAT | ENT_XML1);
541  break;
542  }
543 
544  return $v;
545 
546  }
547 
553  function reformatToSQLDate($date)
554  {
555  trace($date, 3);
556  $fields = array();
557  if (preg_match("|^(\\d+)[\-/](\\d+)[\-/](\\d{4})$|", $date, $fields))
558  {
559  $date = $fields[3]."-".$fields[1]."-".$fields[2];
560  }
561  else if (preg_match("|^(\\d+)/(\\d+)/(\\d{4})\\s+(\\d+:\\d+:\\d+)|", $date, $fields))
562  {
563  $date = $fields[3]."-".$fields[1]."-".$fields[2]." ".$fields[4];
564  }
565 
566  return $date;
567  }
568 
569  function reformatFromSQLDate($date)
570  {
571  $fields = array();
572  if (preg_match("|^(\\d{4})-(\\d\\d)-(\\d\\d)$|", $date, $fields))
573  {
574  $date = $fields[2]."/".$fields[3]."/".$fields[1];
575  }
576  else if (preg_match("|(\\d{4})-(\\d\\d)-(\\d\\d)\\s+(\\d+:\\d+:\\d+)|", $date, $fields))
577  {
578  $date = $fields[2]."/".$fields[3]."/".$fields[1]." ".$fields[4];
579  }
580 
581  if ($date == "00/00/0000") $date = "";
582 
583  return $date;
584  }
585 
589  abstract function insert();
590 
599  abstract function distinctValues($field, $sorted = false, $constraint = "");
600 
604  abstract function fromGET();
605 
612  abstract function fromPOST();
613 
618  abstract function fromREQUEST();
619 
627  abstract function compare($to);
628 
635  abstract function copy($from);
636 
642  abstract function toXML($indent = 0, $path = null);
643 
648  abstract function fromXML($node);
649 
654  function fromJSON($json)
655  {
656  $obj = json_decode($json, true);
657  $fields = $this->getFields();
658  foreach($fields as $field => $type)
659  {
660  $this->set($field, $obj[$field]);
661  }
662  }
663 
664  abstract function format($template = "", $separator = ", ");
665 
666  abstract function prettifyFieldName($field);
667 
668  function prettifyClassName($plural = false)
669  {
670  $c = ($this->pretty_class_name) ? $this->pretty_class_name : get_class($this);
671  $c = preg_replace(array("/([a-z])([A-Z0-9])/",
672  "/_/"),
673  array("$1 $2",
674  " "),
675  $c);
676  $c = ucwords($c);
677 
678  if ($plural)
679  {
680  $c = pluralize($c);
681  }
682 
683  return $c;
684  }
685 
694  abstract function relateTo($target, $field = "");
695 }
696 ?>
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1377
getIdentityConstraint()
Subclasses can override this function to provide additional identity constraints to be used when quer...
getPrimaryKeyValue()
Retrieves the primary key value.
reformatToSQLDate($date)
Reformats the specified date to be in a format used by the database.
formatFieldValue($field, $template="")
Abstract base class for all DataItem implementations.
getDecoration($name)
Retrieve the value of a decoration on the current DataItem.
cacheLookup($id)
Populate the object from the local cache if the object is marked as "cacheLocal". ...
decorate($name, $value)
Adds a decoration to the DataItem.
toXML($tag, $objects, $header="<?xml version=\.0\encoding=\so-8859-1>")
Definition: data_item.inc:1981
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, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
prettifyClassName($plural=false)
getConnection()
Retrieves a connection to the database.
clearOptions($opt)
Clear the specified behavior option flag.
static getConnection()
Retrieves a reference to the global database connection.
static $dataTypeRendererMap
Definition: data_item.inc:66
fromJSON($json)
Populate from the supplied JSON object.
setOption($opt)
Set a behavior option flag.
formatFieldForXML($field)
Format field for XML output.
quoteFieldValue($field, $type)
Returns the properly quoted value of the specified field.
const ProtectHTML