/kernel/content/advancedsearch.php

https://github.com/zerustech/ezpublish · PHP · 302 lines · 238 code · 40 blank · 24 comment · 43 complexity · 345ea03430bd9e82ee126a9417323846 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. * @version //autogentag//
  6. * @package kernel
  7. */
  8. /*!
  9. Get search limit
  10. */
  11. function pageLimit( $searchPageLimit )
  12. {
  13. switch ( $searchPageLimit )
  14. {
  15. case 1:
  16. return 5;
  17. case 2:
  18. default:
  19. return 10;
  20. case 3:
  21. return 20;
  22. case 4:
  23. return 30;
  24. case 5:
  25. return 50;
  26. }
  27. }
  28. $http = eZHTTPTool::instance();
  29. $Module = $Params['Module'];
  30. $ViewMode = $Params['ViewMode'];
  31. $Offset = $Params['Offset'];
  32. if ( $ViewMode == 'offset' )
  33. $ViewMode = '';
  34. $tpl = eZTemplate::factory();
  35. $ini = eZINI::instance();
  36. $useSearchCode = $ini->variable( 'SearchSettings', 'SearchViewHandling' ) == 'default';
  37. $logSearchStats = $ini->variable( 'SearchSettings', 'LogSearchStats' ) == 'enabled';
  38. $searchText = '';
  39. $originalSearchText = '';
  40. $phraseSearchText = '';
  41. if ( !is_numeric( $Offset ) )
  42. $Offset = 0;
  43. $searchPageLimit = 2;
  44. if ( $http->hasVariable( 'SearchPageLimit' ) )
  45. {
  46. $searchPageLimit = $http->variable( 'SearchPageLimit' );
  47. }
  48. $pageLimit = pageLimit( $searchPageLimit );
  49. $maximumSearchLimit = $ini->variable( 'SearchSettings', 'MaximumSearchLimit' );
  50. if ( $pageLimit > $maximumSearchLimit )
  51. $pageLimit = $maximumSearchLimit;
  52. if ( $http->hasVariable( 'PhraseSearchText' ) and trim( $http->variable( 'PhraseSearchText' ) ) != '' )
  53. {
  54. $searchText = '"' . $http->variable( 'PhraseSearchText' ) . '"';
  55. $phraseSearchText = $http->variable( 'PhraseSearchText' );
  56. }
  57. $fullSearchText = '';
  58. if ( $http->hasVariable( 'SearchText' ) )
  59. {
  60. if ( $searchText != '' )
  61. $searchText .= ' ';
  62. $originalSearchText = $http->variable( 'SearchText' );
  63. $searchText .= $originalSearchText;
  64. $fullSearchText = $http->variable( 'SearchText' );
  65. }
  66. $searchContentClassID = -1;
  67. $searchContentClassAttributes = 0;
  68. $searchContentClassAttributeArray = array();
  69. if ( $http->hasVariable( 'SearchContentClassID' ) and
  70. $http->variable( 'SearchContentClassID' ) != -1 )
  71. {
  72. $searchContentClassID = $http->variable( 'SearchContentClassID' );
  73. if ( (int) $searchContentClassID > 0 )
  74. {
  75. $searchContentClass = eZContentClass::fetch( (int) $searchContentClassID );
  76. if ( is_object( $searchContentClass ) )
  77. $searchContentClassAttributeArray = $searchContentClass->fetchSearchableAttributes();
  78. }
  79. }
  80. $searchContentClassAttributeID = -1;
  81. if ( $http->hasVariable( 'SearchContentClassAttributeID' ) and
  82. $http->variable( 'SearchContentClassAttributeID' ) != -1 )
  83. {
  84. $searchContentClassAttributeID = $http->variable( 'SearchContentClassAttributeID' );
  85. }
  86. $searchDate = -1;
  87. if ( $http->hasVariable( 'SearchDate' ) and
  88. $http->variable( 'SearchDate' ) != -1 )
  89. {
  90. $searchDate = $http->variable( 'SearchDate' );
  91. }
  92. $searchTimestamp = false;
  93. if ( $http->hasVariable( 'SearchTimestamp' ) and
  94. $http->variable( 'SearchTimestamp' ) )
  95. {
  96. $searchTimestamp = $http->variable( 'SearchTimestamp' );
  97. }
  98. $searchSectionID = -1;
  99. if ( $http->hasVariable( 'SearchSectionID' ) and
  100. $http->variable( 'SearchSectionID' ) != -1 )
  101. {
  102. $searchSectionID = $http->variable( 'SearchSectionID' );
  103. }
  104. $subTreeArray = array();
  105. if ( $http->hasVariable( 'SubTreeArray' ) )
  106. {
  107. if ( is_array( $http->variable( 'SubTreeArray' ) ) )
  108. {
  109. $subTreeList = $http->variable( 'SubTreeArray' );
  110. }
  111. else
  112. {
  113. $subTreeList = array( $http->variable( 'SubTreeArray' ) );
  114. }
  115. foreach ( $subTreeList as $subTreeItem )
  116. {
  117. // as form input is generally a string, is_int cannot be used for checking the value type
  118. if ( is_numeric( $subTreeItem ) && $subTreeItem > 0 )
  119. {
  120. $subTreeArray[] = $subTreeItem;
  121. }
  122. }
  123. }
  124. $Module->setTitle( "Search for: $searchText" );
  125. $classArray = eZContentClass::fetchList( eZContentClass::VERSION_STATUS_DEFINED, true, false, array( 'name' => 'asc' ) );
  126. $sectionArray = eZSection::fetchList();
  127. $searchArray = eZSearch::buildSearchArray();
  128. if ( $useSearchCode )
  129. {
  130. $searchResult = eZSearch::search( $searchText, array( 'SearchSectionID' => $searchSectionID,
  131. 'SearchContentClassID' => $searchContentClassID,
  132. 'SearchContentClassAttributeID' => $searchContentClassAttributeID,
  133. 'SearchSubTreeArray' => $subTreeArray,
  134. 'SearchDate' => $searchDate,
  135. 'SearchTimestamp' => $searchTimestamp,
  136. 'SearchLimit' => $pageLimit,
  137. 'SearchOffset' => $Offset ),
  138. $searchArray );
  139. if ( strlen(trim($searchText)) == 0 && count( $searchArray ) > 0 )
  140. {
  141. $searchText = 'search by additional parameter';
  142. }
  143. }
  144. $viewParameters = array( 'offset' => $Offset );
  145. $searchData = false;
  146. $tpl->setVariable( "search_data", $searchData );
  147. $tpl->setVariable( 'search_contentclass_id', $searchContentClassID );
  148. $tpl->setVariable( 'search_contentclass_attribute_id', $searchContentClassAttributeID );
  149. $tpl->setVariable( 'search_section_id', $searchSectionID );
  150. $tpl->setVariable( 'search_date', $searchDate );
  151. $tpl->setVariable( 'search_timestamp', $searchTimestamp );
  152. $tpl->setVariable( 'search_sub_tree', $subTreeArray );
  153. $tpl->setVariable( 'search_text', $searchText );
  154. $tpl->setVariable( 'search_page_limit', $searchPageLimit );
  155. $tpl->setVariable( 'full_search_text', $fullSearchText );
  156. $tpl->setVariable( 'phrase_search_text', $phraseSearchText );
  157. $tpl->setVariable( "view_parameters", $viewParameters );
  158. $tpl->setVariable( 'use_template_search', !$useSearchCode );
  159. // --- Compatibility code start ---
  160. if ( $useSearchCode )
  161. {
  162. $tpl->setVariable( 'offset', $Offset );
  163. $tpl->setVariable( 'page_limit', $pageLimit );
  164. $tpl->setVariable( 'search_text_enc', urlencode( $originalSearchText ) );
  165. $tpl->setVariable( 'phrase_search_text_enc', urlencode( $phraseSearchText ) );
  166. $tpl->setVariable( 'search_result', $searchResult['SearchResult'] );
  167. $tpl->setVariable( 'search_count', $searchResult['SearchCount'] );
  168. $tpl->setVariable( 'stop_word_array', $searchResult['StopWordArray'] );
  169. if ( isset( $searchResult["SearchExtras"] ) )
  170. {
  171. $tpl->setVariable( "search_extras", $searchResult["SearchExtras"] );
  172. }
  173. }
  174. else
  175. {
  176. $tpl->setVariable( 'offset', false );
  177. $tpl->setVariable( 'page_limit', false );
  178. $tpl->setVariable( 'search_text_enc', false );
  179. $tpl->setVariable( 'phrase_search_text_enc', false );
  180. $tpl->setVariable( 'search_result', false );
  181. $tpl->setVariable( 'search_count', false );
  182. $tpl->setVariable( 'stop_word_array', false );
  183. }
  184. // --- Compatibility code end ---
  185. $tpl->setVariable( 'content_class_array', $classArray );
  186. $tpl->setVariable( 'section_array', $sectionArray );
  187. $tpl->setVariable( 'search_content_class_attribute_array', $searchContentClassAttributeArray );
  188. // Set template variable containing search terms for attribute-based search.
  189. $searchTermsArray = array();
  190. // BEGIN old code for backwards compatibility
  191. // Set template variable for attribute-based search.
  192. // Make it a hash with classattribute_id as key. If it has an identifier, add that to the key.
  193. $searchArrayByClassAttributeID = array();
  194. // END old code for backwards compatibility
  195. foreach ( $searchArray as $searchItem )
  196. {
  197. foreach ( $searchItem as $searchTerms )
  198. {
  199. if ( isSet( $searchTerms['identifier'] ) )
  200. {
  201. $searchTermsArray[$searchTerms['identifier']] = $searchTerms;
  202. // BEGIN old code for backwards compatibility
  203. $searchArrayByClassAttributeID[$searchTerms['classattribute_id'] . '_' .
  204. $searchTerms['identifier']] = $searchTerms;
  205. // END old code for backwards compatibility
  206. }
  207. else
  208. {
  209. $searchTermsArray[$searchTerms['classattribute_id']] = $searchTerms;
  210. // BEGIN old code for backwards compatibility
  211. $searchArrayByClassAttributeID[$searchTerms['classattribute_id']] = $searchTerms;
  212. // END old code for backwards compatibility
  213. }
  214. }
  215. }
  216. $tpl->setVariable( 'search_terms_array', $searchTermsArray );
  217. // BEGIN old code for backwards compatibility
  218. $tpl->setVariable( 'search_array_by_class_attribute_id', $searchArrayByClassAttributeID );
  219. // END old code for backwards compatibility
  220. if ( $searchSectionID != -1 )
  221. {
  222. $section = eZSection::fetch( $searchSectionID );
  223. $res = eZTemplateDesignResource::instance();
  224. $keyArray = array( array( 'section', $searchSectionID ),
  225. array( 'section_identifier', $section->attribute( 'identifier' ) ) );
  226. $res->setKeys( $keyArray );
  227. }
  228. $Result = array();
  229. if ( trim( $ViewMode ) != '' )
  230. {
  231. // Fetch override template for viewmode if wanted
  232. $Result['content'] = $tpl->fetch( "design:content/advancedsearch/$ViewMode.tpl" );
  233. }
  234. else
  235. {
  236. $Result['content'] = $tpl->fetch( 'design:content/advancedsearch.tpl' );
  237. }
  238. $Result['path'] = array( array( 'text' => ezpI18n::tr( 'kernel/content', 'Search' ),
  239. 'url' => false ),
  240. array( 'text' => ezpI18n::tr( 'kernel/content', 'Advanced' ),
  241. 'url' => false ) );
  242. $searchData = false;
  243. if ( !$useSearchCode )
  244. {
  245. if ( $tpl->hasVariable( "search_data" ) )
  246. {
  247. $searchData = $tpl->variable( "search_data" );
  248. }
  249. }
  250. else
  251. {
  252. $searchData = $searchResult;
  253. }
  254. if ( $logSearchStats and
  255. trim( $searchText ) != "" and
  256. is_array( $searchData ) and
  257. array_key_exists( 'SearchCount', $searchData ) and
  258. is_numeric( $searchData['SearchCount'] ) )
  259. {
  260. eZSearchLog::addPhrase( $searchText, $searchData["SearchCount"] );
  261. }
  262. ?>