/[drupal]/contributions/modules/customdestination/customdestination.module
ViewVC logotype

Contents of /contributions/modules/customdestination/customdestination.module

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


Revision 1.7 - (show annotations) (download) (as text)
Wed Jul 8 12:44:00 2009 UTC (4 months, 3 weeks ago) by flevour
Branch: MAIN
CVS Tags: DRUPAL-6--1-2, DRUPAL-6--1-3
Changes since 1.6: +2 -2 lines
File MIME type: text/x-php
Bug #513460 by flevour: Fixed broken validation on form ID.
1 <?php
2 // $Id: customdestination.module,v 1.6 2009/07/04 21:23:47 flevour Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function customdestination_help($path, $arg) {
8 $output = '';
9 switch ($path) {
10 case "admin/help#customdestination":
11 $output = '<p>'. t("Programmatically sets the destination a form redirects to upon submission") .'</p>';
12 break;
13 }
14 return $output;
15 }
16
17 /**
18 * Implementation of hook_menu
19 */
20 function customdestination_menu() {
21 $items['admin/settings/customdestination'] = array(
22 'title' => 'Custom destination',
23 'description' => 'List of custom destinations set',
24 'page callback' => 'customdestination_overview',
25 'access arguments' => array('access administration pages'),
26 );
27 $items['admin/settings/customdestination/list'] = array(
28 'title' => 'List',
29 'type' => MENU_DEFAULT_LOCAL_TASK,
30 'weight' => -10,
31 );
32 $items['admin/settings/customdestination/add'] = array(
33 'title' => 'Add custom destination',
34 'description' => 'Add a new custom destination',
35 'page callback' => 'customdestination_edit',
36 'access arguments' => array('access administration pages'),
37 'type' => MENU_LOCAL_TASK,
38 );
39 $items['admin/settings/customdestination/edit'] = array(
40 'title' => 'Edit',
41 'page callback' => 'customdestination_edit',
42 'access arguments' => array('access administration pages'),
43 'type' => MENU_CALLBACK,
44 );
45 $items['admin/settings/customdestination/delete'] = array(
46 'title' => 'Delete custom destination',
47 'page callback' => 'drupal_get_form',
48 'page arguments' => array('customdestination_delete_confirm'),
49 'access arguments' => array('access administration pages'),
50 'type' => MENU_CALLBACK,
51 );
52
53 return $items;
54 }
55
56 /**
57 * Menu callback; handles pages for creating and editing Custom destinations.
58 */
59 function customdestination_edit($fid = '') {
60 if (!empty($fid)) {
61 $customdestination = customdestination_load($fid);
62 drupal_set_title(check_plain($customdestination['fid']));
63 $output = drupal_get_form('customdestination_form', $customdestination);
64 }
65 else {
66 $output = drupal_get_form('customdestination_form');
67 }
68
69 return $output;
70 }
71
72 /**
73 * Return a form for editing or creating an individual custom destination.
74 *
75 * @ingroup forms
76 * @see customdestination_form_validate()
77 * @see customdestination_form_submit()
78 */
79 function customdestination_form(&$form_state, $edit = array('fid' => '', 'dst' => '')) {
80 $form['fid'] = array(
81 '#type' => 'textfield',
82 '#title' => t('Form ID'),
83 '#default_value' => $edit['fid'],
84 '#size' => 30,
85 '#description' => t("The ID of the form you want to change"),
86 '#required' => TRUE,
87 );
88 $form['dst'] = array(
89 '#type' => 'textfield',
90 '#title' => t('Destination'),
91 '#default_value' => $edit['dst'],
92 '#size' => 30,
93 '#description' => t("The destination to redirect to upon form submission"),
94 '#required' => TRUE,
95 );
96 if ($edit['fid']) {
97 // use hidden value here if can't usere INSERT OR UPDATE
98 $form['submit'] = array('#type' => 'submit', '#value' => t('Update custom destination'));
99 }
100 else {
101 $form['submit'] = array('#type' => 'submit', '#value' => t('Create new custom destination'));
102 }
103
104 return $form;
105 }
106
107 /**
108 * Save a new custom destination to the database.
109 */
110 function customdestination_form_submit($form, &$form_state) {
111 customdestination_save($form_state['values']['fid'], $form_state['values']['dst']);
112
113 drupal_set_message(t('The custom destination has been saved.'));
114 $form_state['redirect'] = 'admin/settings/customdestination';
115 return;
116 }
117
118 /**
119 * Verify that a new URL alias is valid
120 */
121 function customdestination_form_validate($form, &$form_state) {
122 $fid = $form_state['values']['fid'];
123 $dst = $form_state['values']['dst'];
124
125 // check if form exists - NOT YET IMPLEMENTED
126 /* if (!_customdestination_form_exists($fid)) {
127 form_set_error('fid', t("The form id '@form_id' doesn't exist in the system.", array('@form_id' => $fid)));
128 } */
129
130 // check if form id is valid
131 if (!preg_match("/[a-z_0-9]+/", $fid)) {
132 form_set_error('fid', t("The form '@form_id' is invalid. A valid form ID contains only alphanumeric characters and underscores.", array('@form_id' => $fid)));
133 }
134
135 // check if destination exists and is accessible by the current user
136 if (!menu_valid_path(array('link_item' => $dst))) {
137 form_set_error('dst', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $dst)));
138 }
139 }
140
141 /**
142 * Checks whether a form exists or not
143 * @param $form_id
144 * The form id to validate
145 * @return
146 * Returns TRUE for success, FALSE if $form_id doesn't exist
147 */
148 function _customdestination_form_exists($form_id) {
149 static $forms;
150 if (function_exists($form_id)) {
151 return TRUE;
152 }
153 else { // function doesn't exist
154 if (!isset($forms) || !isset($forms[$form_id])) {
155 $forms = module_invoke_all('forms', $form_id, array());
156 }
157 if (isset($forms[$form_id]) &&
158 isset($forms[$form_id]['callback']) &&
159 function_exists($forms[$form_id]['callback'])) {
160 return TRUE;
161 }
162 }
163
164 return FALSE;
165 }
166
167 /**
168 * Menu callback; confirms deleting a custom destination
169 */
170 function customdestination_delete_confirm($form_state, $fid) {
171 $path = customdestination_load($fid);
172 if (user_access('access administration pages')) {
173 $form['fid'] = array('#type' => 'value', '#value' => $fid);
174 $output = confirm_form($form,
175 t('Are you sure you want to delete custom destination for %title?', array('%title' => $path['fid'])),
176 isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/customdestination');
177 }
178 return $output;
179 }
180
181 /**
182 * Execute URL alias deletion
183 */
184 function customdestination_delete_confirm_submit($form, &$form_state) {
185 if ($form_state['values']['confirm']) {
186 customdestination_delete($form_state['values']['fid']);
187 $form_state['redirect'] = 'admin/settings/customdestination';
188 return;
189 }
190 }
191
192 /**
193 * Fetch a specific custom destination from the database.
194 */
195 function customdestination_load($fid) {
196 return db_fetch_array(db_query("SELECT * FROM {customdestination} WHERE fid = '%s'", $fid));
197 }
198
199 /**
200 * Fetch a specific custom destination from the database.
201 */
202 function customdestination_delete($fid) {
203 return db_query("DELETE FROM {customdestination} WHERE fid = '%s'", $fid);
204 }
205
206 /**
207 * Save a custom destination to the database.
208 */
209 function customdestination_save($fid, $dst) {
210 $customdestination = customdestination_load($fid);
211 if (!empty($customdestination)) {
212 // There is already a custom destination defined for this fid
213 // Update the entry with new dst
214 db_query("UPDATE {customdestination} SET dst = '%s' WHERE fid = '%s'", $dst, $fid);
215 }
216 else {
217 // A new custom destination. Add it to the database.
218 db_query("INSERT INTO {customdestination} (fid, dst) VALUES ('%s', '%s')", $fid, $dst);
219 }
220 }
221
222 /**
223 * Return a listing of all defined URL aliases.
224 * When filter key passed, perform a standard search on the given key,
225 * and return the list of matching URL aliases.
226 */
227 function customdestination_overview() {
228 $output = '';
229 $sql = 'SELECT * FROM {customdestination}';
230 $header = array(
231 array('data' => t('Form ID'), 'field' => 'fid', 'sort' => 'asc'),
232 array('data' => t('Destination'), 'field' => 'dst'),
233 array('data' => t('Operations'), 'colspan' => '2')
234 );
235
236 $sql .= tablesort_sql($header);
237 $result = pager_query($sql, 50, 0 , NULL);
238
239 $rows = array();
240 $destination = drupal_get_destination();
241 while ($data = db_fetch_object($result)) {
242 $row = array(check_plain($data->fid), check_plain($data->dst), l(t('edit'), "admin/settings/customdestination/edit/$data->fid", array('query' => $destination)), l(t('delete'), "admin/settings/customdestination/delete/$data->fid", array('query' => $destination)));
243 $rows[] = $row;
244 }
245
246 if (empty($rows)) {
247 $rows[] = array(array('data' => t('No custom destinations found.'), 'colspan' => 4));
248 }
249
250 $output .= theme('table', $header, $rows);
251 $output .= theme('pager', NULL, 50, 0);
252
253 return $output;
254 }
255
256 /**
257 * The core of the module, form_alter #redirect value with custom destination
258 */
259 function customdestination_form_alter(&$form, $form_state, $form_id) {
260 $customdestination = customdestination_load($form_id);
261 if (!empty($customdestination)) {
262 // dpr($form); dpr($form_state);
263 $form['#redirect'] = $customdestination['dst'];
264 }
265 }
266

  ViewVC Help
Powered by ViewVC 1.1.2