class FlagNonGlobalCookieStorage in Flag 6.2
Same name and namespace in other branches
- 7.3 includes/flag.cookie_storage.inc \FlagNonGlobalCookieStorage
- 7.2 flag.inc \FlagNonGlobalCookieStorage
Storage handler for non-global flags.
Hierarchy
- class \FlagCookieStorage
- class \FlagNonGlobalCookieStorage
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FlagCookieStorage:: |
static | function | Returns the actual storage object compatible with the flag. | |
FlagNonGlobalCookieStorage:: |
protected | function | ||
FlagNonGlobalCookieStorage:: |
static | function |
Deletes the cookie. Overrides FlagCookieStorage:: |
|
FlagNonGlobalCookieStorage:: |
function |
"Flags" an item. Overrides FlagCookieStorage:: |
||
FlagNonGlobalCookieStorage:: |
protected | function | ||
FlagNonGlobalCookieStorage:: |
protected | function | ||
FlagNonGlobalCookieStorage:: |
protected | function | ||
FlagNonGlobalCookieStorage:: |
function |
"Unflags" an item. Overrides FlagCookieStorage:: |
||
FlagNonGlobalCookieStorage:: |
protected | function | ||
FlagNonGlobalCookieStorage:: |
function |
Overrides FlagCookieStorage:: |