Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
I've only been using Couch for 48 hours, and I have to say I'm impressed - coming from a Wordpress and Concrete5 background, this site is coming together with a small fraction of the effort those two would cost me. Bravo!

I'm trying to build something I can't seem to find in the docs, and I'm wondering if you fine folk can help me figure out how to pull it off. Basically, I want to pull a single folder's pages down, then group them by the year of a custom datetime field. Pretty much a carbon-copy of what's going on here, with different content: http://www.jamesjean.com/work/

I've already explored using the <cms:archives> tag - it doesn't seem like that will work, since it uses the publish date on a page. I'm already using the publish date elsewhere, so the custom datetime field is necessary. The <cms:pages> tag might work, but from what I can tell it would require manually writing a <cms:if> for every single year, and that's much less than optimal. I've even tried defaulting to raw php and writing a PDO query to pull everything myself, but wouldn't you know it - I can't get the folder name outside of a <cms> tag.

Anybody have any ideas on how I can pull down a folder's pages and group them by year?
Hello and welcome, CodeMoose :)

It is quitting time for me here - so I am sorry, cannot try to find a solution right away. Will do so when I come back.

Curiously this is the second post in a single day where the 'publish_date' has not been used to capture the publish date and yet archiving is desired :) (in case you want to, you can find the other post at viewtopic.php?f=2&t=8523)

Couldn't resist asking you the same question - could you please let me know what are you using the publish_date for? It happens to be the most straightforward way of archiving posts. IMHO, whatever is it that the publish_date is getting used for could be given a custom field leaving publish_date for its natural function.

What do you say?
Hey KK, thanks for responding! The tricky thing is, the site is already using the publish_date as intended - I have a blog set up that includes the portfolio pieces based on the date they were systematically posted/published. The custom field represents the date the portfolio piece was physically created, and that's the second thing I want to display in the portfolio section.

To answer your question, in theory we can switch the publish_date to reflect the physical creation date - but then the custom field would become the publish date, and we'd have to figure out how to combine that into the blog archive. That seems like even more of a headache ;)
Hi,
I've come across similar tasks, and just wonder maybr my experience could help.
If I clearly understood, you have a custom field for pages ("custom_date"), like:

Page-1 have "custom_date"=2014
Page-2 have "custom_date"=2013
Page-3 have "custom_date"=2014

So if call <cms:pages custom_field='custom_date==2014'><cms:show k_page_title /></cms:pages> we get:

Page-1 Page-3

But you need Archive feature working and not hardcoded custom_field parameter?
So need to create an array of custom_date values and make an 'cms:if' onditional for each? Like that:
Code: Select all
<cms:php>
$custom_dates = array_unique(array(<cms:pages>"<cms:show custom_date />",</cms:pages>));

// then, for example, tansform this $custom_dates to smth like $custom_dates_array="$value|$value" for Couch <cms:each> tag and set it to Couch variable

global $CTX;
$CTX->set('custom_dates_array', $custom_dates_array, 'global');
</cms:php>

So we have <cms:show custom_dates_array /> giving "2013|2014|" and can easily use smth like that
Code: Select all
 <cms:each 'custom_dates_array' sep="|">
  <cms:show item /> -
  <cms:pages custom_field='custom_date==item'>
   <cms:show k_page_title />
  </cms:pages>
  <br>
</cms:each>


I havent tested this code, but suppose we'll have:
2013 - Page-2
2014 - Page-1 Page-3

With some thinking I suppose you could organize a cycle inside <pages> so basic archive features should work. About folders' names and setting Couch variables in php use $CTX->get structure, it should work I think.
@CodeMoose, thanks for the explanation. You mentioned -
The custom field represents the date the portfolio piece was physically created
So I take, you wish to group pages on the basis of their creation year (as opposed to their year of publication).

As it happens, Couch (internally) records the date a page is created. You can take a look at the database to see it. Do you think we can use that for the grouping or you'd still want to use the custom-field?

Please let me know.
Thanks.
@KK yes, that's correct. The piece's creation year is arbitrary though, outside of the page creation date - my client might be posting stuff next month that was made in 1999. Ideally they should be able to set this date at will without getting their hands in the database ;)

@Musman - thanks for the input, I didn't know there was a <cms:php> tag! It looks like this won't quite do the trick though, since the custom_date is a full date and not just the year. I was able to modify the code to get each piece's year based on the date:

Code: Select all
<cms:php>
    $created_years = array_unique(array(<cms:pages masterpage='portfolio.php' folder=k_page_foldername>date("Y", strtotime("<cms:show item_created />")),</cms:pages>));
   
    print_r($created_years);
   
    global $CTX;
    $CTX->set('created_years', $created_years, 'global');
</cms:php>


But the 'folder' attribute doesn't seem to be correctly filtering by the portfolio folder. Also not quite sure how to use that <cms:each> loop to list pages based on the year - any thoughts?
Hey Gang-

Figure it out! Thanks for the help, you guys were able to fill in the gaps I was missing. To help people that might have a similar problem in the future, here's the working code I ended up with:

Note: 'item_created' is the name of the custom datetime field.

Code: Select all
<cms:php>
    $created_years = array_unique(array(<cms:pages masterpage='portfolio.php' folder=k_page_foldername>date("Y", strtotime("<cms:show item_created />")),</cms:pages>));

    global $CTX;
    $CTX->set('created_years', implode("|", $created_years), 'global');
</cms:php>


Then...

Code: Select all
<cms:each created_years sep="|">
        <p><cms:show item /></p>
        <ul class="cat-sub">
            <cms:pages masterpage='portfolio.php' folder=k_page_foldername custom_field="item_created=<cms:show item />">
            <li><cms:show k_page_title /></li>
            </cms:pages>
        </ul>
</cms:each>


The important thing to note is that the years array needs to be imploded in order to work with the <cms:each> loop. Using a single = in the custom_field attribute in the loop allows you to find dates that contain the year as a substring - and as long as you use double-quotes, you can nest in the <cms:show item /> tag to get the current year in the loop.

This CMS continues to impress - a problem like this would have taken me weeks to learn and flesh out in something the size of Wordpress or Concrete5. Keep doing what you're doing, Couch!
7 posts Page 1 of 1
cron