You are here

class Notifications_Subscription_Table in Notifications 7

Loadable collection of subscriptions for tablesel ect

Hierarchy

Expanded class hierarchy of Notifications_Subscription_Table

File

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

View source
class Notifications_Subscription_Table extends Notifications_Subscription_List {
  public $conditions = array();
  public $base_path = 'notifications/subscription';
  public $pager = 50;

  /**
   * Set conditions and remove fields if in the header
   */
  function set_conditions($conditions = array()) {
    $this->conditions = $conditions;
    foreach (array_keys($conditions) as $id) {
      if (isset($this->header[$id])) {
        unset($this->header[$id]);
      }
    }
    return $this;
  }

  /**
   * Produce a select table with all these subscriptions
   */
  function table_select() {
    $options = array();
    foreach ($this
      ->get_subscriptions() as $subscription) {
      $options[$subscription->sid] = $this
        ->subscription_fields($subscription);
    }
    $table = array(
      '#type' => 'tableselect',
      '#header' => $this
        ->get_header(),
      '#options' => $options,
      '#empty' => t('No subscriptions available.'),
    );
    return $table;
  }

  /**
   * Produce simple table with all these subscriptions
   */
  function table_list() {
    $options = array();
    foreach ($this
      ->get_subscriptions() as $subscription) {
      $options[$subscription->sid] = $this
        ->subscription_fields($subscription);
    }
    $table = array(
      '#theme' => 'table',
      '#header' => $this
        ->get_header(),
      '#rows' => $options,
      '#empty' => t('No subscriptions available.'),
    );
    return $table;
  }

  /**
   * Fill in fields for a subscription
   */
  function subscription_fields($subs) {
    $send_methods = messaging_method_info(NULL, 'name');
    $send_intervals = notifications_send_intervals();
    $send_intervals[-1] = t('Scheduled');
    $fields = array();
    foreach (array_keys($this->header) as $index) {
      switch ($index) {
        case 'sid':
          $value = array(
            'data' => array(
              '#type' => 'link',
              '#title' => $subs->sid,
              '#href' => $this->base_path . '/' . $subs->sid,
            ),
          );
          break;
        case 'title':
          $value = $subs
            ->get_title();
          break;
        case 'name':
          $value = $subs
            ->get_name();
          break;
        case 'type':
          $type_list = notifications_subscription_type(NULL, 'title');
          $value = $type_list[$subs->type];
          break;
        case 'status':
          $status_list = Notifications_Subscription::status_list();
          $value = $status_list[$subs->status];
          break;
        case 'created':
          $value = format_date($subs->created, 'short');
          break;
        case 'uid':

          // User
          $value = theme('username', array(
            'account' => $subs
              ->get_user(),
          ));
          break;
        case 'operations':
          $value = $this
            ->field_operations($subs);
          break;
        case 'send_method':
          $value = isset($send_methods[$subs->send_method]) ? $send_methods[$subs->send_method] : $subs->send_method;
          break;
        case 'send_interval':
          $value = isset($send_intervals[$subs->send_interval]) ? $send_intervals[$subs->send_interval] : $subs->send_interval;
          break;
        default:
          $value = isset($subs->{$index}) ? check_plain($subs->{$index}) : '--';
          break;
      }
      $fields[$index] = $value;
    }
    return $fields;
  }

  /**
   * Print username or destination for anonymous users
   *
   * @todo Check the part for
   */
  function field_destination($subs) {
    if ($user = $subs
      ->get_user()) {
      $username = theme('username', array(
        'account' => $user,
      ));
    }
    else {

      // Anonymous subscription, print destination instead
      $dest = $sub
        ->get_destination();
      $username = $dest
        ->address_name() . ' ' . l($dest
        ->format_address(FALSE), 'notifications/destination/' . $sub->mdid . '/manage');
    }
  }

  /**
   * Field operations
   */
  function field_operations($subs) {
    $destination = drupal_get_destination();

    // Build a list of all the accessible operations for the current subscription.
    $operations = array();
    $operations['edit'] = array(
      'title' => t('edit'),
      'href' => $this->base_path . '/' . $subs->sid . '/edit',
      'query' => $destination,
    );
    $operations['delete'] = array(
      'title' => t('delete'),
      'href' => $this->base_path . '/' . $subs->sid . '/delete',
      'query' => $destination,
    );
    if (count($operations) > 1) {

      // Render an unordered list of operations links.
      return array(
        'data' => array(
          '#theme' => 'links',
          '#links' => $operations,
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
        ),
      );
    }
    elseif (!empty($operations)) {

      // Render the first and only operation as a link.
      $link = reset($operations);
      return array(
        'data' => array(
          '#type' => 'link',
          '#title' => $link['title'],
          '#href' => $link['href'],
          '#options' => array(
            'query' => $link['query'],
          ),
        ),
      );
    }
  }

  /**
   * Get query for selection
   */
  function query_select() {

    // Query data with $conditions and filters
    $query = db_select('notifications_subscription', 's')
      ->extend('PagerDefault')
      ->extend('TableSort');
    foreach ($this->conditions as $field => $value) {
      $query
        ->condition('s.' . $field, $value);
    }
    $query
      ->fields('s', array(
      'sid',
    ))
      ->limit(50)
      ->orderByHeader($this->header);
    return $query;
  }

  /**
   * Actually load all the subscriptions
   */
  function query_load() {
    if ($sids = $this
      ->query_select()
      ->execute()
      ->fetchCol()) {
      $subscriptions = entity_load('notifications_subscription', $sids);
      $this
        ->add($subscriptions);
    }
    return $this;
  }

  /**
   * Set fields as header
   */
  function set_header($header = NULL) {
    if (isset($header)) {
      $this->header = $header;
    }
    else {
      $this->header = $this
        ->get_header();
    }
    return $this;
  }

  /**
   * Set default header
   */
  function get_header() {
    return isset($this->header) ? $this->header : array(
      'sid' => array(
        'data' => t('Id'),
        'field' => 's.sid',
      ),
      'type' => array(
        'data' => t('Type'),
        'field' => 's.type',
      ),
      'status' => array(
        'data' => t('Status'),
        'field' => 's.status',
      ),
      'uid' => array(
        'data' => t('User'),
        'field' => 's.uid',
      ),
      'name' => t('Description'),
      'created' => array(
        'data' => t('Created'),
        'field' => 's.created',
        'sort' => 'desc',
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Notifications_Subscription_Table::$base_path public property
Notifications_Subscription_Table::$conditions public property
Notifications_Subscription_Table::$pager public property
Notifications_Subscription_Table::field_destination function Print username or destination for anonymous users
Notifications_Subscription_Table::field_operations function Field operations
Notifications_Subscription_Table::get_header function Set default header
Notifications_Subscription_Table::query_load function Actually load all the subscriptions
Notifications_Subscription_Table::query_select function Get query for selection
Notifications_Subscription_Table::set_conditions function Set conditions and remove fields if in the header
Notifications_Subscription_Table::set_header function Set fields as header
Notifications_Subscription_Table::subscription_fields function Fill in fields for a subscription
Notifications_Subscription_Table::table_list function Produce simple table with all these subscriptions
Notifications_Subscription_Table::table_select function Produce a select table with all these subscriptions