We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49261
    • 18 Posts
    Howdy!

    I love getResources, but I'm having trouble wrapping my head around a particular function.

    I'm trying to only select resources that were published on a month, say July, with the year being a variable. So, if the page title was 2013, I want the resources listed to be from July 2013.

    I'm planning on having a page for a year, like 2012, and have different getResources calls to get all of the resources for that particular year, divided into months.

    I looked at Archivist, but it doesn't allow the flexibility that I'm looking for.

    Can anyone help?

    Thanks!
    David

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      I might be wrong, but I don't think getResources is smart enough to do date comparisons on resource fields. It will do so for sorting by TVs but AFAIK, not for selection. Part of the problem is that the date fields are stored as Unix timestamps.

      This could possibly work:

      This would have to go above the getResources tag:

      [[!SetTime? &month=`January` &year=`2013`]]


      In the getResources tag:

      &where=`[[+CalculatedWhere]]`

      /* SetTime snippet */
      
      $month=$scriptProperties['month'];
      $year=$scriptProperties['year'];
      $a_date = $month . ' ' . $year;
      $min = strtotime($a_date);
      $time =  date("Y-m-t 23:59", strtotime($a_date));
      $max =  strtotime($time);
      
      $where = array(
          'published' => 1,
          'deleted' => 0,
          'publishedon:>=' => $min,
          'publishedon:<=' => $max,
      );
      
      $modx->setPlaceholder('CalculatedWhere', $modx->toJSON($where));
      return '';


      Let me know if it works. wink
        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
        • 49261
        • 18 Posts
        Gah! This helps a super duper ton! Sheesh! I've been trying to figure this one out for soooo long. You should've seen my heart fall over. Ahh, that moment when you hit refresh and all is right with the world. Thank you!

        With the snippet, I actually changed it to return the results:

        /* SetTime snippet */
         
        $month=$scriptProperties['month'];
        $year=$scriptProperties['year'];
        $a_date = $month . ' ' . $year;
        $min = strtotime($a_date);
        $time =  date("Y-m-t 23:59", strtotime($a_date));
        $max =  strtotime($time);
         
        $where = array(
            'published' => 1,
            'deleted' => 0,
            'publishedon:>=' => $min,
            'publishedon:<=' => $max,
        );
         
        return '$modx->toJSON($where)';


        And called the snippet in the getResources &where field, so that I can use this multiple times on the same page:

        [[!getResources?
        &where=`[[!SetTime? &month=`October` &year=`2013`]]`
        &includeTVs=`1`
        &tpl=`latest_sermon_block`
        &parents=`82`
        &depth=`0`
        &limit=`0`]]
          • 3749
          • 24,544 Posts
          TBH, I didn't know if it would work. I'm a little surprised that it works with the SetTime snippet called uncached. That usually delays processing, so I wouldn't expect the results to be available when the getResources tag is parsed.

          If you run into trouble using it this way with multiple getResources calls on the same page, you can always send a unique placeholder name as another property (&ph) in each call and have SetTime set that specific placeholder.
            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
            • 49261
            • 18 Posts
            This is an awesome snippet. Quick question though: how could I use a TV date instead of the publishedon date?

            I tried this, but everyone laughed at me =D

            <?php
            /* SetTime snippet */
             
            $month=$scriptProperties['month'];
            $year=$scriptProperties['year'];
            $a_date = $month . ' ' . $year;
            $min = strtotime($a_date);
            $time =  date("Y-m-t 23:59", strtotime($a_date));
            $max =  strtotime($time);
             
            $where = array(
                'published' => 1,
                'deleted' => 0,
                'getTVValue('sermon_date'):>=' => $min,
                'getTVValue('sermon_date'):<=' => $max,
            );
             
            $modx->setPlaceholder('CalculatedWhere', $modx->toJSON($where));
            return $modx->toJSON($where);
              • 3749
              • 24,544 Posts
              This is actually quite complicated because the TV values are not in the table containing the resource objects. getTVValue() won't do any good because it will get the TVValue for the current resource rather than the ones you want to select. I think you might be able to do it with the tvFilters property. You'd modify the snippet to set placeholders for the min and max values and leave them out of the $where array.

              $modx->setPlaceholder('min', $min);
              $modx->setPlaceholder('max', $max);
              $where = array(
                  'published' => 1,
                  'deleted' => 0,
              );
              


              Then in the getResource tag:

              &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0`



              TV searches tend to be very slow. If the user is inputting the date, it won't be very fast. If you're just displaying them, though, you might be able to use a cached version, which would be much faster.
                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
                • 49261
                • 18 Posts
                Wow, this is super good stuff. It really opens my eyes to how flexible all of this is.


                So, this is my getResources call:
                [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`July` &year=`[[*pagetitle]]`]]`]]


                And here's what I get on the debug. You can see that the min and max placeholders aren't being filled.
                [limit] => 0
                [offset] => 0
                [tvFilters] => sermon_date>=
                [tvFiltersAndDelimiter] => ,
                [tvFiltersOrDelimiter] => ||



                So how do I call that in advance to I can fill the max and min placeholders for the getResources call? By the way, I have a getResources call for each month:

                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`February` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`March` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`April` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`May` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`June` &year=`[[*pagetitle]]`]]`]]
                     </div>
                     <div class="clearfix grpelem" id="u636-9"><!-- content -->
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`July` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`August` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`September` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`October` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`November` &year=`[[*pagetitle]]`]]`]]
                      [[!getResources? &tpl=`` &tplFirst=`` &debug=`1` &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[!SetTime? &month=`December` &year=`[[*pagetitle]]`]]`]]
                  • 3749
                  • 24,544 Posts
                  The snippet that sets the placeholders has to above the placeholder on the page. You would need to call the snippet above each line and user the placeholder set in the snippet for the &where property:

                  [[!SetTime? &month=`February` &year=`[[*pagetitle]]`]]
                  [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[+CalculatedWhere]]`]]
                  [[!SetTime? &month=`March` &year=`[[*pagetitle]]`]]
                  [[!getResources? &tvFilters=`sermon_date>=[[+min],sermon_date<=[[+max]]` &processTVs=`0` &where=`[[+CalculatedWhere]]`]]
                  


                  I'm not sure it would work, and it may be difficult to know for sure whether the correct min and max values are being used by getResources.

                  If it does work, you could leave out the year property and replace the $year line in the snippet with this:

                  $year = (int) $modx->resource->get('pagetitle');


                  FYI, if this all works, the page will be *very* slow to load every time because every getResources call must be called uncached. You could speed it up significantly by using the publishedon or createdon date field rather than the TV, but you could speed it up a lot more by doing that *and* using a custom snippet to do the whole thing without getResources, which is probably major overkill for what you need.
                    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
                    • 3749
                    • 24,544 Posts
                    Just for the heck of it, try this:

                    [[GetSermons? $tpl=`sermonTpl`]


                    sermonTpl chunk (you can modify this after it's working):

                    <li>[[+pagetitle] - [[+publishedon]]</li>




                    /* GetSermons snippet */
                    
                    $tpl = $modx->getOption('sermonTpl', $scriptProperties);
                    $year = (int) $modx->resource->get('pagetitle');
                    $months = array(
                        'January',
                        'February',
                        'March',
                        /* etc. */
                    );
                    $output = '<h2>Sermons/h2>';
                    foreach ($months as $month) {
                    
                        $a_date = $month . ' ' . $year;
                        $min = strtotime($a_date);
                        $time = date("Y-m-t 23:59", strtotime($a_date));
                        $max = strtotime($time);
                    
                        $where = array(
                            'published'      => 1,
                            'deleted'        => 0,
                            'publishedon:>=' => $min,
                            'publishedon:<=' => $max,
                        );
                    
                    
                        $c = $modx->newQuery('modResource');
                        $c->sortby('publishedon', 'ASC');
                        $c->where($where);
                        $sermons = $modx->getCollection('modResource', $c);
                    
                        $output .= "\n<h3>" . $month . "</h3>\n<ul>";
                        foreach($sermons as $sermon) {
                            $fields = $sermon->toArray();
                            $output .= $modx->getChunk('sermonTpl', $fields);
                        }
                        $output .= "\n</ul>";
                    
                    }
                    
                    return $output;
                    


                    This will be infinitely faster (and completely cacheable), and you can modify the publishedon dates to reflect the date the sermon was given if they're not set that way already (this would be fairly easy to do with NewsPublisher).
                      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
                      • 49261
                      • 18 Posts
                      So, I'm scrolling through my email, and I was like, "What? This guy wrote a custom snippet for me?" Thanks a million.

                      So, where should I put my resources for them to get picked up by the snippet? Here's what I'm seeing:

                      <h2>Sermons</h2>
                      <h3>January</h3>
                      <ul>
                      </ul>
                      <h3>February</h3>
                      <ul>
                      </ul>
                      <h3>March</h3>
                      <ul>
                      </ul>
                      <h3>April</h3>
                      <ul>
                      </ul>
                      <h3>May</h3>
                      <ul>
                      </ul>
                      <h3>June</h3>
                      <ul>
                      </ul>
                      <h3>July</h3>
                      <ul>
                      </ul>
                      <h3>August</h3>
                      <ul>
                      </ul>
                      <h3>September</h3>
                      <ul>
                      </ul>
                      <h3>October</h3>
                      <ul>
                      </ul>
                      <h3>November</h3>
                      <ul>
                      </ul>
                      <h3>December</h3>
                      <ul>
                      </ul>


                      I made sure to create the chunk with the contents you gave, and named it appropriately, and I have around 30 resources in a folder named sermons (ID=82). What are your thoughts?