You are here

abstract class Notifications_Field in Notifications 7

Base class for Notifications fields

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
Notifications_Field::$data_type protected property 1
Notifications_Field::$name public property
Notifications_Field::$object protected property
Notifications_Field::$object_type public property 6
Notifications_Field::$position public property
Notifications_Field::$sid public property
Notifications_Field::$subscription protected property
Notifications_Field::$type public property 6
Notifications_Field::$value public property
Notifications_Field::build public static function Quick build
Notifications_Field::build_from_value public static function Build a field object from submitted values
Notifications_Field::build_object public static function Build from db object
Notifications_Field::build_type public static function Build field instance
Notifications_Field::check_value function Check if the field has a valid value or the parameter is a valid value
Notifications_Field::drupal_object function Get related Drupal object
Notifications_Field::element_edit function Build a form element to edit this field 2
Notifications_Field::element_info function Build a form element to display this field
Notifications_Field::format function Format title and value
Notifications_Field::format_value function Format value
Notifications_Field::get_description abstract public function Get description 6
Notifications_Field::get_link function Get link if this field is linked to an object
Notifications_Field::get_name function Get field value name 1
Notifications_Field::get_object function Get related Notifications object 1
Notifications_Field::get_path function Get system path 3
Notifications_Field::get_property protected function Get field type property
Notifications_Field::get_query_condition function Get query condition for current value
Notifications_Field::get_title public function Get title for field 6
Notifications_Field::get_value function Get field value
Notifications_Field::get_value_condition function Get query condition for a given value
Notifications_Field::index function Get unique index for this field
Notifications_Field::load_multiple public static function Load multiple fields
Notifications_Field::object_value function Get value/s from object 3
Notifications_Field::parse_value function Parse value from form submission 1
Notifications_Field::save function Save to db
Notifications_Field::set_subscription function Set subscription
Notifications_Field::set_value function Set value for this field, update related properties
Notifications_Field::type_info public static function Get field type information
Notifications_Field::user_access function Check user access
Notifications_Field::validate_value static function Check if the value is valid for this field has a valid value
Notifications_Field::valid_value function Check if this is a valid value for this field 1
Notifications_Field::__construct public function Constructor
Notifications_Field::__sleep public function PHP Magic. Regurn object properties to be serialized