# HG changeset patch # User Franck Deroche # Date 1285229039 -7200 # Node ID 63b5c6ecc0e59fe7a0f85fe499faaa256b887f82 # Parent 21e600454f9a22756d11e02d22ea02b3649e7df0 # HG changeset patch # Parent 21e600454f9a22756d11e02d22ea02b3649e7df0 Decisions: Make the node_save(node_load(X)) construct works. Both decisions_inserts and decisions_updates assumes that their values come from a form submission, and thus use the values as they would appear in a form api submit callback. That's not always the case though, and a lot of both contrib and even core assumes that a basic construct around the lines of $node = node_load(NID); // Do something with $node node_save($node); is a safe way to update node properties. That's the second version of the patch, taking a different approach of the problem: get rid of the #tree properties in the node_form, to make them match the variables defined by decisions_load. diff --git a/decisions.module b/decisions.module --- a/decisions.module +++ b/decisions.module @@ -1077,6 +1077,7 @@ '#type' => 'markup', '#value' => '', ); + $form['#tree'] = TRUE; return $form; } @@ -1197,6 +1198,8 @@ } $decision->choices = count($decision->choice); $decision->vote = decisions_get_vote($node->nid, $user->uid); + $decision->noenddate = ($decision->runtime == DECISIONS_RUNTIME_INFINITY); + $decision->enddate = $decision->startdate + $decision->runtime; return $decision; } @@ -1220,12 +1223,12 @@ */ function decisions_insert($node) { // Compute startdate and runtime. - $startdate = _decisions_translate_form_date($node->settings['date']['startdate']['date']); - if ($node->settings['date']['noenddate']) { + $startdate = _decisions_translate_form_date($node->startdate); + if ($node->noenddate) { $runtime = DECISIONS_RUNTIME_INFINITY; } else { - $enddate = _decisions_translate_form_date($node->settings['date']['enddate']['date']); + $enddate = _decisions_translate_form_date($node->enddate); if ($enddate < $startdate) { form_set_error('enddate', t('The specified close date is less than the opening date, setting it to the same for now.')); $enddate = $startdate; @@ -1236,11 +1239,11 @@ // just create an empty entry for now $mode = _decisions_get_mode($node); - db_query("INSERT INTO {decisions} (nid, mode, quorum_abs, quorum_percent, uselist, active, runtime, maxchoices, algorithm, startdate, randomize) VALUES (%d, '%s', %d, %f, %d, %d, %d, %d, '%s', %d, %d)", $node->nid, $mode, $node->settings['quorum']['quorum_abs'], $node->settings['quorum']['quorum_percent'], $node->settings['uselist'], $node->settings['active'], $runtime, $node->settings['maxchoices'], $node->settings['algorithm'], $startdate, $node->settings['randomize']); + db_query("INSERT INTO {decisions} (nid, mode, quorum_abs, quorum_percent, uselist, active, runtime, maxchoices, algorithm, startdate, randomize) VALUES (%d, '%s', %d, %f, %d, %d, %d, %d, '%s', %d, %d)", $node->nid, $mode, $node->quorum_abs, $node->quorum_percent, $node->uselist, $node->active, $runtime, $node->maxchoices, $node->algorithm, $startdate, $node->randomize); // create the electoral list if desired - if ($node->settings['uselist']) { + if ($node->uselist) { _decisions_electoral_list_reset($node); } @@ -1273,9 +1276,9 @@ form_set_error("choice][$realchoices][label", t('You must fill in at least two choices.')); } - $startdate = _decisions_translate_form_date($node->settings['date']['startdate']['date']); - $enddate = _decisions_translate_form_date($node->settings['date']['enddate']['date']); - if (!$node->settings['date']['noenddate'] && $enddate < $startdate) { + $startdate = _decisions_translate_form_date($node->startdate); + $enddate = _decisions_translate_form_date($node->enddate); + if (!$node->noenddate && $enddate < $startdate) { form_set_error('enddate', t('The specified close date is less than the opening date.')); } } @@ -1287,12 +1290,12 @@ */ function decisions_update($node) { // Compute startdate and runtime. - $startdate = _decisions_translate_form_date($node->settings['date']['startdate']['date']); - if ($node->settings['date']['noenddate']) { + $startdate = _decisions_translate_form_date($node->startdate); + if ($node->noenddate) { $runtime = DECISIONS_RUNTIME_INFINITY; } else { - $enddate = _decisions_translate_form_date($node->settings['date']['enddate']['date']); + $enddate = _decisions_translate_form_date($node->enddate); if ($enddate < $startdate) { form_set_error('enddate', t('The specified close date is less than the opening date, setting it to the same for now.')); $enddate = $startdate; @@ -1301,9 +1304,9 @@ } $uselist = db_result(db_query("SELECT uselist FROM {decisions} WHERE nid = %d", $node->nid)); - if ($node->settings['uselist'] != $uselist) { + if ($node->uselist != $uselist) { // create the electoral list if activated - if ($node->settings['uselist']) { + if ($node->uselist) { _decisions_electoral_list_reset($node); } // remove otherwise @@ -1312,7 +1315,7 @@ } } - db_query("UPDATE {decisions} SET quorum_abs=%d, quorum_percent=%f, active=%d, runtime=%d, maxchoices=%d, algorithm='%s', uselist=%d, showvotes=%d, startdate=%d, randomize=%d WHERE nid = %d", $node->settings['quorum']['quorum_abs'], $node->settings['quorum']['quorum_percent'], $node->settings['active'], $runtime, $node->settings['maxchoices'], $node->settings['algorithm'], $node->settings['uselist'], $node->settings['showvotes'], $startdate, $node->settings['randomize'], $node->nid); + db_query("UPDATE {decisions} SET quorum_abs=%d, quorum_percent=%f, active=%d, runtime=%d, maxchoices=%d, algorithm='%s', uselist=%d, showvotes=%d, startdate=%d, randomize=%d WHERE nid = %d", $node->quorum_abs, $node->quorum_percent, $node->active, $runtime, $node->maxchoices, $node->algorithm, $node->uselist, $node->showvotes, $startdate, $node->randomize, $node->nid); // XXX: should update decisions here, when it has some parameters // XXX: ... but before doing so, the code below must be factored out in a seperate function for usage in decisions_insert() db_query('DELETE FROM {decisions_choices} WHERE nid = %d', $node->nid); @@ -1571,7 +1574,6 @@ $form['settings'] = array( '#type' => 'fieldset', - '#tree' => TRUE, '#title' => t('Decision settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, @@ -1661,7 +1663,7 @@ '#type' => 'fieldset', '#title' => t('Opening date'), ); - $form['settings']['date']['startdate']['date'] = _decisions_form_date($startdate); + $form['settings']['date']['startdate']['startdate'] = _decisions_form_date($startdate); $form['settings']['date']['noenddate'] = array( '#type' => 'checkbox', @@ -1678,12 +1680,11 @@ '#attributes' => array('id' => 'enddate'), '#title' => t('Closing date'), ); - $form['settings']['date']['enddate']['date'] = _decisions_form_date($enddate); + $form['settings']['date']['enddate']['enddate'] = _decisions_form_date($enddate); $form['settings']['quorum'] = array( '#type' => 'fieldset', - '#tree' => TRUE, '#title' => t('Quorum'), '#collapsible' => TRUE, '#collapsed' => TRUE, @@ -1733,7 +1734,13 @@ * @param $date The name of the date ('decisions_open' or 'decisions_close') to translate. */ function _decisions_translate_form_date($array) { - return _decisions_mktime($array['hour'], $array['minute'], 59, $array['month'], $array['day'], $array['year'], 0); + if (is_array($array)) { + $timestamp = _decisions_mktime($array['hour'], $array['minute'], 59, $array['month'], $array['day'], $array['year'], 0); + } + else { + $timestamp = $array; + } + return $timestamp; } /** @@ -1857,4 +1864,4 @@ 'content_id_column' => 'nid', ); return $relationships; -} \ No newline at end of file +}