diff --git a/election.info b/election.info index 393b4b87735d0ce3dd04eaa0d98c0006187732ad..7133764b0217d3afa5363c19f98e1cd80eb6c575 100644 --- a/election.info +++ b/election.info @@ -6,7 +6,13 @@ php = 5.3 dependencies[] = entity dependencies[] = field_ui dependencies[] = rules +dependencies[] = views files[] = election-default.controller.inc files[] = election.controller.inc files[] = election-post.controller.inc files[] = election-candidate.controller.inc +files[] = views/handlers/views_handler_field_election_type.inc +files[] = views/handlers/views_handler_field_election_vstatus.inc +files[] = views/handlers/views_handler_filter_election_vstatus.inc +files[] = views/handlers/views_handler_field_election_nstatus.inc +files[] = views/handlers/views_handler_filter_election_nstatus.inc \ No newline at end of file diff --git a/views/election.views.inc b/views/election.views.inc index f07d704663a606efcf756ee4c8b08711cf43aa6e..b71e72090ef47dc14d92660f5e98b35affc0b605 100644 --- a/views/election.views.inc +++ b/views/election.views.inc @@ -5,8 +5,6 @@ * * @todo this is totally unfinished and doesn't work at all. */ - - function election_views_data_alter(&$data) { // ---------------------------------------------------------------- @@ -20,6 +18,7 @@ function election_views_data_alter(&$data) { $data['election']['table']['base'] = array( 'field' => 'election_id', 'title' => t('Election'), + 'entity type' => 'election', 'weight' => -10, // 'access query tag' => 'election_access', // @todo work out how to adapt this 'defaults' => array( @@ -37,7 +36,7 @@ function election_views_data_alter(&$data) { // Information for displaying the election_id 'field' => array( 'handler' => 'views_handler_field', // @todo create a handler - 'click sortable' => TRUE, // @todo understand this + 'click sortable' => TRUE, ), // Information for accepting an election_id as an argument 'argument' => array( @@ -63,7 +62,7 @@ function election_views_data_alter(&$data) { // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', // @todo create a handler - 'click sortable' => TRUE, // @todo understand this + 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', @@ -77,6 +76,157 @@ function election_views_data_alter(&$data) { ), ); + // voting status + $data['election']['vstatus'] = array( + 'title' => t('Voting open'), + 'help' => t('Whether voting is open for this election.'), + 'field' => array( + 'handler' => 'views_handler_field_election_vstatus', + 'click sortable' => FALSE, + 'label' => t('Voting status'), // @todo check if this is valid + ), + 'filter' => array( + 'handler' => 'views_handler_filter_election_vstatus', + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + ); + + // nominations status + // @todo make the filter always true if it's a referendum. + $data['election']['nstatus'] = array( + 'title' => t('Nominations open'), + 'help' => t('Whether nominations are open for this election (not relevant for referendums).'), + 'field' => array( + 'handler' => 'views_handler_field_election_nstatus', + 'click sortable' => FALSE, + 'label' => t('Nominations status'), // @todo check if this is valid + ), + 'filter' => array( + 'handler' => 'views_handler_filter_election_nstatus', + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + ); + + // type + $data['election']['type'] = array( + 'title' => t('Type'), // The item it appears as on the UI, + 'help' => t('The election type (for example, "STV Election" or "Referendum").'), + 'field' => array( + 'handler' => 'views_handler_field_election_type', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), +/* 'filter' => array( + 'handler' => 'views_handler_filter_node_type', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_node_type', + ), */ + ); + + // created + $data['election']['created'] = array( + 'title' => t('Created date'), // The item it appears as on the UI, + 'help' => t('The date when the election was created.'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // changed + $data['election']['changed'] = array( + 'title' => t('Updated date'), // The item it appears as on the UI, + 'help' => t('The date when the election settings were last updated.'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // vopen_time + $data['election']['vopen_time'] = array( + 'title' => t('Voting opening date'), // The item it appears as on the UI, + 'help' => t('The date when voting opens for this election.'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // vclose_time + $data['election']['vclose_time'] = array( + 'title' => t('Voting closing date'), // The item it appears as on the UI, + 'help' => t('The date when voting closes for this election.'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // nopen_time + $data['election']['nopen_time'] = array( + 'title' => t('Nominations opening date'), // The item it appears as on the UI, + 'help' => t('The date when nominations open for this election (not relevant for referendums).'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // nclose_time + $data['election']['nclose_time'] = array( + 'title' => t('Nominations closing date'), // The item it appears as on the UI, + 'help' => t('The date when nominations close for this election (not relevant for referendums).'), // The help that appears on the UI, + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // ---------------------------------------------------------------- // election table -- fields hidden from Views diff --git a/views/handlers/views_handler_field_election_nstatus.inc b/views/handlers/views_handler_field_election_nstatus.inc new file mode 100644 index 0000000000000000000000000000000000000000..b5fb92f613374d84b4f33059edd117e56840d4fa --- /dev/null +++ b/views/handlers/views_handler_field_election_nstatus.inc @@ -0,0 +1,27 @@ +<?php +/** + * @file + * Handler for the election nominations status (nstatus) field. + */ + +class views_handler_field_election_nstatus extends views_handler_field { + + function construct() { + parent::construct(); + $this->additional_fields['nstatus'] = 'nstatus'; + $this->additional_fields['nopen_time'] = 'nopen_time'; + $this->additional_fields['nclose_time'] = 'nclose_time'; + } + + function query() { + $this->ensure_my_table(); + $this->add_additional_fields(); + } + + function render($values) { + // Dummy election object + $election = (object) $values; + return _election_format_status($election, 'n'); + } + +} diff --git a/views/handlers/views_handler_field_election_type.inc b/views/handlers/views_handler_field_election_type.inc new file mode 100644 index 0000000000000000000000000000000000000000..2eb1c67c1d9496498e8d36987413da0028e4deae --- /dev/null +++ b/views/handlers/views_handler_field_election_type.inc @@ -0,0 +1,50 @@ +<?php +/** + * @file + * Field handler to translate an election type into its readable form. + * + * @see views_handler_field_node_type.inc + */ + + +class views_handler_field_election_type extends views_handler_field { + + function option_definition() { + $options = parent::option_definition(); + $options['machine_name'] = array('default' => FALSE); + return $options; + } + + /** + * Provide machine_name option for election type display. + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + $form['machine_name'] = array( + '#title' => t('Output machine name'), + '#description' => t('Display field as the election type machine name.'), + '#type' => 'checkbox', + '#default_value' => !empty($this->options['machine_name']), + '#fieldset' => 'more', + ); + } + + /** + * Render election type as human readable name, unless using machine_name option. + */ + function render_name($data, $values) { + if ($this->options['machine_name'] != 1 && $data !== NULL && $data !== '') { + $type = _election_type_load($data); + $name = drupal_ucfirst($type['name']); + return t($this->sanitize_value($name)); + } + return $this->sanitize_value($data); + } + + function render($values) { + $value = $this->get_value($values); + return $this->render_name($value, $values); + } + +} diff --git a/views/handlers/views_handler_field_election_vstatus.inc b/views/handlers/views_handler_field_election_vstatus.inc new file mode 100644 index 0000000000000000000000000000000000000000..e90b88088821e0c3177aaa67e10f5cd380b7595b --- /dev/null +++ b/views/handlers/views_handler_field_election_vstatus.inc @@ -0,0 +1,27 @@ +<?php +/** + * @file + * Handler for the election voting status (vstatus) field. + */ + +class views_handler_field_election_vstatus extends views_handler_field { + + function construct() { + parent::construct(); + $this->additional_fields['vstatus'] = 'vstatus'; + $this->additional_fields['vopen_time'] = 'vopen_time'; + $this->additional_fields['vclose_time'] = 'vclose_time'; + } + + function query() { + $this->ensure_my_table(); + $this->add_additional_fields(); + } + + function render($values) { + // Dummy election object + $election = (object) $values; + return _election_format_status($election, 'v'); + } + +} diff --git a/views/handlers/views_handler_filter_election_nstatus.inc b/views/handlers/views_handler_filter_election_nstatus.inc new file mode 100644 index 0000000000000000000000000000000000000000..d2f05667baa69f51d9f47341e34d87d9f6ed14d2 --- /dev/null +++ b/views/handlers/views_handler_filter_election_nstatus.inc @@ -0,0 +1,24 @@ +<?php +/** + * Filter by nominations status + */ +class views_handler_filter_election_nstatus extends views_handler_filter { + + function admin_summary() { } + function operator_form(&$form, &$form_state) { } + function can_expose() { return FALSE; } + + function query() { + $table = $this->ensure_my_table(); + $this->query->add_where_expression( + $this->options['group'], + "$table.nstatus = :openstatus OR ($table.nstatus = :scheduledstatus AND $table.nopen_time < :rtime AND $table.nclose_time > :rtime)", + array( + ':openstatus' => ELECTION_STATUS_OPEN, + ':scheduledstatus' => ELECTION_STATUS_SCHEDULED, + ':rtime' => REQUEST_TIME, + ) + ); + } + +} diff --git a/views/handlers/views_handler_filter_election_vstatus.inc b/views/handlers/views_handler_filter_election_vstatus.inc new file mode 100644 index 0000000000000000000000000000000000000000..88f98ffb66762d2e0bf6571b947836ba4e7abce1 --- /dev/null +++ b/views/handlers/views_handler_filter_election_vstatus.inc @@ -0,0 +1,24 @@ +<?php +/** + * Filter by voting status + */ +class views_handler_filter_election_vstatus extends views_handler_filter { + + function admin_summary() { } + function operator_form(&$form, &$form_state) { } + function can_expose() { return FALSE; } + + function query() { + $table = $this->ensure_my_table(); + $this->query->add_where_expression( + $this->options['group'], + "$table.vstatus = :openstatus OR ($table.vstatus = :scheduledstatus AND $table.vopen_time < :rtime AND $table.vclose_time > :rtime)", + array( + ':openstatus' => ELECTION_STATUS_OPEN, + ':scheduledstatus' => ELECTION_STATUS_SCHEDULED, + ':rtime' => REQUEST_TIME, + ) + ); + } + +}