Ejemplo n.º 1
0
 /**
  * The callback used by the form class. This callback is only called when
  * the add or edit controller actions are performed. 
  * 
  * @param array $data The data from the form
  * @param Form $form an instance of the form
  * @param mixed $c Specific data from the form, this normally includes an instance of the controller
  * @param boolean $redirect If true the controller redirects the page after execution
  * @see ModelController::$callbackFunction
  * @return boolean
  */
 public static function callback($data, &$form, $c, $redirect = true, &$id = null)
 {
     switch ($c["action"]) {
         case "add":
             $return = $c["instance"]->model->setData($data);
             if ($return === true) {
                 $id = $c["instance"]->model->save();
                 User::log($c["success_message"], $data);
                 if ($redirect) {
                     Application::redirect($c["instance"]->urlPath . "?notification=" . urlencode($c["success_message"]));
                 } else {
                     return true;
                 }
             } else {
                 $fields = array_keys($return["errors"]);
                 foreach ($fields as $field) {
                     foreach ($return["errors"][$field] as $error) {
                         $element = $c["form"]->getElementByName($field);
                         $element->addError(str_replace("%field_name%", $element->getLabel(), $error));
                     }
                 }
                 foreach ($return['errors'] as $fieldName => $error) {
                     $form->addError(str_replace("%field_name%", str_replace('_', ' ', $fieldName), $error));
                 }
             }
             break;
         case "edit":
             $return = $c["instance"]->model->setData($data, $c["key_field"], $c["key_value"]);
             if ($return === true) {
                 $c["instance"]->model->update($c["key_field"], $c["key_value"]);
                 User::log($c["success_message"], $data);
                 if ($redirect) {
                     Application::redirect($c["instance"]->urlPath . "?notification=" . urlencode($c["success_message"]));
                 } else {
                     return true;
                 }
             } else {
                 $fields = array_keys($return["errors"]);
                 self::$pendingErrors = $return['errors'];
                 foreach ($fields as $field) {
                     foreach ($return["errors"][$field] as $error) {
                         $element = $c["form"]->getElementByName($field);
                         $element->addError(str_replace("%field_name%", $element->getLabel(), $error));
                     }
                 }
                 foreach ($return['errors'] as $fieldName => $error) {
                     $form->addError(str_replace("%field_name%", str_replace('_', ' ', $fieldName), $error));
                 }
             }
             break;
     }
 }