Framework  3.9
AbstractDataItem Class Reference

Abstract base class for all DataItem implementations. More...

+ Inheritance diagram for AbstractDataItem:

Public Member Functions

 decorate ($name, $value)
 Adds a decoration to the DataItem. More...
 
 getDecoration ($name)
 Retrieve the value of a decoration on the current DataItem. More...
 
 cast ($class)
 Cast this object to another class. More...
 
 setOption ($opt)
 Set a behavior option flag. More...
 
 clearOptions ($opt)
 Clear the specified behavior option flag. More...
 
 getConnection ()
 Retrieves a connection to the database. More...
 
 joinTransaction ($tx)
 Join the DataItem to the specified DataTransaction. More...
 
 getTransaction ()
 Retrieves the current DataTransaction. More...
 
 getIdentityConstraint ()
 Subclasses can override this function to provide additional identity constraints to be used when querying or updating. More...
 
 populate ($line)
 Populates the object using the supplied associative array (field -> value). More...
 
 getFields ()
 Retrieve the field type list for this object. More...
 
 getFieldList ($alias="")
 Returns a comma-separated list of the fields for this object (applying the assigned filter if there is one). More...
 
 getFieldArray ()
 Return an array of field names for this object filtered by any active filter. More...
 
 hasField ($field)
 Returns true if this DataItem contains a field with the specified name and that field is not excluded by a filter. More...
 
 hasRelation ($relation)
 Returns true if this DataItem contains a relation with the specified name. More...
 
 getPrimaryKey ()
 Retrieves the primary key field name. More...
 
 getPrimaryKeyList ()
 Retrieves a list of all the primary keys used for an object as an array. More...
 
 get ($field)
 Retrieve the value for the specified field. More...
 
 getPrimaryKeyValue ()
 Retrieves the primary key value. More...
 
 set ($field, $value)
 Set the value of the specified field. More...
 
 getType ($field)
 Retrieves the data type of the specified field. More...
 
 getHiddenFields ()
 Retrieves the list of hidden fields. More...
 
 getFilter ()
 Returns the filter set on this object. More...
 
 setFilter ($filter)
 Sets the filter on this object. More...
 
 getFieldAliases ()
 Retrieve the list of field aliases. More...
 
 getFieldAnnotations ()
 Retrieve the list of field annotations. More...
 
 load ($id)
 Load the object with the specified primary key. More...
 
 save ()
 Store the object in the database. More...
 
 select ()
 Select the object from the database, based on the value of the primary key field. More...
 
 exists ($constraint="")
 Check whether the object exists in the database. More...
 
 update ()
 Update the row in the database that corresponds to this object. More...
 
 delete ($constraint="")
 Delete the row in the database that corresponds to this object. More...
 
 deleteAll ()
 Delete all the rows in the database that correspond to this class. More...
 
 cacheLookup ($id)
 Populate the object from the local cache if the object is marked as "cacheLocal". More...
 
 quoteFieldValue ($field, $type=null)
 Returns the properly quoted value of the specified field. More...
 
 quoteValue ($val, $type)
 
 formatFieldValue ($field, $template="")
 
 formatFieldForXML ($field)
 Format field for XML output. More...
 
 reformatToSQLDate ($date)
 Reformats the specified date to be in a format used by the database. More...
 
 reformatFromSQLDate ($date)
 
 insert ()
 Insert a new row in the database to store this object. More...
 
 distinctValues ($field, $sorted=false, $constraint="")
 Retrieves the distinct values in the database for the specified field across the specified set of records. More...
 
 fromGET ()
 Automatically populate the object based on parameters in the $_GET collection. More...
 
 fromPOST ()
 Automatically populate the object based on parameters in the $_POST collection. More...
 
 fromREQUEST ()
 Automatically populate the object based on parameters in either the $_GET or $_POST collection, depending on the method used to access the page. More...
 
 compare ($to)
 Compare this object to another object. More...
 
 copy ($from)
 Copies values from another object, field by field. More...
 
 toXML ($indent=0, $path=null)
 Generates an XML representation of the object. More...
 
 fromXML ($node)
 Populates the object from the specified XML node. More...
 
 fromJSON ($json)
 Populate from the supplied JSON object. More...
 
 format ($template="", $separator=", ")
 
 prettifyFieldName ($field)
 
 prettifyClassName ($plural=false)
 
 relateTo ($target, $field="")
 Link this object to the specified target by setting corresponding field to the value of the target's primary key. More...
 

Public Attributes

 $filter
 
 $_options = 0
 
 $_tx = null
 Tracks the current database transaction for each DataItem. More...
 
 $_decorations = null
 

Detailed Description

Abstract base class for all DataItem implementations.

Author
andy

Definition at line 160 of file abstract_data_item.inc.

Member Function Documentation

◆ cacheLookup()

AbstractDataItem::cacheLookup (   $id)

Populate the object from the local cache if the object is marked as "cacheLocal".

Reimplemented in DataItem.

Definition at line 414 of file abstract_data_item.inc.

415  {
416  return false;
417  }

◆ cast()

AbstractDataItem::cast (   $class)
abstract

Cast this object to another class.

This facility is provided so that it is possible to cast from a CompositeDataItem to one of its constituents to provide support for polymorphism in those cases.

Parameters
string$class
Returns
instance of the specified class, if compatible
Exceptions
DataItemException

Reimplemented in DataItem, and CompositeDataItem.

◆ clearOptions()

AbstractDataItem::clearOptions (   $opt)

Clear the specified behavior option flag.

Parameters
integer$optthe option to clear

Definition at line 215 of file abstract_data_item.inc.

216  {
217  $this->_options |= $opt;
218  }

◆ compare()

AbstractDataItem::compare (   $to)
abstract

Compare this object to another object.

If all the fields match, the method returns true, otherwise false. If a filter is in place on the source object, then only the filtered fields are compared.

Parameters
object$tothe object with which to compare this object

Reimplemented in DataItem, and CompositeDataItem.

◆ copy()

AbstractDataItem::copy (   $from)
abstract

Copies values from another object, field by field.

If a filter is in place on the source object, then only the filtered fields are copied.

Parameters
objectfrom the object from which data is to be copied

Reimplemented in DataItem, and CompositeDataItem.

◆ decorate()

AbstractDataItem::decorate (   $name,
  $value 
)

Adds a decoration to the DataItem.

Decorations are not full fields, but can be referred to in format() calls.

Parameters
string$name
string$value

Definition at line 173 of file abstract_data_item.inc.

174  {
175  if (!$this->_decorations)
176  {
177  $this->_decorations = array();
178  }
179 
180  $this->_decorations[$name] = $value;
181  }

◆ delete()

AbstractDataItem::delete (   $constraint = "")
abstract

Delete the row in the database that corresponds to this object.

Reimplemented in DataItem, and CompositeDataItem.

◆ deleteAll()

AbstractDataItem::deleteAll ( )
abstract

Delete all the rows in the database that correspond to this class.

Reimplemented in DataItem, and CompositeDataItem.

◆ distinctValues()

AbstractDataItem::distinctValues (   $field,
  $sorted = false,
  $constraint = "" 
)
abstract

Retrieves the distinct values in the database for the specified field across the specified set of records.

Parameters
string$field
boolean$sorted
string$constraint
Returns
array

Reimplemented in DataItem, and CompositeDataItem.

◆ exists()

AbstractDataItem::exists (   $constraint = "")
abstract

Check whether the object exists in the database.

Reimplemented in DataItem, and CompositeDataItem.

◆ format()

AbstractDataItem::format (   $template = "",
  $separator = ", " 
)
abstract

Reimplemented in CompositeDataItem, and DataItem.

◆ formatFieldForXML()

AbstractDataItem::formatFieldForXML (   $field)

Format field for XML output.

Definition at line 515 of file abstract_data_item.inc.

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  }
getType($field)
Retrieves the data type of the specified field.
const ProtectHTML

◆ formatFieldValue()

AbstractDataItem::formatFieldValue (   $field,
  $template = "" 
)

Definition at line 492 of file abstract_data_item.inc.

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  }
static $dataTypeRendererMap
Definition: data_item.inc:70

◆ fromGET()

AbstractDataItem::fromGET ( )
abstract

Automatically populate the object based on parameters in the $_GET collection.

Reimplemented in DataItem, and CompositeDataItem.

◆ fromJSON()

AbstractDataItem::fromJSON (   $json)

Populate from the supplied JSON object.

Parameters
mixed$jsoneither a decoded JSON object, or the string representation

Definition at line 657 of file abstract_data_item.inc.

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  }
getFields()
Retrieve the field type list for this object.

◆ fromPOST()

AbstractDataItem::fromPOST ( )
abstract

Automatically populate the object based on parameters in the $_POST collection.

If the key does not exist in $_POST and the type is Boolean, that means that the user unchecked a checkbox and we need to assign 0 to the field.

Reimplemented in DataItem, and CompositeDataItem.

◆ fromREQUEST()

AbstractDataItem::fromREQUEST ( )
abstract

Automatically populate the object based on parameters in either the $_GET or $_POST collection, depending on the method used to access the page.

Reimplemented in DataItem, and CompositeDataItem.

◆ fromXML()

AbstractDataItem::fromXML (   $node)
abstract

Populates the object from the specified XML node.

Parameters
DOMNode$node

Reimplemented in DataItem, and CompositeDataItem.

◆ get()

AbstractDataItem::get (   $field)
abstract

Retrieve the value for the specified field.

Parameters
string$fieldthe field to retrieve
Returns
mixed the value of that field

Reimplemented in DataItem, and CompositeDataItem.

◆ getConnection()

AbstractDataItem::getConnection ( )

Retrieves a connection to the database.

Returns
a connection to the database

Definition at line 225 of file abstract_data_item.inc.

226  {
227  $tx = $this->getTransaction();
228 
229  if ($tx)
230  {
231  return $tx->getConnection();
232  }
233  else
234  {
236  }
237  }
getTransaction()
Retrieves the current DataTransaction.
static getConnection()
Retrieves a reference to the global database connection.

◆ getDecoration()

AbstractDataItem::getDecoration (   $name)

Retrieve the value of a decoration on the current DataItem.

Parameters
string$namethe name of the decoration to retrieve

Definition at line 187 of file abstract_data_item.inc.

188  {
189  return $this->_decorations[$name];
190  }

◆ getFieldAliases()

AbstractDataItem::getFieldAliases ( )
abstract

Retrieve the list of field aliases.

Returns
array the alias list for this object

Reimplemented in DataItem, and CompositeDataItem.

◆ getFieldAnnotations()

AbstractDataItem::getFieldAnnotations ( )
abstract

Retrieve the list of field annotations.

Returns
array the annotation list for this object

Reimplemented in DataItem, and CompositeDataItem.

◆ getFieldArray()

AbstractDataItem::getFieldArray ( )
abstract

Return an array of field names for this object filtered by any active filter.

Returns
array

Reimplemented in DataItem, and CompositeDataItem.

◆ getFieldList()

AbstractDataItem::getFieldList (   $alias = "")
abstract

Returns a comma-separated list of the fields for this object (applying the assigned filter if there is one).

Reimplemented in DataItem, and CompositeDataItem.

◆ getFields()

AbstractDataItem::getFields ( )
abstract

Retrieve the field type list for this object.

Reimplemented in DataItem, and CompositeDataItem.

◆ getFilter()

AbstractDataItem::getFilter ( )
abstract

Returns the filter set on this object.

Reimplemented in DataItem, and CompositeDataItem.

◆ getHiddenFields()

AbstractDataItem::getHiddenFields ( )
abstract

Retrieves the list of hidden fields.

These will be automatically hidden in any AutoForm.

Returns
array of names of hidden fields.

Reimplemented in DataItem, and CompositeDataItem.

◆ getIdentityConstraint()

AbstractDataItem::getIdentityConstraint ( )

Subclasses can override this function to provide additional identity constraints to be used when querying or updating.

This allows for easy and safe siloing of data between application users based on session variables.

Definition at line 255 of file abstract_data_item.inc.

256  {
257  return "";
258  }

◆ getPrimaryKey()

AbstractDataItem::getPrimaryKey ( )
abstract

Retrieves the primary key field name.

Reimplemented in DataItem, and CompositeDataItem.

◆ getPrimaryKeyList()

AbstractDataItem::getPrimaryKeyList ( )
abstract

Retrieves a list of all the primary keys used for an object as an array.

Returns
array all the primary keys for this object's constituent parts

Reimplemented in DataItem, and CompositeDataItem.

◆ getPrimaryKeyValue()

AbstractDataItem::getPrimaryKeyValue ( )

Retrieves the primary key value.

Definition at line 325 of file abstract_data_item.inc.

326  {
327  return $this->get($this->getPrimaryKey());
328  }
getPrimaryKey()
Retrieves the primary key field name.

◆ getTransaction()

AbstractDataItem::getTransaction ( )
abstract

Retrieves the current DataTransaction.

Reimplemented in DataItem, and CompositeDataItem.

◆ getType()

AbstractDataItem::getType (   $field)
abstract

Retrieves the data type of the specified field.

Parameters
$fieldthe field in question
Returns
string the data type of the specified field

Reimplemented in DataItem, and CompositeDataItem.

◆ hasField()

AbstractDataItem::hasField (   $field)
abstract

Returns true if this DataItem contains a field with the specified name and that field is not excluded by a filter.

Parameters
string$field
Returns
true if this field is present and not filtered, false otherwise.

Reimplemented in DataItem, and CompositeDataItem.

◆ hasRelation()

AbstractDataItem::hasRelation (   $relation)
abstract

Returns true if this DataItem contains a relation with the specified name.

Parameters
string$relation
Returns
true if this relation is present and not filtered, false otherwise.

Reimplemented in DataItem, and CompositeDataItem.

◆ insert()

AbstractDataItem::insert ( )
abstract

Insert a new row in the database to store this object.

Reimplemented in DataItem, and CompositeDataItem.

◆ joinTransaction()

AbstractDataItem::joinTransaction (   $tx)
abstract

Join the DataItem to the specified DataTransaction.

Parameters
DataTransaction$txthe transaction to join

Reimplemented in DataItem, and CompositeDataItem.

◆ load()

AbstractDataItem::load (   $id)
abstract

Load the object with the specified primary key.

Parameters
intid the primary key value to instantiate from

Reimplemented in DataItem, and CompositeDataItem.

◆ populate()

AbstractDataItem::populate (   $line)
abstract

Populates the object using the supplied associative array (field -> value).

Only values that match the definition of the sub-classed object will be copied. Fields that have been excluded by the use of an InclusionFilter or ExclusionFilter will also not be populated.

Parameters
array$linethe array of values to populate the object with.

Reimplemented in CompositeDataItem.

◆ prettifyClassName()

AbstractDataItem::prettifyClassName (   $plural = false)

Reimplemented in CompositeDataItem.

Definition at line 682 of file abstract_data_item.inc.

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  }
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1428

◆ prettifyFieldName()

AbstractDataItem::prettifyFieldName (   $field)
abstract

Reimplemented in DataItem, and CompositeDataItem.

◆ quoteFieldValue()

AbstractDataItem::quoteFieldValue (   $field,
  $type = null 
)

Returns the properly quoted value of the specified field.

Quoting rules are dependent on the specified type of the field.

Parameters
string$fieldthe field to return
string$typethe type of the field.

Definition at line 426 of file abstract_data_item.inc.

427  {
428  if (!$type) $type = $this->getType($field);
429 
430  $val = $this->get($field);
431 
432  return $this->quoteValue($val, $type);
433  }

◆ quoteValue()

AbstractDataItem::quoteValue (   $val,
  $type 
)

Definition at line 435 of file abstract_data_item.inc.

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  }
getConnection()
Retrieves a connection to the database.

◆ reformatFromSQLDate()

AbstractDataItem::reformatFromSQLDate (   $date)

Definition at line 572 of file abstract_data_item.inc.

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  }

◆ reformatToSQLDate()

AbstractDataItem::reformatToSQLDate (   $date)

Reformats the specified date to be in a format used by the database.

Parameters
string$datethe date to reformat.

Definition at line 556 of file abstract_data_item.inc.

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  }
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ relateTo()

AbstractDataItem::relateTo (   $target,
  $field = "" 
)
abstract

Link this object to the specified target by setting corresponding field to the value of the target's primary key.

Parameters
DataItem$targetthe DataItem to which the relationship will be created
string$fieldthe field to set for the relationship. If not specified, the primary key of the target object is used for the foreign key field name.

Reimplemented in DataItem, and CompositeDataItem.

◆ save()

AbstractDataItem::save ( )
abstract

Store the object in the database.

Reimplemented in DataItem, and CompositeDataItem.

◆ select()

AbstractDataItem::select ( )
abstract

Select the object from the database, based on the value of the primary key field.

Reimplemented in DataItem, and CompositeDataItem.

◆ set()

AbstractDataItem::set (   $field,
  $value 
)
abstract

Set the value of the specified field.

Parameters
$fieldthe field to set
$valuethe value to which the field is to be set

Reimplemented in DataItem, and CompositeDataItem.

◆ setFilter()

AbstractDataItem::setFilter (   $filter)
abstract

Sets the filter on this object.

Parameters
$filterthe filter

Reimplemented in DataItem, and CompositeDataItem.

◆ setOption()

AbstractDataItem::setOption (   $opt)

Set a behavior option flag.

Parameters
integer$optthe option to set

Definition at line 206 of file abstract_data_item.inc.

207  {
208  $this->_options |= $opt;
209  }

◆ toXML()

AbstractDataItem::toXML (   $indent = 0,
  $path = null 
)
abstract

Generates an XML representation of the object.

Filters are honored when determining which fields are included in the XML. Empty fields are not output (note that a string field with value "" is not considered empty).

Reimplemented in DataItem, and CompositeDataItem.

◆ update()

AbstractDataItem::update ( )
abstract

Update the row in the database that corresponds to this object.

Reimplemented in DataItem, and CompositeDataItem.

Member Data Documentation

◆ $_decorations

AbstractDataItem::$_decorations = null

Definition at line 166 of file abstract_data_item.inc.

◆ $_options

AbstractDataItem::$_options = 0

Definition at line 163 of file abstract_data_item.inc.

◆ $_tx

AbstractDataItem::$_tx = null

Tracks the current database transaction for each DataItem.

Definition at line 164 of file abstract_data_item.inc.

◆ $filter

AbstractDataItem::$filter

Definition at line 162 of file abstract_data_item.inc.


The documentation for this class was generated from the following file: