2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
use SilverStripe\Model\FieldType\DBVarchar;
|
|
|
|
use SilverStripe\Model\FieldType\DBField;
|
|
|
|
use SilverStripe\Model\FieldType\DBHTMLText;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* A ViewableData object is any object that can be rendered into a template/view.
|
|
|
|
*
|
|
|
|
* A view interrogates the object being currently rendered in order to get data to render into the template. This data
|
|
|
|
* is provided and automatically escaped by ViewableData. Any class that needs to be available to a view (controllers,
|
|
|
|
* {@link DataObject}s, page controls) should inherit from this class.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage view
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-11 02:31:43 +01:00
|
|
|
class ViewableData extends Object implements IteratorAggregate {
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* An array of objects to cast certain fields to. This is set up as an array in the format:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* public static $casting = array (
|
|
|
|
* 'FieldName' => 'ClassToCastTo(Arguments)'
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2009-10-11 02:06:58 +02:00
|
|
|
'CSSClasses' => 'Varchar'
|
|
|
|
);
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* The default object to cast scalar fields to if casting information is not specified, and casting to an object
|
|
|
|
* is required.
|
|
|
|
*
|
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_cast = 'Text';
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
private static $casting_cache = array();
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
2012-02-11 03:26:26 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
|
|
|
* A failover object to attempt to get data from if it is not present on this object.
|
|
|
|
*
|
|
|
|
* @var ViewableData
|
|
|
|
*/
|
|
|
|
protected $failover;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
|
|
|
* @var ViewableData
|
|
|
|
*/
|
|
|
|
protected $customisedObject;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
private $objCache = array();
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// FIELD GETTERS & SETTERS -----------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2008-03-11 02:31:43 +01:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Check if a field exists on this object or its failover.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset($property) {
|
|
|
|
return $this->hasField($property) || ($this->failover && $this->failover->hasField($property));
|
2008-03-11 02:31:43 +01:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Get the value of a property/field on this object. This will check if a method called get{$property} exists, then
|
|
|
|
* check if a field is available using {@link ViewableData::getField()}, then fall back on a failover object.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($property) {
|
|
|
|
if($this->hasMethod($method = "get$property")) {
|
|
|
|
return $this->$method();
|
|
|
|
} elseif($this->hasField($property)) {
|
|
|
|
return $this->getField($property);
|
|
|
|
} elseif($this->failover) {
|
|
|
|
return $this->failover->$property;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Set a property/field on this object. This will check for the existence of a method called set{$property}, then
|
|
|
|
* use the {@link ViewableData::setField()} method.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($property, $value) {
|
2016-06-03 10:51:02 +02:00
|
|
|
$this->objCacheClear();
|
2009-10-11 02:06:58 +02:00
|
|
|
if($this->hasMethod($method = "set$property")) {
|
|
|
|
$this->$method($value);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->setField($property, $value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2015-12-22 17:19:13 +01:00
|
|
|
/**
|
|
|
|
* Set a failover object to attempt to get data from if it is not present on this object.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-12-22 17:19:13 +01:00
|
|
|
* @param ViewableData $failover
|
|
|
|
*/
|
|
|
|
public function setFailover(ViewableData $failover) {
|
2016-06-03 18:04:22 +02:00
|
|
|
// Ensure cached methods from previous failover are removed
|
|
|
|
if ($this->failover) {
|
|
|
|
$this->removeMethodsFrom('failover');
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:13 +01:00
|
|
|
$this->failover = $failover;
|
|
|
|
$this->defineMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current failover object if set
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-12-22 17:19:13 +01:00
|
|
|
* @return ViewableData|null
|
|
|
|
*/
|
|
|
|
public function getFailover() {
|
|
|
|
return $this->failover;
|
|
|
|
}
|
2015-07-28 18:57:56 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Check if a field exists on this object. This should be overloaded in child classes.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function hasField($field) {
|
|
|
|
return property_exists($this, $field);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Get the value of a field on this object. This should be overloaded in child classes.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return mixed
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function getField($field) {
|
|
|
|
return $this->$field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Set a field on this object. This should be overloaded in child classes.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param mixed $value
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function setField($field, $value) {
|
2016-06-03 10:51:02 +02:00
|
|
|
$this->objCacheClear();
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->$field = $value;
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an
|
|
|
|
* underscore into a {@link ViewableData::cachedCall()}.
|
2016-06-03 10:51:02 +02:00
|
|
|
*
|
|
|
|
* @throws LogicException
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function defineMethods() {
|
2016-06-03 10:51:02 +02:00
|
|
|
if($this->failover && !is_object($this->failover)) {
|
|
|
|
throw new LogicException("ViewableData::\$failover set to a non-object");
|
|
|
|
}
|
2009-10-11 02:06:58 +02:00
|
|
|
if($this->failover) {
|
2016-06-03 10:51:02 +02:00
|
|
|
$this->addMethodsFrom('failover');
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
if(isset($_REQUEST['debugfailover'])) {
|
|
|
|
Debug::message("$this->class created with a failover class of {$this->failover->class}");
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
parent::defineMethods();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-07-28 18:57:56 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance
|
|
|
|
* with references to both this and the new custom data.
|
|
|
|
*
|
|
|
|
* Note that any fields you specify will take precedence over the fields on this object.
|
|
|
|
*
|
|
|
|
* @param array|ViewableData $data
|
|
|
|
* @return ViewableData_Customised
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function customise($data) {
|
|
|
|
if(is_array($data) && (empty($data) || ArrayLib::is_associative($data))) {
|
|
|
|
$data = new ArrayData($data);
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
if($data instanceof ViewableData) {
|
|
|
|
return new ViewableData_Customised($this, $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
throw new InvalidArgumentException (
|
|
|
|
'ViewableData->customise(): $data must be an associative array or a ViewableData instance'
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2014-08-14 00:28:00 +02:00
|
|
|
/**
|
|
|
|
* @return ViewableData
|
|
|
|
*/
|
|
|
|
public function getCustomisedObj() {
|
|
|
|
return $this->customisedObject;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* @param ViewableData $object
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function setCustomisedObj(ViewableData $object) {
|
|
|
|
$this->customisedObject = $object;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// CASTING ---------------------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object)
|
|
|
|
* for a field on this object. This helper will be a subclass of DBField.
|
2009-10-11 02:06:58 +02:00
|
|
|
*
|
|
|
|
* @param string $field
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string Casting helper As a constructor pattern, and may include arguments.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function castingHelper($field) {
|
2015-09-04 05:49:22 +02:00
|
|
|
$specs = $this->config()->casting;
|
|
|
|
if(isset($specs[$field])) {
|
|
|
|
return $specs[$field];
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
|
|
|
|
// If no specific cast is declared, fall back to failover.
|
|
|
|
// Note that if there is a failover, the default_cast will always
|
|
|
|
// be drawn from this object instead of the top level object.
|
|
|
|
$failover = $this->getFailover();
|
|
|
|
if($failover) {
|
|
|
|
$cast = $failover->castingHelper($field);
|
|
|
|
if($cast) {
|
|
|
|
return $cast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to default_cast
|
|
|
|
return $this->config()->get('default_cast');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Get the class name a field on this object will be casted to.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2009-10-11 02:06:58 +02:00
|
|
|
* @param string $field
|
|
|
|
* @return string
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function castingClass($field) {
|
2016-06-03 10:51:02 +02:00
|
|
|
// Strip arguments
|
2010-10-04 06:46:41 +02:00
|
|
|
$spec = $this->castingHelper($field);
|
2016-06-03 10:51:02 +02:00
|
|
|
return trim(strtok($spec, '('));
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Return the string-format type for the given field.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return string 'xml'|'raw'
|
|
|
|
*/
|
|
|
|
public function escapeTypeForField($field) {
|
2015-02-10 06:01:19 +01:00
|
|
|
$class = $this->castingClass($field) ?: $this->config()->default_cast;
|
2015-02-08 00:00:36 +01:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
// TODO: It would be quicker not to instantiate the object, but to merely
|
|
|
|
// get its class from the Injector
|
|
|
|
return Injector::inst()->get($class, true)->config()->escape_type;
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2015-02-08 00:00:36 +01:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// TEMPLATE ACCESS LAYER -------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
|
|
|
* Render this object into the template, and get the result as a string. You can pass one of the following as the
|
|
|
|
* $template parameter:
|
|
|
|
* - a template name (e.g. Page)
|
|
|
|
* - an array of possible template names - the first valid one will be used
|
|
|
|
* - an SSViewer instance
|
|
|
|
*
|
|
|
|
* @param string|array|SSViewer $template the template to render into
|
|
|
|
* @param array $customFields fields to customise() the object with before rendering
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return DBHTMLText
|
2009-10-11 02:06:58 +02:00
|
|
|
*/
|
|
|
|
public function renderWith($template, $customFields = null) {
|
|
|
|
if(!is_object($template)) {
|
|
|
|
$template = new SSViewer($template);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$data = ($this->customisedObject) ? $this->customisedObject : $this;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2012-04-11 11:34:27 +02:00
|
|
|
if($customFields instanceof ViewableData) {
|
2009-10-11 02:06:58 +02:00
|
|
|
$data = $data->customise($customFields);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-11 02:06:58 +02:00
|
|
|
if($template instanceof SSViewer) {
|
2012-04-11 11:34:27 +02:00
|
|
|
return $template->process($data, is_array($customFields) ? $customFields : null);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
throw new UnexpectedValueException (
|
|
|
|
"ViewableData::renderWith(): unexpected $template->class object, expected an SSViewer instance"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-03-01 22:07:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the cache name for a field
|
|
|
|
*
|
|
|
|
* @param string $fieldName Name of field
|
|
|
|
* @param array $arguments List of optional arguments given
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2015-03-01 22:07:58 +01:00
|
|
|
*/
|
|
|
|
protected function objCacheName($fieldName, $arguments) {
|
|
|
|
return $arguments
|
|
|
|
? $fieldName . ":" . implode(',', $arguments)
|
|
|
|
: $fieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a cached value from the field cache
|
|
|
|
*
|
|
|
|
* @param string $key Cache key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function objCacheGet($key) {
|
2016-06-03 10:51:02 +02:00
|
|
|
if(isset($this->objCache[$key])) {
|
|
|
|
return $this->objCache[$key];
|
|
|
|
}
|
|
|
|
return null;
|
2015-03-01 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a value in the field cache
|
|
|
|
*
|
|
|
|
* @param string $key Cache key
|
|
|
|
* @param mixed $value
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return $this
|
2015-03-01 22:07:58 +01:00
|
|
|
*/
|
|
|
|
protected function objCacheSet($key, $value) {
|
|
|
|
$this->objCache[$key] = $value;
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear object cache
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function objCacheClear() {
|
|
|
|
$this->objCache = [];
|
|
|
|
return $this;
|
2015-03-01 22:07:58 +01:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Get the value of a field on this object, automatically inserting the value into any available casting objects
|
|
|
|
* that have been specified.
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param array $arguments
|
2015-03-01 22:07:58 +01:00
|
|
|
* @param bool $cache Cache this object
|
2009-10-11 02:06:58 +02:00
|
|
|
* @param string $cacheName a custom cache name
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return Object|DBField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function obj($fieldName, $arguments = [], $cache = false, $cacheName = null) {
|
|
|
|
if(!$cacheName && $cache) {
|
|
|
|
$cacheName = $this->objCacheName($fieldName, $arguments);
|
|
|
|
}
|
2015-03-01 22:07:58 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
// Check pre-cached value
|
2015-03-01 22:07:58 +01:00
|
|
|
$value = $cache ? $this->objCacheGet($cacheName) : null;
|
2016-06-03 10:51:02 +02:00
|
|
|
if($value !== null) {
|
|
|
|
return $value;
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
// Load value from record
|
|
|
|
if($this->hasMethod($fieldName)) {
|
|
|
|
$value = call_user_func_array(array($this, $fieldName), $arguments ?: []);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-10-11 02:06:58 +02:00
|
|
|
$value = $this->$fieldName;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
// Cast object
|
|
|
|
if(!is_object($value)) {
|
|
|
|
// Force cast
|
|
|
|
$castingHelper = $this->castingHelper($fieldName);
|
|
|
|
$valueObject = Object::create_from_string($castingHelper, $fieldName);
|
2013-04-21 03:18:18 +02:00
|
|
|
$valueObject->setValue($value, $this);
|
2009-10-11 02:06:58 +02:00
|
|
|
$value = $valueObject;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
// Record in cache
|
|
|
|
if($cache) {
|
|
|
|
$this->objCacheSet($cacheName, $value);
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return $value;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* A simple wrapper around {@link ViewableData::obj()} that automatically caches the result so it can be used again
|
|
|
|
* without re-running the method.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $identifier an optional custom cache identifier
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return Object|DBField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function cachedCall($field, $arguments = [], $identifier = null) {
|
|
|
|
return $this->obj($field, $arguments, true, $identifier);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Checks if a given method/field has a valid value. If the result is an object, this will return the result of the
|
|
|
|
* exists method, otherwise will check if the result is not just an empty paragraph tag.
|
|
|
|
*
|
|
|
|
* @param string $field
|
2010-10-04 06:21:32 +02:00
|
|
|
* @param array $arguments
|
2009-10-11 02:06:58 +02:00
|
|
|
* @param bool $cache
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function hasValue($field, $arguments = [], $cache = true) {
|
|
|
|
$result = $this->obj($field, $arguments, $cache);
|
2009-10-11 02:06:58 +02:00
|
|
|
return $result->exists();
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Get the string value of a field on this object that has been suitable escaped to be inserted directly into a
|
|
|
|
* template.
|
|
|
|
*
|
2009-10-11 02:06:58 +02:00
|
|
|
* @param string $field
|
|
|
|
* @param array $arguments
|
|
|
|
* @param bool $cache
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function XML_val($field, $arguments = [], $cache = false) {
|
|
|
|
$result = $this->obj($field, $arguments, $cache);
|
|
|
|
// Might contain additional formatting over ->XML(). E.g. parse shortcodes, nl2br()
|
|
|
|
return $result->forTemplate();
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Get an array of XML-escaped values by field name
|
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param array $fields an array of field names
|
2009-10-11 02:06:58 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getXMLValues($fields) {
|
|
|
|
$result = array();
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
$result[$field] = $this->XML_val($field);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// ITERATOR SUPPORT ------------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Return a single-item iterator so you can iterate over the fields of a single record.
|
|
|
|
*
|
|
|
|
* This is useful so you can use a single record inside a <% control %> block in a template - and then use
|
|
|
|
* to access individual fields on this object.
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function getIterator() {
|
|
|
|
return new ArrayIterator(array($this));
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// UTILITY METHODS -------------------------------------------------------------------------------------------------
|
2016-04-29 07:54:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* When rendering some objects it is necessary to iterate over the object being rendered, to do this, you need
|
|
|
|
* access to itself.
|
|
|
|
*
|
|
|
|
* @return ViewableData
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function Me() {
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Return the directory if the current active theme (relative to the site root).
|
|
|
|
*
|
|
|
|
* This method is useful for things such as accessing theme images from your template without hardcoding the theme
|
|
|
|
* page - e.g. <img src="$ThemeDir/images/something.gif">.
|
|
|
|
*
|
|
|
|
* This method should only be used when a theme is currently active. However, it will fall over to the current
|
|
|
|
* project directory.
|
|
|
|
*
|
|
|
|
* @param string $subtheme the subtheme path to get
|
|
|
|
* @return string
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function ThemeDir($subtheme = null) {
|
2013-03-27 10:13:24 +01:00
|
|
|
if(
|
2016-04-29 07:54:52 +02:00
|
|
|
Config::inst()->get('SSViewer', 'theme_enabled')
|
2013-03-27 10:13:24 +01:00
|
|
|
&& $theme = Config::inst()->get('SSViewer', 'theme')
|
|
|
|
) {
|
2009-10-11 02:06:58 +02:00
|
|
|
return THEMES_DIR . "/$theme" . ($subtheme ? "_$subtheme" : null);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return project();
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Get part of the current classes ancestry to be used as a CSS class.
|
|
|
|
*
|
|
|
|
* This method returns an escaped string of CSS classes representing the current classes ancestry until it hits a
|
|
|
|
* stop point - e.g. "Page DataObject ViewableData".
|
|
|
|
*
|
|
|
|
* @param string $stopAtClass the class to stop at (default: ViewableData)
|
|
|
|
* @return string
|
|
|
|
* @uses ClassInfo
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function CSSClasses($stopAtClass = 'ViewableData') {
|
|
|
|
$classes = array();
|
|
|
|
$classAncestry = array_reverse(ClassInfo::ancestry($this->class));
|
|
|
|
$stopClasses = ClassInfo::ancestry($stopAtClass);
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
foreach($classAncestry as $class) {
|
|
|
|
if(in_array($class, $stopClasses)) break;
|
|
|
|
$classes[] = $class;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// optionally add template identifier
|
|
|
|
if(isset($this->template) && !in_array($this->template, $classes)) {
|
|
|
|
$classes[] = $this->template;
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return Convert::raw2att(implode(' ', $classes));
|
|
|
|
}
|
2012-02-11 03:08:39 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Return debug information about this object that can be rendered into a template
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return ViewableData_Debugger
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function Debug() {
|
|
|
|
return new ViewableData_Debugger($this);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage view
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ViewableData_Customised extends ViewableData {
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* @var ViewableData
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
protected $original, $customised;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Instantiate a new customised ViewableData object
|
|
|
|
*
|
|
|
|
* @param ViewableData $originalObject
|
|
|
|
* @param ViewableData $customisedObject
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function __construct(ViewableData $originalObject, ViewableData $customisedObject) {
|
|
|
|
$this->original = $originalObject;
|
|
|
|
$this->customised = $customisedObject;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$this->original->setCustomisedObj($this);
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function __call($method, $arguments) {
|
|
|
|
if($this->customised->hasMethod($method)) {
|
|
|
|
return call_user_func_array(array($this->customised, $method), $arguments);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return call_user_func_array(array($this->original, $method), $arguments);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function __get($property) {
|
|
|
|
if(isset($this->customised->$property)) {
|
|
|
|
return $this->customised->$property;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return $this->original->$property;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function __set($property, $value) {
|
|
|
|
$this->customised->$property = $this->original->$property = $value;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function hasMethod($method) {
|
|
|
|
return $this->customised->hasMethod($method) || $this->original->hasMethod($method);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function cachedCall($field, $arguments = null, $identifier = null) {
|
2009-11-21 02:41:06 +01:00
|
|
|
if($this->customised->hasMethod($field) || $this->customised->hasField($field)) {
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->customised->cachedCall($field, $arguments, $identifier);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->original->cachedCall($field, $arguments, $identifier);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
public function obj($fieldName, $arguments = null, $cache = false, $cacheName = null) {
|
2009-10-11 02:06:58 +02:00
|
|
|
if($this->customised->hasField($fieldName) || $this->customised->hasMethod($fieldName)) {
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->customised->obj($fieldName, $arguments, $cache, $cacheName);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->original->obj($fieldName, $arguments, $cache, $cacheName);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Allows you to render debug information about a {@link ViewableData} object into a template.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage view
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ViewableData_Debugger extends ViewableData {
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var ViewableData
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
protected $object;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
|
|
|
* @param ViewableData $object
|
|
|
|
*/
|
|
|
|
public function __construct(ViewableData $object) {
|
|
|
|
$this->object = $object;
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2015-03-24 11:40:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string The rendered debugger
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
2016-04-27 04:26:39 +02:00
|
|
|
return (string)$this->forTemplate();
|
2015-03-24 11:40:25 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:06:58 +02:00
|
|
|
* Return debugging information, as XHTML. If a field name is passed, it will show debugging information on that
|
|
|
|
* field, otherwise it will show information on all methods and fields.
|
|
|
|
*
|
|
|
|
* @param string $field the field name
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function forTemplate($field = null) {
|
|
|
|
// debugging info for a specific field
|
|
|
|
if($field) return "<b>Debugging Information for {$this->class}->{$field}</b><br/>" .
|
|
|
|
($this->object->hasMethod($field)? "Has method '$field'<br/>" : null) .
|
|
|
|
($this->object->hasField($field) ? "Has field '$field'<br/>" : null) ;
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// debugging information for the entire class
|
|
|
|
$reflector = new ReflectionObject($this->object);
|
|
|
|
$debug = "<b>Debugging Information: all methods available in '{$this->object->class}'</b><br/><ul>";
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
foreach($this->object->allMethodNames() as $method) {
|
|
|
|
// check that the method is public
|
|
|
|
if($method[0] === strtoupper($method[0]) && $method[0] != '_') {
|
|
|
|
if($reflector->hasMethod($method) && $method = $reflector->getMethod($method)) {
|
|
|
|
if($method->isPublic()) {
|
|
|
|
$debug .= "<li>\${$method->getName()}";
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
if(count($method->getParameters())) {
|
|
|
|
$debug .= ' <small>(' . implode(', ', $method->getParameters()) . ')</small>';
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$debug .= '</li>';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$debug .= "<li>\$$method</li>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$debug .= '</ul>';
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2012-05-11 01:23:24 +02:00
|
|
|
if($this->object->hasMethod('toMap')) {
|
2009-10-11 02:06:58 +02:00
|
|
|
$debug .= "<b>Debugging Information: all fields available in '{$this->object->class}'</b><br/><ul>";
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2012-05-11 01:23:24 +02:00
|
|
|
foreach($this->object->toMap() as $field => $value) {
|
2009-10-11 02:06:58 +02:00
|
|
|
$debug .= "<li>\$$field</li>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$debug .= "</ul>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
// check for an extra attached data
|
|
|
|
if($this->object->hasMethod('data') && $this->object->data() != $this->object) {
|
2012-04-04 16:59:30 +02:00
|
|
|
$debug .= ViewableData_Debugger::create($this->object->data())->forTemplate();
|
2008-08-08 06:58:31 +02:00
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
return $debug;
|
2008-03-11 02:31:43 +01:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|