/**
  * After the form section is initially created, call this to sanitize the data in the submission
  * which relates to this form section, validate it, and set it as properties on the form.
  * @param array $req_data should usually be $_REQUEST (the default). However, you CAN
  * supply a different array. Consider using set_defaults() instead however. (If you rendered
  * the form in the page using echo $form_x->get_html() the inputs will have the correct name
  * in the request data for this function to find them and populate the form with them.
  * If you have a flat form (with only input subsections), you can supply a flat array where keys
  * are the form input names and values are their values)
  * @param boolean $validate whether or not to perform validation on this data. Default is,
  * of course, to validate that data, and set errors on the invalid values. But if the data
  * has already been validated (eg you validated the data then stored it in the DB) you may want
  * to skip this step.
  * @return void
  */
 public function receive_form_submission($req_data = NULL, $validate = TRUE)
 {
     parent::receive_form_submission($req_data, $validate);
     //create or set the model object, if it isn't already
     if (!$this->_model_object) {
         //check to see if the form indicates a PK, in which case we want to only retrieve it and update it
         $pk_name = $this->_model->primary_key_name();
         $model_obj = $this->_model->get_one_by_ID($this->get_input_value($pk_name));
         if ($model_obj) {
             $this->_model_object = $model_obj;
         } else {
             $this->_model_object = EE_Registry::instance()->load_class($this->_model->get_this_model_name());
         }
     }
     //ok so the model object is set. Just set it with the submitted form data (don't save yet though)
     foreach ($this->inputs_values_corresponding_to_model_fields() as $field_name => $field_value) {
         //only set the non-primary key
         if ($field_name != $this->_model->primary_key_name()) {
             $this->_model_object->set($field_name, $field_value);
         }
     }
 }
 /**
  * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it is a revision save.  If so, then we change things before sending back.  We also do verifications when this IS NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is sometime delayed if the object is created/saved first by the autosave)
  *
  * @param  EE_Base_Class $this_model_obj
  * @param  EE_Base_Class $other_obj
  * @param  boolean       $remove_relation Indicates whether we're doing a remove_relation or add_relation.
  * @return EE_Base_Class. ($other_obj);
  */
 protected function _check_for_revision($this_obj, $other_obj, $remove_relation = FALSE)
 {
     $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name();
     //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling
     if ($this_obj->post_type() == 'revision') {
         //first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one.
         $parent_evt_id = $this_obj->parent();
         /*var_dump($parent_evt_id);
         		var_dump($this_obj);
         		var_dump($other_obj);/**/
         if (!empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) {
             //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table.
             $has_parent_obj = $this->get_other_model()->get_one(array(array($this->_parent_pk_relation_field => $other_obj->ID(), $this->_primary_cpt_field => $this_obj->ID())));
             if ($has_parent_obj) {
                 //this makes sure the update on the current obj happens to the revision's row NOT the parent row.
                 $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID());
                 $other_obj->set($pk_on_related_model, $has_parent_obj->ID());
                 $other_obj->set($this->_primary_cpt_field, $this_obj->ID());
                 if (!$remove_relation) {
                     $other_obj->save();
                     return array($other_obj);
                 } elseif ($remove_relation && !$this->_blocking_delete) {
                     $other_obj->delete();
                     $other_obj->set($this->_parent_pk_relation_field, NULL, true);
                     return array($other_obj);
                 }
             } else {
                 $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID());
                 $other_obj->set($this->_primary_cpt_field, $this_obj->ID());
                 $other_obj->set($pk_on_related_model, NULL, true);
                 //ensure we create a new row for the autosave with parent id the same as the incoming ID.
                 $other_obj->save();
                 //make sure we insert.
                 return array($other_obj);
             }
         }
         //var_dump('what makes it here');
         //var_dump($other_obj);
         //the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it)
         //the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method.
     } else {
         //we only need to do the below IF this is not a remove relation
         if (!$remove_relation) {
             //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object.
             //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first.
             $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID());
             $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : NULL;
             if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) {
                 //yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision.
                 $other_obj->set($this->_primary_cpt_field, $this_obj->ID());
                 $other_obj->save();
                 //now create a new other_obj and fill with details from existing object
                 $new_obj = $other_obj;
                 $new_obj->set($this->_primary_cpt_field, $potential_revision_id);
                 $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID());
                 $new_obj->set($pk_on_related_model, NULL);
                 $new_obj->save();
                 return array($new_obj);
             }
         }
     }
     return $other_obj;
 }
 /**
  * 	_set_date_time_field
  *
  * 	modifies EE_Base_Class EE_Datetime_Field objects
  *
  * @param  EE_Base_Class 	$obj 	EE_Base_Class object
  * @param 	DateTime 			$DateTime 		PHP DateTime object
  * @param  string					$datetime_field_name 	the datetime fieldname to be manipulated
  * @return 	EE_Base_Class
  */
 protected static function _set_date_time_field(EE_Base_Class $obj, DateTime $DateTime, $datetime_field_name)
 {
     // grab current datetime format
     $current_format = $obj->get_format();
     // set new full timestamp format
     $obj->set_date_format(EE_Datetime_Field::mysql_date_format);
     $obj->set_time_format(EE_Datetime_Field::mysql_time_format);
     // set the new date value using a full timestamp format so that no data is lost
     $obj->set($datetime_field_name, $DateTime->format(EE_Datetime_Field::mysql_timestamp_format));
     // reset datetime formats
     $obj->set_date_format($current_format[0]);
     $obj->set_time_format($current_format[1]);
     return $obj;
 }
 /**
  * Overrides parent so when PMD_type is changed we refresh the _type_obj
  * @param string $field_name
  * @param mixed $field_value
  * @param boolean $use_default
  */
 public function set($field_name, $field_value, $use_default = FALSE)
 {
     if ($field_name === 'PMD_type') {
         //the type has probably changed, so forget about its old type object
         $this->_type_obj = NULL;
     }
     parent::set($field_name, $field_value, $use_default);
 }
 public static function date_time_subtract(EE_Base_Class $obj, $dttfield, $what = 'years', $value = 1)
 {
     //get the raw UTC date
     $dtt = $obj->get_raw($dttfield);
     $new_date = self::calc_date($dtt, $what, $value, '-');
     $obj->set($dttfield, $dtt);
     return $obj;
 }
 /**
  * Overrides parent::set method so we can capture any sets for priority.
  * @see parent::set() for phpdocs
  * @param string $field_name
  * @param mixed  $field_value
  * @param bool   $use_default
  * @throws EE_Error
  */
 public function set($field_name, $field_value, $use_default = false)
 {
     if ($field_name === 'MSG_priority') {
         $this->set_priority($field_value);
     }
     parent::set($field_name, $field_value, $use_default);
 }