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

/concreteOLD/blocks/search/controller.php

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