You are here

abstract class Notifications_Object in Notifications 7

Wrapper for Drupal objects

This will be a wrapper for Drupal objects passed around and used as parameters, with some advantages:

  • All the objects have a 'type' property
  • When serialized, the object itself won't be serialized

Hierarchy

Expanded class hierarchy of Notifications_Object

1 string reference to 'Notifications_Object'
Notifications_Message_Template::get_objects in ./notifications.template.inc
Get objects as Drupal objects (Removing Notifications object wrapper)

File

./notifications.object.inc, line 82
Drupal Notifications Framework - Default class file

View source
abstract class Notifications_Object implements Notifications_ObjectInterface {

  // Object type
  public $type;
  public $value;
  public $name;
  protected $object;
  protected $fields;

  /**
   * Get object type title
   */
  public function get_title() {
    return $this
      ->get_property('title', '');
  }

  /**
   * Get object name (user name, node title, etc)
   */
  public function get_name() {
    if (!empty($this->name)) {
      return $this->name;
    }
    elseif ($this
      ->get_object()) {
      return $this
        ->object_name($this
        ->get_object());
    }
    else {
      return '';
    }
  }

  /**
   * Get object token type
   */
  public function get_token_type() {
    return $this->type;
  }

  /**
   * Build from type, value (can be a plain value or a Drupal object)
   */
  public static function build($type, $value = NULL) {
    $class = self::type_info($type, 'class', 'Notifications_Drupal_Object');
    $object = new $class(array(
      'type' => $type,
    ));
    if (isset($value)) {
      $object
        ->set_value($value);
    }
    return $object;
  }

  /**
   * Set value (reset object)
   */
  public function set_value($value) {
    if (is_object($value)) {
      $this
        ->set_object($value);
    }
    else {
      $this->value = $value;
      $this->object = NULL;
    }
    return $this;
  }

  /**
   * Get object value
   */
  public function get_value() {
    return isset($this->value) ? $this->value : NULL;
  }

  /**
   * Set object
   */
  public function set_object($object) {
    $this->object = $object;
    $this->value = $this
      ->object_value($object);
    return $this;
  }

  /**
   * Get object unique key as string. The default will be 'type:value'
   */
  public function index() {
    return $this->type . ':' . (isset($this->value) ? $this->value : 'empty');
  }

  /**
   * Get object value
   */
  public function get_object() {
    if (!isset($this->object)) {
      $object = isset($this->value) ? $this
        ->object_load($this->value) : NULL;
      $this->object = $object ? $object : FALSE;
    }
    return $this->object;
  }

  /**
   * Check user access. By default it will be true if object can be loaded
   */
  function user_access($account) {
    return (bool) $this
      ->get_object();
  }

  /**
   * Get fields for this object type
   */
  function get_fields() {
    if (!isset($this->fields)) {
      $this->fields = array();
      if ($object = $this
        ->get_object()) {

        // As this does an array_merge_recursive() we get grouped field => array(value1, value2..)
        $fields = module_invoke_all('notifications_object_' . $this->type, 'fields', $object);

        // Now we just need to filter out duplicate values
        foreach ($fields as $field) {
          $this->fields[$field
            ->index()] = $field;
        }
      }
    }
    return $this->fields;
  }

  /**
   * Get list of possible and existing subscriptions for user/object
   *
   * @return Notifications_Subscription_List
   */
  function user_subscriptions($account = NULL) {
    $account = $account ? $account : $GLOBALS['user'];
    $subscriptions = $this
      ->subscribe_options($account);
    $subscriptions
      ->build_instances(array(
      'uid' => $account->uid,
    ));
    $subscriptions
      ->set_user($account);
    return $subscriptions;
  }

  /**
   * Get subscription options for object, account. Only enabled subscription types
   *
   * We pass on the user account so we cah check permissions on the fly and save lots of objects
   *
   * @return Notifications_Subscription_List
   */
  function subscribe_options($account) {
    $subscriptions = new Notifications_Subscription_List();
    if ($options = $this
      ->invoke_all('subscriptions', $account)) {
      $subscriptions
        ->add($options);
    }
    return $subscriptions;
  }

  /**
   * Get subscription types for this object
   */
  public function subscription_types() {
    return $this
      ->invoke_all('subscription types');
  }

  /**
   * Get object type property
   */
  protected function get_property($name, $default = NULL) {
    return $this
      ->type_info($this->type, $name, $default);
  }

  /**
   * Get object type information
   */
  public static function type_info($type = NULL, $property = NULL, $default = NULL) {
    return notifications_info('object types', $type, $property, $default);
  }

  /**
   * Run module_invoke_all('notifications_object_[type]') with this (Drupal) object
   */
  protected function invoke_all($op, $param = NULL) {
    return module_invoke_all('notifications_object_' . $this->type, $op, $this
      ->get_object(), $param);
  }

  /**
   * PHP Magic. Regurn object properties to be serialized
   */
  public function __sleep() {
    return array(
      'type',
      'value',
      'name',
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Notifications_Object::$fields protected property
Notifications_Object::$name public property
Notifications_Object::$object protected property
Notifications_Object::$type public property
Notifications_Object::$value public property
Notifications_Object::build public static function Build from type, value (can be a plain value or a Drupal object)
Notifications_Object::get_fields function Get fields for this object type
Notifications_Object::get_name public function Get object name (user name, node title, etc)
Notifications_Object::get_object public function Get object value
Notifications_Object::get_property protected function Get object type property
Notifications_Object::get_title public function Get object type title
Notifications_Object::get_token_type public function Get object token type
Notifications_Object::get_value public function Get object value
Notifications_Object::index public function Get object unique key as string. The default will be 'type:value'
Notifications_Object::invoke_all protected function Run module_invoke_all('notifications_object_[type]') with this (Drupal) object
Notifications_Object::set_object public function Set object
Notifications_Object::set_value public function Set value (reset object)
Notifications_Object::subscribe_options function Get subscription options for object, account. Only enabled subscription types
Notifications_Object::subscription_types public function Get subscription types for this object
Notifications_Object::type_info public static function Get object type information
Notifications_Object::user_access function Check user access. By default it will be true if object can be loaded
Notifications_Object::user_subscriptions function Get list of possible and existing subscriptions for user/object
Notifications_Object::__sleep public function PHP Magic. Regurn object properties to be serialized
Notifications_ObjectInterface::object_load public static function Load related object or data
Notifications_ObjectInterface::object_name public static function Get name for object
Notifications_ObjectInterface::object_value public static function Map object to value (key)