From 47e20abac975dfcb787251a6eb720e504989899b Mon Sep 17 00:00:00 2001
From: Bryn Divey
Date: Thu, 25 May 2006 13:20:15 +0000
Subject: [PATCH] Per-User saved searches. Please test.
---
lib/search/savedsearch.inc.php | 9 +++++++++
plugins/ktcore/KTPortlets.php | 13 ++++++++++++-
plugins/ktcore/admin/savedSearch.php | 12 +++++++++++-
search/booleanSearch.php | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
templates/kt3/browse.smarty | 35 ++++++++++++++++++++++++++++++++++-
templates/kt3/portlets/search_portlet.smarty | 4 +++-
templates/ktcore/search/administration/savedsearches.smarty | 3 +++
7 files changed, 217 insertions(+), 5 deletions(-)
diff --git a/lib/search/savedsearch.inc.php b/lib/search/savedsearch.inc.php
index d4029ac..21611c1 100644
--- a/lib/search/savedsearch.inc.php
+++ b/lib/search/savedsearch.inc.php
@@ -146,6 +146,15 @@ class KTSavedSearch extends KTEntity {
));
}
+ function &getUserSearches($iUserId, $bNoSystem = false) {
+ $sQuery = sprintf('user_id = %d', $iUserId);
+ if(!$bNoSystem) {
+ $sQuery .= ' OR user_id IS NULL';
+ }
+
+ return KTEntityUtil::getList2('KTSavedSearch', $sQuery, array('orderby' => 'user_id, name'));
+ }
+
function &getConditions() {
return KTEntityUtil::getByDict('KTSavedSearch', array(
'is_condition' => true,
diff --git a/plugins/ktcore/KTPortlets.php b/plugins/ktcore/KTPortlets.php
index 6678956..548319d 100644
--- a/plugins/ktcore/KTPortlets.php
+++ b/plugins/ktcore/KTPortlets.php
@@ -40,15 +40,26 @@ class KTSearchPortlet extends KTPortlet {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate("kt3/portlets/search_portlet");
- $aSearches = KTSavedSearch::getSearches();
+ $iFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', 1);
+ $iDocumentId = KTUtil::arrayGet($_REQUEST, 'fDocumentId');
+ if (!$iFolderId && !$iDocumentId) {
+ return null;
+ }
+
+ $iUserId = $_SESSION['userID'];
+ $aSearches = KTSavedSearch::getUserSearches($iUserId);
+
// empty on error.
if (PEAR::isError($aSearches)) {
$aSearches = array();
}
+ $iFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', 1);
$aTemplateData = array(
"context" => $this,
"saved_searches" => $aSearches,
+ "folder_id" => $iFolderId,
+ "document_id" => $iDocumentId,
);
return $oTemplate->render($aTemplateData);
diff --git a/plugins/ktcore/admin/savedSearch.php b/plugins/ktcore/admin/savedSearch.php
index 52d2d6e..80b3439 100755
--- a/plugins/ktcore/admin/savedSearch.php
+++ b/plugins/ktcore/admin/savedSearch.php
@@ -149,7 +149,7 @@ class KTSavedSearchDispatcher extends KTAdminDispatcher {
$oSearch = KTSavedSearch::get($id);
if (PEAR::isError($oSearch) || ($oSearch == false)) {
- $this->errorRedirectToMain('No Such search');
+ $this->errorRedirectToMain('No such search');
}
@@ -212,6 +212,16 @@ class KTSavedSearchDispatcher extends KTAdminDispatcher {
));
$this->successRedirectToMain(_kt('Search saved'));
}
+
+ // helper for the template
+ function _getUserName($iUserId) {
+ $oUser = User::get($iUserId);
+ if(PEAR::isError($oUser)) {
+ return _kt('Error retrieving username');
+ }
+ return $oUser->getUserName();
+ }
+
}
//$oDispatcher = new KTSavedSearchDispatcher();
diff --git a/search/booleanSearch.php b/search/booleanSearch.php
index 60a26d4..7c397d3 100755
--- a/search/booleanSearch.php
+++ b/search/booleanSearch.php
@@ -84,7 +84,7 @@ class BooleanSearchDispatcher extends KTStandardDispatcher {
}
if (is_null(KTUtil::arrayGet($datavars["subgroup"][0], "values"))) {
- $this->errorRedirectToMain("No search parameters given");
+ $this->errorRedirectToMain(_kt("No search parameters given"));
}
if (empty($datavars)) {
@@ -96,6 +96,132 @@ class BooleanSearchDispatcher extends KTStandardDispatcher {
return $res;
}
+ function do_saveSearch() {
+ $this->startTransaction();
+
+ $iSearchId = KTUtil::arrayGet($_REQUEST, 'fSearchId', false);
+ $sName = KTUtil::arrayGet($_REQUEST, 'name', false);
+ $sSearch = KTUtil::arrayGet($_REQUEST, 'boolean_search');
+
+ if($iSearchId === false && $sName === false) {
+ $this->errorRedirectTo('performSearch', _kt('Please either enter a name, or select a search to save over'), sprintf('boolean_search_id=%s', $sSearch));
+ exit(0);
+ }
+
+ $datavars = $_SESSION['boolean_search'][$sSearch];
+ if (!is_array($datavars)) {
+ $datavars = unserialize($datavars);
+ }
+
+ if (empty($datavars)) {
+ $this->errorRedirectToMain(_kt('You need to have at least 1 condition.'));
+ }
+
+ if($iSearchId) {
+ $oSearch = KTSavedSearch::get($iSearchId);
+ if(PEAR::isError($oSearch) || $oSearch == false) {
+ $this->errorRedirectToMain(_kt('No such search'));
+ exit(0);
+ }
+ $oSearch->setSearch($datavars);
+ $oSearch = $oSearch->update();
+
+ } else {
+ $sName = $this->oValidator->validateEntityName('KTSavedSearch',
+ KTUtil::arrayGet($_REQUEST, 'name'),
+ array('extra_condition' => 'not is_condition', 'redirect_to' => array('new')));
+
+ $sNamespace = KTUtil::nameToLocalNamespace('Saved searches', $sName);
+
+ $oSearch = KTSavedSearch::createFromArray(array('name' => $sName,
+ 'namespace' => $sNamespace,
+ 'iscondition' => false,
+ 'iscomplete' => true,
+ 'userid' => $this->oUser->getId(),
+ 'search' => $datavars,));
+ }
+
+ $this->oValidator->notError($oSearch, array(
+ 'redirect_to' => 'main',
+ 'message' => _kt('Search not saved'),
+ ));
+
+ $this->commitTransaction();
+ $this->successRedirectTo('performSearch', _kt('Search saved'), sprintf('boolean_search_id=%s', $sSearch));
+ }
+
+
+ function do_deleteSearch() {
+ $this->startTransaction();
+
+ $iSearchId = KTUtil::arrayGet($_REQUEST, 'fSavedSearchId', false);
+ $oSearch = KTSavedSearch::get($iSearchId);
+ if(PEAR::isError($oSearch) || $oSearch == false) {
+ $this->errorRedirectToMain(_kt('No such search'));
+ exit(0);
+ }
+
+ $res = $oSearch->delete();
+ $this->oValidator->notError($res, array(
+ 'redirect_to' => 'main',
+ 'message' => _kt('Error deleting search'),
+ ));
+
+ $this->commitTransaction();
+
+ $iFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', false);
+ $iDocumentId = KTUtil::arrayGet($_REQUEST, 'fFolderId', false);
+
+ if($iFolderId) {
+ controllerRedirect('browse', 'fFolderId=' . $iFolderId);
+ } else {
+ controllerRedirect('viewDocument', 'fDocumentId=' . $iDocumentId);
+ }
+ }
+
+ function do_editSearch() {
+ $sSearch = KTUtil::arrayGet($_REQUEST, 'boolean_search');
+ $aSearch = $_SESSION['boolean_search'][$sSearch];
+ if (!is_array($aSearch)) {
+ $aSearch = unserialize($aSearch);
+ }
+
+ if (empty($aSearch)) {
+ $this->errorRedirectToMain(_kt('You need to have at least 1 condition.'));
+ }
+
+ $oTemplating =& KTTemplating::getSingleton();
+ $oTemplate = $oTemplating->loadTemplate("ktcore/boolean_search_change");
+
+ $aCriteria = Criteria::getAllCriteria();
+
+ // we need to help out here, since it gets unpleasant inside the template.
+
+ foreach ($aSearch['subgroup'] as $isg => $as) {
+ $aSubgroup =& $aSearch['subgroup'][$isg];
+ if (is_array($aSubgroup['values'])) {
+ foreach ($aSubgroup['values'] as $iv => $t) {
+ $datavars =& $aSubgroup['values'][$iv];
+ $datavars['typename'] = $aCriteria[$datavars['type']]->sDisplay;
+ $datavars['widgetval'] = $aCriteria[$datavars['type']]->searchWidget(null, $datavars['data']);
+ }
+ }
+ }
+
+ $aTemplateData = array(
+ "title" => _kt("Edit an existing condition"),
+ "aCriteria" => $aCriteria,
+ "searchButton" => _kt("Search"),
+ 'aSearch' => $aSearch,
+ 'context' => $this,
+ // 'iSearchId' => $oSearch->getId(),
+ // 'old_name' => $oSearch->getName(),
+ 'sNameTitle' => _kt('Edit Search'),
+ );
+ return $oTemplate->render($aTemplateData);
+ }
+
+
function handleCriteriaSet($aCriteriaSet, $iStartIndex, $sTitle=null) {
if ($sTitle == null) {
@@ -142,6 +268,22 @@ class BooleanSearchDispatcher extends KTStandardDispatcher {
$qObj = new BooleanSearchQuery($aCriteriaSet);
$collection->setQueryObject($qObj);
+
+ // form fields for saving the search
+ $save_fields = array();
+ $save_fields[] = new KTStringWidget(_kt('New search'), _kt('The name to save this search as'), 'name', null, $this->oPage, true);
+
+ $aUserSearches = KTSavedSearch::getUserSearches($this->oUser->getId(), true);
+ if(count($aUserSearches)) {
+ $aVocab = array('' => ' ---- ');
+ foreach($aUserSearches as $oSearch) {
+ $aVocab[$oSearch->getId()] = $oSearch->getName();
+ }
+
+ $aSelectOptions = array('vocab' => $aVocab);
+ $save_fields[] = new KTLookupWidget(_kt('Existing search'), _kt('To save over one of your existing searches, select it here.'), 'fSearchId', null, $this->oPage, true, null, null, $aSelectOptions);
+ }
+
$collection->getResults();
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate("kt3/browse");
@@ -149,6 +291,8 @@ class BooleanSearchDispatcher extends KTStandardDispatcher {
"context" => $this,
"collection" => $collection,
"custom_title" => $sTitle,
+ "save_fields" => $save_fields,
+ "boolean_search" => $sSearch,
);
return $oTemplate->render($aTemplateData);
}
diff --git a/templates/kt3/browse.smarty b/templates/kt3/browse.smarty
index 7e723e0..e101038 100644
--- a/templates/kt3/browse.smarty
+++ b/templates/kt3/browse.smarty
@@ -8,7 +8,8 @@
{$custom_title}
{/if}
-
+
+
+
+{if ($save_fields)}
+
+
+
+{/if}
+
{* we break encapsulation pretty badly here. *}
\ No newline at end of file
diff --git a/templates/kt3/portlets/search_portlet.smarty b/templates/kt3/portlets/search_portlet.smarty
index 4a484b1..0810098 100644
--- a/templates/kt3/portlets/search_portlet.smarty
+++ b/templates/kt3/portlets/search_portlet.smarty
@@ -8,7 +8,9 @@
{i18n}Saved Searches{/i18n}
diff --git a/templates/ktcore/search/administration/savedsearches.smarty b/templates/ktcore/search/administration/savedsearches.smarty
index e42d239..36bb17c 100644
--- a/templates/ktcore/search/administration/savedsearches.smarty
+++ b/templates/ktcore/search/administration/savedsearches.smarty
@@ -22,6 +22,7 @@ newsletters, etc.) based on a category or fieldset value.{/i18n}
| {i18n}Search Name{/i18n} |
+ {i18n}User{/i18n} |
{i18n}Edit{/i18n} |
{i18n}Delete{/i18n} |
{i18n}View Results{/i18n} |
@@ -31,6 +32,8 @@ newsletters, etc.) based on a category or fieldset value.{/i18n}
{foreach item=oSearch from=$saved_searches}
| {$oSearch->getName()} |
+ {capture assign=iUserId}{$oSearch->getUserId()}{/capture}
+ {if ($iUserId === '')}Global{else}{$context->_getUserName($iUserId)}{/if} |
{i18n}Edit{/i18n} |
{i18n}Delete{/i18n} |
getId()}">Run Search |
--
libgit2 0.21.4