pages.module 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // $Id: pages.module,v 1.1 2011/01/22 02:30:55 goodman Exp $
  3. /**
  4. * @file
  5. * Add pages in the simplest way.
  6. */
  7. /**
  8. * TODO:
  9. * - Set node's translation instead of new node
  10. * - Full HTML support
  11. */
  12. /**
  13. * Implements hook_enable().
  14. */
  15. function pages_enable() {
  16. $options = array('menu' => 'main-menu');
  17. pages_create($options);
  18. }
  19. /**
  20. * Create pages from text files.
  21. */
  22. function pages_create($options = NULL) {
  23. foreach (pages_scan() as $file) {
  24. pages_create_page($file, $options);
  25. }
  26. }
  27. /**
  28. * Scan pages directories.
  29. */
  30. function pages_scan() {
  31. $files = array();
  32. $searchdir = array();
  33. $searchdir[] = drupal_get_path('module', 'pages') . '/pages';
  34. $searchdir[] = 'sites/default/files/pages';
  35. $nomask = array('.', '..');
  36. foreach ($searchdir as $dir) {
  37. if (is_dir($dir) && $handle = opendir($dir)) {
  38. while (FALSE !== ($file = readdir($handle))) {
  39. if (!in_array($file, $nomask)) {
  40. if (!is_dir("$dir/$file")) {
  41. $files[] = "$dir/$file";
  42. }
  43. }
  44. }
  45. closedir($handle);
  46. }
  47. }
  48. return $files;
  49. }
  50. /**
  51. * Save files as pages.
  52. */
  53. function pages_create_page($path, $options = NULL) {
  54. $node = new StdClass();
  55. $node->type = 'page';
  56. $node->module = 'node';
  57. $node->status = 1;
  58. $basename = basename($path, '.txt');
  59. if (strrchr($basename, '-')) {
  60. $lang = substr($basename, strrpos($basename, '-') + 1);
  61. $title = substr($basename, 0, strrpos($basename, '-'));
  62. }
  63. else {
  64. $lang = 'und';
  65. $title = $basename;
  66. }
  67. // Multi-word title
  68. $title = str_replace('_', ' ', $title);
  69. // Capitalize letters
  70. $title = ucwords($title);
  71. $node->title = $title;
  72. $node->body[$lang][0]['value'] = file_get_contents($path);
  73. $node->language = $lang;
  74. // Check if there is original node and this node should be a translation
  75. if ($lang != 'und') {
  76. $nodes = node_load_multiple(NULL, array('type' => 'page', 'title' => $title));
  77. if (is_array($nodes) && !empty($nodes) && module_exists('translation')) {
  78. $orig_node = array_pop($nodes);
  79. if ($lang != $orig_node->language) {
  80. $translation = array(
  81. 'translate' => 0,
  82. 'status' => 1,
  83. 'language' => $lang,
  84. 'source' => $orig_node->language,
  85. );
  86. $values = array(
  87. // @todo Title not always enabled!
  88. 'title_field' => $node->title,
  89. 'body' => $node->body,
  90. );
  91. $handler = translation_get_handler('node', $orig_node);
  92. $handler->setTranslation($translation, $values);
  93. field_attach_update('page', $node);
  94. // we don't need normal save or menu item for the translation
  95. return;
  96. }
  97. }
  98. }
  99. if (isset($options['menu'])) {
  100. $node->menu['enabled'] = 1;
  101. $node->menu['link_title'] = $node->title;
  102. $node->menu['description'] = $node->title;
  103. $node->menu['menu_name'] = $options['menu'];
  104. $node->menu['parent'] = $options['menu'] . ':0';
  105. $node->menu['mlid'] = 0;
  106. $node->menu['plid'] = 0;
  107. }
  108. if ($node = node_submit($node)) {
  109. node_save($node);
  110. }
  111. }