Advertisement
Guest User

OtLand PageView

a guest
Jul 14th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2.  
  3.     namespace OtLand;
  4.  
  5.     class PageView {
  6.         protected static $cookie = 'otland_pageview';
  7.         private static $read   = array(),
  8.                        $unread = array();
  9.  
  10.  
  11.         /**
  12.          * Returns the predefined cookie variable.
  13.          *
  14.          * @access protected
  15.          * @static true
  16.          * @throws Exception
  17.          * @return string
  18.         **/
  19.         protected static function getCookie() {
  20.             if (!isset($_COOKIE[self::$cookie])) {
  21.                 if (empty(self::$read)) {
  22.                     self::setCookie(' ', false);
  23.                 } else {
  24.                     self::markAsRead(array());
  25.                 }
  26.             }
  27.  
  28.             return $_COOKIE[self::$cookie];
  29.         }
  30.  
  31.  
  32.         /**
  33.          * Sets the value of a predefined cookie variable.
  34.          *
  35.          * @param  mixed   $value
  36.          * @param  boolean $extend true
  37.          * @access protected
  38.          * @return void
  39.         **/
  40.         protected static function setCookie($value, $extend = true) {
  41.             \setcookie(self::$cookie, ($extend && self::getCookie() != ' ' ? self::getCookie() . PATH_SEPARATOR : null) . $value);
  42.         }
  43.  
  44.  
  45.         /**
  46.          * Returns true in case a post/page id has already been discovered.
  47.          *
  48.          * @param  integer $id
  49.          * @access public
  50.          * @static true
  51.          * @return boolean
  52.         **/
  53.         public static function isRead($id) {
  54.             if (!preg_match('/' . PATH_SEPARATOR . '?' . $id . PATH_SEPARATOR . '?/', self::getCookie())) {
  55.                 return false;
  56.             }
  57.  
  58.             return true;
  59.         }
  60.  
  61.  
  62.         /**
  63.          * Returns true in case a post/page id has yet to be discovered.
  64.          *
  65.          * @param  integer $id
  66.          * @access public
  67.          * @static true
  68.          * @return boolean
  69.         **/
  70.         public static function isUnread($id) {
  71.             return !self::isRead($id);
  72.         }
  73.  
  74.  
  75.         /**
  76.          * Marks a specific post/page id as read.
  77.          *
  78.          * @param  integer $id
  79.          * @access public
  80.          * @static true
  81.          * @return boolean
  82.         **/
  83.         public static function markAsRead($ids) {
  84.             $ids = (array) $ids;
  85.  
  86.             foreach ($ids as $id) {
  87.                 if (self::isRead($id)) {
  88.                     continue;
  89.                 }
  90.  
  91.                 // Workaround for setting cookies once per request.
  92.                 self::$read[] = $id;
  93.             }
  94.  
  95.             if (!empty(self::$read)) {
  96.                 self::setCookie(implode(PATH_SEPARATOR, self::$read));
  97.             }
  98.         }
  99.  
  100.  
  101.         /**
  102.          * Marks a specific post/page id as unread.
  103.          *
  104.          * @param  integer $id
  105.          * @access public
  106.          * @static true
  107.          * @return boolean
  108.         **/
  109.         public static function markAsUnread($ids) {
  110.             $ids = (array) $ids;
  111.  
  112.             foreach ($ids as $id) {
  113.                 if (self::isUnread($id)) {
  114.                     continue;
  115.                 }
  116.  
  117.                 // Workaround for setting cookies once per request.
  118.                 self::$unread[] = $id;
  119.             }
  120.  
  121.             if (!empty(self::$unread)) {
  122.                 $storage = preg_replace('/' . PATH_SEPARATOR . '?(' . implode('|', self::$unread) . ')' . PATH_SEPARATOR . '?/', PATH_SEPARATOR, self::getCookie());
  123.                 $storage = trim($storage, PATH_SEPARATOR);
  124.  
  125.                 self::setCookie($storage, false);
  126.             }
  127.         }
  128.  
  129.  
  130.         /**
  131.          * Returns an array of post/page ids.
  132.          *
  133.          * @param  boolean $sort true
  134.          * @access public
  135.          * @static true
  136.          * @return array
  137.         **/
  138.         public static function getAll($sort = true) {
  139.             $tmp = explode(PATH_SEPARATOR, self::getCookie());
  140.  
  141.             // Remove the empty default value from the array.
  142.             if (array_search(' ', $tmp) !== false) {
  143.                 unset($tmp[array_search(' ', $tmp)]);
  144.             }
  145.  
  146.             // Convert all values into integer values.
  147.             $tmp = (array) array_map('intval', $tmp);
  148.  
  149.             if ($sort) sort($tmp);
  150.  
  151.             return $tmp;
  152.         }
  153.  
  154.  
  155.         /**
  156.          * Resets the storage cookie.
  157.          *
  158.          * @access public
  159.          * @static true
  160.          * @return void
  161.         **/
  162.         public static function reset() {
  163.             setcookie(self::$cookie, '', time() - 3600);
  164.         }
  165.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement