PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/core/controllers/blocks/search.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 230 lines | 179 code | 35 blank | 16 comment | 39 complexity | 411b08f6d6193327a3534da09a646a51 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. * Displays a search prompt and results.
  5. *
  6. * @package Blocks
  7. * @subpackage Search
  8. * @author Andrew Embler <andrew@concrete5.org>
  9. * @copyright Copyright (c) 2003-2012 Concrete5. (http://www.concrete5.org)
  10. * @license http://www.concrete5.org/license/ MIT License
  11. *
  12. */
  13. class Concrete5_Controller_Block_Search extends BlockController {
  14. protected $btTable = 'btSearch';
  15. protected $btInterfaceWidth = "400";
  16. protected $btInterfaceHeight = "240";
  17. protected $btWrapperClass = 'ccm-ui';
  18. public $title = "";
  19. public $buttonText = ">";
  20. public $baseSearchPath = "";
  21. public $resultsURL = "";
  22. public $postTo_cID = "";
  23. protected $hColor = '#EFE795';
  24. public function highlightedMarkup($fulltext, $highlight) {
  25. if (!$highlight) {
  26. return $fulltext;
  27. }
  28. $this->hText = $fulltext;
  29. $this->hHighlight = str_replace(array('"',"'","&quot;"),'',$highlight); // strip the quotes as they mess the regex
  30. $this->hText = @preg_replace( "#$this->hHighlight#ui", '<span style="background-color:'. $this->hColor .';">$0</span>', $this->hText );
  31. return $this->hText;
  32. }
  33. public function validate($post) {
  34. $errors = Loader::helper('validation/error');
  35. if ($post['title'] === false || $post['title'] == '') {
  36. $errors->add(t("Please enter your Search Title."));
  37. }
  38. if ($post['buttonText'] === false || $post['buttonText'] == '') {
  39. $errors->add(t("Please enter your Submit Button Text."));
  40. }
  41. return $errors;
  42. }
  43. public function highlightedExtendedMarkup($fulltext, $highlight) {
  44. $text = @preg_replace("#\n|\r#", ' ', $fulltext);
  45. $matches = array();
  46. $highlight = str_replace(array('"',"'","&quot;"),'',$highlight); // strip the quotes as they mess the regex
  47. if (!$highlight) {
  48. $text = Loader::helper('text')->shorten($fulltext, 180);
  49. if (strlen($fulltext) > 180) {
  50. $text . '&hellip;<wbr>';
  51. }
  52. return $text;
  53. }
  54. $regex = '([[:alnum:]|\'|\.|_|\s]{0,45})'. preg_quote($highlight, '#') .'([[:alnum:]|\.|_|\s]{0,45})';
  55. preg_match_all("#$regex#ui", $text, $matches);
  56. if(!empty($matches[0])) {
  57. $body_length = 0;
  58. $body_string = array();
  59. foreach($matches[0] as $line) {
  60. $body_length += strlen($line);
  61. $r = $this->highlightedMarkup($line, $highlight);
  62. if ($r) {
  63. $body_string[] = $r;
  64. }
  65. if($body_length > 150)
  66. break;
  67. }
  68. if(!empty($body_string))
  69. return @implode("&hellip;<wbr>", $body_string);
  70. }
  71. }
  72. public function setHighlightColor($color) {
  73. $this->hColor = $color;
  74. }
  75. /**
  76. * Used for localization. If we want to localize the name/description we have to include this
  77. */
  78. public function getBlockTypeDescription() {
  79. return t("Add a search box to your site.");
  80. }
  81. public function getBlockTypeName() {
  82. return t("Search");
  83. }
  84. public function getJavaScriptStrings() {
  85. return array('search-title' => t('Please enter a valid search title.'));
  86. }
  87. function __construct($obj = null) {
  88. parent::__construct($obj);
  89. if ($this->title == '') {
  90. $this->title=t("Search");
  91. }
  92. }
  93. public function indexExists() {
  94. $db = Loader::db();
  95. $numRows = $db->GetOne('select count(cID) from PageSearchIndex');
  96. return ($numRows > 0);
  97. }
  98. function view(){
  99. $c = Page::getCurrentPage();
  100. $this->set('title', $this->title);
  101. $this->set('buttonText', $this->buttonText);
  102. $this->set('baseSearchPath', $this->baseSearchPath);
  103. $this->set('postTo_cID', $this->postTo_cID);
  104. $resultsURL = $c->getCollectionPath();
  105. if ($this->resultsURL != '') {
  106. $resultsURL = $this->resultsURL;
  107. } else if ($this->postTo_cID != '') {
  108. $resultsPage = Page::getById($this->postTo_cID);
  109. $resultsURL = $resultsPage->cPath;
  110. }
  111. $this->set('resultTargetURL', $resultsURL);
  112. //run query if display results elsewhere not set, or the cID of this page is set
  113. if( !empty($_REQUEST['query']) || isset($_REQUEST['akID']) || isset($_REQUEST['month'])) {
  114. $this->do_search();
  115. }
  116. }
  117. function save($data) {
  118. $args['title'] = isset($data['title']) ? $data['title'] : '';
  119. $args['buttonText'] = isset($data['buttonText']) ? $data['buttonText'] : '';
  120. $args['baseSearchPath'] = isset($data['baseSearchPath']) ? $data['baseSearchPath'] : '';
  121. if( $args['baseSearchPath']=='OTHER' && intval($data['searchUnderCID'])>0 ){
  122. $customPathC = Page::getByID( intval($data['searchUnderCID']) );
  123. if( !$customPathC ) $args['baseSearchPath']='';
  124. else $args['baseSearchPath'] = $customPathC->getCollectionPath();
  125. }
  126. if( trim($args['baseSearchPath'])=='/' || strlen(trim($args['baseSearchPath']))==0 )
  127. $args['baseSearchPath']='';
  128. if( intval($data['postTo_cID'])>0 ){
  129. $args['postTo_cID'] = intval($data['postTo_cID']);
  130. } else {
  131. $args['postTo_cID'] = '';
  132. }
  133. $args['resultsURL'] = ( $data['externalTarget']==1 && strlen($data['resultsURL'])>0 ) ? trim($data['resultsURL']) : '';
  134. parent::save($args);
  135. }
  136. public $reservedParams=array('page=','query=','search_paths[]=','submit=','search_paths%5B%5D=' );
  137. function do_search() {
  138. $q = $_REQUEST['query'];
  139. // i have NO idea why we added this in rev 2000. I think I was being stupid. - andrew
  140. // $_q = trim(preg_replace('/[^A-Za-z0-9\s\']/i', ' ', $_REQUEST['query']));
  141. $_q = $q;
  142. Loader::library('database_indexed_search');
  143. $ipl = new IndexedPageList();
  144. $aksearch = false;
  145. $ipl->ignoreAliases();
  146. if (is_array($_REQUEST['akID'])) {
  147. Loader::model('attribute/categories/collection');
  148. foreach($_REQUEST['akID'] as $akID => $req) {
  149. $fak = CollectionAttributeKey::getByID($akID);
  150. if (is_object($fak)) {
  151. $type = $fak->getAttributeType();
  152. $cnt = $type->getController();
  153. $cnt->setAttributeKey($fak);
  154. $cnt->searchForm($ipl);
  155. $aksearch = true;
  156. }
  157. }
  158. }
  159. if (isset($_REQUEST['month']) && isset($_REQUEST['year'])) {
  160. $month = strtotime($_REQUEST['year'] . '-' . $_REQUEST['month'] . '-01');
  161. $month = date('Y-m-', $month);
  162. $ipl->filterByPublicDate($month . '%', 'like');
  163. $aksearch = true;
  164. }
  165. if (empty($_REQUEST['query']) && $aksearch == false) {
  166. return false;
  167. }
  168. $ipl->setSimpleIndexMode(true);
  169. if (isset($_REQUEST['query'])) {
  170. $ipl->filterByKeywords($_q);
  171. }
  172. if( is_array($_REQUEST['search_paths']) ){
  173. foreach($_REQUEST['search_paths'] as $path) {
  174. if(!strlen($path)) continue;
  175. $ipl->filterByPath($path);
  176. }
  177. } else if ($this->baseSearchPath != '') {
  178. $ipl->filterByPath($this->baseSearchPath);
  179. }
  180. $ipl->filter(false, '(ak_exclude_search_index = 0 or ak_exclude_search_index is null)');
  181. $res = $ipl->getPage();
  182. foreach($res as $r) {
  183. $results[] = new IndexedSearchResult($r['cID'], $r['cName'], $r['cDescription'], $r['score'], $r['cPath'], $r['content']);
  184. }
  185. $this->set('query', $q);
  186. $this->set('paginator', $ipl->getPagination());
  187. $this->set('results', $results);
  188. $this->set('do_search', true);
  189. $this->set('searchList', $ipl);
  190. }
  191. }