Lise sort elements by year and by date and show year

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
pierrepercee
Forum Members
Forum Members
Posts: 165
Joined: Thu Jan 10, 2013 8:02 am

Lise sort elements by year and by date and show year

Post by pierrepercee »

In Lise I would like to have this presentation:

2023
element
element
element

2020
element
element

2018
element
element
element
element
element
element

I use this code in my summary template:

Code: Select all

{assign var="yet" value=$smarty.now|date_format:"%Y"}
{foreach from=$items item=item}
{if $item@last}
{assign var="mylast" value=$item->fielddefs.datepubli ->value|date_format:"%Y"}
{/if}
{/foreach}
{for $tostart=$yet;$tostart>=$mylast;$tostart--}
	<h4 class="myyear">Year {$tostart}</h4>
<ul>
{foreach from=$items item=item}
{if $item->fielddefs.uploadsbrak!= ''}
{if $item->fielddefs.datepubli ->value|date_format:"%Y"=={$tostart}}
<li>	<strong>Number:&nbsp;</strong>{if $item->fielddefs.mynumb!= ''} {$item->fielddefs.mynumb->value}{/if}</li>
{/if}
{/if}
	{/foreach}
</ul>
{/for}
{/if}
It almost works but when there is no element for a year it still displays the year. If someone has an idea...
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Lise sort elements by year and by date and show year

Post by velden »

In this scenario (and I think it doesn't really sort by the whole date, just the year) you will need to use a little more logic:

Code: Select all

{for $tostart=$yet;$tostart>=$mylast;$tostart--}
  {$foundItem=false}
  {foreach from=$items item=item}
    {if $item->fielddefs.uploadsbrak!= ''}
	  {if !$foundItem}
	    <h4 class="myyear">Year {$tostart}</h4>
		<ul> 
	    {$foundItem=true}	
	  {/if}
      {if $item->fielddefs.datepubli ->value|date_format:"%Y"=={$tostart}}
         <li>	<strong>Number:&nbsp;</strong> {$item->fielddefs.mynumb->value}</li>
      {/if}
    {/if}
  {/foreach}
  {if $foundItem}</ul>{/if}
{/for}
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Lise sort elements by year and by date and show year

Post by velden »

BTW:
You may want to check this LISE help:
(optional) orderby = item_position - You can order by any of the following columns: item_id, item_title, item_position, item_created, category_id, category_name, category_position, category_hierarchy, rand and also by custom fields with custom_* (* would be the field definition alias).
That might save a few loops. Other logic will be required then.
pierrepercee
Forum Members
Forum Members
Posts: 165
Joined: Thu Jan 10, 2013 8:02 am

Re: Lise sort elements by year and by date and show year

Post by pierrepercee »

Thanks Velden,

I already use orderby custom field in my call (datepubli). It's why items are sorted by whole date i think inside each year.
I don't know how to use other logic in this context by decreasing the number of loops. This is important because I will have several thousand items here.
Each item has a title (120 characters max), an alpha numeric reference (10 characters), a publication date and an uploaded file.
Very few fields in fact...
If I can save a little resource with a better thought code, I'm interested :D
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Lise sort elements by year and by date and show year

Post by velden »

If the items are sorted by date (so also by year) this should work I think (untested):

Code: Select all

{$year=''}
{foreach from=$items item=item}
	{if $year != $item->fielddefs.datepubli->value|date_format:"%Y"}
	  {$year == $item->fielddefs.datepubli->value|date_format:"%Y"}
	  {if !$item@first}</ul>{/if}
	  <h4 class="myyear">Year {$year}</h4>
	  <ul> 
	{/if}
	<li><strong>Number:&nbsp;</strong> {$item->fielddefs.mynumb->value}</li>
{/foreach}
</ul>
pierrepercee
Forum Members
Forum Members
Posts: 165
Joined: Thu Jan 10, 2013 8:02 am

Re: Lise sort elements by year and by date and show year

Post by pierrepercee »

Velden,

You are the best developer of all the known and unknown universes, really.
Just a little tipo :

Code: Select all

{$year == $item->fielddefs
== becomes =
It works like a charm !
Do you think with two thousands items it coulds slow down execution on a basic shared hosting ?

Really many thanks !
Post Reply

Return to “Modules/Add-Ons”