<?php

class vote_pool_options extends vote_pool {
  function options_form(&$form, &$form_state) {
    parent::options_form(&$form, &$form_state);
    
    $form['allowed_values'] = array(
      '#type' => 'textarea',
      '#title' => t('Allowed values list'),
      '#default_value' => vote_implode_allowed_values($this->get_allowed_values()),
      '#element_validate' => array('vote_admin_pool_allowed_values_validate'),
      '#description' => '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database and must be a @allowed. The label is optional, and the key will be used as the label if no label is specified. Allowed HTML tags in labels: @tags', array('@allowed' => 'decimal or integer value', '@tags' => _field_filter_xss_display_allowed_tags())) . '</p>',
    );
    
    $form['output'] = array(
      '#type' => 'fieldset',
      '#title' => t('Display options'),
      '#description' => t('Define how values are being displayed in lists.'),
    );
    
    $form['output']['replace'] = array(
      '#type' => 'checkbox',
      '#title' => t('Replace with string value'),
      '#description' => t('Check this if you want to replace the integer value with the associated string in lists.'),
      '#default_value' => $this->replace,
    );
  }  
  
  function options_form_submit(&$form, &$form_state) {
    parent::options_form_submit($form, $form_state);
    
    $values = $form_state['values'];
    
    $this->allowed_values = $values['allowed_values'];
    $this->replace = $values['replace'];
  }
    
  function options() {
    $options = array(
      'allowed_values' => array(),
      'replace' => TRUE,
    );
    
    return $options + parent::options();
  }
  
  function render($vote) {
    if (isset($vote->value)) {
      if ($this->replace) {
	    $allowed = $this->get_allowed_values();
	      
	    return isset($allowed[$vote->value]) ? $allowed[$vote->value] : '';
      } else {
      	return $this->value;
      }
    }
  }  
}

class vote_pool_points extends vote_pool {
  function options_form(&$form, &$form_state) {
    parent::options_form(&$form, &$form_state);
    
    $form['allowed_values'] = array(
      '#type' => 'textarea',
      '#title' => t('Allowed values list'),
      '#default_value' => vote_implode_allowed_values($this->get_allowed_values()),
      '#element_validate' => array('vote_admin_pool_allowed_values_validate'),
      '#description' => '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database and must be a @allowed. The label is optional, and the key will be used as the label if no label is specified. Allowed HTML tags in labels: @tags', array('@allowed' => 'decimal or integer value', '@tags' => _field_filter_xss_display_allowed_tags())) . '</p>',
    );
    
    $form['output'] = array(
      '#type' => 'fieldset',
      '#title' => t('Display options'),
      '#description' => t('Define how values are being displayed in lists.'),
    );
    
    $form['output']['suffix']['singular'] = array(
      '#type' => 'textfield',
      '#title' => t('Suffix (singular)'),
      '#default_value' => $this->suffix['singular'],
      '#parents' => array('suffix', 'singular'),
    );

    $form['output']['suffix']['plural'] = array(
      '#type' => 'textfield',
      '#title' => t('Suffix (plural)'),
      '#default_value' => $this->suffix['plural'],
      '#parents' => array('suffix', 'plural'),
    );  
  }  
  
  function options_form_submit(&$form, &$form_state) {
    parent::options_form_submit($form, $form_state);
    
    $values = $form_state['values'];
    
    $this->allowed_values = $values['allowed_values'];
    $this->suffix = $values['suffix'];
  }
    
  function options() {
    $options = array(
      'suffix' => array(
        'singular' => '',
        'plural' => '',
      ),
    );
    
    return $options + parent::options();
  }
  
  function render($vote) {
    if (isset($vote->value)) {
      $suffix = format_plural($vote->value, $this->suffix['singular'], $this->suffix['plural']);
      
      return $vote->value . (!empty($suffix) ? ' ' . $suffix : '');
    }
  }  
}

class vote_pool_percent extends vote_pool {
  function options_form(&$form, &$form_state) {
    parent::options_form(&$form, &$form_state);
    
    $form['allowed_values'] = array(
      '#type' => 'item',
      '#title' => t('Allowed values'),
      '#description' => t('This pool automatically provides a range of 1-100 as allowed values.'),
    );
    
    $form['output'] = array(
      '#type' => 'fieldset',
      '#title' => t('Display options'),
      '#description' => t('Define how values are being displayed in lists.'),
    );
    
    $form['output']['suffix'] = array(
      '#type' => 'textfield',
      '#title' => t('Suffix'),
      '#default_value' => $this->suffix,
    );
  }
  
  function options_form_submit(&$form, &$form_state) {
    parent::options_form_submit($form, $form_state);
    
    $values = $form_state['values'];
    
    $this->suffix = $values['suffix'];
  }
  
  function options() {
    $options = array(
      'suffix' => '',
    ) + parent::options();
    
    unset($options['allowed_values']);
    
    return $options;
  }
  
  function get_allowed_values() {
    return drupal_map_assoc(range(1, 100));
  }
  
  function render($vote) {
    if (isset($vote->value)) {
      return $vote->value . (!empty($this->suffix) ? ' ' . $this->suffix : '');
    }
  }
}

class vote_pool_broken extends vote_pool {
  function options_form(&$form, &$form_state) {
    drupal_set_message(t('The handler for this pool is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this pool.'), 'warning');
  }
    
  function broken() { 
    return TRUE; 
  }
}

class vote_pool {
  var $pid = NULL;
  var $type = NULL;
  var $pool = '';
  var $name = '';
  var $description = '';
  
  static function factory($row) {
    $pool = vote_pool_handler($row->type);

    $fields = array('pid', 'type', 'pool', 'name', 'description', 'count', 'count_distinct');
    
    foreach ($fields as $field) {
      if (isset($row->$field)) {
        $pool->$field = $row->$field;
      }
    }

    $options = isset($row->options) ? (array) unserialize($row->options) : array();
    $options = $options + $pool->options();

    foreach ($options as $option => $value) {
      $pool->$option = $value;
    }

    return $pool;
  }
  
  static function construct($type) {
    $pool = vote_pool_handler($type);

    foreach ($pool->options() as $option => $value) {
      $pool->$option = $value;
    }

    return $pool;
  }
  
  function get_allowed_values() {
    return isset($this->allowed_values) ? $this->allowed_values : array();
  }
  
  function options() {
    $options = array(
      'allowed_values' => array(),
      'functions' => array(),
    );

    drupal_alter('votes_pool_options', &$options, $this);
    
    return $options;
  }
  
  function options_form(&$form, &$form_state) {
    $functions = vote_allowed_functions($this->type);
    
    if (!empty($functions)) {
      ctools_include('dependent');
      
      $form['#attached']['js'][] = drupal_get_path('module', 'vote') . '/includes/vote.admin.js';
      
      $form['functions']['status'] = array(
        '#type' => 'item',
        '#title' => t('Enabled functions'),
        '#prefix' => '<div id="edit-function-status-wrapper">',
        '#suffix' => '</div>'
      );
      
      $form['functions']['settings'] = array(
        '#type' => 'item',
        '#title' => t('Function settings'),
        '#process' => array('ctools_dependent_process'),
        '#dependency' => array(),
        '#prefix' => '<div id="edit-functions-settings-wrapper">',
        '#suffix' => '</div>',
      );
      
      $form['functions']['settings']['tabs'] = array(
        '#type' => 'vertical_tabs',
      );      

      foreach ($functions as $function => $info) {
        $form['functions']['status'][$function] = array(
          '#type' => 'checkbox',
          '#title' => $info['title'],
          '#description' => $info['description'],          
          '#default_value' => isset($this->functions[$function]) && $this->functions[$function]['status'],
          '#parents' => array('functions', $function, 'status'),
        );

        $handler = vote_function_handler($function);
        $handler->pool = $this->pool;
        
        $settings = $handler->options_form(array(), $form_state);
        
        if (!empty($settings)) {    
          $form['functions']['settings']['#dependency'] += array('edit-functions-' . $function . '-status' => array(1));
    
          $form['functions']['settings']['tabs'][$function] = array(
            '#type' => 'fieldset',
            '#title' => $info['title'],
            '#parents' => array('functions', $function, 'settings'),
            '#group' => 'function_settings',
          ) + (array) $settings;
        }
      }
    }
  }

  function options_form_validate(&$form, &$form_state) {
    foreach ($form_state['values']['functions'] as $function => $settings) {
      $handler = vote_function_handler($function);
      $handler->pool = $this->pool;
      
      $handler->options_form_validate($form['functions']['settings']['tabs'][$function], $form_state, $settings['status']);
    }
  }
  
  function options_form_submit(&$form, &$form_state) {
    foreach ($form_state['values']['functions'] as $function => $settings) {
      $handler = vote_function_handler($function);
      $handler->pool = $this->pool;
      
      $handler->options_form_validate($form['functions']['settings']['tabs'][$function], $form_state, $settings['status']);
    }
    
    $this->functions = $form_state['values']['functions'];
  }  
  
  function render($vote) {
    if (isset($vote->value)) {
      return $vote->value;
    }
  }
  
  function broken() {
    return FALSE;
  }
}