Example #1
0
 /**
  * Store the member
  *
  * @return boolean
  */
 public function store()
 {
     global $zdb, $hist;
     try {
         $values = array();
         $fields = self::getDbFields();
         /** FIXME: quote? */
         foreach ($fields as $field) {
             if ($field !== 'date_modif_adh' || !isset($this->_id) || $this->_id == '') {
                 $prop = '_' . $this->_fields[$field]['propname'];
                 if (($field === 'bool_admin_adh' || $field === 'bool_exempt_adh' || $field === 'bool_display_info') && $this->{$prop} === false) {
                     //Handle booleans for postgres ; bugs #18899 and #19354
                     $values[$field] = 'false';
                 } elseif ($field === 'parent_id') {
                     //handle parents
                     if ($this->_parent === null) {
                         $values['parent_id'] = new Expression('NULL');
                     } elseif ($this->parent instanceof Adherent) {
                         $values['parent_id'] = $this->_parent->id;
                     } else {
                         $values['parent_id'] = $this->_parent;
                     }
                 } else {
                     $values[$field] = $this->{$prop};
                 }
             }
         }
         //an empty value will cause date to be set to 1901-01-01, a null
         //will result in 0000-00-00. We want a database NULL value here.
         if (!$this->_birthdate) {
             $values['ddn_adh'] = new Expression('NULL');
         }
         if (!$this->_due_date) {
             $values['date_echeance'] = new Expression('NULL');
         }
         if ($this->_title instanceof Title) {
             $values['titre_adh'] = $this->_title->id;
         } else {
             $values['titre_adh'] = new Expression('NULL');
         }
         if (!$this->_parent) {
             $values['parent_id'] = new Expression('NULL');
         }
         if (!isset($this->_id) || $this->_id == '') {
             //we're inserting a new member
             unset($values[self::PK]);
             //set modification date
             $this->_modification_date = date('Y-m-d');
             $values['date_modif_adh'] = $this->_modification_date;
             $insert = $zdb->insert(self::TABLE);
             $insert->values($values);
             $add = $zdb->execute($insert);
             if ($add->count() > 0) {
                 if ($zdb->isPostgres()) {
                     $this->_id = $zdb->driver->getLastGeneratedValue(PREFIX_DB . 'adherents_id_seq');
                 } else {
                     $this->_id = $zdb->driver->getLastGeneratedValue();
                 }
                 $this->_picture = new Picture($this->_id);
                 // logging
                 $hist->add(_T("Member card added"), strtoupper($this->_login));
                 return true;
             } else {
                 $hist->add(_T("Fail to add new member."));
                 throw new \Exception('An error occured inserting new member!');
             }
         } else {
             //we're editing an existing member
             if (!$this->isDueFree()) {
                 // deadline
                 $due_date = Contribution::getDueDate($this->_id);
                 if ($due_date) {
                     $values['date_echeance'] = $due_date;
                 }
             }
             if (!$this->_password) {
                 unset($values['mdp_adh']);
             }
             $update = $zdb->update(self::TABLE);
             $update->set($values);
             $update->where(self::PK . '=' . $this->_id);
             $edit = $zdb->execute($update);
             //edit == 0 does not mean there were an error, but that there
             //were nothing to change
             if ($edit->count() > 0) {
                 $this->_updateModificationDate();
                 $hist->add(_T("Member card updated"), strtoupper($this->_login));
             }
             return true;
         }
         //DEBUG
         return false;
     } catch (\Exception $e) {
         Analog::log('Something went wrong :\'( | ' . $e->getMessage() . "\n" . $e->getTraceAsString(), Analog::ERROR);
         return false;
     }
 }