PageRenderTime 46ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions/articles.php

https://github.com/vaughnpaul/NOS
PHP | 249 lines | 158 code | 43 blank | 48 comment | 48 complexity | 9e5c3834fba260c25c21f17e7732cf60 MD5 | raw file
  1. <?php
  2. /*
  3. $Id: articles.php, v1.0 2003/12/04 12:00:00 ra Exp $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2003 osCommerce
  7. Released under the GNU General Public License
  8. */
  9. // Parse and secure the tPath parameter values
  10. function tep_parse_topic_path($tPath) {
  11. // make sure the topic IDs are integers
  12. $tPath_array = array_map('tep_string_to_int', explode('_', $tPath));
  13. // make sure no duplicate topic IDs exist which could lock the server in a loop
  14. $tmp_array = array();
  15. $n = sizeof($tPath_array);
  16. for ($i=0; $i<$n; $i++) {
  17. if ($tPath_array[$i] == 0) continue;
  18. if (!in_array($tPath_array[$i], $tmp_array)) {
  19. $tmp_array[] = $tPath_array[$i];
  20. }
  21. }
  22. return $tmp_array;
  23. }
  24. ////
  25. // Generate a path to topics
  26. // TABLES: topics
  27. function tep_get_topic_path($current_topic_id = '') {
  28. global $tPath_array;
  29. if (tep_not_null($current_topic_id)) {
  30. $cp_size = sizeof($tPath_array);
  31. if ($cp_size == 0) {
  32. $tPath_new = $current_topic_id;
  33. } else {
  34. $tPath_new = '';
  35. $last_topic_query = tep_db_query("select parent_id from " . TABLE_TOPICS . " where topics_id = '" . (int)$tPath_array[($cp_size-1)] . "'");
  36. $last_topic = tep_db_fetch_array($last_topic_query);
  37. $current_topic_query = tep_db_query("select parent_id from " . TABLE_TOPICS . " where topics_id = '" . (int)$current_topic_id . "'");
  38. $current_topic = tep_db_fetch_array($current_topic_query);
  39. if ($last_topic['parent_id'] == $current_topic['parent_id']) {
  40. for ($i=0; $i<($cp_size-1); $i++) {
  41. $tPath_new .= '_' . $tPath_array[$i];
  42. }
  43. } else {
  44. for ($i=0; $i<$cp_size; $i++) {
  45. $tPath_new .= '_' . $tPath_array[$i];
  46. }
  47. }
  48. $tPath_new .= '_' . $current_topic_id;
  49. if (substr($tPath_new, 0, 1) == '_') {
  50. $tPath_new = substr($tPath_new, 1);
  51. }
  52. }
  53. } else {
  54. $tPath_new = implode('_', $tPath_array);
  55. }
  56. return 'tPath=' . $tPath_new;
  57. }
  58. ////
  59. // Return the number of articles in a topic
  60. // TABLES: articles, articles_to_topics, topics
  61. function tep_count_articles_in_topic($topic_id, $include_inactive = false) {
  62. $articles_count = 0;
  63. if ($include_inactive == true) {
  64. $articles_query = tep_db_query("select count(*) as total from " . TABLE_ARTICLES . " a, " . TABLE_ARTICLES_TO_TOPICS . " a2t where (a.articles_date_available IS NULL or to_days(a.articles_date_available) <= to_days(now())) and a.articles_id = a2t.articles_id and a2t.topics_id = '" . (int)$topic_id . "'");
  65. } else {
  66. $articles_query = tep_db_query("select count(*) as total from " . TABLE_ARTICLES . " a, " . TABLE_ARTICLES_TO_TOPICS . " a2t where (a.articles_date_available IS NULL or to_days(a.articles_date_available) <= to_days(now())) and a.articles_id = a2t.articles_id and a.articles_status = '1' and a2t.topics_id = '" . (int)$topic_id . "'");
  67. }
  68. $articles = tep_db_fetch_array($articles_query);
  69. $articles_count += $articles['total'];
  70. $child_topics_query = tep_db_query("select topics_id from " . TABLE_TOPICS . " where parent_id = '" . (int)$topic_id . "'");
  71. if (tep_db_num_rows($child_topics_query)) {
  72. while ($child_topics = tep_db_fetch_array($child_topics_query)) {
  73. $articles_count += tep_count_articles_in_topic($child_topics['topics_id'], $include_inactive);
  74. }
  75. }
  76. return $articles_count;
  77. }
  78. ////
  79. // Return true if the topic has subtopics
  80. // TABLES: topics
  81. function tep_has_topic_subtopics($topic_id) {
  82. $child_topic_query = tep_db_query("select count(*) as count from " . TABLE_TOPICS . " where parent_id = '" . (int)$topic_id . "'");
  83. $child_topic = tep_db_fetch_array($child_topic_query);
  84. if ($child_topic['count'] > 0) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. ////
  91. // Return all topics
  92. // TABLES: topics, topic_descriptions
  93. function tep_get_topics($topics_array = '', $parent_id = '0', $indent = '') {
  94. global $languages_id;
  95. if (!is_array($topics_array)) $topics_array = array();
  96. $topics_query = tep_db_query("select t.topics_id, td.topics_name from " . TABLE_TOPICS . " t, " . TABLE_TOPICS_DESCRIPTION . " td where parent_id = '" . (int)$parent_id . "' and t.topics_id = td.topics_id and td.language_id = '" . (int)$languages_id . "' order by sort_order, td.topics_name");
  97. while ($topics = tep_db_fetch_array($topics_query)) {
  98. $topics_array[] = array('id' => $topics['topics_id'],
  99. 'text' => $indent . $topics['topics_name']);
  100. if ($topics['topics_id'] != $parent_id) {
  101. $topics_array = tep_get_topics($topics_array, $topics['topics_id'], $indent . '&nbsp;&nbsp;');
  102. }
  103. }
  104. return $topics_array;
  105. }
  106. ////
  107. // Return all authors
  108. // TABLES: authors
  109. function tep_get_authors($authors_array = '') {
  110. if (!is_array($authors_array)) $authors_array = array();
  111. $authors_query = tep_db_query("select authors_id, authors_name from " . TABLE_AUTHORS . " order by authors_name");
  112. while ($authors = tep_db_fetch_array($authors_query)) {
  113. $authors_array[] = array('id' => $authors['authors_id'], 'text' => $authors['authors_name']);
  114. }
  115. return $authors_array;
  116. }
  117. ////
  118. // Return all subtopic IDs
  119. // TABLES: topics
  120. function tep_get_subtopics(&$subtopics_array, $parent_id = 0) {
  121. $subtopics_query = tep_db_query("select topics_id from " . TABLE_TOPICS . " where parent_id = '" . (int)$parent_id . "'");
  122. while ($subtopics = tep_db_fetch_array($subtopics_query)) {
  123. $subtopics_array[sizeof($subtopics_array)] = $subtopics['topics_id'];
  124. if ($subtopics['topics_id'] != $parent_id) {
  125. tep_get_subtopics($subtopics_array, $subtopics['topics_id']);
  126. }
  127. }
  128. }
  129. ////
  130. // Recursively go through the topics and retreive all parent topic IDs
  131. // TABLES: topics
  132. function tep_get_parent_topics(&$topics, $topics_id) {
  133. $parent_topics_query = tep_db_query("select parent_id from " . TABLE_TOPICS . " where topics_id = '" . (int)$topics_id . "'");
  134. while ($parent_topics = tep_db_fetch_array($parent_topics_query)) {
  135. if ($parent_topics['parent_id'] == 0) return true;
  136. $topics[sizeof($topics)] = $parent_topics['parent_id'];
  137. if ($parent_topics['parent_id'] != $topics_id) {
  138. tep_get_parent_topics($topics, $parent_topics['parent_id']);
  139. }
  140. }
  141. }
  142. ////
  143. // Construct a topic path to the article
  144. // TABLES: articles_to_topics
  145. function tep_get_article_path($articles_id) {
  146. $tPath = '';
  147. $topic_query = tep_db_query("select a2t.topics_id from " . TABLE_ARTICLES . " a, " . TABLE_ARTICLES_TO_TOPICS . " a2t where a.articles_id = '" . (int)$articles_id . "' and a.articles_status = '1' and a.articles_id = a2t.articles_id limit 1");
  148. if (tep_db_num_rows($topic_query)) {
  149. $topic = tep_db_fetch_array($topic_query);
  150. $topics = array();
  151. tep_get_parent_topics($topics, $topic['topics_id']);
  152. $topics = array_reverse($topics);
  153. $tPath = implode('_', $topics);
  154. if (tep_not_null($tPath)) $tPath .= '_';
  155. $tPath .= $topic['topics_id'];
  156. }
  157. return $tPath;
  158. }
  159. ////
  160. // Return an article's name
  161. // TABLES: articles
  162. function tep_get_articles_name($article_id, $language = '') {
  163. global $languages_id;
  164. if (empty($language)) $language = $languages_id;
  165. $article_query = tep_db_query("select articles_name from " . TABLE_ARTICLES_DESCRIPTION . " where articles_id = '" . (int)$article_id . "' and language_id = '" . (int)$language . "'");
  166. $article = tep_db_fetch_array($article_query);
  167. return $article['articles_name'];
  168. }
  169. ////
  170. //! Cache the articles box
  171. // Cache the articles box
  172. function tep_cache_topics_box($auto_expire = false, $refresh = false) {
  173. global $tPath, $language, $languages_id, $tree, $tPath_array, $topics_string;
  174. if (($refresh == true) || !read_cache($cache_output, 'topics_box-' . $language . '.cache' . $tPath, $auto_expire)) {
  175. ob_start();
  176. // include(DIR_WS_BOXES . 'articles.php');
  177. include(DIR_WS_BOXES . FILENAME_ARTICLESS);
  178. $cache_output = ob_get_contents();
  179. ob_end_clean();
  180. write_cache($cache_output, 'topics_box-' . $language . '.cache' . $tPath);
  181. }
  182. return $cache_output;
  183. }
  184. ////
  185. //! Cache the authors box
  186. // Cache the authors box
  187. function tep_cache_authors_box($auto_expire = false, $refresh = false) {
  188. global $language;
  189. $authors_id = '';
  190. if (isset($_GET['authors_id']) && tep_not_null($_GET['authors_id'])) {
  191. $authors_id = (int)$_GET['authors_id'];
  192. }
  193. if (($refresh == true) || !read_cache($cache_output, 'authors_box-' . $language . '.cache' . $authors_id, $auto_expire)) {
  194. ob_start();
  195. //include(DIR_WS_BOXES . 'authors.php');
  196. include(DIR_WS_BOXES . FILENAME_AUTHORS);
  197. $cache_output = ob_get_contents();
  198. ob_end_clean();
  199. write_cache($cache_output, 'authors_box-' . $language . '.cache' . $authors_id);
  200. }
  201. return $cache_output;
  202. }
  203. ?>