コード例 #1
0
ファイル: ObjectModel.php プロジェクト: crapougnax/t41
 /**
  * 
  * 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();
         }
     }
 }
コード例 #2
0
ファイル: BaseObject.php プロジェクト: crapougnax/t41
 /**
  * 
  * 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();
 }
コード例 #3
0
ファイル: MediaElement.php プロジェクト: crapougnax/t41
 public static function getDownloadUrl(ObjectUri $uri)
 {
     $prefix = Core::getController('medias');
     if (!$prefix) {
         $prefix = '/t41/medias/';
     }
     return $prefix . 'download/obj/' . rawurlencode(base64_encode($uri->__toString()));
 }
コード例 #4
0
ファイル: AbstractAdapter.php プロジェクト: crapougnax/t41
 public function buildObjectUri($url, $class)
 {
     $tmp = explode('/', $url);
     $url = $this->_getTableFromClass($class) . '/' . $tmp[count($tmp) - 1];
     $uri = new ObjectModel\ObjectUri($url, $this->getUri());
     $uri->setClass($class);
     return $uri;
 }
コード例 #5
0
ファイル: MongoDBAdapter.php プロジェクト: crapougnax/t41
 public function find(ObjectModel\Collection $collection)
 {
     $class = $collection->getDataObject()->getClass();
     $mode = $collection->getParameter('memberType');
     // set database to use
     $this->_selectDatabase($collection->getDataObject()->getUri());
     // get collection to use, from mapper if available, else from data object
     $collec = $this->_mapper instanceof Backend\Mapper ? $this->_mapper->getDatastore($class) : $class;
     $collec = $this->_db->selectCollection($collec);
     // primary key is either part of the mapper configuration or 'id'
     $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : 'id';
     $conditions = array();
     /* @var $condition t41_Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         $condition = $conditionArray[0];
         // map property to field
         if ($this->_mapper) {
             $field = $this->_mapper->propertyToDatastoreName($class, $condition->getProperty()->getId());
         } else {
             $field = $condition->getProperty()->getId();
         }
         $conditions += $this->_buildConditionStatement($field, $condition->getClauses(), $conditions);
         switch ($conditionArray[1]) {
             case 'OR':
                 //$select->orWhere($statement);
                 break;
             case 'AND':
             default:
                 //$select->where($statement);
                 break;
         }
     }
     //Zend_Debug::dump($conditions);
     foreach ($collection->getSortings() as $sorting) {
         if ($this->_mapper) {
             $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId());
         } else {
             $field = $sorting[0]->getId();
         }
         //$select->order($field, $sorting[1]);
     }
     //		$select->limit($collection->getBatchBoundary(), $collection->getOffsetBoundary());
     $ids = $collec->find($conditions, array('_id'));
     /* prepare base of object uri */
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($collection->getDataObject()->getClass());
     $uri->setUrl($this->_database . '/');
     return $this->_populateCollection(iterator_to_array($ids), $collection, $uri);
 }
コード例 #6
0
ファイル: PdoAdapter.php プロジェクト: crapougnax/t41
 /**
  * Returns an array of objects queried from the given t41_Object_Collection instance parameters
  * 
  * The given collection is populated if it comes empty of members.
  * 
  * In any other case, this method doesn't directly populate the collection. This action is under the responsability of 
  * the caller. For example, the t41_Object_Collection::find() method takes care of it.
  * 
  * @param t41_Object_Collection $collection
  * @return array
  */
 public function find(t41_Object_Collection $collection, $returnCount = false)
 {
     $class = $collection->getDataObject()->getClass();
     $table = $this->_getTableFromClass($class);
     if (!$table) {
         throw new Exception('BACKEND_MISSING_DBTABLE_PARAM');
     }
     // primary key is either part of the mapper configuration or 'id'
     //$pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : 'id';
     //$pkey = $table . '.' . $pkey;
     // primary key is either part of the mapper configuration or 'id'
     $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : \t41\Backend::DEFAULT_PKEY;
     if (is_array($pkey)) {
         $composite = array();
         /* @var $obj t41_Backend_Key */
         foreach ($pkey as $obj) {
             $composite[] = sprintf('TRIM(%s)', $table . '.' . $obj->getName());
         }
         $pkey = sprintf("%s", implode(',', $composite));
     } else {
         $pkey = $table . '.' . $pkey;
     }
     $this->_connect();
     /* @var $select Zend_Db_Select */
     $select = $this->_ressource->select();
     //$select->from($table, $returnCount ? new Zend_Db_Expr("COUNT($pkey)") : $pkey);
     $select->from($table, $returnCount ? new \Zend_Db_Expr("COUNT(*) AS " . \t41\Backend::MAX_ROWS_IDENTIFIER) : $pkey);
     /* @var $condition t41_Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         $jtable = '';
         $class = $collection->getDataObject()->getClass();
         $condition = $conditionArray[0];
         /* does condition contain another condition object ? */
         if ($condition->isRecursive()) {
             while ($condition->isRecursive()) {
                 $property = $condition->getProperty();
                 $parent = $property->getParent() ? $property->getParent()->getId() : $table;
                 $condition = $condition->getCondition();
                 $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $property->getParameter('instanceof');
                 $jpkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : 'id';
                 $parentTable = $this->_mapper ? $this->_mapper->getDatastore($parent) : $parent;
                 $join = sprintf("%s.%s = %s.%s", $parentTable, $jpkey, $jtable, $jpkey);
                 $select->joinLeft($jtable, $join, array());
                 $class = $property->getParameter('instanceof');
             }
         }
         $property = $condition->getProperty();
         if ($property instanceof Property\ObjectProperty) {
             $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $property->getParameter('instanceof');
             $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $property->getId()) : $property->getId();
             $field = $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : 'id';
             $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $jtable, $rightkey);
             $select->joinLeft($jtable, $join, array());
         } else {
             $field = $property->getId();
             if ($this->_mapper) {
                 $field = $this->_mapper->propertyToDatastoreName($class, $field);
             }
         }
         /* if a join was performed, prefix current field with table name */
         if ($jtable) {
             $field = $jtable . '.' . $field;
         }
         $statement = $this->_buildConditionStatement($field, $condition->getClauses());
         switch ($conditionArray[1]) {
             case 'OR':
                 $select->orWhere($statement);
                 break;
             case 'AND':
             default:
                 $select->where($statement);
                 break;
         }
     }
     if ($returnCount != true) {
         foreach ($collection->getSortings() as $sorting) {
             if ($this->_mapper) {
                 $class = $sorting[0]->getParent() ? $sorting[0]->getParent()->getId() : $collection->getDataObject()->getClass();
                 $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId());
             } else {
                 $field = $sorting[0]->getId();
             }
             $select->order($field, $sorting[1]);
         }
         $select->limit($collection->getBoundaryBatch(), $collection->getBoundaryOffset());
     }
     //		echo $select; die;
     $result = array();
     $context = array('table' => $table);
     try {
         $result = $this->_ressource->fetchAll($select);
     } catch (\Zend_Db_Exception $e) {
         $context['error'] = $e->getMessage();
     }
     $this->_setLastQuery($select->__toString(), $select->getPart('where'), $context);
     if ($returnCount == true) {
         return $result[0][\t41\Backend::MAX_ROWS_IDENTIFIER];
     }
     // convert array of primary keys to strings
     foreach ($result as $key => $val) {
         $result[$key] = implode(\t41\Mapper::VALUES_SEPARATOR, $val);
     }
     /* prepare base of object uri */
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($collection->getDataObject()->getClass());
     $uri->setUrl($this->_database . '/' . $table . '/');
     return $this->_populateCollection($result, $collection, $uri);
 }
コード例 #7
0
ファイル: LdapAdapter.php プロジェクト: crapougnax/t41
 /**
  * Returns an array of objects queried from the given t41_Object_Collection instance parameters
  * 
  * The given collection is populated if it comes empty of members.
  * 
  * In any other case, this method doesn't directly populate the collection. This action is under the responsability of 
  * the caller. For example, the t41_Object_Collection::find() method takes care of it.
  * 
  * @param t41_Object_Collection $collection
  * @return array
  */
 public function find(t41_Object_Collection $collection)
 {
     $class = $collection->getDataObject()->getClass();
     $filters = $sortings = array();
     $searchMode = '&';
     // primary key is either part of the mapper configuration or 'dn'
     $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : 'dn';
     /* @var $condition t41_Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         $condition = $conditionArray[0];
         /* does condition contain another condition object ? */
         if ($condition->isRecursive()) {
             // not supported with LDAP
             continue;
         }
         $property = $condition->getProperty();
         if ($property instanceof Property\ObjectProperty) {
         } else {
             $field = $property->getId();
             if ($this->_mapper) {
                 $field = $this->_mapper->propertyToDatastoreName($class, $field);
             }
         }
         $filters[] = $this->_buildConditionStatement($field, $condition->getClauses());
         switch ($conditionArray[1]) {
             case 'OR':
                 //					$select->orWhere($statement);
                 break;
             case 'AND':
             default:
                 //					$select->where($statement);
                 break;
         }
     }
     foreach ($collection->getSortings() as $sorting) {
         if ($this->_mapper) {
             $class = $sorting[0]->getParent() ? $sorting[0]->getParent()->getId() : $collection->getDataObject()->getClass();
             $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId());
         } else {
             $field = $sorting[0]->getId();
         }
         $sortings[] = $field;
     }
     $filter = implode($filters);
     if (count($sortings) > 0) {
         $filter .= sprintf('(sort=%s)', implode(',', $sortings));
     }
     if ($filter) {
         $filter = sprintf('%s%s', $searchMode, $filter);
     }
     $filter = $filter ? '(' . $filter . ')' : "(objectClass=*)";
     try {
         if ($this->_mapper) {
             $this->_connect($this->_mapper->getDatastore($collection->getDataObject()->getClass()));
         } else {
             $this->_connect();
         }
         /* @var $result Zend_Ldap_Collection */
         /*			$result = $this->_ressource->search(  $filter
         												, $this->_currentDn					// Base DN
         												, null //Zend_Ldap::SEARCH_SCOPE_ONE		// Scope
         												, null//array('sizeLimit' => $collection->getBoundaryBatch())
         											   ); // query result
         */
         $search = ldap_search($this->_ressource, $this->_currentDn, $filter);
         $result = ldap_get_entries($this->_ressource, $search);
     } catch (Exception $e) {
         throw new Exception("LDAP Query error: " . $e->getMessage());
     }
     // first result is total records
     unset($result['count']);
     // populate array with relevant objects type
     $array = array();
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($class);
     if ($collection->getParameter('memberType') != 'uri') {
         $do = new ObjectModel\DataObject($class);
     }
     foreach ($result as $entry) {
         $entry = $this->_flattenArray($entry, true);
         $uri->setUrl($this->_uri->getAlias() . '/' . $entry['dn']);
         switch ($collection->getParameter('memberType')) {
             case 'uri':
                 $data = clone $uri;
                 break;
             case 'data':
             default:
                 $do->setUri(clone $uri);
                 $do->populate($entry, $this->_mapper);
                 $data = clone $do;
                 break;
             case 'model':
                 $do->setUri(clone $uri);
                 $do->populate($entry, $this->_mapper);
                 /* @var $obj t41_Object_Model */
                 $data = new $class(null, null, clone $do);
                 break;
         }
         $array[] = $data;
     }
     return $array;
 }
コード例 #8
0
ファイル: Core.php プロジェクト: crapougnax/t41
 /**
  * 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;
     }
 }
コード例 #9
0
ファイル: Foreignkey.php プロジェクト: crapougnax/t41
 public function setValue($val)
 {
     if (!$val instanceof ObjectModel\ObjectUri) {
         $val = new ObjectModel\ObjectUri($val);
         $val->setClass($this->getCollection()->getClass());
     }
     parent::setValue($val);
 }
コード例 #10
0
ファイル: GridElement.php プロジェクト: crapougnax/t41
 public function formatValue($key = null)
 {
     if ($key == null) {
         return '';
     }
     // value is already available (foreign key with no specific constraint in it
     if (isset($this->_enumValues[$key])) {
         return $this->_enumValues[$key];
     }
     // value no more available to select, though we need to display it !
     if (is_string($key)) {
         $uri = new ObjectModel\ObjectUri($key);
         $uri->setClass($this->getCollection()->getClass());
         $_do = clone $this->_collection->getDataObject();
         $_do->setUri($uri);
         Backend::read($_do);
     } else {
         $_do = $key->getDataObject();
     }
     return Property::parseDisplayProperty($_do, $this->getParameter('display'));
 }
コード例 #11
0
ファイル: XmlAdapter.php プロジェクト: crapougnax/t41
 public function find(ObjectModel\Collection $collection)
 {
     $class = $collection->getDataObject()->getClass();
     $mode = $collection->getParameter('memberType');
     $expr = '';
     $this->_setRessource($class);
     /* @var $condition t41_Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         $condition = $conditionArray[0];
         // map property to field
         if ($this->_mapper) {
             $field = $this->_mapper->propertyToDatastoreName($class, $condition->getProperty()->getId());
         } else {
             $field = $condition->getProperty()->getId();
         }
         if ($expr) {
             switch ($conditionArray[1]) {
                 case 'OR':
                     $expr .= ' or ';
                     break;
                 case 'AND':
                 default:
                     $expr .= ' and ';
                     break;
             }
         }
         $expr .= sprintf("%s %s '%s'", $field, is_numeric($condition->getOperator()) ? $this->_operators[$condition->getOperator()] : $condition->getOperator(), $condition->getValue());
     }
     // get all nodes id
     $result = $this->_findNodes($expr, '@id');
     $dataSet = array();
     foreach ($result as $node) {
         $dataSet[] = $node->nodeValue;
     }
     if (count($collection->getSortings()) > 0) {
         $sort = array();
         foreach ($collection->getSortings() as $key => $sorting) {
             if ($this->_mapper) {
                 $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId());
             } else {
                 $field = $sorting[0]->getId();
             }
             $sort = $this->_sortNodes($sort, $field, $sorting[1], $key == 0 ? $dataSet : null);
         }
     }
     // Flatten array
     $sort = $this->_arrayflat($sort);
     //Zend_Debug::dump($sort);
     $array = array();
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($class);
     if ($mode != 'uri') {
         $do = new ObjectModel\DataObject($class);
     }
     $count = $collection->getBoundaryOffset();
     $limit = $count + $collection->getBoundaryBatch();
     /* iterate over result set as long as requested */
     while ($count < $limit) {
         if (!isset($dataSet[$count])) {
             // if end of result data set has been reached, return array
             return $array;
         }
         $id = $dataSet[$count];
         $uri->setUrl($this->_alias . '/' . $id);
         switch ($mode) {
             case 'uri':
                 $data = clone $uri;
                 break;
             case 'data':
                 $do->setUri(clone $uri);
                 $do->populate();
                 $data = clone $do;
                 break;
             case 'model':
                 $do->setUri(clone $uri);
                 $do->populate();
                 /* @var $obj t41_Object_Model */
                 $data = new $class(null, null, clone $do);
                 break;
         }
         $array[] = $data;
         $count++;
     }
     return $array;
 }
コード例 #12
0
ファイル: AbstractPdoAdapter.php プロジェクト: crapougnax/t41
 /**
  * Returns an array of objects queried from the given t41_Object_Collection instance parameters
  * 
  * The given collection is populated if it comes empty of members.
  * 
  * In any other case, this method doesn't directly populate the collection. This action is under the responsability of 
  * the caller. For example, the t41_Object_Collection::find() method takes care of it.
  * 
  * @param t41\ObjectModel\Collection $collection
  * @param boolean|array $returnCount true = counting, array = stats on listed properties
  * @param string $subOp complex operation like SUM or AVG
  * @return array
  */
 public function find(ObjectModel\Collection $collection, $returnCount = false, $subOp = null)
 {
     $this->_class = $class = $collection->getDataObject()->getClass();
     $table = $this->_getTableFromClass($class);
     if (!$table) {
         throw new Exception('MISSING_DBTABLE_PARAM');
     }
     // primary key is either part of the mapper configuration or 'id'
     $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : \t41\Backend::DEFAULT_PKEY;
     if (is_array($pkey)) {
         $composite = array();
         /* @var $obj t41\Backend\Key */
         foreach ($pkey as $obj) {
             $composite[] = sprintf('TRIM(%s)', $table . '.' . $obj->getName());
             $composite[] = Backend\Mapper::VALUES_SEPARATOR;
         }
         $pkey = sprintf("CONCAT(%s) AS %s", implode(',', $composite), Backend::DEFAULT_PKEY);
     } else {
         $pkey = $table . '.' . $pkey;
     }
     $this->_connect();
     /* @var $select \Zend_Db_Select */
     $this->_select = $this->_ressource->select();
     // detect if query is of stat-kind
     if ($returnCount) {
         switch ($subOp) {
             case ObjectModel::CALC_SUM:
                 $expressions = array();
                 foreach ($returnCount as $propKey => $property) {
                     $prop = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $propKey) : $propKey;
                     $expressions[] = sprintf('SUM(%s.%s)', $table, $prop);
                 }
                 $subOpExpr = implode('+', $expressions);
                 break;
             case ObjectModel::CALC_AVG:
                 $subOpExpr = sprintf('AVG(%s)', $returnCount);
                 break;
             default:
                 $subOpExpr = 'COUNT(*)';
                 break;
         }
         $this->_select->from($table, new \Zend_Db_Expr($subOpExpr . " AS " . \t41\Backend::MAX_ROWS_IDENTIFIER));
     } else {
         $this->_select->distinct();
         $this->_select->from($table, $pkey);
     }
     $this->_alreadyJoined = array();
     /* @var $condition t41\Backend\Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         // combo conditions
         if ($conditionArray[0] instanceof Condition\Combo) {
             $statement = array();
             foreach ($conditionArray[0]->getConditions() as $condition) {
                 $statement[] = $this->_parseCondition($condition[0], $this->_select, $table);
             }
             $statement = implode(' OR ', $statement);
             switch ($conditionArray[1]) {
                 case Condition::MODE_OR:
                     $this->_select->orWhere($statement);
                     break;
                 case Condition::MODE_AND:
                 default:
                     $this->_select->where($statement);
                     break;
             }
             continue;
         }
         // optional table where the column may be
         $jtable = '';
         // condition object is in the first key
         $condition = $conditionArray[0];
         /* does condition contain another condition object ? */
         if ($condition->isRecursive()) {
             while ($condition->isRecursive()) {
                 $property = $condition->getProperty();
                 $parent = $property->getParent() ? $property->getParent()->getId() : $table;
                 $condition = $condition->getCondition();
                 if ($jtable) {
                     $parentTable = $jtable;
                 } else {
                     if ($parent) {
                         $parentTable = $this->_mapper ? $this->_mapper->getDatastore($parent) : $parent;
                     } else {
                         $parentTable = $table;
                     }
                 }
                 $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $this->_getTableFromClass($property->getParameter('instanceof'));
                 /* column name in left table */
                 $jlkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $property->getId()) : $property->getId();
                 $uniqext = $jtable . '__joined_for__' . $jlkey;
                 if (in_array($uniqext, $this->_alreadyJoined)) {
                     $class = $property->getParameter('instanceof');
                     $jtable = $uniqext;
                     continue;
                 }
                 /* pkey name in joined table */
                 $jpkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : Backend::DEFAULT_PKEY;
                 $join = sprintf("%s.%s = %s.%s", $parentTable, $jlkey, $uniqext, $jpkey);
                 $this->_select->joinLeft($jtable . " AS {$uniqext}", $join, array());
                 $this->_alreadyJoined[$jtable] = $uniqext;
                 //$jtable;
                 $jtable = $uniqext;
                 $class = $property->getParameter('instanceof');
             }
         }
         $property = $condition->getProperty();
         if ($property instanceof Property\ObjectProperty) {
             // no join if object is stored in a different backend !
             // @todo improve this part
             if (ObjectModel::getObjectBackend($property->getParameter('instanceof'))->getAlias() != $this->_uri->getAlias()) {
                 $clauses = $condition->getClauses();
                 if ($clauses[0]['value'] != Condition::NO_VALUE) {
                     $clauses[0]['operator'] = Condition::OPERATOR_ENDSWITH | Condition::OPERATOR_EQUAL;
                     $condition->setClauses($clauses);
                 }
                 $field = $this->_mapper ? $this->_mapper->propertyToDatastoreName($this->_class, $property->getId()) : $property->getId();
             } else {
                 // which table to join with ? (in case of condition is last element of a recursion)
                 $jtable2 = $jtable ? $jtable : $table;
                 $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $this->_getTableFromClass($property->getParameter('instanceof'));
                 $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $property->getId()) : $property->getId();
                 $field = $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : Backend::DEFAULT_PKEY;
                 $uniqext = $jtable . '__joined_for__' . $leftkey;
                 if (!in_array($uniqext, $this->_alreadyJoined)) {
                     $join = sprintf("%s.%s = %s.%s", $jtable2, $leftkey, $uniqext, is_array($rightkey) ? $rightkey[0] : $rightkey);
                     $this->_select->joinLeft($jtable . " AS {$uniqext}", $join, array());
                     $this->_alreadyJoined[$jtable] = $uniqext;
                 }
                 $jtable = $uniqext;
             }
         } else {
             if ($property instanceof Property\CollectionProperty) {
                 // handling of conditions based on collection limited to withMembers() and withoutMembers()
                 $leftkey = $property->getParameter('keyprop');
                 $field = $property->getId();
                 $subSelect = $this->_ressource->select();
                 $subseltbl = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $this->_getTableFromClass($property->getParameter('instanceof'));
                 $subSelect->from($subseltbl, new \Zend_Db_Expr(sprintf("COUNT(%s)", $leftkey)));
                 $join = sprintf("%s.%s = %s", $subseltbl, $leftkey, $pkey);
                 $subSelect->where($join);
                 $statement = $this->_buildConditionStatement(new \Zend_Db_Expr(sprintf("(%s)", $subSelect)), $condition->getClauses(), $conditionArray[1]);
                 $this->_select->where($statement);
                 continue;
             } else {
                 $field = $property->getId();
                 if ($this->_mapper) {
                     $field = $this->_mapper->propertyToDatastoreName($class, $field);
                 }
             }
         }
         /* convert identifier tag to the valid primary key */
         if ($field == ObjectUri::IDENTIFIER) {
             // @todo handle multiple keys from mapper
             $field = $table . '.';
             $key = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : Backend::DEFAULT_PKEY;
             $field .= is_array($key) ? $key[0] : $key;
         } else {
             if ($jtable) {
                 if (array_key_exists($jtable, $this->_alreadyJoined)) {
                     $field = $this->_alreadyJoined[$jtable] . '.' . $field;
                 } else {
                     $tmp = $jtable . '.';
                     $tmp .= is_array($field) ? $field[0] : $field;
                     $field = $tmp;
                 }
             } else {
                 if (array_key_exists($table, $this->_alreadyJoined)) {
                     $field = $this->_alreadyJoined[$table] . '.' . $field;
                 } else {
                     $field = $table . '.' . $field;
                 }
             }
         }
         if ($field instanceof Key) {
             $field = $table . '.' . $field->getName();
         }
         // protect DateProperty() with setted timepart parameter from misuse
         if ($property instanceof DateProperty && $property->getParameter('timepart') == true) {
             $field = "DATE({$field})";
         }
         $statement = $this->_buildConditionStatement($field, $condition->getClauses(), $conditionArray[1]);
         switch ($conditionArray[1]) {
             case Condition::MODE_OR:
                 $this->_select->orWhere($statement);
                 break;
             case Condition::MODE_AND:
             default:
                 $this->_select->where($statement);
                 break;
         }
     }
     // Adjust query based on returnCount
     if ($returnCount) {
         if (is_array($returnCount)) {
             if ($subOp) {
             } else {
                 // return count on grouped columns
                 foreach ($returnCount as $key => $property) {
                     $fieldmodifier = null;
                     if ($this->_mapper) {
                         $class = $property->getParent() ? $property->getParent()->getId() : $collection->getDataObject()->getClass();
                         $field = $this->_mapper->propertyToDatastoreName($class, $property->getId());
                     } else {
                         $field = $property->getId();
                     }
                     if ($property instanceof ObjectProperty) {
                         // join with $key if necessary
                         if (strstr($key, '.') !== false) {
                             $leftPart = substr($key, 0, strpos($key, '.'));
                             $intermediateProp = $collection->getDataObject()->getProperty($leftPart);
                             $fieldmodifier = $this->_join($intermediateProp, $table) . '.' . $field;
                         }
                     }
                     // limit date grouping to date part, omitting possible hour part
                     if ($property instanceof DateProperty) {
                         $fieldmodifier = "DATE({$field})";
                     }
                     $this->_select->group($fieldmodifier ? $fieldmodifier : $field);
                     $this->_select->columns(array($field => $fieldmodifier ? $fieldmodifier : $field));
                 }
             }
         } else {
             $this->_select->reset('group');
         }
     } else {
         $this->_select->limit($collection->getBoundaryBatch() != -1 ? $collection->getBoundaryBatch() : null, $collection->getBoundaryOffset());
         /**
          * Sorting part
          */
         foreach ($collection->getSortings() as $sorting) {
             $slUniqext = $slTable = null;
             // Specific cases first
             // @todo find a better way to sort on meta properties
             if ($sorting[0]->getId() == ObjectUri::IDENTIFIER || $sorting[0] instanceof MetaProperty) {
                 $id = Backend::DEFAULT_PKEY;
                 $this->_select->order(new \Zend_Db_Expr($table . '.' . $id . ' ' . $sorting[1]));
                 continue;
             } else {
                 if ($sorting[0] instanceof Property\CollectionProperty) {
                     // handling of conditions based on collection limited to withMembers() and withoutMembers()
                     $leftkey = $sorting[0]->getParameter('keyprop');
                     //$field = $property->getId();
                     $subSelect = $this->_ressource->select();
                     $subseltbl = $this->_mapper ? $this->_mapper->getDatastore($sorting[0]->getParameter('instanceof')) : $this->_getTableFromClass($sorting[0]->getParameter('instanceof'));
                     $subSelect->from($subseltbl, new \Zend_Db_Expr(sprintf("COUNT(%s)", $leftkey)));
                     $join = sprintf("%s.%s = %s", $subseltbl, $leftkey, $pkey);
                     $subSelect->where($join);
                     // $statement = $this->_buildConditionStatement(new \Zend_Db_Expr(sprintf("(%s)", $subSelect)), $condition->getClauses(), $conditionArray[1]);
                     $this->_select->order(new \Zend_Db_Expr('(' . $subSelect->__toString() . ') ' . $sorting[1]));
                     continue;
                 } else {
                     if ($sorting[0] instanceof Property\ObjectProperty) {
                         // find which property to sort by
                         if ($sorting[0]->getParameter('sorting')) {
                             $sprops = array_keys($sorting[0]->getParameter('sorting'));
                         } else {
                             // try to sort with properties used to display value
                             if (substr($sorting[0]->getParameter('display'), 0, 1) == '[') {
                                 // @todo extract elements of pattern to order from them ?
                                 $sprops = array('id');
                             } else {
                                 $sprops = explode(',', $sorting[0]->getParameter('display'));
                             }
                         }
                         // sorting property belongs to a second-level join
                         if ($sorting[0]->getParent()->getClass() != $collection->getClass()) {
                             $leftkey = 'commande';
                             //$this->_mapper ? $this->_mapper->propertyToDatastoreName($collection->getDataObject()->getClass(), $sorting[0]->getParent()getId()) : $sorting[0]->getId();
                             $class = $sorting[0]->getParent()->getClass();
                             $stable = $this->_getTableFromClass($class);
                             $sbackend = ObjectModel::getObjectBackend($class);
                             // Property to sort from is in a different backend from current one
                             if ($sbackend->getAlias() != $this->getAlias()) {
                                 // We presume that the current backend is allowed to connect to the remote one
                                 // Should we raise an exception instead ?
                                 $stable = $sbackend->getUri()->getDatabase() . '.' . $stable;
                             }
                             $field = $sorting[0]->getId();
                             $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : Backend::DEFAULT_PKEY;
                             $uniqext = $stable . '__joined_for__' . $leftkey;
                             if (!in_array($uniqext, $this->_alreadyJoined)) {
                                 if (is_array($rightkey)) {
                                     foreach ($rightkey as $rightkeyObj) {
                                         $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $uniqext, $rightkeyObj->getName());
                                     }
                                 } else {
                                     $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $uniqext, $rightkey);
                                 }
                                 $this->_select->joinLeft("{$stable} AS {$uniqext}", $join, array());
                                 $this->_alreadyJoined[$stable] = $uniqext;
                             }
                             $slTable = $this->_getTableFromClass($sorting[0]->getParameter('instanceof'));
                             $slUniqext = $uniqext;
                         }
                         $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($collection->getDataObject()->getClass(), $sorting[0]->getId()) : $sorting[0]->getId();
                         $class = $sorting[0]->getParameter('instanceof');
                         $stable = isset($slTable) ? $slTable : $this->_getTableFromClass($class);
                         $sbackend = ObjectModel::getObjectBackend($class);
                         // Property to sort from is in a different backend from current one
                         if ($sbackend->getAlias() != $this->getAlias()) {
                             // We presume that the current backend is allowed to connect to the remote one
                             // Should we raise an exception instead ?
                             $stable = $sbackend->getUri()->getDatabase() . '.' . $stable;
                         }
                         $field = $sorting[0]->getId();
                         $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : Backend::DEFAULT_PKEY;
                         $uniqext = $stable . '__joined_for__' . $leftkey;
                         if (!in_array($uniqext, $this->_alreadyJoined)) {
                             if (is_array($rightkey)) {
                                 foreach ($rightkey as $rightkeyObj) {
                                     $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $uniqext, $rightkeyObj->getName());
                                 }
                             } else {
                                 $join = sprintf("%s.%s = %s.%s", isset($slUniqext) ? $slUniqext : $table, $leftkey, $uniqext, $rightkey);
                             }
                             $this->_select->joinLeft("{$stable} AS {$uniqext}", $join, array());
                             $this->_alreadyJoined[$stable] = $uniqext;
                         }
                         foreach ($sprops as $sprop) {
                             if ($this->_mapper) {
                                 $sfield = $this->_mapper->propertyToDatastoreName($class, $sprop);
                             } else {
                                 $sfield = $sprop;
                             }
                             $sortingExpr = $this->_alreadyJoined[$stable] . '.' . $sfield;
                             if (isset($sorting[2]) && !empty($sorting[2])) {
                                 $sortingExpr = sprintf('%s(%s)', $sorting[2], $sortingExpr);
                             }
                             $this->_select->order(new \Zend_Db_Expr($sortingExpr . ' ' . $sorting[1]));
                         }
                         continue;
                     }
                 }
             }
             // default sorting on a different table
             $class = $sorting[0]->getParent() ? $sorting[0]->getParent()->getClass() : $collection->getDataObject()->getClass();
             $stable = $this->_getTableFromClass($class);
             if ($this->_mapper) {
                 $sfield = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId());
             } else {
                 $field = $sorting[0];
                 $sfield = $field->getId();
             }
             // add a left join if the sorting field belongs to a table not yet part of the query
             if ($stable != $table) {
                 // get the property id from the class name
                 $tfield = isset($sorting[3]) ? $sorting[3] : $collection->getDataObject()->getObjectPropertyId($class);
                 $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $tfield) : $tfield;
                 $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($field->getParameter('instanceof')) : Backend::DEFAULT_PKEY;
                 $uniqext = $stable . '__joined_for__' . $leftkey;
                 if (!in_array($uniqext, $this->_alreadyJoined)) {
                     $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $uniqext, $rightkey);
                     $this->_select->joinLeft("{$stable} AS {$uniqext}", $join, array());
                     $this->_alreadyJoined[$stable] = $uniqext;
                 }
                 $sortingExpr = $this->_alreadyJoined[$stable] . '.' . $sfield;
             } else {
                 $sortingExpr = $stable . '.' . $sfield;
             }
             if (isset($sorting[2]) && !empty($sorting[2])) {
                 $sortingExpr = sprintf('%s(%s)', $sorting[2], $sortingExpr);
             }
             $this->_select->order(new \Zend_Db_Expr('TRIM(' . $sortingExpr . ') ' . $sorting[1]));
         }
     }
     $result = array();
     $context = array('table' => $table);
     try {
         if (true && $returnCount == false) {
             $this->_select->columns($this->_getColumns($collection->getDataObject()));
         }
         $result = $this->_ressource->fetchAll($this->_select);
     } catch (\Zend_Db_Exception $e) {
         $context['error'] = $e->getMessage();
         $this->_setLastQuery($this->_select->__toString(), $this->_select->getPart('where'), $context);
         return false;
     }
     $this->_setLastQuery($this->_select->__toString(), $this->_select->getPart('where'), $context);
     if ($returnCount !== false) {
         return is_array($returnCount) ? $result : $result[0][Backend::MAX_ROWS_IDENTIFIER];
     }
     // convert array of primary keys to strings
     foreach ($result as $key => $val) {
         //	$result[$key] = implode(Backend\Mapper::VALUES_SEPARATOR, $val);
     }
     /* prepare base of object uri */
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($collection->getDataObject()->getClass());
     $uri->setUrl($this->_database . '/' . $table . '/');
     return $collection->populate($result, $uri);
     //return $this->_populateCollection($result, $collection, $uri);
 }
コード例 #13
0
ファイル: CsvAdapter.php プロジェクト: crapougnax/t41
 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;
 }
コード例 #14
0
ファイル: AdapterAbstract.php プロジェクト: crapougnax/t41
 public function buildObjectUri($url, $class)
 {
     $url = $this->_database . '/' . $this->_getTableFromClass($class) . '/' . $url;
     $uri = new ObjectModel\ObjectUri($url, $this->getUri());
     $uri->setClass($class);
     return $uri;
 }
コード例 #15
0
ファイル: ObjectModel.php プロジェクト: crapougnax/t41
 /**
  * Returns an instance of an object based on definition matching the given id
  * 
  * @param string|ObjectModel\ObjectUri $param class id or object uri
  * @throws ObjectModel\Exception
  * @return ObjectModel\BaseObject
  */
 public static function factory($param)
 {
     if ($param instanceof DataObject) {
         $class = $param->getClass();
         return new $class($param);
     }
     $class = $param instanceof ObjectModel\ObjectUri ? $param->getClass() : $param;
     if (!array_key_exists($class, self::$_config)) {
         throw new ObjectModel\Exception(array('NO_CLASS_DECLARATION', $class));
     }
     try {
         $obj = new $class($param instanceof ObjectModel\ObjectUri ? $param : null);
     } catch (ObjectModel\Exception $e) {
         die($e->getMessage());
     } catch (ObjectModel\DataObject\Exception $e) {
         die($e->getMessage());
     }
     return $obj;
 }