You are here

class FlagNonGlobalCookieStorage in Flag 6.2

Same name and namespace in other branches
  1. 7.3 includes/flag.cookie_storage.inc \FlagNonGlobalCookieStorage
  2. 7.2 flag.inc \FlagNonGlobalCookieStorage

Storage handler for non-global flags.

Hierarchy

Expanded class hierarchy of FlagNonGlobalCookieStorage

File

./flag.inc, line 1878
Implements various flags. Uses object oriented style inspired by that of Views 2.

View source
class FlagNonGlobalCookieStorage extends FlagCookieStorage {

  // The anonymous per-user flaggings are stored in a single cookie, so that
  // all of them persist as long as the Drupal cookie lifetime.
  function __construct($flag) {
    parent::__construct($flag);
    $this->flaggings = isset($_COOKIE['flags']) ? explode(' ', $_COOKIE['flags']) : array();
  }
  function flag($content_id) {
    if (!$this
      ->is_flagged($content_id)) {
      $this->flaggings[] = $this
        ->cookie_key($content_id);
      $this
        ->write();
    }
  }
  function unflag($content_id) {
    if (($index = $this
      ->index_of($content_id)) !== FALSE) {
      unset($this->flaggings[$index]);
      $this
        ->write();
    }
  }
  protected function get_lifetime() {
    return min((int) ini_get('session.cookie_lifetime'), (int) ini_get('session.gc_maxlifetime'));
  }
  protected function cookie_key($content_id) {
    return $this->flag->name . '_' . $content_id;
  }
  protected function write() {
    $serialized = implode(' ', array_filter($this->flaggings));
    setcookie('flags', $serialized, time() + $this
      ->get_lifetime(), base_path());
    $_COOKIE['flags'] = $serialized;
  }
  protected function is_flagged($content_id) {
    return $this
      ->index_of($content_id) !== FALSE;
  }
  protected function index_of($content_id) {
    return array_search($this
      ->cookie_key($content_id), $this->flaggings);
  }

  /**
   * Deletes the cookie.
   */
  static function drop() {
    if (isset($_COOKIE['flags'])) {
      setcookie('flags', FALSE, 0, base_path());
      unset($_COOKIE['flags']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagCookieStorage::factory static function Returns the actual storage object compatible with the flag.
FlagNonGlobalCookieStorage::cookie_key protected function
FlagNonGlobalCookieStorage::drop static function Deletes the cookie. Overrides FlagCookieStorage::drop
FlagNonGlobalCookieStorage::flag function "Flags" an item. Overrides FlagCookieStorage::flag
FlagNonGlobalCookieStorage::get_lifetime protected function
FlagNonGlobalCookieStorage::index_of protected function
FlagNonGlobalCookieStorage::is_flagged protected function
FlagNonGlobalCookieStorage::unflag function "Unflags" an item. Overrides FlagCookieStorage::unflag
FlagNonGlobalCookieStorage::write protected function
FlagNonGlobalCookieStorage::__construct function Overrides FlagCookieStorage::__construct