예제 #1
0
 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;
 }
예제 #2
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);
 }
예제 #3
0
 /**
  * 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;
 }
예제 #4
0
 public function reduce(array $params = array())
 {
     /* keep object in registry */
     $this->setContextData('uuid', Core\Registry::set($this->_obj));
     $fullAction = $this->_id;
     if ($this->_action) {
         $fullAction .= '/' . $this->_action;
     }
     $array = array('event' => 'click', 'action' => $fullAction, 'data' => $this->getContext());
     if ($this->_callbacks) {
         $array['callbacks'] = $this->_callbacks;
     }
     // add or replace data with optional $params['data'] content
     if (isset($params['extra'])) {
         foreach ((array) $params['extra'] as $key => $val) {
             $array[$key] = $val;
         }
     }
     // return reduced action without parameters
     return $array;
 }
예제 #5
0
 public function reduce(array $params = array(), $cache = true)
 {
     $uuid = Registry::set($this, null, true);
     return array_merge(parent::reduce($params), array('uuid' => $uuid, 'obj' => $this->_collection->reduce($params, $cache)));
 }
예제 #6
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';
     }
 }
예제 #7
0
 /**
  * (non-PHPdoc)
  * @see t41\ObjectModel.ObjectModelAbstract::reduce()
  */
 public function reduce(array $params = array(), $cache = true)
 {
     /* keep object in registry (force refresh) */
     $uuid = $cache ? Core\Registry::set($this, null, true) : null;
     // build an array with remotely callable methods
     $methods = array();
     //		foreach (get_class_methods($this) as $method) {
     //			if (substr($method,0,1) == '_') continue;
     //$methods[] = $method;
     //		}
     $array = $uuid ? array('uuid' => $uuid) : array();
     $array['value'] = $this->__toString();
     return array_merge($this->_dataObject->reduce($params, false), $array);
 }
예제 #8
0
 /**
  * (non-PHPdoc)
  * @see t41\Core.ClientSideInterface::reduce()
  */
 public function reduce(array $params = array())
 {
     return Core\Registry::set($this);
     //return $this->asString();
 }
예제 #9
0
 /**
  * (non-PHPdoc)
  * @see t41\ObjectModel.ObjectModelAbstract::reduce()
  */
 public function reduce(array $params = array(), $cache = true)
 {
     //$uuid = ($this->_uri instanceof ObjectUri) ? $this->_uri->reduce($params) : null;
     $uuid = $cache ? Registry::set($this) : null;
     $props = array();
     foreach ($this->_data as $key => $property) {
         if (isset($params['props']) && !in_array($key, $params['props'])) {
             continue;
         }
         $constraints = $property->getParameter('constraints');
         // ignore stricly server-side properties
         if (isset($constraints['serverside'])) {
             continue;
         }
         $props[$key] = $property->reduce($params, $cache);
     }
     return array_merge(parent::reduce($params), array('uuid' => $uuid, 'props' => $props));
 }
예제 #10
0
 public function reduce(array $params = array(), $cache = true)
 {
     if (!$this->_value) {
         return parent::reduce($params, $cache);
     } else {
         // @todo improve performances !!
         $uuid = Core\Registry::set($this->getValue(ObjectModel::DATA));
         if (isset($params['extprops']) && ($params['extprops'] === true || array_key_exists($this->_id, $params['extprops']))) {
             $value = $this->getValue(ObjectModel::DATA)->reduce(array('props' => $params['extprops'][$this->_id], 'extprops' => $params['extprops']), $cache);
         } else {
             $value = $this->getDisplayValue();
         }
         return array_merge(parent::reduce($params, $cache), array('value' => $value, 'uuid' => $uuid));
     }
 }