You are here

choices.module in Poll Improved 7

Defines simple choices field.

File

modules/choices/choices.module
View source
<?php

/**
 * @file
 * Defines simple choices field.
 */

/**
 * Implements hook_field_info().
 */
function choices_field_info() {

  //@todo: machine name attrbute for the choice field (delta doesn't work when changing orders afterwards)
  return array(
    'choices' => array(
      'label' => t('Poll choices'),
      'description' => t('This field stores varchar text in the database.'),
      'default_widget' => 'choices_default',
      'default_formatter' => 'choices_default',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function choices_field_is_empty($item, $field) {
  if (!isset($item['value']) || $item['value'] === '') {
    return !isset($item['summary']) || $item['summary'] === '';
  }
  return FALSE;
}

/**
 * Implements hook_field_formatter_info().
 */
function choices_field_formatter_info() {
  return array(
    'choices_default' => array(
      'label' => t('Default'),
      'field types' => array(
        'choices',
      ),
    ),
    'choices_with_vote_count' => array(
      'label' => t('Choices with vote count'),
      'field types' => array(
        'choices',
      ),
    ),
    'choices_plain' => array(
      'label' => t('Choices plain'),
      'field types' => array(
        'choices',
      ),
    ),
    'choices_submit_default' => array(
      'label' => t('Default'),
      'field types' => array(
        'choices_submit',
      ),
      'settings' => array(
        'label' => 'hidden',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function choices_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $entity_id = $entity
    ->internalIdentifier();
  switch ($display['type']) {
    case 'choices_default':
    case 'choices_with_vote_count':
      $votes_total_count = db_query("SELECT COUNT(*) FROM {pollim_vote} WHERE pollim_id = :pid", array(
        ':pid' => $entity
          ->internalIdentifier(),
      ))
        ->fetchField();
      $choice_items = array();
      foreach ($items as $delta => $item) {
        $item_vote_count = db_query("SELECT COUNT(*) FROM {pollim_vote} WHERE pollim_id = :pid AND data = :data", array(
          ':pid' => $entity
            ->internalIdentifier(),
          ':data' => $delta,
        ))
          ->fetchField();

        //@todo: theme functions for the output
        $percentage = $votes_total_count ? round($item_vote_count * 100 / $votes_total_count) : 0;
        $votes_txt = $percentage . '%';
        $val = theme('choices_radiobtn', array(
          'element' => array(
            '#entity_id' => $entity_id,
            '#delta' => $delta,
            '#votes_count' => $votes_txt,
            '#txt' => $item['value'],
          ),
        ));
        $choice_items[$delta] = array(
          '#markup' => $val,
          '#percentage' => $percentage,
        );
      }
      $element[0] = array(
        '#choice_items' => $choice_items,
        '#total_count' => $votes_total_count,
        '#theme' => 'choices_choices',
      );
      break;
    case 'choices_plain':
      foreach ($items as $delta => $item) {
        foreach ($items as $delta => $item) {
          $val = theme('choices_radiobtn', array(
            'element' => array(
              '#entity_id' => $entity_id,
              '#delta' => $delta,
              '#txt' => $item['value'],
            ),
          ));
          $choice_items[$delta] = array(
            '#markup' => $val,
          );
        }
        $element[0] = array(
          '#choice_items' => $choice_items,
          '#theme' => 'choices_choices',
        );
      }
      break;
  }
  return $element;
}

/**
 * Implements hook_field_widget_info().
 */
function choices_field_widget_info() {
  return array(
    'choices_default' => array(
      'label' => t('Text field'),
      'field types' => array(
        'choices',
      ),
    ),
    'choices_submit_default' => array(
      'label' => t('Text field'),
      'field types' => array(
        'choices_submit',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function choices_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'choices_default':
      $main_widget = $element + array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
        '#size' => 60,
        '#maxlength' => 255,
        '#attributes' => array(
          'class' => array(
            'choices-text',
          ),
        ),
      );
      break;
    case 'choices_submit_default':
      $main_widget = $element + array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : t('Save'),
        '#size' => 20,
        '#maxlength' => 32,
        '#attributes' => array(
          'class' => array(
            'choices-submit',
          ),
        ),
      );
      break;
  }
  if ($main_widget) {
    $element['value'] = $main_widget;
  }
  return $element;
}

/**
 * Implements hook_field_widget_error().
 */
function choices_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    default:
      $error_element = $element[$element['#columns'][0]];
      break;
  }
  form_error($error_element, $error['message']);
}

/**
 * Implements hook_theme().
 */
function choices_theme() {
  $path = drupal_get_path('module', 'choices');
  return array(
    'choices_choices' => array(
      'render element' => 'element',
      'path' => "{$path}/theme",
      'file' => 'choices.theme.inc',
    ),
    'choices_radiobtn' => array(
      'render element' => 'element',
      'path' => "{$path}/theme",
      'file' => 'choices.theme.inc',
    ),
  );
}