You are here

function privatemsg_filter_form_privatemsg_list_alter in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg_filter/privatemsg_filter.module \privatemsg_filter_form_privatemsg_list_alter()
  2. 6 privatemsg_filter/privatemsg_filter.module \privatemsg_filter_form_privatemsg_list_alter()
  3. 7 privatemsg_filter/privatemsg_filter.module \privatemsg_filter_form_privatemsg_list_alter()

Implements hook_form_FORM_ID_alter() to add a filter widget to the message listing pages.

File

privatemsg_filter/privatemsg_filter.module, line 500
Allows users to tag private messages and to filter based upon those tags.

Code

function privatemsg_filter_form_privatemsg_list_alter(&$form, $form_state) {
  global $user;
  if (privatemsg_user_access('filter private messages') && !empty($form['updated']['list']['#options']) || privatemsg_filter_get_filter($user)) {
    $form += privatemsg_filter_dropdown($form_state, $form['account']['#value']);
  }
  $fields = privatemsg_get_enabled_headers();
  if (privatemsg_user_access('tag private messages') && in_array('tags', $fields) && !empty($form['updated']['list']['#options'])) {

    // Load thread id's of the current list.
    $threads = array_keys($form['updated']['list']['#options']);

    // Fetch all tags of those threads.
    $query = _privatemsg_assemble_query(array(
      'tags',
      'privatemsg_filter',
    ), $user, $threads, 3);

    // Add them to tableselect options.
    foreach ($query
      ->execute() as $tag) {
      $form['updated']['list']['#options'][$tag->thread_id]['tags'][$tag->tag_id] = $tag->tag;
    }

    // Avoid notices for threads without tags.
    foreach ($form['updated']['list']['#options'] as &$thread) {
      if (empty($thread['tags'])) {
        $thread['tags'] = array();
      }
    }
  }
  if (privatemsg_user_access('tag private messages') && !empty($form['updated']['list']['#options'])) {
    $form['updated']['actions']['tag-add'] = array(
      '#type' => 'textfield',
      '#size' => 15,
      '#autocomplete_path' => 'messages/filter/tag-autocomplete',
    );
    $form['updated']['actions']['tag-add-submit'] = array(
      '#type' => 'submit',
      '#value' => t('Apply Tag'),
      '#submit' => array(
        'privatemsg_filter_add_tag_submit',
      ),
      '#ajax' => array(
        'callback' => 'privatemsg_list_js',
        'wrapper' => 'privatemsg-list-form',
        'effect' => 'fade',
      ),
    );
    $tags = privatemsg_filter_get_tags_data($user);
    if (!empty($tags)) {
      $options[0] = t('Remove Tag...');
      foreach ($tags as $tag_id => $tag) {
        $options[$tag_id] = $tag;
      }
      $form['updated']['actions']['tag-remove'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => 0,
        '#ajax' => array(
          'callback' => 'privatemsg_list_js',
          'wrapper' => 'privatemsg-list-form',
          'effect' => 'fade',
        ),
        '#submit' => array(
          'privatemsg_filter_remove_tag_submit',
        ),
        '#executes_submit_callback' => TRUE,
      );
      $form['updated']['actions']['tag-remove-submit'] = array(
        '#type' => 'submit',
        '#value' => t('Remove Tag'),
        '#submit' => array(
          'privatemsg_filter_remove_tag_submit',
        ),
        '#attributes' => array(
          'class' => array(
            'form-item',
          ),
        ),
        '#states' => array(
          'visible' => array(
            // This is never true, button is always hidden when JS is enabled.
            ':input[name=operation]' => array(
              'value' => 'fake',
            ),
          ),
        ),
      );
    }
  }
}