Exemple #1
0
 /**
  * This function performs the cancellation and returns to the proper page
  */
 public function execute()
 {
     if (!is_null($this->getVariable('cancelToOne'))) {
         parse_str(base64_decode($this->getVariable('cancelToOne')), $returnVals);
         $this->controller->setRedirect($returnVals);
     } else {
         $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();
         $redirects = array_merge($flow, $form->getRedirects());
         $todo = 'cancel';
         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'] = $this->id;
         }
         $redirect = $this->replaceOtherVariables($redirect);
         $this->controller->setRedirect($redirect);
     }
 }
Exemple #2
0
 /**
  * This method removes the chosen item and redirects to the proper page
  */
 public function execute()
 {
     $factory = One_Repository::getFactory($this->scheme->getName());
     foreach ($this->id as $id) {
         $this->authorize($this->scheme->getName(), $id);
         $model = $factory->selectOne($id);
         if (is_null($model)) {
             throw new One_Exception('Item could not be found');
         }
         $model->delete();
     }
     $redirects = One_Controller_Flow::getInstance($this->scheme)->getRedirects();
     $redirect = $redirects['default'];
     if (isset($redirects['remove'])) {
         $redirect = $redirects['remove'];
     }
     $redirect = $this->replaceOtherVariables($redirect);
     $this->getController()->setRedirect($redirect);
 }
Exemple #3
0
 /**
  * This method copies the current model into a new item
  */
 public function execute()
 {
     if (!$this->id) {
         throw new One_Exception('Can not copy a non existing item');
     } else {
         $factory = One_Repository::getFactory($this->scheme->getName());
         $model = $factory->selectOne($this->id);
         if (is_null($model)) {
             throw new One_Exception('Can not copy a non existing item');
         }
     }
     $idAttrName = $model->getScheme()->getIdentityAttribute()->getName();
     $this->authorize($this->scheme->getName(), $model->{$idAttrName});
     // Get an instance of the current model
     $copy = clone $model;
     // Not really necessary as when the ID-attribute equals NULL it will treat the model as new
     //		$attributes = $this->scheme->getAttributes();
     //		foreach($attributes as $attribute)
     //		{
     //			$attr = $attribute->getName();
     //			$copy->$attr = $model->$attr;
     //		}
     $copy->{$idAttrName} = NULL;
     $copy->insert();
     // create the copy
     $flow = One_Controller_Flow::getInstance($this->scheme)->getRedirects();
     $todo = is_null($this->getVariable('action')) ? $this->getVariable('task') : $this->getVariable('action');
     $redirect = $flow['default'];
     if (isset($flow['copy'])) {
         $redirect = $flow['copy'];
     }
     if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') {
         $redirect['id'] = $copy->{$idAttrName};
     }
     $redirect = $this->replaceOtherVariables($redirect);
     $this->controller->setRedirect($redirect);
 }
Exemple #4
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;
     }
 }