We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47562
    • 113 Posts
    I have the following migx tables/fields for capturing news (from publications/blogs) pertaining to the client:

    news_source (migxdb)
      id
      name
      image
      url
      article_count
    


    news_article (migx)
      id
      title
      source_id
      pub_date
      url


    All is good. But now, in trying to present the information, I'm looking to have a timeline and present the article list partitioned by year. So my questions:

    (1) User doesn't see article_count; I just want to increment it when a new news_article is entered. I think I need to use a OnDocFormSave in a plugin, yes? Something like:

        switch ($modx->event->name) {
            case 'OnDocFormSave':
                $count = $resource->getTVValue('article_count');
                if (!$resource->setTVValue('article_count', $count+1)) {
                    $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem setting TV article_count.');
                }
                break;
        }


    (2) In my getImageList, can I do something like
    &where=`{"[[+pub_date:date=`%Y`]]:=":"2014"}


    to partition these by year? And can I just pass the year that I want (here 2014) in via a param?

    Thanks!

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

    • discuss.answer
      • 4172
      • 5,888 Posts
      you can try this:

      <?php
      
      switch ($modx->event->name) {
          case 'OnDocFormSave':
              $items = $modx->fromJson($resource->getTVValue('news_article'));
              $articles = array();
              if (is_array($items)){
                  $count = count($items);
                  foreach($items as $item){
                      $date = $modx->getOption('pub_date',$item,'');
                      $item['year'] = strftime('%Y',strtotime($date));
                      $articles[] = $item;
                  }
                  $articles = $modx->toJson($articles);    
              }
              else{
                  $count = 0;
                  $articles = '';
              }
              if (!$resource->setTVValue('article_count', $count)) {
                  $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem setting TV article_count.');
              }
              if (!$resource->setTVValue('news_article', $articles)) {
                  $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem setting TV news_article.');
              }        
              break;
      }
      


      this should count the items and add a new field with the year to each item

      than (I hope so), you can filter by:

      &where=`{"year:=":"2014"}
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 47562
        • 113 Posts
        Thanks Bruno! This is awesome! smiley