/[drupal]/contributions/modules/signup/includes/node_form.inc
ViewVC logotype

Contents of /contributions/modules/signup/includes/node_form.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Wed Sep 16 00:42:50 2009 UTC (2 months, 1 week ago) by dww
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +50 -127 lines
File MIME type: text/x-php
#578502 by dww: Moved signup-related settings from node/N/edit to a
new "Settings" subtab at node/N/signups/settings.
1 <?php
2 // $Id: node_form.inc,v 1.5 2009/01/08 19:36:08 dww Exp $
3
4
5 /**
6 * @file
7 * Signup-related code needed while editing a node.
8 */
9
10 /**
11 * Save signup-related information when a node is created or edited.
12 *
13 * This is a helper function invoked via signup_nodeapi(). If signups are
14 * disabled, the record from {signup} (if any) is cleared. If the signup
15 * administrator editing the node decided to remove all signup data, all the
16 * records from the {signup_log} table for this node are also removed. This
17 * function is also responsible for testing if the node has a start time and
18 * if the autoclose period has already begun, in which case signups are
19 * closed. If a new node is being saved, or an existing node is updated and is
20 * newly-enabled for signups, the site-wide default signup settings are copied
21 * into a record in the {signup} table for this node so that the node is
22 * properly signup-enabled (these settings can be changed by visiting the
23 * "Settings" subtab under the "Signups" tab at node/N/signups/settings).
24 *
25 * @param $node
26 * The node object given to signup_nodeapi().
27 * @param $op
28 * The hook_nodeapi() operation, either 'insert' or 'update'.
29 *
30 * @return
31 * Nothing, this function is expected to update the database.
32 *
33 * @see signup_nodeapi()
34 */
35 function signup_save_node($node, $op) {
36 // See if a user is editing a node and disables signups for it.
37 if ($op == 'update' && isset($node->signup_enabled)) {
38 switch ($node->signup_enabled) {
39 case 2: // Disabled, and delete {signup_log}, too
40 db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);
41 // No break, fall through and remove from {signup} too.
42 case 0: // Disabled, but leave {signup_log} alone
43 db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
44 // We're done.
45 return;
46 }
47 }
48
49 // If the form is configured to have signups enabled, or the form doesn't
50 // include that information at all but the node type defaults to have
51 // signups enabled (which would happen if a user without signup admin
52 // permission created a node that defaulted to have signups enabled based on
53 // the node type), see if we need to insert a new record into the {signup}
54 // table for this node using the site-wide defaults.
55 $needs_defaults = FALSE;
56 if ((isset($node->signup_enabled) && $node->signup_enabled == 1) || (!isset($node->signup_enabled) && variable_get('signup_node_default_state_'. $node->type, 'disabled') == 'enabled_on')) {
57 if ($op == 'insert') {
58 $needs_defaults = TRUE;
59 }
60 else {
61 // Updating -- see if we already have a record for this node.
62 $has_record = db_result(db_query("SELECT nid FROM {signup} WHERE nid = %d", $node->nid));
63 $needs_defaults = empty($has_record);
64 }
65 }
66
67 if ($needs_defaults) {
68 $values = db_fetch_array(db_query("SELECT forwarding_email, send_confirmation, confirmation_email, close_signup_limit, send_reminder, reminder_days_before, reminder_email FROM {signup} WHERE nid = 0"));
69 $values[] = $node->nid;
70 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);
71 }
72
73 $node = node_load($node->nid);
74 if (_signup_node_completed($node) && !empty($node->signup_status)) {
75 // If this is an time-based node, and it's already past the close in
76 // advance time (e.g. someone just changed the node start time), and
77 // signups are still open, close them now.
78 signup_close_signup($node->nid);
79 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))));
80 }
81 }
82
83 /**
84 * Alters the node form to inject the appropriate per-node signup settings.
85 */
86 function signup_alter_node_form(&$form, &$form_state, $form_id) {
87 global $user;
88
89 // Node is not saved but previewed (nid is empty).
90 if (isset($form['#node']->build_mode) && $form['#node']->build_mode == NODE_BUILD_PREVIEW) {
91 $node = $form['#node'];
92 }
93 // Load the node if it already exists.
94 elseif (!empty($form['nid']['#value'])) {
95 $node = node_load($form['nid']['#value']);
96 }
97 else {
98 $node = NULL;
99 }
100 $node_type = $form['type']['#value'];
101
102 $signup_type_default = variable_get('signup_node_default_state_'. $node_type, 'disabled');
103
104 // If signups are possible, and the current user either has the global
105 // 'administer all signups' permission or has the 'administer signups
106 // for own content' permission and is creating new content or editing
107 // their own content, add a fieldset for signup-related settings.
108
109 // Signups are possible if they're not explicitly disallowed for this
110 // node type, or if this node is already signup-enabled (in case an
111 // admin erroneously marks a node-type to disallow signups when there
112 // are already nodes of that type with signups enabled).
113 $signups_possible = $signup_type_default != 'disabled' || (!empty($node) && !empty($node->nid) && !empty($node->signup));
114 $admin_all = user_access('administer all signups');
115 $admin_own = user_access('administer signups for own content') && (empty($node) || ($node->uid == $user->uid));
116 if ($signups_possible && ($admin_all || $admin_own)) {
117 $form['signup'] = array(
118 '#type' => 'fieldset',
119 '#title' => t('Signup settings'),
120 '#collapsible' => TRUE,
121 '#collapsed' => TRUE,
122 '#weight' => 30,
123 );
124
125 // Figure out what the options should be. If there are already
126 // people signed-up for this node, we need a 3rd choice: disable
127 // signups and remove all signup data.
128 $has_signups = !empty($node) && db_result(db_query("SELECT COUNT(*) from {signup_log} WHERE nid = %d", $node->nid));
129 $radio_options[1] = t('Enabled');
130 if ($has_signups) {
131 $radio_options[0] = t('Disabled, but save existing signup information');
132 $radio_options[2] = t('Disabled, and remove all signup information') .' <strong>('. t('This can not be undone, use with extreme caution!') .')</strong>';
133 }
134 else {
135 $radio_options[0] = t('Disabled');
136 }
137
138 // Figure out what the default selection for signups should be.
139 if (isset($node->signup)) {
140 $default_option = $node->signup;
141 }
142 else {
143 $default_option = $signup_type_default == 'enabled_on' ? 1 : 0;
144 }
145 // Add the form element to toggle if signups are allowed.
146 $form['signup']['signup_enabled'] = array(
147 '#type' => 'radios',
148 '#options' => $radio_options,
149 '#default_value' => $default_option,
150 '#description' => t('If enabled, you can control whether users may sign up by visiting the !signup_admin tab and toggling if signups are %open or %closed for this %node_type. Other signup-related settings can be defined at the !signup_settings tab.', array('!signup_admin' => !empty($node->signup) ? l(t('Signups: Administer'), 'node/'. $node->nid .'/signups/admin') : theme('placeholder', t('Signups: Administer')), '!signup_settings' => !empty($node->signup) ? l(t('Signups: Settings'), 'node/'. $node->nid .'/signups/settings') : theme('placeholder', t('Signups: Settings')), '%open' => t('open'), '%closed' => t('closed'), '%node_type' => node_get_types('name', $node_type))),
151 );
152 }
153 }
154

  ViewVC Help
Powered by ViewVC 1.1.2