PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/TNX_Family/modules/Search/libraries/tags.php

https://gitlab.com/BGCX261/zillatek-project-svn-to-git
PHP | 367 lines | 132 code | 90 blank | 145 comment | 12 complexity | bed2920f76f9587f4ea9246a26e44dc0 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Ionize Search module tags
  4. *
  5. * This class define the Search module tags
  6. *
  7. * @package Ionize
  8. * @author Ionize Dev Team
  9. * @license http://ionizecms.com/doc-license
  10. * @link http://ionizecms.com
  11. * @since Version 0.9.5
  12. *
  13. *
  14. */
  15. /**
  16. * Search TagManager
  17. *
  18. */
  19. class Search_Tags extends TagManager
  20. {
  21. /**
  22. * Base search module tag
  23. * The index function of this class refers to the <ion:search /> tag
  24. * In other words, this function makes the <ion:search /> tag available as main module parent tag
  25. * for all other tags defined in this class.
  26. *
  27. * @usage <ion:search >
  28. * ...
  29. * </ion:search>
  30. *
  31. */
  32. public static function index(FTL_Binding $tag)
  33. {
  34. $str = $tag->expand();
  35. return $str;
  36. }
  37. // ------------------------------------------------------------------------
  38. /**
  39. * Search form tag
  40. *
  41. * Returns the search form view
  42. *
  43. * @usage <ion:searchform />
  44. *
  45. */
  46. public static function searchform(FTL_Binding $tag)
  47. {
  48. $CI =& get_instance();
  49. $searchForm_action = (isset($tag->attr['result_page']) ) ? $tag->attr['result_page'] : '';
  50. $tag->locals->result_page = $searchForm_action;
  51. // If the realm data was posted, the form will not be displayed
  52. // Useful when results should be displayed on the same page as the search form
  53. if ($realm = $CI->input->post('realm'))
  54. {
  55. return '';
  56. }
  57. else
  58. {
  59. $tag->expand();
  60. // the tag returns the content of this view :
  61. return $tag->parse_as_nested(file_get_contents(MODPATH.'Search/views/search_form'.EXT));
  62. }
  63. }
  64. // ------------------------------------------------------------------------
  65. /**
  66. * Form status message tag
  67. * Useful if the form is sent again after an unsuccessful search
  68. *
  69. */
  70. public static function status_message(FTL_Binding $tag)
  71. {
  72. // Local results are set : Means the search process was done
  73. // Because the form will only be displayed again after unsuccesful search, if the var is set, it means no results were found.
  74. if ( isset($tag->locals->results))
  75. {
  76. return lang('module_search_message_no_results');
  77. }
  78. else
  79. {
  80. return lang('module_search_fill_the_field');
  81. }
  82. }
  83. // ------------------------------------------------------------------------
  84. /**
  85. * Form action tag
  86. * Used to display the results on a new page
  87. *
  88. */
  89. public static function result_page(FTL_Binding $tag)
  90. {
  91. // Local results are set : Means the search process was done
  92. if ( isset($tag->locals->result_page))
  93. {
  94. return $tag->locals->result_page;
  95. }
  96. else
  97. {
  98. return "";
  99. }
  100. }
  101. // ------------------------------------------------------------------------
  102. /**
  103. * Search results tag
  104. * Parent tag for results
  105. *
  106. *
  107. */
  108. public static function searchresults(FTL_Binding $tag)
  109. {
  110. $CI =& get_instance();
  111. $str = '';
  112. // Add the realm to the local var, so it can be retrieved
  113. $tag->locals->realm = $CI->input->post('realm');
  114. // Did we got the POST realm ?
  115. $realm = $CI->input->post('realm');
  116. $tag->locals->results = array();
  117. if ($realm !== FALSE && $realm != '')
  118. {
  119. // Loads the serach module model
  120. $CI->load->model('search_model', '', true);
  121. // Get the results
  122. $articles = $CI->search_model->get_articles($realm);
  123. // If result, expand the tag
  124. if ( ! empty($articles))
  125. {
  126. // arrays of keys, for multisorting
  127. $knum = $kdate = array();
  128. foreach($articles as $key => &$article)
  129. {
  130. // set number of found words
  131. preg_match_all('#'.$tag->locals->realm.'#i', $article['title'].' '.$article['content'], $match);
  132. $num = count($match[0]);
  133. $article['nb_found'] = $knum[$key] = $num;
  134. $kdate[$key] = strtotime($article['date']);
  135. }
  136. // Sort the results by realm occurences DESC first, by date DESC second.
  137. array_multisort($knum, SORT_DESC, SORT_NUMERIC, $kdate, SORT_DESC, SORT_NUMERIC, $articles);
  138. // Put the results to the local results var
  139. $tag->locals->results = $articles;
  140. $tag->locals->nb_results = count($articles);
  141. }
  142. // If no results, display a message or returns another view, the form view, whatever you want in fact...
  143. else
  144. {
  145. // Do nothing, the tag "no_result" will display the message or the form again.
  146. }
  147. }
  148. $str .= $tag->expand();
  149. return $str;
  150. }
  151. // ------------------------------------------------------------------------
  152. /**
  153. * Nested in <ion:searchresults> tag
  154. * Feeds each result
  155. *
  156. * @usage : <ion:searchresults>
  157. * <ion:results>
  158. * <ion:title/>
  159. * ...
  160. * </ion:results>
  161. * </ion:searchresults>
  162. *
  163. */
  164. public static function results(FTL_Binding $tag)
  165. {
  166. $str = '';
  167. foreach($tag->locals->results as $article)
  168. {
  169. // Add each article as the current local result
  170. $tag->locals->result = $article;
  171. // Expand the tag : Means the children tags of this tag will get the above defined "result"
  172. $str .= $tag->expand();
  173. }
  174. return $str;
  175. }
  176. // ------------------------------------------------------------------------
  177. /**
  178. * Displays the content of the tag if no results
  179. *
  180. */
  181. public static function no_results(FTL_Binding $tag)
  182. {
  183. if( empty($tag->locals->results))
  184. {
  185. return $tag->expand();
  186. }
  187. }
  188. // ------------------------------------------------------------------------
  189. /**
  190. * Returns the searched term
  191. *
  192. */
  193. public static function realm(FTL_Binding $tag)
  194. {
  195. $str = '';
  196. if( ! empty($tag->locals->realm))
  197. {
  198. $str = $tag->locals->realm;
  199. }
  200. return $str;
  201. }
  202. // ------------------------------------------------------------------------
  203. /**
  204. * Returns one asked field of the current result.
  205. * If you query result contains the field 'title', you can retrieve it with this tag.
  206. *
  207. * @usage <ion:result field="title" />
  208. *
  209. *
  210. */
  211. public static function result(FTL_Binding $tag)
  212. {
  213. $field = (isset($tag->attr['field']) ) ? $tag->attr['field'] : false;
  214. if ($field && ( ! empty($tag->locals->result[$field])))
  215. {
  216. return self::wrap($tag, $tag->locals->result[$field]);
  217. }
  218. return '';
  219. }
  220. // ------------------------------------------------------------------------
  221. /**
  222. * Return one result title
  223. * Nested in <ion:results>
  224. *
  225. * @usage : <ion:results>
  226. * <ion:title>
  227. * ...
  228. * </ion:results>
  229. *
  230. */
  231. public static function title(FTL_Binding $tag)
  232. {
  233. return $tag->locals->result['title'];
  234. }
  235. // ------------------------------------------------------------------------
  236. /**
  237. * Returns the result URL
  238. *
  239. */
  240. public static function url(FTL_Binding $tag)
  241. {
  242. return $tag->locals->result['page_url'].'/'.$tag->locals->result['url'];
  243. }
  244. // ------------------------------------------------------------------------
  245. /**
  246. * Returns each result article's content
  247. *
  248. */
  249. public static function content(FTL_Binding $tag)
  250. {
  251. // paragraph limit ?
  252. $paragraph = (isset($tag->attr['paragraph'] )) ? (int)self::get_attribute($tag, 'paragraph') : FALSE ;
  253. $content = $tag->locals->result['content'];
  254. // Limit to x paragraph if the attribute is set
  255. if ($paragraph !== FALSE)
  256. $content = tag_limiter($content, 'p', $paragraph);
  257. return self::wrap($tag, $content);
  258. }
  259. // ------------------------------------------------------------------------
  260. /**
  261. * Returns each result article's date
  262. *
  263. */
  264. public static function date($tag) { return self::wrap($tag, self::format_date($tag, $tag->locals->result['date'])); }
  265. // ------------------------------------------------------------------------
  266. /**
  267. * Returns number of articles found
  268. *
  269. */
  270. public static function nb_results($tag) { return self::wrap($tag, $tag->locals->nb_results); }
  271. // ------------------------------------------------------------------------
  272. /**
  273. * Returns number of term occurences for each article found
  274. *
  275. */
  276. public static function nb_found($tag) { return self::wrap($tag, $tag->locals->result['nb_found']); }
  277. }