? decisions-6.x-0.1-beta3-399952-3.patch ? decisions.js ? drupal-6-HEAD.patch Index: decisions.module =================================================================== RCS file: /cvs/drupal/contributions/modules/decisions/decisions.module,v retrieving revision 1.208 diff -u -p -r1.208 decisions.module --- decisions.module 12 Mar 2009 22:32:47 -0000 1.208 +++ decisions.module 3 Apr 2009 15:02:50 -0000 @@ -173,7 +173,12 @@ function decisions_menu() { 'weight' => 3, 'type' => MENU_LOCAL_TASK, ); - + $items['decisions/add_choices_js'] = array( + 'page callback' => 'decisions_add_choices_js', + 'type' => MENU_CALLBACK, + 'access arguments' => array('administer decisions'), + ); + return $items; } @@ -235,7 +240,8 @@ function decisions_theme() { 'decisions_view_header' => array('arguments' => array('node' => NULL, 'teaser' => FALSE)), 'decisions_view_voting' => array('arguments' => array('form' => NULL)), 'decisions_bar' => array('arguments' => array('title' => NULL, 'percentage' => NULL, 'votes' => NULL)), - 'decisions_status' => array('arguments' => array('message' => NULL)) + 'decisions_status' => array('arguments' => array('message' => NULL)), + 'decisions_morechoices' => array('arguments' => array(), 'decisions_morechoices' => NULL), ); } @@ -723,3 +729,6 @@ function decisions_reset_form_submit($fo drupal_set_message('Votes have been reset.'); drupal_goto('node/'. $nid); } + + + Index: decisions_node.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/decisions/decisions_node.inc,v retrieving revision 1.4 diff -u -p -r1.4 decisions_node.inc --- decisions_node.inc 12 Mar 2009 23:11:33 -0000 1.4 +++ decisions_node.inc 3 Apr 2009 15:02:50 -0000 @@ -168,6 +168,50 @@ function decisions_submit(&$node) { } /** + * Add new choices to decisions form when AHAH isn't available + */ +function decisions_add_choices_submit($form, &$form_state) { + // Set the form to rebuild and run submit handlers. + node_form_submit_build_node($form, $form_state); + if (!isset($form_state['values']['choice'])) return; + + // Add new choices + if ($form_state['values']['choice']['morechoices']) { + $form_state['values']['choice']['choices'] = count($form_state['values']['choice']) + 1; // adding 1 + 2 elements + } +} + +/** + * Add a new choice to decisions form using AHAH menu callback + */ +function decisions_add_choices_js() { + + $form_build_id = $_POST['form_build_id']; + $form_id = $_POST['form_id']; + + $form_state = array('submitted' => FALSE); + $form = form_get_cache($form_build_id, $form_state); + + // Update related form data + $count = ++$form['choice']['choices']['#default_value']; + $form['settings']['maxchoices']['#options'] = _decisions_choice_list($count); + $form['choice'][$count]['label'] = _decisions_new_choice($count); + + form_set_cache($form_build_id, $form, $form_state); + + $form['#post'] = $_POST; + $form['#redirect'] = FALSE; + $form['#programmed'] = FALSE; + + $form = form_builder($form_id, $form, $form_state); + + $newchoice = $form['choice'][$count]; + $output = theme('status_messages') . drupal_render($newchoice); + + drupal_json(array('status' => TRUE, 'data' => $output)); +} + +/** * Implementation of hook_view(). */ function decisions_view(&$node, $teaser = FALSE, $page = FALSE) { @@ -216,6 +260,38 @@ function decisions_view(&$node, $teaser } /** + * Generate a new choice element in decisions form + * + * @param $index position of choice item in list + * @param $value default value of choice element + * @return new choice element + */ +function _decisions_new_choice($index, $value = NULL) { + + $newchoice = array( + '#type' => 'textfield', + '#title' => t('Choice @n', array('@n' => $index)), + '#default_value' => "$value", + '#attributes' => array('class' => 'choices'), + ); + + return $newchoice; +} +/* + * Generate maxchoices value list + * + * @param $choices number of choices to generate + * @return array of choices + */ +function _decisions_choice_list($choices) { + $max_choice_list = array(); + for ($i = 0; $i <= $choices; $i++) { + $max_choice_list[$i] = ($i == 0? 'No limit' : $i); + } + return $max_choice_list; +} + +/** * Implementation of hook_form(). * * This hook displays the form necessary to edit the *node* (ie. not the votes). @@ -242,47 +318,52 @@ function decisions_form(&$node, &$form_s '#required' => FALSE, '#default_value' => $node->body, ); - - if (isset($form_values['choices'])) { - $choices = $form_values['choices']; - } + + if (isset($form_state['values']['choice']['choices']) && is_numeric($form_state['values']['choice']['choices'])) { + $choices = max(2, $form_state['values']['choice']['choices']); + } else { $choices = max(2, isset($node->choice) && count($node->choice) ? count($node->choice) : 5); } + + $form['choice']['js'] = array( + '#type' => 'markup', + '#theme' => 'decisions_morechoices', + ); $form['choice']['choices'] = array( - '#type' => 'hidden', - '#default_value' => $choices - ); - + '#type' => 'hidden', + '#default_value' => $choices, + ); + $form['choice']['morechoices'] = array( - '#type' => 'checkbox', - '#title' => t('Need more choices'), - '#default_value' => 0, - '#return_value' => 1, - '#prefix' => '