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

  ViewVC Help
Powered by ViewVC 1.1.2