You are here

public function UserBadgeUIController::overviewForm in User Badges 7.4

Admin form for searching and doing bulk operations.

Overrides EntityDefaultUIController::overviewForm

File

./user_badges.module, line 1334
Hooks and other stuff related to user badge.

Class

UserBadgeUIController
Custom controller for the administrator UI

Code

public function overviewForm($form, &$form_state) {
  $form['pager'] = array(
    '#theme' => 'pager',
  );
  $header = array(
    'name' => array(
      'data' => t('Name'),
      'field' => 'name',
    ),
    'weight' => array(
      'data' => t('Weight'),
      'field' => 'weight',
    ),
    'operations' => array(
      'data' => t('Operations'),
      'field' => 'operations',
    ),
  );
  $items = array();
  $search_term = !empty($_GET['search']) ? $_GET['search'] : NULL;
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'user_badge');
  if (!empty($search_term)) {
    $query
      ->propertyCondition('name', '%' . $search_term . '%', 'like');
  }

  // Check for sort order and sort key.
  if (!empty($_GET['sort']) && !empty($_GET['order'])) {
    $sort = strtoupper($_GET['sort']);
    $order = strtolower($_GET['order']);
    $order = str_replace(' ', '_', $order);
    if ($order != 'operations') {
      $query
        ->propertyOrderBy($order, $sort);
    }
  }
  $query
    ->pager(10);
  $result = $query
    ->execute();
  $user_badge_results = !empty($result['user_badge']) ? $result['user_badge'] : array();
  $user_badge_array = !empty($user_badge_results) ? user_badge_load_multiple(array_keys($user_badge_results)) : array();
  foreach ($user_badge_array as $bid => $user_badge) {
    $items['bid-' . $bid] = array(
      'name' => l($user_badge->name, 'user-badge/' . $user_badge->bid),
      'weight' => $user_badge->weight,
      'operations' => l(t('View'), 'user-badge/' . $user_badge->bid) . ' ' . l(t('Edit'), USER_BADGES_ADMIN_USER_BADGES_MANAGE_URI . $bid, array(
        'query' => array(
          'destination' => USER_BADGES_ADMIN_USER_BADGES_URI,
        ),
      )) . ' ' . l(t('Delete'), USER_BADGES_ADMIN_USER_BADGES_MANAGE_URI . $bid . '/delete', array(
        'attributes' => array(
          'class' => array(
            'user_badge_delete-' . $user_badge->bid,
          ),
        ),
        'query' => array(
          'destination' => USER_BADGES_ADMIN_USER_BADGES_URI,
        ),
      )),
    );
  }
  $form['search'] = array(
    '#type' => 'fieldset',
    '#title' => t('Basic Search'),
    '#collapsible' => TRUE,
    '#collapsed' => !empty($search_term) ? FALSE : TRUE,
  );
  $form['search']['search_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Search Term'),
    '#default_value' => !empty($search_term) ? $search_term : '',
  );
  $form['search']['search_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  $form['bulk_operations'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bulk Operations'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['bulk_operations']['operations'] = array(
    '#type' => 'select',
    '#options' => array(
      0 => t('Select a bulk operation'),
      'delete' => t('Delete selected user badge'),
    ),
  );
  $form['bulk_operations']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  $form['entities'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $items,
    '#attributes' => array(
      'class' => array(
        'entity-sort-table',
      ),
    ),
    '#empty' => t('There are no user badge.'),
  );
  return $form;
}