Commit [r594] Maximize Restore History

- added new class KoraSearch in model/search.php and javascripts/search.php

- added new ajax for that class in ajax/search.php
- rewrote crossProjectSearchResults.php and searchProjectResults.php as a single, merged operation at searchResults.php
- removed several 'utilty' functions placing them as static (or non-static) members of related classes where appropriate

sucko 2013-12-17

added /branches/2.5.0/model/search.php
added /branches/2.5.0/ajax/search.php
changed /branches/2.5.0/crossProjectSearch.php
changed /branches/2.5.0/model/manager.php
changed /branches/2.5.0/includes/includes.php
changed /branches/2.5.0/searchProject.php
changed /branches/2.5.0/ajax/record.php
changed /branches/2.5.0/includes/utilities.php
copied /branches/2.5.0/crossProjectSearchResults.php -> /branches/2.5.0/searchResults.php
copied /branches/2.5.0/searchProjectResults.php -> /branches/2.5.0/javascripts/search.js
/branches/2.5.0/model/search.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/ajax/search.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/crossProjectSearch.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/model/manager.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/includes/includes.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/searchProject.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/ajax/record.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/includes/utilities.php Diff Switch to side-by-side view
Loading...
/branches/2.5.0/crossProjectSearchResults.php to /branches/2.5.0/searchResults.php
--- a/branches/2.5.0/crossProjectSearchResults.php
+++ b/branches/2.5.0/searchResults.php
@@ -26,75 +26,71 @@
 
 Manager::RequireLogin();
 
-if (!isset($_REQUEST['keywords']) || !isset($_REQUEST['projects']))
-{
-	header('Location: crossProjectSearch.php');
-}
-
 Manager::PrintHeader();
 
 echo '<h2>'.gettext('Search Results').'</h2>';
 
-if (empty($_REQUEST['keywords']))
+// SEVERAL SCENARIOS HERE
+// SCENARIO 1) CROSS-PROJECT-SEARCH, SHOULD PASS IN Projects[] ARRAY, KEYWORD REQUIRED, IGNORE SID ARRAY
+// SCENARIO 2) SINGLE-PROJECT SEARCH, MAY PASS IN Schemes[] ARRAY, KEYWORD REQUIRED, PID IS ASSUMED TO BE Manager::GetPID()
+// SCENARIO 3) LIST ALL OBJECTS FROM SPECIFIC PROJECT, KEYWORD IS EMPTY BUT PASSED IN
+
+$crossproject = false;
+$searchok = true;
+
+$pids = null;
+if (isset($_REQUEST['projects'])) 
+{ $pids = (is_array($_REQUEST['projects'])) ? $_REQUEST['projects'] : array($_REQUEST['projects']); $crossproject = true; }
+elseif (Manager::GetProject()) 
+{ $pids = array(Manager::GetProject()->GetPID()); }
+
+$sids = null;
+if (!$crossproject)
 {
-	echo gettext('Please enter at least one keyword').'.';
+	if (isset($_REQUEST['sids'])) 
+	{ $sids = (is_array($_REQUEST['sids'])) ? $_REQUEST['sids'] : array($_REQUEST['sids']); }
+	elseif (Manager::GetScheme()) 
+	{ $sids = array(Manager::GetScheme()->GetSID()); }
 }
-else if (empty($_REQUEST['projects']))
+else { $sids = ''; }
+
+$keywords = isset($_REQUEST['keywords']) ? $_REQUEST['keywords'] : '';
+$boolean = (isset($_REQUEST['boolean']) && in_array($_REQUEST['boolean'], array('AND', 'OR'))) ? $_REQUEST['boolean'] : 'AND';
+
+// IF EITHER pids OR sids > SIZE 1, THEN WE ARE DOING A CROSS-PROJECT, OR CROSS-SCHEME SEARCH AND SHOULD HAVE KEYWORD
+if (((sizeof($pids) > 1) || (is_array($sids) && sizeof($sids) > 1) || empty($sids)) && empty($keywords))
+{ Manager::PrintErrDiv(gettext('Please enter at least one keyword').'.'); $searchok = false; }
+
+if (sizeof($pids) <= 0)
+{ Manager::PrintErrDiv(gettext('Please select at least one project to search').'.'); $searchok = false; }
+
+// Verify that the user has permission to search through all the requested projects
+if (!Manager::IsSystemAdmin())
 {
-	echo gettext('Please select at least one project to search').'.';
-}
-else
-{
-	if (!is_array($_REQUEST['projects'])) $_REQUEST['projects'] = array($_REQUEST['projects']);
-	
-	// Verify that the user has permission to search through all the requested projects
-	$hasPermission = true;
-	if (!Manager::IsSystemAdmin())
+	foreach($pids AS $pid)
 	{
-		foreach($_REQUEST['projects'] AS $project)
+		if (Project($pid)->GetUserPermissions() <= 0) 
 		{
-			$permQuery = $db->query('SELECT pid FROM member WHERE uid='.Manager::GetUser()->GetUID().' AND pid='.escape($project));
-			if ($permQuery->num_rows == 0)
-			{
-				$hasPermission = false;
-			}
+			Manager::PrintErrDiv(gettext('Sorry, but you do not have permission to search some of the requested projects').'.');
+			$searchok = false;
+			break;
 		}
 	}
-	
-	if ($hasPermission)
+}
+
+if ($searchok)
+{
+	$results = array();
+	$resultsmerged = array();
+
+	foreach($pids as $currpid)
 	{
-		if (isset($_REQUEST['boolean']) && in_array($_REQUEST['boolean'], array('AND', 'OR')))
-		{
-			$boolean = $_REQUEST['boolean'];
-		}
-		else
-		{
-			$boolean = 'AND';
-		}
+		$results[$currpid] = KoraSearch::SortedInternalSearchResults($currpid,'',$keywords,$boolean,'id','ASC',false);
+		$resultsmerged = array_merge($resultsmerged, $results[$currpid]);
+	}
+	$_SESSION['results'] = $results;
 
-		// The search link involves ALL projects, so we need to build the search link
-		// outside the foreach loop
-		
-		$searchLink = 'href="'.$_SERVER['PHP_SELF'].'?';
-		foreach($_REQUEST['projects'] AS $project)
-		{
-			$searchLink .= 'projects[]='.urlencode($project).'&amp;';		
-		}
-		$searchLink .= 'keywords='.urlencode($_REQUEST['keywords']);
-		$searchLink .= '&amp;boolean='.urlencode($boolean);
-		$searchLink .= '&amp;page=%d"';
-		
-		$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
-		
-		// See if "View All" has been clicked; if so up the number of results per page
-		$resultsPerPage = (Manager::IsProjectAdmin() && isset($_REQUEST['viewall']) && ((int)$_REQUEST['viewall'] == 1)) ? RESULTS_IN_VIEWALL_PAGE : RESULTS_IN_PAGE;
-		
-		echo internalSearchResults($_REQUEST['projects'], '', $_REQUEST['keywords'], $boolean, $page,  $resultsPerPage, $searchLink, true, true);
-	}
-	else
-	{
-		Manager::PrintErrDiv(gettext('Sorry, but you do not have permission to search some of the requested projects').'.');
-	}
+	KoraSearch::PrintSearchResultsAJAXLoad($resultsmerged);
 }
 
 Manager::PrintFooter();
/branches/2.5.0/searchProjectResults.php to /branches/2.5.0/javascripts/search.js
--- a/branches/2.5.0/searchProjectResults.php
+++ b/branches/2.5.0/javascripts/search.js
@@ -1,101 +1,89 @@
-<?php
-session_start();
-/**
-Copyright (2008) Matrix: Michigan State University
-
-This file is part of KORA.
-
-KORA is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-KORA is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * searchProject
- *
- * GET/POST arguments:
- *   pid: project id                (REQUIRED)
- *   sid: scheme  id                (optional)
- *   keywords: list of search terms (optional)
- *   boolean: 'AND' or 'OR'         (required if keywords present)
- *
- * if keyword not specified, pulls all objects from project/scheme
- * if scheme not specified, searches entire project
- */
-
-// Initial Version: Brian Beck, 2008
-// Refactor: Joe Deming, 2013
-require_once('includes/includes.php');
-
-Manager::Init();
-
-Manager::RequireLogin();
-Manager::RequireProject();
-// APPARENTLY THIS PAGE CAN BE CALLED AGAINST MULTIPLE SCHEMES?
-//Manager::RequireScheme();
-
-// check once for ability to edit scheme layout to prevent repeated database calls
-$ePerm = Manager::GetUser()->HasProjectPermissions(EDIT_LAYOUT);
-
-Manager::PrintHeader();
-
-$pid = Manager::GetProject()->GetPID();
-$sid = (Manager::GetScheme()) ? Manager::GetScheme()->GetSID() : '';
-$keywords = isset($_REQUEST['keywords']) ? $_REQUEST['keywords'] : '';
-$boolean = isset($_REQUEST['boolean']) ? $_REQUEST['boolean'] : 'AND';
-$pageNum = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
-$searchLink = 'href="'.$_SERVER['PHP_SELF'].'?pid='.urlencode($_REQUEST['pid']);
-if (!empty($sid)) $searchLink .= '&amp;sid='.urlencode($sid);
-if (!empty($boolean)) $searchLink .= '&amp;boolean='.urlencode($boolean);
-$searchLink .= '&amp;keywords='.urlencode($keywords);
-if (Manager::IsProjectAdmin() && isset($_REQUEST['viewall']) && ((int)$_REQUEST['viewall'] == 1))
-{
-	$searchLink .= '&amp;viewall=1';
-}
-$searchLink .= '&amp;page=%d"';
-$results = array();
-
-if(empty($sid))
-{
-	$allSIDs = array();
-	$sid = '';
-	$schemeList = $db->query('SELECT schemeid, schemeName FROM scheme WHERE pid='.$_SESSION['currentProject']);
-    while ($scheme = $schemeList->fetch_assoc()) {
-		$allSIDs[] = $scheme['schemeid'];
-    }
-}
-else $allSIDs[] = $sid;
-foreach($allSIDs as $currSID)
-{
-    $result =  sortedInternalSearchResults($pid,$currSID,$keywords,$boolean,'id','ASC',false);
-    $IDs = '';
-	foreach($result as $index=>$id)
-	{
-		$IDs.=("'".$id."',");
-	}
-    if(!empty($result))  $results[$currSID] = $IDs;
-}
-$_SESSION['results'] = $results;
-// See if "View All" has been clicked; if so up the number of results per page
-$resultsPerPage = (Manager::IsProjectAdmin() && isset($_REQUEST['viewall']) && ((int)$_REQUEST['viewall'] == 1)) ? RESULTS_IN_VIEWALL_PAGE : RESULTS_IN_PAGE;
-$srchResults = internalSearchResults($pid, $sid, $keywords, $boolean, $pageNum, $resultsPerPage, $searchLink);
-echo '<h2>'.gettext('Search Results').'</h2>';
-if(strlen($srchResults)!=39&&Manager::GetUser()->HasProjectPermissions(EXPORT_SCHEME)){
-?>
-<a href="schemeExport.php?type=data">Export Search Results to XML</a></br></br>
-<?php
-}
-echo internalSearchResults($_REQUEST['pid'], $sid, $keywords, $boolean, $pageNum, $resultsPerPage, $searchLink);
-
-Manager::PrintFooter();
-
-?>
+/**
+Copyright (2008) Matrix: Michigan State University
+
+This file is part of KORA.
+
+KORA is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+KORA is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// THE CURRENT PAGE BEING SHOWN, GLOBAL
+ks_currpage = 0;
+
+$(function() {
+		//************************************************************************
+		//                     SEARCH RESULTS HANDLERS
+		//************************************************************************
+		if ($('.ks_results').length > 0) {
+			// TODO: SET THIS TO A SPECIFICALLY REQUESTED PAGE?  RELATED TO TODO 10 LINES DOWN
+			ks_currpage = 1;
+			
+			// HANDLERS FOR THE BREADCRUMB LINKS
+			$(".ks_results" ).on( "click",'.ks_results_nav', function() {
+				var c = $(this);
+				var tardiv = c.parents('.ks_results').first();
+				var tarpage = parseInt(c.html());
+				KS_ShowPage(tardiv, tarpage);
+			});
+			//$(".ks_results" ).find('.kora_control').each(function() {
+			//		var kcdiv = $(this);
+			//		if ($(this).attr('kctype') == 'Date (Multi-Input)')
+			//		{
+			//		}
+			//});
+			
+			// LOAD THE FIRST PAGE (TODO: OR REQUESTED PAGE?)
+			KS_ShowPage($('.ks_results').first(), ks_currpage);
+		}	
+});
+
+function KS_ShowPage(ksdiv, page)
+{
+	ksdiv.find('.ks_results_page').each(function() {
+		// HIDE ALL PAGES NOT EQUAL TO TARGET PAGE
+		if ($(this).attr('page') != page)
+		{ $(this).hide();	}
+		else
+		{
+			// NOW LOAD IF NECESSARY THE TARGET PAGE RECORDS
+			$(this).find('.ks_result_item').each(function() {
+				if ($(this).attr('loaded') != 'true')
+				{
+					var c = $(this);
+					var pid = $(this).attr('pid');
+					var sid = $(this).attr('sid');
+					var rid = $(this).attr('rid');
+					console.log('Loading: '+$(this).attr('rid'));
+					$.ajaxSetup({ async: false });
+					$.post("ajax/record.php",{action:"viewRecord",source:"RecordFunctions",pid:pid,sid:sid,rid:rid},function(resp){c.html(resp); c.attr('loaded', 'true'); }, 'html');
+					$.ajaxSetup({ async: true });
+				}
+			});
+			
+			// NOW SHOW THE TARGET PAGE
+			$(this).show();
+		}
+		
+		ks_currpage = page;
+	});
+	
+	// NOW HANDLE THE NAV LINKS, THIS COULD BE DONE THROUGH JS, BUT A PHP FUNC EXISTS SO AJAX IT...
+	var maxpage = ksdiv.find('.ks_results_page').size();
+	var adjacentpage = ksdiv.attr('navlinkadj');
+	$.ajaxSetup({ async: false });
+	$.post("ajax/search.php",{action:"GetSearchNavLinks",source:"SearchFunctions",maxpage:maxpage,adjacentpage:adjacentpage,currpage:page},function(resp){ksdiv.find('.ks_results_navlinks').each(function(){$(this).html(resp);});}, 'html');
+	$.ajaxSetup({ async: true });
+}
+
+	
+