Advertisement:

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

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #15 on: October 24, 2017, 09:42:37 pm »
Thank you.

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #16 on: October 24, 2017, 09:45:58 pm »
Something is not ok. It;s not displaying the custom field when no category is selected
Sorry I forgot to edit the code:))
Workes ok. Thanks.
« Last Edit: October 24, 2017, 09:49:18 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 #17 on: January 02, 2018, 09:48:50 pm »
Hi Teseo.
Were is this function located?

$mSearch->dao->escape($aux);

I found only

oc-includes/htmlpurifier/HTMLPurifier/Generator.php:    public function escape($string, $quote = null) {
oc-includes/htmlpurifier/HTMLPurifier/Printer.php:    protected function escape($string) {
oc-includes/osclass/classes/database/DBCommandClass.php:        function escape($str)

and in these files there is no "dao" class extended.

Thank you.



teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: [SOLVED] Custom Field search when "All categories"
« Reply #18 on: January 03, 2018, 03:53:49 pm »
It's in DBCommandClass.php

Variable "dao" is assigned in DAO.php as an instance of DBCommandClass class:

$this->dao         = new DBCommandClass($data);

Regards

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: [SOLVED] Custom Field search when "All categories"
« Reply #19 on: January 03, 2018, 04:14:22 pm »
Thank you. I wanted to be shure that I use it ok like this:

oc-includes/osclass/controller/search.php

Code: [Select]
                                    $from = $aux['from'];
                                    $to   = $aux['to'];
                                    $start = Search::newInstance()->dao->escape($from);
                                    $end   = Search::newInstance()->dao->escape($to);
In my other thread about the NUMERIC custom field.

EDIT. and if someone uses this solution from you https://forums.osclass.org/3-5-x/custom-field-search-when-'all-categories'/msg128243/#msg128243
toghether with this new type of custom field https://forums.osclass.org/development/new-type-of-custom-field-suggestion-number-input-and-interval-filter/msg159215/#msg159215

Then your solution should be

instead of

***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
EDITED
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');
?>

Please confirm
« Last Edit: January 07, 2018, 05:57:37 pm by marius-ciclistu »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Custom Field search when "All categories"
« Reply #20 on: June 15, 2018, 02:09:31 am »
.
« Last Edit: June 20, 2018, 03:34:15 am by Sophia_OS »