PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/assets/snippets/ajaxSearch/classes/ajaxSearchOutput.class.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 1079 lines | 958 code | 64 blank | 57 comment | 90 complexity | 32af1d0c6ecd971c91f7ebc7df9f6cee MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /* -----------------------------------------------------------------------------
  3. * Snippet: AjaxSearch
  4. * -----------------------------------------------------------------------------
  5. * @package AjaxSearchOutput
  6. *
  7. * @author Coroico - www.modx.wangba.fr
  8. * @version 1.9.2
  9. * @date 05/12/2010
  10. *
  11. * Purpose:
  12. * The AjaxSearchOutput class contains all functions and data used to display output
  13. *
  14. */
  15. define('HIGHLIGHT_CLASS', 'ajaxSearch_highlight');
  16. define('PREFIX_AJAX_RESULT_CLASS', 'AS_ajax_result');
  17. define('PREFIX_RESULT_CLASS', 'ajaxSearch_result');
  18. define('INTROFAILURE_CLASS', 'AS_ajax_resultsIntroFailure');
  19. define('RESULTS_DIV', '<div id="%sajaxSearch_output" class="init"></div>');
  20. define('AJAXSEARCH_JSDIR','js/ajaxSearch');
  21. define('ASPHX','||-AJAXSEARCH-||');
  22. define('NB_MORE_RESULTS',10);
  23. class AjaxSearchOutput {
  24. // public variables
  25. var $asCfg = null;
  26. var $asCtrl = null;
  27. var $asInput = null;
  28. var $asResults = null;
  29. var $asUtil = null;
  30. var $asLog = null;
  31. var $dbg = false;
  32. var $dbgTpl = false;
  33. var $dbgRes = false;
  34. var $log = false;
  35. var $output;
  36. // class variables
  37. var $asClass = array();
  38. // chunkie variables
  39. var $chkResults;
  40. var $varResults = array();
  41. var $chkGrpResult;
  42. var $varGrpResult = array();
  43. var $tplGrpRes;
  44. var $chkResult;
  45. var $varResult = array();
  46. var $tplRes;
  47. var $scMain = array();
  48. var $scJoined = array();
  49. var $scTvs = array();
  50. var $logIds = array();
  51. // private variables
  52. var $_needsConvert;
  53. function AjaxSearchOutput() {
  54. }
  55. function init(&$asCfg, &$asCtrl, &$asInput, &$asResults, &$asUtil, &$asLog, $log) {
  56. // initialize the output instance
  57. $this->asCfg =& $asCfg;
  58. $this->asCtrl =& $asCtrl;
  59. $this->asInput =& $asInput;
  60. $this->asResults =& $asResults;
  61. $this->asUtil =& $asUtil;
  62. $this->dbg = $asUtil->dbg;
  63. $this->dbgTpl = $asUtil->dbgTpl;
  64. $this->dbgRes = $asUtil->dbgRes;
  65. $this->asLog =& $asLog;
  66. $this->log = $log;
  67. }
  68. /*
  69. * display : display final output
  70. */
  71. function display($validSearch, &$msgErr) {
  72. global $modx;
  73. $this->_checkParams();
  74. if ($this->asCfg->isAjax) {
  75. $jsonPairs = array();
  76. $output = $this->_displayResults($validSearch, $msgErr, $nbResultsInfos);
  77. $output .= $this->_displayComment();
  78. $jsonPairs[] = $this->_getJsonPair('res',$output);
  79. $jsonPairs[] = $this->_getJsonPair('resnb',$nbResultsInfos);
  80. if ($validSearch) $this->_updateAsf($jsonPairs);
  81. $output = $this->_getJson($jsonPairs);
  82. }
  83. else {
  84. $outputInputForm = $this->asInput->inputForm; //
  85. $outputAsfForm = $this->asInput->asfForm;
  86. if ($this->asCfg->cfg['ajaxSearch']) {
  87. $outputResults = $this->_initDisplayResults();
  88. }
  89. else {
  90. $outputResults = $this->_displayResults($validSearch, $msgErr, $nbResultsInfos); // non ajax results output
  91. $outputResults .= $this->_displayComment();
  92. }
  93. if (!$this->asCfg->cfg['output']) {
  94. $output = $outputInputForm;
  95. $output .= $outputAsfForm;
  96. $output .= $outputResults;
  97. }
  98. else {
  99. $output = '';
  100. $modx->setPlaceholder("as.inputForm", $outputInputForm);
  101. $modx->setPlaceholder("as.asfForm", $outputAsfForm);
  102. $modx->setPlaceholder("as.results", $outputResults);
  103. }
  104. }
  105. return $output;
  106. }
  107. /*
  108. * checkParams : Check output field params
  109. */
  110. function _checkParams() {
  111. if (($this->asCfg->cfg['pagingType'] < 0) || ($this->asCfg->cfg['pagingType'] > 2)) $this->asCfg->cfg['pagingType'] = 1;
  112. }
  113. /*
  114. * Init display results
  115. */
  116. function _initDisplayResults(){
  117. $prefix = ($this->asCfg->cfg['asId']) ? $this->asCfg->cfg['asId'] . "_" : '';
  118. $outputResults = sprintf(RESULTS_DIV, $prefix);
  119. return $outputResults;
  120. }
  121. /*
  122. * displayResults : display results
  123. */
  124. function _displayResults($validSearch, &$msgErr, &$nbResultsInfos) {
  125. $this->_initCommonChunks();
  126. $outputResults = '';
  127. $nbFoundResults = 0;
  128. $nbDisplayedResults = 0;
  129. $logIds = array();
  130. $asCall = $this->_getAsCall($this->asCfg->setAsCall($this->asCfg->getUserConfig()));
  131. $select = $this->asResults->_asRequest->asSelect;
  132. if ($this->asCfg->cfg['showResults']) {
  133. if ($validSearch) {
  134. if (!$this->asCfg->isAjax) $this->_setOffset();
  135. $listGrpResults = '';
  136. $ig = 0;
  137. if ($this->asResults->nbResults > 0) {
  138. $this->_getSearchContext();
  139. $nbDisplayedResults = 0;
  140. for ($ig = 0;$ig < $this->asResults->nbGroups;$ig++) {
  141. $found = '';
  142. $site = $this->asResults->groupResults[$ig]['site'];
  143. $subsite = $this->asResults->groupResults[$ig]['subsite'];
  144. $display = $this->asResults->groupResults[$ig]['display'];
  145. $offset = intval($this->asResults->groupResults[$ig]['offset']);
  146. $nbrs = intval($this->asResults->groupResults[$ig]['length']);
  147. $nbFoundResults += $nbrs;
  148. // nb results displayed
  149. if ($this->asCfg->isAjax) $nbMax = ($this->asCfg->cfg['ajaxMax'] > 0) ? $this->asCfg->cfg['ajaxMax'] : $nbrs;
  150. else $nbMax = ($this->asCfg->cfg['grabMax'] > 0) ? $this->asCfg->cfg['grabMax'] : $nbrs;
  151. $searchResults = array_slice($this->asResults->groupResults[$ig]['results'], $offset, $nbMax);
  152. $nbDisplayedResults += count($searchResults);
  153. $this->asCfg->chooseConfig($site, $subsite, $display);
  154. $this->_initDisplayVariables();
  155. $listGrpResults .= $this->_displayGrpResult($ig, $site, $subsite, $display, $nbrs, $searchResults, $offset, $nbMax);
  156. $lid = $this->_setSuccessfullSearches($ig);
  157. if ($lid) $logIds[] = $lid;
  158. }
  159. $this->asCfg->restoreConfig(DEFAULT_SITE, DEFAULT_SUBSITE);
  160. if ($nbFoundResults) {
  161. if ($this->asCtrl->searchString && ($this->asCtrl->advSearch != NOWORDS)) {
  162. $resultsFoundText = ($nbFoundResults > 1) ? $this->asCfg->lang['as_resultsFoundTextMultiple'] : $this->asCfg->lang['as_resultsFoundTextSingle'];
  163. if ($this->asResults->withExtract) {
  164. $searchList = $this->asCtrl->getSearchWords($this->asCtrl->searchString, $this->asCtrl->advSearch);
  165. $hits = 1;
  166. $searchwords = '';
  167. foreach ($searchList as $words) {
  168. $searchwords.= '<span class="ajaxSearch_highlight ajaxSearch_highlight' . $hits . '">' . $words . '</span>&nbsp;';
  169. $hits++;
  170. }
  171. $searchwords = substr($searchwords, 0, strlen($searchwords) - 6);
  172. $this->varResults['resultsFoundText'] = sprintf($resultsFoundText, $nbFoundResults, $searchwords);
  173. } else {
  174. $this->varResults['resultsFoundText'] = sprintf($resultsFoundText, $nbFoundResults, $this->asCtrl->searchString);
  175. }
  176. }
  177. else {
  178. $resultsFoundText = ($nbFoundResults > 1) ? $this->asCfg->lang['as_resultsTextMultiple'] : $this->asCfg->lang['as_resultsTextSingle'];
  179. $this->varResults['resultsFoundText'] = sprintf($resultsFoundText, $nbFoundResults);
  180. }
  181. $resultsDisplayedText = $this->asCfg->lang['as_resultsDisplayed'];
  182. $this->varResults['resultsDisplayedText'] = sprintf($resultsDisplayedText, $nbDisplayedResults);
  183. $this->varResults['noResults'] = 0;
  184. $this->varResults['listGrpResults'] = ASPHX;
  185. if ($this->asCfg->isAjax) $this->_setMoreResultsLink($this->asResults->nbResults);
  186. }
  187. else {
  188. $this->varResults['noResults'] = 1;
  189. $this->varResults['noResultClass'] = INTROFAILURE_CLASS;
  190. $this->varResults['noResultText'] = $this->asCfg->lang['as_resultsIntroFailure'];
  191. $this->_setFailedSearches($asCall,$select);
  192. }
  193. } else {
  194. $this->varResults['noResults'] = 1;
  195. $this->varResults['noResultClass'] = INTROFAILURE_CLASS;
  196. $this->varResults['noResultText'] = $this->asCfg->lang['as_resultsIntroFailure'];
  197. $this->_setFailedSearches($asCall,$select);
  198. }
  199. }
  200. else {
  201. $this->varResults['showCmt'] = 0;
  202. $this->varResults['noResults'] = 1;
  203. $this->varResults['noResultClass'] = 'AS_ajax_resultsIntroFailure';
  204. $this->varResults['noResultText'] = $msgErr;
  205. }
  206. $this->chkResults->AddVar("as", $this->varResults);
  207. $outputResults = $this->chkResults->Render();
  208. $outputResults = str_replace(ASPHX, $listGrpResults, $outputResults);
  209. unset($this->varResults);
  210. unset($this->chkResults);
  211. // UTF-8 conversion is required if mysql character set is different of 'utf8'
  212. if ($this->_needsConvert) $outputResults = mb_convert_encoding($outputResults,"UTF-8",$this->asCfg->pgCharset);
  213. $this->logIds = $logIds;
  214. }
  215. $nbResultsInfos = $nbFoundResults . ',' . $nbDisplayedResults;
  216. return $outputResults;
  217. }
  218. /*
  219. * Display a group of results
  220. */
  221. function _displayGrpResult($ig, $site, $subsite, $display, $nbrs, $searchResults, $offset, $nbMax) {
  222. $found = array();
  223. $this->chkGrpResult = new AsPhxParser($this->tplGrpRes);
  224. $this->varGrpResult = array();
  225. $this->varGrpResult['headerGrpResult'] = $this->_displayHeaderGrpResult($site, $subsite, $display, $nbrs, $searchResults, $offset, $nbMax);
  226. $this->varGrpResult['grpResultsDef'] = 1;
  227. $prefix = ($this->asCfg->cfg['asId']) ? $this->asCfg->cfg['asId'] . "_" : '';
  228. $this->varGrpResult['grpResultId'] = $prefix . 'grpResult_' . $this->_getCleanCssId($subsite);
  229. $listResults = $this->_displayListResults($site, $subsite, $display, $nbrs, $searchResults, $found, $offset);
  230. $this->varGrpResult['listResults'] = ASPHX;
  231. $this->varGrpResult['footerGrpResult'] = $this->_displayFooterGrpResult($ig, $nbrs, $offset, $nbMax);
  232. $this->asResults->groupResults[$ig]['found'] = implode(' ',$found);
  233. $this->chkGrpResult->AddVar("as", $this->varGrpResult);
  234. $grpResult = $this->chkGrpResult->Render();
  235. $grpResult = str_replace(ASPHX, $listResults, $grpResult);
  236. unset($this->varGrpResult);
  237. unset($this->chkGrpResult);
  238. return $grpResult;
  239. }
  240. /*
  241. * Display the header of a group of results
  242. */
  243. function _displayHeaderGrpResult($site, $subsite, $display, $nbrs, $searchResults, $offset, $nbMax) {
  244. $varHeader = array();
  245. $varHeader['grpResultsDef'] = 0;
  246. $label = (isset($this->asCfg->scfg[$site][$subsite]['grpLabel'])) ? $this->asCfg->scfg[$site][$subsite]['grpLabel'] : $subsite;
  247. $varHeader['grpResultNameShow'] = ($label != DEFAULT_SUBSITE) ? 1 : 0;
  248. $varHeader['grpResultName'] = $label;
  249. $grpResultsDisplayedText = $this->asCfg->lang['as_grpResultsDisplayedText'];
  250. $grpResultNb = $nbrs;
  251. $grpResultStart = $offset + 1;
  252. $grpResultEnd = ($offset + $nbMax > $nbrs) ? $nbrs : $offset + $nbMax;
  253. $varHeader['grpResultsDisplayedText'] = sprintf($grpResultsDisplayedText, $grpResultStart, $grpResultEnd, $grpResultNb);
  254. $this->chkGrpResult->AddVar("as", $varHeader);
  255. $header = $this->chkGrpResult->Render();
  256. $this->chkGrpResult->CleanVars();
  257. return $header;
  258. }
  259. /*
  260. * Display the list of results
  261. */
  262. function _displayListResults($site, $subsite, $display, $nbrs, $searchResults, & $found, $offset) {
  263. $nb = count($searchResults);
  264. $listResults = '';
  265. for ($i = 0;$i < $nb;$i++) {
  266. $this->varResult = array();
  267. $found[] = $this->_setResultDisplayed($searchResults[$i]);
  268. $this->_setResultLink($searchResults[$i]);
  269. $this->_setResultExtract($searchResults[$i]);
  270. $this->_setResultBreadcrumbs($searchResults[$i]);
  271. $this->_setResultNumber($offset + $i + 1);
  272. $this->chkResult->AddVar("as", $this->varResult);
  273. $listResults .= $this->chkResult->Render();
  274. unset($this->varResult);
  275. $this->chkResult->CleanVars();
  276. }
  277. return $listResults;
  278. }
  279. /*
  280. * Display the footer of group results (paging)
  281. */
  282. function _displayFooterGrpResult($ig, $nbrs, $offset, $nbMax) {
  283. global $modx;
  284. $footer = '';
  285. $showPagingAlways = (int)$this->asCfg->cfg['showPagingAlways'];
  286. $pagingType = $this->asCfg->cfg['pagingType'];
  287. if ($nbMax > 0) {
  288. $numResultPages = ceil($nbrs / $nbMax);
  289. $maxOffset = ($numResultPages - 1) * $nbMax;
  290. $offset = ($offset > $maxOffset) ? $maxOffset : $offset;
  291. $offset = ($offset < 0) ? 0 : $offset;
  292. if (($pagingType == 0) && (!$this->asCfg->isAjax)) {
  293. $tplPaging = $this->asCfg->cfg['tplPaging0'];
  294. if ($tplPaging == '') $tplPaging = "@FILE:" . AS_SPATH . 'templates/paging0.tpl.html';
  295. $chkPaging = new AsPhxParser($tplPaging);
  296. if ($this->dbgTpl) $this->asUtil->dbgRecord($chkPaging->getTemplate($tplPaging), "tplPaging template " . $tplPaging);
  297. $resultPagingText = (($nbrs > $nbMax) || $showPagingAlways) ? $this->asCfg->lang['as_paginationTextMultiplePages'] : $this->asCfg->lang['as_paginationTextSinglePage'];
  298. $resultPageLinkNumber = 1;
  299. $resultPageLinks = '';
  300. $url = $this->_getParamsUrl();
  301. $otherOffset = $this->_getOtherOffset($ig);
  302. for ($nrp = 0;$nrp < $nbrs && (($nbrs > $nbMax) || $showPagingAlways);$nrp+= $nbMax) {
  303. $varLink = array();
  304. if ($offset == ($resultPageLinkNumber - 1) * $nbMax) {
  305. $varLink['tpl'] = 'pagingLinksCurrent';
  306. } else {
  307. $varLink['tpl'] = 'pagingLinks';
  308. $ofst = (string)$ig . ',' . (string)$nrp;
  309. $asOffset = ($otherOffset) ? $otherOffset . ',' . $ofst : $ofst;
  310. $asOffset = '&aso=' . $asOffset;
  311. $paramsUrl = $url . $asOffset;
  312. $varLink['pagingLink'] = $modx->makeUrl($modx->documentIdentifier, '', $paramsUrl);
  313. }
  314. $varLink['pagingSeparator'] = ($nrp + $nbMax < $nbrs) ? $this->asCfg->cfg['pageLinkSeparator'] : '';
  315. $varLink['pagingText'] = $resultPageLinkNumber;
  316. $resultPageLinkNumber++;
  317. $chkPaging->AddVar("as", $varLink);
  318. $resultPageLinks.= $chkPaging->Render();
  319. unset($varLink);
  320. $chkPaging->CleanVars();
  321. }
  322. $varPaging = array();
  323. $varPaging['tpl'] = 'paging';
  324. $varPaging['pagingText'] = $resultPagingText;
  325. $varPaging['pagingLinks'] = $resultPageLinks;
  326. $chkPaging->AddVar("as", $varPaging);
  327. $footer = $chkPaging->Render();
  328. unset($varPaging);
  329. $chkPaging->CleanVars();
  330. }
  331. else if (($pagingType == 1) && (($nbrs >= $nbMax) || showPagingAlways)) {
  332. $tplPaging = $this->asCfg->cfg['tplPaging1'];
  333. if ($tplPaging == '') $tplPaging = "@FILE:" . AS_SPATH . 'templates/paging1.tpl.html';
  334. if (!$this->asCfg->isAjax) {
  335. $url = $this->_getParamsUrl();
  336. $otherOffset = $this->_getOtherOffset($ig);
  337. }
  338. $chkPaging = new AsPhxParser($tplPaging);
  339. if ($this->dbgTpl) $this->asUtil->dbgRecord($chkPaging->getTemplate($tplPaging), "tplPaging template " . $tplPaging);
  340. $varPaging1 = array();
  341. if ($offset - $nbMax >= 0) {
  342. $varPaging1['showPrev'] = 1;
  343. $prevOffset = $offset - $nbMax;
  344. $prefix = ($this->asCfg->cfg['asId']) ? $this->asCfg->cfg['asId'] . "_" : '';
  345. $varPaging1['prev_grpResultId'] = $prefix . 'prev_' . $this->_getCleanCssId($this->asResults->groupResults[$ig]['subsite']);
  346. if ($this->asCfg->isAjax) $varPaging1['pagingPrev'] = 'javascript:void(0);';
  347. else {
  348. $ofst = (string)$ig . ',' . (string)$prevOffset;
  349. $asOffset = ($otherOffset) ? $otherOffset . ',' . $ofst : $ofst;
  350. $asOffset = '&aso=' . $asOffset;
  351. $paramsUrl = $url . $asOffset;
  352. $varPaging1['pagingPrev'] = $modx->makeUrl($modx->documentIdentifier, '', $paramsUrl);
  353. }
  354. }
  355. else $varPaging1['showPrev'] = 0;
  356. $varPaging1['pagingStart'] = $offset+1;
  357. $varPaging1['pagingEnd'] = ($offset + $nbMax > $nbrs) ? $nbrs : $offset + $nbMax;
  358. $varPaging1['pagingNb'] = $nbrs;
  359. if ($offset + $nbMax < $nbrs) {
  360. $varPaging1['showNext'] = 1;
  361. $nextOffset = $offset + $nbMax;
  362. $prefix = ($this->asCfg->cfg['asId']) ? $this->asCfg->cfg['asId'] . "_" : '';
  363. $varPaging1['next_grpResultId'] = $prefix . 'next_' . $this->_getCleanCssId($this->asResults->groupResults[$ig]['subsite']);
  364. if ($this->asCfg->isAjax) $varPaging1['pagingNext'] = 'javascript:void(0);';
  365. else {
  366. $nrp = $nrp + 1;
  367. $ofst = (string)$ig . ',' . (string)$nextOffset;
  368. $asOffset = ($otherOffset) ? $otherOffset . ',' . $ofst : $ofst;
  369. $asOffset = '&aso=' . $asOffset;
  370. $paramsUrl = $url . $asOffset;
  371. $varPaging1['pagingNext'] = $modx->makeUrl($modx->documentIdentifier, '', $paramsUrl);
  372. }
  373. }
  374. else $varPaging1['showNext'] = 0;
  375. $chkPaging->AddVar("as", $varPaging1);
  376. $footer = $chkPaging->Render();
  377. unset($varPaging1);
  378. $chkPaging->CleanVars();
  379. }
  380. elseif (($pagingType == 2) && ($nbrs >= $nbMax) && ($this->asCfg->isAjax)) {
  381. $tplPaging = $this->asCfg->cfg['tplPaging2'];
  382. if ($tplPaging == '') $tplPaging = "@FILE:" . AS_SPATH . 'templates/paging2.tpl.html';
  383. $chkPaging = new AsPhxParser($tplPaging);
  384. if ($this->dbgTpl) $this->asUtil->dbgRecord($chkPaging->getTemplate($tplPaging), "tplPaging template " . $tplPaging);
  385. $varPaging2 = array();
  386. $varPaging2['pagingStart'] = $offset+1;
  387. $varPaging2['pagingEnd'] = ($offset + $nbMax > $nbrs) ? $nbrs : $offset + $nbMax;
  388. $varPaging2['pagingNb'] = $nbrs;
  389. if ($offset + $nbMax < $nbrs) {
  390. $varPaging2['showMore'] = 1;
  391. $nextOffset = $offset + $nbMax;
  392. $prefix = ($this->asCfg->cfg['asId']) ? $this->asCfg->cfg['asId'] . "_" : '';
  393. $varPaging2['more_grpResultId'] = $prefix . 'more_' . $this->_getCleanCssId($this->asResults->groupResults[$ig]['subsite']);
  394. $varPaging2['pagingMore'] = 'javascript:void(0);';
  395. $paging2Text = $this->asCfg->lang['as_paging2Text'];
  396. $varPaging2['pagingText'] = sprintf($paging2Text, NB_MORE_RESULTS);
  397. }
  398. else $varPaging2['showMore'] = 0;
  399. $chkPaging->AddVar("as", $varPaging2);
  400. $footer = $chkPaging->Render();
  401. unset($varPaging2);
  402. $chkPaging->CleanVars();
  403. }
  404. }
  405. return $footer;
  406. }
  407. /*
  408. * Set the more results link
  409. */
  410. function _setMoreResultsLink($nbrs) {
  411. global $modx;
  412. $ajaxMax = $this->asCfg->cfg['ajaxMax'];
  413. if (($this->asCfg->cfg['moreResultsPage'] || $this->asCfg->cfg['showMoreResults']) && ($ajaxMax < $nbrs)) {
  414. $this->varResults['moreResults'] = 1;
  415. $this->varResults['moreClass'] = 'AS_ajax_more';
  416. $url = $this->_getParamsUrl();
  417. $this->varResults['moreLink'] = $modx->makeUrl($this->asCfg->cfg['moreResultsPage'], '', $url);
  418. $this->varResults['moreTitle'] = $this->asCfg->lang['as_moreResultsTitle'];
  419. $this->varResults['moreText'] = $this->asCfg->lang['as_moreResultsText'];
  420. }
  421. }
  422. /*
  423. * Get a clean css Id
  424. */
  425. function _getCleanCssId($name) {
  426. $name = preg_replace('/\s+\|\|\s+/','_',trim($name));
  427. $name = preg_replace('/\s+/','_',$name);
  428. return $name;
  429. }
  430. /*
  431. * Get the parameters to set up an URL
  432. */
  433. function _getParamsUrl() {
  434. global $modx;
  435. $firstarg = $modx->config['friendly_urls'] ? '?' : '&';
  436. $url = '';
  437. if ($this->asCfg->cfg['asId']) $url = $firstarg . 'asid=' . urlencode($this->asCfg->cfg['asId']);
  438. if ($this->asCtrl->searchString) {
  439. if ($url) $url .= '&search=' . urlencode($this->asCtrl->searchString) . '&amp;advsearch=' . urlencode($this->asCtrl->advSearch);
  440. else $url = $firstarg . 'search=' . urlencode($this->asCtrl->searchString) . '&amp;advsearch=' . urlencode($this->asCtrl->advSearch);
  441. }
  442. if ($this->asCtrl->subSearch) {
  443. if ($url) $url .= '&amp;subsearch=' . urlencode($this->asCtrl->subSearch);
  444. else $url = $firstarg . 'subsearch=' . urlencode($this->asCtrl->subSearch);
  445. }
  446. if ($this->asCtrl->asf) {
  447. if ($url) $url .= '&amp;asf=' . urlencode($this->asCtrl->asf);
  448. else $url = $firstarg . 'asf=' . urlencode($this->asCtrl->asf);
  449. foreach($this->asCtrl->fParams as $key =>$value) {
  450. $url .= '&amp;' . $key . '=' . urlencode($value);
  451. }
  452. }
  453. return $url;
  454. }
  455. /*
  456. * Initialize common chunks variables
  457. */
  458. function _initCommonChunks() {
  459. global $modx;
  460. if (!class_exists('AsPhxParser')) include_once AS_PATH . "classes/asPhxParser.class.inc.php";
  461. if (!$this->asCfg->isAjax) {
  462. $tplResults = $this->asCfg->cfg['tplResults'];
  463. if ($tplResults == '') $tplResults = "@FILE:" . AS_SPATH . 'templates/results.tpl.html';
  464. } else {
  465. $tplResults = $this->asCfg->cfg['tplAjaxResults'];
  466. // if @FILE binding was passed in via ajax processor, verify the path is safe
  467. if(stristr($tplResults, '@FILE:') !== false) {
  468. $path = substr($tplResults, 6);
  469. $frombase = $modx->config['base_path'] . $path;
  470. $dirname = dirname($frombase);
  471. $as_expected_dirname = $modx->config['base_path'] . AS_SPATH . 'templates';
  472. if(strpos($dirname, $as_expected_dirname) === false) {
  473. $path = str_replace('..', '', $path);
  474. $path = str_replace('\\', '/', $path);
  475. if(substr($path, 0, 1) == '/') $path = substr($path, 1);
  476. $tplResults = '@FILE:templates/' . $path;
  477. }
  478. if(!file_exists($as_expected_dirname . '/' . $path)) {
  479. $tplResults = '';
  480. }
  481. }
  482. if ($tplResults == '') $tplResults = "@FILE:" . AS_SPATH . 'templates/ajaxResults.tpl.html';
  483. }
  484. $this->chkResults = new AsPhxParser($tplResults);
  485. if ($this->dbgTpl) {
  486. $this->asUtil->dbgRecord($this->chkResults->getTemplate($tplResults), "tplResults template" . $tplResults);
  487. }
  488. }
  489. /*
  490. * Initialize chunks variables - config dependent
  491. */
  492. function _initChunks() {
  493. if (!$this->asCfg->isAjax) {
  494. $tplGrpResult = $this->asCfg->cfg['tplGrpResult'];
  495. if ($tplGrpResult == '') $tplGrpResult = "@FILE:" . AS_SPATH . 'templates/grpResult.tpl.html';
  496. $tplResult = $this->asCfg->cfg['tplResult'];
  497. if ($tplResult == '') $tplResult = "@FILE:" . AS_SPATH . 'templates/result.tpl.html';
  498. } else {
  499. $tplGrpResult = $this->asCfg->cfg['tplAjaxGrpResult'];
  500. if ($tplGrpResult == '') $tplGrpResult = "@FILE:" . AS_SPATH . 'templates/ajaxGrpResult.tpl.html';
  501. $tplResult = $this->asCfg->cfg['tplAjaxResult'];
  502. if ($tplResult == '') $tplResult = "@FILE:" . AS_SPATH . 'templates/ajaxResult.tpl.html';
  503. }
  504. $this->chkGrpResult = new AsPhxParser($tplGrpResult);
  505. $this->tplGrpRes = "@CODE:" . $this->chkGrpResult->template;
  506. $this->chkResult = new AsPhxParser($tplResult);
  507. $this->tplRes = "@CODE:" . $this->chkResult->template;
  508. if ($this->dbgTpl) {
  509. $this->asUtil->dbgRecord($this->chkGrpResult->getTemplate($tplGrpResult), "tplGrpResult template" . $tplGrpResult);
  510. if ($this->chkResults) $this->asUtil->dbgRecord($this->chkResults->getTemplate($tplResult), "tplResult template " . $tplResult);
  511. }
  512. }
  513. /*
  514. * Set the offset of groups (used only for non-ajax mode)
  515. */
  516. function _setOffset() {
  517. $offset_array = explode(',', $this->asCtrl->offset);
  518. $io = count($offset_array);
  519. for ($i = 0;$i < $io;$i = $i + 2) {
  520. $ig = intval($offset_array[$i]);
  521. $ig = (($ig >= 0) && ($ig < $this->asResults->nbGroups)) ? $ig : 0;
  522. $val = intval($offset_array[$i + 1]);
  523. $val = (($val > 0) && ($val < $this->asResults->groupResults[$ig]['length'])) ? $val : 0;
  524. $this->asResults->groupResults[$ig]['offset'] = $val;
  525. }
  526. }
  527. /*
  528. * Get the search context
  529. */
  530. function _getSearchContext() {
  531. $searchContext = $this->asResults->getSearchContext();
  532. $this->scMain = $searchContext['main'];
  533. $this->scJoined = $searchContext['joined'];
  534. $this->scTvs = $searchContext['tvs'];
  535. }
  536. /*
  537. * Initialize variables used for the display - config context dependent
  538. */
  539. function _initDisplayVariables() {
  540. $this->_initChunks();
  541. $this->_initBreadcrumbs();
  542. }
  543. /*
  544. * Set log infos into DB for failed searches
  545. */
  546. function _setFailedSearches($asCall = '', $select = '') {
  547. $logid = '';
  548. if ($this->log >= 1 ) {
  549. $logInfo = array();
  550. $logInfo['searchString'] = $this->asCtrl->searchString;
  551. $logInfo['nbResults'] = 0;
  552. $logInfo['results'] = '';
  553. $logInfo['asCall'] = $asCall;
  554. $logInfo['asSelect'] = mysql_real_escape_string($select);
  555. $logid = $this->asLog->setLogRecord($logInfo);
  556. }
  557. return $logid;
  558. }
  559. /*
  560. * Set log infos into DB for successfull searches
  561. */
  562. function _setSuccessfullSearches($ig) {
  563. $logid = '';
  564. if ($this->log == 2) {
  565. $logInfo = array();
  566. $logInfo['searchString'] = $this->asCtrl->searchString;
  567. $logInfo['nbResults'] = $this->asResults->groupResults[$ig]['length'];
  568. $logInfo['results'] = $this->asResults->groupResults[$ig]['found'];
  569. $logInfo['asCall'] = $this->_getAsCall($this->asResults->groupResults[$ig]['ucfg']);
  570. $logInfo['asSelect'] = mysql_real_escape_string($this->asResults->groupResults[$ig]['select']);
  571. $logid = $this->asLog->setLogRecord($logInfo);
  572. }
  573. return $logid;
  574. }
  575. /*
  576. * Get the AjaxSearch snippet call
  577. */
  578. function _getAsCall($ucfg) {
  579. $call_array = explode(' ', $ucfg);
  580. $tpl = "&%s=`%s`";
  581. if ($this->asCtrl->advSearch != ONEWORD) $call_array[] = sprintf($tpl, 'advSearch', $this->asCtrl->advSearch);
  582. $asCall = "[!AjaxSearch";
  583. if (count($call_array)) {
  584. $asCall.= "? ";
  585. $asCall.= implode(' ', $call_array);
  586. }
  587. $asCall.= "!]";
  588. return $asCall;
  589. }
  590. /*
  591. * Set all the displayed fields as PHx
  592. */
  593. function _setResultDisplayed($row) {
  594. $id = $this->scMain['id'];
  595. $this->varResult[$id] = $row[$id];
  596. if (isset($this->scMain['date'])) {
  597. foreach ($this->scMain['date'] as $field) $this->_setPhxField($field, $row, 'date');
  598. }
  599. foreach ($this->scMain['displayed'] as $field) $this->_setPhxField($field, $row, 'string');
  600. if (isset($this->scTvs['names'])) foreach ($this->scTvs['names'] as $field) {
  601. if (isset($row[$field])) $this->_setPhxField($field, $row, 'string');
  602. }
  603. if ($this->scMain['append']) foreach ($this->scMain['append'] as $field) {
  604. if (isset($row[$field])) $this->_setPhxField($field, $row, 'string');
  605. }
  606. if (isset($this->scJoined)) foreach ($this->scJoined as $joined) {
  607. $f = $joined['tb_alias'] . '_' . $id;
  608. $this->_setPhxField($f, $row, 'string');
  609. }
  610. if (isset($this->scJoined)) foreach ($this->scJoined as $joined) {
  611. foreach ($joined['displayed'] as $field) {
  612. $f = $joined['tb_alias'] . '_' . $field;
  613. $this->_setPhxField($f, $row, 'string');
  614. }
  615. }
  616. if ($this->asCfg->cfg['rank']) $this->_setPhxField('rank', $row, 'int');
  617. return $row[$id];
  618. }
  619. /*
  620. * Set a field as PHx
  621. */
  622. function _setPhxField($field, $row, $type = 'string') {
  623. $showField = $field . "Show";
  624. $classField = $field . "Class";
  625. $contentField = $row[$field];
  626. if ($contentField != '') {
  627. $this->varResult[$showField] = 1;
  628. $this->varResult[$classField] = $this->asClass['prefix'] . ucfirst($field);
  629. if ($type == 'string') {
  630. $this->varResult[$field] = $this->asResults->cleanText($contentField, $this->asCfg->cfg['stripOutput']);
  631. } elseif ($type == 'date') {
  632. $this->varResult[$field] = date($this->asCfg->cfg['formatDate'], $contentField);
  633. } else {
  634. $this->varResult[$field] = $contentField;
  635. }
  636. } else {
  637. $this->varResult[$showField] = 0;
  638. }
  639. }
  640. /*
  641. * Set the ResultLink PHx
  642. */
  643. function _setResultLink($row) {
  644. global $modx;
  645. $id = $this->scMain['id'];
  646. if (!$row[$id]) {
  647. return;
  648. }
  649. $hClass = $this->asClass['highlight'];
  650. if ($this->asCfg->cfg['highlightResult'] && $hClass) {
  651. $resultLink = $modx->makeUrl($row[$id], '', 'searched=' . urlencode($this->asCtrl->searchString) . '&amp;advsearch=' . urlencode($this->asCtrl->advSearch) . '&amp;highlight=' . urlencode($hClass));
  652. } else {
  653. $resultLink = $modx->makeUrl($row[$id]);
  654. }
  655. $this->varResult['resultClass'] = $this->asClass['prefix'];
  656. $this->varResult['resultLinkClass'] = $this->asClass['prefix'] . 'Link';
  657. $this->varResult['resultLink'] = $resultLink;
  658. }
  659. /*
  660. * Set the ResultExtract PHx
  661. */
  662. function _setResultExtract($row) {
  663. if ($this->asResults->extractNb) {
  664. $this->varResult['extractShow'] = 1;
  665. $this->varResult['extractClass'] = $this->asClass['prefix'] . 'Extract';
  666. $this->varResult['extract'] = $this->asResults->getExtractRow($row);
  667. } else {
  668. $this->varResult['extractShow'] = 0;
  669. }
  670. }
  671. /*
  672. * Set the ResultBreadcrumbs PHx
  673. */
  674. function _setResultBreadcrumbs($row) {
  675. global $modx;
  676. if ($this->asCfg->cfg['breadcrumbs']) {
  677. if ($this->asCfg->cfg['bCrumbsInfo']['type'] == 'function') {
  678. $bc = $this->asCfg->cfg['bCrumbsInfo']['name']($this->scMain, $row, $this->asCfg->cfg['bCrumbsInfo']['params']);
  679. } elseif ($this->asResults->getWithContent()) {
  680. $current_id = $modx->documentObject['id'];
  681. $current_parent = $modx->documentObject['parent'];
  682. $current_pagetitle = $modx->documentObject['pagetitle'];
  683. $current_longtitle = $modx->documentObject['longtitle'];
  684. $current_menutitle = $modx->documentObject['menutitle'];
  685. $current_description = $modx->documentObject['description'];
  686. $id = $this->scMain['id'];
  687. $modx->documentObject['id'] = $row[$id];
  688. $parentIds = $modx->getParentIds($row[$id], 1);
  689. $pid = array_pop($parentIds);
  690. $modx->documentObject['parent'] = $pid;
  691. $modx->documentObject['pagetitle'] = $row['pagetitle'];
  692. $modx->documentObject['longtitle'] = $row['longtitle'];
  693. $modx->documentObject['menutitle'] = $row['menutitle'];
  694. $modx->documentObject['description'] = $row['description'];
  695. $bc = $modx->runSnippet($this->asCfg->cfg['bCrumbsInfo']['name'], $this->asCfg->cfg['bCrumbsInfo']['params']);
  696. $modx->documentObject['id'] = $current_id;
  697. $modx->documentObject['parent'] = $current_parent;
  698. $modx->documentObject['pagetitle'] = $current_pagetitle;
  699. $modx->documentObject['longtitle'] = $current_longtitle;
  700. $modx->documentObject['menutitle'] = $current_menutitle;
  701. $modx->documentObject['description'] = $current_description;
  702. }
  703. $this->varResult['breadcrumbsShow'] = 1;
  704. $this->varResult['breadcrumbsClass'] = $this->asClass['prefix'] . 'Breadcrumbs';
  705. $this->varResult['breadcrumbs'] = $bc;
  706. } else {
  707. $this->varResult['breadcrumbsShow'] = 0;
  708. }
  709. }
  710. /*
  711. * Set number of result as PHx
  712. */
  713. function _setResultNumber($no) {
  714. $this->varResult['resultNumber'] = $no;
  715. }
  716. /*
  717. * Initialize the breadcrumbs variables
  718. */
  719. function _initBreadcrumbs() {
  720. if ($this->asCfg->cfg['breadcrumbs']) {
  721. $bc = explode(',', $this->asCfg->cfg['breadcrumbs']);
  722. if (function_exists($bc[0])) {
  723. $this->asCfg->cfg['bCrumbsInfo']['type'] = 'function';
  724. } elseif ($this->_snippet_exists($bc[0])) {
  725. $this->asCfg->cfg['bCrumbsInfo']['type'] = 'snippet';
  726. } else {
  727. $this->asCfg->cfg['breadcrumbs'] = false;
  728. }
  729. if ($this->asCfg->cfg['breadcrumbs']) {
  730. $this->asCfg->cfg['bCrumbsInfo']['name'] = array_shift($bc);
  731. $this->asCfg->cfg['bCrumbsInfo']['params'] = array();
  732. foreach ($bc as $prm) {
  733. $param = explode(':', $prm);
  734. $this->asCfg->cfg['bCrumbsInfo']['params'][$param[0]] = (isset($param[1]) ? $param[1] : 0);
  735. }
  736. }
  737. }
  738. }
  739. /*
  740. * Check the existing of a snippet
  741. */
  742. function _snippet_exists($snippetName) {
  743. global $modx;
  744. $tbl = $modx->getFullTableName('site_snippets');
  745. $select = "SELECT * FROM " . $tbl . " WHERE " . $tbl . ".name='" . $modx->db->escape($snippetName) . "';";
  746. $rs = $modx->db->query($select);
  747. return $modx->recordCount($rs);
  748. }
  749. /*
  750. * Get offset of other groups
  751. */
  752. function _getOtherOffset($ig) {
  753. $otherOffset = array();
  754. for ($i = 0;$i < $this->asResults->nbGroups;$i++) {
  755. if (($i != $ig) && ($this->asResults->groupResults[$i]['offset'] != 0)) {
  756. $otherOffset[] = (string)$i . ',' . (string)$this->asResults->groupResults[$i]['offset'];
  757. }
  758. }
  759. $output = implode(',', $otherOffset);
  760. return $output;
  761. }
  762. /*
  763. * Set the needsConvert flag value
  764. */
  765. function setNeedsConvert($flag) {
  766. $this->_needsConvert = $flag;
  767. }
  768. /*
  769. * initClassVariables : initialize the required Class values
  770. */
  771. function initClassVariables() {
  772. if ($this->asCfg->cfg['ajaxSearch']) $this->asClass['prefix'] = PREFIX_AJAX_RESULT_CLASS;
  773. else $this->asClass['prefix'] = PREFIX_RESULT_CLASS;
  774. $this->asClass['highlight'] = $this->_getHighlightClass($this->asCtrl->searchString, $this->asCtrl->advSearch);
  775. }
  776. /*
  777. * Depending the search words, set up the highlight classes
  778. */
  779. function _getHighlightClass($search, $advSearch) {
  780. $hClass = '';
  781. $searchList = $this->asCtrl->getSearchWords($search, $advSearch);
  782. if (count($searchList)) {
  783. $hClass = HIGHLIGHT_CLASS;
  784. $count = 1;
  785. foreach ($searchList as $searchTerm) {
  786. $hClass.= ' ' . HIGHLIGHT_CLASS . $count;
  787. $count++;
  788. }
  789. }
  790. return $hClass;
  791. }
  792. function getHClass() {
  793. return HIGHLIGHT_CLASS;
  794. }
  795. /*
  796. * Set the ajax header with the appropriate variables
  797. */
  798. function setAjaxSearchHeader() {
  799. global $modx;
  800. $typeAs = $this->asCfg->cfg['ajaxSearch'];
  801. if ($typeAs) {
  802. if ($this->asCfg->cfg['jscript'] == 'jquery') {
  803. if ($this->asCfg->cfg['addJscript']) $modx->regClientStartupScript($this->asCfg->cfg['jsJquery']);
  804. $jsInclude = AS_SPATH . AJAXSEARCH_JSDIR . $typeAs . '/ajaxSearch-jquery.js';
  805. } elseif ($this->asCfg->cfg['jscript'] == 'mootools2') {
  806. if ($this->asCfg->cfg['addJscript']) $modx->regClientStartupScript($this->asCfg->cfg['jsMooTools2']);
  807. $jsInclude = AS_SPATH . AJAXSEARCH_JSDIR . $typeAs . '/ajaxSearch-mootools2.js';
  808. } else {
  809. if ($this->asCfg->cfg['addJscript']) $modx->regClientStartupScript($this->asCfg->cfg['jsMooTools']);
  810. $jsInclude = AS_SPATH . AJAXSEARCH_JSDIR . $typeAs . '/ajaxSearch.js';
  811. }
  812. $modx->regClientStartupScript($jsInclude);
  813. $json = '{"vsn":"' . AS_VERSION . '"';
  814. $json.= ',"adv":"' . $this->asCtrl->advSearch . '"';
  815. $json.= ',"sub":"' . $this->asCtrl->subSearch . '"';
  816. $json.= ',"bxt":"' . addslashes($this->asCfg->lang['as_boxText']) . '"';
  817. $json.= ',"cfg":"' . addslashes($this->asCfg->setAsCall($this->asCfg->ucfg)) . '"}';
  818. $line = (!$this->asCfg->cfg['asId']) ? "asvar=new Array();asvar[0]='{$json}';" : "asvar[asvar.length]='{$json}';";
  819. $jsVars = <<<EOD
  820. <!-- start AjaxSearch header -->
  821. <script type="text/javascript">
  822. //<![CDATA[
  823. {$line}
  824. //]]>
  825. </script>
  826. <!-- end AjaxSearch header -->
  827. EOD;
  828. $modx->regClientStartupScript($jsVars);
  829. }
  830. }
  831. /*
  832. * Display Comment form
  833. */
  834. function _displayComment() {
  835. $outputComment = '';
  836. if ($this->asLog->logcmt && count($this->logIds)) {
  837. $chkCmt = new AsPhxParser($this->asCfg->cfg['tplComment']);
  838. if ($this->dbgTpl) $this->asUtil->dbgRecord($chkCmt->getTemplate($this->asCfg->cfg['tplComment']), "tplComment template " . $this->asCfg->cfg['tplComment']);
  839. $varCmt = array();
  840. $varCmt['hiddenFieldIntro'] = $this->asCfg->lang['as_cmtHiddenFieldIntro'];
  841. $varCmt['hiddenField'] = 'ajaxSearch_cmtHField';
  842. $varCmt['logid'] = array_pop($this->logIds);
  843. $varCmt['cmtIntroMessage'] = $this->asCfg->lang['as_cmtIntroMessage'];
  844. $varCmt['cmtSubmitText'] = $this->asCfg->lang['as_cmtSubmitText'];
  845. $varCmt['cmtResetText'] = $this->asCfg->lang['as_cmtResetText'];
  846. $varCmt['cmtThksMessage'] = $this->asCfg->lang['as_cmtThksMessage'];
  847. $chkCmt->AddVar("as", $varCmt);
  848. $outputComment = $chkCmt->Render();
  849. }
  850. return $outputComment;
  851. }
  852. /*
  853. * paginate : display the previous / next page of results
  854. */
  855. function paginate($validSearch, &$msgErr) {
  856. $ouputResults = null;
  857. if ($validSearch) {
  858. list($ig, $currentOffset, $sens) = explode(',',$this->asCtrl->pagination);
  859. $this->_getSearchContext();
  860. $site = $this->asResults->groupResults[$ig]['site'];
  861. $subsite = $this->asResults->groupResults[$ig]['subsite'];
  862. $display = $this->asResults->groupResults[$ig]['display'];
  863. $nbrs = intval($this->asResults->groupResults[$ig]['length']);
  864. $ajaxMax = ($this->asCfg->cfg['ajaxMax'] > 0) ? $this->asCfg->cfg['ajaxMax'] : $nbrs;
  865. $pagingType = $this->asCfg->cfg['pagingType'];
  866. // nb results displayed
  867. if ($pagingType == 1) {
  868. if ($sens == 1) {
  869. $offset = $currentOffset + $ajaxMax;
  870. $nbRes = ($offset + $ajaxMax > $nbrs) ? $nbrs - $offset : $ajaxMax;
  871. }
  872. else {
  873. $offset = $currentOffset - $ajaxMax;
  874. $offset = ($offset >= 0) ? $offset : 0;
  875. $nbRes = $ajaxMax;
  876. }
  877. }
  878. elseif ($pagingType == 2) {
  879. if ($sens > 0) {
  880. if ($currentOffset == 0) $currentNbRes = $ajaxMax;
  881. else $currentNbRes = NB_MORE_RESULTS;
  882. $offset = $currentOffset + $currentNbRes;
  883. $nbRes = ($offset + NB_MORE_RESULTS > $nbrs) ? $nbrs - $offset : NB_MORE_RESULTS;
  884. }
  885. else {
  886. if ($currentOffset == $ajaxMax) $previousNbRes = $ajaxMax;
  887. else $previousNbRes = NB_MORE_RESULTS;
  888. $offset = $currentOffset - $previousNbRes;
  889. $offset = ($offset >= 0) ? $offset : 0;
  890. $nbRes = $previousNbRes;
  891. }
  892. }
  893. $searchResults = array_slice($this->asResults->groupResults[$ig]['results'], $offset, $nbRes);
  894. $this->asCfg->chooseConfig($site, $subsite, $display);
  895. $this->_initDisplayVariables();
  896. $jsonPairs = array();
  897. if ($pagingType == 1) { // Prev / Next links
  898. $grpResult = $this->_displayGrpResult($ig, $site, $subsite, $display, $nbrs, $searchResults, $offset, $nbRes);
  899. $jsonPairs[] = $this->_getJsonPair('res',$grpResult);
  900. }
  901. elseif ($pagingType == 2) {
  902. $found = array();
  903. $this->chkGrpResult = new AsPhxParser($this->tplGrpRes);
  904. $moreOffset = 0;
  905. $moreNbMax = $offset + $nbRes;
  906. $header = $this->_displayHeaderGrpResult($site, $subsite, $display, $nbrs, $searchResults, $moreOffset, $moreNbMax);
  907. $listResults = $this->_displayListResults($site, $subsite, $display, $nbrs, $searchResults, $found, $offset);
  908. $footer = $this->_displayFooterGrpResult($ig, $nbrs, $moreOffset, $moreNbMax);
  909. $this->asResults->groupResults[$ig]['found'] = implode(' ',$found);
  910. $jsonPairs[] = $this->_getJsonPair('hdr',$header);
  911. $jsonPairs[] = $this->_getJsonPair('res',$listResults);
  912. $jsonPairs[] = $this->_getJsonPair('ftr',$footer);
  913. }
  914. $pgn = (string)$ig . ',' . (string)$offset;
  915. $jsonPairs[] = $this->_getJsonPair('pgn',$pgn);
  916. $this->_updateAsfPaginate($ig, $jsonPairs);
  917. $outputResults = $this->_getJson($jsonPairs);
  918. $this->asCfg->restoreConfig(DEFAULT_SITE, DEFAULT_SUBSITE);
  919. }
  920. return $outputResults;
  921. }
  922. /*
  923. * Send back categories & tags
  924. */
  925. function _updateAsfPaginate($ig, & $jsonPairs) {
  926. $resultsCateg = array();
  927. $resultsCateg = $this->asResults->getResultsCateg();
  928. $ctgnm = array();
  929. $ctgnm[] = $resultsCateg['name'][$ig];
  930. if ($this->dbgRes) $this->asUtil->dbgRecord($resultsCateg, "getResultsCateg");
  931. $sctgnm = $this->_getJsonArray($ctgnm);
  932. $jsonPairs[] = $this->_getJsonPair('ctgnm',$sctgnm);
  933. return;
  934. }
  935. /*
  936. * Send back categories
  937. */
  938. function _updateAsf( & $jsonPairs) {
  939. $resultsCateg = array();
  940. $resultsCateg = $this->asResults->getResultsCateg();
  941. if ($this->dbgRes) $this->asUtil->dbgRecord($resultsCateg, "getResultsCateg");
  942. $ctgnm = $this->_getJsonArray($resultsCateg['name']);
  943. $jsonPairs[] = $this->_getJsonPair('ctgnm',$ctgnm);
  944. $ctgnb = $this->_getJsonArray($resultsCateg['nb']);
  945. $jsonPairs[] = $this->_getJsonPair('ctgnb',$ctgnb);
  946. return;
  947. }
  948. /*
  949. * Prepare json array of String
  950. * ["art","music","geography"]
  951. */
  952. function _getJsonArray($array) {
  953. $nbr = count($array);
  954. if ($nbr) $jsonArray = '[' . implode(',',$array) . ']';
  955. else $jsonArray = '';
  956. return $jsonArray;
  957. }
  958. /*
  959. * Prepare json pair key : value
  960. */
  961. function _getJsonPair($key,$value) {
  962. $value = addslashes($value);
  963. $value = str_replace(array("\r\n", "\r", "\n"), ' ', $value);
  964. $jsonPair = '"' . $key . '":"' . $value . '"';
  965. return $jsonPair;
  966. }
  967. /*
  968. * Prepare json string
  969. */
  970. function _getJson($pairs) {
  971. $json = '{' . implode(',',$pairs) . '}';
  972. return $json;
  973. }
  974. }
  975. ?>