/htroot/assets/snippets/ajaxSearch/classes/ajaxSearchCtrl.class.inc.php

https://github.com/gunf/novo-isaak.local · PHP · 197 lines · 157 code · 16 blank · 24 comment · 61 complexity · fba7ea0b549c0296a7bc6f15153782ba MD5 · raw file

  1. <?php
  2. /* -----------------------------------------------------------------------------
  3. * Snippet: AjaxSearch
  4. * -----------------------------------------------------------------------------
  5. * @package AjaxSearchCtrl
  6. *
  7. * @author Coroico - www.modx.wangba.fr
  8. * @version 1.9.2
  9. * @date 05/12/2010
  10. *
  11. * Purpose:
  12. * The AjaxSearchCtrl class contains the logic and synchronisation between model and views
  13. *
  14. */
  15. class AjaxSearchCtrl {
  16. // public variables
  17. var $asCfg = null;
  18. var $asInput = null;
  19. var $asResults = null;
  20. var $asOutput = null;
  21. var $asUtil = null;
  22. var $asLog = null;
  23. var $dbg = false;
  24. var $dbgTpl = false;
  25. var $dbgRes = false;
  26. var $log = false;
  27. var $forThisAs;
  28. var $searchString;
  29. var $advSearch;
  30. var $subSearch;
  31. var $pagination;
  32. var $offset;
  33. var $asf;
  34. var $output;
  35. var $fClause;
  36. var $fParams = array();
  37. function AjaxSearchCtrl() {
  38. }
  39. function init(&$asCfg, &$asInput, &$asResults, &$asOutput, &$asUtil, &$asLog){
  40. // initialize the controler instance
  41. $this->asCfg =& $asCfg;
  42. $this->asInput =& $asInput;
  43. $this->asResults =& $asResults;
  44. $this->asOutput =& $asOutput;
  45. $this->asUtil =& $asUtil;
  46. $this->dbg = $asUtil->dbg;
  47. $this->dbgTpl = $asUtil->dbgTpl;
  48. $this->dbgRes = $asUtil->dbgRes;
  49. $this->asLog =& $asLog;
  50. $asLog_array = explode(':', $asCfg->cfg['asLog']);
  51. $this->log = ($asLog_array[0]) ? true : false;
  52. $this->asInput->init($asCfg, $this, $asUtil);
  53. $this->asResults->init($asCfg, $this, $asOutput, $asUtil);
  54. $this->asOutput->init($asCfg, $this, $asInput, $asResults, $asUtil, $asLog, $this->log);
  55. }
  56. /*
  57. * run : run the search
  58. */
  59. function run() {
  60. $this->setforThisAs();
  61. $this->getEvents(); // get $_POST and _GET variables
  62. $valid = $this->asInput->display($msg);
  63. if ($valid) {
  64. $valid2 = $this->asResults->getSearchResults($msg);
  65. if (!$valid2) return $msg;
  66. }
  67. $this->asOutput->setAjaxSearchHeader();
  68. if (!$this->pagination) $output = $this->asOutput->display($valid, $msg);
  69. else $output = $this->asOutput->paginate($valid, $msg);
  70. return $output;
  71. }
  72. /*
  73. * setforThisAs : Check if this instance is concerned
  74. */
  75. function setforThisAs() {
  76. if ($this->asCfg->isAjax) $this->forThisAs = true;
  77. else {
  78. $id = '';
  79. if (isset($_POST['asid']) || isset($_GET['asid'])) {
  80. if (isset($_POST['asid'])) $id = strip_tags($_POST['asid']);
  81. else $id = strip_tags(urldecode($_GET['asid']));
  82. }
  83. $this->forThisAs = ($this->asCfg->cfg['asId'] != $id) ? false : true;
  84. }
  85. }
  86. function getEvents() {
  87. $this->getSearchString();
  88. $this->subSearch = $this->asCfg->cfg['subSearch'];
  89. if (isset($_POST['subsearch']) || isset($_GET['subsearch'])) {
  90. if (isset($_POST['subsearch'])) {
  91. $ssc = isset($_POST['ssc']) ? ':' : ',';
  92. if (is_array($_POST['subsearch'])) {
  93. foreach($_POST['subsearch'] as $key => $value) $_POST['subsearch'][$key] = strip_tags($value);
  94. $this->subSearch = implode($ssc,$_POST['subsearch']);
  95. }
  96. else $this->subSearch = strip_tags($_POST['subsearch']);
  97. }
  98. else {
  99. $ssc = isset($_GET['ssc']) ? ':' : ',';
  100. if (is_array($_GET['subsearch'])) {
  101. foreach($_GET['subsearch'] as $key => $value) $_GET['subsearch'][$key] = strip_tags($value);
  102. $this->subSearch = implode($ssc,$_GET['subsearch']);
  103. }
  104. else $this->subSearch = strip_tags($_GET['subsearch']);
  105. }
  106. }
  107. if ($this->dbg) $this->asUtil->dbgRecord($this->subSearch , "getEvents - subsearch");
  108. $asfConfig = 'asfConfig';
  109. if ((isset($_POST['asf']) || isset($_GET['asf'])) && function_exists($asfConfig)) {
  110. $this->asf = isset($_POST['asf']) ? strip_tags($_POST['asf']) : strip_tags(urldecode($_GET['asf']));
  111. $this->fClause = $asfConfig($this->asf, $this->fParams);
  112. if ($this->dbg) $this->asUtil->dbgRecord($this->fParams , "getEvents - fParams");
  113. if ($this->dbg) $this->asUtil->dbgRecord($this->fClause , "getEvents - fClause");
  114. }
  115. else $this->asf = '';
  116. if ($this->dbg) $this->asUtil->dbgRecord($this->asf , "getEvents - asf");
  117. $this->offset = (isset($_GET['aso'])) ? strip_tags(urldecode($_GET['aso'])) : '0,0';
  118. if ($this->dbg) $this->asUtil->dbgRecord($this->offset , "getEvents - offset");
  119. $this->pagination = (isset($_POST['pgn'])) ? strip_tags($_POST['pgn']) : '';
  120. if ($this->dbg) $this->asUtil->dbgRecord($this->pagination , "getEvents - pgn");
  121. }
  122. function getSearchString() {
  123. $this->searchString = '';
  124. $this->advSearch = $this->asCfg->cfg['advSearch'];
  125. if ($this->forThisAs) {
  126. if (!$this->asCfg->isAjax) {
  127. if (isset($_POST['search']) || (isset($_GET['search']) && (!$this->asCfg->cfg['ajaxSearch']))) {
  128. if (isset($_POST['search'])) {
  129. if (is_array($_POST['search'])) {
  130. foreach($_POST['search'] as $key => $value) $_POST['search'][$key] = strip_tags($value);
  131. $this->searchString = implode(' ', $_POST['search']);
  132. }
  133. else $this->searchString = strip_tags($_POST['search']);
  134. } else {
  135. $this->searchString = strip_tags(urldecode($_GET['search']));
  136. }
  137. if (isset($_POST['advsearch'])) $this->advSearch = strip_tags($_POST['advsearch']);
  138. else if (isset($_GET['advsearch'])) $this->advSearch = strip_tags(urldecode($_GET['advsearch']));
  139. }
  140. }
  141. else {
  142. if (isset($_POST['search'])) {
  143. if (is_array($_POST['search'])) {
  144. foreach($_POST['search'] as $key => $value) $_POST['search'][$key] = strip_tags($value);
  145. $this->searchString = implode(' ', $_POST['search']);
  146. }
  147. else $this->searchString = strip_tags($_POST['search']);
  148. if (($this->asCfg->pgCharset != 'UTF-8') && (ini_get('mbstring.encoding_translation') == '' || strtolower(ini_get('mbstring.http_input')) == 'pass')) {
  149. $this->searchString = mb_convert_encoding($this->searchString, $this->asCfg->pgCharset, "UTF-8");
  150. $this->asOutput->setNeedsConvert(true);
  151. } else {
  152. $this->asOutput->setNeedsConvert(false);
  153. }
  154. if (isset($_POST['advsearch'])) $this->advSearch = strip_tags($_POST['advsearch']);
  155. }
  156. }
  157. }
  158. if ($this->dbg) $this->asUtil->dbgRecord($this->searchString, "getSearchString - searchString");
  159. if ($this->dbg) $this->asUtil->dbgRecord($this->advSearch, "getSearchString - advSearch");
  160. }
  161. /*
  162. * getSearchWords : depending advSearch, get the search words
  163. */
  164. function getSearchWords($search, $advSearch) {
  165. $searchList = array();
  166. if (($advSearch == NOWORDS) || (!$search)) return $searchList;
  167. if ($advSearch == EXACTPHRASE) $searchList[] = $search;
  168. else $searchList = explode(' ', $search);
  169. return $searchList;
  170. }
  171. function setSearchString($searchString) {
  172. $this->searchString = $searchString;
  173. }
  174. function setAdvSearch($advSearch) {
  175. $this->advSearch = $advSearch;
  176. }
  177. function setSubSearch($subSearch) {
  178. $this->subSearch = $subSearch;
  179. }
  180. }
  181. ?>