Example #1
0
 /**
  * 
  * Object constructor
  * @param t41\ObjectModel\DataObject|t41\ObjectModel\ObjectUri|string $val
  * @param array $params
  */
 public function __construct($val = null, array $params = null)
 {
     $this->_setParameterObjects();
     if (is_array($params)) {
         $this->_setParameters($params);
     }
     /* build data object and populate it if possible */
     if ($val instanceof DataObject) {
         if ($val->getClass() != get_class($this)) {
             throw new Exception("Provided Data Object is not build on class definition");
         }
         $this->_dataObject = $val;
     } else {
         if (!is_null($val)) {
             if (!$val instanceof ObjectUri) {
                 // provide backend if uri is partial
                 $backendUri = null;
                 //substr($val,0,1) != Backend::PREFIX ? ObjectModel::getObjectBackend(get_class($this)) : null;
                 $val = new ObjectUri($val, $backendUri);
                 $val->setClass(get_class($this));
             }
             $this->_dataObject = Core::_($val, get_class($this), ObjectModel::DATA);
         } else {
             $this->_dataObject = DataObject::factory(get_class($this));
         }
     }
     /* get object rules from config */
     $this->setRules();
 }
Example #2
0
 /**
  * 
  * Object constructor
  * @param t41_Data_Object|t41_Object_Uri|string $val
  * @param array $params
  */
 public function __construct($val = null, array $params = null)
 {
     $this->_setParameterObjects();
     if (is_array($params)) {
         $this->_setParameters($params);
     }
     /* build data object and populate it if possible */
     if ($val instanceof DataObject) {
         if ($val->getClass() != get_class($this)) {
             throw new Exception("Provided Data Object is not build on class definition");
         }
         $this->_dataObject = $val;
         /* get object rules from config */
         $this->setRules();
     } else {
         $this->_dataObject = DataObject::factory(get_class($this));
         /* get object rules from config */
         $this->setRules();
         /* get object rules from config */
         //$this->_rules = t41_Object::getRules(get_class($this), $this->_dataObject);
         if (!is_null($val)) {
             if (!$val instanceof ObjectUri) {
                 $val = new ObjectUri($val);
                 $val->setClass(get_class($this));
             }
             $this->_dataObject->setUri($val);
             $this->read();
         }
     }
 }
Example #3
0
 /**
  * Define an array of printable columns based on list or setted parameter
  * @return array
  */
 public function getColumns()
 {
     if (!is_array($this->_columns)) {
         $alt = $this->getParameter('altlabels');
         $do = $this->_collection->getDataObject();
         $columns = $this->getParameter('columns') ? $this->getParameter('columns') : array_keys($do->getProperties());
         $this->_columns = array();
         foreach ($columns as $column) {
             // meta columns are useful to display calculated (not stored) values
             if (substr($column, 0, 1) == self::METACOL) {
                 $parts = explode(':', substr($column, 1));
                 $obj = new Element\MetaElement($parts[0]);
                 $obj->setParameter('property', $parts[0]);
                 if (isset($parts[1])) {
                     $obj->setParameter('action', $parts[1]);
                 }
                 $this->_columns[] = $obj;
                 continue;
             } else {
                 if ($column instanceof MetaElement) {
                     $this->_columns[] = $column;
                     continue;
                 }
             }
             if ($column == ObjectUri::IDENTIFIER) {
                 $obj = new Element\IdentifierElement();
                 $obj->setTitle(isset($alt[$column]) ? $alt[$column] : 'ID');
                 $this->_columns[] = $obj;
                 continue;
             }
             // $column may contain recursive property reference
             $parts = explode('.', $column);
             // find matching property
             $property = $do->getRecursiveProperty($column);
             if (!$property instanceof Property\AbstractProperty) {
                 continue;
             }
             $obj = new Element\ColumnElement($column);
             if (count($parts) > 1) {
                 $obj->setParameter('recursion', array_slice($parts, 1));
                 foreach (array_slice($parts, 1) as $recursion) {
                     // recursion to find the related property
                     if ($property instanceof Property\ObjectProperty) {
                         $do2 = ObjectModel\DataObject::factory($property->getParameter('instanceof'));
                         if (($nproperty = $do2->getProperty($recursion)) !== false) {
                             $property = $nproperty;
                         }
                     }
                 }
             }
             if (!is_object($property)) {
                 $alt[$column] = 'Erreur';
             }
             $obj->setParameter('property', $parts[0]);
             $obj->setTitle(isset($alt[$column]) ? $alt[$column] : $property->getLabel());
             $obj->setParameter('align', $property instanceof CurrencyProperty ? 'R' : 'L');
             $this->_columns[] = $obj;
         }
     }
     return $this->_columns;
 }
Example #4
0
 /**
  * Universal factory for DataObject() and BaseObject() instances with caching capabilities
  * @param ObjectUri $uri
  * @param string $class
  * @param string $type
  * @throws Exception
  * @return \t41\ObjectModel\DataObject|t41\ObjectModel\BaseObject
  */
 public static function _($uri, $class = null, $type = ObjectModel::MODEL)
 {
     if (!$uri instanceof ObjectUri) {
         if (is_null($class)) {
             throw new Exception("Give ObjectUri() instance or specify object class as second argument");
         }
         $uri = new ObjectUri($uri);
         $uri->setClass($class);
     } else {
         $class = $uri->getClass();
     }
     if (self::getEnvData('cache_objects') !== true) {
         $obj = DataObject::factory($class);
         $obj->setUri($uri);
         Backend::read($obj);
         return $type == ObjectModel::MODEL ? new $class($obj) : $obj;
     }
     $def = ObjectModel::getObjectDna($class);
     if ($def && isset($def['unchanging'])) {
         // get cache version
         if (($obj = self::cacheGet($uri->getPermanentUUID())) !== false) {
             self::log(sprintf('[Persistence] Loaded %s object (%s) from cache', $class, $uri));
         } else {
             // done this away to avoid infinite recursion in BaseObject::__construct()
             $obj = DataObject::factory($class);
             $obj->setUri($uri);
             Backend::read($obj);
             $obj = new $class($obj);
             self::cacheSet($obj, $uri->getPermanentUUID(), true, array('tags' => array('permanent')));
             self::log(sprintf('[Persistence] Saved %s object (%s) in cache', $class, $uri));
         }
         return $type == ObjectModel::MODEL ? $obj : $obj->getDataObject();
     } else {
         $obj = DataObject::factory($class);
         $obj->setUri($uri);
         Backend::read($obj);
         return $type == ObjectModel::MODEL ? new $class($obj) : $obj;
     }
 }
Example #5
0
 /**
  * Set a new condition on property given id or throws an exception if property doesn't exist
  *  
  * @param string $name
  * @param return \t41\Backend\Condition
  * @throws t41_Exception
  */
 public function having($name)
 {
     // condition on the identifier part of the uri
     if ($name == ObjectUri::IDENTIFIER) {
         $this->_condition = new self(new Property\IdentifierProperty(ObjectUri::IDENTIFIER));
         return $this->_condition;
     } else {
         if (!$this->_property instanceof Property\ObjectProperty && !$this->_property instanceof Property\CollectionProperty) {
             throw new Exception("CONDITION_INCORRECT_PROPERTY");
         }
     }
     $do = ObjectModel\DataObject::factory($this->_property->getParameter('instanceof'));
     if (($property = $do->getProperty($name)) !== false) {
         $this->_condition = new self($property);
         return $this->_condition;
     }
     throw new Exception(array("CONDITION_UNKNOWN_PROPERTY", $name));
 }
Example #6
0
 /**
  * This class is used for manipulation of collection
  * 
  * Possible parameters are:
  * - memberType: [uri|data|model] 
  *   Defines which type of members we expect to get returned from backend.
  *   uri:   returns t41_Object_Uri references
  *   data:  returns populated t41_Data_Object instances
  *   model: returns populated t41_Object_Model-based instances
  *   default value is data
  * 
  * @param \t41\ObjectModel\DataObject|string $do
  * @param array $params
  */
 public function __construct($do, array $params = null)
 {
     if ($do instanceof ObjectModel\DataObject) {
         $this->_do = $do;
     } else {
         if (is_string($do)) {
             $this->_do = ObjectModel\DataObject::factory($do);
         } else {
             throw new Exception("Collection must be instanced from data object or class name");
         }
     }
     /* deal with class parameters first */
     $this->_setParameterObjects();
     if (is_array($params)) {
         $this->_setParameters($params);
     }
 }
Example #7
0
 /**
  * Returns the property matching the pattern in $name, recursively if needed
  * @param string $name
  * @return t41\ObjectModel\Property\AbstractProperty
  */
 public function getRecursiveProperty($name)
 {
     if ($name == ObjectUri::IDENTIFIER) {
         return new IdentifierProperty('id');
     }
     if (strpos($name, '.') === false) {
         return $this->getProperty($name);
     }
     $parts = explode('.', $name);
     $data = $this;
     foreach ($parts as $part) {
         $property = $data->getProperty($part);
         // stop recursion if property is an ArrayProperty because recursion is not possible in do's property
         if ($property instanceof ArrayProperty) {
             return $property;
         }
         if ($property instanceof Property\ObjectProperty) {
             if ($property->getValue() instanceof ObjectModel\DataObject) {
                 $data = $property->getValue();
             } else {
                 if ($property->getValue() instanceof BaseObject) {
                     $data = $property->getValue()->getDataObject();
                 } else {
                     if ($property->getValue() instanceof ObjectUri) {
                         $data = DataObject::factory($property->getParameter('instanceof'));
                         $data->setUri($property->getValue());
                         Backend::read($data);
                     } else {
                         $data = DataObject::factory($property->getParameter('instanceof'));
                     }
                 }
             }
         }
     }
     return $data->getProperty($part) ? $data->getProperty($part) : $property;
 }
Example #8
0
 public function find(Collection $collection, $returnCount = false)
 {
     $url = str_replace('{basepath}', Core::$basePath, $this->_uri->getUrl());
     $class = $collection->getDataObject()->getClass();
     if ($this->_mapper) {
         $file = $this->_mapper->getDatastore($class);
     } else {
         throw new Exception("Csv backends need a mapper where datastores are defined");
     }
     if (substr($file, 0, 1) != DIRECTORY_SEPARATOR) {
         $file = $url . $file;
     }
     try {
         $file = fopen($file, 'r');
     } catch (Exception $e) {
         throw new Exception("Error opening file: " . $e->getMessage());
     }
     // prepare defined conditions to be used next
     $this->_prepareConditionBlock($collection);
     // populate array with relevant objects type
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($class);
     if ($collection->getParameter('memberType') != 'uri') {
         $do = new ObjectModel\DataObject($class);
     }
     $separator = $this->_mapper->getExtraArg('separator', $class);
     if (!$separator) {
         $separator = ',';
     }
     $separator = str_replace(array('{tab}'), array("\t"), $separator);
     $array = array();
     $key = -1;
     // csv file current line key
     $selected = 0;
     // selected members counter
     $offset = $collection->getBoundaryOffset();
     $limit = $collection->getBoundaryBatch();
     while (($data = fgetcsv($file, 1000, $separator)) !== false) {
         if ($this->_mapper->getExtraArg('firstlineisheader', $class) !== false && $key == -1) {
             /* ignore first line if it is declared as header */
             $key += 1;
             $offset += 1;
             continue;
         }
         /* test data array against defined conditions */
         if ($this->_testAgainstConditionBlock($data) === false) {
             continue;
         }
         $key++;
         // ignore everything before offset value
         if ($key < $offset && $returnCount != true) {
             continue;
         }
         // break after limit value is reached except if we count lines
         if ($selected == $limit && $returnCount !== true) {
             return $array;
         }
         // increment selected counter
         $selected++;
         if ($returnCount !== true) {
             $uri = new ObjectModel\ObjectUri();
             $uri->setBackendUri($this->_uri);
             $uri->setClass($collection->getDataObject()->getClass());
             $uri->setUrl($url . '/' . $key);
             $do = DataObject::factory($class);
             $do->setUri($uri);
             $do->populate($data, $this->_mapper);
             $array[] = new $class($do);
         }
     }
     return $returnCount ? $key : $array;
 }
Example #9
0
 /**
  * Objects collection factory
  * @param string $class
  * @return t41\ObjectModel\Collection
  * @throws t41\ObjectModel\Exception
  */
 public static function collectionFactory($class)
 {
     try {
         $do = ObjectModel\DataObject::factory($class);
         $collection = new ObjectModel\Collection($do);
         return $collection;
     } catch (\Exception $e) {
         throw new Exception($e->getMessage());
     }
 }