public function autocompleteAction() { $status = 'OK'; $uuid = $this->_getParam('uuid'); $data = $error = array(); if ($uuid) { $obj = Core\Registry::get($uuid); $obj->setParameter('query', $this->_getParam('q')); $obj->setParameter('batch', (int) $this->_getParam('batch')); try { $result = $obj->execute(); if ($result === false) { $error = Backend::getLastQuery(); $status = 'NOK'; } else { $data = $result; } } catch (\Exception $e) { /* @todo normally no exception is thrown, we should get a on/off flag */ $error = $e->getMessage(); //Backend::getLastQuery(); $status = 'ERR'; } } else { $status = 'NOK'; $error = 'Missing remote object id'; } echo $this->_setResponse($status, $data, $error); exit; }
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(); } }
/** * Execute the action and returns a result * * @return array */ public function execute($data = array()) { $res = false; switch ($this->_action) { case 'create': $res = $this->_obj->save(); break; case 'delete': $res = $this->_obj->delete(); break; case 'update': // test object uri, if empty, object is new or faulty // walk through POST data foreach ($data as $key => $val) { if (($property = $this->_obj->getProperty($key)) !== false) { if ($property instanceof Property\ObjectProperty) { $val = Core\Registry::get($val); if (is_object($val)) { $property->setValue($val); } } 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 members foreach ($val as $memberKey => $memberArray) { $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); } } $property->getValue()->addMember($member); } } else { $property->setValue($val); } } } } $res = $this->_obj->save(); break; } return $res; }
public function updateAction() { try { // populate status message if provided if (isset($this->_post['_status'])) { $this->_obj->status = new Status($this->_post['_status']); } // 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, ObjectModel::MODEL); 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 { $collections = isset($this->_post['_collections']) ? $this->_post['_collections'] : 1; $extprops = isset($this->_post['_extprops']) ? $this->_post['_extprops'] : []; $this->_data['object'] = $this->_obj->reduce(array('params' => array(), 'extprops' => $extprops, 'collections' => $collections)); $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'; } }