Advertisement:

Author Topic: Osclass custom fields in main search bar  (Read 124 times)

AlexZzz

  • Newbie
  • *
  • Posts: 3
Osclass custom fields in main search bar
« on: July 09, 2019, 02:16:57 am »
In old post https://forums.osclass.org/3-5-x/custom-field-search-when-'all-categories'/
this issue was solved
But in Osclass 3.8 this hook does not work.

Anyone know how fix that?
Maybe a plugin already exists?

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Osclass custom fields in main search bar
« Reply #1 on: July 09, 2019, 11:58:07 am »
Hello!

I just tested a solution by @marius-ciclistu provided here and it works. Osclass 3.8, Bender, tried with dropdown select.

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)) {
                                    if(!empty(
$aux['to']) && empty($aux['from'])) $aux['from'] = '0';
                                    if(!empty(
$aux['from']) && empty($aux['to']))  $aux['to'] = '253202544000';
                                    
$from $aux['from'];
                                    
$to   $aux['to'];
                                    
$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;
                            case 
'NUMERIC':
                                if( 
is_array($aux)) {
                                    if(!empty(
$aux['to']) && empty($aux['from'])) $aux['from'] = '-1000000';
                                    if(!empty(
$aux['from']) && empty($aux['to']))  $aux['to'] = '10000000'
                                    
$from $aux['from'];
                                    
$to   $aux['to'];
                                    
$start Search::newInstance()->dao->escape($from);
                                    
$end   Search::newInstance()->dao->escape($to);
                                    
$sql "SELECT fk_i_item_id FROM $table WHERE ";
                                    
$sql .= $table.'.fk_i_field_id = '.$key.' AND CAST(';
                                    
$sql .= $start." AS SIGNED) <= CAST(".$table.".s_value AS SIGNED) AND CAST(".$end." AS SIGNED) >= CAST(".$table.".s_value AS SIGNED)";
                                    
$mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                                }
                                break;
                            default:
                                break;
            }
        }
    }
}

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


Regards.

AlexZzz

  • Newbie
  • *
  • Posts: 3
Re: Osclass custom fields in main search bar
« Reply #2 on: July 09, 2019, 02:44:44 pm »
Maybe I doing something wrog but what exacly?

I paste code to my functions.php without <_?php and ?_> like this https://yadi.sk/i/6mJQ3n_o_-MhRA (screenshot)
And put two custom fields slugs "compatibility" and "part_number" from custom fields options

Then try global search from main page, something like "NP200A5B" https://yadi.sk/i/OABAFOisQlFhyQ (screenshot)
This text from card with field "compatibility" https://yadi.sk/i/pGN9_pfsZBsbnQ (screenshot)

And got nothing https://yadi.sk/i/ylYaN5DDUFvWHw (screenshot)

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Osclass custom fields in main search bar
« Reply #3 on: July 09, 2019, 08:08:02 pm »
Misunderstanding... That topic you posted, as well as the code I posted, is for something else. It allows you to search custom fields in search SIDEBAR without selecting a category.

Check this topic: https://forums.osclass.org/development/please-help-to-do-this/

Regards.

AlexZzz

  • Newbie
  • *
  • Posts: 3
Re: Osclass custom fields in main search bar
« Reply #4 on: July 10, 2019, 07:12:50 pm »
Thanks! It's exactly what I need.

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Osclass custom fields in main search bar
« Reply #5 on: July 11, 2019, 10:49:07 pm »
You're welcome. Consider marking the thread as "SOLVED".

Regards.