We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I'm not familiar with CookieList, but this might be what you want (untested):


    if($addValue || $removeValue) {
        $redirectId = $modx->getOption('redirectTo', $scriptProperties, false);
        $error = (isset($c['cl_check'])) ? false : true;
        $url = $cookielist->url($error);
        if ($redirectTo && (!error)) {
            $url = $modx->makeUrl($redirectTo);
        }
    
        // Creates/updates the cookie and its value
        $value = implode(',', $cookieValues);
        $duration = time() + $modx->getOption('cookielist.cookie.duration',null,2592000);
        setcookie($cookieName, $value, $duration, '', false, false);
        $modx->sendRedirect($url);
    }
      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
      • 43957
      • 79 Posts
      Hi Bob,

      Thanks for your code, I gave it a try but I'm afraid it doesn't work out the box.

      I have done a little more digging and found that cookielist has 4 other snippets:

      addToCookieList.snippet.php
      getCookieList.snippet.php
      getCookieList.properties.php
      addToCookieList.properties.php

      in addToCookieList.properties.php there's a list of properties:

      <?php
      return array(
          'addText' => '',
          'removeText' => '',
          'value' => '',
          'tpl' => 'cl.addToCookieList',
      );


      So I think I need to enter my new RedirectTo property in here. I had more of think.

      CookieList works by appending a URL parameter ?cl_add=id to the page URL you want to add to the list. When clicked it loads the link and I guess it fires the plugin to grab the URL parameter to process.

      I'm calling CookieList a number of times on the page, so I think I need the plugin to discriminate between just "addtocookielist" and my "addtocookielist then redirect"

      So I think I need to do some tweaking in the addToCookieList.properties.php

      <?php
      /**
       * @var modX $modx
       * @var array $scriptProperties
       * @var CookieList $cookielist
       */
      
      /* Load the cookielist class */
      $corePath = $modx->getOption('cookielist.core_path',null,$modx->getOption('core_path').'components/cookielist/');
      $cookielist = $modx->getService('cookielist','CookieList',$corePath.'model/');
      
      /* Set up the properties to be used */
      $value = $modx->getOption('value',$scriptProperties);
      $tpl = $modx->getOption('tpl',$scriptProperties,'cl.addToCookieList');
      $addText = $modx->getOption('addText',$scriptProperties);
      $removeText = $modx->getOption('removeText',$scriptProperties);
      if (empty($value)) $value = $modx->resource->id;
      if (empty($addText)) $addText = $modx->lexicon('cookielist.add_text');
      if (empty($removeText)) $removeText = $modx->lexicon('cookielist.remove_text');
      $cookie = $cookielist->cookiename;
      
      $c = $_COOKIE[$cookie];
      
      $cookieValues = explode(',', $c['items']);
      if(in_array($value, $cookieValues)) {
          // We already have this item in the wishlist
          $label = $removeText;
          $params = array(
              CookieList::removeParam => $value,
          );
      } else {
          // Item is not yet in the list
          $label = $addText;
          $params = array(
              CookieList::addParam => $value,
          );
      }
      
      $split = '?';
      if(strstr($_SERVER['REQUEST_URI'], '?')) {
          $split = '&';
      }
      $link = $_SERVER['REQUEST_URI'].$split.http_build_query($params);
      return $cookielist->getChunk($tpl,array('link' => $link, 'value' => $value, 'label' => $label));


      and in the plugin:

      /* Get the CookieList class */
      $corePath = $modx->getOption('cookielist.core_path',null,$modx->getOption('core_path').'components/cookielist/');
      $cookielist = $modx->getService('cookielist','CookieList',$corePath.'model/');
      
      $cookie = $cookielist->cookiename;
      $c = $_COOKIE[$cookie];
      
      /**
      * Sets a cookie to test cookie support
      */
      if(!$c['cl_check']) {
          setcookie($cookie."[cl_check]", 1, 0, '', false, false);
      }
      if ($_GET['cl_error']) $modx->setPlaceholder('cookielist.error',$modx->lexicon('cookielist.err.no_cookies'));
      
      /* Set up the add/remove params. Could be configurable in a future release? */
      $addParam = CookieList::addParam;
      $removeParam = CookieList::removeParam;
      $addValue = (isset($_GET[$addParam])) ? $_GET[$addParam] : null;
      $removeValue = (isset($_GET[$removeParam])) ? $_GET[$removeParam] : null;
      
      /* If neither are set we don't have to do anything */
      if(!$addValue && !$removeValue) return '';
      
      $cookieName = $cookie."[items]";
      $cookieValues = array();
      $currentValues = array();
      if($c['items']) $currentValues = explode(',', $c['items']);
      $value = '';
      
      /**
      * Adds an item to the wish list
      */
      if($addValue) {
          $cookieValues = array_merge($currentValues, $cookieValues);
          $cookieValues[] = $addValue;
      }
      /**
      * Removes an item for the wish list
      */
      if($removeValue) {
          if(in_array($removeValue, $currentValues)) {
              // We want to remove the item
              $position = array_search($removeValue, $currentValues);
              unset($currentValues[$position]);
              $cookieValues = array_values($currentValues);
          }
      }
      /**
      * Check for the cookie support, sets the wish list cookie & redirects
      * back to the listing.
      */
      if($addValue || $removeValue) {
          $error = (isset($c['cl_check'])) ? false : true;
          $url = $cookielist->url($error);
          // Creates/updates the cookie and its value
          $value = implode(',', $cookieValues);
          $duration = time() + $modx->getOption('cookielist.cookie.duration',null,2592000);
          setcookie($cookieName, $value, $duration, '', false, false);
          $modx->sendRedirect($url);
      }
      
      return '';

        • 3749
        • 24,544 Posts
        I'm afraid I don't know enough about CookieList to advise you. Keep in mind that you can always write to the error log to debug plugins.

        $msg = 'Some Variable = ' . $someVariable;
        $modx->log(modX::LOG_LEVEL_ERROR, $msg);
        


        Sometimes it's handy to just put various "hello" messages in spots just to see if the code in that section is executing.
          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