/** * Takes a entity that's ready to be returned and removes fields which the user shouldn't be able to access. * @param array $entity * @param \EEM_Base $model * @param string $request_type one of the return values from EEM_Base::valid_cap_contexts() * @param Model_Version_Info $model_version_info * @return array ready for converting into json */ public static function filter_out_inaccessible_entity_fields($entity, $model, $request_type, $model_version_info) { //we only care to do this for frontend reads and when the user can't edit the item if ($request_type !== \EEM_Base::caps_read || $model->exists(array(array($model->primary_key_name() => $entity[$model->primary_key_name()]), 'default_where_conditions' => 'none', 'caps' => \EEM_Base::caps_edit))) { return $entity; } foreach ($model->field_settings() as $field_name => $field_obj) { if ($model_version_info->field_has_rendered_format($field_obj) && isset($entity[$field_name]['raw'])) { unset($entity[$field_name]['raw']); } } //theoretically we may want to filter out specific fields for specific models return apply_filters('FHEE__Capabilities__filter_out_inaccessible_entity_fields', $entity, $model, $request_type); }
/** * Gets all the input values that correspond to model fields. Keys are the input/field names, * values are their normalized values * @return array */ public function inputs_values_corresponding_to_model_fields() { return array_intersect_key($this->input_values(), $this->_model->field_settings()); }
/** * 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; }
/** * Extracts the question field's values from the POST request to update or insert them * * @param \EEM_Base $model * @return array where each key is the name of a model's field/db column, and each value is its value. */ protected function _set_column_values_for(EEM_Base $model) { do_action('AHEE_log', __FILE__, __FUNCTION__, ''); $set_column_values = array(); //some initial checks for proper values. //if QST_admin_only, then no matter what QST_required is we disable. if (!empty($this->_req_data['QST_admin_only'])) { $this->_req_data['QST_required'] = 0; } foreach ($model->field_settings() as $fieldName => $settings) { // basically if QSG_identifier is empty or not set if ($fieldName == 'QSG_identifier' && (isset($this->_req_data['QSG_identifier']) && empty($this->_req_data['QSG_identifier']))) { $QSG_name = isset($this->_req_data['QSG_name']) ? $this->_req_data['QSG_name'] : ''; $set_column_values[$fieldName] = sanitize_title($QSG_name) . '-' . uniqid(); // dd($set_column_values); } else { if ($fieldName == 'QST_admin_label' && (isset($this->_req_data['QST_admin_label']) && empty($this->_req_data['QST_admin_label']))) { $QST_text = isset($this->_req_data['QST_display_text']) ? $this->_req_data['QST_display_text'] : ''; $set_column_values[$fieldName] = sanitize_title(wp_trim_words($QST_text, 10)); } else { if ($fieldName == 'QST_admin_only' && !isset($this->_req_data['QST_admin_only'])) { $set_column_values[$fieldName] = 0; } else { if ($fieldName == 'QST_max') { $qst_system = EEM_Question::instance()->get_var(array(array('QST_ID' => isset($this->_req_data['QST_ID']) ? $this->_req_data['QST_ID'] : 0)), 'QST_system'); $max_max = EEM_Question::instance()->absolute_max_for_system_question($qst_system); if (empty($this->_req_data['QST_max']) || $this->_req_data['QST_max'] > $max_max) { $set_column_values[$fieldName] = $max_max; } } } } } //only add a property to the array if it's not null (otherwise the model should just use the default value) if (!isset($set_column_values[$fieldName]) && isset($this->_req_data[$fieldName])) { $set_column_values[$fieldName] = $this->_req_data[$fieldName]; } } return $set_column_values; //validation fo this data to be performed by the model before insertion. }
/** * Extracts the question field's values from the POST request to update or insert them * @return array where each key is the name of a model's field/db column, and each value is its value. */ protected function _set_column_values_for(EEM_Base $model) { do_action('AHEE_log', __FILE__, __FUNCTION__, ''); $set_column_values = array(); //some initial checks for proper values. //if QST_admin_only, then no matter what QST_required is we disable. if (!empty($this->_req_data['QST_admin_only'])) { $this->_req_data['QST_required'] = 0; } foreach ($model->field_settings() as $fieldName => $settings) { // basically if QSG_identifier is empty or not set if ($fieldName == 'QSG_identifier' && (isset($this->_req_data['QSG_identifier']) && empty($this->_req_data['QSG_identifier']))) { $QSG_name = isset($this->_req_data['QSG_name']) ? $this->_req_data['QSG_name'] : ''; $set_column_values[$fieldName] = sanitize_title($QSG_name) . '-' . uniqid(); // dd($set_column_values); } else { if ($fieldName == 'QST_admin_label' && (isset($this->_req_data['QST_admin_label']) && empty($this->_req_data['QST_admin_label']))) { $QST_text = isset($this->_req_data['QST_display_text']) ? $this->_req_data['QST_display_text'] : ''; $set_column_values[$fieldName] = sanitize_title(wp_trim_words($QST_text, 10)); } else { if ($fieldName == 'QST_admin_only' && !isset($this->_req_data['QST_admin_only'])) { $set_column_values[$fieldName] = 0; } else { if (isset($this->_req_data[$fieldName])) { $set_column_values[$fieldName] = $this->_req_data[$fieldName]; } } } } } return $set_column_values; //validation fo this data to be performed by the model before insertion. }
/** * Using the $old_db_to_new_db_mapping array, replaces all the temporary IDs * with their mapped real IDs. Eg, if importing from site A to B, the mapping * file may indicate that the ID "my_event_id" maps to an actual event ID of 123. * So this function searches for any event temp Ids called "my_event_id" and * replaces them with 123. * Also, if there is no temp ID for the INT foreign keys from another database, * replaces them with 0 or the field's default. * @param type $model_object_data * @param EEM_Base $model * @param type $old_db_to_new_db_mapping * @param boolean $export_from_site_a_to_b * @return array updated model object data with temp IDs removed */ protected function _replace_temp_ids_with_mappings($model_object_data, $model, $old_db_to_new_db_mapping, $export_from_site_a_to_b) { //if this model object's primary key is in the mapping, replace it if ($model->has_primary_key_field() && $model->get_primary_key_field()->is_auto_increment() && isset($old_db_to_new_db_mapping[$model->get_this_model_name()]) && isset($old_db_to_new_db_mapping[$model->get_this_model_name()][$model_object_data[$model->primary_key_name()]])) { $model_object_data[$model->primary_key_name()] = $old_db_to_new_db_mapping[$model->get_this_model_name()][$model_object_data[$model->primary_key_name()]]; } try { $model_name_field = $model->get_field_containing_related_model_name(); $models_pointed_to_by_model_name_field = $model_name_field->get_model_names_pointed_to(); } catch (EE_Error $e) { $model_name_field = NULL; $models_pointed_to_by_model_name_field = array(); } foreach ($model->field_settings(true) as $field_obj) { if ($field_obj instanceof EE_Foreign_Key_Int_Field) { $models_pointed_to = $field_obj->get_model_names_pointed_to(); $found_a_mapping = false; foreach ($models_pointed_to as $model_pointed_to_by_fk) { if ($model_name_field) { $value_of_model_name_field = $model_object_data[$model_name_field->get_name()]; if ($value_of_model_name_field == $model_pointed_to_by_fk) { $model_object_data[$field_obj->get_name()] = $this->_find_mapping_in($model_object_data[$field_obj->get_name()], $model_pointed_to_by_fk, $old_db_to_new_db_mapping, $export_from_site_a_to_b); $found_a_mapping = true; break; } } else { $model_object_data[$field_obj->get_name()] = $this->_find_mapping_in($model_object_data[$field_obj->get_name()], $model_pointed_to_by_fk, $old_db_to_new_db_mapping, $export_from_site_a_to_b); $found_a_mapping = true; } //once we've found a mapping for this field no need to continue if ($found_a_mapping) { break; } } } else { //it's a string foreign key (which we leave alone, because those are things //like country names, which we'd really rather not make 2 USAs etc (we'd actually //prefer to just update one) //or it's just a regular value that ought to be replaced } } // if ($model instanceof EEM_Term_Taxonomy) { $model_object_data = $this->_handle_split_term_ids($model_object_data); } return $model_object_data; }