/**
  * Implements \Drupal\Core\Entity\Query\QueryInterface::tableSort().
  */
 public function tableSort(&$headers)
 {
     // If 'field' is not initialized, the header columns aren't clickable.
     foreach ($headers as $key => $header) {
         if (is_array($header) && isset($header['specifier'])) {
             $headers[$key]['field'] = '';
         }
     }
     $order = tablesort_get_order($headers);
     $direction = tablesort_get_sort($headers);
     foreach ($headers as $header) {
         if (is_array($header) && $header['data'] == $order['name']) {
             $this->sort($header['specifier'], $direction, isset($header['langcode']) ? $header['langcode'] : NULL);
         }
     }
     return $this;
 }
  /**
   * Helper function to sort rows.
   *
   * @param array $header
   *   The table's header.
   * @param array $rows
   *   Array of rows.
   *
   * @return array $rows
   *   Array of sorted rows.
   */
  public function sortRows(array $header, array $rows) {
    if (!empty($rows)) {
      // Get selected order from the request or the default one.
      $order = tablesort_get_order($header);
      // Please note that we do not run any sql query against the database. The
      // 'sql' key is simply there for tablesort needs.
      $order = $order['sql'];

      // Get the field we sort by from the request if any.
      $sort = tablesort_get_sort($header);

      // Obtain the column we need to sort by.
      foreach ($rows as $key => $value) {
        $order_column[$key] = $value[$order];
      }
      // Sort data.
      if ($sort == 'asc') {
        array_multisort($order_column, SORT_ASC, $rows);
      }
      elseif ($sort == 'desc') {
        array_multisort($order_column, SORT_DESC, $rows);
      }
    }
    return $rows;
  }
Example #3
0
 /**
  * Determine the current sort criterion.
  *
  * @return
  *   An associative array describing the criterion, containing the keys:
  *   - "name": The localized title of the table column.
  *   - "sql": The name of the database field to sort on.
  *
  * @see tablesort_get_order()
  */
 protected function order()
 {
     return tablesort_get_order($this->header);
 }
Example #4
0
/**
 * Returns an array that can be passed to drupal_render() of the given user's sites/roles.
 * @param string $username
 */
function _unl_get_user_audit_content($username)
{
    if (user_is_anonymous()) {
        return array();
    }
    $audit_map = array();
    foreach (unl_get_site_user_map('username', $username) as $site_id => $site) {
        $audit_map[$site_id] = array('uri' => l($site['uri'], $site['uri']), 'roles' => '', 'last_update' => $site['last_update']);
        foreach ($site['roles'] as $role => $user) {
            $audit_map[$site_id]['roles'] .= "{$role} ";
            $audit_map[$site_id]['roles'] .= $GLOBALS['user']->name != $username ? "({$user})" : '';
            $audit_map[$site_id]['roles'] .= "<br />";
        }
    }
    if (count($audit_map) > 0) {
        $header = array('uri' => array('data' => t('Site'), 'field' => 'uri'), 'role' => array('data' => t('Role') . ($GLOBALS['user']->name != $username ? ' (' . t('User') . ')' : '')), 'last_update' => array('data' => t('Last Updated'), 'field' => 'last_update'));
        // Sort the table data accordingly with a custom sort function.
        $order = tablesort_get_order($header);
        $sort = tablesort_get_sort($header);
        $rows = unl_sites_sort($audit_map, $order, $sort);
        // Now that the access timestamp has been used to sort, convert it to something readable.
        foreach ($rows as $key => $row) {
            $rows[$key]['last_update'] = isset($rows[$key]['last_update']) ? format_date($rows[$key]['last_update'], 'short') : t('never');
        }
        $content = array('#theme' => 'table', '#header' => $header, '#rows' => $rows);
        if ($username == $GLOBALS['user']->name) {
            $content['#caption'] = t('You belong to the following sites as a member of the listed roles.');
        } else {
            $content['#caption'] = t('Users matching "@user" belong to the following sites as a member of the listed roles.', array('@user' => $username));
        }
    } else {
        $content = array('#type' => 'item');
        if ($username == $GLOBALS['user']->name) {
            $content['#title'] = t('You do not belong to any roles on any sites.');
        } else {
            $content['#title'] = t('User matching "@user" does not belong to any roles on any sites.', array('@user' => $username));
        }
    }
    return $content;
}