Ejemplo n.º 1
0
 /**
  * Function for easy update a ORM object
  *
  * @param ORM $object ORM object to update
  * @param array $messages Array of custom messages
  */
 public function update(ORM $object, array $messages = array())
 {
     // Check if is a valid object
     if (!$object->loaded()) {
         Messages::warning(isset($messages['warning']) ? $messages['warning'] : 'El elemento que intentas modificar no existe o fue eliminado.');
         $this->go();
     }
     // Only if Request is POST
     if ($this->request->method() == Request::POST) {
         // Catch ORM_Validation
         try {
             // Set object values and update
             $object->values($this->request->post())->update();
             // If object is saved....
             if ($object->saved()) {
                 // Success message & redirect
                 Messages::success(isset($messages['success']) ? $messages['success'] : 'El elemento fue modificado correctamente.');
                 $this->go();
             }
         } catch (ORM_Validation_Exception $e) {
             // Error message
             if (isset($messages['error'])) {
                 Messages::error($messages['error']);
             }
             // Validation messages
             Messages::validation($e);
         }
     }
 }
Ejemplo n.º 2
0
 public function values(array $values, array $expected = NULL)
 {
     if (!empty($values['dob-year']) && !empty($values['dob-month']) && !empty($values['dob-day'])) {
         $values['dob'] = $values['dob-year'] . '-' . $values['dob-month'] . '-' . $values['dob-day'];
     }
     return parent::values($values, $expected);
 }
Ejemplo n.º 3
0
 public function values($post)
 {
     if ($this->_orm) {
         $this->_orm->values($post);
         return $this;
         // Chain the Entity, not the ORM object
     } else {
         throw new Missing_ORM_Exception("There is no ORM object linked to this Entity instance");
     }
 }
Ejemplo n.º 4
0
 /**
  * @return bool
  */
 public function populate()
 {
     if (!$this->allowed('write')) {
         return FALSE;
     }
     $hash = $this->_hash_input_name;
     // nothing or the other form has been submitted
     if (!isset($_POST[$hash])) {
         return FALSE;
     }
     $values = array();
     foreach ($this as $name => $field) {
         if ($field->allowed('write')) {
             if (!is_null($value = Arr::get($_POST, $name))) {
                 $values[$name] = $field->prepare_to_populate($value);
             }
         }
     }
     $this->_object->values($values);
     foreach ($this as $name => $field) {
         $field->set_value($this->_object->{$name});
     }
     $valid = $this->_object->check();
     foreach ($this as $name => $field) {
         $valid = ($valid and $field->validate($this->_object->validate(), $name));
     }
     if ($valid) {
         return TRUE;
     }
     $errors = $this->_object->validate()->errors($this->_messages_file);
     foreach ($errors as $name => $error) {
         if (isset($this[$name])) {
             $this[$name]->set_error($error);
         }
     }
     return FALSE;
 }
Ejemplo n.º 5
0
 /**
  * Set values from an array with support for one-one relationships.  This method should be used
  * for loading in post data, etc.
  *
  * @param  array $values   Array of column => val
  * @param  array $expected Array of keys to take from $values
  * @return ORM
  */
 public function values(array $values, array $expected = NULL)
 {
     //some work on the data ;)
     if (isset($values['title'])) {
         $values['title'] = Text::banned_words($values['title']);
     }
     if (isset($values['description'])) {
         $values['description'] = Text::banned_words($values['description']);
     }
     if (isset($values['price'])) {
         $values['price'] = floatval(str_replace(',', '.', $values['price']));
     }
     //TODO this is ugly as hell!
     // append to $values new custom values
     foreach ($values as $name => $field) {
         // get by prefix
         if (strpos($name, 'cf_') !== false) {
             //checkbox when selected return string 'on' as a value
             if ($field == 'on') {
                 $values[$name] = 1;
             }
             if ($field == '0000-00-00' or $field == "" or $field == NULL or empty($field)) {
                 $values[$name] = NULL;
             }
         }
     }
     return parent::values($values, $expected);
 }