You are here

function farm_livestock_weight_group_report_form in farmOS 7

Animal group report form.

1 string reference to 'farm_livestock_weight_group_report_form'
farm_livestock_weight_menu in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module
Implements hook_menu().

File

modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module, line 282
Farm livestock weight module.

Code

function farm_livestock_weight_group_report_form($form, &$form_state) {

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

  // Build an entity field query of group assets.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'farm_asset');
  $query
    ->entityCondition('bundle', 'group');
  $query
    ->propertyOrderBy('name', 'ASC');

  // Execute the query and build a list of options.
  $options = array();
  $result = $query
    ->execute();
  if (isset($result['farm_asset'])) {
    $group_ids = array_keys($result['farm_asset']);
    $groups = farm_asset_load_multiple($group_ids);
    if (!empty($groups)) {
      foreach ($groups as $group) {
        if (!empty($group->id)) {
          $label = entity_label('farm_asset', $group);
          if ($group->archived) {
            $date = strftime('%Y-%m-%d', $group->archived);
            $label = $label . ' (' . t('archived') . ' ' . $date . ')';
          }
          $options[$group->id] = $label;
        }
      }
    }
  }

  // Define the input fieldset
  $form['input'] = array(
    '#type' => 'fieldset',
    '#title' => t('Input Form'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Create a select field for the groups.
  $form['input']['group'] = array(
    '#type' => 'select',
    '#title' => t('Animal Group'),
    '#description' => t('Select the group(s) of animals to include in the weight report.'),
    '#options' => $options,
    '#required' => TRUE,
    '#multiple' => TRUE,
  );
  $form['input']['archived'] = array(
    '#type' => 'select',
    '#title' => t('Archived'),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
      2 => '-' . t('Any') . '-',
    ),
    '#default_value' => 0,
  );
  $form['input']['flags'] = array(
    '#type' => 'select',
    '#title' => t('Flags'),
    '#options' => farm_flags_field_allowed_values(),
    '#multiple' => TRUE,
  );

  // Provide a default date in the format YYYY-MM-DD.
  $format = 'Y-m-d';
  $current_date = date($format, REQUEST_TIME);
  $form['input']['birth'] = array(
    '#type' => 'fieldset',
    '#title' => t('Birth Date'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['input']['birth']['earliest_birth'] = array(
    '#type' => 'date_select',
    '#title' => t('Earliest Birth'),
    '#default_value' => '',
    '#date_year_range' => '-10:+1',
    '#date_format' => $format,
    '#date_label_position' => 'within',
  );
  $form['input']['birth']['latest_birth'] = array(
    '#type' => 'date_select',
    '#title' => t('Latest Birth'),
    '#default_value' => '',
    '#date_year_range' => '-10:+1',
    '#date_format' => $format,
    '#date_label_position' => 'within',
  );
  $form['input']['start_date'] = array(
    '#type' => 'date_select',
    '#title' => t('Start date'),
    '#description' => t('First recorded date of animal weights to include..'),
    '#default_value' => '',
    '#date_year_range' => '-10:+1',
    '#date_format' => $format,
    '#date_label_position' => 'within',
  );
  $form['input']['end_date'] = array(
    '#type' => 'date_select',
    '#title' => t('End date'),
    '#description' => t('Last recorded date of animal weights to include..'),
    '#default_value' => $current_date,
    '#date_year_range' => '-10:+1',
    '#date_format' => $format,
    '#date_label_position' => 'within',
  );

  // Add submit button to generate report.
  $form['input']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#submit' => array(
      'farm_livestock_weight_group_report_form_submit',
    ),
  );

  // Add submit button to download CSV.
  $form['input']['csv'] = array(
    '#type' => 'submit',
    '#value' => t('CSV'),
    '#submit' => array(
      'farm_livestock_weight_group_report_csv',
    ),
  );

  // Display results from the form.
  if (!empty($form_state['results_table'])) {

    // Hide the input fieldset
    $form['input']['#collapsed'] = TRUE;

    // Create a results fieldset.
    $form['results'] = array(
      '#type' => 'fieldset',
      '#title' => t('Results'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    // Display the Graphs.
    $graphs = '<div class="farm-livestock-weight-group-report-graphs">' . implode('', $form_state['graph-markup']) . '</div>';

    // Add JS and CSS to build the graphs.
    drupal_add_js($form_state['graph-settings'], 'setting');
    drupal_add_js(drupal_get_path('module', 'farm_livestock_weight') . '/farm_livestock_weight_group_graph.js');
    drupal_add_js('https://cdn.plot.ly/plotly-latest.min.js', 'external');
    drupal_add_css(drupal_get_path('module', 'farm_livestock_weight') . '/farm_livestock_weight.css');
    $form['results']['graphs'] = array(
      '#markup' => $graphs,
    );

    // Display the HTML Table.
    $form['results']['results_table'] = array(
      '#markup' => $form_state['results_table'],
    );
  }
  return $form;
}