/[drupal]/contributions/modules/jqp/jqp.admin.inc
ViewVC logotype

Contents of /contributions/modules/jqp/jqp.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Sun Apr 19 10:35:14 2009 UTC (7 months, 1 week ago) by skilip
Branch: MAIN
CVS Tags: DRUPAL-6--2-0-BETA1, HEAD
Branch point for: DRUPAL-6--2
Changes since 1.1: +1 -1 lines
File MIME type: text/x-php
Updated jqp.module and jqp.info to v.2
1 <?php
2 // $Id: jqp.admin.inc,v 2.0 2009/04/19 12:32:00 jjeff, skilip Exp $
3
4 /**
5 * @file
6 *
7 * This file includes all functionality for the libraries configuration pages
8 */
9
10 /**
11 * Page callback for admin/build/jqp.
12 *
13 * Lists all installed javascript libraries.
14 */
15 function jqp_status_page() {
16 $js_libraries = jqp_rebuild_cache();
17
18 if (!empty($js_libraries)) {
19
20 // Set the table header.
21 $header = array(t('Name'), t('Description'), t('Versions'), t('Operations'), t('Status'));
22
23 foreach ($js_libraries as $name => $library) {
24
25 // Reset the status class to 'ok'.
26 $status = 'ok';
27
28 // Reset all arrays.
29 $versions = $links = $warnings = array();
30
31 // Get all versions and check wheter all files exists
32 foreach (array('scripts', 'stylesheets') as $type) {
33
34 // Proceed only if there are files added to this type
35 if (!empty($library->info[$type])) {
36 foreach ($library->info[$type] as $v => $files) {
37 unset($files['changed']);
38
39 $versions[$v] = $v == 0 ? 'default' : $v;
40 $links[$v] = l(t('configure'), "admin/build/jqp/$name/$v");
41
42 // Check if all files for this version exist
43 foreach ($files as $file) {
44 if (!file_exists($file)) {
45 // The file does not exist.
46 // Set the status of this version to 'warning' and stop checking.
47 $warnings[$v] = TRUE;
48 $status = 'warning';
49 continue;
50 }
51 }
52 }
53 }
54 }
55
56 // Reset the $first which is used to check if we're in the first tr of a library
57 $first = TRUE;
58 foreach (array_keys($versions) as $v) {
59 $library_name = '';
60 if ($first) {
61 if ($library->info['project_url']) {
62 $library_name = l($library->info['name'], $library->info['project_url'], array('attributes' => array('title' => t('Click here to go to %library\'s project page.', array('%library' => $library->info['name'])))));
63 }
64 else {
65 $library_name = $library->info['name'];
66 }
67 }
68
69 // Create the unique table row
70 $rows[$name . $v] = array(
71 'data' => array(
72 ($library_name ? "<strong>$library_name</strong>" : ''),
73 $first ? $library->info['description'] : '',
74 $versions[$v],
75 $links[$v],
76 theme('image', "misc/watchdog-". ($warnings[$v] ? 'warning' : 'ok') .".png"),
77 ),
78 'class' => ($first ? 'first ' : '') . $status,
79 );
80 // The first table row for this library is created.
81 // Set $first to FALSE for reference.
82 $first = FALSE;
83 }
84
85 $rows[$name . $v]['class'] .= ' last';
86 }
87
88 // Add a bit of custom css to overwrite the default 'odd' and 'even' behaviour.
89 drupal_add_css(drupal_get_path('module', 'jqp') .'/jqp.admin.css');
90
91 // Create the table
92 $output = theme('table', $header, $rows, array('class' => 'js_libraries_table multirow'));
93 }
94 else {
95 // Nothing to show.
96 $output = "<p>". t('No javascript libraries installed yet!') ."</p>";
97 }
98
99 return $output;
100 }
101
102 /**
103 * Builds the form which is used to edit a single version of a library
104 */
105 function js_library_edit(&$form_state, $js_library, $version = 0, $op = NULL) {
106
107 $types = array('scripts', 'stylesheets');
108
109 // Used to create an array for form fields which needs to be rendered by the theme function
110 $form['files'] = array('#type' => 'value', "#value" => array());
111
112 $index = 0;
113
114 $form['js_library'] = array('#type' => 'value', "#value" => $js_library);
115 $form['version'] = array('#type' => 'value', "#value" => $version);
116
117 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
118
119 // If operation is 'add' we want to display empty fields.
120 // We overwrite $js_library with an empty placeholder.
121 if ($op == 'add') {
122 $form['add'] = array('#type' => 'value', '#value' => TRUE);
123 _jqp_form_elements($form, $index, array('version' => $version));
124 return $form;
125 }
126
127 foreach ($types as $type) {
128 if (!empty($js_library->info[$type][$version])) {
129 $added_types[] = $type;
130
131 foreach ($js_library->info[$type][$version] as $key => $path) {
132 if ($key !== 'changed') {
133 $values = array(
134 'name' => $key,
135 'path' => $path,
136 'version' => $version,
137 'type' => $type,
138 'operations' => array(
139 l(t('remove'), "admin/build/jqp/$js_library->name/$version/remove_file", array('query' => "type=$type&file=". urlencode($key))),
140 )
141 );
142 _jqp_form_elements($form, $index, $values);
143 $index++;
144 }
145 }
146 }
147 }
148
149 // If this is not the default version (0), and there are files types available which aren't added yet,
150 // we add it here and disable it.
151 if ($version !== 0) {
152 foreach ($types as $type) {
153 if (!in_array($type, $added_types) && isset($js_library->info[$type][0])) {
154
155 foreach ($js_library->info[$type][0] as $key => $path) {
156 if ($key !== 'changed') {
157 _jqp_form_elements($form, -($index), array('name' => $key, 'path' => $path, 'version' => 0));
158 $index++;
159 }
160 }
161 }
162 }
163 }
164
165 $form['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
166
167 return $form;
168 }
169
170 /**
171 * Helper function for dynamically creating the form elements required by a single file.
172 */
173 function _jqp_form_elements(&$form, $index, $values = array()) {
174
175 $form["file-$index-name"] = array(
176 '#title' => t('Name'),
177 '#type' => 'textfield',
178 '#default_value' => $values['name'],
179 '#size' => 10,
180 '#required' => !($index < 0),
181 '#disabled' => ($index < 0),
182 );
183 $form["file-$index-path"] = array(
184 '#title' => t('Path'),
185 '#type' => 'textfield',
186 '#size' => FALSE,
187 '#default_value' => $values['path'],
188 '#autocomplete_path' => 'jqp_autocomplete',
189 '#required' => !($index < 0),
190 '#disabled' => ($index < 0),
191 );
192 $form["file-$index-type"] = array(
193 '#title' => t('Type'),
194 '#type' => 'select',
195 '#default_value' => $values['type'],
196 '#options' => array('scripts' => t('Javascript'), 'stylesheets' => t('Stylesheet')),
197 '#required' => !($index < 0),
198 '#disabled' => ($index < 0),
199 );
200 $form["file-$index-operations"] = array(
201 '#value' => (!empty($values['operations']) ? join($values['operations']) : ''),
202 );
203 $form["file-$index-status"] = array(
204 '#type' => 'value',
205 '#value' => file_exists($values['path']),
206 );
207 $form["file-$index-version"] = array(
208 '#type' => 'value',
209 '#value' => $values['version'],
210 );
211 $form['files']['#value'][$index] = $values['name'];
212 }
213
214 /**
215 * Submit handler for js_library_edit().
216 */
217 function js_library_edit_submit($form, &$form_state) {
218
219 $js_library = $form_state['values']["js_library"];
220 $version = $form_state['values']["version"];
221
222 if ($form_state['values']['op'] == t('Reset')) {
223 $form_state['redirect'] = "admin/build/jqp/$js_library->name/$version/reset";
224 return;
225 }
226
227 foreach (array_keys($form_state['values']['files']) as $index) {
228 if ($index >= 0) {
229 $name = $form_state['values']["file-$index-name"];
230 $path = $form_state['values']["file-$index-path"];
231 $type = $form_state['values']["file-$index-type"];
232
233 $js_library->info[$type][$version][$name] = $path;
234 $js_library->info[$type][$version]['changed'] = TRUE;
235 }
236 }
237
238 db_query("UPDATE {system} SET info = '%s' WHERE type = '%s' AND name = '%s'", serialize($js_library->info), 'javascript library', $js_library->name);
239 drupal_set_message(t('The configuration options have been saved!'));
240 $form_state['redirect'] = "admin/build/jqp";
241
242 if ($form_state['values']['add']) {
243 $form_state['redirect'] .= "/$js_library->name/$version";
244 }
245 }
246
247 /**
248 * Theme function for the js_library_edit form.
249 * Creates a table in which each tr contans all form elements for a single file.
250 */
251 function theme_js_library_edit($form) {
252
253 if (!empty($form['files']['#value'])) {
254 // Set the table header.
255 $header = array(t('Filename'), t('Path'), t('Type'), t('Version'), t('Operations'), t('Status'));
256
257 ksort($form['files']['#value']);
258
259 foreach ($form['files']['#value'] as $index => $file) {
260 $version = $form["file-$index-version"]['#value'];
261 if ($new = ($form['add']['#value'])) {
262 unset($form["file-$index-name"]['#title']);
263 }
264 else {
265 $form["file-$index-name"]['#type'] = 'hidden';
266 $name = $form["file-$index-name"]['#value'];
267 $form["file-$index-type"]['#type'] = 'hidden';
268 $type = $form["file-$index-type"]['#value'];
269 }
270
271 unset($form["file-$index-path"]['#title'], $form["file-$index-type"]['#title']);
272 if ($form['version']['#value'] !== $version) {
273 $version = l(($version == 0 ? t('default') : $version), "admin/build/jqp/". $form['js_library']['#value']->name ."/$version");
274 }
275 else {
276 $version = ($version == 0 ? t('default') : $version);
277 }
278
279 $status = ($form["file-$index-status"]['#value'] ? 'ok' : 'warning');
280 $rows[] = array(
281 'data' => array(
282 $new ? drupal_render($form["file-$index-name"]) : $name,
283 drupal_render($form["file-$index-path"]),
284 $new ? drupal_render($form["file-$index-type"]) : $form["file-$index-type"]['#options'][$type],
285 $version,
286 drupal_render($form["file-$index-operations"]),
287 theme('image', "misc/watchdog-$status.png", $status, $status, NULL, FALSE),
288 ),
289 'class' => $status,
290 );
291 }
292
293 $path = drupal_get_path('module', 'jqp');
294 drupal_add_css("$path/jqp.admin.css");
295 drupal_add_js("$path/jqp.admin.js");
296 drupal_add_js(array('jqp_module_path' => $path), 'setting');
297
298 return theme('table', $header, $rows, array('class' => 'js_libraries_table')) . drupal_render($form);
299 }
300 }
301
302 /**
303 * Wrapper function for all actions which needs confirmation before execution.
304 */
305 function jqp_confirm_form(&$form_state, $js_library = NULL, $version = 0) {
306
307 $form['js_library'] = array('#type' => 'value', '#value' => $js_library);
308 $form['version'] = array('#type' => 'value', '#value' => $version);
309 $form['file'] = array('#type' => 'value', '#value' => $_GET['file']);
310 $form['type'] = array('#type' => 'value', '#value' => $_GET['type']);
311
312 if (isset($js_library, $version, $_GET['file'], $_GET['type'])) {
313 return confirm_form($form, t('Are you sure you want to remove the file %item from %library', array('%item' => $_GET['file'], '%library' => $js_library->info['name'])), "admin/build/jqp/$js_library->name/$version", t('Removal of this file only applies to version !version of this library.', array('!version' => $version)), t('Remove'));
314 }
315 elseif (isset($js_library, $version)) {
316 return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $js_library->info['name'])), "admin/build/jqp/$js_library->name/$version", t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
317 }
318 elseif (arg(3) == 'rebuild_confirm') {
319 $form = array('reset_all' => array('#type' => 'value', '#value' => TRUE));
320 return confirm_form($form, t('Are you sure you want to completely rebuild all javascript libraries?'), "admin/build/jqp", t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
321 }
322 else {
323 drupal_goto('admin/build/jqp');
324 }
325 }
326
327 /**
328 * Submit handler for jqp_confirm_form().
329 */
330 function jqp_confirm_form_submit($form, &$form_state) {
331
332 $js_library = $form_state['values']["js_library"];
333 $version = $form_state['values']["version"];
334 $file = $form_state['values']["file"];
335 $type = $form_state['values']["type"];
336 $op = $form_state['values']['op'];
337 $reset_all = $form_state['values']['reset_all'];
338
339 $form_state['redirect'] = "admin/build/jqp";
340
341 switch ($op) {
342 case t('Remove'):
343 unset($js_library->info[$type][$version][$file]);
344 $message = t('The file has been unattached.');
345 break;
346 case t('Reset'):
347 if (!$reset_all) {
348 foreach (array('scripts', 'stylesheets') as $type) {
349 if (is_array($js_library->info[$type])) {
350 foreach (array_keys($js_library->info[$type]) as $key) {
351 unset($js_library->info[$type][$key]['changed']);
352 }
353 }
354 }
355 $message = t('The javascript library has been reset to its default settings!');
356 }
357 break;
358 }
359 if ($reset_all) {
360 $message = t('All javascript libraries has been reset to their default settings!');
361 }
362 else {
363 db_query("UPDATE {system} SET info = '%s' WHERE filename = '%s'", serialize($js_library->info), $js_library->filename);
364 $form_state['redirect'] .= "/$js_library->name/$version";
365 }
366
367 if ($op == t('Reset')) {
368 jqp_rebuild_cache($reset_all);
369 }
370
371 drupal_set_message($message);
372 }
373
374 /**
375 * AJAX callback function for Drupal.behaviors.libraryCheckInput().
376 *
377 * Validates the path suggested in the input form field from js_library_edit().
378 *
379 * @see jqp.admin.js
380 * @see js_library_edit()
381 */
382 function _jqp_ajax_check_file() {
383 $p = (object)$_POST;
384 if (isset($p->path) && user_access('administer javascript libraries')) {
385
386 $result['result'] = (isset($p->path) && file_exists($p->path) && is_file($p->path));
387
388 if ($result['result'] && isset($p->type)) {
389 $type = ($p->type == t('Javascript') ? '.js' : ($p->type == t('Stylesheet') ? '.css' : FALSE));
390
391 if ($type) {
392 $result['result'] = (strrpos($p->path, "$type") == (drupal_strlen($p->path) - drupal_strlen($type)));
393 }
394 }
395 print drupal_to_js($result);
396 }
397 }
398
399 /**
400 * AJAX response function for #autocomplete form elements.
401 *
402 * Returns all .css and .js file paths matching the search string.
403 */
404 function _jqp_autocomplete() {
405 $matches = array();
406 $args = func_get_args();
407 if (!empty($args)) {
408 $string = join('/', $args);
409 foreach (jqp_scan_dir() as $file) {
410 if (strstr($file->filename, $string)) {
411 $matches[$file->filename] = $file->filename;
412
413 // stop if the matches exceed 10 elements
414 if (count($matches) >= 20) {
415 break;
416 }
417 }
418 }
419 }
420 drupal_json($matches);
421 }

  ViewVC Help
Powered by ViewVC 1.1.2