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

Contents of /contributions/modules/alias/alias.module

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Nov 24 22:45:31 2008 UTC (11 months, 4 weeks ago) by rz
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
30 minute modules presents... Alias module
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Create URL aliases for external URLs.
7 */
8
9 /**
10 * Implementation of hook_menu().
11 */
12 function alias_menu() {
13 $items['admin/build/path/alias'] = array(
14 'type' => MENU_LOCAL_TASK,
15 'title' => 'External Aliases',
16 'page callback' => 'alias_admin_overview',
17 'access arguments' => array('administer url aliases'),
18 );
19 $items['admin/build/path/alias/list'] = array(
20 'type' => MENU_DEFAULT_LOCAL_TASK,
21 'title' => 'List',
22 'access arguments' => array('administer url aliases'),
23 'weight' => -1,
24 );
25 $items['admin/build/path/alias/add'] = array(
26 'type' => MENU_LOCAL_TASK,
27 'title' => 'Add',
28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('alias_admin_edit_form'),
30 'access arguments' => array('administer url aliases'),
31 );
32 $items['admin/build/path/alias/edit/%alias'] = array(
33 'type' => MENU_LOCAL_TASK,
34 'title' => 'Edit',
35 'page callback' => 'drupal_get_form',
36 'page arguments' => array('alias_admin_edit_form', 5),
37 'access arguments' => array('administer url aliases'),
38 );
39 $items['admin/build/path/alias/delete/%alias'] = array(
40 'type' => MENU_CALLBACK,
41 'title' => 'Delete',
42 'page callback' => 'drupal_get_form',
43 'page arguments' => array('alias_admin_delete_form', 5),
44 'access arguments' => array('administer url aliases'),
45 );
46
47 $result = db_query('SELECT * FROM {alias}');
48 while($row = db_fetch_object($result)) {
49 $items[$row->path] = array(
50 'type' => MENU_CALLBACK,
51 'access callback' => TRUE,
52 'page callback' => 'drupal_goto',
53 'page arguments' => array($row->url, NULL, NULL, $row->code),
54 );
55 }
56
57 return $items;
58 }
59
60 /**
61 * Menu object load callback
62 */
63 function alias_load($id) {
64 return db_fetch_object(db_query('SELECT * FROM {alias} WHERE id = %d', $id));
65 }
66
67 /**
68 * Listing of existing path aliass
69 */
70 function alias_admin_overview() {
71 $rows = array();
72
73 $result = db_query('SELECT * FROM {alias}');
74 while($row = db_fetch_object($result)) {
75 $ops = array(
76 l('Edit', 'admin/build/path/alias/edit/'. $row->id, array('query' => drupal_get_destination())),
77 l('Delete', 'admin/build/path/alias/delete/'. $row->id, array('query' => drupal_get_destination())),
78 );
79
80 $rows[] = array(
81 l($row->path, $row->path),
82 check_plain($row->url),
83 check_plain($row->code),
84 join(' | ', $ops),
85 );
86 }
87
88 $header = array(
89 t('Drupal path'),
90 t('Redirect URL'),
91 t('Response code'),
92 t('Operations'),
93 );
94
95 return theme('table', $header, $rows);
96 }
97
98 /**
99 * Add/Edit form
100 */
101 function alias_admin_edit_form(&$form_state, $alias = NULL) {
102 $form['id'] = array(
103 '#type' => 'value',
104 '#value' => $alias->id ? $alias->id : NULL,
105 );
106
107 $form['path'] = array(
108 '#type' => 'textfield',
109 '#title' => t('Drupal path'),
110 '#default_value' => $alias->path ? $alias->path : '',
111 );
112
113 $form['url'] = array(
114 '#type' => 'textfield',
115 '#title' => t('Redirect URL'),
116 '#default_value' => $alias->url ? $alias->url : '',
117 );
118
119 $form['code'] = array(
120 '#type' => 'select',
121 '#title' => 'HTTP response code',
122 '#default_value' => $alias->code ? $alias->code : 302,
123 '#options' => array(
124 301 => t('301: Moved Permanently'),
125 302 => t('302: Found'),
126 303 => t('303: See Other'),
127 304 => t('304: Not Modified'),
128 305 => t('305: Use Proxy'),
129 307 => t('307: Temporary Redirect'),
130 ),
131 );
132
133 $form['submit'] = array(
134 '#type' => 'submit',
135 '#value' => t('Submit'),
136 '#weight' => 10,
137 );
138
139 return $form;
140 }
141
142 /**
143 * Add/Edit form submit
144 */
145 function alias_admin_edit_form_submit($form, &$form_state) {
146 $alias = (object)$form_state['values'];
147
148 if($alias->id) {
149 drupal_write_record('alias', $alias, 'id');
150 }
151 else {
152 drupal_write_record('alias', $alias);
153 }
154
155 menu_rebuild();
156
157 $form_state['redirect'] = 'admin/build/path/alias';
158 }
159
160 /**
161 * Delete form
162 */
163 function alias_admin_delete_form(&$form_state, $alias) {
164 $form['id'] = array(
165 '#type' => 'value',
166 '#value' => $alias->id,
167 );
168
169 return confirm_form($form,
170 t('Are you sure you want delete this alias?'),
171 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path/alias',
172 t('%path redirects to %url. This action cannot be undone.', array('%path' => $alias->path, '%url' => $alias->url)),
173 t('Delete'),
174 t('Cancel')
175 );
176 }
177
178 /**
179 * Delete form submit
180 */
181 function alias_admin_delete_form_submit($form, &$form_state) {
182 db_query('DELETE FROM {alias} WHERE id = %d', $form_state['values']['id']);
183
184 menu_rebuild();
185
186 $form_state['redirect'] = 'admin/build/path/alias';
187 }

  ViewVC Help
Powered by ViewVC 1.1.2