Ejemplo n.º 1
0
 /**
  * Méthode callback qui gère la soumission du formulaire add/edit.
  * Elle est appelée via callback par la méthode process() de HTML_QuickForm
  *
  * @access public
  * @param array $values les données du formulaire.
  * @return void
  */
 public function handlePostData($values)
 {
     if (!$this->innerForm) {
         Database::connection()->startTrans();
     }
     $this->onBeforeHandlePostData($values);
     if ($this->object->getId() == 0) {
         $this->object->generateId();
         $this->objID = $this->object->getId();
     }
     // gestion des propriétés et des fkeys
     foreach ($this->attrs as $property => $class) {
         $setter = 'set' . $property;
         $getter = 'get' . $property;
         if (!method_exists($this->object, $setter)) {
             continue;
         }
         $eltname = $this->clsname . '_' . $property;
         if (!is_string($class) && isset($values[$eltname])) {
             // propriétés simples
             $data = $values[$eltname];
         } else {
             if (isset($values[$eltname . '_ID'])) {
                 // on a une fkey
                 $data = $values[$eltname . '_ID'];
                 $getter .= 'Id';
             } else {
                 if (isset($_FILES[$eltname])) {
                     $data = $_FILES[$eltname];
                 }
             }
         }
         if (isset($data)) {
             $custom = 'handle' . $property;
             if (method_exists($this, $custom)) {
                 call_user_func(array($this, $custom), $data);
                 continue;
             }
             if ($class == Object::TYPE_PASSWORD) {
                 if ($data != $values[$eltname . '_Again']) {
                     Template::errorDialog(E_PASSWD_MISMATCH, $this->url);
                     exit(1);
                 }
                 if ($data != self::PW_NOCHG) {
                     $this->object->{$setter}($data);
                 }
             } elseif ($class == Object::TYPE_FILE_UPLOAD) {
                 if (isset($_FILES[$eltname]['name']) && !empty($_FILES[$eltname]['name'])) {
                     try {
                         $uploader = new Upload($eltname);
                         if (UPLOAD_STORAGE == 'db') {
                             // stock l'image ou le fichier en bd
                             $uploader->dbstore();
                             $this->object->{$setter}($_FILES[$eltname]['name']);
                         } else {
                             // stock l'image ou le fichier en dur et écrit le chemin en bd
                             $uploader->store(UPLOAD_STORAGE);
                             $this->object->{$setter}($data);
                         }
                     } catch (Exception $exc) {
                         Template::errorDialog($exc->getMessage(), $this->guessReturnURL());
                         exit;
                     }
                 }
             } else {
                 if ($class == Object::TYPE_IMAGE) {
                     if (isset($_FILES[$eltname]['name']) && !empty($_FILES[$eltname]['name'])) {
                         try {
                             $manager = new ImageManager($eltname);
                             $manager->dbstore();
                             $this->object->{$setter}($_FILES[$eltname]['name']);
                         } catch (Exception $exc) {
                             Template::errorDialog($exc->getMessage(), $this->guessReturnURL());
                             exit;
                         }
                     }
                 } elseif ($class == Object::TYPE_DATETIME) {
                     $this->object->{$setter}($data . ':00');
                     /*$datetime = explode(' ', $data);
                       if (count($datetime) == 2) {
                           list($date, $time) = $datetime;
                           $date =  explode('/', $date);
                           if (count($date) == 3) {
                               list($d, $m, $y) = $date;
                               $data = sprintf('%04d-%02d-%02d %s:00', $y, $m, $d, $time);
                               $this->object->$setter($data);
                           }
                       }*/
                 } elseif (in_array($class, array(Object::TYPE_FLOAT, Object::TYPE_DECIMAL))) {
                     $this->object->{$setter}(I18N::extractNumber($data));
                 } elseif (is_string($class)) {
                     if ($this->innerForm && $this->innerForm->clsname == $class) {
                         $data = $this->innerForm->object->getId();
                     }
                     $this->object->{$setter}($data);
                 } else {
                     $this->object->{$setter}($data);
                 }
             }
             unset($data);
         }
     }
     // gestion des relations *..*
     foreach ($this->links as $property => $detail) {
         $setter = 'set' . $property . 'CollectionIds';
         if (!method_exists($this->object, $setter)) {
             continue;
         }
         $eltname = $this->clsname . '_' . $property . '_IDs';
         // XXX FIXME hack pour les advmultiselect, arranger ça
         if (!isset($values[$eltname])) {
             $eltname = 'advmultiselect' . $eltname;
             if (!isset($values[$eltname])) {
                 $this->object->{$setter}(array());
             }
         }
         // end hack
         if (isset($values[$eltname])) {
             $data = $values[$eltname];
             if (!is_array($data)) {
                 $data = array($data);
             }
             $custom = 'handle' . $property . 'Collection';
             if (method_exists($this, $custom)) {
                 call_user_func(array($this, $custom), $data);
                 continue;
             }
             $this->object->{$setter}($data);
         }
     }
     $this->onAfterHandlePostData();
     // pour l'action add sur les selects fkey
     if (isset($_REQUEST['fromEntity'])) {
         list($entity, $property) = explode(':', $_REQUEST['fromEntity']);
         if (isset($_SESSION['_' . $entity . '_'])) {
             $setter = 'set' . $property;
             $_SESSION['_' . $entity . '_']->{$setter}($this->object->getId());
         }
     }
     if (!$this->parentForm) {
         if ($values['fromAddButton'] == '1' && !empty($values['redirectURL'])) {
             // un bouton add/edit de fkey a été clické
             Tools::redirectTo($values['redirectURL']);
             exit(0);
         }
     }
     $this->save();
     if (!$this->parentForm) {
         if (Database::connection()->hasFailedTrans()) {
             $err = Database::connection()->errorMsg();
             trigger_error($err, E_USER_WARNING);
             Database::connection()->rollbackTrans();
             Template::errorDialog(E_ERROR_SQL . '.<br />' . $err, $this->guessReturnURL());
             exit;
         }
         Database::connection()->completeTrans();
         $this->form->setDefaults($values);
         $this->onFinish();
     }
 }