Example #1
0
 /**
  * Prep multi field
  *
  * Prepare multi_select and option_group fields
  *
  * @access	private
  * @param	mixed
  * @param	mixed
  * @return	void
  */
 function _prep_multi_field(&$data, $row)
 {
     if (isset($data['field_id_' . $row['field_id']])) {
         if (is_array($data['field_id_' . $row['field_id']])) {
             $data['field_id_' . $row['field_id']] = encode_multi_field($data['field_id_' . $row['field_id']]);
             return;
         }
     }
     //unset($data['field_id_'.$row['field_id']]);
 }
 /**
  * Pre-process the data and return a string
  *
  * @access public
  * @param array data The selected entry channels
  * @return string Concatenated string f entry channels
  */
 public function save($data)
 {
     $this->EE->load->helper('custom_field');
     return encode_multi_field($data);
 }
 /**
  * Processes a POSTed Grid field for validation for saving
  *
  * The main point of the validation method is, of course, to validate the
  * data in each cell and collect any errors. But it also reconstructs
  * the post data in a way that display_field can take it if there is a
  * validation error. The validation routine also keeps track of any other
  * input fields and carries them through to the save method so that those
  * values are available to fieldtypes while they run their save methods.
  *
  * The save method takes the validated data and gives it to the fieldtype's
  * save method for further processing, in which the fieldtype can specify
  * other columns that need to be filled.
  *
  * @param	string	Method to process, 'save' or 'validate'
  * @param	array	Grid publish form data
  * @param	int		Field ID of field being saved
  * @param	int		Entry ID of entry being saved
  * @return	boolean
  */
 protected function _process_field_data($method, $data)
 {
     ee()->load->helper('custom_field_helper');
     // Get column data for the current field
     $columns = ee()->grid_model->get_columns_for_field($this->field_id, $this->content_type);
     // We'll store our final values and errors here
     $final_values = array();
     $errors = FALSE;
     if (!is_array($data)) {
         $data = array();
     } elseif (isset($data['rows'])) {
         $data = $data['rows'];
     }
     // Make a copy of the files array so we can spoof it per field below
     $grid_field_name = $this->field_name;
     $files_backup = $_FILES;
     foreach ($data as $row_id => $row) {
         foreach ($columns as $column) {
             $col_id = 'col_id_' . $column['col_id'];
             // Handle empty data for default input name
             if (!isset($row[$col_id])) {
                 $row[$col_id] = NULL;
             }
             // Assign any other input fields to POST data for normal access
             foreach ($row as $key => $value) {
                 $_POST[$key] = $value;
                 // If we're validating, keep these extra values around so
                 // fieldtypes can access them on save
                 if ($method == 'validate' && !isset($final_values[$row_id][$key])) {
                     $final_values[$row_id][$key] = $value;
                 }
             }
             $fieldtype = ee()->grid_parser->instantiate_fieldtype($column, $row_id, $this->field_id, $this->entry_id);
             // Pass Grid row ID to fieldtype if it's an existing row
             if (strpos($row_id, 'row_id_') !== FALSE) {
                 $fieldtype->settings['grid_row_id'] = str_replace('row_id_', '', $row_id);
             }
             // Inside grid our files arrays end up being deeply nested. Since
             // the fields access these arrays directly, we set the FILES array
             // to what is expected by the field for each iteration.
             $_FILES = array();
             if (isset($files_backup[$grid_field_name])) {
                 $newfiles = array();
                 foreach ($files_backup[$grid_field_name] as $files_key => $value) {
                     if (isset($value['rows'][$row_id][$col_id])) {
                         $newfiles[$files_key] = $value['rows'][$row_id][$col_id];
                     }
                 }
                 $_FILES[$col_id] = $newfiles;
             }
             // Call the fieldtype's validate/save method and capture the output
             $result = ee()->grid_parser->call($method, $row[$col_id]);
             // For validation, gather errors and validated data
             if ($method == 'validate') {
                 $error = $result;
                 // First, assign the row data as the final value
                 $value = $row[$col_id];
                 // Here we extract possible $value and $error variables to
                 // overwrite the assumptions we've made, this is a chance for
                 // fieldtypes to correct input data or show an error message
                 if (is_array($result)) {
                     extract($result, EXTR_OVERWRITE);
                 }
                 // Assign the final value to the array
                 $final_values[$row_id][$col_id] = $value;
                 // If column is required and the value from validation is empty,
                 // throw an error, except if the value is 0 because that can be
                 // a legitimate data entry
                 if ($column['col_required'] == 'y' && empty($value) && $value !== 0 && $value !== '0') {
                     $error = lang('grid_field_required');
                 }
                 // If there's an error, assign the old row data back so the
                 // user can see the error, and set the error message
                 if (is_string($error) && !empty($error)) {
                     $final_values[$row_id][$col_id] = $row[$col_id];
                     $final_values[$row_id][$col_id . '_error'] = $error;
                     $errors = lang('grid_validation_error');
                 }
             } elseif ($method == 'save') {
                 // Flatten array
                 if (is_array($result)) {
                     $result = encode_multi_field($result);
                 }
                 $final_values[$row_id][$col_id] = $result;
             }
             // Remove previous input fields from POST
             foreach ($row as $key => $value) {
                 unset($_POST[$key]);
             }
         }
     }
     // reset $_FILES in case it's used in other code
     $_FILES = $files_backup;
     return array('value' => $final_values, 'error' => $errors);
 }
Example #4
0
 public function save($data)
 {
     if (is_array($data)) {
         ee()->load->helper('custom_field');
         $data = encode_multi_field($data);
     }
     return $data;
 }