Esempio n. 1
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;
 }
 /**
  *
  * @param type $id_in_csv
  * @param type $model_object_data
  * @param EEM_Base $model
  * @param type $old_db_to_new_db_mapping
  * @return array updated $old_db_to_new_db_mapping
  */
 protected function _insert_from_data_array($id_in_csv, $model_object_data, $model, $old_db_to_new_db_mapping)
 {
     //remove the primary key, if there is one (we don't want it for inserts OR updates)
     //we'll put it back in if we need it
     if ($model->has_primary_key_field() && $model->get_primary_key_field()->is_auto_increment()) {
         $effective_id = $model_object_data[$model->primary_key_name()];
         unset($model_object_data[$model->primary_key_name()]);
     } else {
         $effective_id = $model->get_index_primary_key_string($model_object_data);
     }
     //the model takes care of validating the CSV's input
     try {
         $new_id = $model->insert($model_object_data);
         if ($new_id) {
             $old_db_to_new_db_mapping[$model->get_this_model_name()][$id_in_csv] = $new_id;
             $this->_total_inserts++;
             EE_Error::add_success(sprintf(__("Successfully added new %s (with id %s) with csv data %s", "event_espresso"), $model->get_this_model_name(), $new_id, implode(",", $model_object_data)));
         } else {
             $this->_total_insert_errors++;
             //put the ID used back in there for the error message
             if ($model->has_primary_key_field()) {
                 $model_object_data[$model->primary_key_name()] = $effective_id;
             }
             EE_Error::add_error(sprintf(__("Could not insert new %s with the csv data: %s", "event_espresso"), $model->get_this_model_name(), http_build_query($model_object_data)), __FILE__, __FUNCTION__, __LINE__);
         }
     } catch (EE_Error $e) {
         $this->_total_insert_errors++;
         if ($model->has_primary_key_field()) {
             $model_object_data[$model->primary_key_name()] = $effective_id;
         }
         EE_Error::add_error(sprintf(__("Could not insert new %s with the csv data: %s because %s", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data), $e->getMessage()), __FILE__, __FUNCTION__, __LINE__);
     }
     return $old_db_to_new_db_mapping;
 }