Page 1 of 1
LISE: Sorting a custom_date
Posted: Mon Nov 02, 2020 2:07 pm
by tbremer
Hello,
I want to manage a list of publications in LISE and I want to store the publishing date with my items. I'm using a custom field of type "Select DateTime" for this. The publication-date is in the range from 1977 up to today.
But I noticed that sorting for the custom_date went wrong in 2001:
Code: Select all
01.10.2001 01.02.2002 13.04.2002 01.04.1977 02.04.1977
[...]
01.07.2000 13.03.2001 01.07.2001
LISE will save the date as an UNIX-timestamp and this one flipped in September 2001 up to 1000000000. But it seems that LISE will do an alphanumeric sort here (like in
this Thread) instead of an integer sort. So all after September 2001 will be sort before the year 1977
In the admin-panel of my LISE-instance everything works: When I'm sorting the list of items for my custom_date, then the list is correctly sorted.
Is the alphanumeric-search even in a date-field the normal behavior of LISE or is it a bug in the module?
What can I do to get a correct sorting? I can think of two solutions:
- Load all items and doing my own sorting on this in runtime or
- Saving the date in 3 fields (year, month, day) and using the LISE-sort "orderby=custom_year,custom_month,custom_day" (And telling ALL my editors to enter the numbers with a leading zero... OK, this will not work).
Did anybody have another idea for me?
Thanks.
Re: LISE: Sorting a custom_date
Posted: Mon Nov 02, 2020 3:19 pm
by velden
Probably JoMorg (the LISE dev) will jump in sooner or later

but I think I should note that LISE has a very useful API with which you can use to create a UDT that on the 'before save' event formats data as you like it to be formatted.
E.g. you could save the date in your preferred format to a normal text input fielddef. On saving the item it could automatically be filled/updated based on the chosen date in another fielddef.
If you want you can use module_custom to hide/read-only the specific field, but it's not needed as it will be updated anyway on save.
Also LISE has 3 hidden fields (key1, key2, key3) but I don't know by head if those can be used for the orderby parameter.
Latest LISE version had some important improvents regarding date field definitions btw. Are you using latest version (1.4.x?).
Re: LISE: Sorting a custom_date
Posted: Mon Nov 02, 2020 3:36 pm
by Jo Morg
To clarify: that one post you linked to was about ListIt2 and a field that was not a date field. In that context it was correct. keep in mind that ListIt2 saved the date fields in whatever date local format it was set so the only way the "order by" would work with a date then was if the it was all numeric and as yyyymmdd or something similar.
That was why LISE started to store the date as a UNIX time stamp (although it still has the old compatibility mode if needed).
Having said that, LISE saves all data as strings (at least for now) so sorting does have that inconvenience, it sorts alphanumerically.
The backend sorting as expected my be a side effect of sorting after the date formatting being in effect (not sure though, didn't have time to dig further right now). So for now your point (1) may be your best option if there are not that many items in the list (in the order of the few hundreds at the most) until LISE changes a bit the way it handles the storage. I'm working on that so it may be a change for LISE 2, which I hope is not that far away in time.
Note: velden suggestion is actually a very practical one too...
Re: LISE: Sorting a custom_date
Posted: Mon Nov 09, 2020 1:05 pm
by tbremer
Thanks for the replies. I'm using version 1.4 of LISE with an actual CMSMS.
I think the best option for me is to add an extra field called "datesort" filled with YYYYMMDD (as mentioned by velden) and to use this for the LISE-sorting. With this, I will not break my code and the "LISE-way" too much and I can easily return to use the internal sorting on "date" after an update of LISE in the future.
(OK, the other thing is: After googling "smarty sort array" it seems not to be easy to sort an array with smarty. And I want to learn events in CMSMS...)
Where can I find documentation about the Event Handler-system?
https://docs.cmsmadesimple.org/tags/event-manager is very short
At first, may I ask how to access (read and write) the LISE-fields inside of my UDT? Doing things with the values will be easy after that, but I'm missing a starting-point for the eventhandlers. Is the normal $items-array available on PostItemSave or PreItemSave?
Re: LISE: Sorting a custom_date
Posted: Mon Nov 09, 2020 3:44 pm
by velden
Random example from one of my websites:
Code: Select all
//use for pre-save item event
$item = $params['item_object'];
$item->key1 = (string) $item->company;
$item->key2 = lise_datetime_utils::date_to_unix_ts($item->date);
Note: key1, key2 and key3 are always available and hidden. I just don't know if you can use those for filtering. You should test that.
Re: LISE: Sorting a custom_date
Posted: Mon Nov 09, 2020 5:05 pm
by tbremer
Oh, this is very easy and helps a lot. Now my own modified and working UDT is:
Code: Select all
//use for pre-save item event
$item = $params['item_object'];
$item->datesort = date("Ymd",lise_datetime_utils::date_to_unix_ts($item->date));
I tried to save it into "key1", but I cannot sort for this. (The same as I wanted to sort by "alias" some time ago.) The "datesort" is visible to the editors on the bottom of the entry, but this is OK.
One last question: How can I know about the existence of "lise_datetime_utils" (and perhaps other internal helpful functions)?