Projet

Général

Profil

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

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

1
<?php
2
/**
3
 * @file css_injector.admin.inc
4
 * Administrative interface for CSS Injector.
5
 */
6

    
7
/**
8
 * Form builder function for CSS Injector's main admin page.
9
 */
10
function css_injector_admin_form($form, &$form_state) {
11
  $rules = _css_injector_load_rule(NULL, TRUE);
12
  $path = drupal_get_path('module', 'css_injector') . '/';
13
  $form = array();
14
  $form['#tree'] = TRUE;
15

    
16
  // Adding css stylesheet for icons.
17
  $form['#attached']['css'] = array(
18
    drupal_get_path('module', 'css_injector') . '/css_injector.css',
19
  );
20

    
21
  foreach ($rules as $rule) {
22
    $form['rules'][$rule['crid']]['#rule'] = $rule;
23
    $form['rules'][$rule['crid']]['edit'] = array(
24
      '#type' => 'submit',
25
      '#value' => t('Edit rule'),
26
      '#name' => 'edit' . $rule['crid'],
27
      '#submit' => array('css_injector_admin_edit_button'),
28
      '#attributes' => array('class' => array('css-inject-edit')),
29
      '#crid' => $rule['crid'],
30
    );
31
    $form['rules'][$rule['crid']]['delete'] = array(
32
      '#type' => 'submit',
33
      '#value' => t('Delete rule'),
34
      '#name' => 'delete' . $rule['crid'],
35
      '#submit' => array('css_injector_admin_delete_button'),
36
      '#attributes' => array('class' => array('css-inject-delete')),
37
      '#crid' => $rule['crid'],
38
    );
39
  }
40
  return $form;
41
}
42

    
43
/**
44
 * Edit button callback for the CSS rule listing form.
45
 */
46
function css_injector_admin_edit_button($form, &$form_state) {
47
  $button = $form_state['triggering_element'];
48
  $crid = $button['#crid'];
49
  $form_state['redirect'] = 'admin/config/development/css-injector/edit/' . $crid;
50
}
51

    
52
/**
53
 * Delete button callback for the CSS rule listing form.
54
 * Redirects the user to the confirmation form.
55
 */
56
function css_injector_admin_delete_button($form, &$form_state) {
57
  $button = $form_state['triggering_element'];
58
  $crid = $button['#crid'];
59
  $form_state['redirect'] = 'admin/config/development/css-injector/delete/' . $crid;
60
}
61

    
62
/**
63
 * Theme function for the CSS Injector admin overview form.
64
 */
65
function theme_css_injector_admin_form($variables) {
66
  $form = $variables['form'];
67
  $headers = array(t('Title'), t('Location'), t('Actions'));
68
  $rows = array();
69
  if (!empty($form['rules'])) {
70
    foreach (element_children($form['rules']) as $crid) {
71
      $row = array();
72
      $rule = $form['rules'][$crid]['#rule'];
73
      $row[] = check_plain($rule['title']);
74
      $row[] = _css_injector_rule_uri($rule['crid']);
75
      $row[] = drupal_render($form['rules'][$crid]);
76
      $rows[] = $row;
77
    }
78
  }
79

    
80
  $link = l(t('Create a new rule'), 'admin/config/development/css-injector/add');
81
  $row = array();
82
  if (empty($rows)) {
83
    $row[] = array(
84
      'data' => t('No CSS injection rules have been set up yet. !url.', array('!url' => $link)),
85
      'colspan' => 3,
86
    );
87
  }
88
  else {
89
    $row[] = array(
90
      'data' => t('!url.', array('!url' => $link)),
91
      'colspan' => 3,
92
    );
93
  }
94
  $rows[] = $row;
95

    
96
  $output = theme('table', array('header' => $headers, 'rows' => $rows));
97
  $output .= drupal_render_children($form);
98
  return $output;
99
}
100

    
101
/**
102
 * Form builder function for the CSS rule edit form.
103
 */
104
function css_injector_edit($form, $form_state, $crid = NULL) {
105
  if (isset($crid)) {
106
    $rule = _css_injector_load_rule($crid, TRUE);
107
    $path = _css_injector_rule_uri($rule['crid']);
108
    if (file_exists($path)) {
109
      $rule['css_text'] = file_get_contents($path);
110
    }
111
    else {
112
      $rule['css_text'] = '';
113
    }
114
  }
115
  else {
116
    $rule = array(
117
      'title' => '',
118
      'rule_type' => CSS_INJECTOR_PAGES_NOTLISTED,
119
      'rule_themes' => '',
120
      'rule_conditions' => '',
121
      'media' => 'all',
122
      'preprocess' => 1,
123
      'css_text' => '',
124
      'enabled' => 1,
125
    );
126
  }
127

    
128
  // Adding css stylesheet for icons.
129
  $form['#attached']['js'] = array(
130
    drupal_get_path('module', 'css_injector') . '/ace/ace.js',
131
    drupal_get_path('module', 'css_injector') . '/syntax_highlighter.js',
132
  );
133

    
134
  $form['#attached']['css'] = array(
135
    drupal_get_path('module', 'css_injector') . '/syntax_highlighter.css',
136
  );
137

    
138
  if (isset($crid)) {
139
  $form['crid'] = array(
140
      '#type' => 'value',
141
      '#value' => $crid,
142
    );
143
  }
144

    
145
  $form['title'] = array(
146
    '#type' => 'textfield',
147
    '#title' => t('Title'),
148
    '#default_value' => $rule['title'],
149
    '#required' => TRUE,
150
  );
151

    
152
  $form['css_text'] = array(
153
    '#type' => 'textarea',
154
    '#title' => t('CSS code'),
155
    '#rows' => 10,
156
    '#default_value' => $rule['css_text'],
157
    '#required' => TRUE,
158
  );
159

    
160
  $form['css_text_ace'] = array(
161
    '#prefix' => '<span class="disable-ace">Disable syntax highlighter</span>',
162
    '#markup' => '<div class="ace-editor"><div id="editor">' . $rule['css_text'] . '</div></div>',
163
  );
164

    
165
  // Get info of site themes.
166
  $themes = array();
167
  $theme_list = list_themes();
168
  $default_theme = variable_get('theme_default');
169
  foreach ($theme_list as $single_theme) {
170
    $themes[$single_theme->name] = $single_theme->info['name'];
171
  }
172
  $rule['rule_themes'] = unserialize($rule['rule_themes']);
173

    
174
  $form['conditional']['rule_themes'] = array(
175
  '#type' => 'select',
176
  '#title' => 'Themes to show on',
177
  '#default_value' => isset($rule['rule_themes']) ? $rule['rule_themes'] : $default_theme,
178
  '#options' => $themes,
179
  '#description' => t('Select themes css will be applied to. @theme theme is selected by default.', array('@theme' => $themes[$default_theme])),
180
  '#multiple' => TRUE,
181
  );
182

    
183
  // Shamelessly ripped from block.module. Who doesn't use this snippet
184
  // of code, really?
185
  $php_access = (user_access('use PHP for settings') && module_exists('php'));
186
  $options = array(
187
    CSS_INJECTOR_PAGES_NOTLISTED => t('Add on every page except the listed pages.'),
188
    CSS_INJECTOR_PAGES_LISTED => t('add on only the listed pages.')
189
  );
190
  $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>'));
191

    
192
  if ($php_access) {
193
    $options[CSS_INJECTOR_PHP] = t('Add if the following PHP code outputs a nonzero value (PHP-mode, experts only).');
194
    $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 ?>'));
195
  }
196
  $form['conditional']['rule_type'] = array(
197
    '#type' => 'radios',
198
    '#title' => t('Add the CSS on specific pages'),
199
    '#options' => $options,
200
    '#default_value' => $rule['rule_type'],
201
  );
202
  $form['conditional']['rule_conditions'] = array(
203
    '#type' => 'textarea',
204
    '#title' => t('Pages'),
205
    '#default_value' => $rule['rule_conditions'],
206
    '#description' => $description,
207
  );
208
  $form['media'] = array(
209
    '#type' => 'select',
210
    '#title' => t('Media'),
211
    '#options' => array(
212
      'all' => t('All'),
213
      'screen' => t('Screen'),
214
      'print' => t('Print'),
215
      'IE 7' => t('IE7'),
216
      'IE 8' => t('IE8'),
217
      'IE 9' => t('IE9'),
218
    ),
219
    '#default_value' => $rule['media'],
220
  );
221

    
222
  $form['preprocess'] = array(
223
    '#type' => 'checkbox',
224
    '#title' => t('Preprocess CSS'),
225
    '#default_value' => $rule['preprocess'],
226
  );
227
  $form['enabled'] = array(
228
    '#type' => 'checkbox',
229
    '#title' => t('Enable rule'),
230
    '#default_value' => isset($rule['enabled']) ? $rule['enabled'] : 1,
231
  );
232

    
233
  $form['buttons']['save'] = array(
234
    '#type' => 'submit',
235
    '#value' => t('Save'),
236
    '#submit' => array('css_injector_edit_save'),
237
  );
238
  $form['buttons']['save_and_continue'] = array(
239
    '#type' => 'submit',
240
    '#value' => t('Save and Continue Editing'),
241
    '#submit' => array('css_injector_edit_save_and_continue'),
242
  );
243

    
244
  if (!empty($rule['crid'])) {
245
    $form['buttons']['delete'] = array(
246
      '#type' => 'submit',
247
      '#value' => t('Delete'),
248
      '#crid' => $rule['crid'],
249
      '#submit' => array('css_injector_admin_delete_button'),
250
    );
251
  }
252

    
253
  return $form;
254
}
255

    
256
/**
257
 * Validation callback for the CSS rule edit form.
258
 */
259
function css_injector_edit_validate($form, &$form_state) {
260
  $directory = drupal_realpath('public://css_injector');
261
  if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
262
    form_error($form, t('The directory %directory is not writable', array('%directory' => $directory)));
263
  }
264
}
265

    
266
/**
267
 * Submit button callback for the CSS rule edit form.
268
 */
269
function css_injector_edit_save($form, &$form_state) {
270
  //$themes = implode('//', $form_state['values']['themes']);
271
  $rule = $form_state['values'];
272
  $crid = !empty($form_state['values']['crid']) ? $form_state['values']['crid'] : NULL;
273
  // Serialize themes list before inserting to databse.
274
  $rule['rule_themes'] = serialize($rule['rule_themes']);
275
  drupal_write_record('css_injector_rule', $rule, empty($crid) ? array() : 'crid');
276
  // Unserialize themes list for form state.
277
  $rule['rule_themes'] = unserialize($rule['rule_themes']);
278
  $form_state['values']['rule'] = $rule;
279
  file_unmanaged_save_data($rule['css_text'], _css_injector_rule_uri($rule['crid']), FILE_EXISTS_REPLACE);
280
  _css_injector_load_rule(NULL, TRUE);
281

    
282
  drupal_set_message(t('Your CSS injection rule %title was saved.', array('%title' => $rule['title'])));
283
  $form_state['redirect'] = 'admin/config/development/css-injector';
284
}
285

    
286
/**
287
 * Save and continue callback for the CSS rule edit form.
288
 */
289
function css_injector_edit_save_and_continue($form, &$form_state) {
290
  css_injector_edit_save($form, $form_state);
291
  $form_state['redirect'] = 'admin/config/development/css-injector/edit/' . $form_state['values']['rule']['crid'];
292
}
293

    
294

    
295
/**
296
 * Menu callback -- ask for confirmation of rule deletion.
297
 */
298
function css_injector_delete_confirm($form, &$form_state, $crid) {
299
  $form['crid'] = array(
300
    '#type' => 'value',
301
    '#value' => $crid,
302
  );
303

    
304
  $rule = _css_injector_load_rule($crid);
305
  return confirm_form($form,
306
    t('Are you sure you want to delete %title?', array('%title' => $rule['title'])),
307
    isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/development/css-injector',
308
    t('This action cannot be undone.'),
309
    t('Delete'),
310
    t('Cancel')
311
  );
312
}
313

    
314
/**
315
 * Execute node deletion.
316
 */
317
function css_injector_delete_confirm_submit($form, &$form_state) {
318
  if ($form_state['values']['confirm']) {
319
    _css_injector_delete_rule($form_state['values']['crid']);
320
  }
321

    
322
  $form_state['redirect'] = 'admin/config/development/css-injector';
323
  return;
324
}