Advertisement:

Author Topic: Sort ads by custom field  (Read 3013 times)

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Sort ads by custom field
« Reply #15 on: December 20, 2016, 10:27:20 pm »
Hi teseo, don't worries I understand.

Currently I do not have any hidden field. The user see the field and onclick will open the calendar by jquery.

Well the new conditional not works.... maybe is wrong 'ascStartdate' inside?

With inversion (!=) works to "ASC". So I assume the name can be wrong.

Sorry, about this... I'm thinking how I can take a solution to avoid "ASC", or other better solution to keeping s_startdate with same results, but filtering with other sOrders => iOrderType.

Thanks again
Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Sort ads by custom field
« Reply #16 on: December 21, 2016, 08:48:13 pm »
Let's see if I can explain it all:

In any theme, the form in search sidebar (filters) starts with something like (this is Bender):

Code: [Select]
div class="filters">
    <form action="http://mysite.com/index.php" method="get" class="nocsrf">
        <input type="hidden" name="page" value="search"/>
        <input type="hidden" name="sOrder" value="dt_pub_date" />
        <input type="hidden" name="iOrderType" value="desc" />

So, if you want to use a new type of order based on your custom field "startdate", you'd need to programmatically change the value of the two hidden inputs related to Order. Let's think your Datepicker custom fields has ID "startdate", and the order type is "s_startdate":

Code: [Select]
$("#startdate").datepicker({
    onSelect: function() {
        $("input[name='sOrder']").val('s_startdate');
        $("input[name='iOrderType']").val('asc');
    }
});

When the user picks a date, this will change hidden input sOrder value to s_startdate

Then in functions.php:

Code: [Select]
<?php
function cust_events_search_conditions() {
    if (
Params::getParam('startdate')) {
        
$mSearch Search::newInstance();
        
$query_elements = (array)json_decode($mSearch->toJson());
        
$joins count($query_elements['tables_join']);

        
$mSearch->addJoinTable($joinsDB_TABLE_PREFIX 't_item_events e'DB_TABLE_PREFIX 't_item.pk_i_id = e.fk_i_item_id''LEFT');
        
$mSearch->addConditions(sprintf("e.s_startdate >= '%s'"Params::getParam('startdate')));
        if (
Params::getParam('sOrder') == 's_startdate'$mSearch->order("e.s_startdate"Params::getParam('iOrderType'));
    }
}

osc_add_hook ('search_conditions','cust_events_search_conditions');
?>


Then in the next page the user should see only ads with events later than selected date, ordered by date ascendant. If then the user changed order expressly (with the normal dropdown) the list would be ordered according to their selection, no by s_startdate.

The only thing missing here would be to add a new "Order by Start Date" to the normal selector to allow the user to go back where everything started with no need to select the date again.

Now, I haven't tested this myself, let me know if it works for you.

Regards


fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Sort ads by custom field
« Reply #17 on: December 22, 2016, 05:27:14 pm »
Hi teseo, sorry just now. I tested some times the new code posted.

Really javascript change the values on hidden fields, I checked this with inspector of firefox.

The problem is the rest, no appears order by recent date "ASC" to s_stardate on search results.

I mean, first step is: user will click to a date, javascript change the values, but no recent date "asc" to s_startdate on search results.

That's means can be the php function not assuming "asc" to s_startdate, or values of hidden fields was be volatile after refresh the page.

But php not load in firstly with these values in memory, or really need more javascript but inside the php function to confirm/keeping the values on fields?

Sorry, I just try to supose some reason to know why not assume the values after refresh page.

Thanks so far of all on this, is not a easy task to solve.

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Sort ads by custom field
« Reply #18 on: December 22, 2016, 06:44:52 pm »
Hm... Definitely I'll need to test all this myself, I'll get back to you in the next few days.

Happy holidays, regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Sort ads by custom field
« Reply #19 on: December 22, 2016, 06:53:46 pm »
Thanks teseo, happy holidays!  :)

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Sort ads by custom field
« Reply #20 on: December 22, 2016, 07:56:04 pm »
Got a little break, let's see:

Indeed on filters search the hidden param sOrder is always filled with "dt_pub_date" because somewhere there is a check for "allowed sort orders". To trick the script into accept a new order type there you need a second function in functions.php:

Code: [Select]
<?php
function cust_events_change_sOrder() {
    if (
Params::getParam('sOrder') == 's_startdate') {
        
View::newInstance()->_exportVariableToView('search_order''s_startdate');
    }
}

osc_add_hook ('before_html','cust_events_change_sOrder');
?>


Regarding the main function, seems to be correct. The problem on your side might be that your custom Datepicker field isn't named "startdate":

Quote
if (Params::getParam('startdate')) {

???

Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Sort ads by custom field
« Reply #21 on: December 22, 2016, 10:19:27 pm »
Sorry my late to response... some fix to design tasks on theme, and I forgot the hours.  :D

I'm using datetimepicker, so I needed firstly to backup my code, for not mess up, with remove/add code, like I did before (roll back the code is not good, backup is better).

Don't worries, I know what I did to test only with datepicker, and now I testing only with that without any other commented code to datetimepicker, was removed.

You are right, the name of my fields is s_startdate, so I changed that now on php function.

I use two ID for two fields, one on header to homepage, and another on sidebar. So exists different header on both pages for not repeat content of forms.

The results to s_stardate "asc" working now  :D. The only problem like you said before is, if user click on another sort by, he cannot reverse to date, unless he click on datepicker again.

So I assume not exists solution to do it, to add new type "sort by" on menu.

Unless some trick to do it.

Anyway I'm happy to have this part you done already solved :)

Thanks teseo, nice work!

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Sort ads by custom field
« Reply #22 on: December 23, 2016, 12:03:01 am »
to add new type "sort by" on menu.

search.php, find this line or similar using same helper:

Code: [Select]
$orders = osc_list_orders();
Insert below:

Code: [Select]
$orders[__('Event start date')] = array('sOrder' => 's_startdate', 'iOrderType' => 'asc');
Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
[solved] Sort ads by custom field
« Reply #23 on: December 23, 2016, 12:11:41 am »
Perfect!
Now all solved, thanks teseo, all good to you  :)

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Sort ads by custom field
« Reply #24 on: December 23, 2016, 12:55:27 pm »
You're welcome :)

Regards