2 // $Id: node_form.inc,v 1.4 2008/12/14 07:36:43 dww Exp $
7 * Signup-related code needed while editing a node.
11 * Save signup-related information when a node is created or edited.
13 * This is a helper function invoked via signup_nodeapi(). It ensures that
14 * the currently selected signup values are properly saved into the database.
15 * If the node is signup-enabled, the per-node configuration values are saved
16 * to the {signup} table. If signups are disabled, the record from {signup} is
17 * cleared. If the signup administrator editing the node decided to remove
18 * all signup data, all the records from the {signup_log} table for this node
19 * are also removed. This function is also responsible for testing if the
20 * node * has a start time and if the autoclose period has already begun, in
21 * which case signups are closed. Finally, if the signup limit was changed
22 * while editing the node, the function compares the limit against the current
23 * total number of signups and opens or closes signups as appropriate.
26 * The node object given to signup_nodeapi().
28 * The hook_nodeapi() operation, either 'insert' or 'update'.
31 * Nothing, this function is expected to update the database.
33 * @see signup_nodeapi()
35 function signup_save_node($node, $op) {
36 // See if the form indicates that signups are enabled on this node.
37 if (isset($node->signup_enabled)) {
38 if ($node->signup_enabled == 1) {
40 $node->signup_forwarding_email,
41 $node->signup_send_confirmation,
42 $node->signup_confirmation_email,
43 $node->signup_close_signup_limit,
45 // If we're dealing with a node that doesn't have a start time, these
46 // fields are missing from the signup settings form, so we can't assume
48 $values[] = isset($node->signup_send_reminder) ? $node->signup_send_reminder : 0;
49 $values[] = isset($node->signup_reminder_days_before) ? $node->signup_reminder_days_before : 0;
50 $values[] = isset($node->signup_reminder_email) ? $node->signup_reminder_email : '';
53 elseif ($op == 'insert' && variable_get('signup_node_default_state_'. $node->type, 'disabled') == 'enabled_on') {
54 // The form doesn't include any information about signups, but the node
55 // type is signup-enabled. This would happen if a user without any signup
56 // admin permissions creates a node that has been signup-enabled based on
57 // the node type. In this case, we use the site-wide default signup
59 $defaults = db_fetch_object(db_query("SELECT * from {signup} WHERE nid = 0"));
61 $defaults->forwarding_email,
62 $defaults->send_confirmation,
63 $defaults->confirmation_email,
64 $defaults->close_signup_limit,
65 $defaults->send_reminder,
66 $defaults->reminder_days_before,
67 $defaults->reminder_email,
72 // If $values is set, we need to save them to the DB.
74 // Before we update the DB, see if the limit is changing, so we can take
75 // appropriate action after we update to the new settings.
76 $has_signup_record = FALSE;
77 $limit_changed = FALSE;
78 if ($op == 'update') {
79 $signup = db_fetch_object(db_query("SELECT close_signup_limit, status FROM {signup} WHERE nid = %d", $node->nid));
80 if ($signup !== FALSE) {
81 $cur_limit = $signup->close_signup_limit;
82 $node->signup_status = $signup->status;
83 $has_signup_record = TRUE;
84 $limit_changed = $cur_limit != $node->signup_close_signup_limit;
87 // Newly signup-enabled node.
88 $node->signup_status = 1;
92 // See if we need to update an existing record or insert a new one.
93 // Either way, we always the nid as the final value. The nid will either
94 // be used as the last column in the INSERT, or the argument to the WHERE
95 // clause for the UPDATE.
96 $values[] = $node->nid;
97 if ($has_signup_record) {
98 db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', close_signup_limit = %d, send_reminder = %d, reminder_days_before = %d, reminder_email = '%s' WHERE nid = %d", $values);
101 db_query("INSERT INTO {signup} (forwarding_email, send_confirmation, confirmation_email, close_signup_limit, send_reminder, reminder_days_before, reminder_email, nid) VALUES ('%s', %d, '%s', %d, %d, %d, '%s', %d)", $values);
103 if (_signup_node_completed($node) && !empty($node->signup_status)) {
104 // If this is an time-based node, and it's already past the close in
105 // advance time, and signups are still open, close them now (and don't
106 // consider the limit for changing the status).
107 signup_close_signup($node->nid);
108 drupal_set_message(t('%node_type start time is already past the signup close-in-advance time, signups now closed.', array('%node_type' => node_get_types('name', $node->type))));
110 elseif ($limit_changed) {
111 $node->signup_total = db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE nid = %d", $node->nid));
112 _signup_check_limit($node, 'limit');
115 elseif ($op == 'update' && isset($node->signup_enabled)) {
116 // $values was not set, because signups are now disabled on this node.
117 switch ($node->signup_enabled) {
118 case 2: // Disabled, and delete {signup_log}, too
119 db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);
120 // No break, fall through and remove from {signup} too.
121 case 0: // Disabled, but leave {signup_log} alone
122 db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
129 * Alters the node form to inject the appropriate per-node signup settings.
131 function signup_alter_node_form(&$form, &$form_state, $form_id) {
134 // Load the node if it already exists.
135 if (!empty($form['nid']['#value'])) {
136 $node = node_load($form['nid']['#value']);
141 $node_type = $form['type']['#value'];
143 $signup_type_default = variable_get('signup_node_default_state_'. $node_type, 'disabled');
145 $node_scheduler = _signup_get_node_scheduler($node);
148 $node_scheduler = _signup_get_node_type_scheduler($node_type);
150 $node_has_date = $node_scheduler != 'none';
152 // If signups are possible, and the current user either has the global
153 // 'administer all signups' permission or has the 'administer signups
154 // for own content' permission and is creating new content or editing
155 // their own content, add a fieldset for signup-related settings.
157 // Signups are possible if they're not explicitly disallowed for this
158 // node type, or if this node is already signup-enabled (in case an
159 // admin erroneously marks a node-type to disallow signups when there
160 // are already nodes of that type with signups enabled).
161 $signups_possible = $signup_type_default != 'disabled' || (!empty($node) && !empty($node->signup));
162 $admin_all = user_access('administer all signups');
163 $admin_own = user_access('administer signups for own content') && (empty($node) || ($node->uid == $user->uid));
164 if ($signups_possible && ($admin_all || $admin_own)) {
165 $form['signup'] = array(
166 '#type' => 'fieldset',
167 '#title' => t('Signup settings'),
168 '#collapsible' => TRUE,
169 '#collapsed' => TRUE,
173 // Figure out what the options should be. If there are already
174 // people signed-up for this node, we need a 3rd choice: disable
175 // signups and remove all signup data.
176 $has_signups = !empty($node) && db_result(db_query("SELECT COUNT(*) from {signup_log} WHERE nid = %d", $node->nid));
177 $radio_options[1] = t('Enabled');
179 $radio_options[0] = t('Disabled, but save existing signup information');
180 $radio_options[2] = t('Disabled, and remove all signup information') .' <strong>('. t('This can not be undone, use with extreme caution!') .')</strong>';
183 $radio_options[0] = t('Disabled');
186 // Figure out what the default selection for signups should be.
187 if (isset($node->signup)) {
188 $default_option = $node->signup;
191 $default_option = $signup_type_default == 'enabled_on' ? 1 : 0;
193 if ($default_option == 1) {
194 $hint = t('If enabled, you can control whether users may sign up by visiting the !signups tab and toggling if signups are %open or %closed for this %node_type.', array('!signups' => !empty($node) ? l(t('Signups'), 'node/'. $node->nid .'/signups') : theme('placeholder', t('Signups')), '%open' => t('open'), '%closed' => t('closed'), '%node_type' => node_get_types('name', $node_type)));
199 // Add the form element to toggle if signups are allowed.
200 $form['signup']['signup_enabled'] = array(
202 '#options' => $radio_options,
203 '#default_value' => $default_option,
204 '#description' => $hint .'<div class="js-hide">'. t('If disabled, all of the other signup settings will be ignored.') .'</div>',
205 '#prefix' => '<div class="signup-allow-radios">',
206 '#suffix' => '</div>',
209 // If JS is enabled, system.css will hide all the settings on page
210 // load if signups aren't enabled on this node.
211 $settings_class = "signup-node-settings";
212 if ($default_option != 1) {
213 $settings_class .= " js-hide";
216 // Add the actual settings. We wrap this in a div to make it easy
217 // to use jQuery to hide these settings when signups are disabled.
218 drupal_add_js(drupal_get_path('module', 'signup') .'/js/node_form.js');
219 $form['signup']['node_settings'] = array(
220 '#prefix' => '<div class="'. $settings_class .'">',
221 '#suffix' => '</div>',
223 module_load_include('inc', 'signup', 'includes/node_settings');
224 $form['signup']['node_settings']['settings'] = signup_node_settings_form(array(), $node, $node_type, $node_has_date);