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

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

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


Revision 1.7 - (show annotations) (download) (as text)
Sat Sep 19 00:31:56 2009 UTC (2 months, 1 week ago) by dww
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +19 -15 lines
File MIME type: text/x-php
#581734 by dww: Improved the Signup API by adding some much needed hooks:
- hook_signup_data_alter() to modify a $signup object before it's saved
- hook_signup_insert() invoked when a signup is inserted into the DB
- hook_signup_update() invoked when a signup is updated in the DB
1 <?php
2 // $Id: signup_edit_form.inc,v 1.6 2009/03/02 17:25:47 dww Exp $
3
4
5 /**
6 * @file
7 * Code for the form to edit existing signups.
8 */
9
10 /**
11 * Build the form for editing existing signups.
12 *
13 * @param $form_state
14 * The state of the form to build (not currently used).
15 * @param $signup
16 * The fully-loaded signup object with data about the signup to edit.
17 * @param $type
18 * The type of signup edit form to render, can be 'node', 'tab', or 'admin'.
19 *
20 * @return
21 * The FAPI form array for the signup edit form.
22 */
23 function signup_edit_form($form_state, $signup, $type) {
24 global $user;
25 $form = array();
26
27 $form['#signup'] = $signup;
28 $node = node_load($signup->nid);
29
30 // Check permissions.
31 $admin = _signup_menu_access($node, 'admin');
32 $own = !empty($user->uid) && $user->uid == $signup->uid;
33 $can_cancel = $admin || (user_access('cancel own signups') && $own);
34 $can_edit = $admin || (user_access('edit own signups') && $own);
35
36 if ($type == 'admin') {
37 $title = t("Information for !user's signup to !node", array('!user' => _signup_get_username($signup, TRUE), '!node' => l($node->title, 'node/'. $node->nid)));
38 }
39 else {
40 $title = t('Your signup information');
41 }
42
43 if ($type == 'node') {
44 $form['elements'] = array(
45 '#type' => 'fieldset',
46 '#title' => $title,
47 '#collapsible' => TRUE,
48 '#collapsed' => variable_get('signup_fieldset_collapsed', 1),
49 );
50 }
51 else {
52 $form['elements'] = array();
53 $form['elements']['header'] = array(
54 '#type' => 'markup',
55 '#value' => $title,
56 '#prefix' => '<h4>',
57 '#suffix' => '</h4>',
58 );
59 }
60
61 if (!empty($signup->anon_mail)) {
62 $form['elements']['signup_anon_mail'] = array(
63 '#type' => 'textfield',
64 '#title' => t('Email'),
65 '#default_value' => $signup->anon_mail,
66 '#size' => 40,
67 '#maxlength' => 255,
68 '#required' => TRUE,
69 );
70 $form['#validate'][] = 'signup_validate_anon_mail';
71 }
72
73 if ($admin) {
74 $options = array();
75 if (1|| !isset($signup->attended)) {
76 $options[-1] = t('- Not recorded -');
77 }
78 $options[1] = theme('signup_attended_text', 1);
79 $options[0] = theme('signup_attended_text', 0);
80 $form['elements']['attended'] = array(
81 '#type' => 'select',
82 '#title' => t('Attendance'),
83 '#default_value' => isset($signup->attended) ? $signup->attended : -1,
84 '#options' => $options
85 );
86 }
87
88 // Build the themed signup form for this site and include that.
89 $site_form = theme('signup_user_form', $node);
90 $form_data = unserialize($signup->form_data);
91
92 // This is sort of a hack, but we don't support nested arrays for the custom
93 // signup form anyway, so it works for now. Obviously all this will change
94 // with signup_fields and friends, but for now it works.
95 foreach ($form_data as $key => $value) {
96 if (!empty($site_form['signup_form_data'][$key])) {
97 $site_form['signup_form_data'][$key]['#default_value'] = $value;
98 if (!$can_edit) {
99 // If they can't edit, mark all the fields as disabled.
100 $site_form['signup_form_data'][$key]['#disabled'] = TRUE;
101 }
102 }
103 }
104 $form['elements'] += $site_form;
105
106 // Add the appropriate buttons based on permissions.
107 if ($can_edit) {
108 $form['elements']['save'] = array(
109 '#type' => 'submit',
110 '#value' => t('Save'),
111 '#submit' => array('signup_edit_form_save_submit'),
112 );
113 }
114 if ($can_cancel) {
115 if (isset($_REQUEST['destination'])) {
116 $destination = drupal_get_destination();
117 }
118 else {
119 // If there's no destination already set, redirect to the node.
120 $destination = 'destination='. urlencode("node/$signup->nid");
121 }
122 $signup_token = signup_get_token($signup->sid, 'cancel');
123 $form['elements']['cancel-signup'] = array(
124 '#type' => 'markup',
125 '#value' => l(t('Cancel signup'), "signup/cancel/$signup->sid/$signup_token", array('query' => $destination)),
126 );
127 }
128
129 return $form;
130 }
131
132 /**
133 * Validation callback when editing the anonymous email for an existing signup.
134 */
135 function signup_validate_anon_mail($form, &$form_state) {
136 $mail = $form_state['values']['signup_anon_mail'];
137 if (!valid_email_address($mail)) {
138 form_set_error('signup_anon_mail', t('The e-mail address %mail is not valid.', array('%mail' => $mail)));
139 }
140 }
141
142 /**
143 * Submit callback when saving changes to an existing signup.
144 */
145 function signup_edit_form_save_submit($form, $form_state) {
146 $signup = $form['#signup'];
147 if (!empty($form_state['values']['signup_form_data'])) {
148 $signup->form_data = $form_state['values']['signup_form_data'];
149 }
150
151 // If the form contains an e-mail address for an anonymous signup, save it.
152 if (!empty($form_state['values']['signup_anon_mail'])) {
153 $signup->anon_mail = $form_state['values']['signup_anon_mail'];
154 }
155
156 // If the form contains attendance info, save it.
157 if (isset($form_state['values']['attended'])) {
158 if ($form_state['values']['attended'] == -1) {
159 unset($signup->attended);
160 }
161 else {
162 $signup->attended = $form_state['values']['attended'];
163 }
164 }
165
166 // Invoke hook_signup_data_alter() to let other modules change this.
167 drupal_alter('signup_data', $signup, $form_state['values']);
168
169 // Update the signup in the {signup_log} table.
170 signup_save_signup($signup);
171
172 // Because drupal_write_record() doesn't gracefully handle columns that can
173 // be NULL, if the attendence was cleared out by this edit, we need to
174 // manually set the DB record to NULL here.
175 if (!isset($signup->attended)) {
176 db_query("UPDATE {signup_log} SET attended = NULL WHERE sid = %d", $signup->sid);
177 }
178
179 drupal_set_message(t('Signup information updated.'));
180 }
181
182 /**
183 * Page handler for the administrator page to edit an existing signup.
184 *
185 * @param $signup
186 * The fully-loaded signup object to edit.
187 *
188 * @return
189 * The HTML to use for the signup edit page.
190 */
191 function signup_edit_page($signup) {
192 drupal_set_title(t('Edit signup'));
193 return drupal_get_form('signup_edit_form', $signup, 'admin');
194 }
195

  ViewVC Help
Powered by ViewVC 1.1.2