View source
<?php
abstract class Notifications_Entity {
protected $type;
public function __construct($template = NULL) {
if ($template) {
$properties = (array) $template;
foreach ($properties as $key => $value) {
$this->{$key} = $value;
}
}
}
public abstract function get_title();
public abstract function get_name();
public static function build_object($object) {
return new Notifications_Entity($object);
}
public abstract function user_access($account, $op = 'view');
public static function type_info($type = NULL, $property = NULL, $default = NULL) {
}
public function get_info($property = NULL, $default = NULL) {
return $this
->type_info($this->type, $property, $default);
}
}
interface Notifications_ObjectInterface {
public static function object_load($value);
public static function object_name($object);
public static function object_value($object);
}
abstract class Notifications_Object implements Notifications_ObjectInterface {
public $type;
public $value;
public $name;
protected $object;
protected $fields;
public function get_title() {
return $this
->get_property('title', '');
}
public function get_name() {
if (!empty($this->name)) {
return $this->name;
}
elseif ($this
->get_object()) {
return $this
->object_name($this
->get_object());
}
else {
return '';
}
}
public function get_token_type() {
return $this->type;
}
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;
}
public function set_value($value) {
if (is_object($value)) {
$this
->set_object($value);
}
else {
$this->value = $value;
$this->object = NULL;
}
return $this;
}
public function get_value() {
return isset($this->value) ? $this->value : NULL;
}
public function set_object($object) {
$this->object = $object;
$this->value = $this
->object_value($object);
return $this;
}
public function index() {
return $this->type . ':' . (isset($this->value) ? $this->value : 'empty');
}
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;
}
function user_access($account) {
return (bool) $this
->get_object();
}
function get_fields() {
if (!isset($this->fields)) {
$this->fields = array();
if ($object = $this
->get_object()) {
$fields = module_invoke_all('notifications_object_' . $this->type, 'fields', $object);
foreach ($fields as $field) {
$this->fields[$field
->index()] = $field;
}
}
}
return $this->fields;
}
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;
}
function subscribe_options($account) {
$subscriptions = new Notifications_Subscription_List();
if ($options = $this
->invoke_all('subscriptions', $account)) {
$subscriptions
->add($options);
}
return $subscriptions;
}
public function subscription_types() {
return $this
->invoke_all('subscription types');
}
protected function get_property($name, $default = NULL) {
return $this
->type_info($this->type, $name, $default);
}
public static function type_info($type = NULL, $property = NULL, $default = NULL) {
return notifications_info('object types', $type, $property, $default);
}
protected function invoke_all($op, $param = NULL) {
return module_invoke_all('notifications_object_' . $this->type, $op, $this
->get_object(), $param);
}
public function __sleep() {
return array(
'type',
'value',
'name',
);
}
}
class Notifications_Drupal_Object extends Notifications_Object {
public function get_title() {
return t('Object');
}
public static function object_load($value) {
return NULL;
}
public static function object_name($object) {
return t('unknown');
}
public static function object_value($object) {
return NULL;
}
}
class Notifications_Node extends Notifications_Drupal_Object {
public $type = 'node';
public static function object_load($value) {
return node_load($value);
}
public static function object_name($node) {
return $node->title;
}
public static function object_value($node) {
return $node->nid;
}
function user_access($account) {
if ($node = $this
->get_object()) {
return node_access('view', $node, $account);
}
}
}
class Notifications_User extends Notifications_Drupal_Object {
public $type = 'user';
public static function object_load($value) {
return user_load($value);
}
public static function object_name($object) {
return $object->name;
}
public static function object_value($user) {
return $user->uid;
}
function user_access($account) {
$user = $this
->get_object();
return $user && $user->uid && ($user->uid == $account->uid || user_access('administer users', $account) || $user->access && $user->status && user_access('access user profiles', $account));
}
}