You are here

public static function secureCookieTreeTwo::set_tree in Secure Cookie Data 7.2

Setter for the class.

Parameters

object|array $data: The data to be stored in the cookie in the form of an object or a keyed array.

Return value

boolen TRUE if the cookie was modified/set, FALSE if not.

2 calls to secureCookieTreeTwo::set_tree()
secureCookieTreeTwo::delete_node in modules/secure_cookie_data_tree_two.class.inc
Removes a certain node from the tree stored as cookie data.
secure_cookie_data_tree_two_put in modules/secure_cookie_data_tree_two.module
Encode the data and set the cookie with the configuration data.

File

modules/secure_cookie_data_tree_two.class.inc, line 29
secure_cookie_data_tree_two.class.inc @author António P. P. Almeida <appa@perusio.net> @date Mon Dec 2 11:59:02 2013

Class

secureCookieTreeTwo
@file secure_cookie_data_tree_two.class.inc @author António P. P. Almeida <appa@perusio.net> @date Mon Dec 2 11:59:02 2013

Code

public static function set_tree($data) {

  // If given an object then set the cookie immediately.
  if (is_object($data)) {

    // Set the cookie.
    return self::set($data);
  }

  // Check if the cookie exists. If so then recover the existing value.
  if (isset($_COOKIE[static::$__cookie_name])) {
    $previous_data = parent::decode($_COOKIE[static::$__cookie_name]);

    // Unset the hmac.
    if (isset($previous_data->hmac)) {
      unset($previous_data->hmac);
    }
  }

  // If there's no previous data, meaning that no cookie exists then set it
  // now.
  if (!isset($previous_data)) {
    $previous_data = new stdClass();
    $previous_data->data = new stdClass();
  }

  // See if we have already set or not the child object. Just in case.
  if (!isset($previous_data->data)) {
    $previous_data->data = new stdClass();
  }

  // Setting the values for the tree node (level 0 and 1).
  if (!isset($previous_data->data->{$data}[$data['label_level0']])) {

    // Go one level down by creating a child element.
    $previous_data->data->{$data}[$data['label_level0']] = new stdClass();
  }

  // Handle the values list. Either is an array or is brand new. If an array
  // check that the value isn't already there.
  if (is_array($previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'])) {

    // We have to copy the array to another variable. Yes, PHP sucks!
    $values = $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'];

    // Check to see if the value is different.
    if (!in_array($data['value'], $values)) {
      $values[] = $data['value'];
      $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'] = $values;
    }
  }
  else {
    $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'] = array(
      $data['value'],
    );
  }

  // Set the cookie.
  self::set($previous_data);
}