Newer
Older
<?php
// $Id$
/**
* Page callback for CSS Injector's main admin page.
*/
function css_injector_admin_form() {
$rules = _css_injector_load_rule(NULL, TRUE);
$path = drupal_get_path('module', 'css_injector') .'/';
$form = array();
Jeff Eaton
committed
$form['#tree'] = TRUE;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
foreach($rules as $rule) {
$form['rules'][$rule['crid']]['#rule'] = $rule;
$form['rules'][$rule['crid']]['edit'] = array(
'#type' => 'image_button',
'#title' => t('Edit rule'),
'#src' => $path .'text-editor.png',
'#submit' => array('css_injector_admin_edit_button'),
'#crid' => $rule['crid'],
);
$form['rules'][$rule['crid']]['delete'] = array(
'#type' => 'image_button',
'#title' => t('Delete rule'),
'#src' => $path .'edit-delete.png',
'#submit' => array('css_injector_admin_delete_button'),
'#crid' => $rule['crid'],
);
}
return $form;
}
/**
* Edit button callback for the CSS rule listing form.
*/
function css_injector_admin_edit_button($form, &$form_state) {
$button = $form_state['clicked_button'];
$crid = $button['#crid'];
$form_state['redirect'] = 'admin/settings/css_injector/edit/'. $crid;
}
/**
* Delete button callback for the CSS rule listing form.
* Redirects the user to the confirmation form.
*/
function css_injector_admin_delete_button($form, &$form_state) {
$button = $form_state['clicked_button'];
$crid = $button['#crid'];
$form_state['redirect'] = 'admin/settings/css_injector/delete/'. $crid;
}
/**
* Theme function for the CSS Injector admin overview form.
*/
function theme_css_injector_admin_form(&$form) {
$headers = array(t('Title'), t('File path'), t('Actions'));
$rows = array();
if (!empty($form['rules'])) {
foreach(element_children($form['rules']) as $crid) {
$row = array();
$rule = $form['rules'][$crid]['#rule'];
$row[] = check_plain($rule['title']);
$row[] = check_plain($rule['file_path']);
$row[] = drupal_render($form['rules'][$crid]);
$rows[] = $row;
}
}
$link = l(t('Click here'), 'admin/settings/css_injector/add');
$row = array();
if (empty($rows)) {
$row[] = array(
'data' => t('No CSS injection rules have been set up yet. !url to create one.', array('!url' => $link)),
'colspan' => 3,
);
}
else {
$row[] = array(
'data' => t('!url to add a new CSS injection rule.', array('!url' => $link)),
'colspan' => 3,
);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}
$rows[] = $row;
$output = '<p>'. t("Use CSS injection rules to add small snippets of CSS to the page output when specific criteria are met. For example, a simple rule could change the page background color at night or float a particular div to the right on node editing pages.") .'</p>';
$output .= theme('table', $headers, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Constructor for the CSS rule edit form.
*/
function css_injector_edit($form_state, $crid = NULL) {
if (isset($crid)) {
$rule = _css_injector_load_rule($crid, TRUE);
$form['crid'] = array(
'#type' => 'value',
'#value' => $crid,
);
$path = file_create_path($rule['file_path']);
if (file_exists($path)) {
$rule['css_text'] = file_get_contents($path);
$form['file_path'] = array(
'#type' => 'value',
'#value' => $rule['file_path'],
);
}
else {
$rule['css_text'] = '';
}
}
else {
$rule = array(
'title' => '',
'rule_type' => 0,
'rule_conditions' => '',
'media' => 'all',
'preprocess' => 1,
'css_text' => '',
);
}
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $rule['title'],
'#required' => TRUE,
);
$form['css_text'] = array(
'#type' => 'textarea',
'#title' => t('CSS code'),
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'#default_value' => $rule['css_text'],
'#required' => TRUE,
);
// Shamelessly ripped from block.module. Who doesn't use this snippet
// of code, really?
$access = user_access('use PHP for block visibility');
if ($rule['rule_type'] == 2 && !$access) {
$form['conditional'] = array();
$form['conditional']['rule_type'] = array('#type' => 'value', '#value' => 2);
$form['conditional']['rule_conditions'] = array('#type' => 'value', '#value' => $rule['rule_conitions']);
}
else {
$options = array(t('Add on every page except the listed pages.'), t('Add on only the listed pages.'));
$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>'));
if ($access) {
$options[] = t('Add if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
$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 ?>'));
}
$form['conditional']['rule_type'] = array(
'#type' => 'radios',
'#title' => t('Add the CSS on specific pages'),
'#options' => $options,
'#default_value' => $rule['rule_type'],
);
$form['conditional']['rule_conditions'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $rule['rule_conditions'],
'#description' => $description,
);
}
$form['media'] = array(
'#type' => 'select',
'#title' => t('Media'),
'#description' => t(''),
'#options' => array('all' => t('All'), 'screen' => t('Screen'), 'print' => t('Print')),
'#default_value' => $rule['media'],
);
$form['preprocess'] = array(
'#type' => 'checkbox',
'#title' => t('Preprocess CSS'),
'#description' => t(''),
'#default_value' => $rule['preprocess'],
);
Jeff Eaton
committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('css_injector_edit_save'),
);
if (!empty($rule['crid'])) {
$form['buttons']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('css_injector_admin_delete_button'),
'#crid' => $rule['crid'],
);
}
return $form;
}
/**
* Validation callback for the CSS rule edit form.
*/
function css_injector_edit_validate($form, &$form_state) {
$rule = $form_state['values'];
$directory = file_directory_path();
file_check_directory($directory, 1);
$form_state['rule'] = $rule;
}
/**
* Submit button callback for the CSS rule edit form.
*/
function css_injector_edit_save($form, &$form_state) {
$rule = $form_state['rule'];
if (empty($rule['crid'])) {
drupal_write_record('css_injector_rule', $rule);
$directory = file_directory_path();
$rule['file_path'] = file_create_path($directory .'/css_injector_'. $rule['crid'] .'.css');
}
drupal_write_record('css_injector_rule', $rule, array('crid'));
file_save_data($rule['css_text'], file_create_path($rule['file_path']), FILE_EXISTS_REPLACE);
_css_injector_load_rule(NULL, TRUE);
drupal_set_message('Your CSS injection rule was saved.');
$form_state['redirect'] = 'admin/settings/css_injector';
}
/**
* Menu callback -- ask for confirmation of rule deletion
*/
function css_injector_delete_confirm(&$form_state, $crid) {
$form['crid'] = array(
'#type' => 'value',
'#value' => $crid,
);
$rule = _css_injector_load_rule($crid);
return confirm_form($form,
t('Are you sure you want to delete %title?', array('%title' => $rule['title'])),
isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/css_injector',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute node deletion
*/
function css_injector_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
_css_injector_delete_rule($form_state['values']['crid']);
}
$form_state['redirect'] = 'admin/settings/css_injector';
return;