We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    @BobRay - I am trying out the snippet you posted and I have a few questions:

    The 90-day-old pages are available to anyone. So, I really only need to show this when anonymous users visit the page. Instead of the isMember, how would I say "if user anonymous"? I'm guessing this will also affect the hasSessionContext.

    Also, instead of returning a string or sending to a URL, is it possible to call a chunk from within a snippet?
      • 4172
      • 5,888 Posts
      does something like that, what you are after?

      <?php
      
      $validTpl = $modx->getOption('validTpl',$scriptProperties,'');
      $unvalidTpl = $modx->getOption('unvalidTpl',$scriptProperties,'');
      
      $groupdays = array(
      'group_0'=>'0',
      'group_30'=>'30',
      'group_60'=>'60',
      'group_90'=>'90',
      'everybody'=>'120',
      );
       
      $publishedon = strtotime($modx->resource->get('publishedon'));
       
      foreach ($groupdays as $group=>$days){
          if ( ($group == 'everybody' && $publishedon < (time() - ($days * 24 * 60 * 60))) || ($modx->user->isMember($group) && $publishedon < (time() - ($days * 24 * 60 * 60))) ){
              return  $modx->getChunk($validTpl);
          }
      }
       
      return  $modx->getChunk($unvalidTpl);
      
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 1331
        • 129 Posts
        Thanks Bruno, I'll have to give that a try. I did get everything working as expected with Bob's code, though. Got conditional chunks and also pulled out the hasSessionContext because not all of my users will be logged in. Didn't realize that has replaced 'isLoggedIn'.

        I'm still refining things a bit, but these ideas were key in getting this moving.

        I'll keep you all posted with the progress.
          • 3749
          • 24,544 Posts
          With my code, the users shouldn't have to be logged in for it to work. Not-logged-in users should get the "unauthorized" message or get sent to the upgrade page. The hassSessionContext() call is only there to avoid PHP errors and to avoid doing the calculations if the user isn't logged in.

            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
            • 1331
            • 129 Posts
            Here's what I have now and it's working perfectly. Of course, I don't know if there are things that could be done more efficiently. I'm still deciding whether I want to redirect to an unauthorized page vs. using an on-page "error" message.

            The reason I'm using the Unauthorized page now is because I don't want the pages to be indexed by Google with just an up-sell message on it.

            Is there some kind of way to send a code 401 or 403 to the browser WITHOUT redirecting?

            <?php
            /* AgeCheck snippet */
            $currentUser = $modx->user;
            $pageContent = $modx->getChunk('article-contents');
            
            /* get current timestamp */
                $now = time();
            
            /* get the page's birth date as a timestamp */
            $pageBirthday = strtotime($modx->resource->get('publishedon'));
             
                /* Get page age in days */
             
                /* rounds up to nearest day */
                $pageAge = ceil(($now - $pageBirthday) / 86400);
             
                /* rounds down to nearest day */
                // $pageAge = floor(($now - $pageBirthday) / 86400);
             
            /* Check the user's User Group */
                $topLevelGroups = array(
                    'Premium-Users',
                    'Instant-Users',
                    'Administrator',
                );
            
                if ($currentUser->isMember($topLevelGroups)){
                    return $pageContent;
                }
            //IF GROUP 60 AND PAGE IS OLER THAN 60
                if ($currentUser->isMember('60-Day-Users') && ($pageAge >= 60)) {
                    return $pageContent;
                }
              // ANONYMOUS USERS GET ACCESS TO 90 DAYS OR OLDER
             //IF NOT LOGGED IN AND PAGE IS OLDER THAN 90
                if (!$currentUser->hasSessionContext('web') && ($pageAge >= 90)) {
                    return $pageContent;
                }
                
            /* User fails all tests, cannot see the current page */
                     
            //return $modx->getChunk('upsell-message');
             
            // or
                     
            $url = $modx->makeUrl(3);
            return $modx->sendRedirect($url);
              • 3749
              • 24,544 Posts
              The only way I can think of to do it is to set a $_SESSION variable in your snippet and use sendRedirect() to redirect to the current page. The snippet would have to detect and remove the $_SESSION variable and skip the redirection if it's set. I'm not sure if it will work or not.


              sendRedirect() can specify a response code:

              $modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 403 Forbidden'));
              $modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 401 Unauthorized'));
              
                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
                • 1331
                • 129 Posts
                Nah, that didn't work. It returns a 200. Looks like it doesn't do anything if there is no actual redirect taking place. I allowed it to redirect to a separate url and it ends, but it picks up the 302 even though 401 is defined here. So, aside from what I'm doing here that still seems off.

                I'm trying to get more information about modResponse, to see if that might be what I need.
                  • 3749
                  • 24,544 Posts
                  The only other thing I can think of is to tell Google not to index it in robots.txt or put a noindex meta tag on the page -- Google will respect 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