Projet

Général

Profil

Paste
Télécharger (6,14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / twitter_block / twitter_block.admin.inc @ a2baadd1

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin page callbacks for the Twitter Block module.
6
 */
7

    
8
/**
9
 * Form constructor for the add block form.
10
 *
11
 * @see twitter_block_add_block_form_validate()
12
 * @see twitter_block_add_block_form_submit()
13
 * @ingroup forms
14
 */
15
function twitter_block_add_block_form($form, &$form_state) {
16
  module_load_include('inc', 'block', 'block.admin');
17
  $form = block_admin_configure($form, $form_state, 'twitter_block', NULL);
18

    
19
  // Other modules should be able to use hook_form_block_add_block_form_alter()
20
  // to modify this form, so add a base form ID
21
  $form_state['build_info']['base_form_id'] = 'block_add_block_form';
22

    
23
  // Prevent block_add_block_form_validate/submit() from being automatically
24
  // added because of the base form ID by providing these handlers manually
25
  $form['#validate'] = array('twitter_block_add_block_form_validate');
26
  $form['#submit'] = array('twitter_block_add_block_form_submit');
27

    
28
  return $form;
29
}
30

    
31
/**
32
 * Form validation handler for twitter_block_add_block_form().
33
 *
34
 * @see twitter_block_add_block_form()
35
 * @see twitter_block_add_block_form_submit()
36
 */
37
function twitter_block_add_block_form_validate($form, &$form_state) {
38
  $unique_description = (bool) db_query_range('SELECT 1 FROM {twitter_block} WHERE info = :info', 0, 1, array(':info' => $form_state['values']['info']))->fetchField();
39

    
40
  if (empty($form_state['values']['info']) || $unique_description) {
41
    form_set_error('info', t('Ensure that each block description is unique.'));
42
  }
43

    
44
  $unique_widget_id = (bool) db_query_range('SELECT 1 FROM {twitter_block} WHERE widget_id = :widget_id', 0, 1, array(':widget_id' => $form_state['values']['widget_id']))->fetchField();
45

    
46
  if (empty($form_state['values']['widget_id']) || $unique_widget_id) {
47
    form_set_error('widget_id', t('Ensure that each block widget ID is unique.'));
48
  }
49
}
50

    
51
/**
52
 * Form submission handler for twitter_block_add_block_form().
53
 *
54
 * Saves the new custom Twitter block.
55
 *
56
 * @see twitter_block_add_block_form()
57
 * @see twitter_block_add_block_form_validate()
58
 */
59
function twitter_block_add_block_form_submit($form, &$form_state) {
60
  // The serialized 'data' column contains the timeline settings.
61
  $data = array(
62
    'theme' => $form_state['values']['theme'],
63
    'link_color' => $form_state['values']['link_color'],
64
    'width' => $form_state['values']['width'],
65
    'height' => $form_state['values']['height'],
66
    'chrome' => $form_state['values']['chrome'],
67
    'border_color' => $form_state['values']['border_color'],
68
    'language' => $form_state['values']['language'],
69
    'tweet_limit' => $form_state['values']['tweet_limit'],
70
    'related' => $form_state['values']['related'],
71
    'polite' => $form_state['values']['polite'],
72
  );
73

    
74
  // Save the block configuration
75
  $delta = db_insert('twitter_block')
76
    ->fields(array(
77
      'info' => $form_state['values']['info'],
78
      'widget_id' => $form_state['values']['widget_id'],
79
      'data' => serialize($data),
80
    ))
81
    ->execute();
82

    
83
  // Store block delta to allow other modules to work with new block
84
  $form_state['values']['delta'] = $delta;
85

    
86
  // Run the normal new block submission (borrowed from block_add_block_form_submit)
87
  $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
88
  foreach (list_themes() as $key => $theme) {
89
    if ($theme->status) {
90
      $query->values(array(
91
        'visibility' => (int) $form_state['values']['visibility'],
92
        'pages' => trim($form_state['values']['pages']),
93
        'custom' => (int) $form_state['values']['custom'],
94
        'title' => $form_state['values']['title'],
95
        'module' => $form_state['values']['module'],
96
        'theme' => $theme->name,
97
        'status' => 0,
98
        'weight' => 0,
99
        'delta' => $delta,
100
        'cache' => DRUPAL_NO_CACHE,
101
      ));
102
    }
103
  }
104
  $query->execute();
105

    
106
  $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
107
  foreach (array_filter($form_state['values']['roles']) as $rid) {
108
    $query->values(array(
109
      'rid' => $rid,
110
      'module' => $form_state['values']['module'],
111
      'delta' => $delta,
112
    ));
113
  }
114
  $query->execute();
115

    
116
  // Store regions per theme for this block
117
  foreach ($form_state['values']['regions'] as $theme => $region) {
118
    db_merge('block')
119
      ->key(array('theme' => $theme, 'delta' => $delta, 'module' => $form_state['values']['module']))
120
      ->fields(array(
121
        'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
122
        'pages' => trim($form_state['values']['pages']),
123
        'status' => (int) ($region != BLOCK_REGION_NONE),
124
      ))
125
      ->execute();
126
  }
127

    
128
  drupal_set_message(t('The block has been created.'));
129
  cache_clear_all();
130
  $form_state['redirect'] = 'admin/structure/block';
131
}
132

    
133
/**
134
 * Form constructor for the custom Twitter block deletion form.
135
 *
136
 * @param $delta
137
 *   The unique ID of the block within the context of $module.
138
 *
139
 * @see twitter_block_delete_submit()
140
 */
141
function twitter_block_delete($form, &$form_state, $delta) {
142
  $block = block_load('twitter_block', $delta);
143
  $twitter_block = twitter_block_block_get($block->delta);
144
  $form['info'] = array('#type' => 'hidden', '#value' => $twitter_block['info'] ? $twitter_block['info'] : $twitter_block['title']);
145
  $form['bid'] = array('#type' => 'hidden', '#value' => $block->delta);
146

    
147
  return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $twitter_block['info'])), 'admin/structure/block', '', t('Delete'), t('Cancel'));
148
}
149

    
150
/**
151
 * Form submission handler for twitter_block_delete().
152
 *
153
 * @see twitter_block_delete()
154
 */
155
function twitter_block_delete_submit($form, &$form_state) {
156
  db_delete('twitter_block')
157
    ->condition('bid', $form_state['values']['bid'])
158
    ->execute();
159
  db_delete('block')
160
    ->condition('module', 'twitter_block')
161
    ->condition('delta', $form_state['values']['bid'])
162
    ->execute();
163
  db_delete('block_role')
164
    ->condition('module', 'twitter_block')
165
    ->condition('delta', $form_state['values']['bid'])
166
    ->execute();
167
  drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
168
  cache_clear_all();
169
  $form_state['redirect'] = 'admin/structure/block';
170
  return;
171
}