Projet

Général

Profil

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

root / drupal7 / sites / all / modules / twitter_block / twitter_block.admin.inc @ 66b5cbf6

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
      'username' => $form_state['values']['username'],
80
      'data' => serialize($data),
81
    ))
82
    ->execute();
83

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

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

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

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

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

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

    
148
  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'));
149
}
150

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