Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Count hits on links


Reply

842 Views

Avatar
micahsheets

29 April 2009 at 10:39am (Last edited: 29 April 2009 10:44am), Community Member, 164 Posts

I have a splash page that a client wants to count the hits on each of the three main links. All three links actually take the user to the same page but will give some analytics to the client. The usefulness of this aside I still need to figure out how to do it. Here is my SplashPage.php class:

<?php

class SplashPage extends Page {

   public static $db = array(
      'Contractor' => 'Int',
      'Designer' => 'Int',
      'Owner' => 'Int'
   );
   
   public static $has_one = array(
);
   
    protected $ResetCounts = 0;
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      // If this Boolean is set to true when the page is saved in the CMS then the database is reset for this page.
      $fields->addFieldToTab("Root.Content.Main", new CheckboxField("ResetCounts", "Tick to reset click counters on save."), 'Content');
      return $fields;
   }
   
   function onBeforeWrite() {
      if ($this->ResetCounts > 0) {
         $this->Contractor = 0;
         $this->Designer = 0;
         $this->Owner = 0;
      }
      parent::onBeforeWrite();
   }
   
}

class SplashPage_Controller extends Page_Controller {
   
   function init() {      
      parent::init();
      
      /* Note: I put these here instead of the template files so that my page types that inherit from Page.php and use the same
            template can load thier own stylesheets and only need a Layout file. */
      Requirements::javascript("site/javascript/SplashPage.js"); // MainNav.js is used for dropdown/flyout menus in IE 6.      

      if (isset($this->URLParams['Action'])) {
         if ($this->URLParams['Action'] == "Contractor"){$this->Contractor += 1;}
         else if ($this->URLParams['Action'] == "Designer"){$this->Designer += 1;}
         else if ($this->URLParams['Action'] == "HomeOwner"){$this->HomeOwner += 1;}
Director::redirect('/home');
      }
   }
   
}

?>

The reset checkbox in the CMS works just fine to set the values back to zero in the database however the bit of code that checks the URLParams['Action'] vales does not seem to work at least not for incrementing the database values.