You are here

function farm_plan_consideration_list in farmOS 7

Page callback for considerations list, filtered for a plan.

Parameters

$plan: The plan object to filter considerations for..

Return value

string Return the content of the page.

1 string reference to 'farm_plan_consideration_list'
farm_plan_consideration_menu in modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module
Implements hook_menu().

File

modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module, line 225
Farm plan consideration module.

Code

function farm_plan_consideration_list($plan) {

  // Set the page title.
  drupal_set_title(t('Considerations'));

  // Create a fieldset for the considerations.
  $build['considerations'] = array(
    '#type' => 'fieldset',
    '#title' => t('Planning considerations'),
    '#description' => t('Use this to define any planning considerations you want to remember. Considerations can be specific to a single plan, or they can apply to all plans.'),
  );

  // Define the considerations table header.
  $header = array(
    t('Consideration'),
    t('Type'),
    t('Start'),
    t('End'),
    t('Associations'),
    t('Plan'),
    t('Actions'),
  );

  // Load information about all consideration types.
  $consideration_types = farm_plan_consideration_types();

  // Query all considerations from the database that either apply to all plans,
  // or are associated with this plan specifically, and build a set of rows for
  // the table.
  $rows = array();
  $result = db_query('SELECT id FROM {farm_plan_consideration} WHERE plan_id IS NULL OR plan_id = :plan_id', array(
    ':plan_id' => $plan->id,
  ));
  foreach ($result as $record) {

    // Load the consideration.
    $consideration = farm_plan_consideration_load($record->id);

    // If the consideration didn't load, skip it.
    if (empty($consideration)) {
      continue;
    }

    // Sanitize the name.
    $name = check_plain($consideration->name);

    // Get the consideration type label.
    $type = $consideration->type;
    if (!empty($consideration_types[$type]['label'])) {
      $type = $consideration_types[$type]['label'];
    }

    // Format the start and end dates.
    $date_format = 'M j Y';
    $start_time = date($date_format, $consideration->start_time);
    $end_time = date($date_format, $consideration->end_time);

    // If there are entity associations, build a list.
    $associations = '';
    $entity_links = array();
    if (!empty($consideration->entities)) {
      foreach ($consideration->entities as $entity_type => $ids) {
        foreach ($ids as $id) {
          $entities = entity_load($entity_type, array(
            $id,
          ));
          if (empty($entities)) {
            continue;
          }
          $entity = reset($entities);
          $label = entity_label($entity_type, $entity);
          $uri = entity_uri($entity_type, $entity);
          $entity_links[] = l($label, $uri['path']);
        }
      }
    }
    if (!empty($entity_links)) {
      $associations = theme('item_list', array(
        'items' => $entity_links,
      ));
    }

    // If the consideration is linked to a plan, show a link to it. Otherwise,
    // just say "all".
    $plan_link = 'all';
    if (!empty($consideration->plan_id)) {
      $consideration_plan = farm_plan_load($consideration->plan_id);
      if (!empty($plan)) {
        $plan_label = entity_label('farm_plan', $consideration_plan);
        $plan_uri = entity_uri('farm_plan', $consideration_plan);
        $plan_link = l($plan_label, $plan_uri['path']);
      }
    }

    // Build the actions.
    $actions = array();
    $actions[] = l(t('Edit'), 'farm/plan/' . $plan->id . '/considerations/' . $consideration->id . '/edit');
    $actions[] = l(t('Delete'), 'farm/plan/' . $plan->id . '/considerations/' . $consideration->id . '/delete');
    $actions = implode(' | ', $actions);

    // Assemble the row.
    $row = array(
      $name,
      $type,
      $start_time,
      $end_time,
      $associations,
      $plan_link,
      $actions,
    );

    // Add it to the rows array.
    $rows[] = $row;
  }

  // Build the table render array.
  $build['considerations']['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No considerations found.'),
  );
  return $build;
}