/[drupal]/contributions/modules/deadwood/deadwood.module
ViewVC logotype

Contents of /contributions/modules/deadwood/deadwood.module

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


Revision 1.14 - (show annotations) (download) (as text)
Thu Aug 6 20:57:58 2009 UTC (3 months, 3 weeks ago) by solotandem
Branch: MAIN
CVS Tags: DRUPAL-6--1-6, HEAD
Changes since 1.13: +13 -11 lines
File MIME type: text/x-php
Replace messages to screen with writes to a log file; Add .test as a file extension to convert and sort all extensions; Rename 'files' key to to 'deadwood_files' in hook_requirements(); Removing trailing spaces in all files.
1 <?php
2 // $Id: deadwood.module,v 1.13 2009/03/27 20:07:43 solotandem Exp $
3
4 /**
5 * @file
6 * Generate version upgrade code from 5.x to 6.x.
7 *
8 * Copyright 2008 by Jim Berry ("solotandem", http://drupal.org/user/240748)
9 */
10
11 module_load_include('inc', 'deadwood', 'deadwood');
12 module_load_include('inc', 'deadwood', 'deadwood.help');
13 module_load_include('inc', 'deadwood', 'deadwood.conversions');
14
15 /**
16 * Implementation of hook_perm().
17 */
18 function deadwood_perm() {
19 return array('manage conversions');
20 }
21
22 /**
23 * Implementation of hook_node_info().
24 */
25 function deadwood_node_info() {
26 $info = array();
27 $info['deadwood_category'] = array(
28 'name' => t('Deadwood category'),
29 'module' => 'deadwood',
30 'description' => 'Category for deadwood conversion items.',
31 'title_label' => t('Deadwood category'),
32 'body_label' => t('Description'),
33 );
34 $info['deadwood_item'] = array(
35 'name' => t('Deadwood item'),
36 'module' => 'deadwood',
37 'description' => 'Item for a deadwood conversion category.',
38 'title_label' => t('Deadwood item'),
39 'body_label' => t('Description'),
40 );
41 return $info;
42 }
43
44 /**
45 * Implementation of hook_menu().
46 */
47 function deadwood_menu() {
48 $items = array();
49
50 // Settings related items.
51 $items['admin/settings/deadwood'] = array(
52 'title' => 'Module conversion',
53 'description' => 'Configure the module conversion suite.',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('deadwood_settings'),
56 'access arguments' => array('access administration pages')
57 );
58 $items['admin/settings/deadwood/list'] = array(
59 'title' => 'List',
60 'type' => MENU_DEFAULT_LOCAL_TASK,
61 'weight' => -10
62 );
63 $items['admin/settings/deadwood/api'] = array(
64 'title' => 'API changes',
65 'description' => 'Configure default api changes to make.',
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('deadwood_api_settings'),
68 'access arguments' => array('access administration pages'),
69 'type' => MENU_LOCAL_TASK,
70 'weight' => -9
71 );
72
73 // Conversion items.
74 $items['admin/build/deadwood'] = array(
75 'title' => 'Conversions',
76 'description' => 'Convert module code from version 5.x to 6.x.',
77 'page callback' => 'drupal_get_form',
78 'page arguments' => array('deadwood_conversions'),
79 'access arguments' => array('manage conversions')
80 );
81
82 return $items;
83 }
84
85 /**
86 * Implementation of hook_theme().
87 */
88 function deadwood_theme() {
89 return array(
90 'deadwood_extensions_table' => array(
91 'arguments' => array('form' => NULL)
92 ),
93 'deadwood_conversions_table' => array(
94 'arguments' => array('form' => NULL)
95 )
96 );
97 }
98
99 /**
100 * Implementation of settings form.
101 */
102 function deadwood_settings() {
103 $form = array();
104
105 $path = file_directory_path();
106 $form['deadwood_dir'] = array(
107 '#title' => t('Module input directory'),
108 '#type' => 'textfield',
109 '#required' => TRUE,
110 '#default_value' => variable_get('deadwood_dir', DEADWOOD_IN),
111 '#description' => t('Directory beneath the file system path (!path) in which to upload 5.x module code. Default is deadwood.', array('!path' => $path)),
112 '#size' => 30,
113 '#maxlength' => 255,
114 '#validate' => array('deadwood_validate_in_dir')
115 );
116
117 $form['goodwood_dir'] = array(
118 '#title' => t('Module output directory'),
119 '#type' => 'textfield',
120 '#required' => TRUE,
121 '#default_value' => variable_get('goodwood_dir', DEADWOOD_OUT),
122 '#description' => t('Directory beneath the file system path (!path) in which to save the converted 6.x module code. Default is goodwood.', array('!path' => $path)),
123 '#size' => 30,
124 '#maxlength' => 255,
125 '#validate' => array('deadwood_validate_out_dir'),
126 );
127
128 return system_settings_form($form);
129 }
130
131 /**
132 * Implementation of settings form sumission.
133 *
134 * Rename module input and output directories based on user settings.
135 */
136 function deadwood_settings_submit($form, &$form_state) {
137 $values = $form_state['values'];
138
139 $cur = variable_get('deadwood_dir', DEADWOOD_IN);
140 $new = $values['deadwood_dir'];
141 if ($new != $cur) {
142 $cur = file_directory_path() . '/' . $cur;
143 $new = file_directory_path() . '/' . $new;
144 rename($cur, $new);
145 }
146
147 $cur = variable_get('goodwood_dir', DEADWOOD_OUT);
148 $new = $values['goodwood_dir'];
149 if ($new != $cur) {
150 $cur = file_directory_path() . '/' . $cur;
151 $new = file_directory_path() . '/' . $new;
152 rename($cur, $new);
153 }
154 }
155
156 /**
157 * Implementation of api settings form.
158 */
159 function deadwood_api_settings() {
160 $form = array();
161
162 $form['conversions'] = array(
163 '#type' => 'fieldset',
164 '#title' => t('Default API conversions to apply'),
165 '#collapsible' => TRUE,
166 '#collapsed' => FALSE,
167 '#tree' => TRUE
168 );
169
170 $form['conversions']['table'] = deadwood_conversions_table('vid');
171
172 $form['submit'] = array(
173 '#type' => 'submit',
174 '#value' => t('Save configuration')
175 );
176
177 $form['reload'] = array(
178 '#type' => 'submit',
179 '#value' => t('Reload the API changes')
180 );
181
182 return $form;
183 }
184
185 /**
186 * Implementation of api settings form sumission.
187 *
188 * Store default api conversions to apply based on user settings.
189 */
190 function deadwood_api_settings_submit($form, &$form_state) {
191 if ($form_state['values']['op'] == $form_state['values']['reload']) {
192 module_load_include('install', 'deadwood', 'deadwood');
193 deadwood_load_categories(FALSE);
194 return;
195 }
196
197 $values = $form_state['values']['conversions']['table'];
198 if (deadwood_categories_update($values)) {
199 drupal_set_message(t('The configuration options have been saved.'));
200 }
201 }
202
203 /**
204 * Display the module conversion form.
205 */
206 function deadwood_conversions(&$form_state) {
207 // Set default values.
208 list($extensions, $directory, $conversions) = deadwood_conversions_defaults($form_state);
209
210 $form = array();
211
212 $form['extensions'] = array(
213 '#type' => 'item',
214 '#tree' => TRUE,
215 '#theme' => array('deadwood_extensions_table')
216 );
217 // TODO Refactor this to a helper function?
218 $types = array(
219 'inc' => 'PHP code files',
220 'info' => 'Info files used with module installation',
221 'install' => 'PHP code files used with module installation, update and uninstallation',
222 'module' => 'PHP code files',
223 'php' => 'PHP code files',
224 'profile' => 'PHP code files used with site installation',
225 'test' => 'SimpleTest files',
226 'theme' => 'PHP code files used with theming',
227 );
228 foreach ($types as $key => $type) {
229 $row = array();
230 $row['include'] = array(
231 '#type' => 'checkbox',
232 '#default_value' => $extensions[$key]
233 );
234 $row['title'] = array(
235 '#type' => 'item',
236 '#title' => t('Title'),
237 '#value' => $key
238 );
239 $row['description'] = array(
240 '#type' => 'item',
241 '#title' => t('Description'),
242 '#value' => $type
243 );
244
245 $form['extensions'][$key] = $row;
246 }
247
248 $options = array();
249 $path = realpath(file_directory_path() . '/' . variable_get('deadwood_dir', DEADWOOD_IN));
250 $dirs = deadwood_scan_directory($path);
251 foreach ($dirs as $dir) {
252 $options[$dir] = $dir;
253 }
254 if (!$dirs) {
255 drupal_set_message(t('Please place modules to be converted in @path.', array('@path' => $path)), 'error');
256 }
257
258 $form['directory'] = array(
259 '#title' => t('Directory'),
260 '#type' => 'select',
261 '#options' => $options,
262 '#default_value' => $directory,
263 '#description' => t('Directory beneath the module input path (!path) in which to convert the selected files to 6.x code.', array('!path' => $path))
264 );
265
266 $form['conversions'] = array(
267 '#type' => 'fieldset',
268 '#title' => t('Conversions to apply'),
269 '#collapsible' => TRUE,
270 '#collapsed' => TRUE,
271 '#tree' => TRUE
272 );
273
274 $form['conversions']['table'] = deadwood_conversions_table('hook', $conversions);
275
276 $form['convert'] = array(
277 '#type' => 'submit',
278 '#value' => t('Convert files')
279 );
280
281 // TODO Enable this button when code is ready.
282 $form['format'] = array(
283 '#type' => 'submit',
284 '#value' => t('Format files'),
285 '#disabled' => TRUE
286 );
287
288 return $form;
289 }
290
291 /**
292 * Implementation of module conversion form validation.
293 */
294 function deadwood_conversions_validate($form, &$form_state) {
295 // Gather the file types to convert based on extension.
296 $extensions = array();
297 $values = $form_state['values']['extensions'];
298 foreach ($values as $key => $value) {
299 if ($value['include']) {
300 $extensions[] = $key;
301 }
302 }
303 if (!count($extensions)) {
304 form_set_error('extensions', t('Please select at least one file extension.'));
305 }
306
307 // Gather the conversions to apply.
308 $conversions = array();
309 $values = $form_state['values']['conversions']['table'];
310 foreach ($values as $key => $value) {
311 if (strpos($key, '_missing') === 0) {
312 continue;
313 }
314 if ($value['include']) {
315 $conversions[] = $key;
316 }
317 }
318 if ($form_state['values']['op'] != $form_state['values']['format'] && !count($conversions)) {
319 form_set_error('conversions', t('Please select at least one conversion to apply.'));
320 }
321 }
322
323 /**
324 * Execute the module conversion code.
325 */
326 function deadwood_conversions_submit($form, &$form_state) {
327 // Rebuild form with user selections.
328 $form_state['rebuild'] = TRUE;
329
330 // Define the input and output directories.
331 $dirname = file_directory_path() . '/' . variable_get('deadwood_dir', DEADWOOD_IN) . '/' . $form_state['values']['directory'];
332 $newdirname = file_directory_path() . '/' . variable_get('goodwood_dir', DEADWOOD_OUT) . '/' . $form_state['values']['directory'];
333
334 $params = array();
335
336 // Gather the file types to convert based on extension.
337 $extensions = array();
338 $values = $form_state['values']['extensions'];
339 foreach ($values as $key => $value) {
340 if ($value['include']) {
341 $extensions[] = $key;
342 }
343 }
344 $params['extensions'] = $extensions;
345
346 // Gather the conversions to apply.
347 $conversions = array();
348 $values = $form_state['values']['conversions']['table'];
349 foreach ($values as $key => $value) {
350 if (strpos($key, '_missing') === 0) {
351 continue;
352 }
353 if ($value['include']) {
354 $conversions[] = $key;
355 }
356 }
357 $params['conversions'] = $conversions;
358
359 // Apply format functions.
360 if ($form_state['values']['op'] == $form_state['values']['format']) {
361 module_load_include('inc', 'deadwood', 'deadwood.format');
362 $params['conversions'] = array('format');
363 deadwood_convert_dir($dirname, $newdirname, $params);
364 drupal_set_message(t('Module format code was run.'));
365 }
366 // Apply conversion functions.
367 else {
368 deadwood_convert_dir($dirname, $newdirname, $params);
369 drupal_set_message(t('Module conversion code was run.'));
370 }
371 }
372
373 /**
374 * Set the default file extensions to convert.
375 *
376 * @return array of default values.
377 */
378 function deadwood_conversions_defaults($form_state) {
379 // Set defaults when form is first loaded.
380 $extensions = array(
381 'inc' => TRUE,
382 'info' => TRUE,
383 'install' => TRUE,
384 'module' => TRUE,
385 'php' => FALSE,
386 'profile' => FALSE,
387 'test' => FALSE,
388 'theme' => FALSE,
389 );
390 $directory = 'example';
391 $conversions = array(); // If not set below, will be set in deadwood_conversions_table.
392
393 // Set defaults from submitted values.
394 if (isset($form_state['values'])) {
395 if (isset($form_state['values']['extensions'])) {
396 $values = $form_state['values']['extensions'];
397 foreach ($values as $key => $value) {
398 $extensions[$key] = $value['include'];
399 }
400 }
401 if (isset($form_state['values']['directory'])) {
402 $directory = $form_state['values']['directory'];
403 }
404 if (isset($form_state['values']['conversions']['table'])) {
405 $values = $form_state['values']['conversions']['table'];
406 foreach ($values as $key => $value) {
407 $conversions[$key] = $value['include'];
408 }
409 }
410 }
411
412 return array($extensions, $directory, $conversions);
413 }
414
415 /**
416 * Theme the conversion file extension form.
417 *
418 * @return HTML output.
419 */
420 function theme_deadwood_extensions_table($form) {
421 $select_header = theme('table_select_header_cell');
422 $header = array($select_header, t('Extension'), t('Description'));
423 $rows = array();
424 foreach (element_children($form) as $key) {
425 $task = &$form[$key];
426
427 $row = array();
428 $row[] = drupal_render($task['include']);
429 $row[] = '<strong><label for="'. $task['include']['#id'] .'">'. $task['title']['#value'] .'</label></strong>';
430 $row[] = $task['description']['#value'];
431
432 $rows[] = $row;
433 }
434
435 return theme('table', $header, $rows);
436 }
437
438 /**
439 * The deadwood conversions form.
440 */
441 function deadwood_conversions_table($index = 'hook', $includes = array()) {
442 $form = array();
443
444 $form = array(
445 '#type' => 'item',
446 '#theme' => array('deadwood_conversions_table'),
447 );
448
449 $categories = deadwood_categories_load();
450 foreach ($categories as $category) {
451 $row = array();
452 $row['include'] = array(
453 '#type' => 'checkbox',
454 '#default_value' => isset($includes[$category->hook]) ? $includes[$category->hook] : $category->include
455 );
456 $row['title'] = array(
457 '#type' => 'item',
458 '#title' => t('Title'),
459 '#value' => l($category->title, 'node/' . $category->nid . '/edit')
460 );
461 $row['status'] = array(
462 '#type' => 'item',
463 '#title' => t('Status'),
464 '#value' => deadwood_get_code_availability_status($category->code_status),
465 '#class' => deadwood_get_code_availability_class($category->code_status)
466 );
467
468 $form[$category->$index] = $row;
469 }
470
471 return $form;
472 }
473
474 /**
475 * Theme the conversion execution form.
476 *
477 * @return HTML output.
478 */
479 function theme_deadwood_conversions_table($form) {
480 $select_header = theme('table_select_header_cell');
481 $category_header = array('data' => t('Category'), 'style' => 'width: 70%');
482 $header = array($select_header, $category_header, t('Conversion Code'));
483 $rows = array();
484 foreach (element_children($form) as $key) {
485 $task = &$form[$key];
486
487 $row = array();
488 $row[] = drupal_render($task['include']);
489 $row[] = $task['title']['#value'];
490 $row[] = array('header' => '', 'data' => $task['status']['#value']);
491
492 $rows[] = array('data' => $row, 'class' => $task['status']['#class']);
493 }
494 $attributes = array('class' => 'deadwood-conversions');
495 drupal_add_css(drupal_get_path('module', 'deadwood') .'/deadwood.css', 'module');
496
497 return theme('table', $header, $rows, $attributes);
498 }
499
500 /**
501 * Load the module conversion categories.
502 *
503 * @return array List of task node objects.
504 */
505 function deadwood_categories_load() {
506 $sql = 'SELECT c.nid, c.vid, weight, include, code_status, hook, title, body
507 FROM {dw_category} c
508 JOIN {node_revisions} n ON n.vid = c.vid
509 ORDER BY weight';
510 $result = db_query($sql);
511
512 $tasks = array();
513 while ($task = db_fetch_object($result)) {
514 $tasks[] = $task;
515 }
516 return $tasks;
517 }
518
519 /**
520 * Update a module conversion category.
521 *
522 * @param array $values Array indexed by vid with a value of $value['include'].
523 * @return TRUE if all updates succeded.
524 */
525 function deadwood_categories_update($values = array()) {
526 if (!isset($values) || !is_array($values)) {
527 return;
528 }
529
530 $success = TRUE;
531 $sql = 'UPDATE {dw_category} SET include = %d WHERE vid = %d';
532 foreach ($values as $vid => $include) {
533 if (!db_query($sql, $include['include'], $vid)) {
534 drupal_set_message(t('Update failed for category with vid = @vid', array('@vid' => $vid)), 'error');
535 $success = FALSE;
536 }
537 }
538 return $success;
539 }
540
541 // TODO Move these node hooks to a category.inc file when we implement dw_items
542 // node hooks in an item.inc file.
543
544 /**
545 * Implementation of hook_form().
546 *
547 * This hook displays the form necessary to create/edit the deadwood category.
548 */
549 function deadwood_form(&$node, $form_state) {
550 $type = node_get_types('type', $node);
551 // $editing = isset($node->nid);
552
553 // Eliminate warnings about undefined properties when a node is created.
554 if (!isset($node->nid)) {
555 $node->weight = '';
556 $node->include = 1;
557 $node->code_status = 0;
558 $node->hook = '';
559 }
560
561 $form = array();
562
563 $form['title'] = array(
564 '#type' => 'textfield',
565 '#title' => check_plain($type->title_label),
566 '#required' => TRUE,
567 '#default_value' => $node->title,
568 );
569 $form['body_filter']['body'] = array(
570 '#type' => 'textarea',
571 '#title' => check_plain($type->body_label),
572 '#required' => FALSE,
573 '#default_value' => $node->body,
574 );
575
576 $form['body_filter']['format'] = filter_form($node->format);
577
578 $form['weight'] = array(
579 '#type' => 'textfield',
580 '#title' => t('Weight'),
581 '#required' => TRUE,
582 '#default_value' => $node->weight,
583 '#description' => t('Weight for sorting when viewing a list of categories.'),
584 '#maxlength' => 5,
585 '#size' => 2,
586 '#attributes' => array('style' => 'width: auto;'), // Otherwise set to 90% by Drupal style sheet
587 );
588 $form['include'] = array(
589 '#type' => 'checkbox',
590 '#title' => t('Include'),
591 '#default_value' => $node->include,
592 '#description' => t('Check the box to include this category in the set of default conversions applied.'),
593 );
594 $form['code_status'] = array(
595 '#type' => 'select',
596 '#title' => t('Code conversion availability status'),
597 '#default_value' => $node->code_status,
598 '#options' => deadwood_get_code_statuses(),
599 '#description' => t('Code conversion availability status.'),
600 );
601 $form['hook'] = array(
602 '#type' => 'textfield',
603 '#title' => t('Hook'),
604 '#required' => TRUE,
605 '#default_value' => $node->hook,
606 '#description' => t('Name of the hook function to be called to handle conversions in this category. Example: enter "example" to call the function "deadwood_convert_example."'),
607 );
608
609 return $form;
610 }
611
612 /**
613 * Implementation of hook_load().
614 *
615 * Load the deadwood category-specific data into the node object.
616 */
617 function deadwood_load($node) {
618 $category = db_fetch_object(db_query('SELECT * FROM {dw_category} WHERE vid = %d', $node->vid));
619
620 return $category;
621 }
622
623 /**
624 * Implementation of hook_validate().
625 */
626 function deadwood_validate($node, &$form) {
627 // Should we require the weight to be unique or the hook to be defined?
628 }
629
630 /**
631 * Implementation of hook_insert().
632 *
633 * This is called upon node creation.
634 */
635 function deadwood_insert($node) {
636 db_query("INSERT INTO {dw_category} (nid, vid, weight, include, code_status, hook) VALUES (%d, %d, %d, %d, %d, '%s')", $node->nid, $node->vid, $node->weight, $node->include, $node->code_status, $node->hook);
637 }
638
639 /**
640 * Implementation of hook_update().
641 *
642 * This is called upon node editing.
643 */
644 function deadwood_update($node) {
645 db_query("UPDATE {dw_category} SET weight = %d, include = %d, code_status = %d, hook = '%s' WHERE vid = %d", $node->weight, $node->include, $node->code_status, $node->hook, $node->vid);
646 }
647
648 /**
649 * Implementation of hook_delete().
650 */
651 function deadwood_delete(&$node) {
652 // TODO Should the node parameter be passed by reference?
653 db_query("DELETE FROM {dw_item} WHERE nid = %d", $node->nid);
654 db_query("DELETE FROM {dw_category} WHERE nid = %d", $node->nid);
655 }
656
657 /**
658 * Implementation of hook_view().
659 */
660 function deadwood_view($node, $teaser = FALSE, $page = FALSE) {
661 // drupal_add_css(drupal_get_path('module', 'deadwood') .'/deadwood.css', 'module');
662
663 $node = node_prepare($node, $teaser);
664
665 $statuses = deadwood_get_code_statuses();
666 $node->content['include'] = array(
667 '#type' => 'item',
668 '#value' => t('Include this category in the set of default conversions applied: !include', array('!include' => $node->include ? 'True' : 'False')),
669 '#weight' => 1,
670 );
671 $node->content['code_status'] = array(
672 '#type' => 'item',
673 '#value' => t('Code conversion availability status: !status', array('!status' => $statuses[$node->code_status])),
674 '#weight' => 2,
675 );
676 $node->content['hook'] = array(
677 '#type' => 'item',
678 '#value' => t('Function hook: deadwood_convert_!hook', array('!hook' => $node->hook)),
679 '#weight' => 3,
680 );
681
682 return $node;
683 }
684
685 /**
686 * Implementation of hook_form_alter().
687 */
688 function deadwood_form_alter(&$form, &$form_state, $form_id) {
689
690 }

  ViewVC Help
Powered by ViewVC 1.1.2