Advertisement:

Author Topic: Search by "YEAR"  (Read 3270 times)

coolcrash

  • Newbie
  • *
  • Posts: 4
Search by "YEAR"
« on: November 11, 2015, 05:57:47 pm »
I think everybody tought but no one talked about it, is there any chance to create a search by Year of the Car, for example: I just want to search cars from year 2000 to current.

Thanks in advance

Fernando36

  • Newbie
  • *
  • Posts: 16
Re: Search by "YEAR"
« Reply #1 on: November 25, 2015, 12:13:53 pm »
I solve this issue when I delete this opition on cars atribute and create this field in osclass custom atributes + search field range plugin

envmar

  • Newbie
  • *
  • Posts: 8
Re: Search by "YEAR"
« Reply #2 on: February 25, 2016, 04:57:43 am »
Here's some code that will add year search to the cars attributes page. It allows you to do year ranges with a minimum year and max year. More comments about it after the code example.

1) Edit the file os-content/plugins/cars_attributes/search_form.php

2) Find the search fields you want to place it near.  So for example after "Car Type" and before "Transmission" would be around line 74 after the "</div>" and before the "<div class="row one_input">"

3) When you've found where you want to add these form fields insert this code:
Code: [Select]
    <div class="row one_input">
        <h6><?php _e('Year Range''cars_attributes'); ?></h6>
        <?php $d_current_year date ('Y'); ?>
<select name="min_year" id="min_year">
            <?php $d_min_year Params::getParam('min_year') ; ?>
            <option value=""><?php _e('Min. Year''cars_attributes'); ?></option>
            <?php foreach (range(1850$d_current_year) as $d_year) { ?>
                <option value="<?php echo $d_year?>" <?php if($d_year==$d_min_year) { echo 'selected="selected"'; } ?>><?php echo $d_year?></option>
            <?php ?>
</select>   
<select name="max_year" id="max_year">
            <?php $d_max_year Params::getParam('max_year') ; ?>
            <option value=""><?php _e('oMax. Year''cars_attributes'); ?></option>
            <?php foreach (range(1850$d_current_year) as $d_year) { ?>
                <option value="<?php echo $d_year?>" <?php if($d_year==$d_max_year) { echo 'selected="selected"'; } ?>><?php echo $d_year?></option>
            <?php ?>
</select>   
    </div>

4) Go back to the very top of the file and add this code before anything else:
Code: [Select]
<script type="text/javascript">
$(document).ready(function() {
var min_year_limit = 1850;
var max_year_limit = new Date().getFullYear();

var min_year = $('#min_year').val();
var max_year = $('#max_year').val();

/* Rebuild min_year selection */
$.fn.rebuildMinYear = function() {
var options = '<option value="">Min. Year</option>';
var max_year_loop = max_year_limit;

// If we already have a good max_year set then make sure our loop knows
if (max_year !=0 && max_year <= max_year_limit && max_year >= min_year_limit) {
max_year_loop = max_year;
}

// Rebuild the options
for (var i = min_year_limit; i <= max_year_loop; i++) {
options += "<option value='" +i+"'>"+i+"</option>";
}
$('#min_year').html(options);

// Ensure the proper option is set.
$('#min_year').val(min_year).change();
};

/* Rebuild max_year selection */
$.fn.rebuildMaxYear = function() {

var options = '<option value="">Max. Year</option>';
var min_year_loop = min_year_limit;

// Make sure min_year is in range
if (min_year != 0 && min_year >= min_year_limit && min_year <= max_year_limit) {
min_year_loop = min_year;
}

// Rebuild the options
for (var i = min_year_loop; i <= max_year_limit; i++) {
options += "<option value='" +i+"'>"+i+"</option>";
}
$('#max_year').html(options);

// Ensure the proper option is set.
$('#max_year').val(max_year).change();
};

/* check if limits are within range when page loads - if not, deal with it */
if (min_year > max_year || min_year < min_year_limit || max_year > max_year_limit) {
min_year = '';
max_year = '';
$(this).rebuildMinYear();
$(this).rebuildMaxYear();
}

$('#min_year').change(function(event) {
if (event.originalEvent !== undefined) { // min year was changed by user
min_year = $('#min_year').val(); // get current selections
max_year = $('#max_year').val();
if (min_year > max_year && min_year < max_year_limit && max_year != 0) {
// Because user changed min_year it has priority. Reset max_year if it falls out of range.
max_year = '';
}

$(this).rebuildMaxYear(); // Call function to rebuild max_year selection
}
});

$('#max_year').change(function(event) { // Logic follows same pattern as #min_year change
if (event.originalEvent !== undefined) {
min_year = $('#min_year').val();
max_year = $('#max_year').val();

if (max_year < min_year && max_year > min_year_limit && min_year != 0) {
min_year = '';
}

$(this).rebuildMinYear();
}
});
});
</script>

5) Save your file.

6) Open file os-content/plugins/cars_attributes/index.php

7) Around line 47 add the following code just after the "break;" line from the transmission case:
Code: [Select]
case 'min_year':
if ( (int)$value >= 1850 && (int)$value < date('Y') ) {
Search::newInstance()->addConditions(sprintf("%st_item_car_attr.i_year >= '%s'", DB_TABLE_PREFIX, (int)$value));
$has_conditions = true;
}
break;
case 'max_year':
if ( (int)$value > 1850 && (int)$value < date('Y') ) {
Search::newInstance()->addConditions(sprintf("%st_item_car_attr.i_year <= '%s'", DB_TABLE_PREFIX, (int)$value));
$has_conditions = true;
}
break;

8 ) Save the file.

Notes:

The script is hard coded to do a date range from 1850 to the current year:

      - MINIMUM YEAR: If you want to change the minimum year then search for "1850" and replace it with the year you desire. You should find two occurrences of it (one in PHP the other JavaScript).

      - MAXIMUM YEAR: If you want to change this, there are two places to do so:
           1) One is near the top "var max_year_limit = new Date().getFullYear(); " to change that to a hard coded limit remove "new Date().getFullYear();" and replace with your date, for example: "2000;" so it would look like "var max_year_limit = 2000;"
           2) Second place would be around Line 162 with the current changes. It looks like:     <?php $d_current_year = date ('Y'); ?> you would remove "date ('Y');" and replace  with your year for example "<?php $d_current_year = 2000; ?>"


It would probably be better for most to have the script take the years from the database but that would require editing more files. Maybe I'll show an example of that at a later date.

I threw this together last night, there hasn't been much testing so use at your own risk. :)
« Last Edit: February 25, 2016, 09:19:23 am by envmar »

Fernando36

  • Newbie
  • *
  • Posts: 16
Re: Search by "YEAR"
« Reply #3 on: May 30, 2016, 07:37:04 am »
I Try to do this example and Its works fine.
The one issue that I need to solve is when the user is selecting the year, the first year that show is the minimum year, but for the best interface for user, it necessary to show maximum year first when user is select the year.
Someone help me?

Tks.
Fernando

Fernando36

  • Newbie
  • *
  • Posts: 16
Re: Search by "YEAR"
« Reply #4 on: June 26, 2016, 03:31:00 am »
Sorry, only invert the range as show bellow:

    <div class="row one_input">
        <h6><?php _e('Year Range', 'cars_attributes'); ?></h6>
        <?php $d_current_year = date ('Y'); ?>
      <select name="min_year" id="min_year">
            <?php $d_min_year = Params::getParam('min_year') ; ?>
            <option value=""><?php _e('Min. Year', 'cars_attributes'); ?></option>
            <?php foreach (range($d_current_year,1850) as $d_year) { ?>
                <option value="<?php echo $d_year; ?>" <?php if($d_year==$d_min_year) { echo 'selected="selected"'; } ?>><?php echo $d_year; ?></option>
            <?php } ?>
      </select>   
      <select name="max_year" id="max_year">
            <?php $d_max_year = Params::getParam('max_year') ; ?>
            <option value=""><?php _e('oMax. Year', 'cars_attributes'); ?></option>
            <?php foreach (range($d_current_year,1850) as $d_year) { ?>
                <option value="<?php echo $d_year; ?>" <?php if($d_year==$d_max_year) { echo 'selected="selected"'; } ?>><?php echo $d_year; ?></option>
            <?php } ?>
      </select>   
    </div>

bested

  • Newbie
  • *
  • Posts: 8
Re: Search by "YEAR"
« Reply #5 on: May 14, 2017, 01:56:07 am »
Hi,

The modifications worked very well to achieve the year range search.

However, I cannot get the years to list from current to old for both drop down lists.  I followed the exact instructions, especially the last post, but my lists still show from old to current years in the drop down menus.

I tried to figure it out but I winded up breaking the code.  Can someone please help and tell me what specifically needs to be done to fix that? 

Thanks.