Projet

Général

Profil

Paste
Télécharger (9,81 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ddblock / ddblock.admin.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 *  admin blocks of the ddblock module.
6
 *
7
 */
8

    
9
/**
10
 * Form with overview of all dynamic display blocks to manage and to add dynamic display blocks.
11
 */
12
function ddblock_block_add_form($form, &$form_state) {
13

    
14
  // get dynamic display  block.
15
  $rows = array();
16
  $blocks = ddblock_get_blocks(NULL);
17

    
18
  foreach ($blocks as $block) {
19
    $rows[] = array(
20
      'name' => check_plain($block->title),
21
      'edit' => l(t('Edit'), "admin/structure/ddblock/edit/$block->delta"),
22
      'delete' => l(t('Delete'), "admin/structure/ddblock/delete/$block->delta"),
23
      'block' => l(t('Configure block'), "admin/structure/block/manage/ddblock/$block->delta/configure"),
24
    );
25
  }
26
  if (empty($rows)) {
27
    $rows[] = array(array(
28
        'data' => t('No dynamic display blocks available.'),
29
        'colspan' => '4',
30
      ));
31
  }
32

    
33
  $header = array(t('Name'), array(
34
      'data' => t('Operation'),
35
      'colspan' => '3',
36
    ));
37
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'dynamic display block')));
38

    
39
  $form = array();
40
  $form['list'] = array(
41
    '#type' => 'fieldset',
42
    '#title' => t('Dynamic display blocks'),
43
    '#collapsible' => TRUE,
44
  );
45
  $form['list']['table'] = array(
46
    '#type' => 'item',
47
    '#prefix' => '<div>',
48
    '#markup' => $output,
49
    '#suffix' => '</div>',
50
  );
51

    
52
  // add dynamic display  block.
53
  $form['block'] = array(
54
    '#type' => 'fieldset',
55
    '#title' => t('Add dynamic display block.'),
56
  );
57

    
58
  $form['block']['title'] = array(
59
    '#type' => 'textfield',
60
    '#title' => t('Block title'),
61
    '#description' => t('A block with this same name will be created.'),
62
    '#default_value' => '',
63
    '#required' => TRUE,
64
  );
65

    
66
  $form['block']['op'] = array(
67
    '#type' => 'submit',
68
    '#value' => t('Add block'),
69
  );
70

    
71
  return $form;
72
}
73

    
74
/**
75
 * Validate "Add Block" form.
76
 */
77
function ddblock_block_add_form_validate($form, &$form_state) {
78
  $blocks = ddblock_get_blocks(NULL);
79
  $block_titles = array();
80
  foreach ($blocks as $block) {
81
    $block_titles[] = $block->title;
82
  }
83

    
84
  if (!empty($block_titles)) {
85
    // Check if name is unique
86
    if (in_array($form_state['values']['title'], $block_titles)) {
87
      form_set_error('', t('Dynamic display block %s already exists. Please use a different name.', array('%s' => $form_state['values']['title'])));
88
    }
89
  }
90
}
91
/**
92
 * Add dynamic display block to database from "Add Block" form.
93
 */
94
function ddblock_block_add_form_submit($form, &$form_state) {
95
  $id = db_insert('ddblock_block')
96
  ->fields(array(
97
    'title' => $form_state['values']['title'],
98
    'module' => 'ddblock',
99
  ))
100
  ->execute();
101

    
102
  drupal_set_message(t('Dynamic display block %s added.', array('%s' => $form_state['values']['title'])));
103
}
104

    
105
/**
106
 * Edit block form.
107
 */
108
function ddblock_block_edit_form($form, &$form_state, $delta) {
109
  $block = ddblock_get_blocks($delta);
110
  $form = array();
111

    
112
  $form['delta'] = array(
113
    '#type' => 'hidden',
114
    '#value' => $delta,
115
  );
116

    
117
  $form['title'] = array(
118
    '#type' => 'textfield',
119
    '#title' => t('Block title'),
120
    '#description' => t('The block name must be unique.'),
121
    '#default_value' => $block->title,
122
    '#required' => TRUE,
123
  );
124

    
125
  $form['op'] = array(
126
    '#type' => 'submit',
127
    '#value' => t('Save'),
128
  );
129

    
130
  return $form;
131
}
132

    
133
/**
134
 * Validate edit block form.
135
 */
136
function ddblock_block_edit_form_validate($form, &$form_state) {
137
  $blocks = ddblock_get_blocks(NULL);
138
  $block_titles = array();
139
  foreach ($blocks as $block) {
140
    $block_titles[$block->delta] = $block->title;
141
  }
142

    
143
  // Remove current blockname to prevent false error.
144
  unset($block_titles[$form_state['values']['delta']]);
145

    
146
  if (!empty($block_titles)) {
147
    // Check if name is unique.
148
    if (in_array($form_state['values']['title'], $block_titles)) {
149
      form_set_error('', t('Dynamic display block %s already exists. Please use a different name.', array('%s' => $form_state['values']['title'])));
150
    }
151
  }
152
}
153

    
154
/**
155
 * Submit edit block form.
156
 */
157
function ddblock_block_edit_form_submit($form, &$form_state) {
158
  db_update('ddblock_block')
159
  ->fields(array(
160
    'title' => $form_state['values']['title'],
161
  ))
162
  ->condition('delta', $form_state['values']['delta'])
163
  ->execute();
164

    
165
  drupal_set_message(t('Dynamic display block block updated.'));
166
  $form_state['redirect'] = 'admin/structure/ddblock';
167
  return $form;
168
}
169

    
170
/**
171
 * Delete block form.
172
 */
173
function ddblock_block_confirm_delete_form($form, &$form_state, $delta) {
174
  $block = ddblock_get_blocks($delta);
175
  if (empty($block)) {
176
    drupal_set_message(t('The block with delta @delta was not found.', array('@delta' => $delta)), 'error');
177
    return array();
178
  }
179
  else {
180
    if ($block->enabled == 1) {
181

    
182
      //The question to ask the user.
183
      $question = t('Are you sure you want to delete the dynamic display block instance %title?', array('%title' => $block->title));
184
    }
185
    else {
186
      //The question to ask the user.
187
      $question = t('Are you sure you want to delete the dynamic display block %title?', array('%title' => $block->title));
188
    }
189
    // The page to go to if the user denies the action.
190
    $path = 'admin/structure/ddblock';
191

    
192
    // Additional text to display (defaults to "This action cannot be undone.").
193
    $description = t('This action cannot be undone.');
194

    
195
    // A caption for the button which confirms the action.
196
    $yes = t('Delete');
197

    
198
    // A caption for the link which denies the action.
199
    $no = t('Cancel');
200

    
201
    // set delta value to use in submit function.
202
    $form['delta'] = array(
203
      '#type' => 'value',
204
      '#value' => $delta,
205
    );
206

    
207
    // set original module value to use in submit function.
208
    $form['origin'] = array(
209
      '#type' => 'value',
210
      '#value' => $block->module,
211
    );
212

    
213
    // set the redirect path value to use in submit function.
214
    $form_state['redirect'] = $path;
215
    return confirm_form(
216
      $form,
217
      $question,
218
      $path,
219
      $description,
220
      $yes,
221
      $no
222
    );
223
  }
224
}
225

    
226
/**
227
 * Delete a dynamic display block or dynamic display block instance.
228
 */
229
function ddblock_block_confirm_delete_form_submit($form, &$form_state) {
230

    
231
  if (ddblock_block_confirm_delete($form_state['values']['delta'])) {
232
    if ($form_state['values']['origin'] == 'ddblock') {
233
      $succes_message = t('Dynamic display block successfully deleted!');
234
    }
235
    else {
236
      $succes_message = t('Dynamic display block instance successfully deleted!');
237
    }
238
    drupal_set_message($succes_message);
239
  }
240
  else {
241
    if ($form_state['values']['origin'] == 'ddblock') {
242
      $failure_message = t('There was a problem deleting the dynamic display block');
243
    }
244
    else {
245
      $failure_message = t('There was a problem deleting the dynamic display block instance');
246
    }
247
    drupal_set_message($failure_message);
248
  }
249
  $form_state['redirect'] = 'admin/structure/ddblock';
250
  return $form;
251
}
252

    
253
/**
254
 * Delete a dynamic display block or dynamic display block instance from the database.
255
 */
256
/*function ddblock_block_confirm_delete($delta) {
257
  $result = db_delete('ddblock_block')
258
  ->condition('delta', (int) $delta)
259
  ->execute();
260
  if (ctype_digit($delta)) {
261
    _block_rehash();
262
    // variable_del includes a clear cache.
263
    variable_del('ddblock_block_ddblock_' . $delta . '_cycle_settings');
264
    return TRUE;
265
  }
266
  else {
267
    return FALSE;
268
  }
269
}
270
*/
271
function ddblock_block_confirm_delete($delta) {
272
  _block_rehash();
273
  $result = db_delete('ddblock_block')
274
  ->condition('delta', (int) $delta)
275
  ->execute();
276

    
277
  $result = db_delete('block')
278
  ->condition('module', 'ddblock')
279
  ->condition('delta', $delta)
280
  ->execute();
281

    
282
  // variable_del includes a clear cache.
283
  variable_del('ddblock_block_ddblock_' . $delta . '_settings');
284
  variable_del('ddblock_block_ddblock_' . $delta . '_cycle_settings');
285
  variable_del('ddblock_block_ddblock_' . $delta . '_cycle_mapping_settings');
286

    
287
  return TRUE;
288
}
289

    
290
/**
291
 * Dynamic display block settings form for setting the content types to be used with the dynamic display block module.
292
 */
293
function ddblock_settings_form($form, &$form_state) {
294
  $form = array();
295
  // get a list of all available content types,  names parameter is used to get an associative array of content type names.
296
  // the key is the machine-readable name of the content type and the value the human-readable name of the content type.
297
  $content_types = node_type_get_names();
298

    
299
  $form['ddblock_node_type'] = array(
300
    '#type' => 'checkboxes',
301
    '#title' => t('Content types'),
302
    '#description' => t('The content type(s) that can be used with the dynamic display block module.'),
303
    '#default_value' => variable_get('ddblock_node_type', array()),
304
    '#options' => $content_types,
305
    '#multiple' => TRUE,
306
    '#required' => FALSE,
307
  );
308

    
309
  // Fetch blocks directly from modules using block.module function.
310
  $blocks = _block_rehash();
311

    
312
  // Sort blocks how we want them.
313
  usort($blocks, 'ddblock_block_sort');
314

    
315
  // Turn $blocks into form options of available blocks.
316
  $options = array();
317
  foreach ($blocks as $block) {
318
    // Don't include ddblock module blocks in the list.
319
    if ($block['module'] != 'ddblock') {
320
      $options[$block['module'] . ':' . $block['delta'] . ':' . $block['info']] = $block['module'] . ' - ' . $block['info'];
321
    }
322
  }
323

    
324
  $form['ddblock_blocks'] = array(
325
    '#type' => 'checkboxes',
326
    '#title' => t('Blocks'),
327
    '#description' => t('The block(s) that can be used with the dynamic display block module.'),
328
    '#default_value' => variable_get('ddblock_blocks', array()),
329
    '#options' => $options,
330
    '#multiple' => TRUE,
331
    '#required' => FALSE,
332
  );
333

    
334

    
335
  $form_state['redirect'] = 'admin/structure/ddblock/list';
336

    
337
  return system_settings_form($form);
338
}