Advertisement:

Author Topic: [SOLVED] Custom Field search when "All categories"  (Read 5223 times)

destabilizator

  • Newbie
  • *
  • Posts: 24
[SOLVED] Custom Field search when "All categories"
« on: August 24, 2015, 04:44:10 pm »
I have a custom field for listings and set "Tick to allow searches by this field". When I just click "Search" (no search string or category specified), it lists all listings. But I don't see my custom field in the search-sidebar.
If I refine the category (from here or search empty string with some category selected), it shows my custom field in search-sidebar.
Any hint to fix this? So basically user can filter "all listings in all categories" by my custom field.
« Last Edit: August 28, 2015, 03:22:21 pm by destabilizator »

frosticek

  • Hero Member
  • *****
  • Posts: 3948
Re: Custom Field search when "All categories"
« Reply #1 on: August 24, 2015, 04:55:23 pm »
@destabilizator
- every custom fields needs to be assigned to some categories
- that means custom field is dependand on category
- if no category is selected in search, no custom field is shown as all custom fields are category related
- if you select category, all CFs related to this category are shown
- even if you attach CF to all categories, still it is required to have some category selected

Therefore, no bug there, but it would be nice to have checkbox there "Use globally, not category related"

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Custom Field search when "All categories"
« Reply #2 on: August 25, 2015, 03:35:45 pm »
***CORRECTED AGAIN***

Hi,

Here's a way to achieve that:

Add this at the very bottom of your theme functions.php:
Notes:
1.- Take care not to leave blank lines after this.
2.- If your theme functions.php doesn't end with ?> skip first line of my code.

Code: [Select]
<?php
function cust_global_custom_field_form() {
    if(!
osc_search_category_id()) {
        
$global_custom_fields = array('dropdown-1''other-dropdown'); // Put here one or more custom fields slugs

        
$nField = new Field();

        foreach (
$global_custom_fields AS $slug) {
            
$global_custom_field $nField->findBySlug($slug);
            
FieldForm::meta($global_custom_fieldtrue);
            echo 
"<p></p>";
        }
    }
}

osc_add_hook('search_form''cust_global_custom_field_form');

function 
cust_global_custom_field_search_conditions($params) {
    if (@
$params['meta'] && !@$params['sCategory']) {
        
$metas $params['meta'];
        
        
$mSearch Search::newInstance(); 
        
$table DB_TABLE_PREFIX.'t_item_meta';
        
        foreach (
$metas as $key => $aux) {
            
$cField = new Field();
            
$field $cField->findByPrimaryKey($key);
            switch (
$field['e_type']) {
                case 
'TEXTAREA':
                case 
'TEXT':
                case 
'URL':
                    if(
$aux!='') {
                        
$aux "%$aux%";
                        
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                        
$str_escaped $mSearch->dao->escape($aux);
                        
$sql .= $table.'.fk_i_field_id = '.$key.' AND ';
                        
$sql .= $table.".s_value LIKE ".$str_escaped;
                        
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                    }
                    break;
                case 
'DROPDOWN':
                case 
'RADIO':
                    if(
$aux!='') {
                        
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                        
$str_escaped $mSearch->dao->escape($aux);
                        
$sql .= $table.'.fk_i_field_id = '.$key.' AND ';
                        
$sql .= $table.".s_value = ".$str_escaped;
                        
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                    }
                    break;
                case 
'CHECKBOX':
                    if(
$aux!='') {
                        
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                        
$sql .= $table.'.fk_i_field_id = '.$key.' AND ';
                        
$sql .= $table.".s_value = 1";
                        
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                    }
                    break;
                case 
'DATE':
                    if(
$aux!='') {
                        
$y = (int)date('Y'$aux);
                        
$m = (int)date('n'$aux);
                        
$d = (int)date('j'$aux);
                        
$start mktime('0''0''0'$m$d$y);
                        
$end   mktime('23''59''59'$m$d$y);
                        
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                        
$sql .= $table.'.fk_i_field_id = '.$key.' AND ';
                        
$sql .= $table.".s_value >= ".($start)." AND ";
                        
$sql .= $table.".s_value <= ".$end;
                        
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                    }
                    break;
                case 
'DATEINTERVAL':
                    if( 
is_array($aux) && (!empty($aux['from']) && !empty($aux['to'])) ) {
                        
$from $aux['from'];
                        
$to   $aux['to'];
                        
$start $from;
                        
$end   $to;
                        
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                        
$sql .= $table.'.fk_i_field_id = '.$key.' AND ';
                        
$sql .= $start." >= ".$table.".s_value AND s_multi = 'from'";
                        
$sql1 "SELECT fk_i_item_id FROM $table WHERE ";
                        
$sql1 .= $table.".fk_i_field_id = ".$key." AND ";
                        
$sql1 .= $end." <= ".$table.".s_value AND s_multi = 'to'";
                        
$sql_interval "select a.fk_i_item_id from (".$sql.") a where a.fk_i_item_id IN (".$sql1.")";
                        
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql_interval.')');
                    }
                    break; 
                default:
                    break;
            }
        }
    }
}

osc_add_hook('search_conditions''cust_global_custom_field_search_conditions');
?>


You need to manually set one or several "global" custom fields using their slug (Identifier name) to identify them:

Quote
$global_custom_fields = array('dropdown-1', 'other-dropdown'); // Put here one or more custom fields slugs

Regards
« Last Edit: October 24, 2017, 02:15:25 pm by teseo »

destabilizator

  • Newbie
  • *
  • Posts: 24
Re: Custom Field search when "All categories"
« Reply #3 on: August 28, 2015, 11:14:10 am »
Thanks Teseo, this works on the first try! :)

On a related note, I'm using http://plugins-zone.com/free-plugins-for-osclass/tags-plugin.html and I'd like following to happen:

- User enters eg. "blabla hehe" in the main-search field
- apart from normal search, I'll take the user's input, split it to "blabla" and "hehe" words
- I'd like to search for "blabla" and "hehe" also in tags, so the output of the search is every listing where "blabla hehe" is written + those which have "blabla" and "hehe" as tags

Bonus tought: would it be hard to modify even the normal search so it does search for "blabla hehe" "blabla" "hehe"?

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Custom Field search when "All categories"
« Reply #4 on: August 28, 2015, 12:55:59 pm »
You're welcome. :) Please add [SOLVED] to the title of this thread.

Regarding your new question, please open a new thread. I don't know that plugin but I'd take a look at it.

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: [SOLVED] Custom Field search when "All categories"
« Reply #5 on: April 21, 2017, 02:10:31 pm »
Hi,

Detected a bug in the code, it wasn't working properly when using several custom fields. Corrected in the post above.

Regards

shawnpaul

  • Newbie
  • *
  • Posts: 4
Re: [SOLVED] Custom Field search when "All categories"
« Reply #6 on: October 17, 2017, 06:27:09 am »
Not sure why but this does not work for me. When I paste it to the end of functions.php in my theme folder, the entire website gives me an error 500. Can not even access admin panel afterwards.

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #7 on: October 17, 2017, 11:28:12 am »
Look for space or new line after you passed the code into functions. It must be a typo.

Quote
Notes:
1.- Take care not to leave blank lines after this.
2.- If your theme functions.php doesn't end with ?> skip first line of my code.

For me it workes great. Thank you Teseo.
« Last Edit: October 17, 2017, 12:05:03 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #8 on: October 17, 2017, 12:19:32 pm »
Teseo, I get

 PHP Notice:  Undefined index: sCategory in /oc-content/themes/bender/functions.php on line 880 //  when I select one custom field
 PHP Notice:  Undefined index: meta in /oc-content/themes/bender/functions.php on line 880 // when I don't select a custom field

for

Code: [Select]
    if ($params['meta'] && !$params['sCategory']) {
This time when I use the search.

The only case when a notice does not apear is when a category is selected and the global custom field is selected.

SOLVED with

Code: [Select]
    if (isset($params['meta']) && !isset($params['sCategory'])) {
« Last Edit: October 17, 2017, 12:26:30 pm by marius-ciclistu »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: [SOLVED] Custom Field search when "All categories"
« Reply #9 on: October 17, 2017, 12:44:15 pm »
Thanks marius.

This will be enough:

Code: [Select]
    if (@$params['meta'] && !@$params['sCategory']) {
Regards

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #10 on: October 17, 2017, 12:46:00 pm »
Oh yeah... I forgot about the @ :)) anyway. It works fine. Thanks again.

shawnpaul

  • Newbie
  • *
  • Posts: 4
Re: [SOLVED] Custom Field search when "All categories"
« Reply #11 on: October 17, 2017, 07:03:57 pm »
Will this work with the custom attributes plugin? I am unsure how to find the slug id of attributes added with the plugin. Also im using the osclass wizard plugin if that makes a difference. Any help would be great.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: [SOLVED] Custom Field search when "All categories"
« Reply #12 on: October 18, 2017, 04:42:24 pm »
Hi,

No, a completely different mechanism.

Regards

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #13 on: October 23, 2017, 06:40:39 pm »
Teseo. Is this method taking care of the posible sql injection in case the custom field is text type?
At first look it isn't....

Edit And even if it's of other type, with F12 in browser the values could be altered.
« Last Edit: October 23, 2017, 06:45:23 pm by marius-ciclistu »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: [SOLVED] Custom Field search when "All categories"
« Reply #14 on: October 24, 2017, 02:18:09 pm »
Hi,

You're right (although I couldn't see any way to introduce here something really harmful). I've corrected the code, now all the values are escaped following the Osclass standard mechanism.

Regards