You are here

function redhen_org_page in RedHen CRM 7

Page callback for org overview page.

1 string reference to 'redhen_org_page'
redhen_org_menu in modules/redhen_org/redhen_org.module
Implements hook_menu().

File

modules/redhen_org/includes/redhen_org.pages.inc, line 13

Code

function redhen_org_page() {

  // Setup the header for both the query and table.
  $header = array(
    'type' => array(
      'field' => 'type',
      'type' => 'property',
      'data' => 'Type',
      'sort' => 'asc',
      'specifier' => 'type',
    ),
    'label' => array(
      'field' => 'label',
      'type' => 'property',
      'data' => 'Name',
      'sort' => 'asc',
      'specifier' => 'label',
    ),
    'updated' => array(
      'field' => 'updated',
      'type' => 'property',
      'data' => 'Updated',
      'sort' => 'desc',
      'specifier' => 'updated',
    ),
  );
  $header['operations'] = array(
    'data' => t('Operations'),
  );

  // Need to ensure the query doesn't execute when posing the form.
  $result = FALSE;
  if (!isset($_POST['form_id'])) {
    $bundle = isset($_GET['bundle']) ? $_GET['bundle'] : '';
    $properties = isset($_GET['properties']) ? $_GET['properties'] : array();
    $fields = isset($_GET['fields']) ? $_GET['fields'] : array();

    // Check Relation Role Permissions if necessary.
    global $user;
    if (!user_access('access redhen orgs')) {
      $redhen_relation_role_permissions = redhen_relation_role_get_permissions($user);
      if (count($redhen_relation_role_permissions) > 0) {
        $org_ids = array_keys($redhen_relation_role_permissions);
      }
      else {

        // No allowed organizations.
        $org_ids = -1;
      }
      $relation_role_orgs['org_id'] = $org_ids;
      $properties = array_merge($properties, $relation_role_orgs);
    }
    $result = redhen_filter_query('redhen_org', $header, $bundle, $properties, $fields);
  }
  $orgs = array();
  $rows = array();
  if ($result) {
    $orgs = redhen_org_load_multiple(array_keys($result['redhen_org']));
    if (!empty($orgs)) {
      $destination = drupal_get_destination();
      foreach ($orgs as $org) {
        $uri = entity_uri('redhen_org', $org);
        $redhen_org_type = redhen_org_type_load($org->type);
        $data = array(
          'type' => check_plain($redhen_org_type->label),
          'label' => array(
            'data' => array(
              '#type' => 'link',
              '#title' => $org->label,
              '#href' => $uri['path'],
            ),
          ),
          'updated' => redhen_format_date($org->updated, 'short'),
        );
        $row_classes = array();

        // Set a class for the row depending if the org is active or archived.
        $row_classes[] = $org->redhen_state == REDHEN_STATE_ACTIVE ? 'active' : 'archived';
        $rows[$org->org_id] = array(
          'data' => $data,
          'class' => $row_classes,
        );

        // Build a list of all the accessible operations for the current org.
        $ops = array();
        if (redhen_org_access('edit', $org)) {
          $ops['edit'] = array(
            'title' => t('edit'),
            'href' => $uri['path'] . '/view/edit',
            'query' => $destination,
          );
        }
        if (redhen_org_access('delete', $org)) {
          $ops['delete'] = array(
            'title' => t('delete'),
            'href' => $uri['path'] . '/view/delete',
            'query' => $destination,
          );
        }
        if (count($ops) > 1) {

          // Render an unordered list of operations links.
          $rows[$org->org_id]['data']['operations'] = array(
            'data' => array(
              '#theme' => 'links__node_operations',
              '#links' => $ops,
              '#attributes' => array(
                'class' => array(
                  'links',
                  'inline',
                ),
              ),
            ),
          );
        }
        elseif (!empty($ops)) {

          // Render the first and only operation as a link.
          $link = reset($ops);
          $rows[$org->org_id]['data']['operations'] = array(
            'data' => array(
              '#type' => 'link',
              '#title' => $link['title'],
              '#href' => $link['href'],
              '#options' => array(
                'query' => $link['query'],
              ),
            ),
          );
        }
        else {
          $rows[$org->org_id]['data']['operations'] = array(
            'data' => '',
          );
        }
      }
    }
  }
  return array(
    'form' => drupal_get_form('redhen_filter_form', 'redhen_org'),
    'orgs' => array(
      '#theme' => 'redhen_org_list',
      '#orgs' => $orgs,
      '#header' => $header,
      '#rows' => $rows,
    ),
  );
}