We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi all!
    Got a bit of a quandary. I'm using Collections, GetPage and GetResources to show a list of stories published within a Collection.
    I want to only show stories from the current year on the page. So where we normally use &limit to limit the number of stories, I want to limit them by the year they were published.

    I just can't work out how to go about it - I assume it's some sort of output filter - but my brain is fried!

    Any help will be much appreciated!

    Thanks
    Andy
      • 13428 ☆ A M B ☆
      • 1,031 Posts
      Create a snippet named inyear with the following code:

      <?php
      $year = $modx->getOption('year');
      
      if ($year) {
          return json_encode(array(
              'publishedon:>=' => strtotime('1-1-'.$year),
              'publishedon:<' => strtotime('1-1-'.($year+1)),
          ));
      } else {
          return '';
      }


      After this you could call getRessources with

      &where=`[[inyear? &year=`2015`]]`
        • 51020
        • 670 Posts
        Quote from: Jako at Jan 14, 2016, 08:45 AM
        Create a snippet named inyear with the following code:

        <!--?php
        $year = $modx--->getOption('year');
        
        if ($year) {
            return json_encode(array(
                'publishedon:>=' => strtotime('1-1-'.$year),
                'publishedon:<' => strtotime('1-1-'.($year+1)),
            ));
        } else {
            return '';
        }


        After this you could call getRessources with

        &where=`[[inyear? &year=`2015`]]`


        Thanks so much for this - the only problem with that though is that I just want to show the latest year - rather than hard code the year '2015' if that makes sense?
          • 51020
          • 670 Posts
          So to clarify - I want to show the latest, but only if there are posts within that year - so if thre's nothing posted in 2016 yet, show 2015.

          Maybe it's not possible!
            • 13428 ☆ A M B ☆
            • 1,031 Posts
            Something like this (untested - please change the inner where parameter to your needs):

            <?php
            $year = $modx->getOption('year', $scriptProperties, 1970);
            $where = json_decode($modx->getOption('where', $scriptProperties,'[]'), true);
            
            do {
                $c = $modx->newQuery('modResource');
                $c->where(array_merge($where, array(
                    'publishedon:>=' => strtotime('1-1-'.$year),
                    'publishedon:<' => strtotime('1-1-'.($year+1)),
                ));
                $year--;
            } while ($year >= 1970 && !$modx->getCount('modResource', $c));
            
            if ($year >= 1970) {
                return json_encode(array(
                    'publishedon:>=' => strtotime('1-1-'.$year),
                    'publishedon:<' => strtotime('1-1-'.($year+1)),
                ));
            } else {
                return '';
            }


            And call it with
            &where=`[[inyear? &year=`2015` &where=`{"parent":1}`]]`
            [ed. note: Jako last edited this post 10 years, 9 months ago.]
              • 3749
              • 24,544 Posts
              @Jako That's a nice solution, but I think the OP just wants the current year and if it comes up empty, the previous year (maybe I'm misunderstanding the goal).

              The only fast way I can think of to do that is a custom snippet called within getPage that uses getCount() to check the current year and then does a query with either that or the previous year and uses a Tpl chunk or two for the output with toArray().

              It would be a little simpler if you wanted both the current year and the previous year. I think a slightly modified version of your solution would work for that if you didn't need headers for the years and just displayed the pub dates. Even easier yet if you could just use the past year from the current date:

              'publishedon:>=' => strtotime('-1 year'),


              You could do it with two separate getResources calls and some output filters, but I think it would be a challenge and somewhat slow since you'd probably want to call it uncached. Also, the output filters might not work when nested like that.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 51020
                • 670 Posts
                Quote from: BobRay at Jan 15, 2016, 12:28 AM
                @Jako That's a nice solution, but I think the OP just wants the current year and if it comes up empty, the previous year (maybe I'm misunderstanding the goal).

                The only fast way I can think of to do that is a custom snippet called within getPage that uses getCount() to check the current year and then does a query with either that or the previous year and uses a Tpl chunk or two for the output with toArray().

                It would be a little simpler if you wanted both the current year and the previous year. I think a slightly modified version of your solution would work for that if you didn't need headers for the years and just displayed the pub dates. Even easier yet if you could just use the past year from the current date:

                'publishedon:>=' => strtotime('-1 year'),


                You could do it with two separate getResources calls and some output filters, but I think it would be a challenge and somewhat slow since you'd probably want to call it uncached. Also, the output filters might not work when nested like that.


                Yes Bob you're correct - I just want the current year, unless it's empty, and then show the previous year. I'm already using GetPage to call in the resources from my collection.
                  • 13428 ☆ A M B ☆
                  • 1,031 Posts
                  My snippet code above does the count check without getPage and goes back to the previous year (repeating that until 1970 if the previous is empty too). After that it outputs the according where clause for the outer getResources or getPage call.

                  $year could be changed that it always is using the current year with
                  $year = $modx->getOption('year', $scriptProperties, strftime('%Y'));

                  The year property could be removed from the inyear snippet call then. It would (at the moment) start with 2016 then, goes back to 2015 etc.
                  &where=`[[inyear? &where=`{"parent":1}`]]`
                    • 3749
                    • 24,544 Posts
                    @Jako -- Sorry, I misunderstood your code.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting