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

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

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


Revision 1.5 - (show annotations) (download) (as text)
Sun Sep 13 09:33:54 2009 UTC (2 months, 2 weeks ago) by ppblaauw
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +3 -3 lines
File MIME type: text/x-php
by ppblaauw: Dev release (Additional pager functionality and bug fixes), see CHANGELOG.txt
1 <?php
2 // $Id: ddblock.admin.inc,v 1.4 2008/10/30 01:05:44 ppblaauw Exp $
3
4 /**
5 * @file
6 * admin blocks of the ddblock module.
7 *
8 */
9
10 /**
11 * Form with overview of all dynamic display blocks to manage and to add dynamic display blocks.
12 */
13 function ddblock_block_add_form() {
14
15 // get dynamic display block.
16 $rows = array();
17 $blocks = ddblock_get_blocks(NULL);
18 foreach ($blocks as $block) {
19 $rows[] = array(
20 'name' => check_plain($block->title),
21 'edit' => l(t('Edit'), "admin/settings/ddblock/edit/$block->delta"),
22 'delete' => l(t('Delete'), "admin/settings/ddblock/delete/$block->delta"),
23 'block' => l(t('Configure block'), "admin/build/block/configure/ddblock/$block->delta"),
24 );
25 }
26 if (empty($rows)) {
27 $rows[] = array(array('data' => t('No dynamic display blocks available.'), 'colspan' => '4'));
28 }
29 $header = array(t('Name'), array('data' => t('Operation'), 'colspan' => '3'));
30 $output = theme('table', $header, $rows, array('id' => 'dynamic display block'));
31
32 $form = array();
33 $form['list'] = array(
34 '#type' => 'fieldset',
35 '#title' => t('Dynamic display blocks'),
36 '#collapsible' => TRUE,
37 );
38 $form['list']['table'] = array(
39 '#prefix' => '<div>',
40 '#value' => $output,
41 '#suffix' => '</div>',
42 );
43
44 // add dynamic display block.
45 $form['block'] = array(
46 '#type' => 'fieldset',
47 '#title' => t('Add dynamic display block.'),
48 );
49
50 $form['block']['title'] = array(
51 '#type' => 'textfield',
52 '#title' => t('Block title'),
53 '#description' => t('A block with this same name will be created.'),
54 '#default_value' => '',
55 '#required' => TRUE,
56 );
57
58 $form['block']['op'] = array(
59 '#type' => 'submit',
60 '#value' => t('Add block'),
61 );
62
63 return $form;
64 }
65
66 /**
67 * Validate "Add Block" form.
68 */
69 function ddblock_block_add_form_validate($form, &$form_state) {
70 $blocks = ddblock_get_blocks(NULL);
71 $block_titles = array();
72 foreach ($blocks as $block) {
73 $block_titles[] = $block->title;
74 }
75
76 if (!empty($block_titles)) {
77 // Check if name is unique
78 if (in_array($form_state['values']['title'], $block_titles)) {
79 form_set_error('', t('Dynamic display block %s already exists. Please use a different name.', array('%s' => $form_state['values']['title'])));
80 }
81 }
82 }
83 /**
84 * Add dynamic display block to database from "Add Block" form.
85 */
86 function ddblock_block_add_form_submit($form, &$form_state) {
87 db_query("INSERT INTO {ddblock_block} (title, module) VALUES ('%s', '%s')", $form_state['values']['title'], 'ddblock');
88
89 drupal_set_message(t('Dynamic display block %s added.', array('%s' => $form_state['values']['title'])));
90 }
91
92 /**
93 * Edit block form.
94 */
95 function ddblock_block_edit_form(&$form_state, $delta) {
96 $block = ddblock_get_blocks($delta);
97 $form = array();
98
99 $form['delta'] = array(
100 '#type' => 'hidden',
101 '#value' => $delta,
102 );
103
104 $form['title'] = array(
105 '#type' => 'textfield',
106 '#title' => t('Block title'),
107 '#description' => t('The block name must be unique.'),
108 '#default_value' => $block->title,
109 '#required' => TRUE,
110 );
111
112 $form['op'] = array(
113 '#type' => 'submit',
114 '#value' => t('Save'),
115 );
116
117 $form['#redirect'] = 'admin/settings/ddblock';
118
119 return $form;
120 }
121
122 /**
123 * Validate edit block form.
124 */
125 function ddblock_block_edit_form_validate($form, &$form_state) {
126 $blocks = ddblock_get_blocks(NULL);
127 $block_titles = array();
128 foreach ($blocks as $block) {
129 $block_titles[$block->delta] = $block->title;
130 }
131
132 // Remove current blockname to prevent false error.
133 unset($block_titles[$form_state['values']['delta']]);
134
135 if (!empty($block_titles)) {
136 // Check if name is unique.
137 if (in_array($form_state['values']['title'], $block_titles)) {
138 form_set_error('', t('Dynamic display block %s already exists. Please use a different name.', array('%s' => $form_state['values']['title'])));
139 }
140 }
141 }
142
143 /**
144 * Submit edit block form.
145 */
146 function ddblock_block_edit_form_submit($form, &$form_state) {
147 db_query("UPDATE {ddblock_block} SET title = '%s' WHERE delta = %d",
148 $form_state['values']['title'], $form_state['values']['delta']);
149
150 drupal_set_message(t('Dynamic display block block updated.'));
151 return 'admin/settings/ddblock';
152 }
153
154 /**
155 * Delete block form.
156 */
157 function ddblock_block_confirm_delete_form(&$form_state, $delta) {
158 $block = ddblock_get_blocks($delta);
159 if (empty($block)) {
160 drupal_set_message(t('The block with delta @delta was not found.', array('@delta' => $delta)), 'error');
161 return array();
162 }
163 else {
164 if ($block->ddblock_enabled) {
165
166 //The question to ask the user.
167 $question = t('Are you sure you want to delete the dynamic display block instance %title?', array('%title' => $block->title));
168 }
169 else {
170 //The question to ask the user.
171 $question = t('Are you sure you want to delete the dynamic display block %title?', array('%title' => $block->title));
172 }
173 // The page to go to if the user denies the action.
174 $path = 'admin/settings/ddblock';
175
176 // Additional text to display (defaults to "This action cannot be undone.").
177 $description = t('This action cannot be undone.');
178
179 // A caption for the button which confirms the action.
180 $yes = t('Delete');
181
182 // A caption for the link which denies the action.
183 $no = t('Cancel');
184
185 // set delta value to use in submit function.
186 $form['delta'] = array('#type' => 'value', '#value' => $delta);
187
188 // set original module value to use in submit function.
189 $form['origin'] = array('#type' => 'value', '#value' => $block->module);
190
191 // set the redirect path value to use in submit function.
192 $form['#redirect'] = $path;
193 return confirm_form(
194 $form,
195 $question,
196 $path,
197 $description,
198 $yes,
199 $no
200 );
201 }
202 }
203
204 /**
205 * Delete a dynamic display block or dynamic display block instance.
206 */
207 function ddblock_block_confirm_delete_form_submit($form, &$form_state) {
208
209 if (ddblock_block_confirm_delete($form_state['values']['delta'])) {
210 if ($form_state['values']['origin'] == 'ddblock') {
211 $succes_message = t('Dynamic display block successfully deleted!');
212 }
213 else {
214 $succes_message = t('Dynamic display block instance successfully deleted!');
215 }
216 drupal_set_message($succes_message);
217 }
218 else {
219 if ($form_state['values']['origin'] == 'ddblock') {
220 $failure_message = t('There was a problem deleting the dynamic display block');
221 }
222 else {
223 $failure_message = t('There was a problem deleting the dynamic display block instance');
224 }
225 drupal_set_message($failure_message);
226 }
227 }
228
229 /**
230 * Delete a dynamic display block or dynamic display block instance from the database.
231 */
232 function ddblock_block_confirm_delete($delta) {
233 $result = db_query('DELETE FROM {ddblock_block} WHERE delta = %d', (int)$delta);
234 if (ctype_digit($delta) && db_affected_rows() == 1) {
235 _block_rehash();
236 // variable_del includes a clear cache.
237 variable_del('ddblock_block_ddblock_'. $delta .'_cycle_settings');
238 return TRUE;
239 }
240 else {
241 return FALSE;
242 }
243 }
244
245 /**
246 * Dynamic display block settings form for setting the content types to be used with the dynamic display block module.
247 */
248 function ddblock_settings_form() {
249 $form = array();
250
251 // get a list of all available content types, names parameter is used to get an associative array of content type names.
252 // the key is the machine-readable name of the content type and the value the human-readable name of the content type.
253 $content_types = node_get_types('names');
254
255 $form['ddblock_node_type'] = array(
256 '#type' => 'checkboxes',
257 '#title' => t('Content types'),
258 '#description' => t('The content type(s) that can be used with the dynamic display block module.'),
259 '#default_value' => variable_get('ddblock_node_type', array()),
260 '#options' => $content_types,
261 '#multiple' => TRUE,
262 '#required' => FALSE,
263 );
264
265 // Fetch blocks directly from modules using block.module function.
266 $blocks = _block_rehash();
267
268 // Sort blocks how we want them.
269 usort($blocks, 'ddblock_block_sort');
270
271 // Turn $blocks into form options of available blocks.
272 $options = array();
273 foreach ($blocks as $block) {
274 // Don't include ddblock module blocks in the list.
275 if ($block['module'] != 'ddblock') {
276 $options[$block['module'] .':'. $block['delta'] .':'. $block['info']] = $block['module'] .' - '. $block['info'];
277 }
278 }
279
280 $form['ddblock_blocks'] = array(
281 '#type' => 'checkboxes',
282 '#title' => t('Blocks'),
283 '#description' => t('The block(s) that can be used with the dynamic display block module.'),
284 '#default_value' => variable_get('ddblock_blocks', array()),
285 '#options' => $options,
286 '#multiple' => TRUE,
287 '#required' => FALSE,
288 );
289
290 $form['#redirect'] = 'admin/settings/ddblock/list';
291
292 return system_settings_form($form);
293 }

  ViewVC Help
Powered by ViewVC 1.1.2