Commit e701a8b6bccbcff93f18deeb90ea476a553d36a0
1 parent
707f8542
KTS-673
"The search algorithm needs some work" Implemented. Committed By: Conrad Vermeulen Reviewed By: Kevin Fourie git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7145 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
422 additions
and
0 deletions
search2.php
0 → 100755
| 1 | +<?php | |
| 2 | + | |
| 3 | +session_start(); | |
| 4 | +require_once("config/dmsDefaults.php"); | |
| 5 | +require_once(KT_LIB_DIR . "/unitmanagement/Unit.inc"); | |
| 6 | + | |
| 7 | +require_once(KT_LIB_DIR . "/templating/templating.inc.php"); | |
| 8 | +require_once(KT_LIB_DIR . "/dispatcher.inc.php"); | |
| 9 | +require_once(KT_LIB_DIR . "/widgets/forms.inc.php"); | |
| 10 | +require_once(KT_DIR . '/search2/search/search.inc.php'); | |
| 11 | + | |
| 12 | + | |
| 13 | +class SearchDispatcher extends KTStandardDispatcher { | |
| 14 | + | |
| 15 | + private $curUserId; | |
| 16 | + private $sysAdmin; | |
| 17 | + private $savedSearchId; | |
| 18 | + | |
| 19 | + const RESULTS_PER_PAGE = 25; | |
| 20 | + const MAX_PAGE_MOVEMENT = 10; | |
| 21 | + | |
| 22 | + public function __construct() | |
| 23 | + { | |
| 24 | + parent::KTStandardDispatcher(); | |
| 25 | + | |
| 26 | + $this->curUserId = $_SESSION['userID']; | |
| 27 | + | |
| 28 | + $this->sysAdmin=Permission::userIsSystemAdministrator(); | |
| 29 | + | |
| 30 | + if (array_key_exists('fSavedSearchId',$_GET)) | |
| 31 | + { | |
| 32 | + $this->savedSearchId = sanitizeForSQL($_GET['fSavedSearchId']); | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + function do_main() | |
| 37 | + { | |
| 38 | + redirect(KTBrowseUtil::getBrowseBaseUrl()); | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * This proceses any given search expression. | |
| 43 | + * On success, it redirects to the searchResults page. | |
| 44 | + * | |
| 45 | + * @param string $query | |
| 46 | + */ | |
| 47 | + private function processQuery($query) | |
| 48 | + { | |
| 49 | + try | |
| 50 | + { | |
| 51 | + $expr = parseExpression($query); | |
| 52 | + | |
| 53 | + $result = $expr->evaluate(); | |
| 54 | + usort($result, 'rank_compare'); | |
| 55 | + | |
| 56 | + $_SESSION['search2_results'] = serialize($result); | |
| 57 | + $_SESSION['search2_query'] = $query; | |
| 58 | + $_SESSION['search2_sort'] = 'rank'; | |
| 59 | + | |
| 60 | + $this->redirectTo('searchResults'); | |
| 61 | + } | |
| 62 | + catch(Exception $e) | |
| 63 | + { | |
| 64 | + $this->errorRedirectTo('guiBuilder', _kt('Could not process query.' . $e->getMessage())); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Processes a query sent by HTTP POST in searchQuery. | |
| 70 | + * | |
| 71 | + */ | |
| 72 | + function do_process() | |
| 73 | + { | |
| 74 | + if (empty($_REQUEST['txtQuery'])) | |
| 75 | + { | |
| 76 | + $this->errorRedirectTo('searchResults', _kt('Please reattempt the query. The query is missing.')); | |
| 77 | + } | |
| 78 | + $query = $_REQUEST['txtQuery']; | |
| 79 | + | |
| 80 | + session_unregister('search2_savedid'); | |
| 81 | + | |
| 82 | + $this->processQuery($query); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Returns the saved query is resolved from HTTP GET fSavedSearchId field. | |
| 87 | + * | |
| 88 | + * @return mixed False if error, else string. | |
| 89 | + */ | |
| 90 | + private function getSavedExpression() | |
| 91 | + { | |
| 92 | + if (is_null($this->savedSearchId)) | |
| 93 | + { | |
| 94 | + $this->errorRedirectToParent(_kt('The saved search id was not passed correctly.')); | |
| 95 | + } | |
| 96 | + $_SESSION['search2_savedid'] = $this->savedSearchId; | |
| 97 | + | |
| 98 | + $sql = "SELECT name, expression FROM search_saved WHERE type='S' AND id=$this->savedSearchId"; | |
| 99 | + if (!$this->sysAdmin) | |
| 100 | + { | |
| 101 | + $sql .= " and ( user_id=$this->curUserId OR shared=1 ) "; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $query = DBUtil::getOneResult($sql); | |
| 105 | + if (PEAR::isError($query)) | |
| 106 | + { | |
| 107 | + $this->errorRedirectToParent(_kt('The saved search could not be resolved.')); | |
| 108 | + } | |
| 109 | + | |
| 110 | + $_SESSION['search2_savedname'] = $query['name']; | |
| 111 | + return array($query['name'],$query['expression']); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Processes a saved query HTTP GET fSavedSearchId | |
| 116 | + * | |
| 117 | + */ | |
| 118 | + function do_processSaved() | |
| 119 | + { | |
| 120 | + list($name, $expr) = $this->getSavedExpression(); | |
| 121 | + | |
| 122 | + $this->processQuery($expr); | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Renders the search results. | |
| 127 | + * | |
| 128 | + * @return string | |
| 129 | + */ | |
| 130 | + function do_searchResults() | |
| 131 | + { | |
| 132 | + $this->oPage->setBreadcrumbDetails(_kt("Search Results")); | |
| 133 | + $this->oPage->title = _kt("Search Results"); | |
| 134 | + | |
| 135 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 136 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/search_results"); | |
| 137 | + | |
| 138 | + $results = unserialize($_SESSION['search2_results']); | |
| 139 | + | |
| 140 | + if (!is_array($results) || count($results) == 0) | |
| 141 | + { | |
| 142 | + $results=array(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + $numRecs = count($results); | |
| 146 | + $showall = $_GET['showAll']; | |
| 147 | + if (is_numeric($showall)) | |
| 148 | + { | |
| 149 | + $showall = ($showall+0) > 0; | |
| 150 | + } | |
| 151 | + else | |
| 152 | + { | |
| 153 | + $showall = ($showall == 'true'); | |
| 154 | + } | |
| 155 | + $config = KTConfig::getSingleton(); | |
| 156 | + $resultsPerPage = ($showall)?$numRecs:$config->get('search/resultsPerPage', SearchDispatcher::RESULTS_PER_PAGE); | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + $maxPageMove = SearchDispatcher::MAX_PAGE_MOVEMENT; | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + $pageOffset = 1; | |
| 165 | + if (isset($_GET['pageOffset'])) | |
| 166 | + { | |
| 167 | + $pageOffset = $_GET['pageOffset']; | |
| 168 | + } | |
| 169 | + | |
| 170 | + | |
| 171 | + $maxPages = ceil($numRecs / $resultsPerPage) ; | |
| 172 | + if ($pageOffset <= 0 || $pageOffset > $maxPages) | |
| 173 | + { | |
| 174 | + $pageOffset = 1; | |
| 175 | + } | |
| 176 | + | |
| 177 | + | |
| 178 | + $firstRec = ($pageOffset-1) * $resultsPerPage; | |
| 179 | + $lastRec = $firstRec + $resultsPerPage; | |
| 180 | + if ($lastRec > $numRecs) | |
| 181 | + { | |
| 182 | + $lastRec = $numRecs; | |
| 183 | + } | |
| 184 | + | |
| 185 | + $display = array_slice($results,$firstRec ,$resultsPerPage); | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + $startOffset = $pageOffset - $maxPageMove; | |
| 190 | + if ($startOffset < 1) | |
| 191 | + { | |
| 192 | + $startOffset = 1; | |
| 193 | + } | |
| 194 | + $endOffset = $pageOffset + $maxPageMove; | |
| 195 | + if ($endOffset > $maxPages) | |
| 196 | + { | |
| 197 | + $endOffset = $maxPages; | |
| 198 | + } | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + $pageMovement = array(); | |
| 203 | + for($i=$startOffset;$i<=$endOffset;$i++) | |
| 204 | + { | |
| 205 | + $pageMovement[] = $i; | |
| 206 | + } | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + $aBulkActions = KTBulkActionUtil::getAllBulkActions(); | |
| 212 | + | |
| 213 | + $aTemplateData = array( | |
| 214 | + "context" => $this, | |
| 215 | + 'bulkactions'=>$aBulkActions, | |
| 216 | + 'firstRec'=>$firstRec, | |
| 217 | + 'lastRec'=>$lastRec, | |
| 218 | + 'showAll'=>$showall, | |
| 219 | + 'numResults' => count($results), | |
| 220 | + 'pageOffset' => $pageOffset, | |
| 221 | + 'resultsPerPage'=>$resultsPerPage, | |
| 222 | + 'maxPages' => $maxPages, | |
| 223 | + 'results' => $display, | |
| 224 | + 'pageMovement'=>$pageMovement, | |
| 225 | + 'startMovement'=>$startOffset, | |
| 226 | + 'endMovement'=>$endOffset, | |
| 227 | + 'txtQuery' => $_SESSION['search2_query'], | |
| 228 | + 'iSavedID' => $_SESSION['search2_savedid'], | |
| 229 | + 'txtSavedName' => $_SESSION['search2_savedname'] | |
| 230 | + ); | |
| 231 | + | |
| 232 | + return $oTemplate->render($aTemplateData); | |
| 233 | + } | |
| 234 | + | |
| 235 | + function do_manage() | |
| 236 | + { | |
| 237 | + $this->oPage->setBreadcrumbDetails(_kt("Manage Saved Searches")); | |
| 238 | + $this->oPage->title = _kt("Manage Saved Searches"); | |
| 239 | + | |
| 240 | + $sql = "SELECT ss.id, ss.name, u.name as username, user_id is not null as editable, shared | |
| 241 | + FROM search_saved ss | |
| 242 | + LEFT OUTER JOIN users u on ss.user_id = u.id | |
| 243 | + WHERE ss.type='S' "; | |
| 244 | + | |
| 245 | + if (!$this->sysAdmin) | |
| 246 | + { | |
| 247 | + $sql .= " AND (ss.user_id=$this->curUserId OR ss.shared=1)"; | |
| 248 | + } | |
| 249 | + | |
| 250 | + $saved = DBUtil::getResultArray($sql); | |
| 251 | + | |
| 252 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 253 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/manage_saved_search"); | |
| 254 | + $aTemplateData = array( | |
| 255 | + "context" => $this, | |
| 256 | + 'saved'=>$saved, | |
| 257 | + 'sysadmin'=>$this->sysAdmin | |
| 258 | + ); | |
| 259 | + | |
| 260 | + return $oTemplate->render($aTemplateData); | |
| 261 | + } | |
| 262 | + | |
| 263 | + function do_share() | |
| 264 | + { | |
| 265 | + if (is_null($this->savedSearchId)) | |
| 266 | + { | |
| 267 | + $this->errorRedirectTo('manage', _kt('The saved search id was not passed correctly.')); | |
| 268 | + } | |
| 269 | + if (!array_key_exists('share',$_GET)) | |
| 270 | + { | |
| 271 | + $this->errorRedirectTo('manage', _kt('The sharing option was not passed correctly.')); | |
| 272 | + } | |
| 273 | + | |
| 274 | + if ($_GET['share']=='no') | |
| 275 | + { | |
| 276 | + $share=0; | |
| 277 | + $msg = _kt("The saved search can only be seen by you."); | |
| 278 | + } | |
| 279 | + else | |
| 280 | + { | |
| 281 | + $share=1; | |
| 282 | + $msg = _kt("The saved search is now visible to all users."); | |
| 283 | + } | |
| 284 | + | |
| 285 | + | |
| 286 | + $sql = "UPDATE search_saved SET shared=$share WHERE type='S' AND id=$this->savedSearchId"; | |
| 287 | + if (!$this->sysAdmin) | |
| 288 | + { | |
| 289 | + $sql .= " AND ss.user_id=$this->curUserId"; | |
| 290 | + } | |
| 291 | + | |
| 292 | + DBUtil::runQuery($sql); | |
| 293 | + $this->successRedirectTo('manage', $msg); | |
| 294 | + | |
| 295 | + } | |
| 296 | + | |
| 297 | + function do_delete() | |
| 298 | + { | |
| 299 | + if (is_null($this->savedSearchId)) | |
| 300 | + { | |
| 301 | + $this->errorRedirectTo('manage', _kt('The saved search id was not passed correctly.')); | |
| 302 | + } | |
| 303 | + | |
| 304 | + $sql = "DELETE FROM search_saved WHERE type='S' AND id=$this->savedSearchId"; | |
| 305 | + if (!$this->sysAdmin) | |
| 306 | + { | |
| 307 | + $sql .= " AND user_id=$this->curUserId "; | |
| 308 | + } | |
| 309 | + | |
| 310 | + | |
| 311 | + DBUtil::runQuery($sql); | |
| 312 | + $this->successRedirectTo('manage', _kt('The saved search was deleted successfully.')); | |
| 313 | + | |
| 314 | + } | |
| 315 | + | |
| 316 | + function do_guiBuilder() | |
| 317 | + { | |
| 318 | + $this->oPage->setBreadcrumbDetails(_kt("Query Builder")); | |
| 319 | + $this->oPage->title = _kt("Query Builder"); | |
| 320 | + | |
| 321 | + $result = array(); | |
| 322 | + | |
| 323 | + // TODO: need to escape the parameters | |
| 324 | + | |
| 325 | + $result['fieldsets'] = SearchHelper::getFieldsets(); | |
| 326 | + $result['fieldset_str'] = SearchHelper::getJSfieldsetStruct($result['fieldsets']); | |
| 327 | + | |
| 328 | + $result['workflows'] = SearchHelper::getWorkflows(); | |
| 329 | + $result['workflow_str'] = SearchHelper::getJSworkflowStruct($result['workflows']); | |
| 330 | + | |
| 331 | + $result['fields'] = SearchHelper::getSearchFields(); | |
| 332 | + $result['fields_str'] = SearchHelper::getJSfieldsStruct($result['fields']); | |
| 333 | + | |
| 334 | + $result['users_str'] = SearchHelper::getJSusersStruct(); | |
| 335 | + $result['mimetypes_str'] = SearchHelper::getJSmimeTypesStruct(); | |
| 336 | + $result['documenttypes_str'] = SearchHelper::getJSdocumentTypesStruct(); | |
| 337 | + | |
| 338 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 339 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/adv_query_builder"); | |
| 340 | + $aTemplateData = array( | |
| 341 | + "context" => $this, | |
| 342 | + 'metainfo'=> $result | |
| 343 | + ); | |
| 344 | + | |
| 345 | + return $oTemplate->render($aTemplateData); | |
| 346 | + } | |
| 347 | + | |
| 348 | + function do_queryBuilder() | |
| 349 | + { | |
| 350 | + $this->oPage->setBreadcrumbDetails(_kt("Advanced Query Builder")); | |
| 351 | + $this->oPage->title = _kt("Advanced Query Builder"); | |
| 352 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 353 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/adv_query_search"); | |
| 354 | + | |
| 355 | + | |
| 356 | + $registry = ExprFieldRegistry::getRegistry(); | |
| 357 | + $aliases = $registry->getAliasNames(); | |
| 358 | + sort($aliases); | |
| 359 | + | |
| 360 | + $edit = is_numeric($this->savedSearchId); | |
| 361 | + $name = ''; | |
| 362 | + if ($edit) | |
| 363 | + { | |
| 364 | + list($name, $expr) = $this->getSavedExpression(); | |
| 365 | + } | |
| 366 | + else | |
| 367 | + { | |
| 368 | + $expr = $_SESSION['search2_query']; | |
| 369 | + } | |
| 370 | + | |
| 371 | + $aTemplateData = array( | |
| 372 | + "context" => $this, | |
| 373 | + 'aliases' => $aliases, | |
| 374 | + 'bSave'=>$edit, | |
| 375 | + 'edtSaveQueryName'=>$name, | |
| 376 | + 'txtQuery'=>$expr, | |
| 377 | + 'iSavedSearchId'=>$this->savedSearchId | |
| 378 | + | |
| 379 | + ); | |
| 380 | + return $oTemplate->render($aTemplateData); | |
| 381 | + } | |
| 382 | + | |
| 383 | + function do_metadata() | |
| 384 | + { | |
| 385 | + $this->oPage->setBreadcrumbDetails(_kt("Metadata Query Builder")); | |
| 386 | + $this->oPage->title = _kt("Metadata Query Builder"); | |
| 387 | + | |
| 388 | + $result = array(); | |
| 389 | + | |
| 390 | + $result['fieldsets'] = SearchHelper::getFieldsets(); | |
| 391 | + $result['fieldset_str'] = SearchHelper::getJSfieldsetStruct($result['fieldsets']); | |
| 392 | + | |
| 393 | + $result['users_str'] = SearchHelper::getJSusersStruct(); | |
| 394 | + | |
| 395 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 396 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/metadata_search"); | |
| 397 | + $aTemplateData = array( | |
| 398 | + "context" => $this, | |
| 399 | + 'metainfo'=> $result | |
| 400 | + ); | |
| 401 | + return $oTemplate->render($aTemplateData); | |
| 402 | + } | |
| 403 | + | |
| 404 | + function do_tree() | |
| 405 | + { | |
| 406 | + $this->oPage->setBreadcrumbDetails(_kt("Tree Browser")); | |
| 407 | + $this->oPage->title = _kt("Tree Browser"); | |
| 408 | + | |
| 409 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 410 | + $oTemplate = $oTemplating->loadTemplate("ktcore/search2/tree"); | |
| 411 | + $aTemplateData = array( | |
| 412 | + "context" => $this | |
| 413 | + ); | |
| 414 | + return $oTemplate->render($aTemplateData); | |
| 415 | + } | |
| 416 | +} | |
| 417 | + | |
| 418 | + | |
| 419 | +$oDispatcher = new SearchDispatcher(); | |
| 420 | +$oDispatcher->dispatch(); | |
| 421 | + | |
| 422 | +?> | |
| 0 | 423 | \ No newline at end of file | ... | ... |