/**
  * Adds some defaults if they're not specified
  * @param array  $fieldValues
  * @param bool   $bydb
  * @param string $timezone
  */
 protected function __construct($fieldValues = array(), $bydb = FALSE, $timezone = '')
 {
     parent::__construct($fieldValues, $bydb, $timezone);
     if (!$this->get('LIN_code')) {
         $this->set_code($this->generate_code());
     }
 }
 /**
  * Automatically finds the related model info from the form, if present, and
  * save the relations indicated
  * @type string $relation_name
  * @return bool
  * @throws EE_Error
  */
 protected function _save_related_info($relation_name)
 {
     $relation_obj = $this->_model->related_settings_for($relation_name);
     if ($relation_obj instanceof EE_Belongs_To_Relation) {
         //there is just a foreign key on this model pointing to that one
         $this->_model_object->_add_relation_to($this->get_input_value($relation_name), $relation_name);
     } elseif ($relation_obj instanceof EE_Has_Many_Relation) {
         //then we want to consider all of its currently-related things.
         //if they're in this list, keep them
         //if they're not in this list, remove them
         //and lastly add all the new items
         throw new EE_Error(sprintf(__('Automatic saving of related info across a "has many" relation is not yet supported', "event_espresso")));
     } elseif ($relation_obj instanceof EE_HABTM_Relation) {
         //delete everything NOT in this list
         $normalized_input_value = $this->get_input_value($relation_name);
         if ($normalized_input_value && is_array($normalized_input_value)) {
             $where_query_params = array($relation_obj->get_other_model()->primary_key_name() => array('NOT_IN', $normalized_input_value));
         } else {
             $where_query_params = array();
         }
         $this->_model_object->_remove_relations($relation_name, $where_query_params);
         foreach ($normalized_input_value as $id) {
             $this->_model_object->_add_relation_to($id, $relation_name);
         }
     }
     return TRUE;
 }
 /**
  * 
  * @param type $props_n_values
  * @return EE_Answer
  */
 public static function new_instance($props_n_values = array())
 {
     $classname = __CLASS__;
     $has_object = parent::_check_for_object($props_n_values, $classname);
     //		d( $has_object );
     return $has_object ? $has_object : new self($props_n_values);
 }
 /**
  * @param      $table_column
  * @param      $nice_name
  * @param      $nullable
  * @param null $default_value
  * @param null $timezone
  * @param null $date_format
  * @param null $time_format
  * @param null $pretty_date_format
  * @param null $pretty_time_format
  */
 public function __construct($table_column, $nice_name, $nullable, $default_value, $timezone = NULL, $date_format = NULL, $time_format = NULL, $pretty_date_format = NULL, $pretty_time_format = NULL)
 {
     parent::__construct($table_column, $nice_name, $nullable, $default_value);
     $this->_date_format = empty($date_format) ? get_option('date_format') : $date_format;
     $this->_date_format = EE_Base_Class::fix_date_format_for_use_with_strtotime($this->_date_format);
     $this->_time_format = empty($time_format) ? get_option('time_format') : $time_format;
     $this->set_timezone($timezone);
     $this->_pretty_date_format = empty($pretty_date_format) ? get_option('date_format') : $pretty_date_format;
     $this->_pretty_date_format = EE_Base_Class::fix_date_format_for_use_with_strtotime($this->_pretty_date_format);
     $this->_pretty_time_format = empty($pretty_time_format) ? get_option('time_format') : $pretty_time_format;
 }
 /**
  * 	date_time_subtract
  * 	same as date_time_add except subtracting value instead of adding.
  *
  * @param \EE_Base_Class $obj
  * @param  string 		$datetime_field_name 	name of the EE_Datetime_Filed datatype db column to be manipulated
  * @param string         $period
  * @param int            $value
  * @return \EE_Base_Class
  */
 public static function date_time_subtract(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1)
 {
     //get the raw UTC date
     $DateTime = $obj->get_DateTime_object($datetime_field_name);
     $DateTime = EEH_DTT_Helper::calc_date($DateTime, $period, $value, '-');
     return EEH_DTT_Helper::_set_date_time_field($obj, $DateTime, $datetime_field_name);
 }
 /**
  *
  * @param array $props_n_values
  * @return EE_Currency_Payment_Method
  */
 public static function new_instance($props_n_values = array())
 {
     $has_object = parent::_check_for_object($props_n_values, __CLASS__);
     return $has_object ? $has_object : new self($props_n_values);
 }
 /**
  * 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);
 }
 /**
  * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array.
  * We consider something to be a copy if all the attributes match (except the ID, of course).
  * @param array|EE_Base_Class $model_object_or_attributes_array 	If its an array, it's field-value pairs
  * @param array                $query_params like EEM_Base::get_all's query_params.
  * @throws EE_Error
  * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically indexed)
  */
 public function get_all_copies($model_object_or_attributes_array, $query_params = array())
 {
     if ($model_object_or_attributes_array instanceof EE_Base_Class) {
         $attributes_array = $model_object_or_attributes_array->model_field_array();
     } elseif (is_array($model_object_or_attributes_array)) {
         $attributes_array = $model_object_or_attributes_array;
     } else {
         throw new EE_Error(sprintf(__("get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", "event_espresso"), $model_object_or_attributes_array));
     }
     //even copies obviously won't have the same ID, so remove the primary key
     //from the WHERE conditions for finding copies (if there is a primary key, of course)
     if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) {
         unset($attributes_array[$this->primary_key_name()]);
     }
     if (isset($query_params[0])) {
         $query_params[0] = array_merge($attributes_array, $query_params);
     } else {
         $query_params[0] = $attributes_array;
     }
     return $this->get_all($query_params);
 }
 /**
  * This is used to set the model object on the $_model_objects property
  *
  * @since 1.0.0
  *
  * @param  EE_Base_Class $obj
  * @return  void
  */
 protected function _set_model_object(EE_Base_Class $obj)
 {
     $this->_model_objects[$obj->ID()] = $obj;
 }
 private static function _compare_objects(EE_Base_Class $obj_a, EE_Base_Class $obj_b)
 {
     return $obj_a->ID() - $obj_b->ID();
 }
 /**
  * @param array $props_n_values
  * @param null  $timezone
  * @return EE_Extra_Join|mixed
  */
 public static function new_instance($props_n_values = array(), $timezone = NULL)
 {
     $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone);
     return $has_object ? $has_object : new self($props_n_values, FALSE, $timezone);
 }
 /**
  *
  * @param string $log_type !see the acceptable values of LOG_type in EEM__Change_Log::__construct
  * @param mixed $message array|string of the message you want to record
  * @param EE_Base_Class $related_model_obj
  * @return EE_Change_Log
  */
 public function log($log_type, $message, $related_model_obj)
 {
     if ($related_model_obj instanceof EE_Base_Class) {
         $obj_id = $related_model_obj->ID();
         $obj_type = $related_model_obj->get_model()->get_this_model_name();
     } else {
         $obj_id = NULL;
         $obj_type = NULL;
     }
     $log = EE_Change_Log::new_instance(array('LOG_type' => $log_type, 'LOG_message' => $message, 'OBJ_ID' => $obj_id, 'OBJ_type' => $obj_type));
     $log->save();
     return $log;
 }
 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;
 }
 /**
  * update_cache_after_object_save
  * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has obtained after being saved to the db
  *
  * @param string                $relationName       - the type of object that is cached
  * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached
  * @param string                $current_cache_id   - the ID that was used when originally caching the object
  * @return boolean TRUE on success, FALSE on fail
  */
 public function update_cache_after_object_save($relationName, EE_Base_Class $newly_saved_object, $current_cache_id = '')
 {
     // verify that incoming object is of the correct type
     $obj_class = 'EE_' . $relationName;
     if ($newly_saved_object instanceof $obj_class) {
         /* @type EE_Base_Class $newly_saved_object*/
         // now get the type of relation
         $relationship_to_model = $this->get_model()->related_settings_for($relationName);
         // if this is a 1:1 relationship
         if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
             // then just replace the cached object with the newly saved object
             $this->_model_relations[$relationName] = $newly_saved_object;
             return TRUE;
             // or if it's some kind of sordid feral polyamorous relationship...
         } elseif (is_array($this->_model_relations[$relationName]) && isset($this->_model_relations[$relationName][$current_cache_id])) {
             // then remove the current cached item
             unset($this->_model_relations[$relationName][$current_cache_id]);
             // and cache the newly saved object using it's new ID
             $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object;
             return TRUE;
         }
     }
     return FALSE;
 }
 /**
  * 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
  */
 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);
 }
 /**
  *
  * @param array $props_n_values  incoming values
  * @param string $timezone  incoming timezone (if not set the timezone set for the website will be
  *                          		used.)
  * @param array $date_formats  incoming date_formats in an array where the first value is the
  *                             		    date_format and the second value is the time format
  * @return EE_Transaction
  */
 public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
 {
     $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
     return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
 }
 /**
  * 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;
 }
 /**
  * Overrides parent to add some logging for when payment methods get deactivated
  *
  * @param array $set_cols_n_values
  * @return int @see EE_Base_Class::save()
  * @throws \EE_Error
  */
 public function save($set_cols_n_values = array())
 {
     $results = parent::save($set_cols_n_values);
     if ($this->get_original('PMD_scope') !== $this->get('PMD_scope')) {
         EE_Log::instance()->log(__FILE__, __FUNCTION__, sprintf(__('Set new scope on payment method %1$s to %2$s from %3$s on URL %4$s', 'event_espresso'), $this->name(), serialize($this->get_original('PMD_scope')), serialize($this->get('PMD_scope')), EE_Registry::instance()->REQ->get_current_page_permalink()), 'payment_method_change');
     }
     return $results;
 }
 /**
  * Shorthand for setting the OBJ_ID and OBJ_type. Slightly handier than using
  * _add_relation_to because you don't have to specify what type of model you're
  * associating it with
  * @param EE_Base_Class $object
  * @param boolean $save
  * @return boolean if $save=true, NULL is $save=false
  */
 function set_object($object, $save = TRUE)
 {
     if ($object instanceof EE_Base_Class) {
         $this->set_OBJ_type($object->get_model()->get_this_model_name());
         $this->set_OBJ_ID($object->ID());
     } else {
         $this->set_OBJ_type(NULL);
         $this->set_OBJ_ID(NULL);
     }
     if ($save) {
         return $this->save();
     } else {
         return NULL;
     }
 }
 /**
  * this just returns a model_object_id for incoming item that could be an object or id.
  * @param  EE_Base_Class|int $model_object_or_id model object or the primary key of this model
  * @throws EE_Error
  * @return int
  */
 protected function _get_model_object_id($model_object_or_id)
 {
     if ($model_object_or_id instanceof EE_Base_Class) {
         $model_object_id = $model_object_or_id->ID();
     } else {
         $model_object_id = $model_object_or_id;
     }
     if (!$model_object_id) {
         throw new EE_Error(sprintf(__("Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", "event_espresso"), $this->get_other_model()->get_this_model_name(), $this->get_this_model()->get_this_model_name()));
     }
     return $model_object_id;
 }
 /**
  * This simply returns the datetime for the given field name
  * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions (and the equivalent e_date, e_time, e_datetime).
  *
  * @access   protected
  * @param  string  $field_name   Field on the instantiated EE_Base_Class child object
  * @param null     $dt_frmt 	valid datetime format used for date (if '' then we just use the default on the field, if NULL we use the last-used format)
  * @param null     $tm_frmt 	Same as above except this is for time format
  * @param string   $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time.
  * @param  boolean $echo         Whether the dtt is echoing using pretty echoing or just returned using vanilla get
  * @return void | string | bool | EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown if field is not a valid dtt field, or void if echoing
  */
 protected function _get_datetime($field_name, $dt_frmt = NULL, $tm_frmt = NULL, $date_or_time = NULL, $echo = FALSE)
 {
     $in_dt_frmt = empty($dt_frmt) ? $this->_dt_frmt : EE_Base_Class::fix_date_format_for_use_with_strtotime($dt_frmt);
     $in_tm_frmt = empty($tm_frmt) ? $this->_tm_frmt : $tm_frmt;
     //validate field for datetime and returns field settings if valid.
     $field = $this->_get_dtt_field_settings($field_name);
     if ($dt_frmt !== NULL) {
         $this->_clear_cached_property($field_name, $date_or_time);
     }
     if ($echo) {
         $field->set_pretty_date_format($in_dt_frmt);
     } else {
         $field->set_date_format($in_dt_frmt);
     }
     if ($tm_frmt !== NULL) {
         $this->_clear_cached_property($field_name, $date_or_time);
     }
     if ($echo) {
         $field->set_pretty_time_format($in_tm_frmt);
     } else {
         $field->set_time_format($in_tm_frmt);
     }
     //set timezone in field object
     $field->set_timezone($this->_timezone);
     //set the output returned
     switch ($date_or_time) {
         case 'D':
             $field->set_date_time_output('date');
             break;
         case 'T':
             $field->set_date_time_output('time');
             break;
         default:
             $field->set_date_time_output();
     }
     if ($echo) {
         $this->e($field_name, $date_or_time);
         return '';
     }
     return $this->get($field_name, $date_or_time);
 }
 /**
  * Callback for AHEE__EE_Base_Class__delete_before hook so we can ensure any person relationships for an item being deleted
  * are also handled.
  *
  * @param EE_Base_Class $model_object
  */
 public function delete_people_relations_on_related_delete(EE_Base_Class $model_object)
 {
     if ($model_object instanceof EE_Event) {
         $remove_where = array('OBJ_ID' => $model_object->ID(), 'OBJ_type' => 'Event');
         EEM_Person_Post::instance()->delete(array($remove_where));
     }
 }