/**
  * Get all collections and cache them
  *
  * @access      public
  * @param       int      Channel ID
  * @return      array
  */
 public function get_all()
 {
     static $all;
     // Get all from parent class
     if (is_null($all)) {
         ee()->db->order_by('collection_label', 'asc');
         $all = parent::get_all();
         foreach ($all as &$row) {
             $row['settings'] = low_search_decode($row['settings'], FALSE);
         }
         $all = low_associate_results($all, 'collection_id');
     }
     return $all;
 }
 /**
  * Get Upload Preferences (Cross-compatible between ExpressionEngine 2.0 and 2.4) - By Brandon Kelly
  *
  * @param  int $group_id Member group ID specified when returning allowed upload directories only for that member group
  * @param  int $id       Specific ID of upload destination to return
  * @return array         Result array of DB object, possibly merged with custom file upload settings (if on EE 2.4+)
  */
 private function _get_upload_preferences($group_id = NULL, $id = NULL)
 {
     if (version_compare(APP_VER, '2.4', '>=')) {
         $this->EE->load->model('file_upload_preferences_model');
         return $this->EE->file_upload_preferences_model->get_file_upload_preferences($group_id, $id);
     }
     if (version_compare(APP_VER, '2.1.5', '>=')) {
         $this->EE->load->model('file_upload_preferences_model');
         $result = $this->EE->file_upload_preferences_model->get_upload_preferences($group_id, $id);
     } else {
         $this->EE->load->model('tools_model');
         $result = $this->EE->tools_model->get_upload_preferences($group_id, $id);
     }
     // If an $id was passed, just return that directory's preferences
     if (!empty($id)) {
         return $result->row_array();
     }
     // Use upload destination ID as key for row for easy traversing
     return low_associate_results($result->result_array(), 'id');
 }
Example #3
0
 /**
  * Edit reorder set
  *
  * @access      public
  * @return      string
  */
 public function edit()
 {
     ee()->load->helper('form');
     // --------------------------------------
     // Get set by id or empty row
     // --------------------------------------
     $set_id = ee()->input->get('set_id');
     // --------------------------------------
     // Check creation permissions
     // --------------------------------------
     if ($set_id === FALSE && $this->member_group != 1 && !in_array($this->member_group, $this->get_settings('can_create_sets'))) {
         show_error(lang('cannot_create_sets'));
     }
     $set = $set_id === FALSE ? ee()->low_reorder_set_model->empty_row() : ee()->low_reorder_set_model->get_one($set_id);
     // --------------------------------------
     // Get settings & permissions
     // --------------------------------------
     $set['parameters'] = ee()->low_reorder_set_model->get_params($set['parameters']);
     $set['permissions'] = json_decode($set['permissions'], TRUE);
     $perm = ee()->low_reorder_set_model->get_permissions($set['permissions']);
     if (!$set_id && $this->member_group != 1 && !in_array($this->member_group, $this->get_settings('can_create_sets')) || $set_id && !$perm['can_edit']) {
         show_error('Operation not permitted');
     }
     // --------------------------------------
     // Friendly user form elements
     // --------------------------------------
     $this->data['yesno_cache'] = $this->_yesno('clear_cache', @$set['clear_cache'] == 'y' ? 'yes' : 'no');
     $this->data['yesno_expired'] = $this->_yesno('parameters[show_expired]', @$set['parameters']['show_expired']);
     $this->data['yesno_future'] = $this->_yesno('parameters[show_future_entries]', @$set['parameters']['show_future_entries']);
     $this->data['yesno_sticky'] = $this->_yesno('parameters[sticky]', @$set['parameters']['sticky']);
     // --------------------------------------
     // Get all channels
     // --------------------------------------
     $query = ee()->db->select('channel_id, channel_name, channel_title, field_group, cat_group, status_group')->from('channels')->where('site_id', $this->site_id)->order_by('channel_title', 'asc')->get();
     $channels = low_associate_results($query->result_array(), 'channel_id');
     // Create multiple select for filter options
     $this->data['select_channel'] = form_multiselect('channels[]', low_flatten_results($channels, 'channel_title', 'channel_id'), low_delinearize(@$set['channels']), low_multiselect_size(count($channels)));
     // --------------------------------------
     // Statuses
     // --------------------------------------
     $query = ee()->db->select('status, group_name')->from('statuses')->join('status_groups', 'statuses.group_id = status_groups.group_id')->where('statuses.site_id', $this->site_id)->where_not_in('status', array('open', 'closed'))->order_by('group_name, status_order')->get();
     $statuses = array('open' => 'Open', 'closed' => 'Closed');
     foreach ($query->result() as $row) {
         $statuses[$row->group_name][$row->status] = $row->status;
     }
     $this->data['select_status'] = form_multiselect('parameters[status][]', $statuses, low_delinearize(@$set['parameters']['status']), low_multiselect_size(count($statuses) + $query->num_rows()));
     // --------------------------------------
     // Categories
     // --------------------------------------
     // Initiate some vars
     $cat_groups = array();
     // We need the category api for that
     ee()->load->library('api');
     ee()->api->instantiate('channel_categories');
     // get group details from DB
     $query = ee()->db->select('group_id, group_name, sort_order')->from('category_groups')->where('site_id', $this->site_id)->order_by('group_name', 'asc')->get();
     $cat_groups = $query->result_array();
     $cat_count = 0;
     // Loop through groups and get the category group from API
     foreach ($cat_groups as &$cat_group) {
         ee()->api_channel_categories->categories = array();
         ee()->api_channel_categories->category_tree($cat_group['group_id'], '', $cat_group['sort_order']);
         $cat_group['categories'] = ee()->api_channel_categories->categories;
         $cat_count += count($cat_group['categories']);
     }
     $this->data['category_groups'] = $cat_groups;
     $this->data['selected_category_ids'] = low_delinearize(@$set['parameters']['category']);
     // Multi select for category groups
     $this->data['select_category_groups'] = form_multiselect('cat_groups[]', low_flatten_results($cat_groups, 'group_name', 'group_id'), low_delinearize(@$set['cat_groups']), low_multiselect_size(count($cat_groups)));
     // --------------------------------------
     // Get channel fields for search: params
     // --------------------------------------
     $query = ee()->db->select('cf.field_id, cf.field_name, cf.field_label, fg.group_name')->from('channel_fields cf')->join('field_groups fg', 'cf.group_id = fg.group_id')->where('cf.site_id', $this->site_id)->order_by('fg.group_name', 'asc')->order_by('cf.field_order', 'asc')->get();
     $fields = array('' => '--');
     foreach ($query->result() as $row) {
         $fields[$row->group_name][$row->field_name] = $row->field_label;
     }
     $this->data['select_field_name'] = form_dropdown('search[fields][]', $fields, array());
     $this->data['select_field_value'] = form_input(array('name' => 'search[values][]', 'class' => 'medium'));
     // --------------------------------------
     // Get existing search filters
     // --------------------------------------
     $search = ee()->low_reorder_set_model->get_search_params($set['parameters']);
     $this->data['json_fields'] = json_encode($search);
     // --------------------------------------
     // Member groups
     // --------------------------------------
     $query = ee()->db->select('group_id, group_title')->from('member_groups')->where_not_in('group_id', array('1', '2', '3', '4'))->where('can_access_cp', 'y')->order_by('group_title', 'asc')->get();
     $this->data['member_groups'] = low_flatten_results($query->result_array(), 'group_title', 'group_id');
     // Set non-existing groups to 0
     foreach (array_keys($this->data['member_groups']) as $group_id) {
         if (!isset($set['settings']['permissions'][$group_id])) {
             $set['settings']['permissions'][$group_id] = 0;
         }
     }
     // --------------------------------------
     // Add set data to view
     // --------------------------------------
     $this->data['set'] = $set;
     // --------------------------------------
     // Add extra nav item if permitted
     // --------------------------------------
     if ($set_id && $perm['can_reorder']) {
         $this->extra_nav['reorder_entries'] = $this->base_url . AMP . 'method=reorder&set_id=' . $set_id;
     }
     // --------------------------------------
     // Set title and breadcrumb
     // --------------------------------------
     $title = $set_id === FALSE ? lang('create_new_set') : lang('edit_set') . ' #' . $set_id;
     $this->_set_cp_var('cp_page_title', $title);
     ee()->cp->set_breadcrumb($this->base_url, lang('low_reorder_module_name'));
     // Return settings form
     return $this->view('mcp_edit');
 }
Example #4
0
 /**
  * Create new collection or edit existing one
  *
  * @access      public
  * @return      string
  */
 public function edit_collection()
 {
     // --------------------------------------
     // Get collection by id or empty row
     // --------------------------------------
     $collection_id = ee()->input->get('collection_id');
     $collection = $collection_id === FALSE ? $this->collection->empty_row() : $this->collection->get_one($collection_id);
     // --------------------------------------
     // Get settings for this collection
     // --------------------------------------
     if (strlen($collection['settings'])) {
         $collection['settings'] = low_search_decode($collection['settings'], FALSE);
     }
     // --------------------------------------
     // Set default excerpt data
     // --------------------------------------
     if (!strlen($collection['excerpt'])) {
         $collection['excerpt'] = '0';
     }
     // --------------------------------------
     // Set default modifier data
     // --------------------------------------
     if (!strlen($collection['modifier'])) {
         $collection['modifier'] = '1';
     }
     // --------------------------------------
     // Merge collection data with view data
     // --------------------------------------
     $this->data = array_merge($this->data, $collection);
     // --------------------------------------
     // Get searchable channel fields
     // --------------------------------------
     $query = ee()->db->select('field_id, group_id, field_label')->from('channel_fields')->where('site_id', $this->site_id)->where('field_search', 'y')->order_by('field_order')->get();
     $fields = array();
     foreach ($query->result() as $row) {
         $fields[$row->group_id][$row->field_id] = $row->field_label;
     }
     // --------------------------------------
     // Get channels
     // --------------------------------------
     $query = ee()->db->select('channel_id, field_group, cat_group, channel_name, channel_title')->from('channels')->where('site_id', $this->site_id)->order_by('channel_title')->get();
     $channels = low_associate_results($query->result_array(), 'channel_id');
     // Add simple channel data to global JS object for later reference
     ee()->javascript->set_global('low_search_channels', $channels);
     // Add fields to channels
     foreach ($channels as &$row) {
         $row['cat_group'] = array_filter(explode('|', $row['cat_group']));
         $row['fields'] = array(0 => lang('title'));
         if (array_key_exists($row['field_group'], $fields)) {
             $row['fields'] += $fields[$row['field_group']];
         }
     }
     // --------------------------------------
     // Get category groups
     // --------------------------------------
     ee()->lang->loadfile('admin_content');
     ee()->load->model('category_model');
     $query = ee()->category_model->get_category_groups();
     $cat_groups = low_associate_results($query->result_array(), 'group_id');
     if (!empty($cat_groups)) {
         // Add default fields to groups
         foreach ($cat_groups as &$group) {
             $group['fields'] = array('cat_name' => lang('category_name'), 'cat_description' => lang('category_description'));
         }
         // Get fields for category groups
         $query = ee()->db->select('field_id, group_id, field_label')->from('category_fields')->where_in('group_id', array_keys($cat_groups))->order_by('field_order', 'asc')->get();
         foreach ($query->result() as $field) {
             $cat_groups[$field->group_id]['fields'][$field->field_id] = $field->field_label;
         }
     }
     // --------------------------------------
     // Add to view data
     // --------------------------------------
     $this->data['channels'] = $channels;
     $this->data['cat_groups'] = $cat_groups;
     // --------------------------------------
     // Set title and breadcrumb
     // --------------------------------------
     $this->_set_cp_var('cp_page_title', lang($collection_id === FALSE ? 'create_new_collection' : 'edit_collection'));
     ee()->cp->set_breadcrumb($this->mcp_url(), lang('low_search_module_name'));
     ee()->cp->set_breadcrumb($this->mcp_url('collections'), lang('collections'));
     return $this->view('mcp_edit_collection');
 }
Example #5
0
 /**
  * Home screen
  *
  * @access     public
  * @return     string
  */
 public function index()
 {
     // -------------------------------------
     //  Add title to this page
     // -------------------------------------
     $this->_set_cp_var('cp_page_title', lang('low_variables_module_name'));
     // -------------------------------------
     //  Display error message if any
     // -------------------------------------
     if ($this->error_msg != '') {
         return $this->error_msg;
     }
     // -------------------------------------
     //  Check for skipped items
     // -------------------------------------
     $skipped = ee()->session->flashdata('skipped');
     // -------------------------------------
     //  Get variable groups
     // -------------------------------------
     $query = ee()->db->select('group_id, group_label, group_notes')->from('low_variable_groups')->where('site_id', $this->site_id)->order_by('group_order', 'asc')->get();
     $groups = low_associate_results($query->result_array(), 'group_id');
     // -------------------------------------
     //  Get variable counts for groups, including ungrouped
     // -------------------------------------
     ee()->db->select('low.group_id, COUNT(low.variable_id) AS var_count')->from('low_variables AS low')->join('global_variables AS ee', 'low.variable_id = ee.variable_id')->where('ee.site_id', $this->site_id)->group_by('low.group_id');
     // Exclude hidden vars for non-managers
     if (!$this->is_manager()) {
         ee()->db->where('low.is_hidden', 'n');
     }
     $query = ee()->db->get();
     $counts = low_flatten_results($query->result_array(), 'var_count', 'group_id');
     // Add counts to groups
     foreach ($groups as $group_id => &$group) {
         $group['var_count'] = isset($counts[$group_id]) ? $counts[$group_id] : 0;
         $group['group_label'] = htmlspecialchars($group['group_label']);
         // Forget empty groups if not a manager
         if (!$this->is_manager() && $group['var_count'] == 0) {
             unset($groups[$group_id]);
         }
     }
     // -------------------------------------
     //  Add 'ungrouped' group
     // -------------------------------------
     if (isset($counts['0'])) {
         $groups['0'] = array('group_id' => '0', 'group_label' => lang('ungrouped'), 'group_notes' => '', 'var_count' => $counts['0']);
     }
     // -------------------------------------
     //  Get group id, fallback to first in $groups with a var count
     // -------------------------------------
     if (($group_id = ee()->input->get('group_id')) === FALSE || is_numeric($group_id) && !isset($groups[$group_id])) {
         foreach ($groups as $gid => $row) {
             if ($row['var_count']) {
                 $group_id = $gid;
                 break;
             }
         }
     }
     // -------------------------------------
     //  Get variables
     // -------------------------------------
     // Filter out hidden vars
     if (!$this->is_manager()) {
         ee()->db->where('is_hidden', 'n');
     }
     // Show only given group
     if (is_numeric($group_id)) {
         ee()->db->where('group_id', $group_id);
     }
     // Build vars query
     $query = ee()->db->select('*')->from('global_variables AS ee')->join('low_variables AS low', 'ee.variable_id = low.variable_id')->where('ee.site_id', $this->site_id)->order_by('low.group_id', 'asc')->order_by('low.variable_order', 'asc')->order_by('ee.variable_name', 'asc')->get();
     $vars = $query->result_array();
     $alert = array();
     // -------------------------------------
     //  Loop thru vars and add custom data
     // -------------------------------------
     foreach ($vars as $var) {
         // Fallback to default var type if type is not known
         if (!array_key_exists($var['variable_type'], $this->types)) {
             $var['variable_type'] = LOW_VAR_DEFAULT_TYPE;
         }
         // Create shortcut
         $OBJ = $this->types[$var['variable_type']];
         // Set var settings to empty array if not properly decoded
         if (!($var['variable_settings'] = low_array_decode($var['variable_settings']))) {
             $var['variable_settings'] = $OBJ->default_settings;
         }
         // Get input from var type
         $var['variable_input'] = $OBJ->display_input($var['variable_id'], $var['variable_data'], $var['variable_settings']);
         // Load CSS and JS
         $OBJ->load_assets();
         // Fallback to variable name if no label
         $var['variable_label'] = $var['variable_label'] ? $var['variable_label'] : $var['variable_name'];
         // Add to alert array if skipped
         if (is_array($skipped) && isset($skipped[$var['variable_id']])) {
             $var['error_msg'] = $skipped[$var['variable_id']];
             $alert[] = $var;
         }
         // Split by 3 newlines to seperate var header from description
         $var['variable_header'] = '';
         $tmp = explode("\n\n\n", $var['variable_notes'], 2);
         if (count($tmp) == 2) {
             $var['variable_header'] = $tmp[0];
             $var['variable_notes'] = $tmp[1];
         }
         // Group by group id
         $this->data['vars'][$var['group_id']][] = $var;
     }
     // -------------------------------------
     //  Populate data for view
     // -------------------------------------
     $this->data['groups'] = $groups;
     $this->data['group_id'] = $group_id;
     $this->data['group_ids'] = is_numeric($group_id) ? array($group_id) : array_keys($groups);
     $this->data['show_groups'] = !(!$this->is_manager() && count($this->data['groups']) <= 1);
     $this->data['is_manager'] = $this->is_manager();
     $this->data['all_ids'] = implode('|', low_flatten_results($vars, 'variable_id'));
     $this->data['skipped'] = $alert;
     $this->data['clear_cache'] = $this->settings['clear_cache'];
     // -------------------------------------
     //  Title and Crumbs
     // -------------------------------------
     return $this->view('mcp_index');
 }
Example #6
0
 /**
  * Filters
  *
  * @access      public
  * @return      string
  */
 public function filters()
 {
     // --------------------------------------
     // Load up language file
     // --------------------------------------
     ee()->lang->loadfile($this->package);
     // --------------------------------------
     // Read parameters
     // --------------------------------------
     $this->params->set();
     // --------------------------------------
     // Overwrite with shortcut?
     // --------------------------------------
     $this->_get_shortcut();
     // --------------------------------------
     // Prep params for template variables
     // --------------------------------------
     $vars = array();
     foreach ($this->params->get() as $key => $val) {
         $vars[$this->settings->prefix . $key . ':raw'] = $val;
         $vars[$this->settings->prefix . $key] = low_format($val);
     }
     // --------------------------------------
     // Add shortcut data to vars
     // --------------------------------------
     if ($this->shortcut) {
         foreach (ee()->low_search_shortcut_model->get_template_attrs() as $key) {
             $vars[$this->settings->prefix . $key] = $this->shortcut[$key];
         }
     }
     // --------------------------------------
     // Get search collections for this site
     // --------------------------------------
     // Check to see whether we actually need to get collections
     $get_collections = FALSE;
     $collections = array();
     $active_collections = array();
     // Get them only if the var pair exists
     foreach (low_array_get_prefixed(ee()->TMPL->var_pair, 'collections') as $val) {
         $this->_log('Collections variable pair found');
         $get_collections = TRUE;
         // Is the 'show' parameter set?
         // If so, get the ids
         if (isset($val['show'])) {
             $get_collections = $val['show'];
         }
     }
     // Get the collections if necessary
     if ($get_collections !== FALSE) {
         $this->_log('Getting search collection details');
         // --------------------------------------
         // Possibly limit by ids
         // --------------------------------------
         $collections = array();
         $site_collections = ee()->low_search_collection_model->get_by_site(array_values(ee()->TMPL->site_ids));
         if ($get_collections !== TRUE) {
             list($show, $in) = low_explode_param($get_collections);
             $key = low_array_is_numeric($show) ? 'collection_id' : 'collection_name';
             $site_collections = low_associate_results($site_collections, $key);
             foreach ($site_collections as $id => $row) {
                 if ($in === in_array($id, $show)) {
                     $collections[] = $row;
                 }
             }
         } else {
             $collections = array_values($site_collections);
         }
         // --------------------------------------
         // Define collection meta data
         // --------------------------------------
         $meta = array('collection_count' => 0, 'total_collections' => count($collections));
         // --------------------------------------
         // Get array of active collections
         // --------------------------------------
         $col = $this->params->get('collection');
         if (!empty($col)) {
             if (is_string($col)) {
                 list($active_collections, $in) = low_explode_param($col);
             } elseif (is_array($col)) {
                 $active_collections = $col;
             }
         }
         // --------------------------------------
         // Loop thru collections, modify rows
         // --------------------------------------
         // Numeric collections?
         $attr = low_array_is_numeric($active_collections) ? 'collection_id' : 'collection_name';
         foreach ($collections as &$row) {
             // Forget some
             unset($row['site_id'], $row['settings']);
             // Make strings html-safe
             $row = array_map('htmlspecialchars', $row);
             // Is collection selected?
             $row['collection_is_active'] = in_array($row[$attr], $active_collections) ? 'y' : '';
             // Increment collection count
             $meta['collection_count']++;
             // Merge meta with row
             $row = array_merge($row, $meta);
         }
     }
     // --------------------------------------
     // Add collections to vars array
     // --------------------------------------
     $vars['collections'] = $vars[$this->settings->prefix . 'collections'] = $collections;
     // --------------------------------------
     // Handle error messages
     // --------------------------------------
     // Main error message
     $vars['error_message'] = ee()->session->flashdata('error_message');
     // Errors per field
     if (($errors = ee()->session->flashdata('errors')) && is_array($errors)) {
         foreach ($errors as $field) {
             $vars[$this->settings->prefix . $field . '_missing'] = TRUE;
         }
     }
     // --------------------------------------
     // Parse it now
     // --------------------------------------
     $tagdata = ee()->TMPL->parse_variables_row(ee()->TMPL->tagdata, $vars);
     $tagdata = $this->_post_parse($tagdata);
     // --------------------------------------
     // Return output
     // --------------------------------------
     return $tagdata;
 }
Example #7
0
 /**
  * Build collection index
  *
  * @access      protected
  * @return      array
  */
 public function build($collection_id = FALSE, $entry_ids = FALSE, $start = FALSE)
 {
     // --------------------------------------
     // Check for collection_id or entry_id
     // --------------------------------------
     $collection_id = $collection_id !== FALSE ? $collection_id : ee()->input->get_post('collection_id');
     $entry_ids = $entry_ids !== FALSE ? $entry_ids : ee()->input->get_post('entry_id');
     // --------------------------------------
     // Either collection_id or entry_id or both must be given
     // --------------------------------------
     if (!($collection_id || $entry_ids)) {
         show_error(ee()->lang->line('not_authorized'));
     }
     // --------------------------------------
     // Start building query to get collection details
     // --------------------------------------
     ee()->db->select('lsc.collection_id, lsc.channel_id, lsc.settings, lsc.site_id');
     ee()->db->from('low_search_collections lsc');
     // --------------------------------------
     // If there's a collection id, limit query by that one
     // --------------------------------------
     if ($collection_id) {
         ee()->db->where('lsc.collection_id', $collection_id);
     }
     // --------------------------------------
     // If there's an entry_id, limit query by those
     // --------------------------------------
     if ($entry_ids) {
         // Force array
         if (!is_array($entry_ids)) {
             $entry_ids = preg_split('/\\D+/', $entry_ids);
         }
         // Get collections for given entries
         ee()->db->select('GROUP_CONCAT(ct.entry_id) AS entries');
         ee()->db->join('channel_titles ct', 'lsc.channel_id = ct.channel_id');
         ee()->db->where_in('entry_id', $entry_ids);
         ee()->db->group_by('lsc.collection_id');
     }
     // --------------------------------------
     // Execute query and get results. Bail out if none
     // --------------------------------------
     if (!($collections = ee()->db->get()->result_array())) {
         return FALSE;
     }
     $collections = low_associate_results($collections, 'collection_id');
     $channel_ids = array_unique(low_flatten_results($collections, 'channel_id'));
     // --------------------------------------
     // Get batch size
     // --------------------------------------
     $batch_size = ee()->low_search_settings->get('batch_size');
     // --------------------------------------
     // Get total number of entries that need to be indexed
     // --------------------------------------
     if ($entry_ids) {
         $num_entries = count($entry_ids);
     } else {
         ee()->db->where_in('channel_id', $channel_ids);
         $num_entries = ee()->db->count_all_results('channel_titles');
     }
     // --------------------------------------
     // Get weighted field settings only, keep track of field ids
     // --------------------------------------
     $fields = array();
     $entries = array();
     foreach ($collections as &$col) {
         $col['settings'] = array_filter(low_search_decode($col['settings'], FALSE));
         // Add field ids to fields array
         $fields = array_merge($fields, array_keys($col['settings']));
         if (isset($col['entries'])) {
             foreach (explode(',', $col['entries']) as $eid) {
                 $entries[$eid][] = $col['collection_id'];
             }
         }
     }
     // Get rid of duplicate field ids
     $fields = array_unique($fields);
     sort($fields);
     // --------------------------------------
     // Let an extension take over?
     // --------------------------------------
     if (ee()->extensions->active_hook('low_search_get_index_entries') === TRUE) {
         $index_entries = ee()->extensions->call('low_search_get_index_entries', $fields, $channel_ids, $entry_ids, $start, $batch_size);
     } else {
         // --------------------------------------
         // Create select list
         // --------------------------------------
         $select = array('t.entry_id', 't.channel_id');
         foreach ($fields as $field_id) {
             // Skip non-numeric settings
             if (!is_numeric($field_id)) {
                 continue;
             }
             $select[] = $field_id == '0' ? 't.title AS field_id_0' : 'd.field_id_' . $field_id;
         }
         // --------------------------------------
         // Start building query
         // --------------------------------------
         ee()->db->select($select)->from('channel_titles t')->join('channel_data d', 't.entry_id = d.entry_id', 'inner')->where_in('t.channel_id', $channel_ids)->order_by('entry_id', 'asc');
         // --------------------------------------
         // Optional: Limit to given entries
         // --------------------------------------
         if ($entry_ids) {
             ee()->db->where_in('t.entry_id', $entry_ids);
         }
         // --------------------------------------
         // Optional: Limit entries by batch size
         // --------------------------------------
         if ($start !== FALSE && is_numeric($start)) {
             ee()->db->limit($batch_size, $start);
         }
         // --------------------------------------
         // Query it!
         // --------------------------------------
         $query = ee()->db->get();
         // Make sure the rows are keyed by their entry_id
         $index_entries = low_associate_results($query->result_array(), 'entry_id');
         // --------------------------------------
         // Get category info for these entries
         // --------------------------------------
         if ($entry_cats = $this->get_entry_categories(array_keys($index_entries))) {
             // add the categories to the index_entries rows
             foreach ($entry_cats as $entry_id => $cats) {
                 $index_entries[$entry_id] += $cats;
             }
         }
     }
     // --------------------------------------
     // Loop thru the entries to index
     // --------------------------------------
     foreach ($index_entries as $row) {
         // If it's a given entry, loop thru its collections and rebuild index
         if (isset($entries[$row['entry_id']])) {
             foreach ($entries[$row['entry_id']] as $col_id) {
                 // Collection details
                 $col = $collections[$col_id];
                 // Build index for this entry/collection combo
                 ee()->low_search_index_model->build($col, $row);
             }
         } else {
             foreach ($collections as $col_id => $col) {
                 if ($row['channel_id'] == $col['channel_id']) {
                     ee()->low_search_index_model->build($col, $row);
                 }
             }
         }
     }
     // Determine new start
     $new_start = $start + $batch_size;
     // Are we done?
     $done = $new_start >= $num_entries;
     // --------------------------------------
     // Prep response
     // --------------------------------------
     $response = array('status' => $done ? 'done' : 'building', 'start' => (int) $new_start, 'total_entries' => (int) $num_entries, 'processed' => count($index_entries));
     return $response;
 }
Example #8
0
 /**
  * Add/modify entry in sort orders
  *
  * @access      public
  * @param       int
  * @param       array
  * @param       array
  * @return      void
  */
 public function entry_submission_end($entry_id, $meta, $data)
 {
     // -------------------------------------------
     // Not changing anything; get return value from last_call
     // -------------------------------------------
     $return = ee()->extensions->last_call;
     // -------------------------------------------
     // Get sets for this channel
     // -------------------------------------------
     $sets = ee()->low_reorder_set_model->get_by_channel($meta['channel_id']);
     $sets = low_associate_results($sets, 'set_id');
     // If no sets are found, just bail out early
     if (empty($sets)) {
         return $return;
     }
     // -------------------------------------------
     // Define array for new orders, set needle
     // -------------------------------------------
     $new_orders = $old_orders = array();
     // This entry's pipe-separated id
     $needle = "|{$entry_id}|";
     // -------------------------------------------
     // Get all old orders for the sets found
     // -------------------------------------------
     ee()->db->where_in('set_id', array_keys($sets));
     $old = ee()->low_reorder_order_model->get_all();
     foreach ($old as $row) {
         $key = $row['set_id'] . '-' . $row['cat_id'];
         $old_orders[$key] = $row['sort_order'];
     }
     unset($old);
     // -------------------------------------------
     // Make sure we get all posted categories,
     // including parents if necessary
     // -------------------------------------------
     ee()->load->library('api');
     ee()->api->instantiate('channel_categories');
     $categories = empty(ee()->api_channel_categories->cat_parents) ? array() : array_unique(ee()->api_channel_categories->cat_parents);
     // Is this entry sticky?
     $sticky = @$meta['sticky'] == 'y';
     // -------------------------------------------
     // Loop through applicable sets, populate new_orders
     // -------------------------------------------
     foreach ($sets as $set) {
         // Get the set's parameters
         $params = ee()->low_reorder_set_model->get_params($set['parameters']);
         // Skip non-sticky if sticky only
         if (isset($params['sticky']) && $params['sticky'] == 'yes' && !$sticky) {
             continue;
         }
         // Use posted category IDs for Sort By Single Category
         $cat_ids = $set['cat_option'] == 'one' ? array_filter($categories) : array(0);
         // Create array of all the Orders that must be present
         foreach ($cat_ids as $cat_id) {
             // Set the key
             $key = $set['set_id'] . '-' . $cat_id;
             $val = FALSE;
             // Check old orders
             if (isset($old_orders[$key])) {
                 $old_val = $old_orders[$key];
                 // If entry id is not present in old order
                 if (strpos($old_val, $needle) === FALSE) {
                     $val = $set['new_entries'] == 'prepend' ? $needle . ltrim($old_val, '|') : rtrim($old_val, '|') . $needle;
                 }
                 // remove reference from $old_orders
                 unset($old_orders[$key]);
             } else {
                 $val = $needle;
             }
             // Only add new order if we have a valid value
             if ($val !== FALSE) {
                 $new_orders[$key] = $val;
             }
         }
     }
     // -------------------------------------------
     // Loop through remaining old orders and remove references
     // -------------------------------------------
     foreach ($old_orders as $key => $val) {
         // Get set and cat IDs
         list($set_id, $cat_id) = explode('-', $key);
         // Remove lint
         if ($sets[$set_id]['cat_option'] == 'one' && $cat_id == 0 || $sets[$set_id]['cat_option'] != 'one' && $cat_id > 0) {
             ee()->db->where('cat_id', $cat_id);
             ee()->low_reorder_order_model->delete($set_id, 'set_id');
             continue;
         }
         // See if entry id is present in old order,
         // if so, remove it and add new order to array
         if (strpos($val, $needle) !== FALSE) {
             $new_orders[$key] = str_replace($needle, '|', $val);
         }
     }
     // -------------------------------------------
     // Update the new orders
     // -------------------------------------------
     if ($new_orders) {
         $values = array();
         // Loop through new orders and REPLACE INTO orders table
         foreach ($new_orders as $key => $val) {
             list($set_id, $cat_id) = explode('-', $key);
             $values[] = sprintf("('%s', '%s', '%s')", $set_id, $cat_id, $val);
         }
         ee()->db->query(sprintf('REPLACE INTO `%s` (`set_id`, `cat_id`, `sort_order`) VALUES %s', ee()->low_reorder_order_model->table(), implode(",\n", $values)));
         // Cleans up the order table
         ee()->low_reorder_order_model->remove_rogues();
     }
     // Play nice
     return $return;
 }
Example #9
0
 /**
  * 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);
     }
 }
Example #10
0
 /**
  * Allows for keywords="" parameter, along with its associated params
  *
  * @access     private
  * @return     void
  */
 public function filter($entry_ids)
 {
     // --------------------------------------
     // Log it
     // --------------------------------------
     $this->_log('Applying ' . __CLASS__);
     // --------------------------------------
     // Set internal collections array based on param
     // --------------------------------------
     $this->_collections = ($cols = $this->params->get('collection')) ? ee()->low_search_collection_model->get_by_param($cols) : array();
     // --------------------------------------
     // Check validity of search mode and loose ends
     // --------------------------------------
     $this->_search_mode();
     $this->_loose_ends();
     // --------------------------------------
     // Check keywords and set the search terms
     // --------------------------------------
     $this->_set_terms();
     // --------------------------------------
     // Only perform actual search if keywords are given
     // --------------------------------------
     if (empty($this->_terms)) {
         $this->_log('No keyword search');
         $this->_prep_params();
         return $entry_ids;
     }
     // --------------------------------------
     // Reset results
     // --------------------------------------
     $this->_results = array();
     // --------------------------------------
     // Begin composing query
     // --------------------------------------
     ee()->db->select($this->_fulltext ? "entry_id, collection_id, MATCH(index_text) AGAINST('{$this->_query()}') AS score" : 'entry_id, collection_id, index_text', FALSE)->from(ee()->low_search_index_model->table());
     // --------------------------------------
     // Filters used by both searches
     // --------------------------------------
     // Limit query by collection
     if ($this->_collections) {
         ee()->db->where_in('collection_id', low_flatten_results($this->_collections, 'collection_id'));
     }
     // Limit query by site
     if ($site_ids = $this->params->site_ids()) {
         ee()->db->where_in('site_id', array_values($site_ids));
     }
     // If entry ids were given, limit to those
     if ($entry_ids) {
         ee()->db->where_in('entry_id', $entry_ids);
     }
     // Add where clause
     ee()->db->where($this->_fulltext ? $this->_fulltext_keywords() : $this->_fallback_keywords(), NULL, FALSE);
     if ($this->_fulltext) {
         // Actual fulltext search
         ee()->db->order_by('score', 'desc');
         // Limit by min_score
         if ($this->params->get('min_score')) {
             list($score, $include) = $this->_min_score();
             $oper = $include ? '>=' : '>';
             ee()->db->having("score {$oper}", $score);
         }
     }
     // --------------------------------------
     // Extra search stuff
     // --------------------------------------
     if ($add_to_query = $this->params->get_prefixed('keywords-query:', TRUE)) {
         foreach ($add_to_query as $field => $val) {
             if (ee()->db->field_exists($field, ee()->low_search_index_model->table())) {
                 list($items, $in) = low_explode_param($val);
                 ee()->db->{$in ? 'where_in' : 'where_not_in'}($field, $val);
             } else {
                 $this->_log("Field {$field} does not exist in " . ee()->low_search_index_model->table());
             }
         }
     }
     // --------------------------------------
     // Perform the search
     // --------------------------------------
     $this->_log('Starting search ' . ($this->_fulltext ? '(fulltext)' : '(fallback)'));
     $query = ee()->db->get();
     // --------------------------------------
     // If the search had no results, return no results bit
     // --------------------------------------
     if ($query->num_rows == 0) {
         $this->_log('Searched but found nothing. Returning no results.');
         return array();
     }
     // --------------------------------------
     // If we do have results, continue
     // --------------------------------------
     $this->_results = $this->_fulltext ? low_associate_results($query->result_array(), 'entry_id') : $this->_get_fallback_results($query);
     // Bail out if no entry falls above the min_score threshold
     if (empty($this->_results)) {
         $this->_log('No valid results after scoring');
         return array();
     }
     // --------------------------------------
     // Modify scores for each collection
     // --------------------------------------
     if ($modifiers = array_unique(low_flatten_results($this->_collections, 'modifier'))) {
         if (!(count($modifiers) == 1 && $modifiers[0] == 1.0)) {
             $this->_log('Applying collection modifier to search results');
             foreach ($this->_results as &$row) {
                 if ($mod = (double) $this->_collections[$row['collection_id']]['modifier']) {
                     $row['score'] = $row['score'] * $mod;
                 }
             }
         }
     }
     // -------------------------------------
     // 'low_search_modify_score' hook.
     //  - Modify scoring for keyword searches
     // -------------------------------------
     if (ee()->extensions->active_hook('low_search_modify_score') === TRUE) {
         $this->_results = ee()->extensions->call('low_search_modify_score', $this->_results);
         if (empty($this->_results) || ee()->extensions->end_script === TRUE) {
             return array();
         }
     }
     // --------------------------------------
     // Sort by score
     // --------------------------------------
     uasort($this->_results, 'low_by_score');
     // --------------------------------------
     // Add results to cache, so extension can look this up
     // --------------------------------------
     return array_keys($this->_results);
 }