We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22446
    • 181 Posts
    Is there a way to run a ditto call that randomly selects a document and displays that document or the items in that document for only one day? I would like to repeat this process every day.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use a wrapper snippet to do the timing calculations, the use $modx->runSnippet(’Ditto’, array(’param1’=>’value1’, ’param2’=>’value2’ ... ); when it’s time.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22446
        • 181 Posts
        Thanks for the response. My next question is how do I incorporate my ditto call into the wrapper?

        [[!Ditto? &parents=`28` &sortBy=`createdon` &sortDir=`ASC` &summarize=`5`&total=`5` &filter=`id,123,2` &tpl=`Article_Summary` &extenders=`summary` &truncLen=`500`]]

        $modx->runSnippet(’Ditto’, array(’param1’=>’value1’, ’param2’=>’value2’ ... );

        Lastly I am unsure what param1, param2, value1 and value2 are suppose to be. Is there any documentation to assist me?

        Thanks in advance.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          When "calling" a snippet inside of PHP code, it is best to use the $modx->runSnippet() function as I described below. The snippet parameters are an array, you can either define them in the function parameters as I showed, or define a $params array before making the function call and use $modx->runSnippet(’Ditto’, $params).

          How you calculate when to run it again is another story. I would use a text file to record a timestamp of when it was last run, add 24 hours worth of seconds to it, and if it’s greater than or equal to, run the snippet and update the text file. I would also record the document ID last used, to make sure it doesn’t randomly select the same one as yesterday.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 4172
            • 5,888 Posts
            you can try this snippet or someting like that. It stores the docid and the date in a chunk named daylidoc and returns for each day a new random docid.

            <?php
            //[[daylidoc? &parent=`13`&chunkname=`daylidoc`&excludedocs=`18`]]
            //[[daylidoc? &parent=`13`&chunkname=`daylidoc`&mode=`week`&dayoffset=`5`]]
            
            $chunkname = isset ($chunkname)?$chunkname:'daylidoc';
            $mode = isset ($mode)?$mode:'day';//day,week,month
            $dayoffset = isset ($dayoffset)?$dayoffset:1;
            
            if ( isset ($parent))
            {
                $table = $modx->getFullTablename('site_htmlsnippets');
                $rs = $modx->db->select('*', $table, 'name="'.$chunkname.'"');
                $chunk = $modx->db->getRow($rs);
            
                date_default_timezone_set('Europe/Berlin');
                switch($mode)
                {
                    case 'month':
                        $dateformat = '%m.%Y';
            			$date = @strftime($dateformat);
                        break;
                    case 'week':
                        $timestamp=time();
            			$ts_dow = ($w = date("w", $timestamp))?$w:7;
                        $ts_day = strftime("%d", $timestamp);
                        $ts_month = strftime("%m", $timestamp);
                        $ts_year = strftime("%Y", $timestamp);
                        $timestampweekstart = mktime(0, 0, 0, $ts_month, $ts_day-$ts_dow+$dayoffset, $ts_year);
            			$dateformat = '%d.%m.%Y';
            			$date = 'week starts at '.@strftime($dateformat,$timestampweekstart);
                        break;
                    case 'day':
                    default:
                        $dateformat = '%d.%m.%Y';
            		    $date = @strftime($dateformat);
                }
            	
                $fields['description'] = 'randomID for '.$date;
                $randomDocID = $chunk['snippet'];
            
                if (is_array($chunk)){
                	$excludedocs=isset($excludedocs)?explode(',',$excludedocs):array();
            		$excludedocs[]=$randomDocID;
            		$excludedocs=implode(',',$excludedocs);
                }
               $excludedocs=isset($excludedocs)?' AND id not IN ('.$excludedocs.')':'';
            	
                if (is_array($chunk))
                {
                    if ($fields['description'] !== $chunk['description'])
                    {
                        $table2 = $modx->getFullTablename('site_content');
                        $rs = $modx->db->select('id,rand() as random', $table2, 'parent='.$parent.$excludedocs, 'random', '1');
                        $randomdoc = $modx->db->getRow($rs);
                        if (is_array($randomdoc))
                        {
                            $fields['snippet'] = $randomDocID = $randomdoc['id'];
                            $modx->db->update($fields, $table, 'name="'.$chunkname.'"');
                        }
                      
                    }
            
                }
                else
                {
                    $table2 = $modx->getFullTablename('site_content');
                    $rs = $modx->db->select('id,rand() as random', $table2, 'parent='.$parent.$excludedocs, 'random', '1');
                    $randomdoc = $modx->db->getRow($rs);
                    if (is_array($randomdoc))
                    {
                        $fields['snippet'] = $randomDocID = $randomdoc['id'];
                        $fields['name'] = $chunkname;
                        $modx->db->insert($fields, $table);
                    }
            
                }
            }
            
            
            
            
            return $randomDocID;
            ?>
            


            and use it in the ditto-call like that:

            [!Ditto? &documents=`[[daylidoc? &parent=`13`&chunkname=`daylidoc`&excludedocs=`18`]]` &tpl=`Article_Summary` &extenders=`summary` &truncLen=`500`!]


              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 22446
              • 181 Posts
              Bruno17,

              Thanks for the code. I am unsure if its working correctly. I have a document that I do not want included in the ditto call. Does the filter get included into the snippet or into the ditto call? Currently I have it in the snippet. The page displaying the ditto call currently shows no documents found, which isn’t true because there is a document to display.
                • 4172
                • 5,888 Posts
                have updated the code above.
                For testing change the generated date in description-field of your chunk daylidoc or how you call this chunk in &chunkname.
                needed parameter is &parent
                optional parameters are &chunkname and &excludedocs
                default chunkname is ’daylidoc’

                do not use dittos filter-parameter, use &excludedocs in daylidoc for excluding documents.
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 22446
                  • 181 Posts
                  Bruno17,

                  Thanks for you help and patience with me. I just want to make sure I am working through this correctly.

                  1) Create a snippet called daylidoc and insert the php code you provided.
                  2) Create a chunk called daylidoc (No code entered. The snippet passes the document ID to the chunk.)
                  3) On my homepage run the ditto call.

                  Ditto call:
                  [[!Ditto? &documents=`[[daylidoc? &parent=`28`&chunkname=`daylidoc`&excludedocs=`123`]]` &tpl=`Article_Summary` &extenders=`summary` &truncLen=`500`!]


                  Parent Document: 28
                  Excluded Document: 123
                    • 4172
                    • 5,888 Posts
                    Looks good, so far.

                    if it is for revo use ]] at the end of you snippet-call!
                    Did not test it in revo. There are called some db-api-functions, which should work in revo 2.0.
                    But for later upgrades they will be removed!

                    2) Create a chunk called daylidoc (No code entered. The snippet passes the document ID to the chunk.)

                    the chunk should be created automatically when you call this snippet the first time.
                    maybe you recieve an error, if this chunk is empty or has not a number as content.

                    I would also first test this snippet without ditto!
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 22446
                      • 181 Posts
                      Bruno17,

                      Simply amazing. Code works perfectly. Thanks a bunch for your help.