Version: current

     1 <?php
     2 /**
     3  *  Работа с куками
     4  *
     5  * @author menedem
     6  */
     7 class CookieExtention {
     8
     9     /**
    10      * Получение куки по имени
    11      *
    12      * @param (string) $cookieName
    13      * @return (string)
    14      */
    15     static function get_cookie_data($cookieName) {
    16         return base64_decode(Cookie::get($cookieName));
    17     }
    18
    19     /**
    20      * Получение куки по имени
    21      *
    22      * @param (string) $cookieName
    23      * @return (string)
    24      */
    25     static function set_cookie_data($cookieName, $IDs, $expiryDays=180) {
    26         Cookie::set($cookieName, base64_encode($IDs), $expiryDays);
    27     }
    28
    29     /**
    30      * Добаавление к кукам элемента или элементов (в строке через запятую)
    31      *
    32      * @return string
    33      */
    34     static function add_item_to_cookie($cookieName, $IDs, $expiryDays=180) {
    35         $cookies = self::get_cookie_data($cookieName);
    36         $items = implode(',', $IDs);
    37         if ($items) {
    38             if ($cookies) {
    39                 $cookies = $items . ',' . $cookies;
    40                 $cookiesArray = explode(',', $cookies);
    41                 $cookiesArray = array_unique($cookiesArray);
    42                 $cookies = implode(',', $cookiesArray);
    43             } else {
    44                 $cookies = $items;
    45             }
    46         }
    47         Cookie::set($cookieName, base64_encode($cookies), $expiryDays);
    48         return $cookies;
    49     }
    50
    51     /**
    52      * Удаление элемента из избранного (если есть)
    53      *
    54      * @return string
    55      */
    56     static function delete_item_from_cookie($cookieName, $IDs, $expiryDays=180) {
    57         $cookies = self::get_cookie_data($cookieName);
    58         if ($cookies) {
    59             $itemsArray = explode(',', $cookies);
    60             foreach($IDs as $productID) {
    61                 $key = array_search($productID, $itemsArray);
    62                 if ($key !== false) {
    63                     unset($itemsArray[$key]);
    64                 }
    65             }
    66         }
    67         $items = implode(',', $itemsArray);
    68         Cookie::set($cookieName, base64_encode($items), $expiryDays);
    69         return $items;
    70     }
    71
    72     /**
    73      * Очистка кук
    74      *
    75      */
    76     static function clear_cookies($cookieName, $expiryDays=180) {
    77         Cookie::set($cookieName, '', $expiryDays);
    78         return true;
    79     }
    80 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -