|
Athanasios, Thanks for that. I'm only intending to use this on a single group of pages e.g list, view, adv search. How much of this code would I need? Thanks in advance.
Well, here is how I've implemented my save search solution: - I inserted two template contacts in my list view. In order to populate them in all my views I did that in my lheader.htm of the selected templetate. In fact I have implemented a copy of my preferred template where I do my modifications, but that is another topic. So I've inserted a block which I will be using for form POST before {BEGIN left_block}. Also I inserted a placeholder for my dropdown and buttons after the {END pages_block}. Here it is how the lheader.htm should look:
...
{BEGIN savesearchcontents_block}
{BEGIN left_block}
..
...
<div class="menu_text blackshade" id="pages_block{$id}">
{BEGIN pages_block}
<span>##message PAGE1## {$page} ##message PAGE2## {$maxpages}</span>
{END pages_block}
</div>
{BEGIN savesearch_block}
<div class="blackshade2" id="center_block{$id}">
<span>##message SAVEDSEARCH##</span>
</div>
<div class="blackshade">
{$savedsearches}
</div>
{END savesearch_block} ...
...
{END left_block}
{END savesearchcontents_block}
2) I call a php function to do all the work, so I have implemented my own file which I include. In every list's BeforeProcessList event I call . I need to pass on the $xt in order to have the function modify the template, and I also pass on the listname ('mylist') for every list.
include_once('include/QUIGeneric.php'); global $xt; //Saved Searches
QUISavedSearches($xt, 'mylist');
3) In the QUISavedSearches I do the following: Check the $_POST if there is an advsearch for that list to store it in a local variable. That would mean that the list is coming from an advsearch page. The function is QUICheckPOSTForSearch Check the $_GET if there is a running search in order to show it in the dropdown box. The function is QUICheckURLForSearch Prepare the form and assign in to the savesearchcontents_block Check if we need to save anything. The function is QUICheckPOSTForSaving Check if we need to delete an entry. The function is QUICheckPOSTForDelete Check if we need to retrieve an entry. The function is QUIRetreiveSearches Here is the code I've implemented. Sorry for the long post. You may use the code in case you find it useful.
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Procedure to display Error Message Dialog
function QMSGShowError($strTitle, $strDescription)
{
echo "<script langauge=\"javascript\">alert(\"".$strDescription."\");</script>";
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Procedure to display Error Message Dialog
function QMSGShowPrompt($strTitle, $strDescription, $strDefault=null)
{
echo "<script langauge=\"javascript\">var x=prompt(\"".$strDescription."\", \"".$strDefault."\") </script>";
echo "<script langauge=\"javascript\">document.write(x); </script>";
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to check if we need to get POST for Save Search
function QUICheckPOSTForSearch()
{
$savesearchurl='';
if ($_POST['a'] == "advsearch")
{
$savesearchurl = '';
// loop through POST array to build GET like URL
// don't forget to exclude 'SearchButton' foreach($_POST as $key => $data)
{
if (is_array($data))
{
$i = 0;
foreach($data as $subkey => $subdata)
{
if($key != 'SearchButton' && $subdata!='')
{
$savesearchurl.= $key."[".$i."]=".urlencode($subdata)."&";
$i+=1;
}
}
}
else
if($key != 'SearchButton' && $data!='')
{
$savesearchurl.= $key."=".urlencode($data)."&";
}
}
if ($savesearchurl[strlen($savesearchurl)-1]=="&")
$savesearchurl=substr($savesearchurl,0,strlen($savesearchurl)-1); return $savesearchurl;
}
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to check if we need to get URL for Save Search
function QUICheckURLForSearch()
{
$savesearchurl='';
if ($_GET['a'] == "advsearch")
{
$savesearchurl = '';
// loop through POST array to build GET like URL
// don't forget to exclude 'SearchButton' foreach($_GET as $key => $data)
{
if (is_array($data))
{
$i = 0;
foreach($data as $subkey => $subdata)
{
if($key != 'SearchButton' && $subdata!='')
{
$savesearchurl.= $key."[".$i."]=".urlencode($subdata)."&";
$i+=1;
}
}
}
else
if($key != 'SearchButton' && $data!='')
{
$savesearchurl.= $key."=".urlencode($data)."&";
}
}
if ($savesearchurl[strlen($savesearchurl)-1]=="&")
$savesearchurl=substr($savesearchurl,0,strlen($savesearchurl)-1); return $savesearchurl;
}
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to check if we need to save a Search
function QUICheckPOSTForSaving()
{
if ($_POST['a'] == "savesearch" && $_POST['formname'] != null && $_POST['searchname'] != null)
{
global $conn; $searchname = $_POST['searchname'];
$formname = $_POST['formname'];
$url = $_POST['url']; //the code bellow checks if the search name exists in the DB
$sql = "SELECT COUNT(*) iCount FROM savesearch WHERE strFormName = '".$formname."' AND strCharacteristic = 'WEBSEARCH' AND strFieldKey = '".$searchname."' ";
$rs = CustomQuery($sql);
$row = db_fetch_array($rs); if ($row["iCount"] > 0)
{
QMSGShowError('Save Search, 'The search name already exists!');
return;
} //the code bellow inserts the search in the DB
$sql = "INSERT INTO savesearch (strFormName, strFieldKey, strCharacteristic, strValue) VALUES ('".$formname."', '".$searchname."', 'WEBSEARCH', '".$url."') ON DUPLICATE KEY UPDATE strValue = '".$url."'";
db_exec($sql,$conn); //Clear out the form
$_POST['formname'] = '';
$_POST['searchname'] = '';
return true;
}
else
{
return false;
}
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to check if we need to delete a Search
function QUICheckPOSTForDelete()
{
if ($_POST['a'] == "deletesearch" && $_POST['formname'] != null && $_POST['searchname'] != null)
{
global $conn; $searchname = $_POST['searchname'];
$formname = $_POST['formname'];
$url = $_POST['url']; $sql = "DELETE FROM savesearch WHERE strFormName = '".$formname."' AND strCharacteristic = 'WEBSEARCH' AND strFieldKey = '".$searchname."' ";
db_exec($sql,$conn); //Clear out the form
$_POST['formname'] = '';
$_POST['searchname'] = '';
return true;
}
else
{
return false;
}
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to Retreive Saved Searches
function QUIRetreiveSearches($formname)
{
global $conn; //Code to retrieve from DB
$sql = "SELECT strFieldKey, strValue, FROM usersettings WHERE strFormName = '".$formname."' AND strCharacteristic = 'WEBSEARCH' ";
$rs = CustomQuery($sql); $results = array();
while ($row = db_fetch_array($rs))
{
$results[] = array(
'searchname' => $row["strFieldKey"],
'url' => $row["strValue"]
);
}
return $results;
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to load Saved Searches
function QUISavedSearches($xt, $formname)
{ $strPerm = GetUserPermissions();
$allow_search=(strpos($strPerm,"S")!==false); if ($allow_search)
{ $xt->assign('savesearch_block',True); $savedsearches = <<<EOT
<script type=text/javascript>
function savesearch() {
var answer = prompt("Name:", "");
if (answer){
document.forms.savesearchform.a.value='savesearch';
document.forms.savesearchform.searchname.value=answer;
document.forms.savesearchform.submit();
}
}
function deletesearch(searchname) {
var selObj = document.getElementById('optSavedSearches');
var selIndex = selObj.selectedIndex;
var selText = selObj.options[selIndex].text;
var answer = confirm("Do you want to delete the search '" + selText + "';");
if (answer){
document.forms.savesearchform.a.value='deletesearch';
document.forms.savesearchform.searchname.value=selText;
document.forms.savesearchform.submit();
}
} function changesearch(x){
document.location.href=x.options[x.selectedIndex].value;
} </SCRIPT>
EOT; $currentsearch = $formname."_list.php?".QUICheckURLForSearch(); //Check if we have an advanced search in our URL
$currenturl = $formname."_list.php?".QUICheckPOSTForSearch(); $savesearch_block=array();
$savesearch_block["begin"]="<form method=\"POST\" ";
$savesearch_block["begin"].="action=\"".$formname."_list.php\" ";
$savesearch_block["begin"].="name=\"savesearchform\"><input type=\"hidden\" id=\"a\" name=\"a\" value=\"\">";
$savesearch_block["begin"].="<input type=\"hidden\" id=\"formname\" name=\"formname\" value=\"".$formname."\">";
$savesearch_block["begin"].="<input type=\"hidden\" id=\"searchname\" name=\"searchname\" value=\"\">";
$savesearch_block["begin"].="<input type=\"hidden\" id=\"url\" name=\"url\" value=\"".$currenturl."\">";
$savesearch_block["end"]="</form>";
$xt->assignbyref("savesearchcontents_block",$savesearch_block); //Check if we need to save a search
QUICheckPOSTForSaving(); //Check if we need to delete a search
QUICheckPOSTForDelete(); //Check if we need to retreive searches
$reteivedsearches = QUIRetreiveSearches($formname); $savedsearches .= '<select id="optSavedSearches" style="WIDTH: 185px" onchange="changesearch(this)">'; foreach ($reteivedsearches as $strSearch)
{
if (isset($currentsearch) && $currentsearch==$strSearch['url'])
{
$savedsearches .= '<option selected value="'.$strSearch['url'].'">'.$strSearch['searchname'].'</option>';
}
else
{
$savedsearches .= '<option value="'.$strSearch['url'].'">'.$strSearch['searchname'].'</option>';
}
}
$savedsearches .= '</select>'; //blackbutton is a css button that i've impelemented. You can see it in my screenshoots
$savedsearches .= '
<button class=blackbutton85 type=reset value="Save" title="Save Search" hidefocus="hidefocus" onclick="savesearch()">Save</button> ';
$savedsearches .= '<button class=blackbutton85 type=reset value="Delete" title="Delete Search" hidefocus="hidefocus" onclick="deletesearch()">Delete</button> ';
$xt->assign('savedsearches',$savedsearches);
}
}
|