You are here

function cmf_filters in Content Management Filter 7

Same name and namespace in other branches
  1. 5 cmf.module \cmf_filters()
  2. 6.2 cmf.module \cmf_filters()
  3. 6 cmf.module \cmf_filters()

List node administration filters that can be applied.

Parameters

$user : (object) if a user profile page then user object (defaults to false).

Return value

array with filter properties. Any property starting with '#' goes directly into the filter form. '#options' is also used in function cmf_filter_form_submit. 'title' is the text that will show on the filter form. 'single_use' (bool) determines if the filter will be disabled if it is already in use. 'whole_value' "before" if the value is complete and shown before "is", "after" if the value is complete and shown after "is", "no" if it is a portion ("contains"), such as a substring. 'disable' is a list of other filters to remove if this one is selected. 'where' sets a simple WHERE clause for the query (with substitution). 'join' sets a simple JOIN clause for the query (with substitution). 'query_build' provides a funtion for more complex query clauses. If validation and/or submission handling is needed, use the #element_validate and #submit elements. Submit handlers must set the return value in $form_state['values'] array.

NOTE: for the 'where' clause some node fields are translated to comment-equivalent field names if appropriate. See function cmf_perform_query for 'comment' and 'both.'

3 calls to cmf_filters()
cmf_build_filter_query in ./cmf.module
Build the variable parts of the query to be performed regarding the filter status.
cmf_filter_form in ./cmf.module
Defines the form for content administration filters.
cmf_filter_form_refine in ./cmf.module
Handle post-validation form submission.

File

./cmf.module, line 107
@brief Content management filter module file

Code

function cmf_filters($user = FALSE) {

  // Make the array static so it is built only once per page load.
  static $filters;
  if (isset($filters)) {
    return $filters;
  }
  $filters = array();

  // Regular filters.
  $filters['status'] = array(
    'title' => t('node status'),
    'single_use' => FALSE,
    'whole_value' => 'before',
    'query_build' => '_cmf_status_query_build',
    '#type' => 'select',
    '#options' => array(
      'status-1' => t('published'),
      'status-0' => t('not published'),
      'promote-1' => t('promoted'),
      'promote-0' => t('not promoted'),
      'sticky-1' => t('sticky'),
      'sticky-0' => t('not sticky'),
    ),
  );
  $filters['type'] = array(
    'title' => t('node type'),
    'single_use' => TRUE,
    'whole_value' => 'before',
    'where' => "n.type = '%s'",
    '#type' => 'select',
    '#options' => node_type_get_names(),
  );

  //-- workflow filter
  if (module_exists('workflow') && function_exists('workflow_load_all')) {
    $filters['workflow_state'] = array(
      'title' => t('workflow state'),
      'single_use' => FALSE,
      'whole_value' => 'before',
      'query_build' => '_cmf_workflow_query_build',
      '#type' => 'select',
      '#options' => cmf_get_workflows(),
    );
  }

  // The taxonomy filter.
  if (module_exists('taxonomy')) {
    $filters['category'] = array(
      'title' => t('category'),
      'single_use' => FALSE,
      'whole_value' => 'before',
      'query_build' => '_cmf_category_query_build',
      '#type' => 'select',
      '#options' => taxonomy_form_all(TRUE),
    );
  }

  // Cmf filters.
  $filters['title'] = array(
    'title' => t('title/subject'),
    'single_use' => FALSE,
    'whole_value' => 'no',
    'where' => "LOWER(n.title) LIKE LOWER('%%%s%%')",
    '#type' => 'textfield',
    '#element_validate' => array(
      '_cmf_contains_validate',
    ),
  );
  $filters['body_contains'] = array(
    'title' => t('body'),
    'single_use' => FALSE,
    'whole_value' => 'no',
    'where' => "LOWER(r.body) LIKE LOWER('%%%s%%')",
    '#type' => 'textfield',
    '#element_validate' => array(
      '_cmf_contains_validate',
    ),
  );
  $filters['created_after'] = array(
    'title' => t('created after'),
    'single_use' => TRUE,
    'whole_value' => 'after',
    'where' => "n.created >= %d",
    '#type' => 'date',
    '#element_validate' => array(
      'date_validate',
      '_cmf_date_validate',
    ),
    '#submit' => array(
      'cmf_date_handler',
    ),
    '#default_value' => array(
      'year' => date('Y'),
      'month' => 1,
      'day' => 1,
    ),
    '#prefix' => '<div class="date-inline">',
    '#suffix' => '</div>',
  );
  $filters['created_before'] = array(
    'title' => t('created before'),
    'single_use' => TRUE,
    'whole_value' => 'after',
    'where' => "n.created <= %d",
    '#type' => 'date',
    '#element_validate' => array(
      'date_validate',
      '_cmf_date_validate',
    ),
    '#submit' => array(
      'cmf_date_handler',
    ),
    '#default_value' => array(
      'year' => date('Y'),
      'month' => 12,
      'day' => 31,
    ),
    '#prefix' => '<div class="date-inline">',
    '#suffix' => '</div>',
  );
  if (module_exists('locale')) {
    $lang_codes = array(
      '' => t('Neutral'),
    ) + locale_language_list('name');
    $filters['language'] = array(
      'title' => t('language'),
      'single_use' => TRUE,
      'whole_value' => 'before',
      'where' => "n.language ='%s'",
      '#type' => 'select',
      '#options' => $lang_codes,
    );
  }

  // Don't show these on a user page.
  if (!_cmf_valid_user($user)) {
    $filters['user'] = array(
      'title' => t('user list'),
      'single_use' => TRUE,
      'whole_value' => 'before',
      'disable' => array(
        'users',
        'role',
        'blocked',
      ),
      'where' => "u.uid = %d",
      '#type' => 'select',
      '#options' => cmf_get_users('names'),
    );
    $filters['users'] = array(
      'title' => t('user name'),
      'single_use' => TRUE,
      'whole_value' => 'before',
      'disable' => array(
        'user',
        'role',
        'blocked',
      ),
      'where' => "u.name = '%s'",
      '#type' => 'textfield',
      '#submit' => array(
        'cmf_users_handler',
      ),
      '#autocomplete_path' => 'cmf/userauto',
    );
    $filters['role'] = array(
      'title' => t('user role'),
      'single_use' => TRUE,
      'whole_value' => 'before',
      'where' => "u.uid = ur.uid AND ur.rid = %d",
      'join' => "INNER JOIN {users_roles} ur ON u.uid = ur.uid",
      '#type' => 'select',
      '#options' => cmf_get_roles('names'),
    );
    $filters['blocked'] = array(
      'title' => t('user status'),
      'single_use' => TRUE,
      'whole_value' => 'before',
      'where' => "u.status = %d AND u.uid != 0",
      '#type' => 'select',
      '#options' => array(
        1 => t('active'),
        0 => t('blocked'),
      ),
    );
  }
  drupal_alter('cmf_filters', $filters);
  return $filters;
}