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