Guest User

Untitled

a guest
Jan 5th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.66 KB | None | 0 0
  1. <?php
  2. /**
  3. * $RCSfile$
  4. *
  5. * Description: Search output box
  6. *
  7. * @version 1.0.1
  8. * @author Willi Man
  9. * @copyright four for business AG <www.4fb.de>
  10. *
  11. * {@internal
  12. * created 2004-05-04
  13. * modified 2005-07-12 Andreas Lindner
  14. * modified 2008-04-11 Rudi Bieller
  15. * modified 2008-05-06 Rudi Bieller Added CON_SEARCH_MAXLEN_TEASERTEXT; Fixed <nobr> to be xhtml compliant;
  16. *                       Removed $action, $sCatName = getCategoryName($cat_id, $db); which was not used at all and
  17. *                       added a default output in case article/module was called directly (strlen(trim($searchterm)) == 0)
  18. * }}
  19. *
  20. * $Id$
  21. */
  22.  
  23. #System properties in use:
  24. #Type: searchrange, Name: include
  25. #Contains comma-separated list of cats to be included into search (sub-cats are included automatically)
  26.  
  27. #Logical combination of search terms with AND or OR
  28.  
  29. define('CON_SEARCH_ITEMSPERPAGE', 10);
  30. define('CON_SEARCH_MAXLEN_TEASERTEXT', 200);
  31.  
  32. #Includes
  33. cInclude('includes', 'functions.api.string.php');
  34.  
  35. #Initiliaze template object
  36. if (!is_object($tpl)) {
  37.     $tpl = new Template;
  38. }
  39. $tpl->reset();
  40.  
  41. #Settings
  42. $oArticleProp = new Article_Property($db, $cfg);
  43. $iArtspecReference = 2;
  44.  
  45. $cApiClient = new cApiClient($client);
  46. $sSearchRange = $cApiClient->getProperty('searchrange', 'include');
  47. $aSearchRange = explode(',', $sSearchRange);
  48.  
  49. #Multilingual settings
  50. $sYourSearchFor = mi18n("Ihre Suche nach");
  51. $sMore = mi18n("mehr");
  52.  
  53. #Get search term and pre-process it
  54. if (isset ($_GET['searchterm'])) {
  55.     $searchterm = urldecode(conHtmlentities(strip_tags(stripslashes($_GET['searchterm']))));
  56. } elseif (isset ($_POST['searchterm'])) {
  57.     $searchterm = urldecode(conHtmlentities(strip_tags(stripslashes($_POST['searchterm']))));
  58. }
  59. $searchterm = str_replace(' + ', ' AND ', $searchterm);
  60. $searchterm = str_replace(' - ', ' NOT ', $searchterm);
  61. $searchterm_display = $searchterm;
  62.  
  63. #Get all article specs
  64. $sql = "SELECT
  65.        idartspec, artspec
  66.    FROM
  67.        ".$cfg['tab']['art_spec']."
  68.    WHERE
  69.        client=$client AND
  70.        lang=$lang AND
  71.        online=1";
  72.  
  73. $db->query($sql);
  74. $rows = $db->num_rows();
  75. $aArtspecOnline = array();
  76. $aArtSpecs = array();
  77. $c = 1;
  78. $d = 1;
  79. $e = 1;
  80. while ($db->next_record()) {
  81.     $aArtSpecs[] = $db->f('idartspec');
  82. }
  83. $aArtSpecs[] = 0;
  84.  
  85. if (strlen(trim($searchterm)) > 0) {
  86.     #Fix for PHP < 4.3
  87.    if (!function_exists('conHtmlEntityDecode')) {
  88.         function conHtmlEntityDecode($given_html, $quote_style = ENT_QUOTES) {
  89.             $trans_table = array_flip(conGetHtmlTranslationTable(HTML_SPECIALCHARS, $quote_style));
  90.             $trans_table['&#39;'] = "'";
  91.             return (strtr($given_html, $trans_table));
  92.         }
  93.     }
  94.  
  95.     #Parse search term and set search options
  96.    $searchterm = conHtmlEntityDecode($searchterm);
  97.  
  98.     if (stristr($searchterm, ' or ') === FALSE) {
  99.         $combine = 'and';
  100.     } else {
  101.         $combine = 'or';
  102.     }
  103.     $searchterm = str_replace(' and ', ' ', strtolower($searchterm));
  104.     $searchterm = str_replace(' or ', ' ', strtolower($searchterm));
  105.  
  106.     $options = array('db' => 'regexp', // use db function regexp
  107.         'combine' => $combine, // combine searchterms with and
  108.         'exclude' => false, // => searchrange specified in 'cat_tree', 'categories' and 'articles' is excluded, otherwise included (exclusive)
  109.         'cat_tree' => $aSearchRange, // searchrange
  110.         'artspecs' => $aArtSpecs, // array of article specifications => search only articles with these artspecs
  111.         'protected' => true); // => do not search articles or articles in categories which are offline or protected
  112.  
  113.     $search = new Search($options);
  114.  
  115.     $cms_options = array("head", "html", "htmlhead", "htmltext", "text"); // search only in these cms-types
  116.     $search->setCmsOptions($cms_options);
  117.  
  118.     #Execute search
  119.    $aSearchResults = $search->searchIndex($searchterm, '');
  120.  
  121.     #Build results page
  122.    if (count($aSearchResults) > 0) {
  123.         $tpl->set('s', 'result_page', mi18n("Ergebnis-Seite").':');
  124.  
  125.  
  126.         $imgMesssageRight = '<div class="searchImg"><img src="http://www.weltdertutorials.de/cms/images/rightNewsletter.png" /></div>';
  127.         $pTagvorRight = '<p id="message" class="message">';
  128.         $pTagnachRight = '</p>';
  129.         #Build meessage
  130.        $message = $imgMesssageRight. $pTagvorRight. $sYourSearchFor." '".conHtmlSpecialChars(strip_tags($searchterm_display))."' ".mi18n("hat $$$ Treffer ergeben").":". $pTagnachRight;
  131.         $message = str_replace('$$$', count($aSearchResults), $message);
  132.         $tpl->set('s', 'MESSAGE', $message);
  133.  
  134.  
  135.         #Number of results per page
  136.        $number_of_results = CON_SEARCH_ITEMSPERPAGE;
  137.         $oSearchResults = new SearchResult($aSearchResults, $number_of_results);
  138.  
  139.         $num_res = $oSearchResults->getNumberOfResults() + $pdf_count;
  140.         $num_pages = $oSearchResults->getNumberOfPages();
  141.         $oSearchResults->setReplacement('<strong>', '</strong>'); // html-tags to emphasize the located searchterms
  142.  
  143.         #Get current result page
  144.        if (isset ($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) {
  145.             $page = $_GET['page'];
  146.             $res_page = $oSearchResults->getSearchResultPage($page);
  147.         } else {
  148.             $page = 1;
  149.             $res_page = $oSearchResults->getSearchResultPage($page);
  150.         }
  151.  
  152.         #Build links to other result pages
  153.        for ($i = 1; $i <= $num_pages; $i ++) {
  154.             // this is just for sample client - modify to your needs!
  155.             if ($aCfg['url_builder']['name'] == 'front_content' || $aCfg['url_builder']['name'] == 'MR') {
  156.                 $aParams = array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($i.$sArtSpecs));
  157.             } else {
  158.                 $aParams = array('search' => array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($i.$sArtSpecs)),
  159.                                 'idcat' => $idcat, // needed to build category path
  160.                                 'lang' => $lang, // needed to build category path
  161.                                 'level' => 1); // needed to build category path
  162.             }
  163.             try {
  164.                 $nextlink = Contenido_Url::getInstance()->build($aParams);
  165.             } catch (InvalidArgumentException $e) {
  166.                 $nextlink = $sess->url('front_content.php?idcat='.$idcat.'&amp;idart='.$idart.'&amp;searchterm='.$searchterm_display.'&amp;page='.$i.$sArtSpecs);
  167.             }
  168.             if ($i == $page) {
  169.                 $nextlinks .= '<span style="white-space:nowrap;">&nbsp;<strong>'.$i.'</strong>&nbsp;</span>';
  170.             } else {
  171.                 $nextlinks .= '<span style="white-space:nowrap;">&nbsp;<a href="'.$nextlink.'" title="'.$i.'. '.mi18n("Ergebnisseite anzeigen").'">'.$i.'</a>&nbsp;</span>';
  172.             }
  173.         }
  174.         $tpl->set('s', 'PAGES', $nextlinks);
  175.  
  176.         #Build link to next result page
  177.        if ($page < $num_pages) {
  178.             $n = $page +1;
  179.             // this is just for sample client - modify to your needs!
  180.  
  181.             if ($cfg['url_builder']['name'] == 'front_content' || $cfg['url_builder']['name'] == 'MR') {
  182.                 $aParams = array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($n.$sArtSpecs));
  183.             } else {
  184.                 $aParams = array('search' => array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($n.$sArtSpecs)),
  185.                                 'idcat' => $idcat, // needed to build category path
  186.                                 'lang' => $lang, // needed to build category path
  187.                                 'level' => 1); // needed to build category path
  188.             }
  189.             try {
  190.                 $next = Contenido_Url::getInstance()->build($aParams);
  191.             } catch (InvalidArgumentException $e) {
  192.                 $next = $sess->url('front_content.php?idcat='.$idcat.'&amp;idart='.$idart.'&amp;searchterm='.$searchterm.'&amp;page='.$n.$sArtSpecs);
  193.             }
  194.             $nextpage .= '&nbsp;<a href="'.$next.'" title="'.mi18n("nächste Ergebnisseite anzeigen").'">'.mi18n("vor").'&nbsp;&nbsp;<img src="images/link_pfeil_klein.gif" alt="" /></a>';
  195.             $tpl->set('s', 'NEXT', $nextpage);
  196.         } else {
  197.             $tpl->set('s', 'NEXT', '');
  198.         }
  199.  
  200.         #Build link to previous result page
  201.        if ($page > 1) {
  202.             $p = $page -1;
  203.             // this is just for sample client - modify to your needs!
  204.             if ($cfg['url_builder']['name'] == 'front_content' || $cfg['url_builder']['name'] == 'MR') {
  205.                 $aParams = array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($p.$sArtSpecs));
  206.             } else {
  207.                 $aParams = array('search' => array('lang' => $lang, 'idcat' => $idcat, 'idart' => $idart, 'searchterm' => $searchterm_display, 'page' => ($p.$sArtSpecs)),
  208.                                 'idcat' => $idcat, // needed to build category path
  209.                                 'lang' => $lang, // needed to build category path
  210.                                 'level' => 1); // needed to build category path
  211.             }
  212.             try {
  213.                 $pre = Contenido_Url::getInstance()->build($aParams);
  214.             } catch (InvalidArgumentException $e) {
  215.                 $pre = $sess->url('front_content.php?idcat='.$idcat.'&amp;idart='.$idart.'&amp;searchterm='.$searchterm.'&amp;page='.$p.$sArtSpecs);
  216.             }
  217.             $prevpage .= '<a href="'.$pre.'" title="'.mi18n("vorherige Ergebnisseite anzeigen").'"><img src="images/link_pfeil_klein_links.gif" alt="" />&nbsp;&nbsp;'.mi18n("zurück").'</a>&nbsp;';
  218.             $tpl->set('s', 'PREV', $prevpage);
  219.         } else {
  220.             $tpl->set('s', 'PREV', '');
  221.         }
  222.  
  223.         if (count($res_page) > 0) {
  224.             $i = 1;
  225.             #Build single search result on result page
  226.            foreach ($res_page as $key => $val) {
  227.                 $num = $i + (($page -1) * $number_of_results);
  228.                 $oArt = new Article($key, $client, $lang);
  229.                 #Get publishing date of article
  230.                $pub_system = $oArt->getField('published');
  231.                 $pub_user = trim(strip_tags($oArt->getContent('HEAD', 90)));
  232.                 if ($pub_user != '') {
  233.                     $show_pub_date = "[".$pub_user."]";
  234.                 } else {
  235.                     $show_pub_date = '';
  236.                     if ($pub_system[8] != '0') {
  237.                         $show_pub_date .= $pub_system[8];
  238.                     }
  239.                     $show_pub_date .= $pub_system[9].'.';
  240.                     if ($pub_system[5] != '0') {
  241.                         $show_pub_date .= $pub_system[5];
  242.                     }
  243.                     $show_pub_date .= $pub_system[6].".".$pub_system[0].$pub_system[1].$pub_system[2].$pub_system[3]."]";
  244.                     $show_pub_date = "[".$show_pub_date;
  245.                 }
  246.  
  247.                 #Get text and headline of current article
  248.                $iCurrentArtSpec = $oArticleProp->getArticleSpecification($key, $lang);
  249.                 $aHeadline = $oSearchResults->getSearchContent($key, 'HTMLHEAD', 1);
  250.                 $aSubheadline = $oSearchResults->getSearchContent($key, 'HTMLHEAD', 2);
  251.                 $text = $oSearchResults->getSearchContent($key, 'HTML', 1);
  252.                 $text = capiStrTrimAfterWord($text[0], CON_SEARCH_MAXLEN_TEASERTEXT);
  253.                 $headline = capiStrTrimAfterWord($aHeadline[0], CON_SEARCH_MAXLEN_TEASERTEXT); # conflict with capiStrTrimAfterWord and setReplacement('<strong>', '</strong>')
  254.                $subheadline = capiStrTrimAfterWord($aSubheadline[0], CON_SEARCH_MAXLEN_TEASERTEXT);
  255.  
  256.                 $cat_id = $oSearchResults->getArtCat($key);
  257.                 $similarity = $oSearchResults->getSimilarity($key);
  258.  
  259.                 $similarity = sprintf("%.0f", $similarity);
  260.  
  261.                 #Send output to template
  262.                // this is just for sample client - modify to your needs!
  263.                 if ($cfg['url_builder']['name'] == 'front_content' || $aCfg['url_builder']['name'] == 'MR') {
  264.                     $aParams = array('lang' => $lang, 'idcat' => $cat_id, 'idart' => $key);
  265.                 } else {
  266.                     $aParams = array('search' => array('lang' => $lang, 'idcat' => $cat_id, 'idart' => $key),
  267.                                     'idcat' => $idcat, // needed to build category path
  268.                                     'lang' => $lang, // needed to build category path
  269.                                     'level' => 1); // needed to build category path
  270.                 }
  271.                 try {
  272.                     $href = Contenido_Url::getInstance()->build($aParams);
  273.                 } catch (InvalidArgumentException $e) {
  274.                     $href = $sess->url("front_content.php?idcat=$cat_id&amp;idart=$key");
  275.                 }
  276.                 $tpl->set('d', 'more', $sMore);
  277.                 $tpl->set('d', 'HREF', $href);
  278.                 $tpl->set('d', 'TITLE', mi18n("Link zu Suchergebnis").' '.$i);
  279.                 $tpl->set('d', 'NUM', $num);
  280.                 $tpl->set('d', 'CATNAME', $headline);
  281.                 $tpl->set('d', 'HEADLINE', $text);
  282.                 $tpl->set('d', 'SUBHEADLINE', $subheadline);
  283.                 $tpl->set('d', 'SIMILARITY', $similarity);
  284.                 $tpl->set('d', 'TARGET', '_self');
  285.                 $tpl->set('d', 'PUB_DATE', $show_pub_date);
  286.                 $tpl->next();
  287.                 $i ++;
  288.             }
  289.             $tpl->generate('templates/search_output.html');
  290.         }
  291.     } else {
  292.         #No results
  293.        $imgMesssageFalse = '<div class="searchImg"><img src="http://www.weltdertutorials.de/cms/images/falseNewsletter.png" /></div>';
  294.         $pTagvorFalse = '<p id="message" class="message">';
  295.         $pTagnachFalse = '</p>';
  296.         $tpl->set('s', 'MESSAGE', $imgMesssageFalse. $pTagvorFalse. $sYourSearchFor." '".conHtmlSpecialChars(strip_tags($searchterm))."' ".mi18n("hat leider keine Treffer ergeben")." .".  $pTagnachFalse);
  297.         $tpl->set('s', 'NEXT', '');
  298.         $tpl->set('s', 'PREV', '');
  299.         $tpl->set('s', 'PAGES', '');
  300.         $tpl->set('s', 'result_page', '');
  301.         $tpl->generate('templates/search_output.html');
  302.     }
  303.  
  304. } else {
  305.     echo '<div id="searchResults">';
  306.     echo '<h1>'.mi18n("Keine Suchergebnisse - Bitte suchen Sie über das Sucheingabefeld!").'</h1>';
  307.     echo '</div>';
  308. }
  309.  
  310. class Article_Property {
  311.     var $globalConfig;
  312.     var $oDBInstance;
  313.  
  314.     /**
  315.      * Constructor
  316.      * Hint: Call constructor with Article_Property($db, $cfg);
  317.      * @param  oDBInstance instance of class DB_Contenido
  318.      * @param  globalConfig
  319.      */
  320.     function Article_Property($oDBInstance, $globalConfig) {
  321.         $this->globalConfig = $globalConfig;
  322.         $this->oDBInstance = $oDBInstance;
  323.     }
  324.  
  325.     /**
  326.      * Get specification of an article
  327.      *
  328.      * @param   $iArticleId
  329.      * @return  id of article specification
  330.      */
  331.     function getArticleSpecification($iArticleId, $iLangId) {
  332.  
  333.         $sqlString = "
  334.                    SELECT
  335.                        artspec
  336.                    FROM
  337.                        ".$this->globalConfig['tab']['art_lang']."
  338.                    WHERE
  339.                        idart = '".$iArticleId."' AND
  340.                        idlang = '".$iLangId."'
  341.                    ";
  342.  
  343.         #echo "<pre>$sqlString</pre>";
  344.        $this->oDBInstance->query($sqlString);
  345.  
  346.         if ($this->oDBInstance->next_record()) {
  347.             return $this->oDBInstance->f('artspec');
  348.         } else {
  349.             return false;
  350.         }
  351.     }
  352. }
  353. ?>
Advertisement
Add Comment
Please, Sign In to add comment