Example #1
0
 public function preDispatch()
 {
     $this->_post = $this->getRequest()->getPost();
     $this->_uuid = $this->_getParam('uuid');
     if ($this->_uuid) {
         $this->_obj = Core\Registry::get($this->_uuid);
         $this->_defineRedirect(array('ok', 'nok', 'err'));
         if ($this->_obj instanceof ObjectModel\ObjectUri) {
             $this->_obj = ObjectModel::factory($this->_obj);
         }
         if (!$this->_obj) {
             $this->_context['message'] = "Unable to restore object";
             $this->_status = 'ERR';
             $this->postDispatch();
         }
         // @todo post actions coming from js, unsecure, we should keep a reference of the handling form object
         if ($this->_getParam('post_ok')) {
             $this->_actions['ok'] = $this->_getParam('post_ok');
         }
     } else {
         $this->_context['message'] = 'Missing remote object id';
         $this->_status = 'NOK';
         $this->postDispatch();
     }
 }
Example #2
0
 /**
  * Prepares the environment before running a test.
  */
 protected function setUp()
 {
     parent::setUp();
     $path = explode(DIRECTORY_SEPARATOR, dirname(__FILE__));
     array_pop($path);
     array_pop($path);
     $path = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR;
     set_include_path(get_include_path() . PATH_SEPARATOR . $path . 'library' . PATH_SEPARATOR . $path . 'tests/fixtures/model');
     require_once 't41/Core.php';
     t41\Core::enableAutoloader(array('t41', 'Tests'));
     t41\Config::addPath($path . 'tests/fixtures/configs/', t41\Config::REALM_CONFIGS);
     t41\ObjectModel::loadConfig();
     $this->Person = \t41\ObjectModel::factory('Tests\\Person');
 }
Example #3
0
 /**
  * Execute the action and returns a result
  *
  * @return array
  */
 public function execute()
 {
     $res = false;
     switch ($this->_callback) {
         case 'create':
             $this->_obj = ObjectModel::factory($this->getClass());
             foreach ($this->getContext() as $key => $value) {
                 if ($this->_obj->setProperty($key, $value) === false) {
                     var_dump($key);
                     die;
                     continue;
                 }
             }
             $res = $this->_obj->save();
             break;
     }
     return $res;
 }
Example #4
0
 /**
  * Return the current value in the $param form
  * @see t41\ObjectModel\Property.AbstractProperty::getValue()
  * @param string $param define which format to use
  */
 public function getValue($param = null)
 {
     if (is_null($this->_value)) {
         return null;
     }
     /* if param is null, return the value in its current format */
     if (is_null($param)) {
         return $this->_value;
     }
     switch ($param) {
         case ObjectModel::MODEL:
             if ($this->_value instanceof ObjectModel\DataObject) {
                 $this->_value = ObjectModel::factory($this->_value);
                 return $this->_value;
             } else {
                 if ($this->_value instanceof ObjectUri) {
                     /* object uri */
                     $this->_value = ObjectModel::factory($this->_value);
                 }
             }
             return $this->_value;
             break;
         case ObjectModel::DATA:
             if ($this->_value instanceof ObjectUri) {
                 $this->_value = ObjectModel::factory($this->_value);
                 return $this->_value->getDataObject();
             } else {
                 if ($this->_value instanceof DataObject) {
                     return $this->_value;
                 } else {
                     return $this->_value->getDataObject();
                 }
             }
             break;
         case ObjectModel::URI:
         default:
             if ($this->_value instanceof ObjectUri) {
                 return $this->_value;
             } else {
                 if ($this->_value instanceof DataObject) {
                     return $this->_value->getUri();
                 } else {
                     return $this->_value->getDataObject()->getUri();
                 }
             }
             break;
     }
 }
Example #5
0
 public function setRules(array $rules = array())
 {
     if (count($rules) == 0) {
         $rules = (array) ObjectModel::getRules($this);
     }
     // attach each rule to the relevant property
     foreach ($rules as $trigger => $rulesArray) {
         $parts = explode('/', $trigger);
         foreach ($rulesArray as $key => $rule) {
             // rules on properties
             if (isset($parts[2]) && ($property = $this->_dataObject->getProperty($parts[2])) !== false) {
                 $property->attach($rule, $parts[0] . '/' . $parts[1]);
                 unset($rules[$trigger][$key]);
                 /*
                  * @todo find  a way to also pass rules to whatever property is concerned
                  * ex: if the rules allows to compute data from a collection, any change to the collection
                  * members should trigger the rule.
                  */
             } else {
                 $this->attach($rule, $parts[0] . '/' . $parts[1]);
             }
         }
     }
     $this->_rules = $rules;
     return $this;
 }
Example #6
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 #7
0
 /**
  * Return the number of distinct objects matching the given property
  * @param string $string
  * @param unknown_type $backend
  * @throws Backend\Exception
  * @return array
  */
 public function returnsDistinct($string, $backend = null)
 {
     $prop = $this->_do->getProperty($string);
     if (!$prop instanceof Property\AbstractProperty) {
         throw new Backend\Exception(array("CONDITION_UNKNOWN_PROPERTY", $string));
     }
     if (is_null($backend)) {
         $backend = ObjectModel::getObjectBackend($this->_do->getClass());
     }
     return (array) Backend::returnsDistinct($this, $prop, $backend);
 }
Example #8
0
 /**
  * Populates a data object from a key/value array
  *
  * @param array $data
  * @param \t41\Backend\Mapper $mapper
  * @return \t41\ObjectModel\DataObject
  */
 public function populate(array $data, Backend\Mapper $mapper = null)
 {
     if ($mapper) {
         // @todo fix compatibility with metakeys in array
         $data = $mapper->toDataObject($data, $this->_class);
     }
     // then sent to data object properties
     foreach ($data as $key => $value) {
         // don't use empty() to check $value to avoid zero being ignored
         if (($property = $this->getProperty($key)) !== false && !is_null($value)) {
             if ($value == Property::EMPTY_VALUE) {
                 $property->resetValue();
                 continue;
             }
             if ($property instanceof ObjectProperty || $property instanceof MediaProperty) {
                 if ($property->getParameter('instanceof') == null) {
                     throw new DataObject\Exception("Parameter 'instanceof' for '{$key}' in class should contain a class name");
                 }
                 // Specific case of MediaObject()
                 if ($property->getParameter('instanceof') == 't41\\ObjectModel\\MediaObject') {
                     // new file case : value is a string prepended by  'tmp:'
                     if ($value && substr($value, 0, strlen(MediaObject::TMP_PREFIX)) == MediaObject::TMP_PREFIX) {
                         $parts = explode('|', substr($value, 4));
                         // 0 => hash, 1 => original file name
                         $file = '/tmp/' . $parts[0];
                         if (file_exists($file)) {
                             $media = new MediaObject();
                             $media->setUri(md5(rand() * microtime()));
                             $media->setLabel($parts[1]);
                             $finfo = finfo_open(FILEINFO_MIME_TYPE);
                             $mime = finfo_file($finfo, $file);
                             $media->setMedia($file);
                             // blob property
                             $media->setSize(filesize($file));
                             $media->setMime($mime);
                             $media->save();
                             $property->setValue($media);
                             // media property
                             unlink($file);
                         }
                         continue;
                         // don't go further in this case
                     }
                 }
                 if ($value && $value != Property::EMPTY_VALUE) {
                     if (is_object($value) && get_class($value) == $property->getParameter('instanceof')) {
                         $property->setValue($value);
                     } else {
                         if (substr($value, 0, 4) == 'obj_') {
                             // get object from cache
                             $property->setValue(Core::cacheGet($value));
                         } else {
                             if (substr($value, 0, 1) == Backend::PREFIX) {
                                 /* get & call object's backend to get a full configured object uri */
                                 $backend = ObjectModel::getObjectBackend($property->getParameter('instanceof'));
                                 $value = $backend->buildObjectUri($value, $property->getParameter('instanceof'));
                                 $property->setValue($value instanceof ObjectUri ? $value : new ObjectUri($value));
                             } else {
                                 //$class = $property->getParameter('instanceof');
                                 $backend = ObjectModel::getObjectBackend($property->getParameter('instanceof'));
                                 $value = $backend->buildObjectUri($value, $property->getParameter('instanceof'));
                                 $property->setValue($value);
                             }
                         }
                     }
                 } else {
                     $property->resetValue();
                 }
             } else {
                 if ($property instanceof Property\CollectionProperty) {
                     // @todo handle collection populating here
                 } else {
                     // if value is an array, it is most likely a set of multiple options
                     if (is_array($value)) {
                         $value = implode('|', $value);
                     }
                     $property->setValue($value);
                 }
             }
         }
     }
     return $this;
 }
Example #9
0
 public function getInstanceOf()
 {
     return ObjectModel::factory($this->getParameter('instanceof'));
 }
Example #10
0
 public static function returnsDistinct(ObjectModel\Collection $co, Property\PropertyAbstract $property, Backend\Adapter\AbstractAdapter $backend)
 {
     if (is_null($backend)) {
         $backend = ObjectModel::getObjectBackend($co->getDataObject()->getClass());
         if (is_null($backend)) {
             // get default backend
             $backend = self::getDefaultBackend();
         }
     }
     if (!$backend) {
         throw new Backend\Exception("NO_AVAILABLE_BACKEND");
     }
     return $backend->returnsDistinct($co, $property);
 }
Example #11
0
 public function setRules(array $rules = array())
 {
     if (count($rules) == 0) {
         $rules = (array) \t41\ObjectModel::getRules(get_class($this));
     }
     $doRules = array();
     foreach ($rules as $key => $rule) {
         /* if $rule is an array, targeted property contains an object and we need to delegate logic to data object */
         if (is_array($rule)) {
             $doRules[] = $rule;
             unset($rules[$key]);
         }
     }
     if (count($doRules) > 0) {
         $this->_dataObject->delegateRules($doRules);
     }
     $this->_rules = $rules;
     return $this;
 }
Example #12
0
 protected function _contentRendering()
 {
     $i = 0;
     $p = '';
     $aliases = $this->_obj->getAliases();
     // print out rows
     foreach ($this->_obj->getCollection()->getMembers(ObjectModel::DATA) as $this->_key => $this->_do) {
         $css = $i % 2 == 0 ? 'odd' : 'even';
         //	\Zend_Debug::dump($this->_do->getProperty('marche')->getValue(ObjectModel::MODEL)); die;
         // @todo handle objects coming from different backends
         $p .= sprintf('<tr data-member="%s" data-alias="%s" class="%s">', $this->_key, $this->_do->getUri() ? $aliases[$this->_do->getUri()->getIdentifier()] : null, $css);
         $i++;
         if ($this->_obj->getParameter('selectable') === true) {
             // make list items selectable
             $p .= sprintf('<td><input type="checkbox" name="t41_selection[]" value="%s"/></td>', $this->_do->getUri()->getIdentifier());
         }
         $altDec = (array) $this->_obj->getParameter('decorators');
         foreach ($this->_obj->getColumns() as $column) {
             if ($column instanceof Element\IdentifierElement) {
                 $p .= sprintf('<td>%s</td>', $this->_do->getUri()->getIdentifier());
                 continue;
             }
             if ($column instanceof Element\MetaElement) {
                 $attrib = $column->getParameter('type') == 'currency' ? ' class="cellcurrency"' : null;
                 $p .= "<td{$attrib}>" . $column->getDisplayValue($this->_do) . '</td>';
                 continue;
             }
             $property = $this->_do->getProperty($column->getParameter('property'));
             if (!$property) {
                 $p .= '<td>??</td>';
                 continue;
             }
             $column->setValue($property->getValue());
             /* if a decorator has been declared for property/element, use it */
             if (isset($altDec[$column->getId()])) {
                 $column->setDecorator($altDec[$column->getId()]);
                 $deco = Decorator::factory($column);
                 $p .= sprintf('<td>%s</td>', $deco->render());
                 continue;
             }
             $attrib = sprintf(' class="tb-%s', $column->getId());
             $attrib .= $property instanceof Property\CurrencyProperty ? ' cellcurrency"' : '"';
             if ($column->getParameter('recursion')) {
                 $parts = $column->getParameter('recursion');
                 foreach ($parts as $rkey => $recursion) {
                     if ($property instanceof ArrayProperty) {
                         // property won't be a property here !
                         $property = $property->getValue();
                         $property = $property[$recursion];
                         if ($property instanceof BaseObject && isset($parts[$rkey + 1])) {
                             $property = $property->{$parts[$rkey + 1]};
                         }
                         break;
                     }
                     // property is an object property
                     if ($property instanceof AbstractProperty && $property->getValue()) {
                         $property = $property->getValue(ObjectModel::DATA)->getProperty($recursion);
                     }
                     if ($property instanceof ObjectModel || $property instanceof DataObject) {
                         $property = $property->getProperty($recursion);
                     } else {
                         if ($property instanceof ObjectUri) {
                             $property = ObjectModel::factory($property)->getProperty($recursion);
                         }
                     }
                 }
             }
             if ($property instanceof Property\MediaProperty) {
                 $column->setValue($property->getDisplayValue());
                 $deco = Decorator::factory($column);
                 $value = $deco->render();
             } else {
                 $value = $property instanceof Property\AbstractProperty ? $property->getDisplayValue() : $property;
             }
             //$p .= "<td$attrib>" . $this->_escape($value) . '</td>';
             $p .= "<td{$attrib}>" . $value . '</td>';
         }
         $p .= '<td class="tb-actions">';
         foreach ($this->_obj->getEvents('row') as $button) {
             $button->setParameter('uri', $this->_do->getUri());
             $p .= $this->_renderButton($button, $aliases[$this->_do->getUri()->getIdentifier()]);
         }
         $p .= '</td></tr>' . "\n";
     }
     return $p;
 }
Example #13
-1
 /**
  * Perform the correct join from given object property and return the table alias
  * @todo test and implement in various places in find() method
  * @param Property\ObjectProperty $property
  * @param string $table
  * @return string
  */
 protected function _join(Property\ObjectProperty $property, $table)
 {
     $join = array();
     $class = $property->getParameter('instanceof');
     $stable = $this->_getTableFromClass($class);
     $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($property->getParent()->getDataObject()->getClass(), $property->getId()) : $property->getId();
     $uniqext = $stable . '__joined_for__' . $leftkey;
     if (!isset($this->_alreadyJoined[$stable])) {
         $sbackend = ObjectModel::getObjectBackend($class);
         if ($sbackend->getAlias() != $this->getAlias()) {
             // @todo raise and exception if backends are not of same type
             // 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 = $property->getId();
         $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : Backend::DEFAULT_PKEY;
         if (is_array($rightkey)) {
             foreach ($rightkey as $rightkeyObj) {
                 // @todo fix left key that should be provided by mapper
                 $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}", implode(' AND ', $join), array());
         $this->_alreadyJoined[$stable] = $uniqext;
     }
     return $uniqext;
 }