/** * Update Field Column Associations * * Before Matrix 2.2, Matrix would associate Matrix columns to fields via the fields’ col_ids setting. * But now (on EE2 only), those associations are made via the exp_matrix_cols.field_id column. * This function populates that field_id column accordingly, and also duplicates any Matrix columns that belong to more than one field (via MSM field duplication) */ function _update_field_col_associations($field_id = FALSE) { $this->EE->load->dbforge(); $affected_cols = 0; // get each of the Matrix fields if ($this->var_id) { $fields = $this->EE->db->select('low_variables.variable_id, site_id, variable_settings')->from('low_variables')->join('global_variables', 'global_variables.variable_id = low_variables.variable_id')->where('variable_type', 'matrix')->order_by('group_id')->get(); } else { $this->EE->db->select('field_id, site_id, field_settings')->where('field_type', 'matrix'); if ($field_id) { $this->EE->db->where('field_id', $field_id); } $fields = $this->EE->db->order_by('site_id')->get('channel_fields'); } if ($fields->num_rows()) { $field_id_column = $this->var_id ? 'variable_id' : 'field_id'; $col_id_column = $this->var_id ? 'var_id' : 'field_id'; foreach ($fields->result() as $field) { // unserialize the field settings if ($this->var_id) { $settings = low_array_decode($field->variable_settings); } else { $settings = unserialize(base64_decode($field->field_settings)); } $new_col_ids = array(); $old_data_col_names = array(); $new_data_col_names = array(); // make sure the col_ids setting is in-tact if (isset($settings['col_ids'])) { foreach (array_filter($settings['col_ids']) as $col_id) { // get the column data $col = $this->EE->db->get_where('matrix_cols', array('col_id' => $col_id)); if ($col->num_rows()) { $col_data = $col->row_array(); // does this col already belong to this field? if ($col_data[$col_id_column] == $field->{$field_id_column}) { $new_col_ids[] = $col_id; } else { $affected_cols++; // start assempling the new column data $new_col_data = array_merge($col_data); $new_col_data['site_id'] = $field->site_id; if ($this->var_id) { $new_col_data['var_id'] = $field->variable_id; } else { $new_col_data['field_id'] = $field->field_id; } // does it belong to another? if ($col_data[$col_id_column]) { // duplicate it unset($new_col_data['col_id']); $this->EE->db->insert('matrix_cols', $new_col_data); // get the new col_id $new_col_data['col_id'] = $this->EE->db->insert_id(); $new_col_ids[] = $new_col_data['col_id']; // remember the old data column names $celltype = $this->_get_celltype($col_data['col_type']); $old_data_cols = $this->_apply_settings_modify_matrix_column($celltype, $col_data, 'get_data'); $old_data_col_names = array_merge($old_data_col_names, array_keys($old_data_cols)); // add the new data column(s) $new_data_cols = $this->_apply_settings_modify_matrix_column($celltype, $new_col_data, 'add'); $this->EE->dbforge->add_column('matrix_data', $new_data_cols); // remember the new data column names $new_data_col_names = array_merge($new_data_col_names, array_keys($new_data_cols)); } else { // just assign it to this field $this->EE->db->where('col_id', $col_id)->update('matrix_cols', $new_col_data); $new_col_ids[] = $col_id; } } } } } // update the field settings with the new col_ids array $settings['col_ids'] = $new_col_ids; if ($this->var_id) { $this->EE->db->where('variable_id', $field->variable_id)->update('low_variables', array('variable_settings' => low_array_encode($settings))); } else { $this->EE->db->where('field_id', $field->field_id)->update('channel_fields', array('field_settings' => base64_encode(serialize($settings)))); } // migrate the field data if ($old_data_col_names) { $sql = 'UPDATE exp_matrix_data SET '; foreach ($new_data_col_names as $i => $new_col_name) { $old_col_name = $old_data_col_names[$i]; if ($i) { $sql .= ', '; } $sql .= "{$new_col_name} = {$old_col_name}, {$old_col_name} = NULL"; } if ($this->var_id) { $sql .= " WHERE var_id = {$field->variable_id}"; } else { $sql .= " WHERE field_id = {$field->field_id}"; } $this->EE->db->query($sql); } } } return $affected_cols; }
/** * Saves variable data * * @access public * @return void */ public function save_var() { // ------------------------------------- // Where are we coming from? // ------------------------------------- $from = ee()->input->post('from'); // ------------------------------------- // Get variable_id // ------------------------------------- if (!($variable_id = ee()->input->post('variable_id'))) { // No id found, exit! ee()->functions->redirect($this->base_url); exit; } // ------------------------------------- // Get data from POST // ------------------------------------- $ee_vars = $low_vars = $all_vars = $errors = array(); // ------------------------------------- // Check variable name // ------------------------------------- $var_name = trim(ee()->input->post('variable_name')); $suffix = trim(ee()->input->post('variable_suffix')); // prep var name for sprintf if ($suffix) { $var_name = strpos($var_name, '{suffix}') !== FALSE ? str_replace('{suffix}', '%s', $var_name) : $var_name . '_%s'; } // Check if variable name is valid if ($var_name && preg_match('/^[a-z0-9\\-_:]+$/i', str_replace('%s', '', $var_name))) { $ee_vars['variable_name'] = $var_name; } else { $errors[] = 'invalid_variable_name'; } // ------------------------------------- // Check if var already exists // ------------------------------------- if (!$suffix) { // Count possible existing var $existing = ee()->db->where('site_id', $this->site_id)->where('variable_name', $var_name)->where('variable_id !=', $variable_id)->count_all_results('global_variables'); if ($existing) { $errors[] = 'variable_name_already_exists'; } } // ------------------------------------- // Check for errors // ------------------------------------- if (!empty($errors)) { $msg = array(); foreach ($errors as $line) { $msg[] = lang($line); } ee()->session->set_flashdata('errors', $msg); ee()->functions->redirect(sprintf($this->data['edit_var_url'], $variable_id, $from)); exit; } // ------------------------------------- // Check variable data // ------------------------------------- if ($variable_id == 'new') { $ee_vars['variable_data'] = FALSE !== ($var_data = ee()->input->post('variable_data')) ? $var_data : ''; } // ------------------------------------- // Check boolean values // ------------------------------------- foreach (array('early_parsing', 'is_hidden', 'save_as_file') as $var) { $low_vars[$var] = ($value = ee()->input->post($var)) ? 'y' : 'n'; } // ------------------------------------- // Check other regular vars // ------------------------------------- foreach (array('group_id', 'variable_label', 'variable_notes', 'variable_type', 'variable_order') as $var) { $low_vars[$var] = ($value = ee()->input->post($var)) !== FALSE ? $value : ''; } // ------------------------------------- // Get variable settings // ------------------------------------- // All settings $var_settings = ee()->input->post('variable_settings'); // Focus on this type's settings $var_settings = array_key_exists($low_vars['variable_type'], $var_settings) ? $var_settings[$low_vars['variable_type']] : array(); // ------------------------------------- // Call save_settings from API, fallback to default handling // ------------------------------------- if (is_object($this->types[$low_vars['variable_type']])) { // Shortcut to type object $OBJ = $this->types[$low_vars['variable_type']]; if (method_exists($OBJ, 'save_settings')) { // Settings? $settings = empty($var_settings) ? $OBJ->default_settings : $var_settings; // Call API for custom handling of settings $var_settings = $OBJ->save_settings($variable_id, $settings); } else { // Default handling of settings: set missing settings to empty string foreach (array_keys($OBJ->default_settings) as $setting) { if (!isset($var_settings[$setting])) { $var_settings[$setting] = ''; } } } } // Overwrite posted data $low_vars['variable_settings'] = low_array_encode($var_settings); // ------------------------------------- // Check for suffixes // ------------------------------------- // init vars $suffixes = $suffixed = array(); if ($variable_id == 'new' && $suffix) { // Get existing var names to check against $query = ee()->db->select('variable_name')->from('global_variables')->where('site_id', $this->site_id)->get(); $existing = low_flatten_results($query->result_array(), 'variable_name'); foreach (explode(' ', $suffix) as $sfx) { // Skip illegal ones if (!preg_match('/^[a-zA-Z0-9\\-_]+$/', $sfx)) { continue; } // Remove underscore if it's there if (substr($sfx, 0, 1) == '_') { $sfx = substr($sfx, 1); } // Skip suffix if name already exists if (in_array(sprintf($var_name, $sfx), $existing)) { continue; } $suffixes[] = $sfx; } } // ------------------------------------- // Update EE table // ------------------------------------- if (!empty($ee_vars)) { if ($variable_id == 'new') { // ------------------------------------- // Add site id to array, INSERT new var // Get inserted id // ------------------------------------- $ee_vars['site_id'] = $this->site_id; if ($suffixes) { foreach ($suffixes as $sfx) { // Add suffix to name $data = $ee_vars; $data['variable_name'] = sprintf($ee_vars['variable_name'], $sfx); // Insert row ee()->db->insert('global_variables', $data); $vid = ee()->db->insert_id(); // Keep track of inserted rows $suffixed[$vid] = $sfx; // Keep track of all vars $all_vars[$vid] = $data; } } else { ee()->db->insert('global_variables', $ee_vars); $variable_id = ee()->db->insert_id(); $all_vars[$variable_id] = $ee_vars; } } else { ee()->db->update('global_variables', $ee_vars, "variable_id = '{$variable_id}'"); $all_vars[$variable_id] = $ee_vars; } } // ------------------------------------- // Update low_variables table // ------------------------------------- if (!empty($low_vars)) { $query = ee()->db->select('variable_id')->from('low_variables')->get(); $update = low_flatten_results($query->result_array(), 'variable_id'); // ------------------------------------- // Get default value for new sort order // ------------------------------------- if ($low_vars['variable_order'] == 0) { $query = ee()->db->query("SELECT COUNT(*) AS max FROM exp_low_variables WHERE group_id = '{$low_vars['group_id']}'"); if ($query->num_rows()) { $row = $query->row(); $low_vars['variable_order'] = (int) $row->max + 1; } else { $low_vars['variable_order'] = 1; } } if ($suffixed) { $i = (int) $low_vars['variable_order']; foreach ($suffixed as $var_id => $sfx) { $row = $low_vars; $row['variable_label'] = strpos($low_vars['variable_label'], '{suffix}') !== FALSE ? str_replace('{suffix}', $sfx, $low_vars['variable_label']) : $low_vars['variable_label'] . " ({$sfx})"; $row['variable_order'] = $i++; $rows[$var_id] = $row; } } else { $rows[$variable_id] = $low_vars; } // ------------------------------------- // INSERT/UPDATE rows // ------------------------------------- foreach ($rows as $var_id => $data) { if (in_array($var_id, $update)) { ee()->db->update('exp_low_variables', $data, "variable_id = '{$var_id}'"); } else { $data['variable_id'] = $var_id; ee()->db->insert('exp_low_variables', $data); } $all_vars[$var_id] += $data; } } else { // ------------------------------------- // Delete reference if no low_vars were found // ------------------------------------- ee()->db->query("DELETE FROM `exp_low_variables` WHERE `variable_id` = '{$variable_id}'"); } // ------------------------------------- // Trigger post_save_settings // ------------------------------------- foreach ($all_vars as $vid => $vdata) { // Skip if it's not an object if (!is_object($this->types[$vdata['variable_type']])) { continue; } // Shortcut to type object $OBJ = $this->types[$vdata['variable_type']]; // Skip if necessary method doesn't exist if (!method_exists($OBJ, 'post_save_settings')) { continue; } // Decode settings if (!is_array($vdata['variable_settings'])) { $vdata['variable_settings'] = low_array_decode($vdata['variable_settings']); } // Call the API $OBJ->post_save_settings($vid, $vdata['variable_settings']); } // ------------------------------------- // Return url // ------------------------------------- $return_url = $this->base_url; if ($from == 'manage') { $return_url .= AMP . 'method=manage'; } elseif (is_numeric($from)) { $return_url = sprintf($this->data['show_group_url'], $from); } else { $return_url = sprintf($this->data['show_group_url'], $low_vars['group_id']); } // ------------------------------------- // Return with message // ------------------------------------- ee()->session->set_flashdata('msg', 'low_variables_saved'); ee()->functions->redirect($return_url); }
/** * Get variables for given site from cache or DB * * @access private * @param mixed [bool|string|array] * @param int * @return array */ private function _get_variables($var = FALSE, $site_id = FALSE) { // ------------------------------------- // If no site id is given, use current // ------------------------------------- if ($site_id == FALSE) { $site_id = $this->site_id; } // ------------------------------------- // Get cached vars // ------------------------------------- $var_cache = low_get_cache($this->package, 'vars'); if (isset($var_cache[$site_id])) { $this->EE->TMPL->log_item('Low Variables: Getting variables from Session Cache'); $this->vars = $var_cache[$site_id]; } else { $this->EE->TMPL->log_item('Low Variables: Getting variables from Database'); // ------------------------------------- // Query DB // ------------------------------------- $select = array('ee.variable_id', 'ee.variable_name', 'ee.variable_data', 'ee.site_id', 'low.variable_label', 'low.variable_type', 'low.variable_settings'); $this->EE->db->select($select)->from(array('global_variables ee', 'low_variables low'))->where('ee.variable_id = low.variable_id')->where('ee.site_id', $site_id); // Limit by given vars // if ($var != FALSE) // { // $where = is_array($var) ? 'where_in' : 'where'; // $this->EE->db->$where('ee.variable_name', $var); // } $rows = $this->EE->db->get()->result_array(); // ------------------------------------- // Get results // ------------------------------------- foreach ($rows as $row) { // Prep settings $row['variable_settings'] = low_array_decode($row['variable_settings']); // Add prep'd row to data array $this->vars[$row['variable_name']] = $row; } // ------------------------------------- // Register to cache // ------------------------------------- $var_cache[$site_id] = $this->vars; low_set_cache($this->package, 'vars', $var_cache); } unset($var_cache); return $this->vars; }
/** * Get encoded data from variable contents */ private function _get_data($data) { if (preg_match('/<!\\-\\-(.*?)\\-\\->/', $data, $match) && ($rows = low_array_decode($match[1]))) { return $rows; } else { return array(); } }
/** * Update to version 2.1.0 */ private function _v210() { // Load JS lib ee()->load->library('javascript'); // Shortcut to table $table = ee()->low_reorder_set_model->table(); // Add set_name Change DB stuff, only not coming from v1 if ($this->_from_v1 == FALSE) { ee()->db->query("ALTER TABLE `{$table}` ADD `set_name` VARCHAR(50) NOT NULL AFTER `site_id`"); // Add 'none' as cat_option ee()->db->query("ALTER TABLE `{$table}` CHANGE `cat_option` `cat_option` ENUM('all', 'some', 'one', 'none') DEFAULT 'all' NOT NULL"); // Add indexes to table ee()->db->query("ALTER TABLE {$table} ADD INDEX (`site_id`)"); ee()->db->query("ALTER TABLE {$table} ADD INDEX (`set_name`)"); } // Change base64/serialize to json_encode and populate set_name foreach (ee()->low_reorder_set_model->get_all() as $row) { $set_id = $row['set_id']; unset($row['set_id']); $row['parameters'] = json_encode(low_array_decode($row['parameters'])); $row['permissions'] = json_encode(low_array_decode($row['permissions'])); $row['set_name'] = 'set_' . $set_id; ee()->low_reorder_set_model->update($set_id, $row); } }