Fakoli Framework
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)
 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 157 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".

Definition at line 411 of file abstract_data_item.inc.

412  {
413  return false;
414  }

◆ 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

◆ clearOptions()

AbstractDataItem::clearOptions (   $opt)

Clear the specified behavior option flag.

Parameters
integer$optthe option to clear

Definition at line 212 of file abstract_data_item.inc.

213  {
214  $this->_options |= $opt;
215  }

◆ 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

◆ 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

◆ 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 170 of file abstract_data_item.inc.

171  {
172  if (!$this->_decorations)
173  {
174  $this->_decorations = array();
175  }
176 
177  $this->_decorations[$name] = $value;
178  }

◆ delete()

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

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

◆ deleteAll()

AbstractDataItem::deleteAll ( )
abstract

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

◆ 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

◆ exists()

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

Check whether the object exists in the database.

◆ format()

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

◆ formatFieldForXML()

AbstractDataItem::formatFieldForXML (   $field)

Format field for XML output.

Definition at line 512 of file abstract_data_item.inc.

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

◆ formatFieldValue()

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

Definition at line 489 of file abstract_data_item.inc.

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  }
getType($field)
Retrieves the data type of the specified field.
static $dataTypeRendererMap
Definition: data_item.inc:66

◆ fromGET()

AbstractDataItem::fromGET ( )
abstract

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

◆ fromJSON()

AbstractDataItem::fromJSON (   $json)

Populate from the supplied JSON object.

Parameters
unknown$json

Definition at line 654 of file abstract_data_item.inc.

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  }
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.

◆ 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.

◆ fromXML()

AbstractDataItem::fromXML (   $node)
abstract

Populates the object from the specified XML node.

Parameters
DOMNode$node

◆ 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

◆ getConnection()

AbstractDataItem::getConnection ( )

Retrieves a connection to the database.

Returns
a connection to the database

Definition at line 222 of file abstract_data_item.inc.

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

◆ 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 184 of file abstract_data_item.inc.

185  {
186  return $this->_decorations[$name];
187  }

◆ getFieldAliases()

AbstractDataItem::getFieldAliases ( )
abstract

Retrieve the list of field aliases.

Returns
array the alias list for this object

◆ getFieldAnnotations()

AbstractDataItem::getFieldAnnotations ( )
abstract

Retrieve the list of field annotations.

Returns
array the annotation list for this object

◆ getFieldArray()

AbstractDataItem::getFieldArray ( )
abstract

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

Returns
array

◆ getFieldList()

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

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

◆ getFields()

AbstractDataItem::getFields ( )
abstract

Retrieve the field type list for this object.

◆ getFilter()

AbstractDataItem::getFilter ( )
abstract

Returns the filter set on this object.

◆ 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.

◆ 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 252 of file abstract_data_item.inc.

253  {
254  return "";
255  }

◆ getPrimaryKey()

AbstractDataItem::getPrimaryKey ( )
abstract

Retrieves the primary key field name.

◆ 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

◆ getPrimaryKeyValue()

AbstractDataItem::getPrimaryKeyValue ( )

Retrieves the primary key value.

Definition at line 322 of file abstract_data_item.inc.

323  {
324  return $this->get($this->getPrimaryKey());
325  }
getPrimaryKey()
Retrieves the primary key field name.

◆ getTransaction()

AbstractDataItem::getTransaction ( )
abstract

Retrieves the current DataTransaction.

◆ 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

◆ 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.

◆ 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.

◆ insert()

AbstractDataItem::insert ( )
abstract

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

◆ joinTransaction()

AbstractDataItem::joinTransaction (   $tx)
abstract

Join the DataItem to the specified DataTransaction.

Parameters
DataTransaction$txthe transaction to join

◆ load()

AbstractDataItem::load (   $id)
abstract

Load the object with the specified primary key.

Parameters
intid the primary key value to instantiate from

◆ 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.

◆ prettifyClassName()

AbstractDataItem::prettifyClassName (   $plural = false)

Definition at line 668 of file abstract_data_item.inc.

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

◆ prettifyFieldName()

AbstractDataItem::prettifyFieldName (   $field)
abstract

◆ quoteFieldValue()

AbstractDataItem::quoteFieldValue (   $field,
  $type 
)

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 423 of file abstract_data_item.inc.

424  {
425  if (!$type) $type = $this->getType($field);
426 
427  $val = $this->get($field);
428 
429  return $this->quoteValue($val, $type);
430  }
getType($field)
Retrieves the data type of the specified field.

◆ quoteValue()

AbstractDataItem::quoteValue (   $val,
  $type 
)

Definition at line 432 of file abstract_data_item.inc.

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

◆ reformatFromSQLDate()

AbstractDataItem::reformatFromSQLDate (   $date)

Definition at line 569 of file abstract_data_item.inc.

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  }

◆ 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 553 of file abstract_data_item.inc.

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

◆ 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.

◆ save()

AbstractDataItem::save ( )
abstract

Store the object in the database.

◆ select()

AbstractDataItem::select ( )
abstract

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

◆ 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

◆ setFilter()

AbstractDataItem::setFilter (   $filter)
abstract

Sets the filter on this object.

Parameters
$filterthe filter

◆ setOption()

AbstractDataItem::setOption (   $opt)

Set a behavior option flag.

Parameters
integer$optthe option to set

Definition at line 203 of file abstract_data_item.inc.

204  {
205  $this->_options |= $opt;
206  }

◆ 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).

◆ update()

AbstractDataItem::update ( )
abstract

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

Member Data Documentation

◆ $_decorations

AbstractDataItem::$_decorations = null

Definition at line 163 of file abstract_data_item.inc.

◆ $_options

AbstractDataItem::$_options = 0

Definition at line 160 of file abstract_data_item.inc.

◆ $_tx

AbstractDataItem::$_tx = null

Tracks the current database transaction for each DataItem.

Definition at line 161 of file abstract_data_item.inc.

◆ $filter

AbstractDataItem::$filter

Definition at line 159 of file abstract_data_item.inc.


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