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:
<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:
<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:
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.