/[drupal]/contributions/modules/sections/sections.admin.inc
ViewVC logotype

Contents of /contributions/modules/sections/sections.admin.inc

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


Revision 1.4 - (show annotations) (download) (as text)
Sun Jun 1 13:03:53 2008 UTC (17 months, 3 weeks ago) by hass
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +187 -61 lines
File MIME type: text/x-php
Sync D6 to HEAD
1 <?php
2 // $Id: sections.admin.inc,v 1.3.2.10 2008/06/01 12:51:51 hass Exp $
3
4 /**
5 * @file
6 * Administrative page callbacks for the sections module.
7 */
8
9 function sections_admin_settings_form(&$form_state, $section = NULL) {
10
11 if ($section) {
12 $edit = (array)$section;
13 }
14 else {
15 $edit = NULL;
16 }
17
18 $form['section_settings'] = array(
19 '#type' => 'fieldset',
20 '#title' => t('Section settings')
21 );
22 $form['section_settings']['name'] = array(
23 '#type' => 'textfield',
24 '#title' => t('Name'),
25 '#default_value' => $edit['name'],
26 '#size' => 40,
27 '#maxlength' => 64,
28 '#description' => t('Give the name of the section.')
29 );
30 $form['section_settings']['status'] = array(
31 '#type' => 'checkbox',
32 '#title' => t('Enabled'),
33 '#default_value' => $edit['status'],
34 '#description' => t('Enable or disable this section.')
35 );
36 $form['section_settings']['theme'] = array(
37 '#type' => 'select',
38 '#title' => t('Select theme'),
39 '#default_value' => $edit['theme'],
40 '#options' => _sections_theme_select(),
41 '#description' => t('Select the theme you want to use for this section. Disabled themes are not used until they are enabled on <a href="@url">themes</a> page.', array('@url' => url('admin/build/themes')))
42 );
43 $form['section_settings']['weight'] = array(
44 '#type' => 'weight',
45 '#title' => t('Weight'),
46 '#default_value' => $edit['weight'] ? $edit['weight'] : 0
47 );
48
49 // Role-based visibility settings.
50 $default_role_options = array();
51 $result = db_query('SELECT rid FROM {sections_roles} WHERE sid = %d', $edit['sid'] ? $edit['sid'] : 0);
52 while ($role = db_fetch_object($result)) {
53 $default_role_options[] = $role->rid;
54 }
55 $roles = user_roles();
56 $role_options = array();
57 foreach ($roles as $rid => $name) {
58 $role_options[$rid] = $name;
59 }
60 $form['role_vis_settings'] = array(
61 '#type' => 'fieldset',
62 '#title' => t('Role specific visibility settings'),
63 '#collapsible' => TRUE,
64 );
65 $form['role_vis_settings']['roles'] = array(
66 '#type' => 'checkboxes',
67 '#title' => t('Show section for specific roles'),
68 '#default_value' => $default_role_options,
69 '#options' => $role_options,
70 '#description' => t('Show this section only for the selected role(s). If you select no roles, the section will be visible to all users. If a user has any of the roles checked, the section will be visible for this user.'),
71 );
72
73 // Page specific visibility configurations.
74 $form['page_vis_settings'] = array(
75 '#type' => 'fieldset',
76 '#title' => t('Page specific visibility settings'),
77 '#collapsible' => TRUE,
78 );
79
80 $access = user_access('use PHP for block visibility');
81 $visibility = $edit['visibility'] ? $edit['visibility'] : 1;
82
83 if ($visibility == 2 && !$access) {
84 $form['page_vis_settings'] = array();
85 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
86 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['path']);
87 }
88 else {
89 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
90 $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
91
92 if ($access) {
93 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
94 $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
95 }
96 $form['page_vis_settings']['visibility'] = array(
97 '#type' => 'radios',
98 '#title' => t('Activate section on the specific pages'),
99 '#options' => $options,
100 '#default_value' => $visibility,
101 );
102 $form['page_vis_settings']['path'] = array(
103 '#type' => 'textarea',
104 '#title' => t('Pages'),
105 '#default_value' => $edit['path'],
106 '#description' => $description,
107 );
108 }
109
110 if (!empty($edit['sid'])) {
111 // We are updating a section.
112 drupal_set_title(t('Edit section %name', array('%name' => $section->name)));
113 $form['sid'] = array(
114 '#type' => 'hidden',
115 '#value' => $edit['sid']
116 );
117 $form['save'] = array(
118 '#type' => 'submit',
119 '#value' => t('Save'),
120 '#submit' => array('sections_admin_settings_form_save_function')
121 );
122 }
123 else {
124 // We are adding a new section.
125 drupal_set_title(t('Add section'));
126 $form['submit'] = array(
127 '#type' => 'submit',
128 '#value' => t('Add section')
129 );
130 }
131
132 return $form;
133 }
134
135 function sections_admin_settings_form_save_function($form, &$form_state) {
136 db_query("UPDATE {sections_data} SET name = '%s', status = %d, visibility = %d, theme = '%s', path = '%s', weight = %d WHERE sid = %d", $form_state['values']['name'], $form_state['values']['status'], $form_state['values']['visibility'], $form_state['values']['theme'], $form_state['values']['path'], $form_state['values']['weight'], $form_state['values']['sid']);
137 db_query("DELETE FROM {sections_roles} WHERE sid = %d", $form_state['values']['sid']);
138 foreach (array_filter($form_state['values']['roles']) as $rid) {
139 db_query("INSERT INTO {sections_roles} (rid, sid) VALUES (%d, %d)", $rid, $form_state['values']['sid']);
140 }
141 drupal_set_message(t('The sections configuration has been saved.'));
142 $form_state['redirect'] = 'admin/build/sections';
143 }
144
145 function sections_admin_settings_form_submit($form, &$form_state) {
146 db_query("INSERT INTO {sections_data} (name, status, visibility, path, theme, weight) VALUES ('%s', %d, %d, '%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['status'], $form_state['values']['visibility'], $form_state['values']['path'], $form_state['values']['theme'], $form_state['values']['weight']);
147 $sid = db_last_insert_id('sections_data', 'sid');
148 foreach (array_filter($form_state['values']['roles']) as $rid) {
149 db_query("INSERT INTO {sections_roles} (rid, sid) VALUES (%d, %d)", $rid, $sid);
150 }
151 drupal_set_message(t('The sections configuration has been saved.'));
152 $form_state['redirect'] = 'admin/build/sections';
153 }
154
155 function sections_delete_form(&$form_state, $section) {
156 $form['name'] = array('#type' => 'hidden', '#value' => $section->name);
157 $form['sid'] = array('#type' => 'hidden', '#value' => $section->sid);
158
159 return confirm_form(
160 $form,
161 t('Delete section %name', array('%name' => $section->name)),
162 'admin/build/sections',
163 '<p>'. t('Are you sure you want to delete the section %name?', array('%name' => $section->name)) .'</p>',
164 t('Delete'),
165 t('Cancel')
166 );
167 }
168
169 function sections_delete_form_submit($form, &$form_state) {
170 db_query('DELETE FROM {sections_data} WHERE sid = %d', $form_state['values']['sid']);
171 drupal_set_message(t('The section %name has been deleted.', array('%name' => $form_state['values']['name'])));
172 $form_state['redirect'] = 'admin/build/sections';
173 return;
174 }
175
176 /**
177 * Build the form for ordering sections.
178 */
179 function sections_admin_display_form(&$form_state) {
180 $sections = _sections_load();
181
182 // Create tree for submitted data.
183 $form['sections'] = array('#tree' => TRUE);
184
185 foreach ($sections as $section) {
186 $form['sections'][$section->sid]['sid'] = array(
187 '#type' => 'hidden',
188 '#value' => $section->sid
189 );
190 $form['sections'][$section->sid]['name'] = array(
191 '#value' => $section->name
192 );
193 $form['sections'][$section->sid]['status'] = array(
194 '#type' => 'checkbox',
195 '#default_value' => $section->status
196 );
197 $form['sections'][$section->sid]['theme'] = array(
198 '#value' => $section->theme
199 );
200 $form['sections'][$section->sid]['weight'] = array(
201 '#type' => 'weight',
202 '#default_value' => $section->weight
203 );
204 $form['sections'][$section->sid]['edit'] = array(
205 '#value' => l(t('Edit'), 'admin/build/sections/edit/'. $section->sid)
206 );
207 $form['sections'][$section->sid]['delete'] = array(
208 '#value' => l(t('Delete'), 'admin/build/sections/delete/'. $section->sid)
209 );
210 }
211 $form['submit'] = array(
212 '#type' => 'submit',
213 '#value' => t('Save configuration')
214 );
215
216 return $form;
217 }
218
219 /**
220 * Theme section order configuration form.
221 */
222 function theme_sections_admin_display_form($form) {
223 drupal_add_tabledrag('sections-order', 'order', 'sibling', 'sections-order-weight');
224
225 $header = array(
226 array('data' => t('Section')),
227 array('data' => t('Enabled')),
228 array('data' => t('Theme')),
229 array('data' => t('Weight')),
230 array('data' => t('Operations'), 'colspan' => '2')
231 );
232
233 $rows = array();
234 foreach (element_children($form['sections']) as $key => $id) {
235 // Don't take form control structures.
236 if (is_array($form['sections'][$id]['name'])) {
237 $form['sections'][$id]['weight']['#attributes']['class'] = 'sections-order-weight';
238 $rows[] = array(
239 'data' => array(
240 drupal_render($form['sections'][$id]['name']),
241 drupal_render($form['sections'][$id]['status']),
242 drupal_render($form['sections'][$id]['theme']),
243 drupal_render($form['sections'][$id]['weight']),
244 drupal_render($form['sections'][$id]['edit']),
245 drupal_render($form['sections'][$id]['delete'])
246 ),
247 'class' => 'draggable',
248 );
249 }
250 }
251 if (empty($rows)) {
252 $rows[] = array(array('data' => t('No sections available.'), 'colspan' => '6'));
253 }
254
255 $output = theme('table', $header, $rows, array('id' => 'sections-order'));
256 $output .= drupal_render($form);
257 return $output;
258 }
259
260 /**
261 * Process sections order configuration form submission.
262 */
263 function sections_admin_display_form_submit($form, &$form_state) {
264 foreach ($form_state['values']['sections'] as $section) {
265 db_query("UPDATE {sections_data} SET status = %d, weight = %d WHERE sid = %d", $section['status'], $section['weight'], $section['sid']);
266 }
267 drupal_set_message(t('The sections configuration has been saved.'));
268 }
269
270 /**
271 * Load all sections.
272 */
273 function _sections_load() {
274 $res = db_query('SELECT * FROM {sections_data} ORDER BY weight');
275 $sections = array();
276 while ($row = db_fetch_object($res)) {
277 $sections[] = $row;
278 }
279 return $sections;
280 }
281
282 /**
283 * Loads the options for the themes select form element.
284 */
285 function _sections_theme_select() {
286 $options = array();
287
288 foreach (list_themes() as $key => $theme) {
289 $options[$theme->name] = t('@name (@status)', array('@name' => $theme->name, '@status' => ($theme->status ? t('Enabled') : t('Disabled'))));
290 }
291 return $options;
292 }

  ViewVC Help
Powered by ViewVC 1.1.2