1: <?php
2:
3: namespace Alpha\Controller;
4:
5: use Alpha\Util\Logging\Logger;
6: use Alpha\Util\Logging\KPI;
7: use Alpha\Util\Logging\LogProviderFile;
8: use Alpha\Util\Config\ConfigProvider;
9: use Alpha\Util\Http\Request;
10: use Alpha\Util\Http\Response;
11: use Alpha\Util\Search\SearchProviderFactory;
12: use Alpha\View\View;
13: use Alpha\View\Widget\Button;
14: use Alpha\Controller\Front\FrontController;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: class SearchController extends Controller implements ControllerInterface
59: {
60: 61: 62: 63: 64: 65: 66:
67: private static $logger = null;
68:
69: 70: 71: 72: 73: 74: 75:
76: protected $startPoint;
77:
78: 79: 80: 81: 82: 83: 84:
85: protected $resultCount = 0;
86:
87: 88: 89: 90: 91: 92: 93:
94: protected $query;
95:
96: 97: 98: 99: 100: 101: 102:
103: public function __construct($visibility = 'Public')
104: {
105: self::$logger = new Logger('SearchController');
106: self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
107:
108: $config = ConfigProvider::getInstance();
109:
110:
111: parent::__construct($visibility);
112:
113: self::$logger->debug('<<__construct');
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
127: public function doGET($request)
128: {
129: self::$logger->debug('>>doGET($request=['.var_export($request, true).'])');
130:
131: $params = $request->getParams();
132:
133: if (isset($params['start']) ? $this->startPoint = $params['start'] : $this->startPoint = 0);
134:
135: $config = ConfigProvider::getInstance();
136:
137: $KPI = new KPI('search');
138:
139: $body = '';
140:
141: if (isset($params['query'])) {
142: $this->query = $params['query'];
143:
144:
145: $params['query'] = str_replace('%20', ' ', $params['query']);
146:
147: $this->setTitle('Search results - '.$params['query']);
148: $body .= View::displayPageHead($this);
149:
150:
151: $log = new LogProviderFile();
152: $log->setPath($config->get('app.file.store.dir').'logs/search.log');
153: $log->writeLine(array($params['query'], date('Y-m-d H:i:s'), $request->getUserAgent(), $request->getIP()));
154:
155: $KPI->logStep('log search query');
156:
157: $provider = SearchProviderFactory::getInstance('Alpha\Util\Search\SearchProviderTags');
158:
159:
160: if (isset($params['ActiveRecordType'])) {
161: $results = $provider->search($params['query'], $params['bo'], $this->startPoint);
162: } else {
163: $results = $provider->search($params['query'], 'all', $this->startPoint);
164: }
165:
166: $this->resultCount = $provider->getNumberFound();
167:
168: $KPI->logStep('search completed using SearchProviderTags provider');
169:
170: $body .= $this->renderResultList($results, $params['query']);
171: } else {
172: $this->setTitle('Search results');
173: $body .= View::displayPageHead($this);
174: self::$logger->debug('No search query provided!');
175: }
176:
177: $body .= View::displayPageFoot($this);
178:
179: $KPI->log();
180:
181: self::$logger->debug('<<doGET');
182:
183: return new Response(200, $body, array('Content-Type' => 'text/html'));
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: protected function renderResultList($results, $query = '', $showTags = true)
198: {
199: $config = ConfigProvider::getInstance();
200:
201:
202: $end = ($this->startPoint + $config->get('app.list.page.amount'));
203:
204: $body = '';
205:
206: if (!empty($query)) {
207: $body .= '<h2>Displaying results for "'.$query.'"</h2>';
208: }
209:
210: foreach ($results as $bo) {
211: if ($bo instanceof \Alpha\Model\Article && $bo->get('published') == false) {
212: --$this->resultCount;
213: } else {
214: $view = View::getInstance($bo);
215: $URI = $this->request->getURI();
216: $body .= $view->listView(array('formAction' => $URI));
217:
218: if ($showTags) {
219: $tags = $bo->getPropObject('tags')->getRelatedObjects();
220:
221: if (count($tags) > 0) {
222: $body .= '<p>Tags: ';
223:
224: $queryTerms = explode(' ', mb_strtolower($query));
225:
226: foreach ($tags as $tag) {
227: $body .= (in_array($tag->get('content'), $queryTerms) ? '<strong>'.$tag->get('content').' </strong>' : $tag->get('content').' ');
228: }
229:
230: $body .= '</p>';
231: }
232: }
233: }
234: }
235:
236: return $body;
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: public function after_displayPageHead_callback()
247: {
248: $config = ConfigProvider::getInstance();
249:
250: $body = parent::after_displayPageHead_callback();
251:
252: $body .= '<div align="center" class="form-group"><form class="form-inline" method="GET" id="search_form" onsubmit="document.location = \''.$config->get('app.url').'search/\'+document.getElementById(\'q\').value; return false;">';
253: $body .= '<label for="q">Search for</label><input type="text" name="q" id="q" class="form-control" style="width:50%; margin:10px;"/>';
254: $button = new Button('document.location = \''.$config->get('app.url').'/search/\'+document.getElementById(\'q\').value', 'Search', 'searchButton');
255: $body .= $button->render();
256: $body .= '</p></form></div>';
257:
258: return $body;
259: }
260:
261: 262: 263: 264: 265: 266: 267:
268: public function ()
269: {
270: $body = $this->renderPageLinks();
271:
272: $body .= '<br>';
273:
274: return $body;
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: protected function renderPageLinks()
285: {
286: $config = ConfigProvider::getInstance();
287:
288: $params = $this->request->getParams();
289:
290: $body = '';
291:
292: $end = ($this->startPoint + $config->get('app.list.page.amount'));
293:
294: if ($end > $this->resultCount) {
295: $end = $this->resultCount;
296: }
297:
298: if ($this->resultCount > 0) {
299: $body .= '<p align="center">Displaying '.($this->startPoint + 1).' to '.$end.' of <strong>'.$this->resultCount.'</strong>. ';
300: } else {
301: if (!empty($this->query)) {
302: $body .= View::displayUpdateMessage('There were no search results for your query.');
303: }
304: }
305:
306: $body .= '<ul class="pagination">';
307:
308: if ($this->startPoint > 0) {
309:
310: if (isset($params['tk'])) {
311: $body .= '<li><a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.($this->startPoint - $config->get('app.list.page.amount'))).'">«</a></li>';
312: } else {
313: $body .= '<li><a href="'.$config->get('app.url').'/search/'.$this->query.'/'.($this->startPoint - $config->get('app.list.page.amount')).'">«</a></li>';
314: }
315: } elseif ($this->resultCount > $config->get('app.list.page.amount')) {
316: $body .= '<li class="disabled"><a href="#">«</a></li>';
317: }
318:
319: $page = 1;
320:
321: for ($i = 0; $i < $this->resultCount; $i += $config->get('app.list.page.amount')) {
322: if ($i != $this->startPoint) {
323:
324: if (isset($params['tk'])) {
325: $body .= '<li><a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.$i).'">'.$page.'</a></li>';
326: } else {
327: $body .= '<li><a href="'.$config->get('app.url').'/search/'.$this->query.'/'.$i.'">'.$page.'</a></li>';
328: }
329: } elseif ($this->resultCount > $config->get('app.list.page.amount')) {
330: $body .= '<li class="active"><a href="#">'.$page.'</a></li>';
331: }
332:
333: ++$page;
334: }
335:
336: if ($this->resultCount > $end) {
337:
338: if (isset($params['tk'])) {
339: $body .= '<li><a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.($this->startPoint + $config->get('app.list.page.amount'))).'">Next->></a></li>';
340: } else {
341: $body .= '<li><a href="'.$config->get('app.url').'/search/'.$this->query.'/'.($this->startPoint + $config->get('app.list.page.amount')).'">»</a></li>';
342: }
343: } elseif ($this->resultCount > $config->get('app.list.page.amount')) {
344: $body .= '<li class="disabled"><a href="#">»</a></li>';
345: }
346:
347: $body .= '</ul>';
348: $body .= '</p>';
349:
350: return $body;
351: }
352:
353: 354: 355: 356: 357: 358: 359:
360: public function getResultCount()
361: {
362: return $this->resultCount;
363: }
364:
365: 366: 367: 368: 369: 370: 371:
372: protected function setResultCount($resultCount)
373: {
374: $this->resultCount = $resultCount;
375: }
376:
377: 378: 379: 380: 381: 382: 383:
384: public function getSearchQuery()
385: {
386: return $this->query;
387: }
388:
389: 390: 391: 392: 393: 394: 395:
396: protected function setSearchQuery($query)
397: {
398: $this->query = $query;
399: }
400: }
401: