Forums / Developer / How to get published date of last published article

How to get published date of last published article

Author Message

Pierre SCALFATI

Wednesday 04 March 2009 7:14:25 am

Hello,

I'd like to get the published date of the last article in sub directories. Here's what I want to get:

>>> Press Review 04/02/2009

-> Finance articles
- article 1
- article 2
-> Politics articles
- article 1
- article 2

>>> Photo gallery 02/02/2009

-> Finance photos
- article 1
- article 2
-> Politics photos
- photo 1
- photo2
I would like to have the date of the last article published in front of the main title (in my example: Press Review or Photo Gallery).

I tried using fetch and looping commands to display my data. Everything displays well but when I try to display the last published article date in front of my main titles, I get always the same date for all main titles. Perhaps the date of creation of the folders.

I use {$node.object.published|l10n(shortdate)} to display the date.

Any ideas ?
Thanks.

Jean-Yves Zinsou

Wednesday 04 March 2009 8:35:45 am

Hi there,
what you have to do for each of your main titles is to do a fetch ('content', tree..) sorted by published date, and with limit parameter set to 1:

{def $last=fetch( 'content', 'tree',
        hash( 'parent_node_id', 42,
              'sort_by',        array( 'published', false() ) ) 
'limit',1)}

{$last.published|i10 blablabla}

not tested , but that is the idea...

This the quick and dirty way.

The neat solution is there:

-fetch the sub nodes of parent node sorted by published date
Get the published date of the most recent one,
-display the category and the date of the most recent node
- loop through the subnodes to display their data

HTH

Do Androids Dream of Electric Sheep?
I dream of eZpubliSheep....
------------------------------------------------------------------------
http://www.alma.fr

Pierre SCALFATI

Wednesday 04 March 2009 9:23:54 am

That's what I thought and I tried exactly the same...But the displayed date was 01/01/1970...So I suppose that EZP was not for some reason able to understand or to find a value corresponding to this type of syntax...

My content structure is defined like that :

Press Review (a floder)
Finance Articles (a sub folder)
- article 1
- article 2
Politics Articles (a 2nd sub folder)
- article 1
- article 2

and the same for the Gallery folder.

I think that for some reason it is not able to analyse the sub-folders in order to find the latest article and its date :-(

Is there any function that could do the job, that's to say: to browse the sub folders and get the value of the latest article published date ?

If an article of a sub folder is published, does EZP consider that the date of modification for the folder has to be updated? If so, I could use the modification date of the folder but I don't think that it is the case...

Jean-Yves Zinsou

Wednesday 04 March 2009 11:53:06 am

Why don't you post your code so we can figure out what is wrong ...

Do Androids Dream of Electric Sheep?
I dream of eZpubliSheep....
------------------------------------------------------------------------
http://www.alma.fr

Pierre SCALFATI

Thursday 05 March 2009 12:56:56 am

Sorry, here is my code (Don't be hard with me, I'm a newbie in EZP and it's only two months since I use it). The result displays expert persons like this :

EXPERTS - 01/03/2009 <--- Here we should have the date of publication of last article
>>International Management
- Pierre Dupont - Specialist in International Strategy

>>Management, Law and Human Resources
- Jean NEMAR - specialist in Human Resources Management and Sociology

and here is the code for it (which works correctly except for the date which seems to be that of the creation of folders or web site ):

<div id="last_updates_content">
<div id="expertises">

{*-----------------------------------------------------------------------------
affichage des derniers articles de la partie expertises
---------------------------------------------------------------------------------*}

{def $nodes=fetch( 'content', 'tree', hash( 'parent_node_id',161,'limit',1 ),
'sort_by', array( 'published', true() ) )}
{def $folders = fetch('content', 'list', hash('parent_node_id', 161, 'depth', 1))}


{foreach $nodes as $node }
<h4>{$node.parent.name|wash} - {$node.object.published|l10n(shortdate)}</h4>

{/foreach}
{foreach $folders as $folder}
{def $node_id=$folder.node_id}

<h5>{$folder.name}</h5>

{def $articles = fetch('content', 'list', hash('parent_node_id', $node_id,
'limit',1, 'sort_by', array('published', false() )))
}

{foreach $articles as $article}
<a href="{$article.url}">{$article.name} - {$article.data_map.specialite.content}</a>

{/foreach}
{/foreach}
</div>
etc.....

Jean-Yves Zinsou

Thursday 05 March 2009 1:29:01 am

Bonjour ! ;-)

I carefully watched the below code, i did put my comments inside your code

 


<div id="last_updates_content">
<div id="expertises">

{*------------------------------------------------------------------------------*}
{* affichage des derniers articles de la partie experts *}
{*------------------------------------------------------------------------------*}


{* jyz : fetches all nodes recursively under node 161, return one result, but the OLDEST of any class!*}
{*
{def $nodes=fetch( 'content', 'tree', hash( 'parent_node_id',161,'limit',1 ),
'sort_by', array( 'published', true() ) )}
*}

{* jyz :try and switch to this to get the most recent article :*}

{def $most_recent_article = fetch('content', 'list', hash(
'parent_node_id', 161,
'limit',1,
 'class_filter_type',  'include',
   'class_filter_array', array( 'article),
 'sort_by', array('published', false() )))
}



{def $folders = fetch('content', 'list', hash('parent_node_id', 161, 'depth', 1))}

{* jyz we have only one node here, don't bother to do a for each loop, try this* }
{def $anode=$nodes[0]}
<h4>{$anode.parent.name|wash} - {$most_recent_article.object.published|l10n(shortdate)}</h4>


{*instead of those*}
{*
{foreach $nodes as $node }
<h4>{$node.parent.name|wash} - {$node.object.published|l10n(shortdate)}</h4>
{/foreach}
*}

{foreach $folders as $folder}
{def $node_id=$folder.node_id}

<h5>{$folder.name}</h5>

{def $articles = fetch('content', 'list', hash('parent_node_id', $node_id,
'limit',1, 'sort_by', array('published', false() )))
}

{foreach $articles as $article}
<a href="{$article.url}">{$article.name} - {$article.data_map.specialite.content}</a>

{/foreach}
{/foreach}
</div> 
 

The problem is the you weren't feching the proper nodes...

The code below is not tested, you will have to correct it yourself if needed, but you got the idea...

HTH !

Do Androids Dream of Electric Sheep?
I dream of eZpubliSheep....
------------------------------------------------------------------------
http://www.alma.fr

Pierre SCALFATI

Thursday 05 March 2009 6:06:41 am

Many thanks Jean-Yves,

It gave me some tracks to follow .

- First : I thought as you that when I have one record, it's suffiscent to say :
{def $anode as $nodes[0]} but for some reason, it doesn't work and I had to keep my {foreach} loop.

- Second : I had, anyhow to specify a depth 2 in my fetch function in order to reach the sub folders and I had also to declare the fetch on my class named 'expert' (instead of 'article', but it's normal you asked me to adapt my code to my specific needs).

Here is the code (if someone has any comments to do concerning this, don't hesitate) :

{*------------------------------------------------------------------------------
display my last expert articles
------------------------------------------------------------------------------ *}

{def $nodes = fetch('content', 'list', hash(
'parent_node_id', 161,
'depth',2,
'limit',1,
'class_filter_type', 'include',
'class_filter_array', array( 'expert'),
'sort_by', array('published', false() )
)
)
}
{foreach $nodes as $node }
{* parent.parent.name because I want no the name of the sub folder but the name of the root folder *}
<h4>{$node.parent.parent.name|wash} - {$node.object.published|l10n(shortdate)}</h4>
{/foreach}

{def $folders = fetch('content', 'list', hash('parent_node_id', 161, 'depth', 1))}

{* parcours des dossiers pour afficher le titre du dossier et le dernier article associƩ *}
{foreach $folders as $folder}
{def $node_id=$folder.node_id}
<h5>{$folder.name}</h5>
{def $articles = fetch('content', 'list', hash('parent_node_id', $node_id,
'limit',1, 'sort_by', array('published', false() )))
}
{foreach $articles as $article}
<a href="{$article.url}">{$article.name} - {$article.data_map.specialite.content}</a>
{/foreach}
{/foreach}

Many thanks to you, Jean-Yves (Merci beaucoup et toutes mes amitiƩs) and if you want to give comments, you're welcome!!!

Jean-Yves Zinsou

Thursday 05 March 2009 6:23:20 am

Hi !

First : I thought as you that when I have one record, it's suffiscent to say :
{def $anode as $nodes[0]} but for some reason, it doesn't work and I had to keep my {foreach} loop.

I/O error says the machine...

You should have read :
{def $anode = $nodes[0]}

and not
{def $anode as $nodes[0]}

= and not AS

;-)

Little note: :
The default behaviour of fetcth ('content', 'tree',...) does not restrict the nodes depth when you don't set the depth parameter.
That means by default the system will go all the way down your tree.

Specifying depth will reduce the explored levels (usefull if you have a deep and wide tree)

Regards

Glad to be of any help ...

Do Androids Dream of Electric Sheep?
I dream of eZpubliSheep....
------------------------------------------------------------------------
http://www.alma.fr

Pierre SCALFATI

Thursday 05 March 2009 8:16:58 am

What is really strange is that it's not the first time I've been using {def} and the fact is that I spent 2 days trying to find a solution for that st...date of publication and that made me really loose my EZP language :-) I laughed when I saw your comments concerning this horrible and really shameful bullshit :-)

Sorry for this stupid mistake and many thanks, I'll adapt my code to make it cleaner.

Best Regards!!!