/**
  * Save the New Order (dundundun)
  *
  * @access      public
  *  @return      void
  */
 public function save_order()
 {
     // --------------------------------------
     // Get Set id
     // --------------------------------------
     if (!($set_id = ee()->input->post('set_id'))) {
         return $this->_show_error('invalid_request');
     }
     // --------------------------------------
     // Get Cat id
     // --------------------------------------
     $cat_id = ee()->input->post('cat_id');
     // --------------------------------------
     // Get entries
     // --------------------------------------
     $entries = (array) ee()->input->post('entries');
     // --------------------------------------
     // Reverse entries if sort = desc
     // --------------------------------------
     if (ee()->input->post('sort') == 'desc') {
         $entries = array_reverse($entries);
     }
     // --------------------------------------
     // REPLACE INTO table statement
     // --------------------------------------
     ee()->low_reorder_order_model->replace(array('set_id' => $set_id, 'cat_id' => $cat_id, 'sort_order' => low_linearize($entries)));
     // --------------------------------------
     // That's the entries updated
     // Now, do we need to clear the cache?
     // --------------------------------------
     $clear_cache = ee()->input->post('clear_caching') == 'y';
     if ($clear_cache) {
         ee()->functions->clear_caching('all', '', TRUE);
     }
     // -------------------------------------
     // 'low_reorder_post_sort' hook.
     //  - Do something after new order is saved
     // -------------------------------------
     if (ee()->extensions->active_hook('low_reorder_post_sort') === TRUE) {
         ee()->extensions->call('low_reorder_post_sort', $entries, $clear_cache);
     }
     // --------------------------------------
     // Get ready to redirect back
     // --------------------------------------
     $url = $this->base_url . AMP . 'method=reorder&set_id=' . $set_id;
     // Redirect to selected category, if any
     if ($cat_id) {
         $url .= AMP . 'category=' . $cat_id;
     }
     // --------------------------------------
     // Set flashdata for feedback
     // --------------------------------------
     ee()->session->set_flashdata('msg', lang('new_order_saved'));
     // And go back
     ee()->functions->redirect($url);
     exit;
 }
 /**
  * Update routines for version 2.0b1
  *
  * @access      private
  * @return      void
  */
 private function _v20b1()
 {
     // --------------------------------------
     // Install new tables
     // --------------------------------------
     ee()->low_reorder_set_model->install();
     ee()->low_reorder_order_model->install();
     // --------------------------------------
     // Get all current records from settings
     // --------------------------------------
     $query = ee()->db->get('low_reorder_settings');
     $rows = $query->result_array();
     // Return if no settings exist
     if (!empty($rows)) {
         // Upgrading from EE1
         if (!isset($rows[0]['channel_id'])) {
             foreach ($rows as &$r) {
                 $r['channel_id'] = $r['weblog_id'];
             }
         }
         // --------------------------------------
         // Get Field, Channel and Status details
         // --------------------------------------
         // Fields
         $query = ee()->db->select('field_id, site_id, field_name, field_label, field_instructions')->from('channel_fields')->where_in('field_id', low_flatten_results($rows, 'field_id'))->get();
         $fields = low_associate_results($query->result_array(), 'field_id');
         // Channels
         $query = ee()->db->select('channel_id, channel_name, channel_title, cat_group')->from('channels')->where_in('channel_id', low_flatten_results($rows, 'channel_id'))->get();
         $channels = low_associate_results($query->result_array(), 'channel_id');
         // Statuses
         $query = ee()->db->select('status_id, status')->from('statuses')->get();
         $statuses = low_flatten_results($query->result_array(), 'status', 'status_id');
         // --------------------------------------
         // Loop through rows and populate new table
         // --------------------------------------
         foreach ($rows as $row) {
             // Skip non-existent channels or fields
             if (!(isset($channels[$row['channel_id']]) && isset($fields[$row['field_id']]))) {
                 continue;
             }
             // Shortcut to related channel and field
             $channel = $channels[$row['channel_id']];
             $field = $fields[$row['field_id']];
             // Decode the settings
             $settings = decode_reorder_settings($row['settings']);
             // Initiate parameter array
             $params = array();
             // --------------------------------------
             // Set Channel parameter
             // --------------------------------------
             $params['channel'] = $channels[$row['channel_id']]['channel_name'];
             // --------------------------------------
             // Set Category parameter
             // --------------------------------------
             if (!empty($settings['categories'])) {
                 $params['category'] = implode('|', array_filter($settings['categories']));
             }
             // --------------------------------------
             // Set Status parameter
             // --------------------------------------
             if (!empty($settings['statuses'])) {
                 $tmp = array();
                 foreach ($settings['statuses'] as $status_id) {
                     $tmp[] = $statuses[$status_id];
                 }
                 $params['status'] = implode('|', array_unique($tmp));
                 unset($tmp);
             }
             // --------------------------------------
             // Set Show Expired parameter
             // --------------------------------------
             if (!empty($settings['show_expired']) && ($settings['show_expired'] == 'y' or $settings['show_expired'] === TRUE)) {
                 $params['show_expired'] = 'yes';
             }
             // --------------------------------------
             // Set Show Future Entries parameter
             // --------------------------------------
             if (!empty($settings['show_future']) && ($settings['show_future'] == 'y' or $settings['show_future'] === TRUE)) {
                 $params['show_future_entries'] = 'yes';
             }
             // --------------------------------------
             // Get permissions from settings
             // --------------------------------------
             $permissions = !empty($settings['permissions']) ? low_array_encode($settings['permissions']) : '';
             // --------------------------------------
             // Set Category Option value
             // --------------------------------------
             $cat_option = $settings['category_options'];
             // --------------------------------------
             // Set Category Groups value, if option is 'one'
             // --------------------------------------
             if ($cat_option == 'one' && $channel['cat_group']) {
                 $cat_groups = low_linearize(explode('|', $channel['cat_group']));
             } else {
                 $cat_groups = '';
             }
             // --------------------------------------
             // Set clear_cache value
             // --------------------------------------
             $clear_cache = !empty($settings['clear_cache']) && $settings['clear_cache'] == 'n' ? 'n' : 'y';
             // --------------------------------------
             // Sort order setting
             // --------------------------------------
             $reverse = @$settings['sort_order'] == 'desc';
             // --------------------------------------
             // Insert new row
             // --------------------------------------
             $set_id = ee()->low_reorder_set_model->insert(array('site_id' => $field['site_id'], 'set_label' => $channel['channel_title'] . ', ' . $field['field_label'], 'set_notes' => $field['field_instructions'], 'new_entries' => $reverse ? 'prepend' : 'append', 'clear_cache' => $clear_cache, 'channels' => low_linearize(array($row['channel_id'])), 'cat_option' => $cat_option, 'cat_groups' => $cat_groups, 'parameters' => low_array_encode($params), 'permissions' => $permissions));
             // --------------------------------------
             // Get current values
             // --------------------------------------
             ee()->db->select("GROUP_CONCAT(DISTINCT d.entry_id ORDER BY d.field_id_{$field['field_id']} ASC SEPARATOR '|') AS entries", FALSE)->from('channel_data d')->where('d.channel_id', $channel['channel_id'])->where("d.field_id_{$field['field_id']} !=", '');
             if ($cat_option != 'one') {
                 ee()->db->select("'0' AS cat_id", FALSE);
             } else {
                 ee()->db->select('cp.cat_id')->from('category_posts cp')->where('d.entry_id = cp.entry_id')->group_by('cat_id');
             }
             $query = ee()->db->get();
             foreach ($query->result() as $row) {
                 $entries = low_delinearize($row->entries);
                 if ($reverse) {
                     $entries = array_reverse($entries);
                 }
                 ee()->low_reorder_order_model->insert(array('set_id' => $set_id, 'cat_id' => $row->cat_id, 'sort_order' => low_linearize($entries)));
             }
         }
     }
     // end if $rows
     // --------------------------------------
     // Change low_reorder fieldtype to text
     // --------------------------------------
     ee()->db->where('field_type', $this->package);
     ee()->db->update('channel_fields', array('field_type' => 'text', 'field_settings' => low_array_encode(array('field_content_type' => 'text'))));
     // --------------------------------------
     // Remove low_reorder fieldtype
     // --------------------------------------
     ee()->db->where('name', $this->package);
     ee()->db->delete('fieldtypes');
     // --------------------------------------
     // Drop old table
     // --------------------------------------
     ee()->db->query("DROP TABLE IF EXISTS `exp_low_reorder_settings`");
     // --------------------------------------
     // Enable extension
     // --------------------------------------
     foreach ($this->hooks as $hook) {
         $this->_add_hook($hook);
     }
 }