PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/phreehelp/functions/phreehelp.php

http://phreedom.googlecode.com/
PHP | 222 lines | 195 code | 8 blank | 19 comment | 50 complexity | 6cdd1babe7ecc3f74888ce25076b2374 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  6. // | http://www.PhreeSoft.com |
  7. // +-----------------------------------------------------------------+
  8. // | This program is free software: you can redistribute it and/or |
  9. // | modify it under the terms of the GNU General Public License as |
  10. // | published by the Free Software Foundation, either version 3 of |
  11. // | the License, or any later version. |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  16. // | GNU General Public License for more details. |
  17. // +-----------------------------------------------------------------+
  18. // Path: /modules/phreehelp/functions/phreehelp.php
  19. //
  20. function synchronize() {
  21. global $db;
  22. $config = $db->Execute('TRUNCATE ' . TABLE_PHREEHELP);
  23. // recursively read file and store in db
  24. $extensions = explode(',', VALID_EXTENSIONS);
  25. $file_list = array();
  26. $modules = scandir(DIR_FS_MODULES);
  27. foreach ($modules as $module) {
  28. if ($module <> '.' && $module <> '..') {
  29. if (file_exists(DIR_WS_MODULES . $module . '/language/' . $_SESSION['language'] . '/manual')) {
  30. $file_list = array_merge($file_list, directory_to_array(DIR_WS_MODULES . $module . '/language/' . $_SESSION['language'] . '/manual', $extensions));
  31. } elseif (file_exists(DIR_WS_MODULES . $module . '/language/en_us/manual')) {
  32. $file_list = array_merge($file_list, directory_to_array(DIR_WS_MODULES . $module . '/language/en_us/manual', $extensions));
  33. }
  34. }
  35. }
  36. $toc = array();
  37. foreach ($file_list as $file_name) {
  38. $file_name = str_replace(DOC_REL_PATH, DOC_ROOT_URL, $file_name); // convert to url to read script generated filenames
  39. $tags = get_meta_tags($file_name);
  40. $doc_html = file_get_contents($file_name);
  41. preg_match('/<title>([^>]*)<\/title>/si', $doc_html, $match);
  42. $doc_title = (isset($match) && is_array($match) && count($match) > 0) ? strip_tags($match[1]) : TEXT_NO_TITLE;
  43. $doc_text = trim(strip_tags($doc_html));
  44. $doc_text = str_replace(chr(10), ' ', $doc_text); // process out special characters
  45. $sql = "insert into " . TABLE_PHREEHELP . " (doc_url, doc_pos, doc_index, doc_title, doc_text)
  46. values ('" . $file_name . "', '" . $tags['doc_pos'] . "', '" . $tags['doc_index_1'] . "', '" . $doc_title . "', '" . addslashes($doc_text) . "')";
  47. $row = $db->Execute($sql);
  48. $id = db_insert_id();
  49. $toc[$id] = $tags['doc_pos'];
  50. }
  51. foreach ($toc as $id => $value) {
  52. if (strrpos($value, '.') === false) {
  53. $parent = '0';
  54. } else {
  55. $parent = substr($value, 0, strrpos($value, '.'));
  56. $key = array_search($parent, $toc);
  57. if ($key !== false) { // if no parent found, default to root
  58. $db->Execute("update " . TABLE_PHREEHELP . " set parent_id = " . $key . " where id = " . $id);
  59. $db->Execute("update " . TABLE_PHREEHELP . " set doc_type = '0' where id = " . $key); // set parent to type folder
  60. }
  61. }
  62. }
  63. }
  64. function directory_to_array($directory, $extension = "", $full_path = true) {
  65. $array_items = array();
  66. if (!$contents = scandir($directory)) return $array_items;
  67. foreach ($contents as $file) {
  68. if ($file <> "." && $file <> "..") {
  69. if (is_dir($directory. "/" . $file)) {
  70. $array_items = array_merge($array_items, directory_to_array($directory. "/" . $file, $extension, $full_path));
  71. } else {
  72. $file_ext = substr(strrchr($file, "."), 1);
  73. if (!$extension || in_array($file_ext, $extension)) {
  74. $array_items[] = $full_path ? ($directory . "/" . $file) : $file;
  75. }
  76. }
  77. }
  78. }
  79. return $array_items;
  80. }
  81. function retrieve_toc() {
  82. global $db;
  83. $toc = $db->Execute('select id, parent_id, doc_type, doc_url, doc_title from ' . TABLE_PHREEHELP . '
  84. order by parent_id, doc_pos');
  85. $toc_array = array();
  86. $toc_array[-1][] = array('id' => 0, 'doc_type' => '0', 'doc_title' => TEXT_MANUAL); // home dir
  87. while (!$toc->EOF) {
  88. if (!$dir_only || ($dir_only && $toc->fields['doc_type'] == '0')) {
  89. $toc_array[$toc->fields['parent_id']][] = array(
  90. 'id' => $toc->fields['id'],
  91. 'doc_type' => $toc->fields['doc_type'],
  92. 'doc_url' => $toc->fields['doc_url'],
  93. 'doc_title' => $toc->fields['doc_title'],
  94. );
  95. }
  96. $toc->MoveNext();
  97. }
  98. $toc_string = build_help_tree('dir_tree', $toc_array, $index = -1, $level = 0, $cont_level = array());
  99. return $toc_string;
  100. }
  101. function build_help_tree($name, $full_array, $index = -1, $level = 0, $cont_level = array()) {
  102. $entry_string = '';
  103. for ($j = 0; $j < sizeof($full_array[$index]); $j++) {
  104. $new_ref = $index . '_' . $full_array[$index][$j]['id'];
  105. $cont_temp = array_keys($cont_level);
  106. $entry_string .= '<div style="height:16px;">' . chr(10);
  107. for ($i = 0; $i < $level; $i++) {
  108. if (false) {
  109. } elseif ($i == $level-1 && $j < sizeof($full_array[$index])-1) {
  110. $entry_string .= html_icon('phreebooks/cont-end.gif', '', 'small');
  111. } elseif ($i == $level-1 && $j == sizeof($full_array[$index])-1) {
  112. $entry_string .= html_icon('phreebooks/end-end.gif', '', 'small');
  113. } elseif (in_array($i, $cont_temp)) {
  114. $entry_string .= html_icon('phreebooks/cont.gif', '', 'small');
  115. } elseif ($i < $level-1) {
  116. $entry_string .= html_icon('phreebooks/blank.gif', '', 'small');
  117. }
  118. }
  119. if ($full_array[$index][$j]['doc_type'] == '0') { // folder
  120. $entry_string .= '<a id="imgdc_' . $new_ref . '" href="javascript:Toggle(\'dc_' . $new_ref . '\');">' . html_icon('places/folder.png', TEXT_OPEN, 'small', 'class="draggable"', '', '', 'icndc_' . $new_ref) . '</a>';
  121. } else {
  122. $entry_string .= html_icon('mimetypes/text-x-generic.png', $full_array[$index][$j]['doc_title'], 'small');
  123. }
  124. $short_title = (strlen($full_array[$index][$j]['doc_title']) <= PH_DEFAULT_TRIM_LENGTH) ? $full_array[$index][$j]['doc_title'] : substr($full_array[$index][$j]['doc_title'], 0, PH_DEFAULT_TRIM_LENGTH) . '...';
  125. if ($full_array[$index][$j]['doc_url']) {
  126. $entry_string .= '&nbsp;<a href="' . $full_array[$index][$j]['doc_url'] . '" target="mainFrame">' . htmlspecialchars($short_title) . '</a>' . chr(10);
  127. } else {
  128. $entry_string .= '&nbsp;' . htmlspecialchars($short_title) . chr(10);
  129. }
  130. $entry_string .= '</div>' . chr(10);
  131. if ($j < sizeof($full_array[$index])-1) {
  132. $cont_level[$level-1] = true;
  133. } else {
  134. unset($cont_level[$level-1]);
  135. }
  136. if (isset($full_array[$full_array[$index][$j]['id']])) {
  137. $display_none = ($level == 0) ? '' : 'display:none; ';
  138. $entry_string .= '<div id="dc_' . $new_ref . '" style="' . $display_none . 'margin-left:0px;">' . chr(10);
  139. $entry_string .= build_help_tree($name, $full_array, $full_array[$index][$j]['id'], $level+1, $cont_level) . chr(10);
  140. $entry_string .= '</div>' . chr(10);
  141. }
  142. }
  143. return $entry_string;
  144. }
  145. function build_href($array_tree, $ref = '') {
  146. ksort($array_tree);
  147. $entry_string = '';
  148. foreach ($array_tree as $key => $entry) {
  149. $new_ref = $ref . '_' . $entry['id'];
  150. if (isset($entry['children'])) {
  151. $entry_string .= '<a id="ph_' . $new_ref . '" href="javascript:Toggle(\'' . $new_ref . '\');">' . html_icon('places/folder.png', TEXT_FOLDER, 'small') . '</a>' . chr(10);
  152. } else {
  153. $entry_string .= html_icon('mimetypes/text-html.png', TEXT_DOCUMENT, 'small') . chr(10);
  154. }
  155. if (isset($entry['doc_title'])) {
  156. if (isset($entry['doc_url'])) {
  157. $entry_string .= '<a href="' . $entry['doc_url'] . '" target="mainFrame">' . $entry['doc_title'] . '</a>' . chr(10);
  158. } else {
  159. $entry_string .= $entry['doc_title'] . chr(10);
  160. }
  161. } else {
  162. $entry_string .= TEXT_UNTITLED . chr(10);
  163. }
  164. $entry_string .= '<br />' . chr(10);
  165. if (isset($entry['children'])) {
  166. $entry_string .= '<div id="' . $new_ref . '" style="display:none; margin-left:1px;">' . chr(10) . chr(10);
  167. $entry_string .= build_href($entry['children'], $new_ref) . chr(10);
  168. $entry_string .= '</div>' . chr(10);
  169. }
  170. }
  171. return $entry_string;
  172. }
  173. function retrieve_index() {
  174. global $db;
  175. $index = $db->Execute('select id, doc_url, doc_index from ' . TABLE_PHREEHELP . ' order by doc_index');
  176. $index_array = array();
  177. while (!$index->EOF) {
  178. $element = explode('.', $index->fields['doc_index']);
  179. $index_array[$element[0]]['id'] = $index->fields['id'];
  180. $index_array[$element[0]]['doc_title'] = $element[0];
  181. switch (count($element)) { // currently handles two levels deep
  182. case '1': $index_array[$element[0]]['doc_url'] = $index->fields['doc_url']; break;
  183. case '2':
  184. $index_array[$element[0]]['children'][$element[1]]['doc_url'] = $index->fields['doc_url'];
  185. $index_array[$element[0]]['children'][$element[1]]['doc_title'] = $element[1];
  186. break;
  187. default: // no index specified, ignore
  188. }
  189. $index->MoveNext();
  190. }
  191. $index_string = build_href($index_array, $ref = 'idx');
  192. return $index_string;
  193. }
  194. function search_results($search_text) {
  195. global $db;
  196. if (!$search_text) return '';
  197. $sql = "select id, doc_url, doc_title, MATCH (doc_title, doc_text) AGAINST ('" . $search_text . "') as score
  198. from " . TABLE_PHREEHELP . " where MATCH (doc_title, doc_text) AGAINST ('" . $search_text . "')";
  199. $results = $db->Execute($sql);
  200. if ($results->RecordCount() == 0) return TEXT_NO_RESULTS;
  201. $search_array = array();
  202. $index = 0;
  203. while (!$results->EOF) {
  204. $score = number_format($index->fields['score'], 2);
  205. $search_array[$index]['id'] = $results->fields['id'];
  206. $search_array[$index]['doc_url'] = $results->fields['doc_url'];
  207. $search_array[$index]['doc_title'] = $results->fields['doc_title'];
  208. $index++;
  209. $results->MoveNext();
  210. }
  211. $search_string = build_href($search_array, $ref = 'srch');
  212. return $search_string;
  213. }
  214. ?>