Example #1
0
 /**
  * Get field id for given field short name
  *
  * @access      protected
  * @param       string
  * @param       array
  * @return      int
  */
 protected function _get_field_id($str, $fields = array())
 {
     // --------------------------------------
     // Get custom channel fields from cache
     // --------------------------------------
     if (empty($fields) && !($fields = low_get_cache('channel', 'custom_channel_fields'))) {
         // If not present, get them from the API
         // Takes some effort, but its reusable for others this way
         $this->_log('Getting channel field info from API');
         ee()->load->library('api');
         ee()->api->instantiate('channel_fields');
         $fields = ee()->api_channel_fields->fetch_custom_channel_fields();
         foreach ($fields as $key => $val) {
             low_set_cache('channel', $key, $val);
         }
         $fields = $fields['custom_channel_fields'];
     }
     // --------------------------------------
     // To be somewhat compatible with MSM,
     // get the first ID that matches,
     // not just for current site, but all given.
     // --------------------------------------
     // Initiate ID
     $it = 0;
     // Check active site IDs, return first match encountered
     foreach (ee()->low_search_params->site_ids() as $site_id) {
         if (isset($fields[$site_id][$str])) {
             $it = $fields[$site_id][$str];
             break;
         }
     }
     // Please
     return $it;
 }
 /**
  * Prep variable data for saving
  *
  * @param	int		$var_id			The id of the variable
  * @param	mixed	$var_data		The value of the variable, array or string
  * @param	array	$var_settings	The settings of the variable
  * @return	string
  */
 function save_input($var_id, $var_data, $var_settings)
 {
     // Include upload library
     $this->EE->load->library('upload');
     // Get upload setting
     $upload = $this->get_setting('upload', $var_settings);
     // -------------------------------------
     //  Is there a valid upload for this var id?
     // -------------------------------------
     if ($upload && isset($_FILES['newfile']['name'][$var_id]) && !empty($_FILES['newfile']['name'][$var_id])) {
         // -------------------------------------
         //  Fetch upload folder from cache or DB
         // -------------------------------------
         $upload_cache = low_get_cache(LOW_VAR_PACKAGE, 'uploads');
         if (isset($upload_cache[$upload])) {
             $folder = $upload_cache[$upload];
         } else {
             // Fetch record from DB
             if ($folder = $this->_get_upload_preferences(NULL, $upload)) {
                 // get folder and register to session cache
                 $upload_cache[$upload] = $folder;
                 low_set_cache(LOW_VAR_PACKAGE, 'uploads', $upload_cache);
             } else {
                 // -------------------------------------
                 //  Bail out if folder wasn't found
                 // -------------------------------------
                 $this->error_msg = 'folder_not_found';
                 return FALSE;
             }
         }
         unset($upload_cache);
         // -------------------------------------
         //  Reset and fill $_FILES['userfile']
         // -------------------------------------
         $_FILES['userfile'] = array();
         // Get uploaded files details from $_FILES
         foreach ($_FILES['newfile'] as $key => $val) {
             if (isset($val[$var_id])) {
                 $_FILES['userfile'][$key] = $val[$var_id];
             }
         }
         // -------------------------------------
         //  Set parameters according to folder prefs
         // -------------------------------------
         $config = array('upload_path' => $folder['server_path'], 'allowed_types' => $folder['allowed_types'] == 'img' ? 'gif|jpg|jpeg|png|jpe' : '*', 'max_size' => $folder['max_size'], 'max_width' => $folder['max_width'], 'max_height' => $folder['max_height']);
         $this->EE->upload->initialize($config);
         // -------------------------------------
         //  Upload the file
         // -------------------------------------
         if (!$this->EE->upload->do_upload()) {
             // Set error msg and bail if unsuccessful
             $this->error_msg = $this->EE->upload->error_msg;
             return FALSE;
         }
         // get the new file's full path; the data we're going to save
         $newfile = $folder['url'] . $this->EE->upload->file_name;
         if (is_array($var_data)) {
             // add it to the selected files
             $var_data[] = $newfile;
         } else {
             // or replace single value
             $var_data = $newfile;
         }
         // Create thumbnail for this
         $this->EE->load->library('filemanager');
         $this->EE->load->model('file_model');
         $folder['file_name'] = $this->EE->upload->file_name;
         $folder['dimensions'] = NULL;
         $this->EE->filemanager->create_thumb($folder['server_path'] . $this->EE->upload->file_name, $folder);
         // Add to native DB table
         $this->EE->file_model->save_file(array('site_id' => $this->site_id, 'title' => $this->EE->upload->file_name, 'upload_location_id' => $upload, 'rel_path' => $this->EE->upload->upload_path, 'mime_type' => $this->EE->upload->file_type, 'file_name' => $this->EE->upload->file_name, 'file_size' => $this->EE->upload->file_size, 'uploaded_by_member_id' => $this->EE->session->userdata['member_id'], 'upload_date' => $this->EE->localize->now));
     }
     // END if upload?
     // Return new value
     return is_array($var_data) ? implode($this->separators[$this->get_setting('separator', $var_settings)], $var_data) : $var_data;
 }
 /**
  * Get settings
  *
  * @access     protected
  * @param      array
  * @return     array
  */
 protected function get_settings($settings = array())
 {
     if (!$settings) {
         // Check cache
         if (($this->settings = low_get_cache($this->package, 'settings')) === FALSE) {
             // Not in cache? Get from DB and add to cache
             $query = $this->EE->db->select('settings')->from('extensions')->where('class', $this->package . '_ext')->limit(1)->get();
             $this->settings = (array) @unserialize($query->row('settings'));
             // Add to cache
             low_set_cache($this->package, 'settings', $this->settings);
         }
     } else {
         $this->settings = $settings;
     }
     // Always fallback to default settings
     $this->settings = array_merge($this->default_settings, $this->settings);
     return $this->settings;
 }
Example #4
0
 /**
  * Get category id from param, URI, DB or Cache
  *
  * @access      private
  * @param       array    limited by these category groups
  * @return      int
  */
 private function _get_cat_id($cat_groups = array())
 {
     // --------------------------------------
     // Check category parameter first
     // --------------------------------------
     if ($cat_id = ee()->TMPL->fetch_param('category')) {
         $this->_log("Retrieving cat_id from parameter");
         return $cat_id;
     }
     // --------------------------------------
     // Check URI for C123
     // --------------------------------------
     if (preg_match('#/?C(\\d+)(/|$)#', ee()->uri->uri_string(), $match)) {
         $this->_log("Retrieving cat_id from URI");
         return $match[1];
     }
     // --------------------------------------
     // Check URI for category keyword
     // --------------------------------------
     // Check if cat group is not empty and reserved category word is valid
     if ($cat_groups && ee()->config->item('use_category_name') == 'y' && ($cat_word = ee()->config->item('reserved_category_word')) != '') {
         // Check if reserved cat word is in URI and if there's a segment behind it
         if (($key = array_search($cat_word, ee()->uri->segment_array())) && ($cat_url_title = ee()->uri->segment($key + 1))) {
             // Get category cache
             $categories = (array) low_get_cache(LOW_REORDER_PACKAGE, 'categories');
             // Fetch cat_id from DB if not in cache
             if (!($cat_id = (int) array_search($cat_url_title, $categories))) {
                 $this->_log("Retrieving cat_id from database");
                 $query = ee()->db->select('cat_id, cat_url_title')->from('categories')->where('cat_url_title', $cat_url_title)->where_in('group_id', $cat_groups)->get();
                 $cat_id = $query->row('cat_id');
                 $categories[$cat_id] = $query->row('cat_url_title');
                 low_set_cache(LOW_REORDER_PACKAGE, 'categories', $categories);
             } else {
                 $this->_log("Retrieving cat_id from cache");
             }
             // Return the cat id
             return $cat_id;
         }
     }
     // Return 0 by default if all else fails
     return 0;
 }
Example #5
0
 /**
  * Get channel fields from Cache or DB
  *
  * @access     private
  * @return     array
  */
 private function _get_channel_fields()
 {
     // --------------------------------------
     // Try and get channel field data from cache
     // --------------------------------------
     if (!($fields = low_get_cache('channel', 'custom_channel_fields'))) {
         // Load channel fields API
         ee()->load->library('api');
         ee()->api->instantiate('channel_fields');
         // Call API
         $fields = ee()->api_channel_fields->fetch_custom_channel_fields();
         // Register to cache
         foreach ($fields as $key => $val) {
             low_set_cache('channel', $key, $val);
         }
         // Shortcut
         $fields = $fields['custom_channel_fields'];
     }
     // --------------------------------------
     // Return the custom channel fields
     // --------------------------------------
     return $fields[$this->site_id];
 }
Example #6
0
 /**
  * Show search results
  *
  * @access      public
  * @return      string
  */
 public function results()
 {
     // --------------------------------------
     // Avoid no_results conflict
     // --------------------------------------
     $this->_prep_no_results();
     // --------------------------------------
     // Set the parameters
     // --------------------------------------
     $this->params->set();
     // --------------------------------------
     // Are we using a shortcut?
     // --------------------------------------
     $this->_get_shortcut();
     // --------------------------------------
     // If query parameter is set but empty or invalid,
     // show no_results and abort
     // --------------------------------------
     if (!$this->params->query_given() && ee()->TMPL->fetch_param('require_query') == 'yes') {
         $this->_log('Query required but none given, returning no results');
         return $this->_no_results();
     }
     // Query given but not valid == no results
     if ($this->params->query_given() && !$this->params->valid_query()) {
         $this->_log('Returning no results due to invalid query');
         return $this->_no_results();
     }
     // --------------------------------------
     // Merge tagparams into all params, set default params
     // --------------------------------------
     $this->params->combine();
     $this->params->set_defaults();
     // -------------------------------------
     // 'low_search_pre_search' hook.
     //  - Do something just before the search is executed
     // -------------------------------------
     if (ee()->extensions->active_hook('low_search_pre_search') === TRUE) {
         $params = $this->params->get();
         $params = ee()->extensions->call('low_search_pre_search', $params);
         if (ee()->extensions->end_script === TRUE) {
             return ee()->TMPL->tagdata;
         }
         $this->params->overwrite($params);
     }
     // --------------------------------------
     // Optionally log search
     // --------------------------------------
     if ($this->params->get('log_search') == 'yes' && !preg_match('#/P\\d+/?$#', ee()->uri->uri_string())) {
         $this->_log_search($this->params->get());
     }
     // --------------------------------------
     // Check orderby_sort=""
     // --------------------------------------
     if ($this->params->get('orderby_sort') && strpos($this->params->get('orderby_sort'), '|') !== FALSE) {
         list($a, $b) = explode('|', $this->params->get('orderby_sort'), 2);
         $this->params->set('orderby', $a);
         $this->params->set('sort', $b);
     }
     // --------------------------------------
     // Apply all available filters
     // --------------------------------------
     ee()->low_search_filters->filter();
     // --------------------------------------
     // What entry IDs do we have as a result?
     // --------------------------------------
     $entry_ids = ee()->low_search_filters->entry_ids();
     // --------------------------------------
     // If entry_ids is an array, some filters fired
     // --------------------------------------
     if (is_array($entry_ids)) {
         // Empty array -> No results
         if (empty($entry_ids)) {
             $this->_log('Filters found no matches, returning no results');
             return $this->_no_results();
         } else {
             if ($fixed_order_param = $this->params->get('fixed_order')) {
                 $entry_ids = low_merge_params($entry_ids, $fixed_order_param);
             }
             if ($entry_id_param = $this->params->get('entry_id')) {
                 $entry_ids = low_merge_params($entry_ids, $entry_id_param);
             }
             if (empty($entry_ids)) {
                 $this->_log('No results after entry_id/fixed_order');
                 return $this->_no_results();
             }
             $param = ee()->low_search_filters->fixed_order() || $fixed_order_param ? 'fixed_order' : 'entry_id';
             // Still here: set the entry_id param
             $this->_log("Setting {$param} param");
             $this->params->set($param, low_implode_param($entry_ids));
         }
     }
     // -------------------------------------
     // 'low_search_post_search' hook.
     //  - Do something just after the search is executed
     // -------------------------------------
     if (ee()->extensions->active_hook('low_search_post_search') === TRUE) {
         $params = $this->params->get();
         $params = ee()->extensions->call('low_search_post_search', $params);
         if (ee()->extensions->end_script === TRUE) {
             return ee()->TMPL->tagdata;
         }
         $this->params->overwrite($params);
     }
     // --------------------------------------
     // Set misc tagparams
     // --------------------------------------
     $this->params->apply();
     // --------------------------------------
     // Log the set parameters
     // --------------------------------------
     $this->_log('Parameters set: ' . low_param_string(array_merge(ee()->TMPL->tagparams, ee()->TMPL->search_fields)));
     // --------------------------------------
     // Pre-apply parameters as vars
     // --------------------------------------
     $this->_log('Pre-applying search vars to tagdata');
     ee()->TMPL->tagdata = ee()->TMPL->parse_variables_row(ee()->TMPL->tagdata, $this->params->get_vars($this->settings->prefix));
     // --------------------------------------
     // Set parameter so extension kicks in
     // --------------------------------------
     $this->params->apply('low_search', 'yes');
     // --------------------------------------
     // Remember the shortcut, if there
     // --------------------------------------
     low_set_cache(LOW_SEARCH_PACKAGE, 'shortcut', $this->shortcut);
     // -------------------------------------
     // 'low_search_channel_entries' hook.
     //  - Call your own channel:entries, fall back to default
     // -------------------------------------
     $tagdata = ee()->extensions->active_hook('low_search_channel_entries') === TRUE ? ee()->extensions->call('low_search_channel_entries') : $this->_channel_entries();
     // --------------------------------------
     // Don't post_parse no_results
     // --------------------------------------
     $tagdata = $tagdata == ee()->TMPL->no_results ? $this->_no_results($tagdata) : $this->_post_parse($tagdata);
     return $tagdata;
 }
Example #7
0
 /**
  * Check whether given string is a grid field
  */
 private function _get_matrix_col_id($field_id, $col_name)
 {
     $matrix_cols = low_get_cache(LOW_SEARCH_PACKAGE, 'matrix_cols');
     if (!isset($matrix_cols[$field_id])) {
         $query = ee()->db->select('col_id, col_name')->from('matrix_cols')->where('field_id', $field_id)->get();
         foreach ($query->result() as $row) {
             $matrix_cols[$field_id][$row->col_id] = $row->col_name;
         }
         low_set_cache(LOW_SEARCH_PACKAGE, 'matrix_cols', $matrix_cols);
     }
     return array_search($col_name, $matrix_cols[$field_id]);
 }
Example #8
0
 /**
  * Display field in publish form or Matrix cell
  *
  * @param	string	Current value for field
  * @return	string	HTML containing input field
  */
 private function _display_field($data = '', $cell = FALSE)
 {
     // -------------------------------------
     //  What's the field name?
     // -------------------------------------
     $field_name = $cell ? $this->cell_name : $this->field_name;
     // -------------------------------------
     //  We need groups!
     // -------------------------------------
     if (empty($this->settings['lv_ft_groups'])) {
         return lang('no_variable_group_selected');
     }
     // -------------------------------------
     //  Get all variable groups
     // -------------------------------------
     if (!($groups = low_get_cache($this->package, 'groups'))) {
         $query = ee()->db->select('group_id, group_label')->from('low_variable_groups')->where('site_id', ee()->config->item('site_id'))->order_by('group_order')->get();
         $groups = low_flatten_results($query->result_array(), 'group_label', 'group_id');
         if (!$groups) {
             $groups = array();
         }
         $groups += array('0' => lang('ungrouped'));
         low_set_cache($this->package, 'groups', $groups);
     }
     // -------------------------------------
     //  Get variables from groups
     // -------------------------------------
     $query = ee()->db->select('ee.variable_name, low.variable_label, low.group_id')->from('global_variables ee')->join('low_variables low', 'ee.variable_id = low.variable_id')->where('ee.site_id', ee()->config->item('site_id'))->where_in('low.group_id', $this->settings['lv_ft_groups'])->where('low.early_parsing', 'n')->where('low.is_hidden', 'n')->order_by('low.variable_order', 'asc')->get();
     // Initiate arrays to get vars by
     $unordered_vars = $vars = array();
     // Loop through found vars and group by group label
     foreach ($query->result_array() as $row) {
         $unordered_vars[$row['group_id']][$row['variable_name']] = $row['variable_label'];
     }
     // Loop through groups (which are in the right order)
     // and group the vars by group label to easily create optgroups and such
     foreach ($groups as $group_id => $group_label) {
         if (isset($unordered_vars[$group_id])) {
             $vars[$group_label] = $unordered_vars[$group_id];
         }
     }
     // Reduce to 1 dimensional array
     if (count($vars) === 1) {
         $vars = $vars[key($vars)];
     }
     // clean up
     unset($unordered_vars);
     // -------------------------------------
     //  Multiple?
     // -------------------------------------
     if (@$this->settings['lv_ft_multiple'] == 'y') {
         // Init arrays for checkboxes
         $boxes = array();
         $data = explode("\n", $data);
         // Loop thru vars and create checkbox in a label
         foreach ($vars as $key => $val) {
             if (is_array($val)) {
                 $boxes[] = "<div style=\"margin:1em 0 .5em\"><strong>{$key}</strong></div>";
                 foreach ($val as $k => $v) {
                     $boxes[] = $this->_box($field_name, $k, in_array($k, $data), $v);
                 }
             } else {
                 $boxes[] = $this->_box($field_name, $key, in_array($key, $data), $val);
             }
         }
         // return string of checkboxes
         return implode("\n", $boxes);
     } else {
         $vars = array('' => '--') + $vars;
         return form_dropdown($field_name, $vars, $data);
     }
 }
 /**
  * Get variables types from cache or settings
  *
  * @access     private
  * @return     array
  */
 private function _get_types()
 {
     if (($this->types = low_get_cache($this->package, 'types')) === FALSE) {
         $this->get_settings();
         $this->types = $this->get_types($this->settings['enabled_types']);
         low_set_cache($this->package, 'types', $this->types);
     }
     return $this->types;
 }