Esempio n. 1
0
 protected function _get_models_metadata_entity()
 {
     $response = array();
     foreach ($this->get_model_version_info()->models_for_requested_version() as $model_name => $model_classname) {
         $model = $this->get_model_version_info()->load_model($model_name);
         $fields_json = array();
         foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field_obj) {
             if ($this->get_model_version_info()->field_is_ignored($field_obj)) {
                 continue;
             }
             if ($field_obj instanceof \EE_Boolean_Field) {
                 $datatype = 'Boolean';
             } elseif ($field_obj->get_wpdb_data_type() == '%d') {
                 $datatype = 'Number';
             } elseif ($field_name instanceof \EE_Serialized_Text_Field) {
                 $datatype = 'Object';
             } else {
                 $datatype = 'String';
             }
             $default_value = Model_Data_Translator::prepare_field_value_for_json($field_obj, $field_obj->get_default_value(), $this->get_model_version_info()->requested_version());
             $field_json = array('name' => $field_name, 'nicename' => $field_obj->get_nicename(), 'has_rendered_format' => $this->get_model_version_info()->field_has_rendered_format($field_obj), 'has_pretty_format' => $this->get_model_version_info()->field_has_pretty_format($field_obj), 'type' => str_replace('EE_', '', get_class($field_obj)), 'datatype' => $datatype, 'nullable' => $field_obj->is_nullable(), 'default' => $default_value, 'table_alias' => $field_obj->get_table_alias(), 'table_column' => $field_obj->get_table_column());
             $fields_json[$field_json['name']] = $field_json;
         }
         $fields_json = array_merge($fields_json, $this->get_model_version_info()->extra_resource_properties_for_model($model));
         $response[$model_name]['fields'] = apply_filters('FHEE__Meta__handle_request_models_meta__fields', $fields_json, $model);
         $relations_json = array();
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
             $relation_json = array('name' => $relation_name, 'type' => str_replace('EE_', '', get_class($relation_obj)), 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false);
             $relations_json[$relation_name] = $relation_json;
         }
         $response[$model_name]['relations'] = apply_filters('FHEE__Meta__handle_request_models_meta__relations', $relations_json, $model);
     }
     return $response;
 }
Esempio n. 2
0
 /**
  * Gets the calculated fields for the response
  *
  * @param \EEM_Base        $model
  * @param array            $wpdb_row
  * @param \WP_REST_Request $rest_request
  * @return array the _calculations item in the entity
  */
 protected function _get_entity_calculations($model, $wpdb_row, $rest_request)
 {
     $calculated_fields = $this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '');
     //note: setting calculate=* doesn't do anything
     $calculated_fields_to_return = new \stdClass();
     foreach ($calculated_fields as $field_to_calculate) {
         try {
             $calculated_fields_to_return->{$field_to_calculate} = Model_Data_Translator::prepare_field_value_for_json(null, $this->_fields_calculator->retrieve_calculated_field_value($model, $field_to_calculate, $wpdb_row, $rest_request, $this), $this->get_model_version_info()->requested_version());
         } catch (Rest_Exception $e) {
             //if we don't have permission to read it, just leave it out. but let devs know about the problem
             $this->_set_response_header('Notices-Field-Calculation-Errors[' . $e->get_string_code() . '][' . $model->get_this_model_name() . '][' . $field_to_calculate . ']', $e->getMessage(), true);
         }
     }
     return $calculated_fields_to_return;
 }