345 lines
9.1 KiB
PHP
345 lines
9.1 KiB
PHP
<?php
|
|
|
|
/** Search Page
|
|
*
|
|
* @version 1.0.0
|
|
* @since 2010-01-10
|
|
* @author martin lenzelbauer
|
|
*
|
|
*
|
|
*/
|
|
class SearchPage extends Page{
|
|
|
|
var $template; //html template
|
|
|
|
/** C'tor
|
|
*/
|
|
//------------------------------------------------
|
|
function SearchPage($id, $parent){
|
|
//------------------------------------------------
|
|
parent::Page($id, $parent);
|
|
$this->name = "[Suche]";
|
|
$this->dynamic = true;
|
|
$this->buttonPreview = true;
|
|
$this->buttonPublish = true;
|
|
$this->template = "";
|
|
$this->editable = USER_ADMIN;
|
|
}
|
|
|
|
|
|
|
|
/** @see Page::install()
|
|
*/
|
|
//-----------------------------------------------
|
|
function install(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_searchpage (
|
|
template VARCHAR(100) NOT NULL DEFAULT '',
|
|
PRIMARY KEY (id)
|
|
)");
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see CmsObject::load()
|
|
*/
|
|
//-----------------------------------------------
|
|
function load($path=array()){
|
|
//-----------------------------------------------
|
|
parent::load($path);
|
|
if(!$this->classId){
|
|
return;
|
|
}
|
|
$query = sprintf("SELECT * FROM bruckm_searchpage WHERE id = %d", $this->classId);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
$this->template = $line['template'];
|
|
}
|
|
|
|
|
|
/** @see Page::doSave()
|
|
*/
|
|
//----------------------------------------------
|
|
function doSave(){
|
|
//----------------------------------------------
|
|
$query = sprintf("UPDATE bruckm_searchpage SET template = %s WHERE id = %d",
|
|
sqlstring($this->template),
|
|
sqlnum($this->classId));
|
|
dbQuery($query);
|
|
parent::doSave();
|
|
}
|
|
|
|
|
|
/** @see Page::doCreate()
|
|
*/
|
|
//----------------------------------------------
|
|
function doCreate(){
|
|
//----------------------------------------------
|
|
$query = sprintf("INSERT INTO bruckm_searchpage (template) VALUES (%s)", sqlstring($this->template));
|
|
dbQuery($query);
|
|
$this->classId = mysql_insert_id();
|
|
parent::doCreate();
|
|
}
|
|
|
|
|
|
/** @see Page::doDelete()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doDelete(){
|
|
//-----------------------------------------------
|
|
parent::doDelete();
|
|
$query = sprintf("DELETE FROM bruckm_searchpage WHERE id = %d LIMIT 1", $this->classId);
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see CmsObject::update()
|
|
*/
|
|
//-----------------------------------------------
|
|
function update(){
|
|
//-----------------------------------------------
|
|
parent::update();
|
|
if(isset($_POST['keyword'])){
|
|
$this->keyword = $_POST['keyword'];
|
|
}
|
|
if(isset($_POST['template'])){
|
|
$this->template = $_POST['template'];
|
|
}
|
|
}
|
|
|
|
|
|
/** @see CmsObject::doPrintClassContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doPrintClassContent(){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."searchpage.html");
|
|
$templates = "";
|
|
$dir = opendir(TEMPLATE_DIR);
|
|
while($file = readdir($dir)){
|
|
if(is_file(TEMPLATE_DIR.$file)){
|
|
if($file == $this->template){
|
|
$templates .= "<option value=\"$file\" selected=\"selected\">$file</option>";
|
|
}
|
|
else{
|
|
$templates .= "<option value=\"$file\">$file</option>\n";
|
|
}
|
|
}
|
|
}
|
|
$t->setVar("TEMPLATES", $templates);
|
|
if($this->isEditable()){
|
|
$t->setVar("TEMPLATE_DISABLED", "");
|
|
}
|
|
else{
|
|
$t->setVar("TEMPLATES_DISABLED", "disabled=\"disabled\"");
|
|
}
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see CmsObject::canBePublished()
|
|
*/
|
|
//----------------------------------------------
|
|
function canBePublished(){
|
|
//----------------------------------------------
|
|
if(empty($this->template)){
|
|
logError(3, "No template selected", __FILE__, __LINE__);
|
|
$this->addError("Bitte wählen Sie ein Template aus!");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/** @see CmsObject::publish()
|
|
*/
|
|
//---------------------------------------------
|
|
function doPublish(){
|
|
//---------------------------------------------
|
|
$t = new Template(TEMPLATE_DIR.$this->template);
|
|
$content = "<h1>".$this->toString()."</h1>";
|
|
|
|
// search form
|
|
$f = new Template(TEMPLATE_DIR."subparts/searchform.html");
|
|
$f->setVar("ID", $this->id);
|
|
$f->setVar("KEYWORDS", $_POST['keywords']);
|
|
$content .= $f->toString();
|
|
|
|
// search results
|
|
if (isset($_POST['keywords'])) {
|
|
$results = $this->doSearch();
|
|
if (empty($results)) {
|
|
$content .= '<p><em>Die Suche ergab keine Treffer</em></p>';
|
|
}
|
|
else {
|
|
$classes = array('StdPage','TicketEvent','PersonPage','PressPage');
|
|
$content .= '<ol>';
|
|
foreach ($results as $i=>$j) {
|
|
if (in_array($j['class'], $classes)) {
|
|
$content .= '<li>';
|
|
$content .= '<p><a href="?id=' . $i . '" target="_blank">' . htmlspecialchars($j['name']) . '</a></p>';
|
|
if (empty($j['preview'])) {
|
|
$j['preview'] = $this->searchPreview($i);
|
|
}
|
|
$content .= '<p>' . substr($j['preview'], 0, 200) . '...</p>';
|
|
$content .= '</li>';
|
|
}
|
|
}
|
|
$content .= '</ol>';
|
|
}
|
|
}
|
|
|
|
$t->setVar("CONTENT", $content);
|
|
if(!$this->parentObj){
|
|
$this->parentObj = FlexiconFactory::instanceById($this->parentId);
|
|
}
|
|
$parent = clone($this->parentObj);
|
|
$parent->load();
|
|
$t = $parent->printMenu($t);
|
|
$t->setVar("TITLE", $this->toString());
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see CmsObject::show()
|
|
*/
|
|
//------------------------------------------------
|
|
function show(){
|
|
//------------------------------------------------
|
|
if($this->canBePublished()){
|
|
return $this->doPublish();
|
|
}
|
|
}
|
|
|
|
|
|
/** @see CmsObject::getCssClass()
|
|
*/
|
|
//------------------------------------------------
|
|
function getCssClass(){
|
|
//------------------------------------------------
|
|
return "itemSearchPage";
|
|
}
|
|
|
|
|
|
// === ADDITIONAL METHODS ============================================== //
|
|
|
|
/** returns the name of the template file
|
|
* @return template
|
|
*/
|
|
//-----------------------------------------------
|
|
function getTemplate(){
|
|
//-----------------------------------------------
|
|
return $this->template;
|
|
}
|
|
|
|
|
|
/** returns the Menu object
|
|
* @return Menu
|
|
*/
|
|
//-----------------------------------------------
|
|
function getMenu(){
|
|
//-----------------------------------------------
|
|
if(!$this->parentObj){
|
|
$this->parentObj = FlexiconFactory::instanceById($this->parentId);
|
|
}
|
|
$parent = clone($this->parentObj);
|
|
$parent->load();
|
|
return $parent;
|
|
}
|
|
|
|
|
|
/** executes the search
|
|
*/
|
|
//----------------------------------------------
|
|
function doSearch(){
|
|
//----------------------------------------------
|
|
// extract useful keywords
|
|
$keywords = explode(" ", $_POST['keywords']);
|
|
$terms = array();
|
|
foreach ($keywords as $term) {
|
|
if (strlen($term) > 2) {
|
|
if ($term != "AND") {
|
|
$terms[] = $term;
|
|
}
|
|
}
|
|
}
|
|
|
|
$matches = array();
|
|
// search in 'name' field of 'index' table
|
|
$like = "";
|
|
foreach ($terms as $term) {
|
|
if (strlen($like) == 0) {
|
|
$like .= "LIKE " . sqlstring("%" . $term . "%");
|
|
}
|
|
else {
|
|
$like .= " AND name LIKE " . sqlstring("%" . $term . "%");
|
|
}
|
|
}
|
|
$query = sprintf("SELECT id, name, class FROM bruckm_index WHERE name %s", $like);
|
|
$result = dbQuery($query);
|
|
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
|
|
$matches[$line['id']]['name'] = $line['name'];
|
|
$matches[$line['id']]['class'] = $line['class'];
|
|
}
|
|
|
|
// search text blocks
|
|
$like = "";
|
|
foreach ($terms as $term) {
|
|
if (strlen($like) == 0) {
|
|
$like .= "LIKE " . sqlstring("%" . $term . "%");
|
|
}
|
|
else {
|
|
$like .= " AND content LIKE " . sqlstring("%" . $term . "%");
|
|
}
|
|
}
|
|
$query = sprintf("SELECT id, content FROM bruckm_textblock WHERE content %s", $like);
|
|
$result = dbQuery($query);
|
|
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
|
|
$query = sprintf("SELECT id FROM bruckm_stdpage WHERE buildingBlocks REGEXP '.*textblock,%d[^0-9]*' LIMIT 1", $line['id']);
|
|
$r = dbQuery($query);
|
|
$l = mysqli_fetch_array($r, MYSQLI_ASSOC);
|
|
$query = sprintf("SELECT id, name FROM bruckm_index WHERE classId = %d AND class = 'StdPage' LIMIT 1", $l['id']);
|
|
$r = dbQuery($query);
|
|
if ($l = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
|
|
$matches[$l['id']]['name'] = $l['name'];
|
|
$matches[$l['id']]['class'] = 'StdPage';
|
|
$matches[$l['id']]['preview'] = $line['content'];
|
|
}
|
|
}
|
|
|
|
return $matches;
|
|
}
|
|
|
|
|
|
/** searches for a page preview by flexicon id
|
|
*/
|
|
############################################
|
|
function searchPreview($id) {
|
|
############################################
|
|
$query = sprintf("SELECT class, classId, childObjects FROM bruckm_index WHERE id = %d", $id);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
if ($line['class'] == 'TicketEvent') {
|
|
return $this->searchPreview($line['childObjects']);
|
|
}
|
|
|
|
$query = sprintf("SELECT buildingBlocks FROM bruckm_stdpage WHERE id = %d", $line['classId']);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
$blocks = explode("\t", $line['buildingBlocks']);
|
|
foreach ($blocks as $b) {
|
|
if (substr($b, 0, 4) == "text") {
|
|
$id = substr($b, 10);
|
|
$query = sprintf("SELECT content FROM bruckm_textblock WHERE id = %d", $id);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
return $line['content'];
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
};
|
|
|
|
|
|
?>
|