You are here

class Notifications_Subscription_List in Notifications 7

List of subscriptions or subscription types

Hierarchy

Expanded class hierarchy of Notifications_Subscription_List

1 string reference to 'Notifications_Subscription_List'
Notifications_Subscription_List::add in ./notifications.list.inc
Add subscription/s to the list

File

./notifications.list.inc, line 10
Drupal Notifications Framework - Default class file

View source
class Notifications_Subscription_List implements Iterator {

  // The list type will be a unique name identifier, for static caching
  public $type = 'subscriptions';
  public $subscriptions = array();

  // First index will be 1, will save some problems
  protected $index = 0;

  // Template for multiple subscriptions of the same type
  public $template;

  // Header for tableselect
  public $header;

  // Whether instances have been loaded from db
  protected $loaded = FALSE;

  // Array indexed by subscription type
  protected $types = array();

  // Parameters common to all instances
  protected $instance_params = array();

  // Index for Iterator
  private $iterator_index = 0;

  /**
   * Construct with list of subscriptions
   */
  public function __construct($type = 'subscriptions') {
    $this->type = $type;
  }

  /**
   * Add subscription/s to the list
   */
  public function add($items) {
    if (is_array($items)) {
      foreach ($items as $item) {
        $this
          ->add($item);
      }
    }
    elseif (is_object($items)) {
      if (is_a($items, 'Notifications_Subscription_List')) {
        $this
          ->add($items
          ->get_subscriptions());
      }
      elseif (is_a($items, 'Notifications_Subscription')) {
        $this
          ->add_subscription($items);
      }
    }
    return $this;
  }

  /**
   * Get number of subscriptions
   */
  public function count() {
    return count($this->subscriptions);
  }

  /**
   * Load multiple subscriptions into the list
   */
  function load_multiple($sids = array(), $conditions = array()) {
    $this->instance_params = $conditions;
    if ($subs = entity_load('notifications_subscription', $sids, $conditions)) {
      $this
        ->add($subs);
    }
    return $this;
  }

  /**
   * Add single subscription
   */
  function add_subscription($subscription) {
    $this->index++;
    $this->subscriptions[$this->index] = $subscription;
    return $this;
  }

  /**
   * Get array of subscription / subscription types
   */
  function get_subscriptions() {
    return $this->subscriptions;
  }

  /**
   * Get all of the subscriptions that are actual instances
   */
  function get_instances() {
    $this
      ->load_instances();
    $list = array();
    foreach ($this->subscriptions as $index => $subscription) {
      if ($subscription
        ->is_instance()) {
        $list[$index] = $subscription;
      }
      else {
        $list[$index] = $subscription
          ->instance();
      }
    }
    return $list;
  }

  /**
   * Load stored instances for subscriptions in the list
   */
  function load_instances() {
    if (!$this->loaded) {
      foreach ($this->subscriptions as $key => $subscription) {
        if (!$subscription
          ->is_stored() && ($stored = $subscription
          ->get_instance())) {

          // Carry name over to the new one as it may be page specific
          if (!empty($subscription->name)) {
            $stored->name = $subscription->name;
          }
          $this->subscriptions[$key] = $stored;
        }
      }
      $this->loaded = TRUE;
    }
    return $this;
  }

  /**
   * Build instances for all with given parameters
   *
   * @todo Handle multiple instances with the same parameters
   */
  function build_instances($params = array()) {
    $this->instance_params = $params;
    foreach ($this->subscriptions as $index => $subscription) {
      if ($instance = $subscription
        ->get_instance($params)) {
        $this->subscriptions[$index] = $instance;
      }
    }
  }

  /**
   * Set user to all subscriptions
   */
  function set_user($user) {
    $this->instance_params['uid'] = $user->uid;
    foreach ($this
      ->get_subscriptions() as $subscription) {
      $subscription
        ->set_user($user);
    }
    return $this;
  }

  /**
   * Set a property to all of the subscription instances
   */
  function set_all($name, $value) {
    $this->instance_params[$name] = $value;
    foreach ($this
      ->get_subscriptions() as $subscription) {
      $subscription->{$name} = $value;
    }
    return $this;
  }

  /**
   * Get all subscription links
   *
   * @param $type
   *   - If 'subscribe', 'unsubscribe' will return just that type of links
   *   - If 'subscription' will return all subscriptions with either 'subscribe' or 'unsubscribe'
   *   - Type 'grouped' will return two nested lists: subscribe /unsubscribe
   */
  public function get_links($type = 'subscription', $options = array()) {
    if ($type == 'grouped') {
      $items = array();
      $groups = array(
        'subscribe' => t('Subscribe to:'),
        'unsubscribe' => t('Unsubscribe from:'),
      );
      foreach ($groups as $group_type => $title) {
        if ($subscriptions = $this
          ->get_links($group_type)) {
          $items[] = theme('item_list', array(
            'items' => $subscriptions,
            'title' => $title,
          ));
        }
      }
      return $items;
    }
    if ($type == 'subscribe' || $type == 'unsubscribe') {

      // Filter out the ones that don't match
      $subscriptions = $this
        ->get_stored($type == 'unsubscribe');
    }
    else {
      $subscriptions = $this
        ->get_instances();
    }
    $links = array();
    foreach ($subscriptions as $subscription) {
      $links[] = $subscription
        ->get_link($type, $options);
    }
    return $links;
  }

  /**
   * Get only existing subscription instances (stored)
   */
  function get_stored($stored = TRUE) {
    $result = array();
    foreach ($this
      ->get_instances() as $index => $subscription) {
      if ($subscription
        ->is_stored() == $stored) {
        $result[$index] = $subscription;
      }
    }
    return $result;
  }

  /**
   * Check access for current user to manage multiple subscriptions
   */
  public function check_access($account = NULL) {
    $account = $account ? $account : $GLOBALS['user'];
    if (user_access('administer notifications', $account) || user_access('manage all subscriptions', $account)) {
      return TRUE;
    }
    else {

      // Check all subscriptions belong to the user
      foreach ($this
        ->get_subscriptions() as $subscription) {
        if (isset($subscription->uid) && $subscription->uid != $account->uid) {
          return FALSE;
        }
      }
      return TRUE;
    }
  }

  /**
   * Get options form
   */
  public function get_form($type, $form, &$form_state) {
    $form['type'] = array(
      '#type' => 'value',
      '#value' => $type,
    );
    $form['subscription_list'] = array(
      '#type' => 'value',
      '#value' => $this,
    );
    switch ($type) {
      case 'checkboxes':

        // List of subscribe/unsubscribe checkboxes
        $form['operation'] = array(
          '#type' => 'value',
          '#value' => 'update',
        );
        $form['subscriptions'] = $this
          ->element_checkboxes();
        $form['save'] = array(
          '#type' => 'submit',
          '#value' => t('Update'),
        );
        $destination = $_GET['q'];
        break;
      case 'unsubscribe':
      case 'delete':

        // Unsubscribe confirm form
        $form['operation'] = array(
          '#type' => 'value',
          '#value' => 'unsubscribe',
        );
        $form['subscriptions'] = $this
          ->element_items();
        $form = confirm_form($form, t('Are you sure you want to delete these subscriptions?'), variable_get('notifications_frontpage', variable_get('site_frontpage', 'node')), t('This action cannot be undone.'), t('Delete all'), t('Cancel'));
        break;
    }
    return $form;
  }

  /**
   * Build subscription list instance from object or array of subscriptions
   */
  public static function build_list($items) {

    // PHP 5.3.0 only, we may want to construct another class extending this one
    $class = get_called_class();
    if (is_a($items, $class)) {
      return $items;
    }
    else {
      $list = new $class();
      $list
        ->add($items);
      return $list;
    }
  }

  /**
   * Build subscription list from array of sids
   */
  public static function build_sids($sids) {

    // PHP 5.3.0 only, we may want to construct another class extending this one
    $class = get_called_class();
    $list = new $class();
    $list
      ->load_multiple($sids);
    return $list;
  }

  /**
   * Build subscription list instance from form submission
   */
  public static function build_from_submission($form, &$form_state) {

    // The subscription object should be here, it may be a subscription type
    $subscriptions = $form_state['values']['subscription_list'];
    return $subscriptions;
  }

  /**
   * Validate form
   * @todo
   */
  public function form_validate($form, &$form_state) {
  }

  /**
   * Process form submission
   */
  public function form_submit($form, &$form_state) {
    $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
    if ($op == t('Cancel')) {
      $operation = 'cancel';
    }
    else {
      $operation = $form_state['values']['operation'];
    }
    return $this
      ->form_operation($operation, $form, $form_state);
  }

  /**
   * Form operation
   */
  protected function form_operation($operation, $form, &$form_state) {
    switch ($operation) {
      case 'cancel':
        break;
      case 'update':
        $options = $form_state['values']['subscriptions'];
        $subscribe = array_filter($options);
        $created = $deleted = 0;
        foreach ($this
          ->get_subscriptions() as $index => $subscription) {
          $status = in_array($index, $subscribe);
          if (!$subscription
            ->is_stored() && $status) {
            $subscription
              ->save();
            $created++;
          }
          elseif ($subscription
            ->is_stored() && !$status) {
            $subscription
              ->delete();
            $deleted++;
          }
        }
        if ($created || $deleted) {
          drupal_set_message(t('The subscriptions have been updated: created @created, deleted @deleted.', array(
            '@created' => $created,
            '@deleted' => $deleted,
          )));
        }
        break;
      case 'unsubscribe':
      case 'delete':
        $count = 0;
        foreach ($this
          ->get_instances() as $subscription) {
          $subscription
            ->delete();
          $count++;
        }
        $form_state['redirect'] = variable_get('notifications_frontpage', '<front>');
        drupal_set_message(format_plural($count, 'A subscription has been deleted.', '@count subscriptions have been deleted.'));
        break;
    }
  }

  /**
   * Get elements
   */
  public function get_elements($type) {
    switch ($type) {
      case 'items':
        return $this
          ->element_items();
      case 'checkboxes':
      default:
        return $this
          ->element_checkboxes();
    }
  }

  /**
   * Subform with subscription options so it can be reused for a fieldset on a bigger form
   *
   * Was: notifications_object_options_subform($subscriptions, $buttons = TRUE)
   *
   * @param $subscriptions
   *   List of subscription objects
   * @param $buttons
   *   Whether to add buttons to the fieldset
   */
  function options_subform($buttons = TRUE) {
    $form['subscriptions'] = $this
      ->options_fieldset(TRUE);
    $form['subscriptions'] += array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    if ($buttons) {
      $form['subscriptions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
      );

      // If full form, redirect so the full page which may have subscription links is updated
      $form['#redirect'] = $_GET['q'];
    }
    $form['#submit'][] = 'notifications_subscriptions_options_form_submit';
    return $form;
  }

  /**
   * Build fieldset with subscription options
   *
   * Was: notifications_object_options_fieldset($subscriptions, $title = FALSE)
   */
  function element_checkboxes($element = array()) {
    $options = $defaults = array();

    // Note: the options array cannot have a 0 key, but we shouldn't have a 0 subscription either
    // http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#checkboxes
    foreach ($this
      ->get_instances() as $index => $subscription) {
      $options[$index] = $subscription
        ->get_name();
      if ($subscription
        ->is_stored()) {
        $defaults[] = $index;
      }
    }
    return $element + array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $defaults,
    );
  }

  /**
   * Build fieldset with subscription names
   */
  function element_items($element = array()) {
    foreach ($this
      ->get_instances() as $index => $subscription) {
      $items[] = $subscription
        ->get_name();
    }
    return $element + array(
      '#title' => t('Subscription list'),
      '#theme' => 'item_list',
      '#items' => $items,
    );
  }

  /**
   * Filter out subscription types for a given option
   */
  public function filter_option($option, $value = TRUE) {
    foreach ($this->subscriptions as $index => $subscription) {
      if (notifications_subscription_type($subscription->type, $option) != $value) {
        unset($this->subscriptions[$index]);
      }
    }
    return $this;
  }

  /**
   * Implementation of iterator interface
   */
  public function rewind() {
    $this->iterator_index = 0;
  }
  public function current() {
    $k = array_keys($this->subscriptions);
    return $this->subscriptions[$k[$this->iterator_index]];
  }
  public function key() {
    $k = array_keys($this->subscriptions);
    return $k[$this->iterator_index];
  }
  public function next() {
    $k = array_keys($this->subscriptions);
    if (isset($k[++$this->iterator_index])) {
      return $this->subscriptions[$k[$this->iterator_index]];
    }
    else {
      return FALSE;
    }
  }
  public function valid() {
    $k = array_keys($this->subscriptions);
    return isset($k[$this->iterator_index]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Notifications_Subscription_List::$header public property
Notifications_Subscription_List::$index protected property
Notifications_Subscription_List::$instance_params protected property
Notifications_Subscription_List::$iterator_index private property
Notifications_Subscription_List::$loaded protected property
Notifications_Subscription_List::$subscriptions public property
Notifications_Subscription_List::$template public property
Notifications_Subscription_List::$type public property
Notifications_Subscription_List::$types protected property
Notifications_Subscription_List::add public function Add subscription/s to the list
Notifications_Subscription_List::add_subscription function Add single subscription
Notifications_Subscription_List::build_from_submission public static function Build subscription list instance from form submission
Notifications_Subscription_List::build_instances function Build instances for all with given parameters
Notifications_Subscription_List::build_list public static function Build subscription list instance from object or array of subscriptions
Notifications_Subscription_List::build_sids public static function Build subscription list from array of sids
Notifications_Subscription_List::check_access public function Check access for current user to manage multiple subscriptions
Notifications_Subscription_List::count public function Get number of subscriptions
Notifications_Subscription_List::current public function
Notifications_Subscription_List::element_checkboxes function Build fieldset with subscription options
Notifications_Subscription_List::element_items function Build fieldset with subscription names
Notifications_Subscription_List::filter_option public function Filter out subscription types for a given option
Notifications_Subscription_List::form_operation protected function Form operation
Notifications_Subscription_List::form_submit public function Process form submission
Notifications_Subscription_List::form_validate public function Validate form @todo
Notifications_Subscription_List::get_elements public function Get elements
Notifications_Subscription_List::get_form public function Get options form
Notifications_Subscription_List::get_instances function Get all of the subscriptions that are actual instances
Notifications_Subscription_List::get_links public function Get all subscription links
Notifications_Subscription_List::get_stored function Get only existing subscription instances (stored)
Notifications_Subscription_List::get_subscriptions function Get array of subscription / subscription types
Notifications_Subscription_List::key public function
Notifications_Subscription_List::load_instances function Load stored instances for subscriptions in the list
Notifications_Subscription_List::load_multiple function Load multiple subscriptions into the list
Notifications_Subscription_List::next public function
Notifications_Subscription_List::options_subform function Subform with subscription options so it can be reused for a fieldset on a bigger form
Notifications_Subscription_List::rewind public function Implementation of iterator interface
Notifications_Subscription_List::set_all function Set a property to all of the subscription instances
Notifications_Subscription_List::set_user function Set user to all subscriptions
Notifications_Subscription_List::valid public function
Notifications_Subscription_List::__construct public function Construct with list of subscriptions