Advertisement:

Author Topic: SOLVED!. New type of custom field: number input and interval filter  (Read 2370 times)

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: New type of custom field suggestion: number input and interval filter
« Reply #15 on: January 03, 2018, 12:09:19 pm »
I nailed it.
oc-includes/osclass/frm/Field.form.class.php

The addition must be:

Code: [Select]
                } else {
                    if($field['e_type']=="NUMERIC") {
                        if($search) {
                            echo '<h6>'.$field['s_name'].'</h6>';
                            echo '<input type="text" id="meta_'.$field['s_slug'].'_from" name="meta['.$field['pk_i_id'].'][from]" value="'.$field['s_value']['from'].'" />';
                            echo ' - <input type="text" id="meta_'.$field['s_slug'].'_to" name="meta['.$field['pk_i_id'].'][to]" value="'.$field['s_value']['to'].'" />';
                        } else {
                            echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                            echo '<input id="meta_'.$field['s_slug'].'" type="number" step="1" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                        }

                    } else {
                            if($search) {
                                echo '<h6>'.$field['s_name'].'</h6>';
                            } else {
                                echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                            }
                            echo '<input id="meta_'.$field['s_slug'].'" type="text" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                    }
                }
            }
        }

instead of

Code: [Select]
                } else {
                    if($search) {
                        echo '<h6>'.$field['s_name'].'</h6>';
                    } else {
                        echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                    }
                    echo '<input id="meta_'.$field['s_slug'].'" type="text" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                }
            }
        }

I also edited my previous posts.

Now the only issue remains https://forums.osclass.org/development/why-aren't-the-dateinterval-params-shown-in-search-sidebar-is-they-are-in-url/msg159193/#msg159193
« Last Edit: January 03, 2018, 01:55:10 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
SOLVED. New type of custom field: number input and interval filter
« Reply #16 on: January 03, 2018, 01:36:08 pm »
I solved the bug and my work is done here(I hope) . Please test and report any bugs.

So in oc-includes/osclass/frm/Field.form.class.php
I put:
Code: [Select]
        static public function meta($field = null, $search = false) {

            if($field!=null) {
                // date interval
                if($field['e_type']=='DATEINTERVAL') { $type='DATEINTERVAL';
                   /* $field['s_value'] = array();
                    $field['s_value']['from']   = '';
                    $field['s_value']['to']     = '';*/

                    if(!$search) {
                        $aInterval = Field::newInstance()->getDateIntervalByPrimaryKey($field['fk_i_item_id'], $field['pk_i_id']);

                        if(is_array($aInterval) && !empty($aInterval) ) {
                            $temp['from']       = @$aInterval['from'];
                            $temp['to']         = @$aInterval['to'];
                            $field['s_value']   = $temp;
                        }
                    } /*else {
                        $temp['from']   = Params::getParam('meta['.$field['pk_i_id'].'][from]');
                        $temp['to']     = Params::getParam('meta['.$field['pk_i_id'].'][to]');
                        $field['s_value'] = $temp;
                    }*/
                }
                // end date interval

                if(Session::newInstance()->_getForm('meta_'.$field['pk_i_id']) != ""){
                    $field['s_value'] = Session::newInstance()->_getForm('meta_'.$field['pk_i_id']);
                } else if(!isset($field['s_value']) || $field['s_value']=='') {
                    $s_value = Params::getParam('meta');
                    $field['s_value'] = '';
                    if(isset($s_value[$field['pk_i_id']])) {
                        if($search) {
                            if($field['e_type']=='NUMERIC') {
                               $tmp['from'] = $s_value[$field['pk_i_id']]['from'];
                               $tmp['to'] = $s_value[$field['pk_i_id']]['to'];
                               $field['s_value'] = $tmp;
                            } else {
                                $field['s_value'] = $s_value[$field['pk_i_id']];
                            }
                        } else {
                            $field['s_value'] = $s_value[$field['pk_i_id']];   
                           
                        }
                    }
                }
instead of

Code: [Select]
        static public function meta($field = null, $search = false) {

            if($field!=null) {
                // date interval
                if($field['e_type']=='DATEINTERVAL') {
                    $field['s_value'] = array();
                    $field['s_value']['from']   = '';
                    $field['s_value']['to']     = '';

                    if(!$search) {
                        $aInterval = Field::newInstance()->getDateIntervalByPrimaryKey($field['fk_i_item_id'], $field['pk_i_id']);

                        if(is_array($aInterval) && !empty($aInterval) ) {
                            $temp['from']       = @$aInterval['from'];
                            $temp['to']         = @$aInterval['to'];
                            $field['s_value']   = $temp;
                        }
                    } else {
                        $temp['from']   = Params::getParam('meta['.$field['pk_i_id'].'][from]');
                        $temp['to']     = Params::getParam('meta['.$field['pk_i_id'].'][to]');
                        $field['s_value'] = $temp;
                    }
                }
                // end date interval
                if(Session::newInstance()->_getForm('meta_'.$field['pk_i_id']) != ""){
                    $field['s_value'] = Session::newInstance()->_getForm('meta_'.$field['pk_i_id']);
                } else if(!isset($field['s_value']) || $field['s_value']=='') {
                    $s_value = Params::getParam('meta');
                    $field['s_value'] = '';
                    if(isset($s_value[$field['pk_i_id']])) {
                        $field['s_value'] = $s_value[$field['pk_i_id']];
                    }
                }

I also edited my above post.

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #17 on: January 03, 2018, 02:04:12 pm »
Final solution:
(including this one https://forums.osclass.org/development/why-aren't-the-dateinterval-params-shown-in-search-sidebar-is-they-are-in-url/msg159217/#msg159217)

1.
oc-includes/osclass/installer/struct.sql

Put

Code: [Select]
    e_type ENUM('TEXT','TEXTAREA','DROPDOWN','RADIO','CHECKBOX','URL', 'DATE', 'DATEINTERVAL','NUMERIC') NOT NULL DEFAULT  'TEXT',   
Instead of
   
Code: [Select]
    e_type ENUM('TEXT','TEXTAREA','DROPDOWN','RADIO','CHECKBOX','URL', 'DATE', 'DATEINTERVAL') NOT NULL DEFAULT  'TEXT',
+ Edit "t_meta_fields" in db to contain also 'NUMERIC'

2
oc-includes/osclass/controller/search.php

Put
EDITED to work only with one of to or from inserted in both DATEINTERVAL and NUMERIC.
Code: [Select]
                                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.")";
                                    $this->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)";
                                    $this->mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql.')');
                                }
                                break;
                            default:
                                break;
instead of

Code: [Select]
                                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.")";
                                    $this->mSearch->addConditions(DB_TABLE_PREFIX.'t_item.pk_i_id IN ('.$sql_interval.')');
                                }
                                break;
                            default:
                                break;



3
oc-includes/osclass/frm/Field.form.class.php

Put

Code: [Select]
                <option value="DATEINTERVAL" <?php if($field['e_type']=="DATEINTERVAL") { echo 'selected="selected"';};?>><?php _e('DATE INTERVAL'); ?></option>
                <option value="NUMERIC" <?php if($field['e_type']=="NUMERIC") { echo 'selected="selected"';};?>><?php _e('NUMERIC'); ?></option>
               
instead of

Code: [Select]
                <option value="DATEINTERVAL" <?php if($field['e_type']=="DATEINTERVAL") { echo 'selected="selected"';};?>><?php _e('DATE INTERVAL'); ?></option>
and
Code: [Select]
                foreach($aCustomFields as $field) {
                    if($field['e_type']=='DATEINTERVAL' || $field['e_type']=='NUMERIC') {
instead of
Code: [Select]
                foreach($aCustomFields as $field) {
                    if($field['e_type']=='DATEINTERVAL') {
                   
and
Code: [Select]
        static public function meta($field = null, $search = false) {

            if($field!=null) {
                // date interval
                if($field['e_type']=='DATEINTERVAL') { $type='DATEINTERVAL';
                   /* $field['s_value'] = array();
                    $field['s_value']['from']   = '';
                    $field['s_value']['to']     = '';*/

                    if(!$search) {
                        $aInterval = Field::newInstance()->getDateIntervalByPrimaryKey($field['fk_i_item_id'], $field['pk_i_id']);

                        if(is_array($aInterval) && !empty($aInterval) ) {
                            $temp['from']       = @$aInterval['from'];
                            $temp['to']         = @$aInterval['to'];
                            $field['s_value']   = $temp;
                        }
                    } /*else {
                        $temp['from']   = Params::getParam('meta['.$field['pk_i_id'].'][from]');
                        $temp['to']     = Params::getParam('meta['.$field['pk_i_id'].'][to]');
                        $field['s_value'] = $temp;
                    }*/
                }
                // end date interval

                if(Session::newInstance()->_getForm('meta_'.$field['pk_i_id']) != ""){
                    $field['s_value'] = Session::newInstance()->_getForm('meta_'.$field['pk_i_id']);
                } else if(!isset($field['s_value']) || $field['s_value']=='') {
                    $s_value = Params::getParam('meta');
                    $field['s_value'] = '';
                    if(isset($s_value[$field['pk_i_id']])) {
                        if($search) {
                            if($field['e_type']=='NUMERIC') {
                               $tmp['from'] = @$s_value[$field['pk_i_id']]['from'];
                               $tmp['to'] = @$s_value[$field['pk_i_id']]['to'];
                               $field['s_value'] = $tmp;
                            } else {
                                $field['s_value'] = $s_value[$field['pk_i_id']];
                            }
                        } else {
                            $field['s_value'] = $s_value[$field['pk_i_id']];   
                           
                        }
                    }
                }
instead of

Code: [Select]
        static public function meta($field = null, $search = false) {

            if($field!=null) {
                // date interval
                if($field['e_type']=='DATEINTERVAL') {
                    $field['s_value'] = array();
                    $field['s_value']['from']   = '';
                    $field['s_value']['to']     = '';

                    if(!$search) {
                        $aInterval = Field::newInstance()->getDateIntervalByPrimaryKey($field['fk_i_item_id'], $field['pk_i_id']);

                        if(is_array($aInterval) && !empty($aInterval) ) {
                            $temp['from']       = @$aInterval['from'];
                            $temp['to']         = @$aInterval['to'];
                            $field['s_value']   = $temp;
                        }
                    } else {
                        $temp['from']   = Params::getParam('meta['.$field['pk_i_id'].'][from]');
                        $temp['to']     = Params::getParam('meta['.$field['pk_i_id'].'][to]');
                        $field['s_value'] = $temp;
                    }
                }
                // end date interval
                if(Session::newInstance()->_getForm('meta_'.$field['pk_i_id']) != ""){
                    $field['s_value'] = Session::newInstance()->_getForm('meta_'.$field['pk_i_id']);
                } else if(!isset($field['s_value']) || $field['s_value']=='') {
                    $s_value = Params::getParam('meta');
                    $field['s_value'] = '';
                    if(isset($s_value[$field['pk_i_id']])) {
                        $field['s_value'] = $s_value[$field['pk_i_id']];
                    }
                }
and

EDITED with @ to avoid php warnings when one of the fields from or to is missing

Code: [Select]
                    echo '<input type="hidden" id="meta_'.$field['s_slug'].'_from" name="meta['.$field['pk_i_id'].'][from]" value="'.@$field['s_value']['from'].'" />';
                    echo '<input type="text" id="" class="meta_'.$field['s_slug'].'_from cf_date_interval" value="" />';
                    FieldForm::initDatePicker('meta_'.$field['s_slug'].'_from', osc_date_format(), $field['s_value']['from'], 'from');

                    echo ' ' . __('to'). ' ';
                    echo '<input type="hidden" id="meta_'.$field['s_slug'].'_to" name="meta['.$field['pk_i_id'].'][to]" value="'.@$field['s_value']['to'].'" />';
                    echo '<input type="text" id="" class="meta_'.$field['s_slug'].'_to cf_date_interval" value="" />';
                    FieldForm::initDatePicker('meta_'.$field['s_slug'].'_to', osc_date_format(), $field['s_value']['to'], 'to');

                } else {
                    if($field['e_type']=="NUMERIC") {
                        if($search) {
                            echo '<h6>'.$field['s_name'].'</h6>';
                            echo '<input type="text" id="meta_'.$field['s_slug'].'_from" name="meta['.$field['pk_i_id'].'][from]" value="'.@$field['s_value']['from'].'" />';
                            echo ' - <input type="text" id="meta_'.$field['s_slug'].'_to" name="meta['.$field['pk_i_id'].'][to]" value="'.@$field['s_value']['to'].'" />';
                        } else {
                            echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                            echo '<input id="meta_'.$field['s_slug'].'" type="number" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                        }

                    } else {
                            if($search) {
                                echo '<h6>'.$field['s_name'].'</h6>';
                            } else {
                                echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                            }
                            echo '<input id="meta_'.$field['s_slug'].'" type="text" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                    }
                }
            }
        }

instead of

Code: [Select]
                    echo '<input type="hidden" id="meta_'.$field['s_slug'].'_from" name="meta['.$field['pk_i_id'].'][from]" value="'.$field['s_value']['from'].'" />';
                    echo '<input type="text" id="" class="meta_'.$field['s_slug'].'_from cf_date_interval" value="" />';
                    FieldForm::initDatePicker('meta_'.$field['s_slug'].'_from', osc_date_format(), $field['s_value']['from'], 'from');

                    echo ' ' . __('to'). ' ';
                    echo '<input type="hidden" id="meta_'.$field['s_slug'].'_to" name="meta['.$field['pk_i_id'].'][to]" value="'.$field['s_value']['to'].'" />';
                    echo '<input type="text" id="" class="meta_'.$field['s_slug'].'_to cf_date_interval" value="" />';
                    FieldForm::initDatePicker('meta_'.$field['s_slug'].'_to', osc_date_format(), $field['s_value']['to'], 'to');

                } else {
                    if($search) {
                        echo '<h6>'.$field['s_name'].'</h6>';
                    } else {
                        echo '<label for="meta_'.$field['s_slug'].'">'.$field['s_name'].': </label>';
                    }
                    echo '<input id="meta_'.$field['s_slug'].'" type="text" name="meta['.$field['pk_i_id'].']" value="' . osc_esc_html((isset($field) && isset($field["s_value"])) ? $field["s_value"] : "") . '" />';
                }
            }
        }
« Last Edit: January 30, 2018, 09:50:41 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #18 on: January 05, 2018, 02:13:05 pm »
 I found that without permalinks enabled this new custom field is not working in search sidebar.

EDIT None of the custom fields work in search-sidebar without permalinks. Is it from my script or is a general thing? Edited: it's a general thing.

Edited:

Solution for permalinks off : https://forums.osclass.org/general-help/custom-fields-can-not-search-by-filters!!!/msg162800/#msg162800
« Last Edit: June 02, 2018, 07:01:52 pm by marius-ciclistu »

sniti01

  • Newbie
  • *
  • Posts: 2
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #19 on: August 14, 2018, 03:52:11 pm »
How to make a multilingual interval filter? The Multilanguage Custom Fields plugin makes Field 1 of the interval.

WEBmods

  • Hero Member
  • *****
  • Posts: 936
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #20 on: August 14, 2018, 04:43:01 pm »
Hello,

If I understood correctly, the number input doesn't show when using Multilanguage Custom Fields plugin, if that's correct this may work.

There should be a PHP file inside that plugin that looks like Field.form.class.php file. Check if you can find it and edit it like you would edit Field.form.class.php.

Regards.
« Last Edit: August 14, 2018, 04:44:43 pm by patrickFromCroatia »

grisbi

  • Newbie
  • *
  • Posts: 44
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #21 on: July 25, 2019, 05:49:53 pm »
Hello

why I have only one field displayed instead of two fields, a field mni and a field maxi same problem with "dateinterval"

Thank you for your help

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #22 on: July 25, 2019, 06:01:02 pm »
Did you made all the core changes?

grisbi

  • Newbie
  • *
  • Posts: 44
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #23 on: July 25, 2019, 07:10:47 pm »
hi Marius
as your post I created a field "NUMERIC" in custom fields but already before date interval displayed a single field

I tried several themes = ditto

thanks for your help

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #24 on: July 25, 2019, 07:13:59 pm »
But did you made the core changes?

grisbi

  • Newbie
  • *
  • Posts: 44
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #25 on: July 25, 2019, 07:28:29 pm »
no other changes

grisbi

  • Newbie
  • *
  • Posts: 44
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #26 on: July 25, 2019, 08:16:35 pm »
I handed the files as original and dateinterval works
but i would like to have the possibility to create custom fields with interval example age, km dimensions etc

thanks in advance

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #27 on: July 26, 2019, 12:22:17 pm »
:) why do you think that I posted core mods for this to work?
Osclass has no numeric interval  custom field by default.

grisbi

  • Newbie
  • *
  • Posts: 44
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #28 on: July 26, 2019, 02:57:56 pm »
Now it works, an error on my part in the file "Field.form.class.php" excuses to you Marius for the time spent of my fault.

Forgive me for my bad English

see you soon

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: SOLVED!. New type of custom field: number input and interval filter
« Reply #29 on: July 26, 2019, 04:06:51 pm »
;)