/**
  * Takes the default query parameters, and traverses them, adding the model relation chain
  * onto them (intelligently doesn't do that to logic query params like NOT, OR, and AND)
  * @param array $where_conditions
  * @param string $model_relation_chain
  * @return array
  * @throws \EE_Error
  */
 public function prepare_where_conditions_for_querying($where_conditions, $model_relation_chain)
 {
     $where_conditions_with_model_relation_chain_prefixes = array();
     if (!is_array($where_conditions)) {
         $where_conditions = array();
     }
     foreach ($where_conditions as $key => $value) {
         if (in_array($key, array('OR', 'AND', 'NOT')) || strpos($key, 'OR*') !== false || strpos($key, 'AND*') !== false || strpos($key, 'NOT*') !== false) {
             $where_conditions_with_model_relation_chain_prefixes[$key] = $this->prepare_where_conditions_for_querying($value, $model_relation_chain);
         } else {
             if ($model_relation_chain != '' && $model_relation_chain[strlen($model_relation_chain) - 1] != '.') {
                 $model_relation_chain = $model_relation_chain . ".";
             }
             //check for the current user id place holder, and if present change it
             if ($value === self::current_user_placeholder) {
                 $value = get_current_user_id();
             }
             //check for user field placeholder
             if ($key == self::user_field_name_placeholder) {
                 if (!$this->_model->wp_user_field_name()) {
                     throw new EE_Error(sprintf(__('There is no foreign key to the WP_User model on model %s. Please either modify your default where conditions, add a _model_chain_to_wp_user onto the model, or a proper EE_WP_User_Field onto the model', 'event_espresso'), $this->_model->get_this_model_name()));
                 }
                 $key = $this->_model->wp_user_field_name();
             }
             $where_conditions_with_model_relation_chain_prefixes[$model_relation_chain . $key] = $value;
         }
     }
     return $where_conditions_with_model_relation_chain_prefixes;
 }
 /**
  * After the form section is initially created, call this to sanitize the data in the submission
  * which relates to this form section, validate it, and set it as properties on the form.
  * @param array $req_data should usually be $_REQUEST (the default). However, you CAN
  * supply a different array. Consider using set_defaults() instead however. (If you rendered
  * the form in the page using echo $form_x->get_html() the inputs will have the correct name
  * in the request data for this function to find them and populate the form with them.
  * If you have a flat form (with only input subsections), you can supply a flat array where keys
  * are the form input names and values are their values)
  * @param boolean $validate whether or not to perform validation on this data. Default is,
  * of course, to validate that data, and set errors on the invalid values. But if the data
  * has already been validated (eg you validated the data then stored it in the DB) you may want
  * to skip this step.
  * @return void
  */
 public function receive_form_submission($req_data = NULL, $validate = TRUE)
 {
     parent::receive_form_submission($req_data, $validate);
     //create or set the model object, if it isn't already
     if (!$this->_model_object) {
         //check to see if the form indicates a PK, in which case we want to only retrieve it and update it
         $pk_name = $this->_model->primary_key_name();
         $model_obj = $this->_model->get_one_by_ID($this->get_input_value($pk_name));
         if ($model_obj) {
             $this->_model_object = $model_obj;
         } else {
             $this->_model_object = EE_Registry::instance()->load_class($this->_model->get_this_model_name());
         }
     }
     //ok so the model object is set. Just set it with the submitted form data (don't save yet though)
     foreach ($this->inputs_values_corresponding_to_model_fields() as $field_name => $field_value) {
         //only set the non-primary key
         if ($field_name != $this->_model->primary_key_name()) {
             $this->_model_object->set($field_name, $field_value);
         }
     }
 }
 /**
  * Gets all the fields that should exist on this model right now
  *
  * @param \EEM_Base $model
  * @return array|\EE_Model_Field_Base[]
  */
 public function fields_on_model_in_this_version($model)
 {
     if (!isset($this->_cached_fields_on_models[$model->get_this_model_name()])) {
         //get all model changes between the requested version and current core version
         $changes = $this->model_changes_between_requested_version_and_current();
         //fetch all fields currently on this model
         $current_fields = $model->field_settings();
         //remove all fields that have been added since
         foreach ($changes as $version => $changes_in_version) {
             if (isset($changes_in_version[$model->get_this_model_name()]) && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added) {
                 $current_fields = array_diff_key($current_fields, array_flip($changes_in_version[$model->get_this_model_name()]));
             }
         }
         $this->_cached_fields_on_models = $current_fields;
     }
     return $this->_cached_fields_on_models;
 }
 /**
  * Retrieves the value for this calculation
  *
  * @param \EEM_Base                                               type $model
  * @param string                                                  $field_name
  * @param array                                                   $wpdb_row
  * @param \WP_REST_Request
  * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller
  * @return mixed|null
  * @throws \EE_Error
  */
 public function retrieve_calculated_field_value(\EEM_Base $model, $field_name, $wpdb_row, $rest_request, Base $controller)
 {
     $mapping = $this->mapping();
     if (isset($mapping[$model->get_this_model_name()]) && isset($mapping[$model->get_this_model_name()][$field_name])) {
         $classname = $mapping[$model->get_this_model_name()][$field_name];
         return call_user_func(array($classname, $field_name), $wpdb_row, $rest_request, $controller);
     }
     throw new Rest_Exception('calculated_field_does_not_exist', sprintf(__('There is no calculated field %1$s on resource %2$s', 'event_espresso'), $field_name, $model->get_this_model_name()));
 }
Beispiel #5
0
 /**
 * Gets links we want to add to the response
 *
 *@global \WP_REST_Server $wp_rest_server
 * @param \EEM_Base $model
 * @param array $db_row
 * @param array $entity_array
 * @return array the _links item in the entity
 */
 protected function _get_entity_links($model, $db_row, $entity_array)
 {
     //add basic links
     $links = array('self' => array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) . '/' . $entity_array[$model->primary_key_name()]))), 'collection' => array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name())))));
     //add link to the wp core endpoint, if wp api is active
     global $wp_rest_server;
     if ($model instanceof \EEM_CPT_Base && $wp_rest_server instanceof \WP_REST_Server && $wp_rest_server->get_route_options('/wp/v2/posts')) {
         $links[\EED_Core_Rest_Api::ee_api_link_namespace . 'self_wp_post'] = array(array('href' => rest_url('/wp/v2/posts/' . $db_row[$model->get_primary_key_field()->get_qualified_column()]), 'single' => true));
     }
     //add links to related models
     foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) {
         $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj);
         $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) . '/' . $entity_array[$model->primary_key_name()] . '/' . $related_model_part), 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false));
     }
     return $links;
 }
 /**
  * Given the model object data, finds the row to update and updates it
  * @param string|int $id_in_csv
  * @param array $model_object_data
  * @param EEM_Base $model
  * @param array $old_db_to_new_db_mapping
  * @return array updated $old_db_to_new_db_mapping
  */
 protected function _update_from_data_array($id_in_csv, $model_object_data, $model, $old_db_to_new_db_mapping)
 {
     try {
         //let's keep two copies of the model object data:
         //one for performing an update, one for everthing else
         $model_object_data_for_update = $model_object_data;
         if ($model->has_primary_key_field()) {
             $conditions = array($model->primary_key_name() => $model_object_data[$model->primary_key_name()]);
             //remove the primary key because we shouldn't use it for updating
             unset($model_object_data_for_update[$model->primary_key_name()]);
         } elseif ($model->get_combined_primary_key_fields() > 1) {
             $conditions = array();
             foreach ($model->get_combined_primary_key_fields() as $key_field) {
                 $conditions[$key_field->get_name()] = $model_object_data[$key_field->get_name()];
             }
         } else {
             $model->primary_key_name();
             //this shoudl just throw an exception, explaining that we dont have a primary key (or a combine dkey)
         }
         $success = $model->update($model_object_data_for_update, array($conditions));
         if ($success) {
             $this->_total_updates++;
             EE_Error::add_success(sprintf(__("Successfully updated %s with csv data %s", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data_for_update)));
             //we should still record the mapping even though it was an update
             //because if we were going to insert somethign but it was going to conflict
             //we would have last-minute decided to update. So we'd like to know what we updated
             //and so we record what record ended up being updated using the mapping
             if ($model->has_primary_key_field()) {
                 $new_key_for_mapping = $model_object_data[$model->primary_key_name()];
             } else {
                 //no primary key just a combined key
                 $new_key_for_mapping = $model->get_index_primary_key_string($model_object_data);
             }
             $old_db_to_new_db_mapping[$model->get_this_model_name()][$id_in_csv] = $new_key_for_mapping;
         } else {
             $matched_items = $model->get_all(array($conditions));
             if (!$matched_items) {
                 //no items were matched (so we shouldn't have updated)... but then we should have inserted? what the heck?
                 $this->_total_update_errors++;
                 EE_Error::add_error(sprintf(__("Could not update %s with the csv data: '%s' for an unknown reason (using WHERE conditions %s)", "event_espresso"), $model->get_this_model_name(), http_build_query($model_object_data), http_build_query($conditions)), __FILE__, __FUNCTION__, __LINE__);
             } else {
                 $this->_total_updates++;
                 EE_Error::add_success(sprintf(__("%s with csv data '%s' was found in the database and didn't need updating because all the data is identical.", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data)));
             }
         }
     } catch (EE_Error $e) {
         $this->_total_update_errors++;
         $basic_message = sprintf(__("Could not update %s with the csv data: %s because %s", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data), $e->getMessage());
         $debug_message = $basic_message . ' Stack trace: ' . $e->getTraceAsString();
         EE_Error::add_error("{$basic_message} | {$debug_message}", __FILE__, __FUNCTION__, __LINE__);
     }
     return $old_db_to_new_db_mapping;
 }