/** * Map any given action string to the execute() method of the action's object */ public function saveAction() { if (!$this->_obj instanceof FormComponent) { $this->_status = 'NOK'; $this->_context['message'] = "Server-side object is not an action"; return; } try { // if record has no uri yet and an identifier value is present, inject it so backend will use it as primary key if (!$this->_obj->getSource()->getUri() && isset($this->_post[ObjectUri::IDENTIFIER])) { $this->_obj->getSource()->setUri($this->_post[ObjectUri::IDENTIFIER]); } // save form $result = $this->_obj->save($this->_post); if ($result === false) { $this->context['debug'] = Backend::getLastQuery(); $this->_status = 'NOK'; } else { $this->_data = $this->_obj->getSource()->reduce(); } } catch (\Exception $e) { /* @todo normally no exception is thrown, we should get a on/off flag */ $this->_context['err'] = $e->getMessage(); $this->_status = 'ERR'; } }
/** * Map any given action string to the execute() method of the action's object */ public function __call($methodName, $args) { if (!$this->_obj instanceof Action\AbstractAction) { $this->_status = 'NOK'; $this->_context['message'] = "Server-side object is not an action"; return; } try { $array = array_merge($this->_post, array('action' => substr($methodName, 0, strlen($methodName) - 6))); $result = $this->_obj->execute($array); if ($result === false) { // if result is false, try to get more information from object status or action status $status = $this->_obj->getObject() ? $this->_obj->getObject()->status : $this->_obj->status; if ($status instanceof Core\Status) { $this->_context['message'] = $status->getMessage(); $this->_context['context'] = $status->getContext(); } $this->context['debug'] = Backend::getLastQuery(); $this->_status = 'NOK'; } else { if (is_array($result)) { $this->_data = $result; } } } catch (\Exception $e) { /* @todo normally no exception is thrown, we should get a on/off flag */ $this->_context['err'] = $e->getMessage(); if (Core::$env == Core::ENV_DEV) { $this->_context['trace'] = $e->getTraceAsString(); } $this->_status = 'ERR'; } }
public function postDispatch() { if ($this->_uuid && $this->_refreshCache === true) { Registry::set($this->_obj, $this->_uuid, true); } // reinject some data foreach ($this->_post as $key => $val) { if (substr($key, 0, 1) == '_') { $this->_data[$key] = $val; } } // if a redirect is available for the status, add it to the data if (isset($this->_redirects[strtolower($this->_status)])) { // declare object in tag parsing class to use it for tag substitution on redirect urls if ($this->_obj instanceof ObjectModel\BaseObject || $this->_obj instanceof ObjectModel\DataObject) { Core\Tag\ObjectTag::$object = $this->_obj; } else { if ($this->_obj instanceof Action\AbstractAction) { Core\Tag\ObjectTag::$object = $this->_obj->getObject(); } } $this->_context['redirect'] = Core\Tag::parse($this->_redirects[strtolower($this->_status)]); } if (isset($this->_post['_debug'])) { $this->_context['debug'] = Backend::getLastQuery(); } $this->_setResponse($this->_status, $this->_data, $this->_context); }
public function crudAction() { $request = $this->getRequest(); $response = $this->getResponse(); // Zend_Debug::dump($request->getPost()); $method = $this->_getParam('method'); $class = $this->_getParam('model'); $data = $request->getPost(); try { $action = new Action\CrudAction(); $action->setClass($class)->setCallback($method)->setContext($data); Zend_Debug::dump($action->execute()); Zend_Debug::dump(\t41\Backend::getLastQuery()); die; //$response->setHttpResponseCode($action->execute() ? 200 : 403); } catch (\Exception $e) { echo $e->getMessage() . $e->getTraceAsString(); //$response->setHttpResponseCode(500); //$response->setException($e); } }
protected function _setLastQuery($literal, $data = null, array $context = array()) { $context['db'] = $this->_database; if (!isset($context['table'])) { $context['table'] = $this->_table; } $context[Backend::PREFIX] = $this->_uri->getAlias(); Backend::setLastQuery($literal, $data, $context); }
/** * Returns the value of the given property * */ public function getAction() { try { $res = $this->_obj->getProperty($this->_post['property']); $res->getValue(); // if the save flag is defined, force object saving if (isset($this->_post['save'])) { Backend::save($res->getParent()); $this->_refreshCache = true; } $this->_data = $res->reduce(array('params' => array())); } catch (\Exception $e) { $this->_context['message'] = $e->getMessage(); $this->_status = 'ERR'; } if ($this->_obj->status instanceof Core\Status) { $this->_context['message'] = $this->_obj->status->getMessage(); $this->_context['context'] = $this->_obj->status->getContext(); } $this->_status = $res ? 'OK' : 'NOK'; }
/** * Delete object in backend * Object Uri is resetted. Object can then be saved in another backend * @param Backend\Adapter\AbstractAdapter $backend * @return boolean */ public function delete(Backend\Adapter\AbstractAdapter $backend = null) { $res = Backend::delete($this->_dataObject, $backend); if ($res === true) { $this->_dataObject->resetUri(); } else { $this->status = new Core\Status('error', null, Backend::getLastQuery()); } return $res; }
/** * 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; } }
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 ! $uri = new ObjectModel\ObjectUri($key); $uri->setClass($this->getCollection()->getClass()); $_do = clone $this->_collection->getDataObject(); $_do->setUri($uri); Backend::read($_do); $this->_displayProps = explode(',', $this->getParameter('display')); $str = array(); foreach ($this->_displayProps as $disProp) { $property = $_do->getProperty($disProp); if (!$property instanceof Property\PropertyAbstract) { continue; } $str[] = $property->getValue(); } $str = implode(' ', $str); $this->_enumValues[$key] = $str; return $str; }
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')); }
public function setClass($class) { $this->_class = $class; $this->_url = Backend::getInstance($this->_backendUri)->getTableFromClass($class) . '/' . $this->_identifier; return $this; }
public function updateAction() { try { // test object uri, if empty, object is new or faulty // @todo mix this with ObjectModel::populate() // walk through POST data foreach ($this->_post as $key => $val) { if (($property = $this->_obj->getProperty($key)) !== false) { if ($property instanceof Property\ObjectProperty) { if ($val) { $class = $property->getParameter('instanceof'); if (substr($val, 0, 4) == 'obj_') { // get object from cache $property->setValue(Core::cacheGet($val)); } else { $property->setValue(new $class($val)); } } else { $property->resetValue(); } } else { if ($property instanceof Property\CollectionProperty) { $class = $property->getParameter('instanceof'); $keyprop = $property->getParameter('keyprop'); // val for a collection should come as an array of new/existing members foreach ($val as $memberKey => $memberArray) { if (!is_numeric($memberKey)) { $this->_status = "NOK"; $this->_context['message'] = 'member-id is not a number'; return false; } // action exists to update or remove member if (isset($memberArray['action'])) { // get target member $object = $property->getValue()->getMember($memberKey); switch ($memberArray['action']) { case 'delete': if ($property->setValue($object, Collection::MEMBER_REMOVE) !== true) { $this->_status = "NOK"; $this->_context['message'] = 'error removing member from collection'; return false; } break; case 'update': foreach ($memberArray['props'] as $mApropN => $mApropV) { if (($mAprop = $object->getProperty($mApropN)) !== false) { $mAprop->setValue($mApropV); } } // direct update of the member $object->save(); break; } } else { // no action, default is new member to add $member = new $class(); // set keyprop property value $member->getProperty($member->getProperty($keyprop)->setValue($this->_obj)); // walk through foreach ($memberArray as $memberPropKey => $memberPropVal) { $mprop = $member->getProperty($memberPropKey); if ($mprop instanceof Property\ObjectProperty) { $memberPropVal = Core\Registry::get($memberPropVal); if ($memberPropVal instanceof Property\AbstractProperty) { $mprop->setValue($memberPropVal->getValue()); } else { $mprop->setValue($memberPropVal); } } else { $mprop->setValue($memberPropVal); } } // check if member was added successfully, break otherwise if ($property->setValue($member) === false) { $this->_status = "NOK"; $this->_context['message'] = 'error adding member to collection'; break; } } } } else { $property->setValue($val); } } } } // if record has no uri yet and an identifier value is present, inject it so backend will use it as primary key if (!$this->_obj->getUri() && isset($this->_post[ObjectUri::IDENTIFIER])) { $this->_obj->setUri($this->_post[ObjectUri::IDENTIFIER]); } $result = $this->_obj->save(); if ($result === false) { $this->_context['message'] = Backend::getLastQuery(); $this->_status = 'NOK'; } else { $this->_data['object'] = $this->_obj->reduce(array('params' => array(), 'extprops' => true, 'collections' => 1)); $this->_executeActions('ok'); $this->_refreshCache = true; } } catch (\Exception $e) { $this->_context['err'] = $e->getMessage(); if (Core::$env == Core::ENV_DEV) { $this->_context['trace'] = $e->getTraceAsString(); } $this->_status = 'ERR'; } }
public function populate(array $dataset, ObjectUri $uriBase) { if (count($dataset) == 0) { return array(); } $do = clone $this->getDataObject(); $class = $do->getClass(); // populate array with relevant objects type $array = array(); foreach ($dataset as $key => $data) { $uri = clone $uriBase; $uri->setUrl($uri->getUrl() . $data[Backend::DEFAULT_PKEY])->setIdentifier($data[Backend::DEFAULT_PKEY]); unset($data[Backend::DEFAULT_PKEY]); if (isset($do)) { $obj = clone $do; $obj->setUri($uri); } else { unset($obj); } switch ($this->getParameter('memberType')) { case ObjectModel::URI: $obj = $uri; break; case ObjectModel::MODEL: $obj = clone $do; $obj->setUri($uri); Backend::read($obj, null, $data); $obj = new $class($obj); break; case ObjectModel::DATA: default: $obj = clone $do; $obj->setUri($uri); Backend::read($obj, null, $data); break; } $array[] = $obj; } return $array; }
/** * 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; }
/** * Save object * * @return boolean */ public function save(Backend\Adapter\AdapterAbstract $backend = null) { return \t41\Backend::save($this->_dataObject, $backend); }
/** * Tests if a definition exists for given $id * Returns a t41\Backend\Adapter\AbstractAdapter instance if object definition includes a default backend value * * @param string $id * @return t41\Backend\Adapter\AbstractAdapter * @throws ObjectModel\Exception */ public static function getObjectBackend($id) { if (!self::definitionExists($id)) { throw new ObjectModel\Exception(array('NO_CLASS_DECLARATION', $id)); } if (isset(self::$_config[$id]['backend'])) { return Backend::getInstance(Backend::PREFIX . self::$_config[$id]['backend']); } else { return Backend::getDefaultBackend(); } }