Exemple #1
0
 /**
  * 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';
     }
 }
Exemple #2
0
 /**
  * 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';
     }
 }
Exemple #3
0
 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);
 }
Exemple #4
0
 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);
     }
 }
Exemple #5
0
 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';
     }
 }
Exemple #6
0
 /**
  * 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;
 }
Exemple #7
0
 public function debug()
 {
     $this->find();
     $this->setParameter('populated', false);
     \Zend_Debug::dump(Backend::getLastQuery());
 }