abstract class Notifications_Field in Notifications 7
Base class for Notifications fields
Hierarchy
- class \Notifications_Field
Expanded class hierarchy of Notifications_Field
File
- ./
notifications.field.inc, line 10 - Drupal Notifications Framework - Default class file
View source
abstract class Notifications_Field {
// Subscription id
public $sid;
// Field type
public $type;
// Value
public $value;
// Index for subscription
public $position;
// Object name
public $name;
// Object type, generic one
public $object_type = 'object';
// Linked object
protected $object;
// Data type
protected $data_type = 'int';
// Subscription object this field belongs to
protected $subscription;
/**
* Constructor
*/
public function __construct($template = array()) {
foreach ((array) $template as $field => $value) {
$this->{$field} = $value;
}
}
/**
* Quick build
*/
public static function build($type, $value) {
return self::build_type($type, array(
'value' => $value,
));
}
/**
* Build field instance
*/
public static function build_type($type, $template = NULL) {
if ($class = notifications_field_type($type, 'class')) {
return new $class($template);
}
else {
// Unknown field type, let's build something
$field = new Notifications_Field_Default($template);
$field->type = $type;
return $field;
}
}
/**
* Build from db object
*/
public static function build_object($object) {
return self::build_type($object->type, $object);
}
/**
* Get field value name
*/
function get_name() {
if (isset($this->name)) {
return $this->name;
}
elseif ($this
->get_value()) {
return $this->name = $this
->get_object()
->get_name();
}
else {
return t('none');
}
}
/**
* Get field value
*/
function get_value() {
return isset($this->value) ? $this->value : NULL;
}
/**
* Get title for field
*/
public function get_title() {
return $this
->get_property('title', '');
}
/**
* Get description
*/
public abstract function get_description();
/**
* Get link if this field is linked to an object
*/
function get_link($options = array()) {
if (($path = $this
->get_path()) && ($name = $this
->get_name())) {
return l($name, $path, $options);
}
else {
// Fallback if we have no link, will be plaintext name
return check_plain($this
->get_name());
}
}
/**
* Get system path
*/
function get_path() {
return '';
}
/**
* Get related Notifications object
*/
function get_object() {
if (!isset($this->object)) {
$this->object = notifications_object($this->object_type, $this->value);
}
return $this->object;
}
/**
* Get related Drupal object
*/
function drupal_object() {
return $this
->get_object()
->get_object();
}
/**
* Get query condition for current value
*/
function get_query_condition($alias = 'f') {
if (isset($this->value)) {
return $this
->get_value_condition($this->value, $alias);
}
}
/**
* Get value/s from object
*/
function object_value($object) {
if ($object->type == $this->object_type) {
return $object
->get_value();
}
}
/**
* Get query condition for a given value
*
* @param $value
* Single value or array of values for this field
* @param $alias
* Alias of the notifications_subscription_fields table
*/
function get_value_condition($value, $alias = 'f') {
$and = db_and();
$and
->condition($alias . '.type', $this->type);
if (isset($this->position)) {
$and
->condition($alias . '.position', $this->position);
}
if ($this->data_type == 'int') {
$value = is_array($value) ? array_map('intval', $value) : (int) $value;
$and
->condition($alias . '.intval', $value);
}
else {
$and
->condition($alias . '.value', $value);
}
return $and;
}
/**
* Format title and value
*/
function format($format = NOTIFICATIONS_FORMAT_HTML) {
$items = array(
$this
->get_title(),
$this
->format_value($format),
);
return notifications_format_items($items, $format);
}
/**
* Format value
*/
function format_value($format = NOTIFICATIONS_FORMAT_HTML) {
if ($format & NOTIFICATIONS_FORMAT_HTML) {
return $this
->get_path() ? $this
->get_link() : check_plain($this
->get_name());
}
else {
return check_plain($this
->get_name());
}
}
/**
* Check user access
*/
function user_access($account) {
return $this
->get_object()
->user_access($account);
}
/**
* Get unique index for this field
*/
function index() {
return $this->object_type . ':' . $this->type . ':' . (isset($this->value) ? $this->value : '');
}
/**
* Set value for this field, update related properties
*/
function set_value($value, $validate = FALSE) {
if (!$validate || $this
->valid_value($value)) {
$this->value = $value;
$this->name = NULL;
$this->object = NULL;
return TRUE;
}
else {
return FALSE;
}
}
/**
* Build a field object from submitted values
*/
public static function build_from_value($data, $type = NULL, $index = NULL) {
if (is_object($data)) {
if (!$type || $data->type == $type) {
$field = $data;
}
}
elseif (is_array($data)) {
if (isset($data['type']) && (!$type || $type == $data['type'])) {
$field = self::build_type($data['type']);
if (isset($data['value'])) {
$value = $field
->parse_value($data['value']);
$field
->set_value($value, TRUE);
// Set value with previous validation
}
}
}
elseif ($type) {
$field = self::build_type($type);
$value = $field
->parse_value($data);
$field
->set_value($value, TRUE);
// Set value with previous validation
}
if (!empty($field)) {
$field->position = $index;
return $field;
}
}
/**
* Set subscription
*/
function set_subscription($subscription) {
$this->subscription = $subscription;
$this->sid = !empty($subscription->sid) ? $subscription->sid : NULL;
}
/**
* Check if the field has a valid value or the parameter is a valid value
*/
function check_value() {
return isset($this->value) ? $this
->valid_value($this->value) : FALSE;
}
/**
* Parse value from form submission
*/
function parse_value($value) {
switch ($this->data_type) {
case 'int':
return (int) $value;
case 'float':
return (double) $value;
case 'string':
return (string) $value;
default:
return $value;
}
}
/**
* Check if this is a valid value for this field/**
*/
function valid_value($value = NULL) {
return $this
->validate_value($value, $this->data_type);
}
/**
* Save to db
*/
function save() {
// We may have a new sid for this subscription object
if (!empty($this->subscription)) {
$this->sid = $this->subscription->sid;
}
// The int value must be set for drupal_write_record
$this->intval = (int) $this
->get_value();
return drupal_write_record('notifications_subscription_fields', $this);
}
/**
* Load multiple fields
*/
public static function load_multiple($conditions) {
$fields = array();
$query = db_select('notifications_subscription_fields', 'f')
->fields('f');
foreach ($conditions as $key => $value) {
$query
->condition($key, $value);
}
foreach ($query
->execute()
->fetchAll() as $field) {
$fields[] = self::build_object($field);
}
return $fields;
}
/**
* Check if the value is valid for this field has a valid value
*
* Was: notifications_field_valid_value($value, $type = NULL)
*/
static function validate_value($value, $data_type = NULL) {
// A numeric value of zero is possible too, that's why the is_numeric()
if (!is_numeric($value) && empty($value)) {
// The field has no value at all, no go
return FALSE;
}
elseif ($data_type) {
// We want aditional field type validation
switch ($data_type) {
case 'int':
// @todo Better integer validation, is_int not working for strings
return is_numeric($value);
case 'float':
return is_numeric($value);
case 'string':
default:
return is_string($value);
}
}
else {
return TRUE;
}
}
/**
* Build a form element to edit this field
*
* Was: notifications_field_form_element($type, $value, $subscription = NULL, $title = FALSE, $required = FALSE, $size = 40)
*/
function element_edit($element = array()) {
$element += array(
'#title' => $this
->get_title(),
'#type' => 'textfield',
'#default_value' => $this
->get_value(),
'#required' => TRUE,
'#size' => 40,
);
return $element;
}
/**
* Build a form element to display this field
*/
function element_info($element = array()) {
$element += array(
'#type' => 'item',
'#title' => $this
->get_title(),
'#markup' => $this
->get_link(),
);
return $element;
}
/**
* Get field type property
*/
protected function get_property($name, $default = NULL) {
return $this
->type_info($this->type, $name, $default);
}
/**
* Get field type information
*/
public static function type_info($type = NULL, $property = NULL, $default = NULL) {
return notifications_info('field types', $type, $property, $default);
}
/**
* PHP Magic. Regurn object properties to be serialized
*/
public function __sleep() {
return array(
'type',
'value',
'position',
'name',
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Notifications_Field:: |
protected | property | 1 | |
Notifications_Field:: |
public | property | ||
Notifications_Field:: |
protected | property | ||
Notifications_Field:: |
public | property | 6 | |
Notifications_Field:: |
public | property | ||
Notifications_Field:: |
public | property | ||
Notifications_Field:: |
protected | property | ||
Notifications_Field:: |
public | property | 6 | |
Notifications_Field:: |
public | property | ||
Notifications_Field:: |
public static | function | Quick build | |
Notifications_Field:: |
public static | function | Build a field object from submitted values | |
Notifications_Field:: |
public static | function | Build from db object | |
Notifications_Field:: |
public static | function | Build field instance | |
Notifications_Field:: |
function | Check if the field has a valid value or the parameter is a valid value | ||
Notifications_Field:: |
function | Get related Drupal object | ||
Notifications_Field:: |
function | Build a form element to edit this field | 2 | |
Notifications_Field:: |
function | Build a form element to display this field | ||
Notifications_Field:: |
function | Format title and value | ||
Notifications_Field:: |
function | Format value | ||
Notifications_Field:: |
abstract public | function | Get description | 6 |
Notifications_Field:: |
function | Get link if this field is linked to an object | ||
Notifications_Field:: |
function | Get field value name | 1 | |
Notifications_Field:: |
function | Get related Notifications object | 1 | |
Notifications_Field:: |
function | Get system path | 3 | |
Notifications_Field:: |
protected | function | Get field type property | |
Notifications_Field:: |
function | Get query condition for current value | ||
Notifications_Field:: |
public | function | Get title for field | 6 |
Notifications_Field:: |
function | Get field value | ||
Notifications_Field:: |
function | Get query condition for a given value | ||
Notifications_Field:: |
function | Get unique index for this field | ||
Notifications_Field:: |
public static | function | Load multiple fields | |
Notifications_Field:: |
function | Get value/s from object | 3 | |
Notifications_Field:: |
function | Parse value from form submission | 1 | |
Notifications_Field:: |
function | Save to db | ||
Notifications_Field:: |
function | Set subscription | ||
Notifications_Field:: |
function | Set value for this field, update related properties | ||
Notifications_Field:: |
public static | function | Get field type information | |
Notifications_Field:: |
function | Check user access | ||
Notifications_Field:: |
static | function | Check if the value is valid for this field has a valid value | |
Notifications_Field:: |
function | Check if this is a valid value for this field | 1 | |
Notifications_Field:: |
public | function | Constructor | |
Notifications_Field:: |
public | function | PHP Magic. Regurn object properties to be serialized |