@@ -22,83 +22,83 @@ |
||
| 22 | 22 | class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Gets the SQL string for performing the join between this model and the other model. |
|
| 27 | - * |
|
| 28 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 29 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
| 30 | - * other_model_primary_table.fk" etc |
|
| 31 | - * @throws \EE_Error |
|
| 32 | - */ |
|
| 33 | - public function get_join_statement($model_relation_chain) |
|
| 34 | - { |
|
| 35 | - //create the sql string like |
|
| 36 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
| 37 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
| 38 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 39 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 40 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 41 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 42 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
| 43 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
| 44 | - $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
| 25 | + /** |
|
| 26 | + * Gets the SQL string for performing the join between this model and the other model. |
|
| 27 | + * |
|
| 28 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 29 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
| 30 | + * other_model_primary_table.fk" etc |
|
| 31 | + * @throws \EE_Error |
|
| 32 | + */ |
|
| 33 | + public function get_join_statement($model_relation_chain) |
|
| 34 | + { |
|
| 35 | + //create the sql string like |
|
| 36 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
| 37 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
| 38 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 39 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 40 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 41 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 42 | + $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
| 43 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
| 44 | + $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
| 45 | 45 | |
| 46 | - return $this->_left_join($fk_table, |
|
| 47 | - $fk_table_alias, |
|
| 48 | - $other_table_fk_field->get_table_column(), |
|
| 49 | - $pk_table_alias, |
|
| 50 | - $this_table_pk_field->get_table_column(), |
|
| 51 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
| 52 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
| 53 | - } |
|
| 46 | + return $this->_left_join($fk_table, |
|
| 47 | + $fk_table_alias, |
|
| 48 | + $other_table_fk_field->get_table_column(), |
|
| 49 | + $pk_table_alias, |
|
| 50 | + $this_table_pk_field->get_table_column(), |
|
| 51 | + $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
| 52 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
| 58 | - * you like. |
|
| 59 | - * |
|
| 60 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 61 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 62 | - * @param array $extra_join_model_fields_n_values |
|
| 63 | - * @return \EE_Base_Class |
|
| 64 | - * @throws \EE_Error |
|
| 65 | - */ |
|
| 66 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 67 | - { |
|
| 68 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 69 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 56 | + /** |
|
| 57 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
| 58 | + * you like. |
|
| 59 | + * |
|
| 60 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 61 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 62 | + * @param array $extra_join_model_fields_n_values |
|
| 63 | + * @return \EE_Base_Class |
|
| 64 | + * @throws \EE_Error |
|
| 65 | + */ |
|
| 66 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 67 | + { |
|
| 68 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 69 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 70 | 70 | |
| 71 | - //find the field on the other model which is a foreign key to this model |
|
| 72 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 73 | - //set that field on the other model to this model's ID |
|
| 74 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
| 75 | - $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
| 76 | - $this->get_this_model()->get_this_model_name()); |
|
| 77 | - $other_model_obj->save(); |
|
| 78 | - return $other_model_obj; |
|
| 79 | - } |
|
| 71 | + //find the field on the other model which is a foreign key to this model |
|
| 72 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 73 | + //set that field on the other model to this model's ID |
|
| 74 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
| 75 | + $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
| 76 | + $this->get_this_model()->get_this_model_name()); |
|
| 77 | + $other_model_obj->save(); |
|
| 78 | + return $other_model_obj; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
| 84 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
| 85 | - * |
|
| 86 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 87 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 88 | - * @param array $where_query |
|
| 89 | - * @return \EE_Base_Class |
|
| 90 | - * @throws \EE_Error |
|
| 91 | - */ |
|
| 92 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 93 | - { |
|
| 94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 95 | - //find the field on the other model which is a foreign key to this model |
|
| 96 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 97 | - //set that field on the other model to this model's ID |
|
| 98 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 99 | - $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null, |
|
| 100 | - true); |
|
| 101 | - $other_model_obj->save(); |
|
| 102 | - return $other_model_obj; |
|
| 103 | - } |
|
| 82 | + /** |
|
| 83 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
| 84 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
| 85 | + * |
|
| 86 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 87 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 88 | + * @param array $where_query |
|
| 89 | + * @return \EE_Base_Class |
|
| 90 | + * @throws \EE_Error |
|
| 91 | + */ |
|
| 92 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 93 | + { |
|
| 94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 95 | + //find the field on the other model which is a foreign key to this model |
|
| 96 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 97 | + //set that field on the other model to this model's ID |
|
| 98 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 99 | + $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null, |
|
| 100 | + true); |
|
| 101 | + $other_model_obj->save(); |
|
| 102 | + return $other_model_obj; |
|
| 103 | + } |
|
| 104 | 104 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | /* |
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
| 7 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
| 8 | 8 | |
| 9 | 9 | |
| 10 | 10 | /** |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
| 38 | 38 | $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
| 39 | 39 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 40 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 40 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
| 41 | 41 | $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 42 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
| 42 | + $this->get_other_model()->get_this_model_name()).$other_table_fk_field->get_table_alias(); |
|
| 43 | 43 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
| 44 | 44 | $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
| 45 | 45 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | $other_table_fk_field->get_table_column(), |
| 49 | 49 | $pk_table_alias, |
| 50 | 50 | $this_table_pk_field->get_table_column(), |
| 51 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
| 51 | + $fk_table_alias.'.'.$field_with_model_name->get_table_column()."='".$this->get_this_model()->get_this_model_name()."'") |
|
| 52 | 52 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -14,303 +14,303 @@ |
||
| 14 | 14 | { |
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
| 19 | - * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
| 20 | - * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - private $_primary_cpt_field; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
| 29 | - * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
| 30 | - * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
| 31 | - * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
| 32 | - * primary key in the other model (EEM_Datetime). |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - private $_parent_pk_relation_field; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
| 41 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
| 42 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
| 43 | - * related models across the relation, and add-and-remove the relationships. |
|
| 44 | - * |
|
| 45 | - * @param string $primary_cpt_field See property description for details |
|
| 46 | - * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
| 47 | - * See property desc for details. |
|
| 48 | - * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
| 49 | - * related models across this relation, block (prevent and add an |
|
| 50 | - * error) the deletion of this model |
|
| 51 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 52 | - * default |
|
| 53 | - */ |
|
| 54 | - public function __construct( |
|
| 55 | - $primary_cpt_field, |
|
| 56 | - $parent_pk_relation_field, |
|
| 57 | - $block_deletes = true, |
|
| 58 | - $blocking_delete_error_message = null |
|
| 59 | - ) { |
|
| 60 | - $this->_primary_cpt_field = $primary_cpt_field; |
|
| 61 | - $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
| 62 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
| 68 | - * you like. |
|
| 69 | - * |
|
| 70 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 71 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 72 | - * @param array $extra_join_model_fields_n_values |
|
| 73 | - * @return \EE_Base_Class |
|
| 74 | - * @throws \EE_Error |
|
| 75 | - */ |
|
| 76 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 77 | - { |
|
| 78 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 79 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 80 | - |
|
| 81 | - //handle possible revisions |
|
| 82 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
| 83 | - |
|
| 84 | - //if is array, then we've already done the add_relation so let's get out |
|
| 85 | - if (is_array($other_model_obj)) { |
|
| 86 | - return $other_model_obj[0]; |
|
| 87 | - } |
|
| 88 | - //find the field on the other model which is a foreign key to this model |
|
| 89 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 90 | - //set that field on the other model to this model's ID |
|
| 91 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
| 92 | - $other_model_obj->save(); |
|
| 93 | - return $other_model_obj; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
| 99 | - * |
|
| 100 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 101 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 102 | - * @param array $where_query |
|
| 103 | - * @return \EE_Base_Class |
|
| 104 | - * @throws \EE_Error |
|
| 105 | - */ |
|
| 106 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 107 | - { |
|
| 108 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
| 109 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 110 | - //handle possible revisions |
|
| 111 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - //if is array, then we've already done the add_relation so let's get out |
|
| 115 | - if (is_array($other_model_obj)) { |
|
| 116 | - return $other_model_obj[0]; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - //find the field on the other model which is a foreign key to this model |
|
| 120 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - //set that field on the other model to this model's ID |
|
| 124 | - if ($this->_blocking_delete) { |
|
| 125 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 126 | - $other_model_obj->save(); |
|
| 127 | - } else { |
|
| 128 | - $other_model_obj->delete(); |
|
| 129 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 130 | - return $other_model_obj; |
|
| 131 | - } |
|
| 132 | - return $other_model_obj; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
| 138 | - * in here. |
|
| 139 | - * |
|
| 140 | - * @param EE_Base_Class|int $model_object_or_id |
|
| 141 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 142 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
| 143 | - * @return EE_Base_Class[] |
|
| 144 | - * @throws \EE_Error |
|
| 145 | - */ |
|
| 146 | - public function get_all_related( |
|
| 147 | - $model_object_or_id, |
|
| 148 | - $query_params = array(), |
|
| 149 | - $values_already_prepared_by_model_object = false |
|
| 150 | - ) { |
|
| 151 | - if ($values_already_prepared_by_model_object !== false) { |
|
| 152 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
| 153 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
| 154 | - '4.8.1'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - //if this is an autosave then we're going to get things differently |
|
| 158 | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 159 | - return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return parent::get_all_related($model_object_or_id, $query_params); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
| 168 | - * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
| 169 | - * |
|
| 170 | - * @access protected |
|
| 171 | - * @param $model_object_or_id |
|
| 172 | - * @param $query_params |
|
| 173 | - * @param bool $deprecated |
|
| 174 | - * @return \EE_Base_Class[] |
|
| 175 | - * @throws \EE_Error |
|
| 176 | - */ |
|
| 177 | - protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
| 178 | - { |
|
| 179 | - |
|
| 180 | - //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
| 181 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
| 182 | - |
|
| 183 | - $autosave = wp_get_post_autosave($model_object_id); |
|
| 184 | - $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
| 185 | - |
|
| 186 | - $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
| 187 | - $parent_ids = $parents = array(); |
|
| 188 | - $return_objs = array(); |
|
| 189 | - |
|
| 190 | - //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
| 191 | - foreach ($autosave_relations as $a_r) { |
|
| 192 | - $pid = $a_r->parent(); |
|
| 193 | - if (! empty($pid)) { |
|
| 194 | - $parent_ids[] = $pid; |
|
| 195 | - } else { |
|
| 196 | - $return_objs[] = $a_r; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - //we have to make sure we also include the ORIGINAL values |
|
| 201 | - $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
| 202 | - |
|
| 203 | - //merge $originals with $return_objs |
|
| 204 | - if ($originals) { |
|
| 205 | - $return_objs = array_merge($originals, $return_objs); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - //now we setup the query to get all the parents |
|
| 209 | - if (! empty($parent_ids)) { |
|
| 210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
| 211 | - $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
| 212 | - $parents = $this->get_other_model()->get_all($query_params); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - //var_dump($parents); |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - //now merge parents with our current $return_objs and send back |
|
| 219 | - return array_merge($parents, $return_objs); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
| 225 | - * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
| 226 | - * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
| 227 | - * sometime delayed if the object is created/saved first by the autosave) |
|
| 228 | - * |
|
| 229 | - * @param EE_Base_Class $this_obj |
|
| 230 | - * @param EE_Base_Class $other_obj |
|
| 231 | - * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
| 232 | - * @return EE_Base_Class. ($other_obj); |
|
| 233 | - * @throws \EE_Error |
|
| 234 | - */ |
|
| 235 | - protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
| 236 | - { |
|
| 237 | - $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
| 238 | - //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
| 239 | - if ($this_obj->post_type() === 'revision') { |
|
| 240 | - //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. |
|
| 241 | - $parent_evt_id = $this_obj->parent(); |
|
| 242 | - /*var_dump($parent_evt_id); |
|
| 17 | + /** |
|
| 18 | + * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
| 19 | + * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
| 20 | + * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + private $_primary_cpt_field; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
| 29 | + * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
| 30 | + * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
| 31 | + * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
| 32 | + * primary key in the other model (EEM_Datetime). |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + private $_parent_pk_relation_field; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
| 41 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
| 42 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
| 43 | + * related models across the relation, and add-and-remove the relationships. |
|
| 44 | + * |
|
| 45 | + * @param string $primary_cpt_field See property description for details |
|
| 46 | + * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
| 47 | + * See property desc for details. |
|
| 48 | + * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
| 49 | + * related models across this relation, block (prevent and add an |
|
| 50 | + * error) the deletion of this model |
|
| 51 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 52 | + * default |
|
| 53 | + */ |
|
| 54 | + public function __construct( |
|
| 55 | + $primary_cpt_field, |
|
| 56 | + $parent_pk_relation_field, |
|
| 57 | + $block_deletes = true, |
|
| 58 | + $blocking_delete_error_message = null |
|
| 59 | + ) { |
|
| 60 | + $this->_primary_cpt_field = $primary_cpt_field; |
|
| 61 | + $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
| 62 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
| 68 | + * you like. |
|
| 69 | + * |
|
| 70 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 71 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 72 | + * @param array $extra_join_model_fields_n_values |
|
| 73 | + * @return \EE_Base_Class |
|
| 74 | + * @throws \EE_Error |
|
| 75 | + */ |
|
| 76 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 77 | + { |
|
| 78 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 79 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 80 | + |
|
| 81 | + //handle possible revisions |
|
| 82 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
| 83 | + |
|
| 84 | + //if is array, then we've already done the add_relation so let's get out |
|
| 85 | + if (is_array($other_model_obj)) { |
|
| 86 | + return $other_model_obj[0]; |
|
| 87 | + } |
|
| 88 | + //find the field on the other model which is a foreign key to this model |
|
| 89 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 90 | + //set that field on the other model to this model's ID |
|
| 91 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
| 92 | + $other_model_obj->save(); |
|
| 93 | + return $other_model_obj; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
| 99 | + * |
|
| 100 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 101 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 102 | + * @param array $where_query |
|
| 103 | + * @return \EE_Base_Class |
|
| 104 | + * @throws \EE_Error |
|
| 105 | + */ |
|
| 106 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 107 | + { |
|
| 108 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
| 109 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 110 | + //handle possible revisions |
|
| 111 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + //if is array, then we've already done the add_relation so let's get out |
|
| 115 | + if (is_array($other_model_obj)) { |
|
| 116 | + return $other_model_obj[0]; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + //find the field on the other model which is a foreign key to this model |
|
| 120 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + //set that field on the other model to this model's ID |
|
| 124 | + if ($this->_blocking_delete) { |
|
| 125 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 126 | + $other_model_obj->save(); |
|
| 127 | + } else { |
|
| 128 | + $other_model_obj->delete(); |
|
| 129 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
| 130 | + return $other_model_obj; |
|
| 131 | + } |
|
| 132 | + return $other_model_obj; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
| 138 | + * in here. |
|
| 139 | + * |
|
| 140 | + * @param EE_Base_Class|int $model_object_or_id |
|
| 141 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 142 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
| 143 | + * @return EE_Base_Class[] |
|
| 144 | + * @throws \EE_Error |
|
| 145 | + */ |
|
| 146 | + public function get_all_related( |
|
| 147 | + $model_object_or_id, |
|
| 148 | + $query_params = array(), |
|
| 149 | + $values_already_prepared_by_model_object = false |
|
| 150 | + ) { |
|
| 151 | + if ($values_already_prepared_by_model_object !== false) { |
|
| 152 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
| 153 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
| 154 | + '4.8.1'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + //if this is an autosave then we're going to get things differently |
|
| 158 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 159 | + return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return parent::get_all_related($model_object_or_id, $query_params); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
| 168 | + * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
| 169 | + * |
|
| 170 | + * @access protected |
|
| 171 | + * @param $model_object_or_id |
|
| 172 | + * @param $query_params |
|
| 173 | + * @param bool $deprecated |
|
| 174 | + * @return \EE_Base_Class[] |
|
| 175 | + * @throws \EE_Error |
|
| 176 | + */ |
|
| 177 | + protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
| 178 | + { |
|
| 179 | + |
|
| 180 | + //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
| 181 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
| 182 | + |
|
| 183 | + $autosave = wp_get_post_autosave($model_object_id); |
|
| 184 | + $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
| 185 | + |
|
| 186 | + $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
| 187 | + $parent_ids = $parents = array(); |
|
| 188 | + $return_objs = array(); |
|
| 189 | + |
|
| 190 | + //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
| 191 | + foreach ($autosave_relations as $a_r) { |
|
| 192 | + $pid = $a_r->parent(); |
|
| 193 | + if (! empty($pid)) { |
|
| 194 | + $parent_ids[] = $pid; |
|
| 195 | + } else { |
|
| 196 | + $return_objs[] = $a_r; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + //we have to make sure we also include the ORIGINAL values |
|
| 201 | + $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
| 202 | + |
|
| 203 | + //merge $originals with $return_objs |
|
| 204 | + if ($originals) { |
|
| 205 | + $return_objs = array_merge($originals, $return_objs); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + //now we setup the query to get all the parents |
|
| 209 | + if (! empty($parent_ids)) { |
|
| 210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
| 211 | + $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
| 212 | + $parents = $this->get_other_model()->get_all($query_params); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + //var_dump($parents); |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + //now merge parents with our current $return_objs and send back |
|
| 219 | + return array_merge($parents, $return_objs); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
| 225 | + * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
| 226 | + * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
| 227 | + * sometime delayed if the object is created/saved first by the autosave) |
|
| 228 | + * |
|
| 229 | + * @param EE_Base_Class $this_obj |
|
| 230 | + * @param EE_Base_Class $other_obj |
|
| 231 | + * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
| 232 | + * @return EE_Base_Class. ($other_obj); |
|
| 233 | + * @throws \EE_Error |
|
| 234 | + */ |
|
| 235 | + protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
| 236 | + { |
|
| 237 | + $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
| 238 | + //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
| 239 | + if ($this_obj->post_type() === 'revision') { |
|
| 240 | + //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. |
|
| 241 | + $parent_evt_id = $this_obj->parent(); |
|
| 242 | + /*var_dump($parent_evt_id); |
|
| 243 | 243 | var_dump($this_obj); |
| 244 | 244 | var_dump($other_obj);/**/ |
| 245 | 245 | |
| 246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
| 247 | - //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. |
|
| 248 | - $has_parent_obj = $this->get_other_model()->get_one(array( |
|
| 249 | - array( |
|
| 250 | - $this->_parent_pk_relation_field => $other_obj->ID(), |
|
| 251 | - $this->_primary_cpt_field => $this_obj->ID(), |
|
| 252 | - ), |
|
| 253 | - )); |
|
| 254 | - |
|
| 255 | - if ($has_parent_obj) { |
|
| 256 | - //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
| 257 | - |
|
| 258 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 259 | - $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
| 260 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 261 | - |
|
| 262 | - if (! $remove_relation) { |
|
| 263 | - $other_obj->save(); |
|
| 264 | - return array($other_obj); |
|
| 265 | - } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
| 266 | - $other_obj->delete(); |
|
| 267 | - $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
| 268 | - return array($other_obj); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - } else { |
|
| 272 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 273 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 274 | - $other_obj->set($pk_on_related_model, null, |
|
| 275 | - true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
| 276 | - $other_obj->save(); //make sure we insert. |
|
| 277 | - return array($other_obj); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - //var_dump('what makes it here'); |
|
| 282 | - //var_dump($other_obj); |
|
| 283 | - //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) |
|
| 284 | - |
|
| 285 | - //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. |
|
| 286 | - |
|
| 287 | - } else { |
|
| 288 | - |
|
| 289 | - //we only need to do the below IF this is not a remove relation |
|
| 290 | - if (! $remove_relation) { |
|
| 291 | - //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. |
|
| 292 | - //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. |
|
| 293 | - $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
| 294 | - $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
| 295 | - |
|
| 296 | - if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
| 297 | - //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. |
|
| 298 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 299 | - $other_obj->save(); |
|
| 300 | - |
|
| 301 | - //now create a new other_obj and fill with details from existing object |
|
| 302 | - $new_obj = $other_obj; |
|
| 303 | - $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
| 304 | - $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 305 | - $new_obj->set($pk_on_related_model, null); |
|
| 306 | - $new_obj->save(); |
|
| 307 | - return array($new_obj); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - return $other_obj; |
|
| 314 | - } |
|
| 246 | + if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
| 247 | + //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. |
|
| 248 | + $has_parent_obj = $this->get_other_model()->get_one(array( |
|
| 249 | + array( |
|
| 250 | + $this->_parent_pk_relation_field => $other_obj->ID(), |
|
| 251 | + $this->_primary_cpt_field => $this_obj->ID(), |
|
| 252 | + ), |
|
| 253 | + )); |
|
| 254 | + |
|
| 255 | + if ($has_parent_obj) { |
|
| 256 | + //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
| 257 | + |
|
| 258 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 259 | + $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
| 260 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 261 | + |
|
| 262 | + if (! $remove_relation) { |
|
| 263 | + $other_obj->save(); |
|
| 264 | + return array($other_obj); |
|
| 265 | + } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
| 266 | + $other_obj->delete(); |
|
| 267 | + $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
| 268 | + return array($other_obj); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + } else { |
|
| 272 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 273 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 274 | + $other_obj->set($pk_on_related_model, null, |
|
| 275 | + true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
| 276 | + $other_obj->save(); //make sure we insert. |
|
| 277 | + return array($other_obj); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + //var_dump('what makes it here'); |
|
| 282 | + //var_dump($other_obj); |
|
| 283 | + //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) |
|
| 284 | + |
|
| 285 | + //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. |
|
| 286 | + |
|
| 287 | + } else { |
|
| 288 | + |
|
| 289 | + //we only need to do the below IF this is not a remove relation |
|
| 290 | + if (! $remove_relation) { |
|
| 291 | + //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. |
|
| 292 | + //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. |
|
| 293 | + $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
| 294 | + $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
| 295 | + |
|
| 296 | + if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
| 297 | + //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. |
|
| 298 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
| 299 | + $other_obj->save(); |
|
| 300 | + |
|
| 301 | + //now create a new other_obj and fill with details from existing object |
|
| 302 | + $new_obj = $other_obj; |
|
| 303 | + $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
| 304 | + $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
| 305 | + $new_obj->set($pk_on_related_model, null); |
|
| 306 | + $new_obj->save(); |
|
| 307 | + return array($new_obj); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + return $other_obj; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | 316 | } |
| 317 | 317 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(EE_MODELS . 'relations/EE_Has_Many_Relation.php'); |
|
| 2 | +require_once(EE_MODELS.'relations/EE_Has_Many_Relation.php'); |
|
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | /** |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
| 191 | 191 | foreach ($autosave_relations as $a_r) { |
| 192 | 192 | $pid = $a_r->parent(); |
| 193 | - if (! empty($pid)) { |
|
| 193 | + if ( ! empty($pid)) { |
|
| 194 | 194 | $parent_ids[] = $pid; |
| 195 | 195 | } else { |
| 196 | 196 | $return_objs[] = $a_r; |
@@ -206,8 +206,8 @@ discard block |
||
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | //now we setup the query to get all the parents |
| 209 | - if (! empty($parent_ids)) { |
|
| 210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
| 209 | + if ( ! empty($parent_ids)) { |
|
| 210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name().".".$this->get_this_model()->get_primary_key_field()->get_name(); |
|
| 211 | 211 | $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
| 212 | 212 | $parents = $this->get_other_model()->get_all($query_params); |
| 213 | 213 | } |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | var_dump($this_obj); |
| 244 | 244 | var_dump($other_obj);/**/ |
| 245 | 245 | |
| 246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
| 246 | + if ( ! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
| 247 | 247 | //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. |
| 248 | 248 | $has_parent_obj = $this->get_other_model()->get_one(array( |
| 249 | 249 | array( |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
| 260 | 260 | $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
| 261 | 261 | |
| 262 | - if (! $remove_relation) { |
|
| 262 | + if ( ! $remove_relation) { |
|
| 263 | 263 | $other_obj->save(); |
| 264 | 264 | return array($other_obj); |
| 265 | 265 | } elseif ($remove_relation && ! $this->_blocking_delete) { |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | } else { |
| 288 | 288 | |
| 289 | 289 | //we only need to do the below IF this is not a remove relation |
| 290 | - if (! $remove_relation) { |
|
| 290 | + if ( ! $remove_relation) { |
|
| 291 | 291 | //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. |
| 292 | 292 | //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. |
| 293 | 293 | $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
@@ -10,264 +10,264 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class EE_HABTM_Any_Relation extends EE_HABTM_Relation |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $_alphabetically_first_model_name; |
|
| 13 | + /** |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $_alphabetically_first_model_name; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 20 | - * (and an ee joining-model.) This knows how to join the models, |
|
| 21 | - * get related models across the relation, and add-and-remove the relationships. |
|
| 22 | - * |
|
| 23 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 24 | - * are related models across this relation, block (prevent and add an |
|
| 25 | - * error) the deletion of this model |
|
| 26 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 27 | - * default |
|
| 28 | - */ |
|
| 29 | - public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
| 30 | - { |
|
| 31 | - parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
| 32 | - } |
|
| 18 | + /** |
|
| 19 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 20 | + * (and an ee joining-model.) This knows how to join the models, |
|
| 21 | + * get related models across the relation, and add-and-remove the relationships. |
|
| 22 | + * |
|
| 23 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 24 | + * are related models across this relation, block (prevent and add an |
|
| 25 | + * error) the deletion of this model |
|
| 26 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 27 | + * default |
|
| 28 | + */ |
|
| 29 | + public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
| 30 | + { |
|
| 31 | + parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param $this_model_name |
|
| 37 | - * @param $other_model_name |
|
| 38 | - * @throws EE_Error |
|
| 39 | - */ |
|
| 40 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
| 41 | - { |
|
| 42 | - if ($this_model_name < $other_model_name) { |
|
| 43 | - $this->_alphabetically_first_model_name = $this_model_name; |
|
| 44 | - } else { |
|
| 45 | - $this->_alphabetically_first_model_name = $other_model_name; |
|
| 46 | - } |
|
| 47 | - parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
| 48 | - } |
|
| 35 | + /** |
|
| 36 | + * @param $this_model_name |
|
| 37 | + * @param $other_model_name |
|
| 38 | + * @throws EE_Error |
|
| 39 | + */ |
|
| 40 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
| 41 | + { |
|
| 42 | + if ($this_model_name < $other_model_name) { |
|
| 43 | + $this->_alphabetically_first_model_name = $this_model_name; |
|
| 44 | + } else { |
|
| 45 | + $this->_alphabetically_first_model_name = $other_model_name; |
|
| 46 | + } |
|
| 47 | + parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string $model_name |
|
| 53 | - * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
| 54 | - * @return EE_Model_Field_Base |
|
| 55 | - * @throws \EE_Error |
|
| 56 | - */ |
|
| 57 | - public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
| 58 | - { |
|
| 59 | - $order = null; |
|
| 60 | - if ($model_name === $this->_alphabetically_first_model_name) { |
|
| 61 | - $order = 'first'; |
|
| 62 | - } else { |
|
| 63 | - $order = 'second'; |
|
| 64 | - } |
|
| 65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
| 66 | - } |
|
| 51 | + /** |
|
| 52 | + * @param string $model_name |
|
| 53 | + * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
| 54 | + * @return EE_Model_Field_Base |
|
| 55 | + * @throws \EE_Error |
|
| 56 | + */ |
|
| 57 | + public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
| 58 | + { |
|
| 59 | + $order = null; |
|
| 60 | + if ($model_name === $this->_alphabetically_first_model_name) { |
|
| 61 | + $order = 'first'; |
|
| 62 | + } else { |
|
| 63 | + $order = 'second'; |
|
| 64 | + } |
|
| 65 | + return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 71 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 72 | - * |
|
| 73 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 74 | - * @return string of SQL |
|
| 75 | - * @throws \EE_Error |
|
| 76 | - */ |
|
| 77 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 78 | - { |
|
| 79 | - //create sql like |
|
| 80 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 81 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 82 | - //remember the model relation chain to the JOIN model, because we'll |
|
| 83 | - //need it for get_join_statement() |
|
| 84 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 85 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
| 86 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
| 87 | - $this->get_this_model()->get_this_model_name(), |
|
| 88 | - 'ID'); |
|
| 89 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
| 90 | - $this->get_this_model()->get_this_model_name(), |
|
| 91 | - 'name'); |
|
| 92 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 94 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 96 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 97 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 98 | - $SQL = $this->_left_join( |
|
| 99 | - $join_table, |
|
| 100 | - $join_table_alias, |
|
| 101 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
| 102 | - $this_table_alias, |
|
| 103 | - $this_table_pk_field->get_table_column(), |
|
| 104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
| 105 | - $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 69 | + /** |
|
| 70 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 71 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 72 | + * |
|
| 73 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 74 | + * @return string of SQL |
|
| 75 | + * @throws \EE_Error |
|
| 76 | + */ |
|
| 77 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 78 | + { |
|
| 79 | + //create sql like |
|
| 80 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 81 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 82 | + //remember the model relation chain to the JOIN model, because we'll |
|
| 83 | + //need it for get_join_statement() |
|
| 84 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 85 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
| 86 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
| 87 | + $this->get_this_model()->get_this_model_name(), |
|
| 88 | + 'ID'); |
|
| 89 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
| 90 | + $this->get_this_model()->get_this_model_name(), |
|
| 91 | + 'name'); |
|
| 92 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 93 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 94 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 95 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 96 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 97 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 98 | + $SQL = $this->_left_join( |
|
| 99 | + $join_table, |
|
| 100 | + $join_table_alias, |
|
| 101 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
| 102 | + $this_table_alias, |
|
| 103 | + $this_table_pk_field->get_table_column(), |
|
| 104 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
| 105 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 106 | 106 | |
| 107 | - return $SQL; |
|
| 108 | - } |
|
| 107 | + return $SQL; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 113 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 114 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 115 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 116 | - * the intermediate join table, and then to the other model's pk's table |
|
| 117 | - * |
|
| 118 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 119 | - * @return string of SQL |
|
| 120 | - * @throws \EE_Error |
|
| 121 | - */ |
|
| 122 | - public function get_join_statement($model_relation_chain) |
|
| 123 | - { |
|
| 124 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
| 125 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 126 | - 'event_espresso'))); |
|
| 127 | - } |
|
| 128 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
| 129 | - $this->get_this_model()->get_this_model_name(), |
|
| 130 | - 'ID'); |
|
| 131 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
| 132 | - $this->get_other_model()->get_this_model_name(), |
|
| 133 | - 'ID'); |
|
| 134 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
| 135 | - $this->get_other_model()->get_this_model_name(), |
|
| 136 | - 'name'); |
|
| 111 | + /** |
|
| 112 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 113 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 114 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 115 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 116 | + * the intermediate join table, and then to the other model's pk's table |
|
| 117 | + * |
|
| 118 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 119 | + * @return string of SQL |
|
| 120 | + * @throws \EE_Error |
|
| 121 | + */ |
|
| 122 | + public function get_join_statement($model_relation_chain) |
|
| 123 | + { |
|
| 124 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
| 125 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 126 | + 'event_espresso'))); |
|
| 127 | + } |
|
| 128 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
| 129 | + $this->get_this_model()->get_this_model_name(), |
|
| 130 | + 'ID'); |
|
| 131 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
| 132 | + $this->get_other_model()->get_this_model_name(), |
|
| 133 | + 'ID'); |
|
| 134 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
| 135 | + $this->get_other_model()->get_this_model_name(), |
|
| 136 | + 'name'); |
|
| 137 | 137 | |
| 138 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
| 139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 138 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
| 139 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 140 | 140 | |
| 141 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 142 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 144 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 141 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 142 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 143 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 144 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 145 | 145 | |
| 146 | - $SQL = $this->_left_join( |
|
| 147 | - $other_table, |
|
| 148 | - $other_table_alias, |
|
| 149 | - $other_table_pk_field->get_table_column(), |
|
| 150 | - $join_table_alias, |
|
| 151 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
| 152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
| 153 | - ) . |
|
| 154 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 155 | - return $SQL; |
|
| 156 | - } |
|
| 146 | + $SQL = $this->_left_join( |
|
| 147 | + $other_table, |
|
| 148 | + $other_table_alias, |
|
| 149 | + $other_table_pk_field->get_table_column(), |
|
| 150 | + $join_table_alias, |
|
| 151 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
| 152 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
| 153 | + ) . |
|
| 154 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 155 | + return $SQL; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 161 | - * |
|
| 162 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 163 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 164 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 165 | - * checking existing values and for setting new rows if |
|
| 166 | - * no exact matches. |
|
| 167 | - * @return EE_Base_Class |
|
| 168 | - * @throws \EE_Error |
|
| 169 | - */ |
|
| 170 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 171 | - { |
|
| 172 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 173 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 174 | - //check if such a relationship already exists |
|
| 175 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
| 176 | - $this->get_this_model()->get_this_model_name(), |
|
| 177 | - 'ID'); |
|
| 178 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
| 179 | - $this->get_this_model()->get_this_model_name(), |
|
| 180 | - 'name'); |
|
| 181 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
| 182 | - $this->get_other_model()->get_this_model_name(), |
|
| 183 | - 'ID'); |
|
| 184 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
| 185 | - $this->get_other_model()->get_this_model_name(), |
|
| 186 | - 'name'); |
|
| 159 | + /** |
|
| 160 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 161 | + * |
|
| 162 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 163 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 164 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 165 | + * checking existing values and for setting new rows if |
|
| 166 | + * no exact matches. |
|
| 167 | + * @return EE_Base_Class |
|
| 168 | + * @throws \EE_Error |
|
| 169 | + */ |
|
| 170 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 171 | + { |
|
| 172 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 173 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 174 | + //check if such a relationship already exists |
|
| 175 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
| 176 | + $this->get_this_model()->get_this_model_name(), |
|
| 177 | + 'ID'); |
|
| 178 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
| 179 | + $this->get_this_model()->get_this_model_name(), |
|
| 180 | + 'name'); |
|
| 181 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
| 182 | + $this->get_other_model()->get_this_model_name(), |
|
| 183 | + 'ID'); |
|
| 184 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
| 185 | + $this->get_other_model()->get_this_model_name(), |
|
| 186 | + 'name'); |
|
| 187 | 187 | |
| 188 | - $cols_n_values = array( |
|
| 189 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 190 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
| 191 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 192 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
| 193 | - ); |
|
| 188 | + $cols_n_values = array( |
|
| 189 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 190 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
| 191 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 192 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
| 193 | + ); |
|
| 194 | 194 | |
| 195 | - //if $where_query exists lets add them to the query_params. |
|
| 196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 197 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 198 | - //make sure we strip THIS models name from the query param |
|
| 199 | - $parsed_query = array(); |
|
| 200 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 202 | - $query_param); |
|
| 203 | - $parsed_query[$query_param] = $val; |
|
| 204 | - } |
|
| 205 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 206 | - } |
|
| 195 | + //if $where_query exists lets add them to the query_params. |
|
| 196 | + if (! empty($extra_join_model_fields_n_values)) { |
|
| 197 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 198 | + //make sure we strip THIS models name from the query param |
|
| 199 | + $parsed_query = array(); |
|
| 200 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 202 | + $query_param); |
|
| 203 | + $parsed_query[$query_param] = $val; |
|
| 204 | + } |
|
| 205 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - $query_params = array($cols_n_values); |
|
| 208 | + $query_params = array($cols_n_values); |
|
| 209 | 209 | |
| 210 | 210 | |
| 211 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
| 212 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
| 213 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 214 | - //the other tables, use the joining model directly! |
|
| 215 | - if (! $existing_entry_in_join_table) { |
|
| 216 | - $this->get_join_model()->insert($cols_n_values); |
|
| 217 | - } |
|
| 218 | - return $other_model_obj; |
|
| 219 | - } |
|
| 211 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
| 212 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
| 213 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 214 | + //the other tables, use the joining model directly! |
|
| 215 | + if (! $existing_entry_in_join_table) { |
|
| 216 | + $this->get_join_model()->insert($cols_n_values); |
|
| 217 | + } |
|
| 218 | + return $other_model_obj; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 224 | - * |
|
| 225 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 226 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 227 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 228 | - * values and for removing existing rows if exact matches exist. |
|
| 229 | - * @return EE_Base_Class |
|
| 230 | - * @throws \EE_Error |
|
| 231 | - */ |
|
| 232 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 233 | - { |
|
| 234 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 235 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 236 | - //check if such a relationship already exists |
|
| 237 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
| 238 | - $this->get_this_model()->get_this_model_name(), |
|
| 239 | - 'ID'); |
|
| 240 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
| 241 | - $this->get_this_model()->get_this_model_name(), |
|
| 242 | - 'name'); |
|
| 243 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
| 244 | - $this->get_other_model()->get_this_model_name(), |
|
| 245 | - 'ID'); |
|
| 246 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
| 247 | - $this->get_other_model()->get_this_model_name(), |
|
| 248 | - 'name'); |
|
| 222 | + /** |
|
| 223 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 224 | + * |
|
| 225 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 226 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 227 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 228 | + * values and for removing existing rows if exact matches exist. |
|
| 229 | + * @return EE_Base_Class |
|
| 230 | + * @throws \EE_Error |
|
| 231 | + */ |
|
| 232 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 233 | + { |
|
| 234 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 235 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 236 | + //check if such a relationship already exists |
|
| 237 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
| 238 | + $this->get_this_model()->get_this_model_name(), |
|
| 239 | + 'ID'); |
|
| 240 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
| 241 | + $this->get_this_model()->get_this_model_name(), |
|
| 242 | + 'name'); |
|
| 243 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
| 244 | + $this->get_other_model()->get_this_model_name(), |
|
| 245 | + 'ID'); |
|
| 246 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
| 247 | + $this->get_other_model()->get_this_model_name(), |
|
| 248 | + 'name'); |
|
| 249 | 249 | |
| 250 | - $cols_n_values = array( |
|
| 251 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 252 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
| 253 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 254 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
| 255 | - ); |
|
| 250 | + $cols_n_values = array( |
|
| 251 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 252 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
| 253 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 254 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
| 255 | + ); |
|
| 256 | 256 | |
| 257 | - //if $where_query exists lets add them to the query_params. |
|
| 258 | - if (! empty($where_query)) { |
|
| 259 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 260 | - //make sure we strip THIS models name from the query param |
|
| 261 | - $parsed_query = array(); |
|
| 262 | - foreach ($where_query as $query_param => $val) { |
|
| 263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 264 | - $query_param); |
|
| 265 | - $parsed_query[$query_param] = $val; |
|
| 266 | - } |
|
| 267 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 268 | - } |
|
| 257 | + //if $where_query exists lets add them to the query_params. |
|
| 258 | + if (! empty($where_query)) { |
|
| 259 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 260 | + //make sure we strip THIS models name from the query param |
|
| 261 | + $parsed_query = array(); |
|
| 262 | + foreach ($where_query as $query_param => $val) { |
|
| 263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 264 | + $query_param); |
|
| 265 | + $parsed_query[$query_param] = $val; |
|
| 266 | + } |
|
| 267 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
| 271 | - return $other_model_obj; |
|
| 272 | - } |
|
| 270 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
| 271 | + return $other_model_obj; |
|
| 272 | + } |
|
| 273 | 273 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | } else { |
| 63 | 63 | $order = 'second'; |
| 64 | 64 | } |
| 65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
| 65 | + return $this->get_join_model()->field_settings_for('EXJ_'.$order.'_model_'.$id_or_name_field); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | |
@@ -90,9 +90,9 @@ discard block |
||
| 90 | 90 | $this->get_this_model()->get_this_model_name(), |
| 91 | 91 | 'name'); |
| 92 | 92 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 93 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
| 94 | 94 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 95 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 96 | 96 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
| 97 | 97 | //phew! ok, we have all the info we need, now we can create the SQL join string |
| 98 | 98 | $SQL = $this->_left_join( |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | $join_table_fk_field_to_this_table->get_table_column(), |
| 102 | 102 | $this_table_alias, |
| 103 | 103 | $this_table_pk_field->get_table_column(), |
| 104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
| 104 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name()."'"). |
|
| 105 | 105 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
| 106 | 106 | |
| 107 | 107 | return $SQL; |
@@ -136,11 +136,11 @@ discard block |
||
| 136 | 136 | 'name'); |
| 137 | 137 | |
| 138 | 138 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
| 139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 139 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 140 | 140 | |
| 141 | 141 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 142 | 142 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 143 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
| 144 | 144 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
| 145 | 145 | |
| 146 | 146 | $SQL = $this->_left_join( |
@@ -149,8 +149,8 @@ discard block |
||
| 149 | 149 | $other_table_pk_field->get_table_column(), |
| 150 | 150 | $join_table_alias, |
| 151 | 151 | $join_table_fk_field_to_other_table->get_table_column(), |
| 152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
| 153 | - ) . |
|
| 152 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
| 153 | + ). |
|
| 154 | 154 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
| 155 | 155 | return $SQL; |
| 156 | 156 | } |
@@ -193,12 +193,12 @@ discard block |
||
| 193 | 193 | ); |
| 194 | 194 | |
| 195 | 195 | //if $where_query exists lets add them to the query_params. |
| 196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 196 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
| 197 | 197 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 198 | 198 | //make sure we strip THIS models name from the query param |
| 199 | 199 | $parsed_query = array(); |
| 200 | 200 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
| 201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
| 202 | 202 | $query_param); |
| 203 | 203 | $parsed_query[$query_param] = $val; |
| 204 | 204 | } |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | //if there is already an entry in the join table, indicating a relationship, we're done |
| 213 | 213 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
| 214 | 214 | //the other tables, use the joining model directly! |
| 215 | - if (! $existing_entry_in_join_table) { |
|
| 215 | + if ( ! $existing_entry_in_join_table) { |
|
| 216 | 216 | $this->get_join_model()->insert($cols_n_values); |
| 217 | 217 | } |
| 218 | 218 | return $other_model_obj; |
@@ -255,12 +255,12 @@ discard block |
||
| 255 | 255 | ); |
| 256 | 256 | |
| 257 | 257 | //if $where_query exists lets add them to the query_params. |
| 258 | - if (! empty($where_query)) { |
|
| 258 | + if ( ! empty($where_query)) { |
|
| 259 | 259 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 260 | 260 | //make sure we strip THIS models name from the query param |
| 261 | 261 | $parsed_query = array(); |
| 262 | 262 | foreach ($where_query as $query_param => $val) { |
| 263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
| 264 | 264 | $query_param); |
| 265 | 265 | $parsed_query[$query_param] = $val; |
| 266 | 266 | } |
@@ -18,83 +18,83 @@ |
||
| 18 | 18 | { |
| 19 | 19 | |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * get_join_statement |
|
| 23 | - * |
|
| 24 | - * @param string $model_relation_chain |
|
| 25 | - * @return string |
|
| 26 | - * @throws \EE_Error |
|
| 27 | - */ |
|
| 28 | - public function get_join_statement($model_relation_chain) |
|
| 29 | - { |
|
| 30 | - //create the sql string like |
|
| 31 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 32 | - //ALSO, need to get the field with the model name |
|
| 33 | - $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
| 21 | + /** |
|
| 22 | + * get_join_statement |
|
| 23 | + * |
|
| 24 | + * @param string $model_relation_chain |
|
| 25 | + * @return string |
|
| 26 | + * @throws \EE_Error |
|
| 27 | + */ |
|
| 28 | + public function get_join_statement($model_relation_chain) |
|
| 29 | + { |
|
| 30 | + //create the sql string like |
|
| 31 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 32 | + //ALSO, need to get the field with the model name |
|
| 33 | + $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 37 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 41 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 42 | - return $this->_left_join($other_table, |
|
| 43 | - $other_table_alias, |
|
| 44 | - $other_table_pk_field->get_table_column(), |
|
| 45 | - $this_table_alias, |
|
| 46 | - $this_table_fk_field->get_table_column(), |
|
| 47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
| 48 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 49 | - } |
|
| 36 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 37 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 38 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 40 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 41 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 42 | + return $this->_left_join($other_table, |
|
| 43 | + $other_table_alias, |
|
| 44 | + $other_table_pk_field->get_table_column(), |
|
| 45 | + $this_table_alias, |
|
| 46 | + $this_table_fk_field->get_table_column(), |
|
| 47 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
| 48 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
| 54 | - * you like. |
|
| 55 | - * |
|
| 56 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 57 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 58 | - * @param array $extra_join_model_fields_n_values |
|
| 59 | - * @return \EE_Base_Class |
|
| 60 | - * @throws \EE_Error |
|
| 61 | - */ |
|
| 62 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 63 | - { |
|
| 64 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 65 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 66 | - //find the field on THIS model which a foreign key to the other model |
|
| 67 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 68 | - //set that field on the other model to this model's ID |
|
| 69 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
| 70 | - //and make sure this model's field with the foreign model name is set to the correct value |
|
| 71 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
| 72 | - $this->get_other_model()->get_this_model_name()); |
|
| 73 | - $this_model_obj->save(); |
|
| 74 | - return $other_model_obj; |
|
| 75 | - } |
|
| 52 | + /** |
|
| 53 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
| 54 | + * you like. |
|
| 55 | + * |
|
| 56 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 57 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 58 | + * @param array $extra_join_model_fields_n_values |
|
| 59 | + * @return \EE_Base_Class |
|
| 60 | + * @throws \EE_Error |
|
| 61 | + */ |
|
| 62 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 63 | + { |
|
| 64 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 65 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 66 | + //find the field on THIS model which a foreign key to the other model |
|
| 67 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 68 | + //set that field on the other model to this model's ID |
|
| 69 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
| 70 | + //and make sure this model's field with the foreign model name is set to the correct value |
|
| 71 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
| 72 | + $this->get_other_model()->get_this_model_name()); |
|
| 73 | + $this_model_obj->save(); |
|
| 74 | + return $other_model_obj; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
| 80 | - * |
|
| 81 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 82 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 83 | - * @param array $where_query |
|
| 84 | - * @return \EE_Base_Class |
|
| 85 | - * @throws \EE_Error |
|
| 86 | - */ |
|
| 87 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 88 | - { |
|
| 89 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 90 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 91 | - //find the field on the other model which is a foreign key to this model |
|
| 92 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 93 | - //set that field on the other model to this model's ID |
|
| 94 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
| 95 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
| 96 | - true); |
|
| 97 | - $this_model_obj->save(); |
|
| 98 | - return $other_model_obj; |
|
| 99 | - } |
|
| 78 | + /** |
|
| 79 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
| 80 | + * |
|
| 81 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 82 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 83 | + * @param array $where_query |
|
| 84 | + * @return \EE_Base_Class |
|
| 85 | + * @throws \EE_Error |
|
| 86 | + */ |
|
| 87 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 88 | + { |
|
| 89 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 90 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 91 | + //find the field on the other model which is a foreign key to this model |
|
| 92 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 93 | + //set that field on the other model to this model's ID |
|
| 94 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
| 95 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
| 96 | + true); |
|
| 97 | + $this_model_obj->save(); |
|
| 98 | + return $other_model_obj; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(EE_MODELS . 'relations/EE_Belongs_To_Relation.php'); |
|
| 2 | +require_once(EE_MODELS.'relations/EE_Belongs_To_Relation.php'); |
|
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | /** |
@@ -35,16 +35,16 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 37 | 37 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 38 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
| 39 | 39 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 40 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
| 41 | 41 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
| 42 | 42 | return $this->_left_join($other_table, |
| 43 | 43 | $other_table_alias, |
| 44 | 44 | $other_table_pk_field->get_table_column(), |
| 45 | 45 | $this_table_alias, |
| 46 | 46 | $this_table_fk_field->get_table_column(), |
| 47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
| 47 | + $field_with_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'") |
|
| 48 | 48 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
| 3 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -68,18 +68,18 @@ discard block |
||
| 68 | 68 | //remember the model relation chain to the JOIN model, because we'll |
| 69 | 69 | //need it for get_join_statement() |
| 70 | 70 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
| 71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); //get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 72 | 72 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
| 73 | 73 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 74 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
| 75 | 75 | |
| 76 | 76 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 77 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 78 | 78 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
| 79 | 79 | //phew! ok, we have all the info we need, now we can create the SQL join string |
| 80 | 80 | $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
| 81 | 81 | $this_table_alias, |
| 82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 82 | + $this_table_pk_field->get_table_column()).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 83 | 83 | |
| 84 | 84 | return $SQL; |
| 85 | 85 | } |
@@ -104,16 +104,16 @@ discard block |
||
| 104 | 104 | } |
| 105 | 105 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
| 106 | 106 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
| 107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 107 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 108 | 108 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 109 | 109 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
| 110 | 110 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 111 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
| 112 | 112 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
| 113 | 113 | |
| 114 | 114 | $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
| 115 | 115 | $join_table_alias, |
| 116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 116 | + $join_table_fk_field_to_other_table->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 117 | 117 | return $SQL; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | 148 | //if $where_query exists lets add them to the query_params. |
| 149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 149 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
| 150 | 150 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 151 | 151 | //make sure we strip THIS models name from the query param |
| 152 | 152 | $parsed_query = array(); |
| 153 | 153 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
| 154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
| 155 | 155 | $query_param); |
| 156 | 156 | $parsed_query[$query_param] = $val; |
| 157 | 157 | } |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | //if there is already an entry in the join table, indicating a relationship, we're done |
| 166 | 166 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
| 167 | 167 | //the other tables, use the joining model directly! |
| 168 | - if (! $existing_entry_in_join_table) { |
|
| 168 | + if ( ! $existing_entry_in_join_table) { |
|
| 169 | 169 | $this->get_join_model()->insert($cols_n_values); |
| 170 | 170 | } |
| 171 | 171 | return $other_model_obj; |
@@ -196,12 +196,12 @@ discard block |
||
| 196 | 196 | ); |
| 197 | 197 | |
| 198 | 198 | //if $where_query exists lets add them to the query_params. |
| 199 | - if (! empty($where_query)) { |
|
| 199 | + if ( ! empty($where_query)) { |
|
| 200 | 200 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 201 | 201 | //make sure we strip THIS models name from the query param |
| 202 | 202 | $parsed_query = array(); |
| 203 | 203 | foreach ($where_query as $query_param => $val) { |
| 204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
| 205 | 205 | $query_param); |
| 206 | 206 | $parsed_query[$query_param] = $val; |
| 207 | 207 | } |
@@ -12,213 +12,213 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 17 | - * which joins EE_Event and EE_Question_Group |
|
| 18 | - * |
|
| 19 | - * @var EEM_Base |
|
| 20 | - */ |
|
| 21 | - protected $_joining_model_name; |
|
| 22 | - |
|
| 23 | - protected $_model_relation_chain_to_join_model; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 28 | - * (and an ee joining-model.) This knows how to join the models, |
|
| 29 | - * get related models across the relation, and add-and-remove the relationships. |
|
| 30 | - * |
|
| 31 | - * @param bool $joining_model_name |
|
| 32 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 33 | - * are related models across this relation, block (prevent and add an |
|
| 34 | - * error) the deletion of this model |
|
| 35 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 36 | - * default |
|
| 37 | - */ |
|
| 38 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 39 | - { |
|
| 40 | - $this->_joining_model_name = $joining_model_name; |
|
| 41 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Gets the joining model's object |
|
| 46 | - * |
|
| 47 | - * @return EEM_Base |
|
| 48 | - */ |
|
| 49 | - public function get_join_model() |
|
| 50 | - { |
|
| 51 | - return $this->_get_model($this->_joining_model_name); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 57 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 58 | - * |
|
| 59 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 60 | - * @return string of SQL |
|
| 61 | - * @throws \EE_Error |
|
| 62 | - */ |
|
| 63 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 64 | - { |
|
| 65 | - //create sql like |
|
| 66 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 67 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 68 | - //remember the model relation chain to the JOIN model, because we'll |
|
| 69 | - //need it for get_join_statement() |
|
| 70 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 72 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 73 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 75 | - |
|
| 76 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 78 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 79 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 80 | - $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
| 81 | - $this_table_alias, |
|
| 82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 83 | - |
|
| 84 | - return $SQL; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 90 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 91 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 92 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 93 | - * the intermediate join table, and then to the other model's pk's table |
|
| 94 | - * |
|
| 95 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 96 | - * @return string of SQL |
|
| 97 | - * @throws \EE_Error |
|
| 98 | - */ |
|
| 99 | - public function get_join_statement($model_relation_chain) |
|
| 100 | - { |
|
| 101 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
| 102 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 103 | - 'event_espresso'))); |
|
| 104 | - } |
|
| 105 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 106 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
| 107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 108 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 109 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 110 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 112 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 113 | - |
|
| 114 | - $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
| 115 | - $join_table_alias, |
|
| 116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 117 | - return $SQL; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 123 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 124 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 125 | - * columns' values |
|
| 126 | - * |
|
| 127 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 128 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 129 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 130 | - * checking existing values and for setting new rows if |
|
| 131 | - * no exact matches. |
|
| 132 | - * @return EE_Base_Class |
|
| 133 | - * @throws \EE_Error |
|
| 134 | - */ |
|
| 135 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 136 | - { |
|
| 137 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 138 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 139 | - //check if such a relationship already exists |
|
| 140 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 141 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 142 | - |
|
| 143 | - $cols_n_values = array( |
|
| 144 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 145 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - //if $where_query exists lets add them to the query_params. |
|
| 149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 150 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 151 | - //make sure we strip THIS models name from the query param |
|
| 152 | - $parsed_query = array(); |
|
| 153 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 155 | - $query_param); |
|
| 156 | - $parsed_query[$query_param] = $val; |
|
| 157 | - } |
|
| 158 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $query_params = array($cols_n_values); |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
| 165 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
| 166 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 167 | - //the other tables, use the joining model directly! |
|
| 168 | - if (! $existing_entry_in_join_table) { |
|
| 169 | - $this->get_join_model()->insert($cols_n_values); |
|
| 170 | - } |
|
| 171 | - return $other_model_obj; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 177 | - * |
|
| 178 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 179 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 180 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 181 | - * values and for removing existing rows if exact matches exist. |
|
| 182 | - * @return EE_Base_Class |
|
| 183 | - * @throws \EE_Error |
|
| 184 | - */ |
|
| 185 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 186 | - { |
|
| 187 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 188 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 189 | - //check if such a relationship already exists |
|
| 190 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 191 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 192 | - |
|
| 193 | - $cols_n_values = array( |
|
| 194 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 195 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 196 | - ); |
|
| 197 | - |
|
| 198 | - //if $where_query exists lets add them to the query_params. |
|
| 199 | - if (! empty($where_query)) { |
|
| 200 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 201 | - //make sure we strip THIS models name from the query param |
|
| 202 | - $parsed_query = array(); |
|
| 203 | - foreach ($where_query as $query_param => $val) { |
|
| 204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 205 | - $query_param); |
|
| 206 | - $parsed_query[$query_param] = $val; |
|
| 207 | - } |
|
| 208 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
| 212 | - return $other_model_obj; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - public function get_schema() { |
|
| 217 | - $schema = parent::get_schema(); |
|
| 218 | - $schema['properties']['joining_model_name'] = array( |
|
| 219 | - 'type' => 'string', |
|
| 220 | - 'enum' => array($this->get_join_model()->get_this_model_name()) |
|
| 221 | - ); |
|
| 222 | - return $schema; |
|
| 223 | - } |
|
| 15 | + /** |
|
| 16 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 17 | + * which joins EE_Event and EE_Question_Group |
|
| 18 | + * |
|
| 19 | + * @var EEM_Base |
|
| 20 | + */ |
|
| 21 | + protected $_joining_model_name; |
|
| 22 | + |
|
| 23 | + protected $_model_relation_chain_to_join_model; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 28 | + * (and an ee joining-model.) This knows how to join the models, |
|
| 29 | + * get related models across the relation, and add-and-remove the relationships. |
|
| 30 | + * |
|
| 31 | + * @param bool $joining_model_name |
|
| 32 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 33 | + * are related models across this relation, block (prevent and add an |
|
| 34 | + * error) the deletion of this model |
|
| 35 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 36 | + * default |
|
| 37 | + */ |
|
| 38 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 39 | + { |
|
| 40 | + $this->_joining_model_name = $joining_model_name; |
|
| 41 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Gets the joining model's object |
|
| 46 | + * |
|
| 47 | + * @return EEM_Base |
|
| 48 | + */ |
|
| 49 | + public function get_join_model() |
|
| 50 | + { |
|
| 51 | + return $this->_get_model($this->_joining_model_name); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 57 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 58 | + * |
|
| 59 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 60 | + * @return string of SQL |
|
| 61 | + * @throws \EE_Error |
|
| 62 | + */ |
|
| 63 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 64 | + { |
|
| 65 | + //create sql like |
|
| 66 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 67 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 68 | + //remember the model relation chain to the JOIN model, because we'll |
|
| 69 | + //need it for get_join_statement() |
|
| 70 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 72 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 73 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 74 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
| 75 | + |
|
| 76 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 77 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 78 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 79 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 80 | + $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
| 81 | + $this_table_alias, |
|
| 82 | + $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 83 | + |
|
| 84 | + return $SQL; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 90 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 91 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 92 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 93 | + * the intermediate join table, and then to the other model's pk's table |
|
| 94 | + * |
|
| 95 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 96 | + * @return string of SQL |
|
| 97 | + * @throws \EE_Error |
|
| 98 | + */ |
|
| 99 | + public function get_join_statement($model_relation_chain) |
|
| 100 | + { |
|
| 101 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
| 102 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 103 | + 'event_espresso'))); |
|
| 104 | + } |
|
| 105 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 106 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
| 107 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 108 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 109 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 110 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 111 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 112 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 113 | + |
|
| 114 | + $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
| 115 | + $join_table_alias, |
|
| 116 | + $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 117 | + return $SQL; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 123 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 124 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 125 | + * columns' values |
|
| 126 | + * |
|
| 127 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 128 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 129 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 130 | + * checking existing values and for setting new rows if |
|
| 131 | + * no exact matches. |
|
| 132 | + * @return EE_Base_Class |
|
| 133 | + * @throws \EE_Error |
|
| 134 | + */ |
|
| 135 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 136 | + { |
|
| 137 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 138 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 139 | + //check if such a relationship already exists |
|
| 140 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 141 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 142 | + |
|
| 143 | + $cols_n_values = array( |
|
| 144 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 145 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + //if $where_query exists lets add them to the query_params. |
|
| 149 | + if (! empty($extra_join_model_fields_n_values)) { |
|
| 150 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 151 | + //make sure we strip THIS models name from the query param |
|
| 152 | + $parsed_query = array(); |
|
| 153 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 155 | + $query_param); |
|
| 156 | + $parsed_query[$query_param] = $val; |
|
| 157 | + } |
|
| 158 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $query_params = array($cols_n_values); |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
| 165 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
| 166 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 167 | + //the other tables, use the joining model directly! |
|
| 168 | + if (! $existing_entry_in_join_table) { |
|
| 169 | + $this->get_join_model()->insert($cols_n_values); |
|
| 170 | + } |
|
| 171 | + return $other_model_obj; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 177 | + * |
|
| 178 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 179 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 180 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 181 | + * values and for removing existing rows if exact matches exist. |
|
| 182 | + * @return EE_Base_Class |
|
| 183 | + * @throws \EE_Error |
|
| 184 | + */ |
|
| 185 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 186 | + { |
|
| 187 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 188 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 189 | + //check if such a relationship already exists |
|
| 190 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 191 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 192 | + |
|
| 193 | + $cols_n_values = array( |
|
| 194 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 195 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 196 | + ); |
|
| 197 | + |
|
| 198 | + //if $where_query exists lets add them to the query_params. |
|
| 199 | + if (! empty($where_query)) { |
|
| 200 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 201 | + //make sure we strip THIS models name from the query param |
|
| 202 | + $parsed_query = array(); |
|
| 203 | + foreach ($where_query as $query_param => $val) { |
|
| 204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
| 205 | + $query_param); |
|
| 206 | + $parsed_query[$query_param] = $val; |
|
| 207 | + } |
|
| 208 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
| 212 | + return $other_model_obj; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + public function get_schema() { |
|
| 217 | + $schema = parent::get_schema(); |
|
| 218 | + $schema['properties']['joining_model_name'] = array( |
|
| 219 | + 'type' => 'string', |
|
| 220 | + 'enum' => array($this->get_join_model()->get_this_model_name()) |
|
| 221 | + ); |
|
| 222 | + return $schema; |
|
| 223 | + } |
|
| 224 | 224 | } |
@@ -13,128 +13,128 @@ |
||
| 13 | 13 | class EE_Belongs_To_Relation extends EE_Model_Relation_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
| 18 | - * to the other model. This knows how to join the models, |
|
| 19 | - * get related models across the relation, and add-and-remove the relationships. |
|
| 20 | - * |
|
| 21 | - * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
| 22 | - * default. if there are related models across this |
|
| 23 | - * relation, block (prevent and add an error) the |
|
| 24 | - * deletion of this model |
|
| 25 | - * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
| 26 | - * instead of the default |
|
| 27 | - */ |
|
| 28 | - public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
| 29 | - { |
|
| 30 | - parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
| 31 | - } |
|
| 16 | + /** |
|
| 17 | + * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
| 18 | + * to the other model. This knows how to join the models, |
|
| 19 | + * get related models across the relation, and add-and-remove the relationships. |
|
| 20 | + * |
|
| 21 | + * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
| 22 | + * default. if there are related models across this |
|
| 23 | + * relation, block (prevent and add an error) the |
|
| 24 | + * deletion of this model |
|
| 25 | + * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
| 26 | + * instead of the default |
|
| 27 | + */ |
|
| 28 | + public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
| 29 | + { |
|
| 30 | + parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * get_join_statement |
|
| 36 | - * |
|
| 37 | - * @param string $model_relation_chain |
|
| 38 | - * @return string |
|
| 39 | - * @throws \EE_Error |
|
| 40 | - */ |
|
| 41 | - public function get_join_statement($model_relation_chain) |
|
| 42 | - { |
|
| 43 | - //create the sql string like |
|
| 44 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 45 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 46 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 48 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 50 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 51 | - return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
| 52 | - $this_table_alias, |
|
| 53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 54 | - } |
|
| 34 | + /** |
|
| 35 | + * get_join_statement |
|
| 36 | + * |
|
| 37 | + * @param string $model_relation_chain |
|
| 38 | + * @return string |
|
| 39 | + * @throws \EE_Error |
|
| 40 | + */ |
|
| 41 | + public function get_join_statement($model_relation_chain) |
|
| 42 | + { |
|
| 43 | + //create the sql string like |
|
| 44 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 45 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 46 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 47 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 48 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 49 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 50 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 51 | + return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
| 52 | + $this_table_alias, |
|
| 53 | + $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
| 59 | - * you like. |
|
| 60 | - * |
|
| 61 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 62 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 63 | - * @param array $extra_join_model_fields_n_values |
|
| 64 | - * @return \EE_Base_Class |
|
| 65 | - * @throws \EE_Error |
|
| 66 | - */ |
|
| 67 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 68 | - { |
|
| 69 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 70 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 71 | - //find the field on the other model which is a foreign key to this model |
|
| 72 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 73 | - if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
| 74 | - //set that field on the other model to this model's ID |
|
| 75 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
| 76 | - $this_model_obj->save(); |
|
| 77 | - } |
|
| 78 | - return $other_model_obj; |
|
| 79 | - } |
|
| 57 | + /** |
|
| 58 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
| 59 | + * you like. |
|
| 60 | + * |
|
| 61 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 62 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 63 | + * @param array $extra_join_model_fields_n_values |
|
| 64 | + * @return \EE_Base_Class |
|
| 65 | + * @throws \EE_Error |
|
| 66 | + */ |
|
| 67 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 68 | + { |
|
| 69 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 70 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 71 | + //find the field on the other model which is a foreign key to this model |
|
| 72 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 73 | + if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
| 74 | + //set that field on the other model to this model's ID |
|
| 75 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
| 76 | + $this_model_obj->save(); |
|
| 77 | + } |
|
| 78 | + return $other_model_obj; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
| 84 | - * |
|
| 85 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 86 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 87 | - * @param array $where_query |
|
| 88 | - * @return \EE_Base_Class |
|
| 89 | - * @throws \EE_Error |
|
| 90 | - */ |
|
| 91 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 92 | - { |
|
| 93 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 95 | - //find the field on the other model which is a foreign key to this model |
|
| 96 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 97 | - //set that field on the other model to this model's ID |
|
| 98 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
| 99 | - $this_model_obj->save(); |
|
| 100 | - return $other_model_obj; |
|
| 101 | - } |
|
| 82 | + /** |
|
| 83 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
| 84 | + * |
|
| 85 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 86 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 87 | + * @param array $where_query |
|
| 88 | + * @return \EE_Base_Class |
|
| 89 | + * @throws \EE_Error |
|
| 90 | + */ |
|
| 91 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 92 | + { |
|
| 93 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
| 95 | + //find the field on the other model which is a foreign key to this model |
|
| 96 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 97 | + //set that field on the other model to this model's ID |
|
| 98 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
| 99 | + $this_model_obj->save(); |
|
| 100 | + return $other_model_obj; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
| 106 | - * |
|
| 107 | - * @param EE_Base_Class $model_obj_or_id |
|
| 108 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 109 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
| 110 | - * @return EE_Base_Class[] |
|
| 111 | - * @throws \EE_Error |
|
| 112 | - */ |
|
| 113 | - public function get_all_related( |
|
| 114 | - $model_obj_or_id, |
|
| 115 | - $query_params = array(), |
|
| 116 | - $values_already_prepared_by_model_object = false |
|
| 117 | - ) { |
|
| 118 | - if ($values_already_prepared_by_model_object !== false) { |
|
| 119 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
| 120 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
| 121 | - '4.8.1'); |
|
| 122 | - } |
|
| 123 | - //get column on this model object which is a foreign key to the other model |
|
| 124 | - $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 125 | - //get its value |
|
| 126 | - if ($model_obj_or_id instanceof EE_Base_Class) { |
|
| 127 | - $model_obj = $model_obj_or_id; |
|
| 128 | - } else { |
|
| 129 | - $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
| 130 | - } |
|
| 131 | - $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
| 132 | - //get all where their PK matches that value |
|
| 133 | - $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
| 134 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
| 104 | + /** |
|
| 105 | + * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
| 106 | + * |
|
| 107 | + * @param EE_Base_Class $model_obj_or_id |
|
| 108 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 109 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
| 110 | + * @return EE_Base_Class[] |
|
| 111 | + * @throws \EE_Error |
|
| 112 | + */ |
|
| 113 | + public function get_all_related( |
|
| 114 | + $model_obj_or_id, |
|
| 115 | + $query_params = array(), |
|
| 116 | + $values_already_prepared_by_model_object = false |
|
| 117 | + ) { |
|
| 118 | + if ($values_already_prepared_by_model_object !== false) { |
|
| 119 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
| 120 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
| 121 | + '4.8.1'); |
|
| 122 | + } |
|
| 123 | + //get column on this model object which is a foreign key to the other model |
|
| 124 | + $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 125 | + //get its value |
|
| 126 | + if ($model_obj_or_id instanceof EE_Base_Class) { |
|
| 127 | + $model_obj = $model_obj_or_id; |
|
| 128 | + } else { |
|
| 129 | + $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
| 130 | + } |
|
| 131 | + $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
| 132 | + //get all where their PK matches that value |
|
| 133 | + $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
| 134 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
| 135 | 135 | // echo '$query_params'; |
| 136 | 136 | // var_dump($query_params); |
| 137 | - return $this->get_other_model()->get_all($query_params); |
|
| 138 | - } |
|
| 137 | + return $this->get_other_model()->get_all($query_params); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
| 2 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | 5 | * Class EE_Belongs_To_Relation |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
| 45 | 45 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 46 | 46 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
| 47 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
| 48 | 48 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
| 49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
| 49 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
| 50 | 50 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
| 51 | 51 | return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
| 52 | 52 | $this_table_alias, |
| 53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 53 | + $this_table_fk_field->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | |
@@ -127,9 +127,9 @@ discard block |
||
| 127 | 127 | protected $_table_analysis; |
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | - * @var EE_Data_Migration_Manager $_instance |
|
| 130 | + * @var EE_Data_Migration_Manager $_instance |
|
| 131 | 131 | * @access private |
| 132 | - */ |
|
| 132 | + */ |
|
| 133 | 133 | private static $_instance = NULL; |
| 134 | 134 | |
| 135 | 135 | |
@@ -720,18 +720,18 @@ discard block |
||
| 720 | 720 | |
| 721 | 721 | |
| 722 | 722 | |
| 723 | - /** |
|
| 724 | - * Gets all the data migration scripts available in the core folder and folders |
|
| 725 | - * in addons. Has the side effect of adding them for autoloading |
|
| 726 | - * |
|
| 727 | - * @return array keys are expected classnames, values are their filepaths |
|
| 728 | - * @throws \EE_Error |
|
| 729 | - */ |
|
| 723 | + /** |
|
| 724 | + * Gets all the data migration scripts available in the core folder and folders |
|
| 725 | + * in addons. Has the side effect of adding them for autoloading |
|
| 726 | + * |
|
| 727 | + * @return array keys are expected classnames, values are their filepaths |
|
| 728 | + * @throws \EE_Error |
|
| 729 | + */ |
|
| 730 | 730 | public function get_all_data_migration_scripts_available(){ |
| 731 | 731 | if( ! $this->_data_migration_class_to_filepath_map){ |
| 732 | 732 | $this->_data_migration_class_to_filepath_map = array(); |
| 733 | 733 | foreach($this->get_data_migration_script_folders() as $folder_path){ |
| 734 | - $folder_path = EEH_File::end_with_directory_separator($folder_path); |
|
| 734 | + $folder_path = EEH_File::end_with_directory_separator($folder_path); |
|
| 735 | 735 | $files = glob( $folder_path. '*.dms.php' ); |
| 736 | 736 | |
| 737 | 737 | if ( empty( $files ) ) { |
@@ -748,15 +748,15 @@ discard block |
||
| 748 | 748 | if( $slug !== 'Core' ){ |
| 749 | 749 | if( ! EE_Registry::instance()->get_addon_by_name( $slug ) ) { |
| 750 | 750 | EE_Error::doing_it_wrong( |
| 751 | - __FUNCTION__, |
|
| 752 | - sprintf( |
|
| 753 | - __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
| 754 | - $classname, |
|
| 755 | - $slug, |
|
| 756 | - implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
| 757 | - ), |
|
| 758 | - '4.3.0.alpha.019' |
|
| 759 | - ); |
|
| 751 | + __FUNCTION__, |
|
| 752 | + sprintf( |
|
| 753 | + __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
| 754 | + $classname, |
|
| 755 | + $slug, |
|
| 756 | + implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
| 757 | + ), |
|
| 758 | + '4.3.0.alpha.019' |
|
| 759 | + ); |
|
| 760 | 760 | } |
| 761 | 761 | } |
| 762 | 762 | $this->_data_migration_class_to_filepath_map[$classname] = $file; |
@@ -939,12 +939,12 @@ discard block |
||
| 939 | 939 | } |
| 940 | 940 | |
| 941 | 941 | /** |
| 942 | - * Resets the borked data migration scripts so they're no longer borked |
|
| 943 | - * so we can again attempt to migrate |
|
| 944 | - * |
|
| 945 | - * @return bool |
|
| 946 | - * @throws \EE_Error |
|
| 947 | - */ |
|
| 942 | + * Resets the borked data migration scripts so they're no longer borked |
|
| 943 | + * so we can again attempt to migrate |
|
| 944 | + * |
|
| 945 | + * @return bool |
|
| 946 | + * @throws \EE_Error |
|
| 947 | + */ |
|
| 948 | 948 | public function reattempt(){ |
| 949 | 949 | //find if the last-ran script was borked |
| 950 | 950 | //set it as being non-borked (we shouldn't ever get DMSs that we don't recognize) |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * to indicate it successfully migrate some data, and has nothing left to do; or EE_Data_Migration_Manager::status_fatal_error to indicate |
| 25 | 25 | * an error occurred which means the ajax script should probably stop executing. |
| 26 | 26 | */ |
| 27 | -class EE_Data_Migration_Manager{ |
|
| 27 | +class EE_Data_Migration_Manager { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * of this EE installation. Keys should be the name of the version the script upgraded to |
| 82 | 82 | * @var EE_Data_Migration_Script_Base[] |
| 83 | 83 | */ |
| 84 | - private $_data_migrations_ran =null; |
|
| 84 | + private $_data_migrations_ran = null; |
|
| 85 | 85 | /** |
| 86 | 86 | * The last ran script. It's nice to store this somewhere accessible, as its easiest |
| 87 | 87 | * to know which was the last run by which is the newest wp option; but in most of the code |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | */ |
| 142 | 142 | public static function instance() { |
| 143 | 143 | // check if class object is instantiated |
| 144 | - if ( ! self::$_instance instanceof EE_Data_Migration_Manager ) { |
|
| 144 | + if ( ! self::$_instance instanceof EE_Data_Migration_Manager) { |
|
| 145 | 145 | self::$_instance = new self(); |
| 146 | 146 | } |
| 147 | 147 | return self::$_instance; |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * all new usages of the singleton should be made with Classname::instance()) and returns it |
| 152 | 152 | * @return EE_Data_Migration_Manager |
| 153 | 153 | */ |
| 154 | - public static function reset(){ |
|
| 154 | + public static function reset() { |
|
| 155 | 155 | self::$_instance = NULL; |
| 156 | 156 | return self::instance(); |
| 157 | 157 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | /** |
| 162 | 162 | * constructor |
| 163 | 163 | */ |
| 164 | - private function __construct(){ |
|
| 164 | + private function __construct() { |
|
| 165 | 165 | $this->stati_that_indicate_to_continue_migrations = array( |
| 166 | 166 | self::status_continue, |
| 167 | 167 | self::status_completed |
@@ -180,13 +180,13 @@ discard block |
||
| 180 | 180 | ); |
| 181 | 181 | //make sure we've included the base migration script, because we may need the EE_DMS_Unknown_1_0_0 class |
| 182 | 182 | //to be defined, because right now it doesn't get autoloaded on its own |
| 183 | - EE_Registry::instance()->load_core( 'Data_Migration_Class_Base', array(), TRUE ); |
|
| 184 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Base', array(), TRUE ); |
|
| 185 | - EE_Registry::instance()->load_core( 'DMS_Unknown_1_0_0', array(), TRUE ); |
|
| 186 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Stage', array(), TRUE ); |
|
| 187 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Stage_Table', array(), TRUE ); |
|
| 188 | - $this->_table_manager = EE_Registry::instance()->create( 'TableManager', array(), true ); |
|
| 189 | - $this->_table_analysis = EE_Registry::instance()->create( 'TableAnalysis', array(), true ); |
|
| 183 | + EE_Registry::instance()->load_core('Data_Migration_Class_Base', array(), TRUE); |
|
| 184 | + EE_Registry::instance()->load_core('Data_Migration_Script_Base', array(), TRUE); |
|
| 185 | + EE_Registry::instance()->load_core('DMS_Unknown_1_0_0', array(), TRUE); |
|
| 186 | + EE_Registry::instance()->load_core('Data_Migration_Script_Stage', array(), TRUE); |
|
| 187 | + EE_Registry::instance()->load_core('Data_Migration_Script_Stage_Table', array(), TRUE); |
|
| 188 | + $this->_table_manager = EE_Registry::instance()->create('TableManager', array(), true); |
|
| 189 | + $this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | |
@@ -199,21 +199,21 @@ discard block |
||
| 199 | 199 | * @param string $option_name (see EE_Data_Migration_Manage::_save_migrations_ran() where the option name is set) |
| 200 | 200 | * @return array where the first item is the plugin slug (eg 'Core','Calendar',etc) and the 2nd is the version of that plugin (eg '4.1.0') |
| 201 | 201 | */ |
| 202 | - private function _get_plugin_slug_and_version_string_from_dms_option_name($option_name){ |
|
| 202 | + private function _get_plugin_slug_and_version_string_from_dms_option_name($option_name) { |
|
| 203 | 203 | $plugin_slug_and_version_string = str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix, "", $option_name); |
| 204 | 204 | //check if $plugin_slug_and_version_string is like '4.1.0' (4.1-style) or 'Core.4.1.0' (4.2-style) |
| 205 | - $parts = explode(".",$plugin_slug_and_version_string); |
|
| 205 | + $parts = explode(".", $plugin_slug_and_version_string); |
|
| 206 | 206 | |
| 207 | - if(count($parts) == 4){ |
|
| 207 | + if (count($parts) == 4) { |
|
| 208 | 208 | //it's 4.2-style.eg Core.4.1.0 |
| 209 | - $plugin_slug = $parts[0];//eg Core |
|
| 209 | + $plugin_slug = $parts[0]; //eg Core |
|
| 210 | 210 | $version_string = $parts[1].".".$parts[2].".".$parts[3]; //eg 4.1.0 |
| 211 | - }else{ |
|
| 211 | + } else { |
|
| 212 | 212 | //it's 4.1-style: eg 4.1.0 |
| 213 | 213 | $plugin_slug = 'Core'; |
| 214 | - $version_string = $plugin_slug_and_version_string;//eg 4.1.0 |
|
| 214 | + $version_string = $plugin_slug_and_version_string; //eg 4.1.0 |
|
| 215 | 215 | } |
| 216 | - return array($plugin_slug,$version_string); |
|
| 216 | + return array($plugin_slug, $version_string); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -224,21 +224,21 @@ discard block |
||
| 224 | 224 | * @return EE_Data_Migration_Script_Base |
| 225 | 225 | * @throws EE_Error |
| 226 | 226 | */ |
| 227 | - private function _get_dms_class_from_wp_option($dms_option_name,$dms_option_value){ |
|
| 227 | + private function _get_dms_class_from_wp_option($dms_option_name, $dms_option_value) { |
|
| 228 | 228 | $data_migration_data = maybe_unserialize($dms_option_value); |
| 229 | - if(isset($data_migration_data['class']) && class_exists($data_migration_data['class'])){ |
|
| 229 | + if (isset($data_migration_data['class']) && class_exists($data_migration_data['class'])) { |
|
| 230 | 230 | $class = new $data_migration_data['class']; |
| 231 | - if($class instanceof EE_Data_Migration_Script_Base){ |
|
| 231 | + if ($class instanceof EE_Data_Migration_Script_Base) { |
|
| 232 | 232 | $class->instantiate_from_array_of_properties($data_migration_data); |
| 233 | 233 | return $class; |
| 234 | - }else{ |
|
| 234 | + } else { |
|
| 235 | 235 | //huh, so its an object but not a data migration script?? that shouldn't happen |
| 236 | 236 | //just leave it as an array (which will probably just get ignored) |
| 237 | - throw new EE_Error(sprintf(__("Trying to retrieve DMS class from wp option. No DMS by the name '%s' exists", 'event_espresso'),$data_migration_data['class'])); |
|
| 237 | + throw new EE_Error(sprintf(__("Trying to retrieve DMS class from wp option. No DMS by the name '%s' exists", 'event_espresso'), $data_migration_data['class'])); |
|
| 238 | 238 | } |
| 239 | - }else{ |
|
| 239 | + } else { |
|
| 240 | 240 | //so the data doesn't specify a class. So it must either be a legacy array of info or some array (which we'll probably just ignore), or a class that no longer exists |
| 241 | - throw new EE_Error(sprintf(__("The wp option with key '%s' does not represent a DMS", 'event_espresso'),$dms_option_name)); |
|
| 241 | + throw new EE_Error(sprintf(__("The wp option with key '%s' does not represent a DMS", 'event_espresso'), $dms_option_name)); |
|
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | 244 | /** |
@@ -246,34 +246,34 @@ discard block |
||
| 246 | 246 | * the last ran which hasn't finished yet |
| 247 | 247 | * @return array where each element should be an array of EE_Data_Migration_Script_Base (but also has a few legacy arrays in there - which should probably be ignored) |
| 248 | 248 | */ |
| 249 | - public function get_data_migrations_ran(){ |
|
| 250 | - if( ! $this->_data_migrations_ran ){ |
|
| 249 | + public function get_data_migrations_ran() { |
|
| 250 | + if ( ! $this->_data_migrations_ran) { |
|
| 251 | 251 | //setup autoloaders for each of the scripts in there |
| 252 | 252 | $this->get_all_data_migration_scripts_available(); |
| 253 | - $data_migrations_options = $this->get_all_migration_script_options();//get_option(EE_Data_Migration_Manager::data_migrations_option_name,get_option('espresso_data_migrations',array())); |
|
| 253 | + $data_migrations_options = $this->get_all_migration_script_options(); //get_option(EE_Data_Migration_Manager::data_migrations_option_name,get_option('espresso_data_migrations',array())); |
|
| 254 | 254 | |
| 255 | 255 | $data_migrations_ran = array(); |
| 256 | 256 | //convert into data migration script classes where possible |
| 257 | - foreach($data_migrations_options as $data_migration_option){ |
|
| 258 | - list($plugin_slug,$version_string) = $this->_get_plugin_slug_and_version_string_from_dms_option_name($data_migration_option['option_name']); |
|
| 257 | + foreach ($data_migrations_options as $data_migration_option) { |
|
| 258 | + list($plugin_slug, $version_string) = $this->_get_plugin_slug_and_version_string_from_dms_option_name($data_migration_option['option_name']); |
|
| 259 | 259 | |
| 260 | - try{ |
|
| 261 | - $class = $this->_get_dms_class_from_wp_option($data_migration_option['option_name'],$data_migration_option['option_value']); |
|
| 260 | + try { |
|
| 261 | + $class = $this->_get_dms_class_from_wp_option($data_migration_option['option_name'], $data_migration_option['option_value']); |
|
| 262 | 262 | $data_migrations_ran[$plugin_slug][$version_string] = $class; |
| 263 | 263 | //ok so far THIS is the 'last-ran-script'... unless we find another on next iteration |
| 264 | 264 | $this->_last_ran_script = $class; |
| 265 | - if( ! $class->is_completed()){ |
|
| 265 | + if ( ! $class->is_completed()) { |
|
| 266 | 266 | //sometimes we also like to know which was the last incomplete script (or if there are any at all) |
| 267 | 267 | $this->_last_ran_incomplete_script = $class; |
| 268 | 268 | } |
| 269 | - }catch(EE_Error $e){ |
|
| 269 | + } catch (EE_Error $e) { |
|
| 270 | 270 | //ok so its not a DMS. We'll just keep it, although other code will need to expect non-DMSs |
| 271 | 271 | $data_migrations_ran[$plugin_slug][$version_string] = maybe_unserialize($data_migration_option['option_value']); |
| 272 | 272 | } |
| 273 | 273 | } |
| 274 | 274 | //so here the array of $data_migrations_ran is actually a mix of classes and a few legacy arrays |
| 275 | 275 | $this->_data_migrations_ran = $data_migrations_ran; |
| 276 | - if ( ! $this->_data_migrations_ran || ! is_array($this->_data_migrations_ran) ){ |
|
| 276 | + if ( ! $this->_data_migrations_ran || ! is_array($this->_data_migrations_ran)) { |
|
| 277 | 277 | $this->_data_migrations_ran = array(); |
| 278 | 278 | } |
| 279 | 279 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | * @param $new_table |
| 291 | 291 | * @return mixed string or int |
| 292 | 292 | */ |
| 293 | - public function get_mapping_new_pk( $script_name, $old_table, $old_pk, $new_table){ |
|
| 293 | + public function get_mapping_new_pk($script_name, $old_table, $old_pk, $new_table) { |
|
| 294 | 294 | $script = EE_Registry::instance()->load_dms($script_name); |
| 295 | 295 | $mapping = $script->get_mapping_new_pk($old_table, $old_pk, $new_table); |
| 296 | 296 | return $mapping; |
@@ -301,9 +301,9 @@ discard block |
||
| 301 | 301 | * option returned in this array is the most-recently ran DMS option |
| 302 | 302 | * @return array |
| 303 | 303 | */ |
| 304 | - public function get_all_migration_script_options(){ |
|
| 304 | + public function get_all_migration_script_options() { |
|
| 305 | 305 | global $wpdb; |
| 306 | - return $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id ASC",ARRAY_A); |
|
| 306 | + return $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id ASC", ARRAY_A); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /** |
@@ -311,8 +311,8 @@ discard block |
||
| 311 | 311 | * @return array where each value is the full folder path of a folder containing data migration scripts, WITH slashes at the end of the |
| 312 | 312 | * folder name. |
| 313 | 313 | */ |
| 314 | - public function get_data_migration_script_folders(){ |
|
| 315 | - return apply_filters( 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders',array(EE_CORE.'data_migration_scripts') ); |
|
| 314 | + public function get_data_migration_script_folders() { |
|
| 315 | + return apply_filters('FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', array(EE_CORE.'data_migration_scripts')); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | /** |
@@ -324,11 +324,11 @@ discard block |
||
| 324 | 324 | * } |
| 325 | 325 | * @throws EE_Error |
| 326 | 326 | */ |
| 327 | - public function script_migrates_to_version($migration_script_name){ |
|
| 327 | + public function script_migrates_to_version($migration_script_name) { |
|
| 328 | 328 | $dms_info = $this->parse_dms_classname($migration_script_name); |
| 329 | 329 | return array( |
| 330 | - 'slug'=> $dms_info[ 'slug' ], |
|
| 331 | - 'version'=> $dms_info[ 'major_version' ] . "." . $dms_info[ 'minor_version' ] . "." . $dms_info[ 'micro_version' ] ); |
|
| 330 | + 'slug'=> $dms_info['slug'], |
|
| 331 | + 'version'=> $dms_info['major_version'].".".$dms_info['minor_version'].".".$dms_info['micro_version'] ); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | /** |
@@ -337,13 +337,13 @@ discard block |
||
| 337 | 337 | * @return array with keys 'slug','major_version','minor_version', and 'micro_version' (the last 3 are ints) |
| 338 | 338 | * @throws EE_Error |
| 339 | 339 | */ |
| 340 | - public function parse_dms_classname($classname){ |
|
| 340 | + public function parse_dms_classname($classname) { |
|
| 341 | 341 | $matches = array(); |
| 342 | - preg_match('~EE_DMS_(.*)_([0-9]*)_([0-9]*)_([0-9]*)~',$classname,$matches); |
|
| 343 | - if( ! $matches || ! (isset($matches[1]) && isset($matches[2]) && isset($matches[3]))){ |
|
| 344 | - throw new EE_Error(sprintf(__("%s is not a valid Data Migration Script. The classname should be like EE_DMS_w_x_y_z, where w is either 'Core' or the slug of an addon and x, y and z are numbers, ", "event_espresso"),$classname)); |
|
| 342 | + preg_match('~EE_DMS_(.*)_([0-9]*)_([0-9]*)_([0-9]*)~', $classname, $matches); |
|
| 343 | + if ( ! $matches || ! (isset($matches[1]) && isset($matches[2]) && isset($matches[3]))) { |
|
| 344 | + throw new EE_Error(sprintf(__("%s is not a valid Data Migration Script. The classname should be like EE_DMS_w_x_y_z, where w is either 'Core' or the slug of an addon and x, y and z are numbers, ", "event_espresso"), $classname)); |
|
| 345 | 345 | } |
| 346 | - return array('slug'=>$matches[1],'major_version'=>intval($matches[2]),'minor_version'=>intval($matches[3]),'micro_version'=>intval($matches[4])); |
|
| 346 | + return array('slug'=>$matches[1], 'major_version'=>intval($matches[2]), 'minor_version'=>intval($matches[3]), 'micro_version'=>intval($matches[4])); |
|
| 347 | 347 | } |
| 348 | 348 | /** |
| 349 | 349 | * Ensures that the option indicating the current DB version is set. This should only be |
@@ -352,33 +352,33 @@ discard block |
||
| 352 | 352 | * to 4.1.x. |
| 353 | 353 | * @return string of current db state |
| 354 | 354 | */ |
| 355 | - public function ensure_current_database_state_is_set(){ |
|
| 356 | - $espresso_db_core_updates = get_option( 'espresso_db_update', array() ); |
|
| 355 | + public function ensure_current_database_state_is_set() { |
|
| 356 | + $espresso_db_core_updates = get_option('espresso_db_update', array()); |
|
| 357 | 357 | $db_state = get_option(EE_Data_Migration_Manager::current_database_state); |
| 358 | - if( ! $db_state ){ |
|
| 358 | + if ( ! $db_state) { |
|
| 359 | 359 | //mark the DB as being in the state as the last version in there. |
| 360 | 360 | //this is done to trigger maintenance mode and do data migration scripts |
| 361 | 361 | //if the admin installed this version of EE over 3.1.x or 4.0.x |
| 362 | 362 | //otherwise, the normal maintenance mode code is fine |
| 363 | 363 | $previous_versions_installed = array_keys($espresso_db_core_updates); |
| 364 | 364 | $previous_version_installed = end($previous_versions_installed); |
| 365 | - if(version_compare('4.1.0', $previous_version_installed)){ |
|
| 365 | + if (version_compare('4.1.0', $previous_version_installed)) { |
|
| 366 | 366 | //last installed version was less than 4.1 |
| 367 | 367 | //so we want the data migrations to happen. SO, we're going to say the DB is at that state |
| 368 | 368 | // echo "4.1.0 is greater than $previous_version_installed! update the option"; |
| 369 | 369 | $db_state = array('Core'=>$previous_version_installed); |
| 370 | - }else{ |
|
| 370 | + } else { |
|
| 371 | 371 | // echo "4.1.0 is SMALLER than $previous_version_installed"; |
| 372 | 372 | $db_state = array('Core'=>EVENT_ESPRESSO_VERSION); |
| 373 | 373 | } |
| 374 | - update_option(EE_Data_Migration_Manager::current_database_state,$db_state); |
|
| 374 | + update_option(EE_Data_Migration_Manager::current_database_state, $db_state); |
|
| 375 | 375 | } |
| 376 | 376 | //in 4.1, $db_state would have only been a simple string like '4.1.0', |
| 377 | 377 | //but in 4.2+ it should be an array with at least key 'Core' and the value of that plugin's |
| 378 | 378 | //db, and possibly other keys for other addons like 'Calendar','Permissions',etc |
| 379 | - if( ! is_array($db_state)){ |
|
| 379 | + if ( ! is_array($db_state)) { |
|
| 380 | 380 | $db_state = array('Core'=>$db_state); |
| 381 | - update_option(EE_Data_Migration_Manager::current_database_state,$db_state); |
|
| 381 | + update_option(EE_Data_Migration_Manager::current_database_state, $db_state); |
|
| 382 | 382 | } |
| 383 | 383 | return $db_state; |
| 384 | 384 | } |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | * or they don't apply), returns an empty array |
| 390 | 390 | * @return EE_Data_Migration_Script_Base[] |
| 391 | 391 | */ |
| 392 | - public function check_for_applicable_data_migration_scripts(){ |
|
| 392 | + public function check_for_applicable_data_migration_scripts() { |
|
| 393 | 393 | //get the option describing what options have already run |
| 394 | 394 | $scripts_ran = $this->get_data_migrations_ran(); |
| 395 | 395 | //$scripts_ran = array('4.1.0.core'=>array('monkey'=>null)); |
@@ -402,62 +402,62 @@ discard block |
||
| 402 | 402 | $iteration = 0; |
| 403 | 403 | $next_database_state_to_consider = $current_database_state; |
| 404 | 404 | $theoretical_database_state = NULL; |
| 405 | - do{ |
|
| 405 | + do { |
|
| 406 | 406 | //the next state after the currently-considered one will start off looking the same as the current, but we may make additions... |
| 407 | 407 | $theoretical_database_state = $next_database_state_to_consider; |
| 408 | 408 | //the next db state to consider is "what would the DB be like had we run all the scripts we found that applied last time?) |
| 409 | - foreach($script_class_and_filepaths_available as $classname => $filepath){ |
|
| 409 | + foreach ($script_class_and_filepaths_available as $classname => $filepath) { |
|
| 410 | 410 | |
| 411 | 411 | $migrates_to_version = $this->script_migrates_to_version($classname); |
| 412 | - $script_converts_plugin_slug = $migrates_to_version[ 'slug' ]; |
|
| 413 | - $script_converts_to_version = $migrates_to_version[ 'version' ]; |
|
| 412 | + $script_converts_plugin_slug = $migrates_to_version['slug']; |
|
| 413 | + $script_converts_to_version = $migrates_to_version['version']; |
|
| 414 | 414 | //check if this version script is DONE or not; or if it's never been ran |
| 415 | - if( ! $scripts_ran || |
|
| 415 | + if ( ! $scripts_ran || |
|
| 416 | 416 | ! isset($scripts_ran[$script_converts_plugin_slug]) || |
| 417 | - ! isset($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version])){ |
|
| 417 | + ! isset($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version])) { |
|
| 418 | 418 | //we haven't ran this conversion script before |
| 419 | 419 | //now check if it applies... note that we've added an autoloader for it on get_all_data_migration_scripts_available |
| 420 | - $script = new $classname( $this->_get_table_manager(), $this->_get_table_analysis() ); |
|
| 420 | + $script = new $classname($this->_get_table_manager(), $this->_get_table_analysis()); |
|
| 421 | 421 | /* @var $script EE_Data_Migration_Script_Base */ |
| 422 | 422 | $can_migrate = $script->can_migrate_from_version($theoretical_database_state); |
| 423 | - if($can_migrate){ |
|
| 423 | + if ($can_migrate) { |
|
| 424 | 424 | $script_classes_that_should_run_per_iteration[$iteration][$script->priority()][] = $script; |
| 425 | 425 | $migrates_to_version = $script->migrates_to_version(); |
| 426 | - $next_database_state_to_consider[ $migrates_to_version[ 'slug' ] ] = $migrates_to_version[ 'version' ]; |
|
| 426 | + $next_database_state_to_consider[$migrates_to_version['slug']] = $migrates_to_version['version']; |
|
| 427 | 427 | unset($script_class_and_filepaths_available[$classname]); |
| 428 | 428 | } |
| 429 | - } elseif($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version] instanceof EE_Data_Migration_Script_Base){ |
|
| 429 | + } elseif ($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version] instanceof EE_Data_Migration_Script_Base) { |
|
| 430 | 430 | //this script has been ran, or at least started |
| 431 | 431 | $script = $scripts_ran[$script_converts_plugin_slug][$script_converts_to_version]; |
| 432 | - if( $script->get_status() != self::status_completed){ |
|
| 432 | + if ($script->get_status() != self::status_completed) { |
|
| 433 | 433 | //this script is already underway... keep going with it |
| 434 | 434 | $script_classes_that_should_run_per_iteration[$iteration][$script->priority()][] = $script; |
| 435 | 435 | $migrates_to_version = $script->migrates_to_version(); |
| 436 | - $next_database_state_to_consider[ $migrates_to_version[ 'slug' ] ] = $migrates_to_version[ 'version' ]; |
|
| 436 | + $next_database_state_to_consider[$migrates_to_version['slug']] = $migrates_to_version['version']; |
|
| 437 | 437 | unset($script_class_and_filepaths_available[$classname]); |
| 438 | - }else{ |
|
| 438 | + } else { |
|
| 439 | 439 | //it must have a status that indicates it has finished, so we don't want to try and run it again |
| 440 | 440 | } |
| 441 | - }else{ |
|
| 441 | + } else { |
|
| 442 | 442 | //it exists but it's not a proper data migration script |
| 443 | 443 | //maybe the script got renamed? or was simply removed from EE? |
| 444 | 444 | //either way, its certainly not runnable! |
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | 447 | $iteration++; |
| 448 | - }while( $next_database_state_to_consider != $theoretical_database_state && $iteration<6); |
|
| 448 | + }while ($next_database_state_to_consider != $theoretical_database_state && $iteration < 6); |
|
| 449 | 449 | //ok we have all the scripts that should run, now let's make them into flat array |
| 450 | 450 | $scripts_that_should_run = array(); |
| 451 | - foreach($script_classes_that_should_run_per_iteration as $scripts_at_priority){ |
|
| 451 | + foreach ($script_classes_that_should_run_per_iteration as $scripts_at_priority) { |
|
| 452 | 452 | ksort($scripts_at_priority); |
| 453 | - foreach($scripts_at_priority as $scripts){ |
|
| 454 | - foreach($scripts as $script){ |
|
| 453 | + foreach ($scripts_at_priority as $scripts) { |
|
| 454 | + foreach ($scripts as $script) { |
|
| 455 | 455 | $scripts_that_should_run[get_class($script)] = $script; |
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - do_action( 'AHEE__EE_Data_Migration_Manager__check_for_applicable_data_migration_scripts__scripts_that_should_run', $scripts_that_should_run ); |
|
| 460 | + do_action('AHEE__EE_Data_Migration_Manager__check_for_applicable_data_migration_scripts__scripts_that_should_run', $scripts_that_should_run); |
|
| 461 | 461 | return $scripts_that_should_run; |
| 462 | 462 | } |
| 463 | 463 | |
@@ -471,14 +471,14 @@ discard block |
||
| 471 | 471 | * @param bool $include_completed_scripts |
| 472 | 472 | * @return EE_Data_Migration_Script_Base |
| 473 | 473 | */ |
| 474 | - public function get_last_ran_script($include_completed_scripts = false){ |
|
| 474 | + public function get_last_ran_script($include_completed_scripts = false) { |
|
| 475 | 475 | //make sure we've setup the class properties _last_ran_script and _last_ran_incomplete_script |
| 476 | - if( ! $this->_data_migrations_ran){ |
|
| 476 | + if ( ! $this->_data_migrations_ran) { |
|
| 477 | 477 | $this->get_data_migrations_ran(); |
| 478 | 478 | } |
| 479 | - if($include_completed_scripts){ |
|
| 479 | + if ($include_completed_scripts) { |
|
| 480 | 480 | return $this->_last_ran_script; |
| 481 | - }else{ |
|
| 481 | + } else { |
|
| 482 | 482 | return $this->_last_ran_incomplete_script; |
| 483 | 483 | } |
| 484 | 484 | } |
@@ -501,19 +501,19 @@ discard block |
||
| 501 | 501 | * @type string $message string describing what was done during this step |
| 502 | 502 | * } |
| 503 | 503 | */ |
| 504 | - public function migration_step( $step_size = 0 ){ |
|
| 504 | + public function migration_step($step_size = 0) { |
|
| 505 | 505 | |
| 506 | 506 | //bandaid fix for issue https://events.codebasehq.com/projects/event-espresso/tickets/7535 |
| 507 | - if ( class_exists( 'EE_CPT_Strategy' ) ) { |
|
| 508 | - remove_action( 'pre_get_posts', array( EE_CPT_Strategy::instance(), 'pre_get_posts' ), 5 ); |
|
| 507 | + if (class_exists('EE_CPT_Strategy')) { |
|
| 508 | + remove_action('pre_get_posts', array(EE_CPT_Strategy::instance(), 'pre_get_posts'), 5); |
|
| 509 | 509 | } |
| 510 | 510 | |
| 511 | - try{ |
|
| 511 | + try { |
|
| 512 | 512 | $currently_executing_script = $this->get_last_ran_script(); |
| 513 | - if( ! $currently_executing_script){ |
|
| 513 | + if ( ! $currently_executing_script) { |
|
| 514 | 514 | //Find the next script that needs to execute |
| 515 | 515 | $scripts = $this->check_for_applicable_data_migration_scripts(); |
| 516 | - if( ! $scripts ){ |
|
| 516 | + if ( ! $scripts) { |
|
| 517 | 517 | //huh, no more scripts to run... apparently we're done! |
| 518 | 518 | //but dont forget to make sure initial data is there |
| 519 | 519 | //we should be good to allow them to exit maintenance mode now |
@@ -534,18 +534,18 @@ discard block |
||
| 534 | 534 | //and add to the array/wp option showing the scripts ran |
| 535 | 535 | // $this->_data_migrations_ran[$this->script_migrates_to_version(get_class($currently_executing_script))] = $currently_executing_script; |
| 536 | 536 | $migrates_to = $this->script_migrates_to_version(get_class($currently_executing_script)); |
| 537 | - $plugin_slug = $migrates_to[ 'slug' ]; |
|
| 538 | - $version = $migrates_to[ 'version' ]; |
|
| 537 | + $plugin_slug = $migrates_to['slug']; |
|
| 538 | + $version = $migrates_to['version']; |
|
| 539 | 539 | $this->_data_migrations_ran[$plugin_slug][$version] = $currently_executing_script; |
| 540 | 540 | } |
| 541 | 541 | $current_script_name = get_class($currently_executing_script); |
| 542 | - }catch(Exception $e){ |
|
| 542 | + } catch (Exception $e) { |
|
| 543 | 543 | //an exception occurred while trying to get migration scripts |
| 544 | 544 | |
| 545 | - $message = sprintf( __("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage() . '<br>', $e->getTraceAsString() ); |
|
| 545 | + $message = sprintf(__("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage().'<br>', $e->getTraceAsString()); |
|
| 546 | 546 | //record it on the array of data migration scripts ran. This will be overwritten next time we try and try to run data migrations |
| 547 | 547 | //but that's ok-- it's just an FYI to support that we couldn't even run any data migrations |
| 548 | - $this->add_error_to_migrations_ran(sprintf(__("Could not run data migrations because: %s", "event_espresso"),$message)); |
|
| 548 | + $this->add_error_to_migrations_ran(sprintf(__("Could not run data migrations because: %s", "event_espresso"), $message)); |
|
| 549 | 549 | return array( |
| 550 | 550 | 'records_to_migrate'=>1, |
| 551 | 551 | 'records_migrated'=>0, |
@@ -555,16 +555,16 @@ discard block |
||
| 555 | 555 | ); |
| 556 | 556 | } |
| 557 | 557 | //ok so we definitely have a data migration script |
| 558 | - try{ |
|
| 558 | + try { |
|
| 559 | 559 | //how big of a bite do we want to take? Allow users to easily override via their wp-config |
| 560 | - if( ! absint( $step_size ) > 0 ){ |
|
| 561 | - $step_size = defined( 'EE_MIGRATION_STEP_SIZE' ) && absint( EE_MIGRATION_STEP_SIZE ) ? EE_MIGRATION_STEP_SIZE : EE_Data_Migration_Manager::step_size; |
|
| 560 | + if ( ! absint($step_size) > 0) { |
|
| 561 | + $step_size = defined('EE_MIGRATION_STEP_SIZE') && absint(EE_MIGRATION_STEP_SIZE) ? EE_MIGRATION_STEP_SIZE : EE_Data_Migration_Manager::step_size; |
|
| 562 | 562 | } |
| 563 | 563 | //do what we came to do! |
| 564 | 564 | $currently_executing_script->migration_step($step_size); |
| 565 | 565 | //can we wrap it up and verify default data? |
| 566 | 566 | $init_dbs = false; |
| 567 | - switch($currently_executing_script->get_status()){ |
|
| 567 | + switch ($currently_executing_script->get_status()) { |
|
| 568 | 568 | case EE_Data_Migration_Manager::status_continue: |
| 569 | 569 | $response_array = array( |
| 570 | 570 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
@@ -576,16 +576,16 @@ discard block |
||
| 576 | 576 | case EE_Data_Migration_Manager::status_completed: |
| 577 | 577 | //ok so THAT script has completed |
| 578 | 578 | $this->update_current_database_state_to($this->script_migrates_to_version($current_script_name)); |
| 579 | - $response_array = array( |
|
| 579 | + $response_array = array( |
|
| 580 | 580 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
| 581 | 581 | 'records_migrated'=>$currently_executing_script->count_records_migrated(), |
| 582 | 582 | 'status'=> EE_Data_Migration_Manager::status_completed, |
| 583 | 583 | 'message'=>$currently_executing_script->get_feedback_message(), |
| 584 | - 'script'=> sprintf(__("%s Completed",'event_espresso'),$currently_executing_script->pretty_name()) |
|
| 584 | + 'script'=> sprintf(__("%s Completed", 'event_espresso'), $currently_executing_script->pretty_name()) |
|
| 585 | 585 | ); |
| 586 | 586 | //check if there are any more after this one. |
| 587 | 587 | $scripts_remaining = $this->check_for_applicable_data_migration_scripts(); |
| 588 | - if( ! $scripts_remaining ){ |
|
| 588 | + if ( ! $scripts_remaining) { |
|
| 589 | 589 | //we should be good to allow them to exit maintenance mode now |
| 590 | 590 | EE_Maintenance_Mode::instance()->set_maintenance_level(intval(EE_Maintenance_Mode::level_0_not_in_maintenance)); |
| 591 | 591 | ////huh, no more scripts to run... apparently we're done! |
@@ -599,39 +599,39 @@ discard block |
||
| 599 | 599 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
| 600 | 600 | 'records_migrated'=>$currently_executing_script->count_records_migrated(), |
| 601 | 601 | 'status'=> $currently_executing_script->get_status(), |
| 602 | - 'message'=> sprintf(__("Minor errors occurred during %s: %s", "event_espresso"), $currently_executing_script->pretty_name(), implode(", ",$currently_executing_script->get_errors())), |
|
| 602 | + 'message'=> sprintf(__("Minor errors occurred during %s: %s", "event_espresso"), $currently_executing_script->pretty_name(), implode(", ", $currently_executing_script->get_errors())), |
|
| 603 | 603 | 'script'=>$currently_executing_script->pretty_name() |
| 604 | 604 | ); |
| 605 | 605 | break; |
| 606 | 606 | } |
| 607 | - }catch(Exception $e){ |
|
| 607 | + } catch (Exception $e) { |
|
| 608 | 608 | //ok so some exception was thrown which killed the data migration script |
| 609 | 609 | //double-check we have a real script |
| 610 | - if($currently_executing_script instanceof EE_Data_Migration_Script_Base){ |
|
| 610 | + if ($currently_executing_script instanceof EE_Data_Migration_Script_Base) { |
|
| 611 | 611 | $script_name = $currently_executing_script->pretty_name(); |
| 612 | 612 | $currently_executing_script->set_broken(); |
| 613 | 613 | $currently_executing_script->add_error($e->getMessage()); |
| 614 | - }else{ |
|
| 614 | + } else { |
|
| 615 | 615 | $script_name = __("Error getting Migration Script", "event_espresso"); |
| 616 | 616 | } |
| 617 | 617 | $response_array = array( |
| 618 | 618 | 'records_to_migrate'=>1, |
| 619 | 619 | 'records_migrated'=>0, |
| 620 | 620 | 'status'=>self::status_fatal_error, |
| 621 | - 'message'=> sprintf(__("A fatal error occurred during the migration: %s", "event_espresso"),$e->getMessage()), |
|
| 621 | + 'message'=> sprintf(__("A fatal error occurred during the migration: %s", "event_espresso"), $e->getMessage()), |
|
| 622 | 622 | 'script'=>$script_name |
| 623 | 623 | ); |
| 624 | 624 | } |
| 625 | 625 | $successful_save = $this->_save_migrations_ran(); |
| 626 | - if($successful_save !== TRUE){ |
|
| 626 | + if ($successful_save !== TRUE) { |
|
| 627 | 627 | //ok so the current wp option didn't save. that's tricky, because we'd like to update it |
| 628 | 628 | //and mark it as having a fatal error, but remember- WE CAN'T SAVE THIS WP OPTION! |
| 629 | 629 | //however, if we throw an exception, and return that, then the next request |
| 630 | 630 | //won't have as much info in it, and it may be able to save |
| 631 | - throw new EE_Error(sprintf(__("The error '%s' occurred updating the status of the migration. This is a FATAL ERROR, but the error is preventing the system from remembering that. Please contact event espresso support.", "event_espresso"),$successful_save)); |
|
| 631 | + throw new EE_Error(sprintf(__("The error '%s' occurred updating the status of the migration. This is a FATAL ERROR, but the error is preventing the system from remembering that. Please contact event espresso support.", "event_espresso"), $successful_save)); |
|
| 632 | 632 | } |
| 633 | 633 | //if we're all done, initialize EE plugins' default data etc. |
| 634 | - if( $init_dbs ) { |
|
| 634 | + if ($init_dbs) { |
|
| 635 | 635 | $this->initialize_db_for_enqueued_ee_plugins(); |
| 636 | 636 | } |
| 637 | 637 | return $response_array; |
@@ -649,23 +649,23 @@ discard block |
||
| 649 | 649 | * 'message'=>a string, containing any message you want to show to the user. We may decide to split this up into errors, notifications, and successes |
| 650 | 650 | * 'script'=>a pretty name of the script currently running |
| 651 | 651 | */ |
| 652 | - public function response_to_migration_ajax_request(){ |
|
| 652 | + public function response_to_migration_ajax_request() { |
|
| 653 | 653 | // //start output buffer just to make sure we don't mess up the json |
| 654 | 654 | ob_start(); |
| 655 | - try{ |
|
| 655 | + try { |
|
| 656 | 656 | $response = $this->migration_step(); |
| 657 | - }catch(Exception $e){ |
|
| 657 | + } catch (Exception $e) { |
|
| 658 | 658 | $response = array( |
| 659 | 659 | 'records_to_migrate'=>0, |
| 660 | 660 | 'records_migrated'=>0, |
| 661 | 661 | 'status'=> EE_Data_Migration_Manager::status_fatal_error, |
| 662 | - 'message'=> sprintf(__("Unknown fatal error occurred: %s", "event_espresso"),$e->getMessage()), |
|
| 662 | + 'message'=> sprintf(__("Unknown fatal error occurred: %s", "event_espresso"), $e->getMessage()), |
|
| 663 | 663 | 'script'=>'Unknown'); |
| 664 | 664 | $this->add_error_to_migrations_ran($e->getMessage()."; Stack trace:".$e->getTraceAsString()); |
| 665 | 665 | } |
| 666 | 666 | $warnings_etc = @ob_get_contents(); |
| 667 | 667 | ob_end_clean(); |
| 668 | - $response['message'] .=$warnings_etc; |
|
| 668 | + $response['message'] .= $warnings_etc; |
|
| 669 | 669 | return $response; |
| 670 | 670 | } |
| 671 | 671 | |
@@ -678,14 +678,14 @@ discard block |
||
| 678 | 678 | * } |
| 679 | 679 | * @return void |
| 680 | 680 | */ |
| 681 | - public function update_current_database_state_to($slug_and_version = null){ |
|
| 682 | - if( ! $slug_and_version ){ |
|
| 681 | + public function update_current_database_state_to($slug_and_version = null) { |
|
| 682 | + if ( ! $slug_and_version) { |
|
| 683 | 683 | //no version was provided, assume it should be at the current code version |
| 684 | 684 | $slug_and_version = array('slug' => 'Core', 'version' => espresso_version()); |
| 685 | 685 | } |
| 686 | 686 | $current_database_state = get_option(self::current_database_state); |
| 687 | - $current_database_state[ $slug_and_version[ 'slug' ] ]=$slug_and_version[ 'version' ]; |
|
| 688 | - update_option(self::current_database_state,$current_database_state); |
|
| 687 | + $current_database_state[$slug_and_version['slug']] = $slug_and_version['version']; |
|
| 688 | + update_option(self::current_database_state, $current_database_state); |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | /** |
@@ -696,20 +696,20 @@ discard block |
||
| 696 | 696 | * } |
| 697 | 697 | * @return boolean |
| 698 | 698 | */ |
| 699 | - public function database_needs_updating_to( $slug_and_version ) { |
|
| 699 | + public function database_needs_updating_to($slug_and_version) { |
|
| 700 | 700 | |
| 701 | - $slug = $slug_and_version[ 'slug' ]; |
|
| 702 | - $version = $slug_and_version[ 'version' ]; |
|
| 701 | + $slug = $slug_and_version['slug']; |
|
| 702 | + $version = $slug_and_version['version']; |
|
| 703 | 703 | $current_database_state = get_option(self::current_database_state); |
| 704 | - if( ! isset( $current_database_state[ $slug ] ) ) { |
|
| 704 | + if ( ! isset($current_database_state[$slug])) { |
|
| 705 | 705 | return true; |
| 706 | - }else{ |
|
| 706 | + } else { |
|
| 707 | 707 | //just compare the first 3 parts of version string, eg "4.7.1", not "4.7.1.dev.032" because DBs shouldn't change on nano version changes |
| 708 | - $version_parts_current_db_state = array_slice( explode('.', $current_database_state[ $slug ] ), 0, 3); |
|
| 709 | - $version_parts_of_provided_db_state = array_slice( explode( '.', $version ), 0, 3 ); |
|
| 708 | + $version_parts_current_db_state = array_slice(explode('.', $current_database_state[$slug]), 0, 3); |
|
| 709 | + $version_parts_of_provided_db_state = array_slice(explode('.', $version), 0, 3); |
|
| 710 | 710 | $needs_updating = false; |
| 711 | - foreach($version_parts_current_db_state as $offset => $version_part_in_current_db_state ) { |
|
| 712 | - if( $version_part_in_current_db_state < $version_parts_of_provided_db_state[ $offset ] ) { |
|
| 711 | + foreach ($version_parts_current_db_state as $offset => $version_part_in_current_db_state) { |
|
| 712 | + if ($version_part_in_current_db_state < $version_parts_of_provided_db_state[$offset]) { |
|
| 713 | 713 | $needs_updating = true; |
| 714 | 714 | break; |
| 715 | 715 | } |
@@ -727,33 +727,33 @@ discard block |
||
| 727 | 727 | * @return array keys are expected classnames, values are their filepaths |
| 728 | 728 | * @throws \EE_Error |
| 729 | 729 | */ |
| 730 | - public function get_all_data_migration_scripts_available(){ |
|
| 731 | - if( ! $this->_data_migration_class_to_filepath_map){ |
|
| 730 | + public function get_all_data_migration_scripts_available() { |
|
| 731 | + if ( ! $this->_data_migration_class_to_filepath_map) { |
|
| 732 | 732 | $this->_data_migration_class_to_filepath_map = array(); |
| 733 | - foreach($this->get_data_migration_script_folders() as $folder_path){ |
|
| 733 | + foreach ($this->get_data_migration_script_folders() as $folder_path) { |
|
| 734 | 734 | $folder_path = EEH_File::end_with_directory_separator($folder_path); |
| 735 | - $files = glob( $folder_path. '*.dms.php' ); |
|
| 735 | + $files = glob($folder_path.'*.dms.php'); |
|
| 736 | 736 | |
| 737 | - if ( empty( $files ) ) { |
|
| 737 | + if (empty($files)) { |
|
| 738 | 738 | continue; |
| 739 | 739 | } |
| 740 | 740 | |
| 741 | - foreach($files as $file){ |
|
| 742 | - $pos_of_last_slash = strrpos($file,DS); |
|
| 741 | + foreach ($files as $file) { |
|
| 742 | + $pos_of_last_slash = strrpos($file, DS); |
|
| 743 | 743 | $classname = str_replace('.dms.php', '', substr($file, $pos_of_last_slash + 1)); |
| 744 | - $migrates_to = $this->script_migrates_to_version( $classname ); |
|
| 745 | - $slug = $migrates_to[ 'slug' ]; |
|
| 744 | + $migrates_to = $this->script_migrates_to_version($classname); |
|
| 745 | + $slug = $migrates_to['slug']; |
|
| 746 | 746 | //check that the slug as contained in the DMS is associated with |
| 747 | 747 | //the slug of an addon or core |
| 748 | - if( $slug !== 'Core' ){ |
|
| 749 | - if( ! EE_Registry::instance()->get_addon_by_name( $slug ) ) { |
|
| 748 | + if ($slug !== 'Core') { |
|
| 749 | + if ( ! EE_Registry::instance()->get_addon_by_name($slug)) { |
|
| 750 | 750 | EE_Error::doing_it_wrong( |
| 751 | 751 | __FUNCTION__, |
| 752 | 752 | sprintf( |
| 753 | - __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
| 753 | + __('The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso'), |
|
| 754 | 754 | $classname, |
| 755 | 755 | $slug, |
| 756 | - implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
| 756 | + implode(', ', array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
| 757 | 757 | ), |
| 758 | 758 | '4.3.0.alpha.019' |
| 759 | 759 | ); |
@@ -776,7 +776,7 @@ discard block |
||
| 776 | 776 | * from each addon, and check if they need updating, |
| 777 | 777 | * @return boolean |
| 778 | 778 | */ |
| 779 | - public function addons_need_updating(){ |
|
| 779 | + public function addons_need_updating() { |
|
| 780 | 780 | return false; |
| 781 | 781 | } |
| 782 | 782 | /** |
@@ -785,25 +785,25 @@ discard block |
||
| 785 | 785 | * @param string $error_message |
| 786 | 786 | * @throws EE_Error |
| 787 | 787 | */ |
| 788 | - public function add_error_to_migrations_ran($error_message){ |
|
| 788 | + public function add_error_to_migrations_ran($error_message) { |
|
| 789 | 789 | //get last-ran migration script |
| 790 | 790 | global $wpdb; |
| 791 | - $last_migration_script_option = $wpdb->get_row("SELECT * FROM $wpdb->options WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id DESC LIMIT 1",ARRAY_A); |
|
| 791 | + $last_migration_script_option = $wpdb->get_row("SELECT * FROM $wpdb->options WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id DESC LIMIT 1", ARRAY_A); |
|
| 792 | 792 | |
| 793 | 793 | $last_ran_migration_script_properties = isset($last_migration_script_option['option_value']) ? maybe_unserialize($last_migration_script_option['option_value']) : null; |
| 794 | 794 | //now, tread lightly because we're here because a FATAL non-catchable error |
| 795 | 795 | //was thrown last time when we were trying to run a data migration script |
| 796 | 796 | //so the fatal error could have happened while getting the migration script |
| 797 | 797 | //or doing running it... |
| 798 | - $versions_migrated_to = isset($last_migration_script_option['option_name']) ? str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix,"",$last_migration_script_option['option_name']) : null; |
|
| 798 | + $versions_migrated_to = isset($last_migration_script_option['option_name']) ? str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix, "", $last_migration_script_option['option_name']) : null; |
|
| 799 | 799 | |
| 800 | 800 | //check if it THINKS its a data migration script and especially if it's one that HASN'T finished yet |
| 801 | 801 | //because if it has finished, then it obviously couldn't be the cause of this error, right? (because its all done) |
| 802 | - if(isset($last_ran_migration_script_properties['class']) && isset($last_ran_migration_script_properties['_status']) && $last_ran_migration_script_properties['_status'] != self::status_completed){ |
|
| 802 | + if (isset($last_ran_migration_script_properties['class']) && isset($last_ran_migration_script_properties['_status']) && $last_ran_migration_script_properties['_status'] != self::status_completed) { |
|
| 803 | 803 | //ok then just add this error to its list of errors |
| 804 | 804 | $last_ran_migration_script_properties['_errors'][] = $error_message; |
| 805 | 805 | $last_ran_migration_script_properties['_status'] = self::status_fatal_error; |
| 806 | - }else{ |
|
| 806 | + } else { |
|
| 807 | 807 | //so we don't even know which script was last running |
| 808 | 808 | //use the data migration error stub, which is designed specifically for this type of thing |
| 809 | 809 | $general_migration_error = new EE_DMS_Unknown_1_0_0(); |
@@ -813,39 +813,39 @@ discard block |
||
| 813 | 813 | $versions_migrated_to = 'Unknown.1.0.0'; |
| 814 | 814 | //now just to make sure appears as last (in case the were previously a fatal error like this) |
| 815 | 815 | //delete the old one |
| 816 | - delete_option( self::data_migration_script_option_prefix . $versions_migrated_to ); |
|
| 816 | + delete_option(self::data_migration_script_option_prefix.$versions_migrated_to); |
|
| 817 | 817 | } |
| 818 | - update_option(self::data_migration_script_option_prefix.$versions_migrated_to,$last_ran_migration_script_properties); |
|
| 818 | + update_option(self::data_migration_script_option_prefix.$versions_migrated_to, $last_ran_migration_script_properties); |
|
| 819 | 819 | |
| 820 | 820 | } |
| 821 | 821 | /** |
| 822 | 822 | * saves what data migrations have ran to the database |
| 823 | 823 | * @return mixed TRUE if successfully saved migrations ran, string if an error occurred |
| 824 | 824 | */ |
| 825 | - protected function _save_migrations_ran(){ |
|
| 826 | - if($this->_data_migrations_ran == null){ |
|
| 825 | + protected function _save_migrations_ran() { |
|
| 826 | + if ($this->_data_migrations_ran == null) { |
|
| 827 | 827 | $this->get_data_migrations_ran(); |
| 828 | 828 | } |
| 829 | 829 | //now, we don't want to save actual classes to the DB because that's messy |
| 830 | 830 | $successful_updates = true; |
| 831 | - foreach($this->_data_migrations_ran as $plugin_slug => $migrations_ran_for_plugin){ |
|
| 832 | - foreach($migrations_ran_for_plugin as $version_string => $array_or_migration_obj){ |
|
| 831 | + foreach ($this->_data_migrations_ran as $plugin_slug => $migrations_ran_for_plugin) { |
|
| 832 | + foreach ($migrations_ran_for_plugin as $version_string => $array_or_migration_obj) { |
|
| 833 | 833 | // echo "saving migration script to $version_string<br>"; |
| 834 | 834 | $plugin_slug_for_use_in_option_name = $plugin_slug."."; |
| 835 | 835 | $option_name = self::data_migration_script_option_prefix.$plugin_slug_for_use_in_option_name.$version_string; |
| 836 | 836 | $old_option_value = get_option($option_name); |
| 837 | - if($array_or_migration_obj instanceof EE_Data_Migration_Script_Base){ |
|
| 837 | + if ($array_or_migration_obj instanceof EE_Data_Migration_Script_Base) { |
|
| 838 | 838 | $script_array_for_saving = $array_or_migration_obj->properties_as_array(); |
| 839 | - if( $old_option_value != $script_array_for_saving){ |
|
| 840 | - $successful_updates = update_option($option_name,$script_array_for_saving); |
|
| 839 | + if ($old_option_value != $script_array_for_saving) { |
|
| 840 | + $successful_updates = update_option($option_name, $script_array_for_saving); |
|
| 841 | 841 | } |
| 842 | - }else{//we don't know what this array-thing is. So just save it as-is |
|
| 842 | + } else {//we don't know what this array-thing is. So just save it as-is |
|
| 843 | 843 | // $array_of_migrations[$version_string] = $array_or_migration_obj; |
| 844 | - if($old_option_value != $array_or_migration_obj){ |
|
| 845 | - $successful_updates = update_option($option_name,$array_or_migration_obj); |
|
| 844 | + if ($old_option_value != $array_or_migration_obj) { |
|
| 845 | + $successful_updates = update_option($option_name, $array_or_migration_obj); |
|
| 846 | 846 | } |
| 847 | 847 | } |
| 848 | - if( ! $successful_updates ){ |
|
| 848 | + if ( ! $successful_updates) { |
|
| 849 | 849 | global $wpdb; |
| 850 | 850 | return $wpdb->last_error; |
| 851 | 851 | } |
@@ -869,17 +869,17 @@ discard block |
||
| 869 | 869 | * @return EE_Data_Migration_Script_Base |
| 870 | 870 | * @throws EE_Error |
| 871 | 871 | */ |
| 872 | - function _instantiate_script_from_properties_array($properties_array){ |
|
| 873 | - if( ! isset($properties_array['class'])){ |
|
| 874 | - throw new EE_Error(sprintf(__("Properties array has no 'class' properties. Here's what it has: %s", "event_espresso"),implode(",",$properties_array))); |
|
| 872 | + function _instantiate_script_from_properties_array($properties_array) { |
|
| 873 | + if ( ! isset($properties_array['class'])) { |
|
| 874 | + throw new EE_Error(sprintf(__("Properties array has no 'class' properties. Here's what it has: %s", "event_espresso"), implode(",", $properties_array))); |
|
| 875 | 875 | } |
| 876 | 876 | $class_name = $properties_array['class']; |
| 877 | - if( ! class_exists($class_name)){ |
|
| 878 | - throw new EE_Error(sprintf(__("There is no migration script named %s", "event_espresso"),$class_name)); |
|
| 877 | + if ( ! class_exists($class_name)) { |
|
| 878 | + throw new EE_Error(sprintf(__("There is no migration script named %s", "event_espresso"), $class_name)); |
|
| 879 | 879 | } |
| 880 | 880 | $class = new $class_name; |
| 881 | - if( ! $class instanceof EE_Data_Migration_Script_Base){ |
|
| 882 | - throw new EE_Error(sprintf(__("Class '%s' is supposed to be a migration script. Its not, its a '%s'", "event_espresso"),$class_name,get_class($class))); |
|
| 881 | + if ( ! $class instanceof EE_Data_Migration_Script_Base) { |
|
| 882 | + throw new EE_Error(sprintf(__("Class '%s' is supposed to be a migration script. Its not, its a '%s'", "event_espresso"), $class_name, get_class($class))); |
|
| 883 | 883 | } |
| 884 | 884 | $class->instantiate_from_array_of_properties($properties_array); |
| 885 | 885 | return $class; |
@@ -891,25 +891,25 @@ discard block |
||
| 891 | 891 | * @param string $plugin_slug the slug for the ee plugin we are searching for. Default is 'Core' |
| 892 | 892 | * @return string |
| 893 | 893 | */ |
| 894 | - public function get_most_up_to_date_dms($plugin_slug = 'Core'){ |
|
| 894 | + public function get_most_up_to_date_dms($plugin_slug = 'Core') { |
|
| 895 | 895 | $class_to_filepath_map = $this->get_all_data_migration_scripts_available(); |
| 896 | 896 | $most_up_to_date_dms_classname = NULL; |
| 897 | - foreach($class_to_filepath_map as $classname => $filepath){ |
|
| 898 | - if($most_up_to_date_dms_classname === NULL){ |
|
| 897 | + foreach ($class_to_filepath_map as $classname => $filepath) { |
|
| 898 | + if ($most_up_to_date_dms_classname === NULL) { |
|
| 899 | 899 | $migrates_to = $this->script_migrates_to_version($classname); |
| 900 | - $this_plugin_slug = $migrates_to[ 'slug' ]; |
|
| 900 | + $this_plugin_slug = $migrates_to['slug']; |
|
| 901 | 901 | // $version_string = $migrates_to[ 'version' ]; |
| 902 | 902 | // $details = $this->parse_dms_classname($classname); |
| 903 | - if($this_plugin_slug == $plugin_slug){ |
|
| 903 | + if ($this_plugin_slug == $plugin_slug) { |
|
| 904 | 904 | //if it's for core, it wins |
| 905 | 905 | $most_up_to_date_dms_classname = $classname; |
| 906 | 906 | } |
| 907 | 907 | //if it wasn't for core, we must keep searching for one that is! |
| 908 | 908 | continue; |
| 909 | - }else{ |
|
| 910 | - $champion_migrates_to= $this->script_migrates_to_version($most_up_to_date_dms_classname); |
|
| 909 | + } else { |
|
| 910 | + $champion_migrates_to = $this->script_migrates_to_version($most_up_to_date_dms_classname); |
|
| 911 | 911 | $contender_migrates_to = $this->script_migrates_to_version($classname); |
| 912 | - if($contender_migrates_to[ 'slug' ] == $plugin_slug && version_compare($champion_migrates_to[ 'version' ], $contender_migrates_to[ 'version' ], '<')){ |
|
| 912 | + if ($contender_migrates_to['slug'] == $plugin_slug && version_compare($champion_migrates_to['version'], $contender_migrates_to['version'], '<')) { |
|
| 913 | 913 | //so the contenders version is higher and its for Core |
| 914 | 914 | $most_up_to_date_dms_classname = $classname; |
| 915 | 915 | } |
@@ -929,11 +929,11 @@ discard block |
||
| 929 | 929 | * @param string $plugin_slug like 'Core', 'Mailchimp', 'Calendar', etc |
| 930 | 930 | * @return EE_Data_Migration_Script_Base |
| 931 | 931 | */ |
| 932 | - public function get_migration_ran( $version, $plugin_slug = 'Core' ) { |
|
| 932 | + public function get_migration_ran($version, $plugin_slug = 'Core') { |
|
| 933 | 933 | $migrations_ran = $this->get_data_migrations_ran(); |
| 934 | - if( isset( $migrations_ran[ $plugin_slug ] ) && isset( $migrations_ran[ $plugin_slug ][ $version ] ) ){ |
|
| 935 | - return $migrations_ran[ $plugin_slug ][ $version ]; |
|
| 936 | - }else{ |
|
| 934 | + if (isset($migrations_ran[$plugin_slug]) && isset($migrations_ran[$plugin_slug][$version])) { |
|
| 935 | + return $migrations_ran[$plugin_slug][$version]; |
|
| 936 | + } else { |
|
| 937 | 937 | return NULL; |
| 938 | 938 | } |
| 939 | 939 | } |
@@ -945,20 +945,20 @@ discard block |
||
| 945 | 945 | * @return bool |
| 946 | 946 | * @throws \EE_Error |
| 947 | 947 | */ |
| 948 | - public function reattempt(){ |
|
| 948 | + public function reattempt() { |
|
| 949 | 949 | //find if the last-ran script was borked |
| 950 | 950 | //set it as being non-borked (we shouldn't ever get DMSs that we don't recognize) |
| 951 | 951 | //add an 'error' saying that we attempted to reset |
| 952 | 952 | //does it have a stage that was borked too? if so make it no longer borked |
| 953 | 953 | //add an 'error' saying we attempted to reset |
| 954 | 954 | $last_ran_script = $this->get_last_ran_script(); |
| 955 | - if( $last_ran_script instanceof EE_DMS_Unknown_1_0_0 ){ |
|
| 955 | + if ($last_ran_script instanceof EE_DMS_Unknown_1_0_0) { |
|
| 956 | 956 | //if it was an error DMS, just mark it as complete (if another error occurs it will overwrite it) |
| 957 | 957 | $last_ran_script->set_completed(); |
| 958 | - }elseif( $last_ran_script instanceof EE_Data_Migration_Script_Base ) { |
|
| 958 | + }elseif ($last_ran_script instanceof EE_Data_Migration_Script_Base) { |
|
| 959 | 959 | $last_ran_script->reattempt(); |
| 960 | - }else{ |
|
| 961 | - throw new EE_Error( sprintf( __( 'Unable to reattempt the last ran migration script because it was not a valid migration script. || It was %s', 'event_espresso' ), print_r( $last_ran_script, true ) ) ); |
|
| 960 | + } else { |
|
| 961 | + throw new EE_Error(sprintf(__('Unable to reattempt the last ran migration script because it was not a valid migration script. || It was %s', 'event_espresso'), print_r($last_ran_script, true))); |
|
| 962 | 962 | } |
| 963 | 963 | return $this->_save_migrations_ran(); |
| 964 | 964 | } |
@@ -968,19 +968,19 @@ discard block |
||
| 968 | 968 | * @param string $plugin_slug like 'Core', 'Mailchimp', 'Calendar', etc |
| 969 | 969 | * @return boolean |
| 970 | 970 | */ |
| 971 | - public function migration_has_ran( $version, $plugin_slug = 'Core' ) { |
|
| 972 | - return $this->get_migration_ran( $version, $plugin_slug ) !== NULL; |
|
| 971 | + public function migration_has_ran($version, $plugin_slug = 'Core') { |
|
| 972 | + return $this->get_migration_ran($version, $plugin_slug) !== NULL; |
|
| 973 | 973 | } |
| 974 | 974 | /** |
| 975 | 975 | * Enqueues this ee plugin to have its data initialized |
| 976 | 976 | * @param string $plugin_slug either 'Core' or EE_Addon::name()'s return value |
| 977 | 977 | */ |
| 978 | - public function enqueue_db_initialization_for( $plugin_slug ) { |
|
| 978 | + public function enqueue_db_initialization_for($plugin_slug) { |
|
| 979 | 979 | $queue = $this->get_db_initialization_queue(); |
| 980 | - if( ! in_array( $plugin_slug, $queue ) ) { |
|
| 980 | + if ( ! in_array($plugin_slug, $queue)) { |
|
| 981 | 981 | $queue[] = $plugin_slug; |
| 982 | 982 | } |
| 983 | - update_option( self::db_init_queue_option_name, $queue ); |
|
| 983 | + update_option(self::db_init_queue_option_name, $queue); |
|
| 984 | 984 | } |
| 985 | 985 | /** |
| 986 | 986 | * Calls EE_Addon::initialize_db_if_no_migrations_required() on each addon |
@@ -990,26 +990,26 @@ discard block |
||
| 990 | 990 | public function initialize_db_for_enqueued_ee_plugins() { |
| 991 | 991 | // EEH_Debug_Tools::instance()->start_timer( 'initialize_db_for_enqueued_ee_plugins' ); |
| 992 | 992 | $queue = $this->get_db_initialization_queue(); |
| 993 | - foreach( $queue as $plugin_slug ) { |
|
| 994 | - $most_up_to_date_dms = $this->get_most_up_to_date_dms( $plugin_slug ); |
|
| 995 | - if( ! $most_up_to_date_dms ) { |
|
| 993 | + foreach ($queue as $plugin_slug) { |
|
| 994 | + $most_up_to_date_dms = $this->get_most_up_to_date_dms($plugin_slug); |
|
| 995 | + if ( ! $most_up_to_date_dms) { |
|
| 996 | 996 | //if there is NO DMS for this plugin, obviously there's no schema to verify anyways |
| 997 | 997 | $verify_db = false; |
| 998 | - }else{ |
|
| 999 | - $most_up_to_date_dms_migrates_to = $this->script_migrates_to_version( $most_up_to_date_dms ); |
|
| 1000 | - $verify_db = $this->database_needs_updating_to( $most_up_to_date_dms_migrates_to ); |
|
| 998 | + } else { |
|
| 999 | + $most_up_to_date_dms_migrates_to = $this->script_migrates_to_version($most_up_to_date_dms); |
|
| 1000 | + $verify_db = $this->database_needs_updating_to($most_up_to_date_dms_migrates_to); |
|
| 1001 | 1001 | } |
| 1002 | - if( $plugin_slug == 'Core' ){ |
|
| 1002 | + if ($plugin_slug == 'Core') { |
|
| 1003 | 1003 | EE_System::instance()->initialize_db_if_no_migrations_required( |
| 1004 | 1004 | false, |
| 1005 | 1005 | $verify_db |
| 1006 | 1006 | ); |
| 1007 | - }else{ |
|
| 1007 | + } else { |
|
| 1008 | 1008 | //just loop through the addons to make sure their database is setup |
| 1009 | - foreach( EE_Registry::instance()->addons as $addon ) { |
|
| 1010 | - if( $addon->name() == $plugin_slug ) { |
|
| 1009 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
| 1010 | + if ($addon->name() == $plugin_slug) { |
|
| 1011 | 1011 | |
| 1012 | - $addon->initialize_db_if_no_migrations_required( $verify_db ); |
|
| 1012 | + $addon->initialize_db_if_no_migrations_required($verify_db); |
|
| 1013 | 1013 | break; |
| 1014 | 1014 | } |
| 1015 | 1015 | } |
@@ -1019,7 +1019,7 @@ discard block |
||
| 1019 | 1019 | // EEH_Debug_Tools::instance()->show_times(); |
| 1020 | 1020 | //because we just initialized the DBs for the enqueued ee plugins |
| 1021 | 1021 | //we don't need to keep remembering which ones needed to be initialized |
| 1022 | - delete_option( self::db_init_queue_option_name ); |
|
| 1022 | + delete_option(self::db_init_queue_option_name); |
|
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | 1025 | /** |
@@ -1028,8 +1028,8 @@ discard block |
||
| 1028 | 1028 | * 'Core', or the return value of EE_Addon::name() for an addon |
| 1029 | 1029 | * @return array |
| 1030 | 1030 | */ |
| 1031 | - public function get_db_initialization_queue(){ |
|
| 1032 | - return get_option ( self::db_init_queue_option_name, array() ); |
|
| 1031 | + public function get_db_initialization_queue() { |
|
| 1032 | + return get_option(self::db_init_queue_option_name, array()); |
|
| 1033 | 1033 | } |
| 1034 | 1034 | |
| 1035 | 1035 | /** |
@@ -1038,13 +1038,13 @@ discard block |
||
| 1038 | 1038 | * @throws \EE_Error |
| 1039 | 1039 | */ |
| 1040 | 1040 | protected function _get_table_analysis() { |
| 1041 | - if( $this->_table_analysis instanceof TableAnalysis ) { |
|
| 1041 | + if ($this->_table_analysis instanceof TableAnalysis) { |
|
| 1042 | 1042 | return $this->_table_analysis; |
| 1043 | 1043 | } else { |
| 1044 | 1044 | throw new \EE_Error( |
| 1045 | 1045 | sprintf( |
| 1046 | - __( 'Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
| 1047 | - get_class( $this ) |
|
| 1046 | + __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
| 1047 | + get_class($this) |
|
| 1048 | 1048 | ) |
| 1049 | 1049 | ); |
| 1050 | 1050 | } |
@@ -1056,13 +1056,13 @@ discard block |
||
| 1056 | 1056 | * @throws \EE_Error |
| 1057 | 1057 | */ |
| 1058 | 1058 | protected function _get_table_manager() { |
| 1059 | - if( $this->_table_manager instanceof TableManager ) { |
|
| 1059 | + if ($this->_table_manager instanceof TableManager) { |
|
| 1060 | 1060 | return $this->_table_manager; |
| 1061 | 1061 | } else { |
| 1062 | 1062 | throw new \EE_Error( |
| 1063 | 1063 | sprintf( |
| 1064 | - __( 'Table manager class on class %1$s is not set properly.', 'event_espresso'), |
|
| 1065 | - get_class( $this ) |
|
| 1064 | + __('Table manager class on class %1$s is not set properly.', 'event_espresso'), |
|
| 1065 | + get_class($this) |
|
| 1066 | 1066 | ) |
| 1067 | 1067 | ); |
| 1068 | 1068 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php use EventEspresso\core\domain\entities\DbSafeDateTime; |
| 2 | 2 | |
| 3 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 3 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 4 | 4 | exit('No direct script access allowed'); |
| 5 | 5 | } |
| 6 | 6 | |
@@ -220,8 +220,8 @@ discard block |
||
| 220 | 220 | |
| 221 | 221 | default : |
| 222 | 222 | return $pretty |
| 223 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
| 224 | - : $this->_date_format . ' ' . $this->_time_format; |
|
| 223 | + ? $this->_pretty_date_format.' '.$this->_pretty_time_format |
|
| 224 | + : $this->_date_format.' '.$this->_time_format; |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
@@ -477,7 +477,7 @@ discard block |
||
| 477 | 477 | */ |
| 478 | 478 | protected function _prepare_for_display($DateTime, $schema = false) |
| 479 | 479 | { |
| 480 | - if (! $DateTime instanceof DateTime) { |
|
| 480 | + if ( ! $DateTime instanceof DateTime) { |
|
| 481 | 481 | if ($this->_nullable) { |
| 482 | 482 | return ''; |
| 483 | 483 | } else { |
@@ -512,15 +512,15 @@ discard block |
||
| 512 | 512 | if ($this->_display_timezone()) { |
| 513 | 513 | //must be explicit because schema could equal true. |
| 514 | 514 | if ($schema === 'no_html') { |
| 515 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
| 515 | + $timezone_string = ' ('.$DateTime->format('T').')'; |
|
| 516 | 516 | } else { |
| 517 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
| 517 | + $timezone_string = ' <span class="ee_dtt_timezone_string">('.$DateTime->format('T').')</span>'; |
|
| 518 | 518 | } |
| 519 | 519 | } else { |
| 520 | 520 | $timezone_string = ''; |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | - return $DateTime->format($format_string) . $timezone_string; |
|
| 523 | + return $DateTime->format($format_string).$timezone_string; |
|
| 524 | 524 | } else { |
| 525 | 525 | return $DateTime->format($format_string); |
| 526 | 526 | } |
@@ -538,7 +538,7 @@ discard block |
||
| 538 | 538 | public function prepare_for_use_in_db($datetime_value) |
| 539 | 539 | { |
| 540 | 540 | //we allow an empty value or DateTime object, but nothing else. |
| 541 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
| 541 | + if ( ! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
| 542 | 542 | throw new EE_Error( |
| 543 | 543 | __( |
| 544 | 544 | 'The incoming value being prepared for setting in the database must either be empty or a php DateTime object', |
@@ -593,7 +593,7 @@ discard block |
||
| 593 | 593 | } |
| 594 | 594 | } |
| 595 | 595 | |
| 596 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
| 596 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
| 597 | 597 | // if still no datetime object, then let's just use now |
| 598 | 598 | $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
| 599 | 599 | } |
@@ -672,7 +672,7 @@ discard block |
||
| 672 | 672 | } |
| 673 | 673 | //not a unix timestamp. So we will use the set format on this object and set timezone to |
| 674 | 674 | //create the DateTime object. |
| 675 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
| 675 | + $format = $this->_date_format.' '.$this->_time_format; |
|
| 676 | 676 | try { |
| 677 | 677 | $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
| 678 | 678 | if ($DateTime instanceof DateTime) { |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | $this->_DateTimeZone |
| 682 | 682 | ); |
| 683 | 683 | } |
| 684 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
| 684 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
| 685 | 685 | throw new EE_Error( |
| 686 | 686 | sprintf( |
| 687 | 687 | __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
@@ -567,7 +567,7 @@ discard block |
||
| 567 | 567 | * allowed) |
| 568 | 568 | * |
| 569 | 569 | * @param string $datetime_string mysql timestamp in UTC |
| 570 | - * @return mixed null | DateTime |
|
| 570 | + * @return null|DbSafeDateTime null | DateTime |
|
| 571 | 571 | * @throws \EE_Error |
| 572 | 572 | */ |
| 573 | 573 | public function prepare_for_set_from_db($datetime_string) |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | * |
| 725 | 725 | * @param \DateTimeZone $DateTimeZone |
| 726 | 726 | * @param int $time |
| 727 | - * @return mixed |
|
| 727 | + * @return integer |
|
| 728 | 728 | * @throws \DomainException |
| 729 | 729 | */ |
| 730 | 730 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php use EventEspresso\core\domain\entities\DbSafeDateTime; |
| 2 | 2 | |
| 3 | 3 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 4 | - exit('No direct script access allowed'); |
|
| 4 | + exit('No direct script access allowed'); |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -16,769 +16,769 @@ discard block |
||
| 16 | 16 | class EE_Datetime_Field extends EE_Model_Field_Base |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
| 21 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
| 22 | - * |
|
| 23 | - * @type string unix_timestamp_regex |
|
| 24 | - */ |
|
| 25 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @type string mysql_timestamp_format |
|
| 29 | - */ |
|
| 30 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @type string mysql_date_format |
|
| 34 | - */ |
|
| 35 | - const mysql_date_format = 'Y-m-d'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @type string mysql_time_format |
|
| 39 | - */ |
|
| 40 | - const mysql_time_format = 'H:i:s'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Const for using in the default value. If the field's default is set to this, |
|
| 44 | - * then we will return the time of calling `get_default_value()`, not |
|
| 45 | - * just the current time at construction |
|
| 46 | - */ |
|
| 47 | - const now = 'now'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The following properties hold the default formats for date and time. |
|
| 51 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
| 52 | - * However they can also be overridden later by the set_format() method |
|
| 53 | - * (and corresponding set_date_format, set_time_format methods); |
|
| 54 | - */ |
|
| 55 | - /** |
|
| 56 | - * @type string $_date_format |
|
| 57 | - */ |
|
| 58 | - protected $_date_format = ''; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @type string $_time_format |
|
| 62 | - */ |
|
| 63 | - protected $_time_format = ''; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @type string $_pretty_date_format |
|
| 67 | - */ |
|
| 68 | - protected $_pretty_date_format = ''; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @type string $_pretty_time_format |
|
| 72 | - */ |
|
| 73 | - protected $_pretty_time_format = ''; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @type DateTimeZone $_DateTimeZone |
|
| 77 | - */ |
|
| 78 | - protected $_DateTimeZone; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @type DateTimeZone $_UTC_DateTimeZone |
|
| 82 | - */ |
|
| 83 | - protected $_UTC_DateTimeZone; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @type DateTimeZone $_blog_DateTimeZone |
|
| 87 | - */ |
|
| 88 | - protected $_blog_DateTimeZone; |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
| 93 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
| 94 | - * and time returned via getters. |
|
| 95 | - * |
|
| 96 | - * @var mixed (null|string) |
|
| 97 | - */ |
|
| 98 | - protected $_date_time_output; |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * timezone string |
|
| 103 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
| 104 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
| 105 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
| 106 | - * |
|
| 107 | - * @var string |
|
| 108 | - */ |
|
| 109 | - protected $_timezone_string; |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
| 114 | - * offsets for comparison purposes). |
|
| 115 | - * |
|
| 116 | - * @var int |
|
| 117 | - */ |
|
| 118 | - protected $_blog_offset; |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param string $table_column |
|
| 123 | - * @param string $nice_name |
|
| 124 | - * @param bool $nullable |
|
| 125 | - * @param string $default_value |
|
| 126 | - * @param string $timezone_string |
|
| 127 | - * @param string $date_format |
|
| 128 | - * @param string $time_format |
|
| 129 | - * @param string $pretty_date_format |
|
| 130 | - * @param string $pretty_time_format |
|
| 131 | - * @throws \EE_Error |
|
| 132 | - */ |
|
| 133 | - public function __construct( |
|
| 134 | - $table_column, |
|
| 135 | - $nice_name, |
|
| 136 | - $nullable, |
|
| 137 | - $default_value, |
|
| 138 | - $timezone_string = '', |
|
| 139 | - $date_format = '', |
|
| 140 | - $time_format = '', |
|
| 141 | - $pretty_date_format = '', |
|
| 142 | - $pretty_time_format = '' |
|
| 143 | - ) { |
|
| 144 | - |
|
| 145 | - $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 146 | - $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 147 | - $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
| 148 | - $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
| 149 | - |
|
| 150 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
| 151 | - $this->set_timezone($timezone_string); |
|
| 152 | - |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function get_wpdb_data_type() |
|
| 160 | - { |
|
| 161 | - return '%s'; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @return DateTimeZone |
|
| 167 | - * @throws \EE_Error |
|
| 168 | - */ |
|
| 169 | - public function get_UTC_DateTimeZone() |
|
| 170 | - { |
|
| 171 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
| 172 | - ? $this->_UTC_DateTimeZone |
|
| 173 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @return DateTimeZone |
|
| 179 | - * @throws \EE_Error |
|
| 180 | - */ |
|
| 181 | - public function get_blog_DateTimeZone() |
|
| 182 | - { |
|
| 183 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
| 184 | - ? $this->_blog_DateTimeZone |
|
| 185 | - : $this->_create_timezone_object_from_timezone_string(''); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
| 191 | - * |
|
| 192 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
| 193 | - * timestamp |
|
| 194 | - * @return DateTime |
|
| 195 | - */ |
|
| 196 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 197 | - { |
|
| 198 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
| 204 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
| 205 | - * |
|
| 206 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
| 207 | - * @return string The final assembled format string. |
|
| 208 | - */ |
|
| 209 | - protected function _get_date_time_output($pretty = false) |
|
| 210 | - { |
|
| 211 | - |
|
| 212 | - switch ($this->_date_time_output) { |
|
| 213 | - case 'time' : |
|
| 214 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
| 215 | - break; |
|
| 216 | - |
|
| 217 | - case 'date' : |
|
| 218 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
| 219 | - break; |
|
| 220 | - |
|
| 221 | - default : |
|
| 222 | - return $pretty |
|
| 223 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
| 224 | - : $this->_date_format . ' ' . $this->_time_format; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
| 231 | - * returned (using the format properties) |
|
| 232 | - * |
|
| 233 | - * @param string $what acceptable values are 'time' or 'date'. |
|
| 234 | - * Any other value will be set but will always result |
|
| 235 | - * in both 'date' and 'time' being returned. |
|
| 236 | - * @return void |
|
| 237 | - */ |
|
| 238 | - public function set_date_time_output($what = null) |
|
| 239 | - { |
|
| 240 | - $this->_date_time_output = $what; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 246 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 247 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
| 248 | - * We also set some other properties in this method. |
|
| 249 | - * |
|
| 250 | - * @param string $timezone_string A valid timezone string as described by @link |
|
| 251 | - * http://www.php.net/manual/en/timezones.php |
|
| 252 | - * @return void |
|
| 253 | - * @throws \EE_Error |
|
| 254 | - */ |
|
| 255 | - public function set_timezone($timezone_string) |
|
| 256 | - { |
|
| 257 | - if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
| 258 | - // leave the timezone AS-IS if we already have one and |
|
| 259 | - // the function arg didn't provide one |
|
| 260 | - return; |
|
| 261 | - } |
|
| 262 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
| 263 | - $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
| 264 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * _create_timezone_object_from_timezone_name |
|
| 270 | - * |
|
| 271 | - * @access protected |
|
| 272 | - * @param string $timezone_string |
|
| 273 | - * @return \DateTimeZone |
|
| 274 | - * @throws \EE_Error |
|
| 275 | - */ |
|
| 276 | - protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
| 277 | - { |
|
| 278 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * This just returns whatever is set for the current timezone. |
|
| 284 | - * |
|
| 285 | - * @access public |
|
| 286 | - * @return string timezone string |
|
| 287 | - */ |
|
| 288 | - public function get_timezone() |
|
| 289 | - { |
|
| 290 | - return $this->_timezone_string; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * set the $_date_format property |
|
| 296 | - * |
|
| 297 | - * @access public |
|
| 298 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
| 299 | - * @param bool $pretty Whether to set pretty format or not. |
|
| 300 | - * @return void |
|
| 301 | - */ |
|
| 302 | - public function set_date_format($format, $pretty = false) |
|
| 303 | - { |
|
| 304 | - if ($pretty) { |
|
| 305 | - $this->_pretty_date_format = $format; |
|
| 306 | - } else { |
|
| 307 | - $this->_date_format = $format; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * return the $_date_format property value. |
|
| 314 | - * |
|
| 315 | - * @param bool $pretty Whether to get pretty format or not. |
|
| 316 | - * @return string |
|
| 317 | - */ |
|
| 318 | - public function get_date_format($pretty = false) |
|
| 319 | - { |
|
| 320 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * set the $_time_format property |
|
| 326 | - * |
|
| 327 | - * @access public |
|
| 328 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
| 329 | - * @param bool $pretty Whether to set pretty format or not. |
|
| 330 | - * @return void |
|
| 331 | - */ |
|
| 332 | - public function set_time_format($format, $pretty = false) |
|
| 333 | - { |
|
| 334 | - if ($pretty) { |
|
| 335 | - $this->_pretty_time_format = $format; |
|
| 336 | - } else { |
|
| 337 | - $this->_time_format = $format; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * return the $_time_format property value. |
|
| 344 | - * |
|
| 345 | - * @param bool $pretty Whether to get pretty format or not. |
|
| 346 | - * @return string |
|
| 347 | - */ |
|
| 348 | - public function get_time_format($pretty = false) |
|
| 349 | - { |
|
| 350 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * set the $_pretty_date_format property |
|
| 356 | - * |
|
| 357 | - * @access public |
|
| 358 | - * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
| 359 | - * @return void |
|
| 360 | - */ |
|
| 361 | - public function set_pretty_date_format($format) |
|
| 362 | - { |
|
| 363 | - $this->_pretty_date_format = $format; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * set the $_pretty_time_format property |
|
| 369 | - * |
|
| 370 | - * @access public |
|
| 371 | - * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
| 372 | - * @return void |
|
| 373 | - */ |
|
| 374 | - public function set_pretty_time_format($format) |
|
| 375 | - { |
|
| 376 | - $this->_pretty_time_format = $format; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Only sets the time portion of the datetime. |
|
| 382 | - * |
|
| 383 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
| 384 | - * @param DateTime $current current DateTime object for the datetime field |
|
| 385 | - * @return DateTime |
|
| 386 | - */ |
|
| 387 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
| 388 | - { |
|
| 389 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
| 390 | - // Otherwise parse the string. |
|
| 391 | - if ($time_to_set_string instanceof DateTime) { |
|
| 392 | - $parsed = array( |
|
| 393 | - 'hour' => $time_to_set_string->format('H'), |
|
| 394 | - 'minute' => $time_to_set_string->format('i'), |
|
| 395 | - 'second' => $time_to_set_string->format('s'), |
|
| 396 | - ); |
|
| 397 | - } else { |
|
| 398 | - //parse incoming string |
|
| 399 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - //make sure $current is in the correct timezone. |
|
| 403 | - $current->setTimezone($this->_DateTimeZone); |
|
| 404 | - |
|
| 405 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Only sets the date portion of the datetime. |
|
| 411 | - * |
|
| 412 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
| 413 | - * @param DateTime $current current DateTime object for the datetime field |
|
| 414 | - * @return DateTime |
|
| 415 | - */ |
|
| 416 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
| 417 | - { |
|
| 418 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
| 419 | - // Otherwise parse the string. |
|
| 420 | - if ($date_to_set_string instanceof DateTime) { |
|
| 421 | - $parsed = array( |
|
| 422 | - 'year' => $date_to_set_string->format('Y'), |
|
| 423 | - 'month' => $date_to_set_string->format('m'), |
|
| 424 | - 'day' => $date_to_set_string->format('d'), |
|
| 425 | - ); |
|
| 426 | - } else { |
|
| 427 | - //parse incoming string |
|
| 428 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - //make sure $current is in the correct timezone |
|
| 432 | - $current->setTimezone($this->_DateTimeZone); |
|
| 433 | - |
|
| 434 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
| 440 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
| 441 | - * |
|
| 442 | - * @param DateTime $DateTime |
|
| 443 | - * @return string formatted date time for given timezone |
|
| 444 | - * @throws \EE_Error |
|
| 445 | - */ |
|
| 446 | - public function prepare_for_get($DateTime) |
|
| 447 | - { |
|
| 448 | - return $this->_prepare_for_display($DateTime); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
| 454 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
| 455 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
| 456 | - * abbreviation to the date_string. |
|
| 457 | - * |
|
| 458 | - * @param mixed $DateTime |
|
| 459 | - * @param null $schema |
|
| 460 | - * @return string |
|
| 461 | - * @throws \EE_Error |
|
| 462 | - */ |
|
| 463 | - public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
| 464 | - { |
|
| 465 | - return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
| 471 | - * timezone). |
|
| 472 | - * |
|
| 473 | - * @param DateTime $DateTime |
|
| 474 | - * @param bool|string $schema |
|
| 475 | - * @return string |
|
| 476 | - * @throws \EE_Error |
|
| 477 | - */ |
|
| 478 | - protected function _prepare_for_display($DateTime, $schema = false) |
|
| 479 | - { |
|
| 480 | - if (! $DateTime instanceof DateTime) { |
|
| 481 | - if ($this->_nullable) { |
|
| 482 | - return ''; |
|
| 483 | - } else { |
|
| 484 | - if (WP_DEBUG) { |
|
| 485 | - throw new EE_Error( |
|
| 486 | - sprintf( |
|
| 487 | - __( |
|
| 488 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
| 489 | - 'event_espresso' |
|
| 490 | - ), |
|
| 491 | - $this->_nicename |
|
| 492 | - ) |
|
| 493 | - ); |
|
| 494 | - } else { |
|
| 495 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
| 496 | - EE_Error::add_error( |
|
| 497 | - sprintf( |
|
| 498 | - __( |
|
| 499 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
| 500 | - 'event_espresso' |
|
| 501 | - ), |
|
| 502 | - $this->_nicename |
|
| 503 | - ) |
|
| 504 | - ); |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - $format_string = $this->_get_date_time_output($schema); |
|
| 509 | - //make sure datetime_value is in the correct timezone (in case that's been updated). |
|
| 510 | - $DateTime->setTimezone($this->_DateTimeZone); |
|
| 511 | - if ($schema) { |
|
| 512 | - if ($this->_display_timezone()) { |
|
| 513 | - //must be explicit because schema could equal true. |
|
| 514 | - if ($schema === 'no_html') { |
|
| 515 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
| 516 | - } else { |
|
| 517 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
| 518 | - } |
|
| 519 | - } else { |
|
| 520 | - $timezone_string = ''; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - return $DateTime->format($format_string) . $timezone_string; |
|
| 524 | - } else { |
|
| 525 | - return $DateTime->format($format_string); |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
| 532 | - * timezone). |
|
| 533 | - * |
|
| 534 | - * @param mixed $datetime_value u |
|
| 535 | - * @return string mysql timestamp in UTC |
|
| 536 | - * @throws \EE_Error |
|
| 537 | - */ |
|
| 538 | - public function prepare_for_use_in_db($datetime_value) |
|
| 539 | - { |
|
| 540 | - //we allow an empty value or DateTime object, but nothing else. |
|
| 541 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
| 542 | - throw new EE_Error( |
|
| 543 | - __( |
|
| 544 | - 'The incoming value being prepared for setting in the database must either be empty or a php DateTime object', |
|
| 545 | - 'event_espresso' |
|
| 546 | - ) |
|
| 547 | - ); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - if ($datetime_value instanceof DateTime) { |
|
| 551 | - if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
| 552 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - return $datetime_value->setTimezone($this->get_UTC_DateTimeZone())->format( |
|
| 556 | - EE_Datetime_Field::mysql_timestamp_format |
|
| 557 | - ); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
| 561 | - return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
| 567 | - * allowed) |
|
| 568 | - * |
|
| 569 | - * @param string $datetime_string mysql timestamp in UTC |
|
| 570 | - * @return mixed null | DateTime |
|
| 571 | - * @throws \EE_Error |
|
| 572 | - */ |
|
| 573 | - public function prepare_for_set_from_db($datetime_string) |
|
| 574 | - { |
|
| 575 | - //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
| 576 | - if (empty($datetime_string) && $this->_nullable) { |
|
| 577 | - return null; |
|
| 578 | - } |
|
| 579 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
| 580 | - if (empty($datetime_string)) { |
|
| 581 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
| 582 | - } else { |
|
| 583 | - $DateTime = DateTime::createFromFormat( |
|
| 584 | - EE_Datetime_Field::mysql_timestamp_format, |
|
| 585 | - $datetime_string, |
|
| 586 | - $this->get_UTC_DateTimeZone() |
|
| 587 | - ); |
|
| 588 | - if ($DateTime instanceof \DateTime) { |
|
| 589 | - $DateTime = new DbSafeDateTime( |
|
| 590 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
| 591 | - $this->get_UTC_DateTimeZone() |
|
| 592 | - ); |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
| 597 | - // if still no datetime object, then let's just use now |
|
| 598 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
| 599 | - } |
|
| 600 | - // THEN apply the field's set DateTimeZone |
|
| 601 | - $DateTime->setTimezone($this->_DateTimeZone); |
|
| 602 | - |
|
| 603 | - return $DateTime; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
| 609 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
| 610 | - * If so, then true. |
|
| 611 | - * |
|
| 612 | - * @return bool true for yes false for no |
|
| 613 | - * @throws \EE_Error |
|
| 614 | - */ |
|
| 615 | - protected function _display_timezone() |
|
| 616 | - { |
|
| 617 | - |
|
| 618 | - // first let's do a comparison of timezone strings. |
|
| 619 | - // If they match then we can get out without any further calculations |
|
| 620 | - $blog_string = get_option('timezone_string'); |
|
| 621 | - if ($blog_string === $this->_timezone_string) { |
|
| 622 | - return false; |
|
| 623 | - } |
|
| 624 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
| 625 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
| 626 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
| 627 | - // now compare |
|
| 628 | - return $blog_offset !== $this_offset; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
| 634 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
| 635 | - * with. |
|
| 636 | - * |
|
| 637 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
| 638 | - * in the format that is set on the date_field (or DateTime |
|
| 639 | - * object)! |
|
| 640 | - * @return DateTime |
|
| 641 | - */ |
|
| 642 | - protected function _get_date_object($date_string) |
|
| 643 | - { |
|
| 644 | - //first if this is an empty date_string and nullable is allowed, just return null. |
|
| 645 | - if ($this->_nullable && empty($date_string)) { |
|
| 646 | - return null; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - // if incoming date |
|
| 650 | - if ($date_string instanceof DateTime) { |
|
| 651 | - $date_string->setTimezone($this->_DateTimeZone); |
|
| 652 | - |
|
| 653 | - return $date_string; |
|
| 654 | - } |
|
| 655 | - // if empty date_string and made it here. |
|
| 656 | - // Return a datetime object for now in the given timezone. |
|
| 657 | - if (empty($date_string)) { |
|
| 658 | - return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 659 | - } |
|
| 660 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
| 661 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
| 662 | - try { |
|
| 663 | - // This is operating under the assumption that the incoming Unix timestamp |
|
| 664 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
| 665 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 666 | - $DateTime->setTimestamp($date_string); |
|
| 667 | - |
|
| 668 | - return $DateTime; |
|
| 669 | - } catch (Exception $e) { |
|
| 670 | - // should be rare, but if things got fooled then let's just continue |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
| 674 | - //create the DateTime object. |
|
| 675 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
| 676 | - try { |
|
| 677 | - $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
| 678 | - if ($DateTime instanceof DateTime) { |
|
| 679 | - $DateTime = new DbSafeDateTime( |
|
| 680 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
| 681 | - $this->_DateTimeZone |
|
| 682 | - ); |
|
| 683 | - } |
|
| 684 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
| 685 | - throw new EE_Error( |
|
| 686 | - sprintf( |
|
| 687 | - __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
| 688 | - $date_string, |
|
| 689 | - $format |
|
| 690 | - ) |
|
| 691 | - ); |
|
| 692 | - } |
|
| 693 | - } catch (Exception $e) { |
|
| 694 | - // if we made it here then likely then something went really wrong. |
|
| 695 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
| 696 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - return $DateTime; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * get_timezone_transitions |
|
| 706 | - * |
|
| 707 | - * @param \DateTimeZone $DateTimeZone |
|
| 708 | - * @param int $time |
|
| 709 | - * @param bool $first_only |
|
| 710 | - * @return mixed |
|
| 711 | - */ |
|
| 712 | - public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
| 713 | - { |
|
| 714 | - $time = is_int($time) || $time === null ? $time : strtotime($time); |
|
| 715 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
| 716 | - $transitions = $DateTimeZone->getTransitions($time); |
|
| 717 | - return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * get_timezone_offset |
|
| 724 | - * |
|
| 725 | - * @param \DateTimeZone $DateTimeZone |
|
| 726 | - * @param int $time |
|
| 727 | - * @return mixed |
|
| 728 | - * @throws \DomainException |
|
| 729 | - */ |
|
| 730 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
| 731 | - { |
|
| 732 | - $transitions = $this->get_timezone_transitions($DateTimeZone, $time); |
|
| 733 | - if ( ! isset($transitions['offset'])) { |
|
| 734 | - throw new DomainException(); |
|
| 735 | - } |
|
| 736 | - return $transitions['offset']; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
| 742 | - * |
|
| 743 | - * @param string $timezone_string |
|
| 744 | - * @return string abbreviation |
|
| 745 | - * @throws \EE_Error |
|
| 746 | - */ |
|
| 747 | - public function get_timezone_abbrev($timezone_string) |
|
| 748 | - { |
|
| 749 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
| 750 | - $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
| 751 | - |
|
| 752 | - return $dateTime->format('T'); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - /** |
|
| 756 | - * Overrides the parent to allow for having a dynamic "now" value |
|
| 757 | - * |
|
| 758 | - * @return mixed |
|
| 759 | - */ |
|
| 760 | - public function get_default_value() |
|
| 761 | - { |
|
| 762 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
| 763 | - return time(); |
|
| 764 | - } else { |
|
| 765 | - return parent::get_default_value(); |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * This returns elements used to represent this field in the json schema. |
|
| 772 | - * |
|
| 773 | - * @link http://json-schema.org/ |
|
| 774 | - * @return array |
|
| 775 | - */ |
|
| 776 | - public function get_schema() |
|
| 777 | - { |
|
| 778 | - return array( |
|
| 779 | - 'description' => $this->get_nicename(), |
|
| 780 | - 'type' => 'string', |
|
| 781 | - 'format' => 'date-time' |
|
| 782 | - ); |
|
| 783 | - } |
|
| 19 | + /** |
|
| 20 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
| 21 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
| 22 | + * |
|
| 23 | + * @type string unix_timestamp_regex |
|
| 24 | + */ |
|
| 25 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @type string mysql_timestamp_format |
|
| 29 | + */ |
|
| 30 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @type string mysql_date_format |
|
| 34 | + */ |
|
| 35 | + const mysql_date_format = 'Y-m-d'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @type string mysql_time_format |
|
| 39 | + */ |
|
| 40 | + const mysql_time_format = 'H:i:s'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Const for using in the default value. If the field's default is set to this, |
|
| 44 | + * then we will return the time of calling `get_default_value()`, not |
|
| 45 | + * just the current time at construction |
|
| 46 | + */ |
|
| 47 | + const now = 'now'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The following properties hold the default formats for date and time. |
|
| 51 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
| 52 | + * However they can also be overridden later by the set_format() method |
|
| 53 | + * (and corresponding set_date_format, set_time_format methods); |
|
| 54 | + */ |
|
| 55 | + /** |
|
| 56 | + * @type string $_date_format |
|
| 57 | + */ |
|
| 58 | + protected $_date_format = ''; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @type string $_time_format |
|
| 62 | + */ |
|
| 63 | + protected $_time_format = ''; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @type string $_pretty_date_format |
|
| 67 | + */ |
|
| 68 | + protected $_pretty_date_format = ''; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @type string $_pretty_time_format |
|
| 72 | + */ |
|
| 73 | + protected $_pretty_time_format = ''; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @type DateTimeZone $_DateTimeZone |
|
| 77 | + */ |
|
| 78 | + protected $_DateTimeZone; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @type DateTimeZone $_UTC_DateTimeZone |
|
| 82 | + */ |
|
| 83 | + protected $_UTC_DateTimeZone; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @type DateTimeZone $_blog_DateTimeZone |
|
| 87 | + */ |
|
| 88 | + protected $_blog_DateTimeZone; |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
| 93 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
| 94 | + * and time returned via getters. |
|
| 95 | + * |
|
| 96 | + * @var mixed (null|string) |
|
| 97 | + */ |
|
| 98 | + protected $_date_time_output; |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * timezone string |
|
| 103 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
| 104 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
| 105 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
| 106 | + * |
|
| 107 | + * @var string |
|
| 108 | + */ |
|
| 109 | + protected $_timezone_string; |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
| 114 | + * offsets for comparison purposes). |
|
| 115 | + * |
|
| 116 | + * @var int |
|
| 117 | + */ |
|
| 118 | + protected $_blog_offset; |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param string $table_column |
|
| 123 | + * @param string $nice_name |
|
| 124 | + * @param bool $nullable |
|
| 125 | + * @param string $default_value |
|
| 126 | + * @param string $timezone_string |
|
| 127 | + * @param string $date_format |
|
| 128 | + * @param string $time_format |
|
| 129 | + * @param string $pretty_date_format |
|
| 130 | + * @param string $pretty_time_format |
|
| 131 | + * @throws \EE_Error |
|
| 132 | + */ |
|
| 133 | + public function __construct( |
|
| 134 | + $table_column, |
|
| 135 | + $nice_name, |
|
| 136 | + $nullable, |
|
| 137 | + $default_value, |
|
| 138 | + $timezone_string = '', |
|
| 139 | + $date_format = '', |
|
| 140 | + $time_format = '', |
|
| 141 | + $pretty_date_format = '', |
|
| 142 | + $pretty_time_format = '' |
|
| 143 | + ) { |
|
| 144 | + |
|
| 145 | + $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
| 146 | + $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
| 147 | + $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
| 148 | + $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
| 149 | + |
|
| 150 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
| 151 | + $this->set_timezone($timezone_string); |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function get_wpdb_data_type() |
|
| 160 | + { |
|
| 161 | + return '%s'; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @return DateTimeZone |
|
| 167 | + * @throws \EE_Error |
|
| 168 | + */ |
|
| 169 | + public function get_UTC_DateTimeZone() |
|
| 170 | + { |
|
| 171 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
| 172 | + ? $this->_UTC_DateTimeZone |
|
| 173 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @return DateTimeZone |
|
| 179 | + * @throws \EE_Error |
|
| 180 | + */ |
|
| 181 | + public function get_blog_DateTimeZone() |
|
| 182 | + { |
|
| 183 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
| 184 | + ? $this->_blog_DateTimeZone |
|
| 185 | + : $this->_create_timezone_object_from_timezone_string(''); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
| 191 | + * |
|
| 192 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
| 193 | + * timestamp |
|
| 194 | + * @return DateTime |
|
| 195 | + */ |
|
| 196 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 197 | + { |
|
| 198 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
| 204 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
| 205 | + * |
|
| 206 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
| 207 | + * @return string The final assembled format string. |
|
| 208 | + */ |
|
| 209 | + protected function _get_date_time_output($pretty = false) |
|
| 210 | + { |
|
| 211 | + |
|
| 212 | + switch ($this->_date_time_output) { |
|
| 213 | + case 'time' : |
|
| 214 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
| 215 | + break; |
|
| 216 | + |
|
| 217 | + case 'date' : |
|
| 218 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
| 219 | + break; |
|
| 220 | + |
|
| 221 | + default : |
|
| 222 | + return $pretty |
|
| 223 | + ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
| 224 | + : $this->_date_format . ' ' . $this->_time_format; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
| 231 | + * returned (using the format properties) |
|
| 232 | + * |
|
| 233 | + * @param string $what acceptable values are 'time' or 'date'. |
|
| 234 | + * Any other value will be set but will always result |
|
| 235 | + * in both 'date' and 'time' being returned. |
|
| 236 | + * @return void |
|
| 237 | + */ |
|
| 238 | + public function set_date_time_output($what = null) |
|
| 239 | + { |
|
| 240 | + $this->_date_time_output = $what; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 246 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 247 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
| 248 | + * We also set some other properties in this method. |
|
| 249 | + * |
|
| 250 | + * @param string $timezone_string A valid timezone string as described by @link |
|
| 251 | + * http://www.php.net/manual/en/timezones.php |
|
| 252 | + * @return void |
|
| 253 | + * @throws \EE_Error |
|
| 254 | + */ |
|
| 255 | + public function set_timezone($timezone_string) |
|
| 256 | + { |
|
| 257 | + if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
| 258 | + // leave the timezone AS-IS if we already have one and |
|
| 259 | + // the function arg didn't provide one |
|
| 260 | + return; |
|
| 261 | + } |
|
| 262 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
| 263 | + $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
| 264 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * _create_timezone_object_from_timezone_name |
|
| 270 | + * |
|
| 271 | + * @access protected |
|
| 272 | + * @param string $timezone_string |
|
| 273 | + * @return \DateTimeZone |
|
| 274 | + * @throws \EE_Error |
|
| 275 | + */ |
|
| 276 | + protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
| 277 | + { |
|
| 278 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * This just returns whatever is set for the current timezone. |
|
| 284 | + * |
|
| 285 | + * @access public |
|
| 286 | + * @return string timezone string |
|
| 287 | + */ |
|
| 288 | + public function get_timezone() |
|
| 289 | + { |
|
| 290 | + return $this->_timezone_string; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * set the $_date_format property |
|
| 296 | + * |
|
| 297 | + * @access public |
|
| 298 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
| 299 | + * @param bool $pretty Whether to set pretty format or not. |
|
| 300 | + * @return void |
|
| 301 | + */ |
|
| 302 | + public function set_date_format($format, $pretty = false) |
|
| 303 | + { |
|
| 304 | + if ($pretty) { |
|
| 305 | + $this->_pretty_date_format = $format; |
|
| 306 | + } else { |
|
| 307 | + $this->_date_format = $format; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * return the $_date_format property value. |
|
| 314 | + * |
|
| 315 | + * @param bool $pretty Whether to get pretty format or not. |
|
| 316 | + * @return string |
|
| 317 | + */ |
|
| 318 | + public function get_date_format($pretty = false) |
|
| 319 | + { |
|
| 320 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * set the $_time_format property |
|
| 326 | + * |
|
| 327 | + * @access public |
|
| 328 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
| 329 | + * @param bool $pretty Whether to set pretty format or not. |
|
| 330 | + * @return void |
|
| 331 | + */ |
|
| 332 | + public function set_time_format($format, $pretty = false) |
|
| 333 | + { |
|
| 334 | + if ($pretty) { |
|
| 335 | + $this->_pretty_time_format = $format; |
|
| 336 | + } else { |
|
| 337 | + $this->_time_format = $format; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * return the $_time_format property value. |
|
| 344 | + * |
|
| 345 | + * @param bool $pretty Whether to get pretty format or not. |
|
| 346 | + * @return string |
|
| 347 | + */ |
|
| 348 | + public function get_time_format($pretty = false) |
|
| 349 | + { |
|
| 350 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * set the $_pretty_date_format property |
|
| 356 | + * |
|
| 357 | + * @access public |
|
| 358 | + * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
| 359 | + * @return void |
|
| 360 | + */ |
|
| 361 | + public function set_pretty_date_format($format) |
|
| 362 | + { |
|
| 363 | + $this->_pretty_date_format = $format; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * set the $_pretty_time_format property |
|
| 369 | + * |
|
| 370 | + * @access public |
|
| 371 | + * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
| 372 | + * @return void |
|
| 373 | + */ |
|
| 374 | + public function set_pretty_time_format($format) |
|
| 375 | + { |
|
| 376 | + $this->_pretty_time_format = $format; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Only sets the time portion of the datetime. |
|
| 382 | + * |
|
| 383 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
| 384 | + * @param DateTime $current current DateTime object for the datetime field |
|
| 385 | + * @return DateTime |
|
| 386 | + */ |
|
| 387 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
| 388 | + { |
|
| 389 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
| 390 | + // Otherwise parse the string. |
|
| 391 | + if ($time_to_set_string instanceof DateTime) { |
|
| 392 | + $parsed = array( |
|
| 393 | + 'hour' => $time_to_set_string->format('H'), |
|
| 394 | + 'minute' => $time_to_set_string->format('i'), |
|
| 395 | + 'second' => $time_to_set_string->format('s'), |
|
| 396 | + ); |
|
| 397 | + } else { |
|
| 398 | + //parse incoming string |
|
| 399 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + //make sure $current is in the correct timezone. |
|
| 403 | + $current->setTimezone($this->_DateTimeZone); |
|
| 404 | + |
|
| 405 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Only sets the date portion of the datetime. |
|
| 411 | + * |
|
| 412 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
| 413 | + * @param DateTime $current current DateTime object for the datetime field |
|
| 414 | + * @return DateTime |
|
| 415 | + */ |
|
| 416 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
| 417 | + { |
|
| 418 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
| 419 | + // Otherwise parse the string. |
|
| 420 | + if ($date_to_set_string instanceof DateTime) { |
|
| 421 | + $parsed = array( |
|
| 422 | + 'year' => $date_to_set_string->format('Y'), |
|
| 423 | + 'month' => $date_to_set_string->format('m'), |
|
| 424 | + 'day' => $date_to_set_string->format('d'), |
|
| 425 | + ); |
|
| 426 | + } else { |
|
| 427 | + //parse incoming string |
|
| 428 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + //make sure $current is in the correct timezone |
|
| 432 | + $current->setTimezone($this->_DateTimeZone); |
|
| 433 | + |
|
| 434 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
| 440 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
| 441 | + * |
|
| 442 | + * @param DateTime $DateTime |
|
| 443 | + * @return string formatted date time for given timezone |
|
| 444 | + * @throws \EE_Error |
|
| 445 | + */ |
|
| 446 | + public function prepare_for_get($DateTime) |
|
| 447 | + { |
|
| 448 | + return $this->_prepare_for_display($DateTime); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
| 454 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
| 455 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
| 456 | + * abbreviation to the date_string. |
|
| 457 | + * |
|
| 458 | + * @param mixed $DateTime |
|
| 459 | + * @param null $schema |
|
| 460 | + * @return string |
|
| 461 | + * @throws \EE_Error |
|
| 462 | + */ |
|
| 463 | + public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
| 464 | + { |
|
| 465 | + return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
| 471 | + * timezone). |
|
| 472 | + * |
|
| 473 | + * @param DateTime $DateTime |
|
| 474 | + * @param bool|string $schema |
|
| 475 | + * @return string |
|
| 476 | + * @throws \EE_Error |
|
| 477 | + */ |
|
| 478 | + protected function _prepare_for_display($DateTime, $schema = false) |
|
| 479 | + { |
|
| 480 | + if (! $DateTime instanceof DateTime) { |
|
| 481 | + if ($this->_nullable) { |
|
| 482 | + return ''; |
|
| 483 | + } else { |
|
| 484 | + if (WP_DEBUG) { |
|
| 485 | + throw new EE_Error( |
|
| 486 | + sprintf( |
|
| 487 | + __( |
|
| 488 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
| 489 | + 'event_espresso' |
|
| 490 | + ), |
|
| 491 | + $this->_nicename |
|
| 492 | + ) |
|
| 493 | + ); |
|
| 494 | + } else { |
|
| 495 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
| 496 | + EE_Error::add_error( |
|
| 497 | + sprintf( |
|
| 498 | + __( |
|
| 499 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
| 500 | + 'event_espresso' |
|
| 501 | + ), |
|
| 502 | + $this->_nicename |
|
| 503 | + ) |
|
| 504 | + ); |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + $format_string = $this->_get_date_time_output($schema); |
|
| 509 | + //make sure datetime_value is in the correct timezone (in case that's been updated). |
|
| 510 | + $DateTime->setTimezone($this->_DateTimeZone); |
|
| 511 | + if ($schema) { |
|
| 512 | + if ($this->_display_timezone()) { |
|
| 513 | + //must be explicit because schema could equal true. |
|
| 514 | + if ($schema === 'no_html') { |
|
| 515 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
| 516 | + } else { |
|
| 517 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
| 518 | + } |
|
| 519 | + } else { |
|
| 520 | + $timezone_string = ''; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + return $DateTime->format($format_string) . $timezone_string; |
|
| 524 | + } else { |
|
| 525 | + return $DateTime->format($format_string); |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
| 532 | + * timezone). |
|
| 533 | + * |
|
| 534 | + * @param mixed $datetime_value u |
|
| 535 | + * @return string mysql timestamp in UTC |
|
| 536 | + * @throws \EE_Error |
|
| 537 | + */ |
|
| 538 | + public function prepare_for_use_in_db($datetime_value) |
|
| 539 | + { |
|
| 540 | + //we allow an empty value or DateTime object, but nothing else. |
|
| 541 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
| 542 | + throw new EE_Error( |
|
| 543 | + __( |
|
| 544 | + 'The incoming value being prepared for setting in the database must either be empty or a php DateTime object', |
|
| 545 | + 'event_espresso' |
|
| 546 | + ) |
|
| 547 | + ); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + if ($datetime_value instanceof DateTime) { |
|
| 551 | + if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
| 552 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + return $datetime_value->setTimezone($this->get_UTC_DateTimeZone())->format( |
|
| 556 | + EE_Datetime_Field::mysql_timestamp_format |
|
| 557 | + ); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
| 561 | + return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
| 567 | + * allowed) |
|
| 568 | + * |
|
| 569 | + * @param string $datetime_string mysql timestamp in UTC |
|
| 570 | + * @return mixed null | DateTime |
|
| 571 | + * @throws \EE_Error |
|
| 572 | + */ |
|
| 573 | + public function prepare_for_set_from_db($datetime_string) |
|
| 574 | + { |
|
| 575 | + //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
| 576 | + if (empty($datetime_string) && $this->_nullable) { |
|
| 577 | + return null; |
|
| 578 | + } |
|
| 579 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
| 580 | + if (empty($datetime_string)) { |
|
| 581 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
| 582 | + } else { |
|
| 583 | + $DateTime = DateTime::createFromFormat( |
|
| 584 | + EE_Datetime_Field::mysql_timestamp_format, |
|
| 585 | + $datetime_string, |
|
| 586 | + $this->get_UTC_DateTimeZone() |
|
| 587 | + ); |
|
| 588 | + if ($DateTime instanceof \DateTime) { |
|
| 589 | + $DateTime = new DbSafeDateTime( |
|
| 590 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
| 591 | + $this->get_UTC_DateTimeZone() |
|
| 592 | + ); |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
| 597 | + // if still no datetime object, then let's just use now |
|
| 598 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
| 599 | + } |
|
| 600 | + // THEN apply the field's set DateTimeZone |
|
| 601 | + $DateTime->setTimezone($this->_DateTimeZone); |
|
| 602 | + |
|
| 603 | + return $DateTime; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
| 609 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
| 610 | + * If so, then true. |
|
| 611 | + * |
|
| 612 | + * @return bool true for yes false for no |
|
| 613 | + * @throws \EE_Error |
|
| 614 | + */ |
|
| 615 | + protected function _display_timezone() |
|
| 616 | + { |
|
| 617 | + |
|
| 618 | + // first let's do a comparison of timezone strings. |
|
| 619 | + // If they match then we can get out without any further calculations |
|
| 620 | + $blog_string = get_option('timezone_string'); |
|
| 621 | + if ($blog_string === $this->_timezone_string) { |
|
| 622 | + return false; |
|
| 623 | + } |
|
| 624 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
| 625 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
| 626 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
| 627 | + // now compare |
|
| 628 | + return $blog_offset !== $this_offset; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
| 634 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
| 635 | + * with. |
|
| 636 | + * |
|
| 637 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
| 638 | + * in the format that is set on the date_field (or DateTime |
|
| 639 | + * object)! |
|
| 640 | + * @return DateTime |
|
| 641 | + */ |
|
| 642 | + protected function _get_date_object($date_string) |
|
| 643 | + { |
|
| 644 | + //first if this is an empty date_string and nullable is allowed, just return null. |
|
| 645 | + if ($this->_nullable && empty($date_string)) { |
|
| 646 | + return null; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + // if incoming date |
|
| 650 | + if ($date_string instanceof DateTime) { |
|
| 651 | + $date_string->setTimezone($this->_DateTimeZone); |
|
| 652 | + |
|
| 653 | + return $date_string; |
|
| 654 | + } |
|
| 655 | + // if empty date_string and made it here. |
|
| 656 | + // Return a datetime object for now in the given timezone. |
|
| 657 | + if (empty($date_string)) { |
|
| 658 | + return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 659 | + } |
|
| 660 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
| 661 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
| 662 | + try { |
|
| 663 | + // This is operating under the assumption that the incoming Unix timestamp |
|
| 664 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
| 665 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 666 | + $DateTime->setTimestamp($date_string); |
|
| 667 | + |
|
| 668 | + return $DateTime; |
|
| 669 | + } catch (Exception $e) { |
|
| 670 | + // should be rare, but if things got fooled then let's just continue |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
| 674 | + //create the DateTime object. |
|
| 675 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
| 676 | + try { |
|
| 677 | + $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
| 678 | + if ($DateTime instanceof DateTime) { |
|
| 679 | + $DateTime = new DbSafeDateTime( |
|
| 680 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
| 681 | + $this->_DateTimeZone |
|
| 682 | + ); |
|
| 683 | + } |
|
| 684 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
| 685 | + throw new EE_Error( |
|
| 686 | + sprintf( |
|
| 687 | + __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
| 688 | + $date_string, |
|
| 689 | + $format |
|
| 690 | + ) |
|
| 691 | + ); |
|
| 692 | + } |
|
| 693 | + } catch (Exception $e) { |
|
| 694 | + // if we made it here then likely then something went really wrong. |
|
| 695 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
| 696 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + return $DateTime; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * get_timezone_transitions |
|
| 706 | + * |
|
| 707 | + * @param \DateTimeZone $DateTimeZone |
|
| 708 | + * @param int $time |
|
| 709 | + * @param bool $first_only |
|
| 710 | + * @return mixed |
|
| 711 | + */ |
|
| 712 | + public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
| 713 | + { |
|
| 714 | + $time = is_int($time) || $time === null ? $time : strtotime($time); |
|
| 715 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
| 716 | + $transitions = $DateTimeZone->getTransitions($time); |
|
| 717 | + return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * get_timezone_offset |
|
| 724 | + * |
|
| 725 | + * @param \DateTimeZone $DateTimeZone |
|
| 726 | + * @param int $time |
|
| 727 | + * @return mixed |
|
| 728 | + * @throws \DomainException |
|
| 729 | + */ |
|
| 730 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
| 731 | + { |
|
| 732 | + $transitions = $this->get_timezone_transitions($DateTimeZone, $time); |
|
| 733 | + if ( ! isset($transitions['offset'])) { |
|
| 734 | + throw new DomainException(); |
|
| 735 | + } |
|
| 736 | + return $transitions['offset']; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
| 742 | + * |
|
| 743 | + * @param string $timezone_string |
|
| 744 | + * @return string abbreviation |
|
| 745 | + * @throws \EE_Error |
|
| 746 | + */ |
|
| 747 | + public function get_timezone_abbrev($timezone_string) |
|
| 748 | + { |
|
| 749 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
| 750 | + $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
| 751 | + |
|
| 752 | + return $dateTime->format('T'); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * Overrides the parent to allow for having a dynamic "now" value |
|
| 757 | + * |
|
| 758 | + * @return mixed |
|
| 759 | + */ |
|
| 760 | + public function get_default_value() |
|
| 761 | + { |
|
| 762 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
| 763 | + return time(); |
|
| 764 | + } else { |
|
| 765 | + return parent::get_default_value(); |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * This returns elements used to represent this field in the json schema. |
|
| 772 | + * |
|
| 773 | + * @link http://json-schema.org/ |
|
| 774 | + * @return array |
|
| 775 | + */ |
|
| 776 | + public function get_schema() |
|
| 777 | + { |
|
| 778 | + return array( |
|
| 779 | + 'description' => $this->get_nicename(), |
|
| 780 | + 'type' => 'string', |
|
| 781 | + 'format' => 'date-time' |
|
| 782 | + ); |
|
| 783 | + } |
|
| 784 | 784 | } |
@@ -15,47 +15,47 @@ |
||
| 15 | 15 | { |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
| 20 | - * |
|
| 21 | - * @param type $value_on_field_to_be_outputted |
|
| 22 | - * @param type $schema |
|
| 23 | - * @return string |
|
| 24 | - */ |
|
| 25 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
| 26 | - { |
|
| 27 | - if ($schema == 'form_input') { |
|
| 28 | - return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
| 29 | - } elseif ($schema == 'no_wpautop') { |
|
| 30 | - return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
| 31 | - } else { |
|
| 32 | - return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
| 33 | - } |
|
| 34 | - } |
|
| 18 | + /** |
|
| 19 | + * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
| 20 | + * |
|
| 21 | + * @param type $value_on_field_to_be_outputted |
|
| 22 | + * @param type $schema |
|
| 23 | + * @return string |
|
| 24 | + */ |
|
| 25 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
| 26 | + { |
|
| 27 | + if ($schema == 'form_input') { |
|
| 28 | + return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
| 29 | + } elseif ($schema == 'no_wpautop') { |
|
| 30 | + return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
| 31 | + } else { |
|
| 32 | + return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | |
| 38 | - public function get_schema() |
|
| 39 | - { |
|
| 40 | - $schema = parent::get_schema(); |
|
| 41 | - $schema['type'] = 'object'; |
|
| 42 | - $schema['properties'] = array( |
|
| 43 | - 'raw' => array( |
|
| 44 | - 'description' => sprintf( |
|
| 45 | - __('%s - the value in the database.', 'event_espresso'), |
|
| 46 | - $this->get_nicename() |
|
| 47 | - ), |
|
| 48 | - 'type' => 'string' |
|
| 49 | - ), |
|
| 50 | - 'rendered' => array( |
|
| 51 | - 'description' => sprintf( |
|
| 52 | - __('%s - transformed for display.', 'event_espresso'), |
|
| 53 | - $this->get_nicename() |
|
| 54 | - ), |
|
| 55 | - 'type' => 'string', |
|
| 56 | - 'readonly' => true |
|
| 57 | - ) |
|
| 58 | - ); |
|
| 59 | - return $schema; |
|
| 60 | - } |
|
| 38 | + public function get_schema() |
|
| 39 | + { |
|
| 40 | + $schema = parent::get_schema(); |
|
| 41 | + $schema['type'] = 'object'; |
|
| 42 | + $schema['properties'] = array( |
|
| 43 | + 'raw' => array( |
|
| 44 | + 'description' => sprintf( |
|
| 45 | + __('%s - the value in the database.', 'event_espresso'), |
|
| 46 | + $this->get_nicename() |
|
| 47 | + ), |
|
| 48 | + 'type' => 'string' |
|
| 49 | + ), |
|
| 50 | + 'rendered' => array( |
|
| 51 | + 'description' => sprintf( |
|
| 52 | + __('%s - transformed for display.', 'event_espresso'), |
|
| 53 | + $this->get_nicename() |
|
| 54 | + ), |
|
| 55 | + 'type' => 'string', |
|
| 56 | + 'readonly' => true |
|
| 57 | + ) |
|
| 58 | + ); |
|
| 59 | + return $schema; |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | \ No newline at end of file |