XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
SearchResultsAction.class.php
1<?php
11
12if (!defined('XOOPS_ROOT_PATH')) {
13 exit();
14}
15
16require_once XOOPS_MODULE_PATH . '/legacy/forms/SearchResultsForm.class.php';
17
18const LEGACY_SEARCH_RESULT_MAXHIT = 5;
19const LEGACY_SEARCH_SHOWALL_MAXHIT = 20;
20
21
22class Legacy_SearchResultsAction extends Legacy_Action
23{
24 public $mActionForm = null;
25 public $mSearchResults = [];
26 public $mModules = [];
27
28 public $mConfig = [];
29
30 public function prepare(&$controller, &$xoopsUser)
31 {
32 $root =& $controller->mRoot;
33 $root->mLanguageManager->loadPageTypeMessageCatalog('search');
34 $root->mLanguageManager->loadModuleMessageCatalog('legacy');
35
36 $handler =& xoops_gethandler('config');
37 $this->mConfig =& $handler->getConfigsByCat(XOOPS_CONF_SEARCH);
38
39 $this->_setupActionForm();
40 }
41
42 public function _setupActionForm()
43 {
44 $this->mActionForm =new Legacy_SearchResultsForm($this->mConfig['keyword_min']);
45 $this->mActionForm->prepare();
46 }
47
48 public function hasPermission(&$controller, &$xoopsUser)
49 {
50 // Avoid strict check !
51 if (1 != $this->mConfig['enable_search']) {
52 $controller->executeRedirect(XOOPS_URL . '/', 3, _MD_LEGACY_ERROR_SEARCH_NOT_ENABLED);
53 return false;
54 }
55 return true;
56 }
57
58 public function _getMaxHit()
59 {
60 return LEGACY_SEARCH_RESULT_MAXHIT;
61 }
62
63 public function getDefaultView(&$controller, &$xoopsUser)
64 {
65 $params = [];
66 $root =& $controller->mRoot;
67 $service =& $root->mServiceManager->getService('LegacySearch');
68
69 if (is_object($service)) {
70 $client =& $root->mServiceManager->createClient($service);
71 $this->mModules = $client->call('getActiveModules', []);
72 }
73
74 $this->mActionForm->fetch();
75 $this->mActionForm->validate();
76
77 if ($this->mActionForm->hasError()) {
78 return LEGACY_FRAME_VIEW_INDEX;
79 }
80
81 //
82 // TODO ErrorHandling
83 //
84 if (is_object($service)) {
85 $this->mActionForm->update($params);
86
87 $handler =& xoops_gethandler('module');
88 foreach ($this->_getSelectedMids() as $mid) {
89 $t_module =& $handler->get($mid);
90 if (is_object($t_module)) {
91 $module = [];
92
93 $module['mid'] = $mid;
94 $module['name'] = $t_module->get('name');
95
96 $params['mid'] = $mid;
97 $module['results'] = $this->_doSearch($client, $xoopsUser, $params);
98
99 if ((is_countable($module['results']) ? count($module['results']) : 0) > 0) {
100 // @todo @gigamaster $module['has_more'] = (count($module['results']) >= $this->_getMaxHit()) ? true : false;
101 $module['has_more'] = (is_countable($module['results']) ? count($module['results']) : 0) >= $this->_getMaxHit();
102 $this->mSearchResults[] = $module;
103 }
104 }
105 }
106 } else {
107 return LEGACY_FRAME_VIEW_ERROR;
108 }
109
110 return LEGACY_FRAME_VIEW_INDEX;
111 }
112
113 public function _doSearch(&$client, &$xoopsUser, &$params)
114 {
115 $root =& XCube_Root::getSingleton();
116 $timezone = $root->mContext->getXoopsConfig('server_TZ') * 3600;
117
118 $results = $client->call('searchItems', $params);
119
120 return $results;
121 }
122
123 public function execute(&$controller, &$xoopsUser)
124 {
125 return $this->getDefaultView($controller, $xoopsUser);
126 }
127
128 public function executeViewIndex(&$controller, &$xoopsUser, &$render)
129 {
130 $render->setTemplateName($this->_getTemplateName());
131
132 $render->setAttribute('actionForm', $this->mActionForm);
133
134 $render->setAttribute('searchResults', $this->mSearchResults);
135 $render->setAttribute('moduleArr', $this->mModules);
136
137 //
138 // If the request include $mids, setAttribute it. If it don't include,
139 // setAttribute $mid or $this->mModules.
140 //
141 $render->setAttribute('selectedMidArr', $this->_getSelectedMids());
142 $render->setAttribute('searchRuleMessage', @sprintf(_SR_KEYTOOSHORT, $this->mConfig['keyword_min']));
143 }
144
145 public function _getTemplateName()
146 {
147 return 'legacy_search_results.html';
148 }
149
150 public function _getSelectedMids()
151 {
152 $ret = $this->mActionForm->get('mids');
153 if (!(is_countable($ret) ? count($ret) : 0)) {
154 foreach ($this->mModules as $module) {
155 $ret[] = $module['mid'];
156 }
157 }
158
159 return $ret;
160 }
161
162 public function executeViewError(&$controller, &$xoopsUser, &$render)
163 {
164 $controller->executeForward(XOOPS_URL . '/');
165 }
166}