1: <?php
2:
3:
4: if(!isset($config)) {
5: require_once '../util/AlphaConfig.inc';
6: $config = AlphaConfig::getInstance();
7:
8: require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
9: }
10:
11: 12: 13: 14: 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: class Search extends AlphaController implements AlphaControllerInterface {
57: 58: 59: 60: 61: 62:
63: private static $logger = null;
64:
65: 66: 67: 68: 69: 70:
71: protected $startPoint;
72:
73: 74: 75: 76: 77: 78:
79: private $resultCount = 0;
80:
81: 82: 83: 84: 85: 86:
87: private $query;
88:
89: 90: 91: 92: 93: 94:
95: public function __construct($visibility='Public') {
96: self::$logger = new Logger('Search');
97: self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
98:
99: global $config;
100:
101:
102: parent::__construct($visibility);
103:
104: self::$logger->debug('<<__construct');
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function doGET($params) {
115: self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
116:
117: if (isset($params['start']) ? $this->startPoint = $params['start']: $this->startPoint = 0);
118:
119: global $config;
120:
121: $KPI = new AlphaKPI('search');
122:
123: if(isset($params['q'])) {
124:
125: $this->query = $params['q'];
126:
127:
128: $params['q'] = str_replace('%20', ' ', $params['q']);
129:
130: $this->setTitle('Search results - '.$params['q']);
131: echo AlphaView::displayPageHead($this);
132:
133:
134: $log = new LogFile($config->get('app.file.store.dir').'logs/search.log');
135: $log->writeLine(array($params['q'], date('Y-m-d H:i:s'), $_SERVER['HTTP_USER_AGENT'], $_SERVER['REMOTE_ADDR']));
136:
137: $KPI->logStep('log search query');
138:
139: $provider = SearchProviderFactory::getInstance('SearchProviderTags');
140:
141:
142: if(isset($params['bo']))
143: $results = $provider->search($params['q'], $params['bo'], $this->startPoint);
144: else
145: $results = $provider->search($params['q'], 'all', $this->startPoint);
146:
147: $this->resultCount = $provider->getNumberFound();
148:
149: $KPI->logStep('search completed using SearchProviderTags provider');
150:
151: $this->renderResultList($results, $params['q']);
152:
153: }else{
154: $this->setTitle('Search results');
155: echo AlphaView::displayPageHead($this);
156: self::$logger->debug('No search query provided!');
157: }
158:
159: echo AlphaView::displayPageFoot($this);
160:
161: $KPI->log();
162:
163: self::$logger->debug('<<doGET');
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173:
174: protected function renderResultList($results, $query='', $showTags=true) {
175: global $config;
176:
177:
178: $end = ($this->startPoint+$config->get('app.list.page.amount'));
179:
180: if(!empty($query))
181: echo '<h2>Displaying results for "'.$query.'"</h2>';
182:
183: foreach($results as $bo) {
184:
185: if($bo instanceof ArticleObject && $bo->get('published') == false){
186: $this->resultCount--;
187: }else{
188: $view = AlphaView::getInstance($bo);
189: echo $view->listView();
190:
191: if($showTags) {
192: $tags = $bo->getPropObject('tags')->getRelatedObjects();
193:
194: if(count($tags) > 0) {
195: echo '<p>Tags: ';
196:
197: $queryTerms = explode(' ', mb_strtolower($query));
198:
199: foreach($tags as $tag) {
200: echo (in_array($tag->get('content'), $queryTerms) ? '<strong>'.$tag->get('content').' </strong>' : $tag->get('content').' ');
201: }
202:
203: echo '</p>';
204: }
205: }
206: }
207:
208: }
209: }
210:
211: 212: 213: 214: 215: 216:
217: public function doPOST($params) {
218: self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
219:
220: self::$logger->debug('<<doPOST');
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function after_displayPageHead_callback() {
230: global $config;
231:
232: $html = '<div align="center"><form method="GET" id="search_form" onsubmit="document.location = \''.$config->get('app.url').'search/q/\'+document.getElementById(\'q\').value; return false;">';
233: $html .= 'Search for: <input type="text" size="80" name="q" id="q"/> ';
234: $button = new Button('document.location = \''.$config->get('app.url').'search/q/\'+document.getElementById(\'q\').value', 'Search', 'searchButton');
235: $html .= $button->render();
236: $html .= '</form></div>';
237:
238: return $html;
239: }
240:
241: 242: 243: 244: 245: 246:
247: public function () {
248: $html = $this->renderPageLinks();
249:
250: $html .= '<br>';
251:
252: return $html;
253: }
254:
255: 256: 257: 258: 259: 260:
261: protected function renderPageLinks() {
262: global $config;
263:
264: $html = '';
265:
266: $end = ($this->startPoint+$config->get('app.list.page.amount'));
267:
268: if($end > $this->resultCount)
269: $end = $this->resultCount;
270:
271: if($this->resultCount > 0) {
272: $html .= '<p align="center">Displaying '.($this->startPoint+1).' to '.$end.' of <strong>'.$this->resultCount.'</strong>. ';
273: }else{
274: if(!empty($this->query))
275: $html .= AlphaView::displayUpdateMessage('There were no search results for your query.');
276: }
277:
278: if ($this->startPoint > 0) {
279:
280: if(isset($_GET['tk']))
281: $html .= '<a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.($this->startPoint-$config->get('app.list.page.amount'))).'"><<-Previous</a> ';
282: else
283: $html .= '<a href="'.$config->get('app.url').'search/q/'.$this->query.'/start/'.($this->startPoint-$config->get('app.list.page.amount')).'"><<-Previous</a> ';
284: }elseif($this->resultCount > $config->get('app.list.page.amount')){
285: $html .= '<<-Previous ';
286: }
287: $page = 1;
288: for ($i = 0; $i < $this->resultCount; $i+=$config->get('app.list.page.amount')) {
289: if($i != $this->startPoint) {
290:
291: if(isset($_GET['tk']))
292: $html .= ' <a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.$i).'">'.$page.'</a> ';
293: else
294: $html .= ' <a href="'.$config->get('app.url').'search/q/'.$this->query.'/start/'.$i.'">'.$page.'</a> ';
295: }elseif($this->resultCount > $config->get('app.list.page.amount')){
296: $html .= ' '.$page.' ';
297: }
298: $page++;
299: }
300: if ($this->resultCount > $end) {
301:
302: if(isset($_GET['tk']))
303: $html .= ' <a href="'.FrontController::generateSecureURL('act=Search&q='.$this->query.'&start='.($this->startPoint+$config->get('app.list.page.amount'))).'">Next->></a>';
304: else
305: $html .= ' <a href="'.$config->get('app.url').'search/q/'.$this->query.'/start/'.($this->startPoint+$config->get('app.list.page.amount')).'">Next->></a>';
306: }elseif($this->resultCount > $config->get('app.list.page.amount')){
307: $html .= ' Next->>';
308: }
309: $html .= '</p>';
310:
311: return $html;
312: }
313: }
314:
315:
316: if(basename($_SERVER['PHP_SELF']) == 'Search.php') {
317: $controller = new Search();
318:
319: if(!empty($_POST)) {
320: $controller->doPOST($_REQUEST);
321: }else{
322: $controller->doGET($_GET);
323: }
324: }
325:
326: ?>