Beispiel #1
0
 /**
  * This method composes the form and returns the output
  *
  * @return string Output of the form
  */
 public function execute()
 {
     // Fetch the model needed for this form. We will need it because authorization can depend on the model itself
     if ($this->id) {
         $factory = One_Repository::getFactory($this->scheme->getName());
         $model = $factory->selectOne($this->id);
     } else {
         $model = One::make($this->scheme->getName());
     }
     if (is_null($model)) {
         throw new One_Exception('Could not generate a form for scheme "' . $this->scheme->getName() . '" with id ' . $this->id);
     }
     $this->authorize($this->scheme->getName(), $model->id);
     $session = One_Repository::getSession();
     $formFile = $this->getVariable('formFile', 'form');
     $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', '');
     // Create a DOM and render the form in it
     $dom = One_Repository::createDom();
     $form->render($model, $dom);
     //    print_r($dom);
     $this->view->setModel($model);
     $this->view->set('scheme', $this->scheme);
     $this->view->set('form', $form);
     $this->view->set('dom', $dom);
     $this->view->set('errors', $session->get('errors', 'OneFormErrors'));
     $vForm = $this->view->show();
     $session->remove('errors', 'OneFormErrors');
     $session->remove('posted', 'OneFormErrors');
     return $vForm;
 }
Beispiel #2
0
 /**
  * Convert an array to an instance of the specified scheme
  *
  * @param One_Scheme $scheme
  * @param array $row
  * @return One_Model
  */
 private function &arrayToInstance(&$scheme, &$row)
 {
     // check the scheme cache
     $idAttribute = $scheme->getIdentityAttribute();
     $id = $row[$idAttribute->getName()];
     $cached = One_Model_IdentityMap::find($scheme->getName(), $id);
     if ($cached) {
         return $cached;
     }
     // not found : create a new instance
     // @TODO: use a specific class specified in the scheme
     $model = One::make($scheme->getName());
     $model->fromArray($row);
     // fire afterLoad event for model
     $model->afterLoad();
     One_Model_IdentityMap::add($model);
     return $model;
 }
Beispiel #3
0
 /**
  * This method renders the search form and if needed fetches and show the search results
  * @see plugins/system/one/lib/action/One_Controller_Action#execute()
  */
 public function execute()
 {
     $this->authorize($this->scheme, $model->id);
     $results = NULL;
     $model = One::make($this->scheme->getName());
     $cx = new One_Context();
     $session = One_Repository::getSession();
     if (!is_null($cx->get('doSearch'))) {
         $session->remove('usedSearchOptions', 'one_search');
         $results = $this->performSearch();
         $session->set('usedSearchOptions', $this->options, 'one_search');
     } else {
         if (!is_null($cx->get('doWeightSearch'))) {
             $session->remove('usedSearchOptions', 'one_search');
             $results = $this->performWeightSearch();
             $session->set('usedSearchOptions', $this->options, 'one_search');
         } else {
             if (isset($this->options['savedSearch'])) {
                 if ($session->varExists('usedSearchOptions', 'one_search')) {
                     $this->options = $session->get('usedSearchOptions', 'one_search');
                     if (isset($this->options['doSearch'])) {
                         $results = $this->performSearch();
                     } else {
                         if (isset($this->options['doWeightSearch'])) {
                             $results = $this->performWeightSearch();
                         }
                     }
                 }
             } else {
                 $session->remove('usedSearchOptions', 'one_search');
                 $session->remove('results', 'one_search');
             }
         }
     }
     $dom = One_Repository::createDom();
     $this->view->setModel($model);
     $this->view->set('scheme', $this->scheme);
     $this->view->set('formfile', $this->searchform);
     $this->view->set('results', $results);
     $vForm = $this->view->show();
     return $vForm;
 }
Beispiel #4
0
 /**
  * Bind the model to the widget
  *
  * @param One_Model $model
  */
 public function bindModel($model)
 {
     $value = $this->requestValue();
     // bad name
     preg_match_all('/([^:]+):([^,]+),?/', $value, $matches);
     $parts = explode(':', $this->getCfg('role'), 2);
     $targetAttr = $this->getCfg('targetAttribute');
     $tScheme = One_Repository::getScheme($model->getScheme()->getLink($parts[1])->getTarget());
     $tIdAttr = $tScheme->getIdentityAttribute()->getName();
     $value = array();
     if (0 < count($matches[0])) {
         foreach ($matches[0] as $key => $tmp) {
             $usedKey = $matches[1][$key];
             $usedValue = $matches[2][$key];
             if (null === One_Repository::selectOne($tScheme->getName(), $usedKey)) {
                 $tModel = One::make($tScheme->getName());
                 $tModel->{$targetAttr} = $usedValue;
                 $tModel->insert();
                 $usedKey = $tModel->{$tIdAttr};
             }
             $value[$usedKey] = $usedKey;
         }
     }
     if (is_null($value)) {
         $value = array();
     }
     // if == NULL, set to 0, because NULL won't be picked up by $model->__modified
     $attributeName = $this->_name;
     // When the attributeName starts with 'r__', we're not saving an attribute but a relation
     if (preg_match('/^r__(.*)_(((?!._).)*)$/iU', $attributeName, $tmp)) {
         $relName = $tmp[1];
         $relRole = $tmp[2];
         $relValue = $value;
         $model->setRelated($relRole, $relValue);
     } else {
         $model->{$attributeName} = $value;
     }
 }
Beispiel #5
0
 /**
  * Convert an array to an instance of the specified scheme
  *
  * @param One_Scheme $scheme
  * @param array $row
  * @return One_Model
  */
 protected function arrayToInstance(One_Scheme $scheme, $row)
 {
     // check the scheme cache
     $idAttribute = $scheme->getIdentityAttribute();
     $id = $row[$idAttribute->getName()];
     $cached = One_Model_IdentityMap::find($scheme->getName(), $id);
     if ($cached) {
         return $cached;
     }
     // not found : create a new instance
     //TODO: use a specific class specified in the scheme
     $model = One::make($scheme->getName());
     // PD17OCT08: for optimal performance, raw-store the data row entirely
     $model->fromArray($row);
     // fire afterLoad event for model
     $model->afterLoad();
     One_Model_IdentityMap::add($model);
     return $model;
 }
Beispiel #6
0
 /**
  * @param One_Scheme $scheme
  *
  * POST schemename
  * Create a new instance
  */
 public static function restPost($scheme)
 {
     try {
         // retrieve input data from body (a JSON encoded structure)
         $request = $this->slim->request();
         $body = $request->getBody();
         $input = json_decode($body, true);
         // instantiate and fill the model
         $model = One::make($scheme->getName());
         $model->fromArray($input);
         $model->insert();
         echo self::toPrettyJson($model->asRestResponse());
     } catch (Exception $e) {
         $this->slim->response()->status(400);
         $this->slim->response()->header('X-Status-Reason', $e->getMessage());
     }
 }
Beispiel #7
0
 /**
  * This method validates a submitted form and returns to the proper page according to whether the submission
  * contained errors or whether the form was saved or applied
  */
 public function execute()
 {
     $session = One_Repository::getSession();
     $isNew = false;
     if ($this->id) {
         // update existing
         $factory = One_Repository::getFactory($this->scheme->getName());
         $model = $factory->selectOne($this->id);
         if (is_null($model) && !$factory->getScheme()->getIdentityAttribute()->isAutoInc()) {
             $model = One::make($this->scheme->getName());
             $isNew = true;
         }
     } else {
         $model = One::make($this->scheme->getName());
     }
     $idAttrName = $model->getScheme()->getIdentityAttribute()->getName();
     $this->authorize($this->scheme->getName(), $model->{$idAttrName});
     $formFile = $this->getVariable('formFile', 'form');
     $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', '');
     $flow = One_Controller_Flow::getInstance($this->scheme)->getRedirects();
     $noErrors = $form->validate();
     if ($noErrors || is_array($noErrors) && count($noErrors) == 0) {
         $form->bindModel($model);
         if ($this->id && !$isNew) {
             $model->update();
             $id = $this->id;
         } else {
             $model->insert();
             $idAttr = $this->scheme->getIdentityAttribute()->getName();
             $id = $model->{$idAttr};
         }
         $this->model = $model;
         // handle redirects
         // @TODO this code can use some cleanup
         $redirects = array_merge($flow, $form->getRedirects());
         $todo = is_null($this->getVariable('action')) ? $this->getVariable('task') : $this->getVariable('action');
         if (isset($this->options['flow'])) {
             $todo = $this->options['flow'];
         }
         $redirect = $redirects['default'];
         if (isset($redirects[$todo])) {
             $redirect = $redirects[$todo];
         }
         if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') {
             $redirect['id'] = $model->{$idAttrName};
         }
         $redirect = $this->replaceOtherVariables($redirect);
         $this->controller->setRedirect($redirect);
     } else {
         $errors = base64_encode(serialize($form->getErrors()));
         $session->set('executedReturn', $model, 'executedForm');
         $session->set('errors', $form->getErrors(), 'OneFormErrors');
         $session->set('posted', $_REQUEST, 'OneFormErrors');
         $id = $this->id;
         $toView = 'edit';
         if (!is_null($this->getVariable('returnToOne'))) {
             parse_str(base64_decode($this->getVariable('returnToOne')), $returnVals);
             $this->controller->setRedirect($returnVals);
         } else {
             $redirects = array_merge($flow, $form->getRedirects());
             $todo = 'default';
             if (isset($this->options['flowerror'])) {
                 $todo = $this->options['flowerror'];
             } elseif (isset($redirects['formerror'])) {
                 $todo = 'formerror';
             }
             $redirect = $redirects[$todo];
             if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') {
                 $redirect['id'] = $model->{$idAttrName};
             }
             $redirect = $this->replaceOtherVariables($redirect);
             $this->controller->setRedirect($redirect);
         }
         return false;
     }
 }
Beispiel #8
0
 /**
  * Gets an instance of a scheme according to a path
  *
  * @param string $path
  * @return One_Model
  */
 public function get($path)
 {
     $parts = explode('.', trim(strtolower($path)));
     return One::make($parts[0]);
 }
Beispiel #9
0
 /**
  * Return an empty instance of this model
  *
  * @return One_Model
  */
 public function getInstance()
 {
     return One::make($this->scheme->getName());
 }