/**
  * Prep variable data for saving
  */
 public function save_input($var_id, $var_data, $var_settings)
 {
     // Initiate rows data
     $rows = array();
     $data = '';
     if (!empty($var_data) && is_array($var_data)) {
         // Loop through posted data and strip out empty rows
         foreach ($var_data as $row) {
             $row = array_map('trim', $row);
             if (count(array_filter($row))) {
                 $rows[] = $row;
             }
         }
         // Overwrite data if there are rows present
         if ($rows) {
             $cols = $this->get_setting('columns', $var_settings);
             $data = $this->EE->load->view('mod_vt_table', array('var_id' => $var_id, 'columns' => array_map('trim', explode('|', $cols)), 'rows' => $rows, 'encoded_rows' => low_array_encode($rows)), TRUE);
         }
     }
     return $data;
 }
Example #2
0
 /**
  * Post-Save Variable Settings
  */
 function post_save_var_settings()
 {
     if (!$this->var_id) {
         return;
     }
     $settings = $this->_save_settings();
     $data = array('variable_settings' => low_array_encode($settings));
     $this->EE->db->where('variable_id', $this->var_id)->update('low_variables', $data);
 }
 /**
  * Do update to 2.0.0
  */
 private function _v200()
 {
     // Add extra table attrs
     $this->EE->db->query("ALTER TABLE `exp_low_variables` ADD `save_as_file` char(1) NOT NULL DEFAULT 'n'");
     $this->EE->db->query("ALTER TABLE `exp_low_variables` ADD `edit_date` int(10) unsigned default 0 NOT NULL");
     // Change settings to smaller array
     $query = $this->EE->db->select('variable_id, variable_type, variable_settings')->from('low_variables')->get();
     foreach ($query->result_array() as $row) {
         $settings = unserialize($row['variable_settings']);
         $settings = low_array_encode($settings[$row['variable_type']]);
         $this->EE->db->where('variable_id', $row['variable_id']);
         $this->EE->db->update('low_variables', array('variable_settings' => $settings));
     }
 }
Example #4
0
 /**
  * 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);
 }
Example #5
0
 /**
  * Update routines for version 2.0b2
  *
  * @access      private
  * @return      void
  */
 private function _v20b2()
 {
     // Check if new_entries attr already exits
     $table = ee()->low_reorder_set_model->table();
     $field = 'new_entries';
     $query = ee()->db->query("SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'");
     // Already exists
     if ($query->num_rows()) {
         return;
     }
     // Add column
     ee()->db->query("ALTER TABLE {$table} ADD COLUMN `{$field}` enum('append','prepend') NOT NULL DEFAULT 'append' AFTER `set_notes`");
     // Migrate to column
     $sets = ee()->low_reorder_set_model->get_all();
     foreach ($sets as $set) {
         $params = low_array_decode($set['parameters']);
         $pend = @$params['sort'] == 'desc' ? 'prepend' : 'append';
         unset($params['sort']);
         $data = array($field => $pend, 'parameters' => low_array_encode($params));
         ee()->low_reorder_set_model->update($set['set_id'], $data);
     }
     // --------------------------------------
     // Update extension
     // --------------------------------------
     $this->_add_hook('channel_entries_query_result');
 }