Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_fullsearch.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00083 class t3lib_fullsearch {
00084    var $storeList = "search_query_smallparts,queryConfig,queryTable,queryFields,queryLimit,queryOrder,queryOrderDesc,queryOrder2,queryOrder2Desc,queryGroup,search_query_makeQuery";
00085    var $downloadScript = "index.php";
00086    var $formW=48;
00087    var $noDownloadB=0;
00088 
00089 
00090 
00091 
00092 
00098    function form()   {
00099       $out='
00100       Search Word:<BR>
00101       <input type="text" name="SET[sword]" value="'.htmlspecialchars($GLOBALS["SOBE"]->MOD_SETTINGS["sword"]).'"'.$GLOBALS["TBE_TEMPLATE"]->formWidth(20).'><input type="submit" name="submit" value="Search All Records">
00102       ';
00103 
00104       return $out;
00105    }
00106 
00112    function makeStoreControl()   {
00113          // Load/Save
00114       $storeArray = $this->initStoreArray();
00115       $cur="";
00116 
00117          // Store Array:
00118       $opt=array();
00119       reset($storeArray);
00120       while(list($k,$v)=each($storeArray))   {
00121          $opt[]='<option value="'.$k.'"'.(!strcmp($cur,$v)?" selected":"").'>'.htmlspecialchars($v).'</option>';
00122       }
00123 
00124          // Actions:
00125       if (t3lib_extMgm::isLoaded("sys_action")) {
00126          $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', 'type=2', '', 'title');
00127          if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
00128             $opt[]='<option value="0">__Save to Action:__</option>';
00129             while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00130                $opt[]='<option value="-'.$row["uid"].'"'.(!strcmp($cur,"-".$row["uid"])?" selected":"").'>'.htmlspecialchars($row["title"]." [".$row["uid"]."]").'</option>';
00131             }
00132          }
00133          $GLOBALS['TYPO3_DB']->sql_free_result($res);
00134       }
00135 
00136       $TDparams=' nowrap="nowrap" class="bgColor4"';
00137       $tmpCode='
00138       <table border=0 cellpadding=3 cellspacing=1>
00139       <tr'.$TDparams.'><td><select name="storeControl[STORE]" onChange="document.forms[0][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';">'.implode(chr(10),$opt).'</select><input type="submit" name="storeControl[LOAD]" value="Load"></td></tr>
00140       <tr'.$TDparams.'><td nowrap><input name="storeControl[title]" value="" type="text" max=80'.$GLOBALS["SOBE"]->doc->formWidth().'><input type="submit" name="storeControl[SAVE]" value="Save" onClick="if (document.forms[0][\'storeControl[STORE]\'].options[document.forms[0][\'storeControl[STORE]\'].selectedIndex].value<0) return confirm(\'Are you sure you want to overwrite the existing query in this action?\');"><input type="submit" name="storeControl[REMOVE]" value="Remove"></td></tr>
00141       </table>
00142       ';
00143       return $tmpCode;
00144    }
00145 
00151    function initStoreArray()  {
00152       $storeArray=array(
00153          "0" => "[New]"
00154       );
00155 
00156       $savedStoreArray = unserialize($GLOBALS["SOBE"]->MOD_SETTINGS["storeArray"]);
00157 
00158       if (is_array($savedStoreArray))  {
00159          $storeArray = array_merge($storeArray,$savedStoreArray);
00160       }
00161       return $storeArray;
00162    }
00163 
00171    function cleanStoreQueryConfigs($storeQueryConfigs,$storeArray)   {
00172       if (is_array($storeQueryConfigs))   {
00173          reset($storeQueryConfigs);
00174          while(list($k,$v)=each($storeQueryConfigs))  {
00175             if (!isset($storeArray[$k]))  unset($storeQueryConfigs[$k]);
00176          }
00177       }
00178       return $storeQueryConfigs;
00179    }
00180 
00188    function addToStoreQueryConfigs($storeQueryConfigs,$index)  {
00189       $keyArr = explode(",",$this->storeList);
00190       reset($keyArr);
00191       $storeQueryConfigs[$index]=array();
00192       while(list(,$k)=each($keyArr))   {
00193          $storeQueryConfigs[$index][$k]=$GLOBALS["SOBE"]->MOD_SETTINGS[$k];
00194       }
00195       return $storeQueryConfigs;
00196    }
00197 
00204    function saveQueryInAction($uid) {
00205       if (t3lib_extMgm::isLoaded("sys_action")) {
00206          $keyArr = explode(",",$this->storeList);
00207          reset($keyArr);
00208          $saveArr=array();
00209          while(list(,$k)=each($keyArr))   {
00210             $saveArr[$k]=$GLOBALS["SOBE"]->MOD_SETTINGS[$k];
00211          }
00212 
00213          $qOK = 0;
00214             // Show query
00215          if ($saveArr["queryTable"])   {
00216             $qGen = t3lib_div::makeInstance("t3lib_queryGenerator");
00217             $qGen->init("queryConfig",$saveArr["queryTable"]);
00218             $qGen->makeSelectorTable($saveArr);
00219 
00220             $qGen->enablePrefix=1;
00221             $qString = $qGen->getQuery($qGen->queryConfig);
00222             $qCount = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString.t3lib_BEfunc::deleteClause($qGen->table));
00223             $qSelect = $qGen->getSelectQuery($qString);
00224 
00225             $res = @$GLOBALS['TYPO3_DB']->sql(TYPO3_db,$qCount);
00226             if (!$GLOBALS['TYPO3_DB']->sql_error())   {
00227                $dA = array();
00228                $dA["t2_data"] = serialize(array(
00229                   "qC"=>$saveArr,
00230                   "qCount" => $qCount,
00231                   "qSelect" => $qSelect,
00232                   "qString" => $qString
00233                ));
00234                $GLOBALS['TYPO3_DB']->exec_UPDATEquery("sys_action", "uid=".intval($uid), $dA);
00235                $qOK=1;
00236             }
00237          }
00238 
00239          return $qOK;
00240       }
00241    }
00242 
00251    function loadStoreQueryConfigs($storeQueryConfigs,$storeIndex,$writeArray) {
00252       if ($storeQueryConfigs[$storeIndex])   {
00253          $keyArr = explode(",",$this->storeList);
00254          reset($keyArr);
00255          while(list(,$k)=each($keyArr))   {
00256             $writeArray[$k]=$storeQueryConfigs[$storeIndex][$k];
00257          }
00258       }
00259       return $writeArray;
00260    }
00261 
00267    function procesStoreControl() {
00268       $storeArray = $this->initStoreArray();
00269       $storeQueryConfigs = unserialize($GLOBALS["SOBE"]->MOD_SETTINGS["storeQueryConfigs"]);
00270 
00271       $storeControl = t3lib_div::_GP("storeControl");
00272       $storeIndex = intval($storeControl["STORE"]);
00273       $saveStoreArray=0;
00274       $writeArray=array();
00275       if (is_array($storeControl))  {
00276          if ($storeControl["LOAD"]) {
00277             if ($storeIndex>0)   {
00278                $writeArray=$this->loadStoreQueryConfigs($storeQueryConfigs,$storeIndex,$writeArray);
00279                $saveStoreArray=1;
00280                $msg="'".htmlspecialchars($storeArray[$storeIndex])."' query loaded!";
00281             } elseif ($storeIndex<0 && t3lib_extMgm::isLoaded("sys_action"))  {
00282                $actionRecord=t3lib_BEfunc::getRecord("sys_action",abs($storeIndex));
00283                if (is_array($actionRecord))  {
00284                   $dA = unserialize($actionRecord["t2_data"]);
00285                   $dbSC=array();
00286                   if (is_array($dA["qC"]))   {
00287                      $dbSC[0] = $dA["qC"];
00288                   }
00289                   $writeArray=$this->loadStoreQueryConfigs($dbSC,"0",$writeArray);
00290                   $saveStoreArray=1;
00291                   $acTitle=htmlspecialchars($actionRecord["title"]);
00292                   $msg="Query from action '".$acTitle."' loaded!";
00293                }
00294             }
00295          } elseif ($storeControl["SAVE"]) {
00296             if ($storeIndex<0)   {
00297                $qOK = $this->saveQueryInAction(abs($storeIndex));
00298                if ($qOK)   {
00299                   $msg="Query OK and saved.";
00300                } else {
00301                   $msg="No query saved!";
00302                }
00303             } else {
00304                if (trim($storeControl["title"]))   {
00305                   if ($storeIndex>0)   {
00306                      $storeArray[$storeIndex]=$storeControl["title"];
00307                   } else {
00308                      $storeArray[]=$storeControl["title"];
00309                      end($storeArray);
00310                      $storeIndex=key($storeArray);
00311                   }
00312                   $storeQueryConfigs=$this->addToStoreQueryConfigs($storeQueryConfigs,$storeIndex);
00313                   $saveStoreArray=1;
00314                   $msg="'".htmlspecialchars($storeArray[$storeIndex])."' query saved!";
00315                }
00316             }
00317          } elseif ($storeControl["REMOVE"])  {
00318             if ($storeIndex>0)   {
00319                $msg="'".$storeArray[$storeControl["STORE"]]."' query entry removed!";
00320                unset($storeArray[$storeControl["STORE"]]);  // Removing
00321                $saveStoreArray=1;
00322             }
00323          }
00324       }
00325       if ($saveStoreArray) {
00326          unset($storeArray[0]);  // making sure, index 0 is not set!
00327          $writeArray["storeArray"]=serialize($storeArray);
00328          $writeArray["storeQueryConfigs"]=serialize($this->cleanStoreQueryConfigs($storeQueryConfigs,$storeArray));
00329          $GLOBALS["SOBE"]->MOD_SETTINGS = t3lib_BEfunc::getModuleData($GLOBALS["SOBE"]->MOD_MENU, $writeArray, $GLOBALS["SOBE"]->MCONF["name"], "ses");
00330       }
00331       return $msg;
00332    }
00333 
00339    function queryMaker()   {
00340       global $TCA;
00341 
00342       $msg=$this->procesStoreControl();
00343 
00344       $output.= $GLOBALS["SOBE"]->doc->section('Load/Save Query',$this->makeStoreControl(),0,1);
00345       if ($msg)   {
00346          $output.= $GLOBALS["SOBE"]->doc->section('','<font color=red><strong>'.$msg.'</strong></font>');
00347       }
00348       $output.= $GLOBALS["SOBE"]->doc->spacer(20);
00349 
00350 
00351          // Query Maker:
00352       $qGen = t3lib_div::makeInstance("t3lib_queryGenerator");
00353       $qGen->init("queryConfig",$GLOBALS["SOBE"]->MOD_SETTINGS["queryTable"]);
00354       $tmpCode=$qGen->makeSelectorTable($GLOBALS["SOBE"]->MOD_SETTINGS);
00355       $output.= $GLOBALS["SOBE"]->doc->section('Make query',$tmpCode,0,1);
00356 
00357       $mQ = $GLOBALS["SOBE"]->MOD_SETTINGS["search_query_makeQuery"];
00358 
00359          // Make form elements:
00360       if ($qGen->table && is_array($TCA[$qGen->table]))  {
00361          if ($mQ) {
00362                // Show query
00363             $qGen->enablePrefix=1;
00364             $qString = $qGen->getQuery($qGen->queryConfig);
00365 //          debug($qGen->queryConfig);
00366 
00367             switch($mQ) {
00368                case "count":
00369                   $qExplain = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString.t3lib_BEfunc::deleteClause($qGen->table));
00370                break;
00371                default:
00372                   $qExplain = $qGen->getSelectQuery($qString);
00373                   if ($mQ=="explain")  {
00374                      $qExplain="EXPLAIN ".$qExplain;
00375                   }
00376                break;
00377             }
00378 
00379             $output.= $GLOBALS["SOBE"]->doc->section('SQL query',$this->tableWrap(htmlspecialchars($qExplain)),0,1);
00380 
00381             $res = @$GLOBALS['TYPO3_DB']->sql(TYPO3_db,$qExplain);
00382             if ($GLOBALS['TYPO3_DB']->sql_error()) {
00383                $out.="<BR><strong>Error:</strong><BR><font color=red><strong>".$GLOBALS['TYPO3_DB']->sql_error()."</strong></font>";
00384                $output.= $GLOBALS["SOBE"]->doc->section('SQL error',$out,0,1);
00385             } else {
00386                $cPR = $this->getQueryResultCode($mQ,$res,$qGen->table);
00387                $output.=$GLOBALS["SOBE"]->doc->section($cPR["header"],$cPR["content"],0,1);
00388             }
00389          }
00390       }
00391       return $output;
00392    }
00393 
00402    function getQueryResultCode($mQ,$res,$table) {
00403       global $TCA;
00404       $output="";
00405       $cPR=array();
00406       switch($mQ) {
00407          case "count":
00408             $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
00409             $cPR["header"]='Count';
00410             $cPR["content"]="<BR><strong>".$row[0]. "</strong> records selected.";
00411          break;
00412          case "all":
00413             $rowArr=array();
00414             while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00415                $rowArr[]=$this->resultRowDisplay($row,$TCA[$table],$table);
00416                $lrow=$row;
00417             }
00418             if (count($rowArr))  {
00419                $out.="<table border=0 cellpadding=2 cellspacing=1>".$this->resultRowTitles($lrow,$TCA[$table],$table).implode(chr(10),$rowArr)."</table>";
00420             }
00421             if (!$out)  $out="<em>No rows selected!</em>";
00422             $cPR["header"]='Result';
00423             $cPR["content"]=$out;
00424          break;
00425          case "csv":
00426             $rowArr=array();
00427             $first=1;
00428             while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00429                if ($first) {
00430                   $rowArr[]=$this->csvValues(array_keys($row),",","");
00431                   $first=0;
00432                }
00433                $rowArr[]=$this->csvValues($row);
00434             }
00435             if (count($rowArr))  {
00436                $out.='<textarea name="whatever" rows="20" wrap="off"'.$GLOBALS["SOBE"]->doc->formWidthText($this->formW,"","off").'>'.t3lib_div::formatForTextarea(implode(chr(10),$rowArr)).'</textarea>';
00437                if (!$this->noDownloadB)   {
00438                   $out.='<BR><input type="submit" name="download_file" value="Click to download file" onClick="document.location=\''.$this->downloadScript.'\';">';     // document.forms[0].target=\'_blank\';
00439                }
00440                   // Downloads file:
00441                if (t3lib_div::_GP("download_file"))   {
00442                   $filename="TYPO3_".$table."_export_".date("dmy-Hi").".csv";
00443                   $mimeType = "application/octet-stream";
00444                   Header("Content-Type: ".$mimeType);
00445                   Header("Content-Disposition: attachment; filename=".$filename);
00446                   echo implode(chr(13).chr(10),$rowArr);
00447                   exit;
00448                }
00449             }
00450             if (!$out)  $out="<em>No rows selected!</em>";
00451             $cPR["header"]='Result';
00452             $cPR["content"]=$out;
00453          break;
00454          case "xml":
00455             $className=t3lib_div::makeInstanceClassName("t3lib_xml");
00456             $xmlObj = new $className("typo3_export");
00457             $xmlObj->includeNonEmptyValues=1;
00458             $xmlObj->renderHeader();
00459             $first=1;
00460             while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00461                if ($first) {
00462                   $xmlObj->setRecFields($table,implode(",",array_keys($row)));
00463       //          debug($xmlObj->XML_recFields);
00464                   $first=0;
00465                }
00466                $xmlObj->addRecord($table,$row);
00467             }
00468             $xmlObj->renderFooter();
00469             if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
00470                $xmlData=$xmlObj->getResult();
00471                $out.='<textarea name="whatever" rows="20" wrap="off"'.$GLOBALS["SOBE"]->doc->formWidthText($this->formW,"","off").'>'.t3lib_div::formatForTextarea($xmlData).'</textarea>';
00472                if (!$this->noDownloadB)   {
00473                   $out.='<BR><input type="submit" name="download_file" value="Click to download file" onClick="document.location=\''.$this->downloadScript.'\';">';     // document.forms[0].target=\'_blank\';
00474                }
00475                   // Downloads file:
00476                if (t3lib_div::_GP("download_file"))   {
00477                   $filename="TYPO3_".$table."_export_".date("dmy-Hi").".xml";
00478                   $mimeType = "application/octet-stream";
00479                   Header("Content-Type: ".$mimeType);
00480                   Header("Content-Disposition: attachment; filename=".$filename);
00481                   echo $xmlData;
00482                   exit;
00483                }
00484             }
00485             if (!$out)  $out="<em>No rows selected!</em>";
00486             $cPR["header"]='Result';
00487             $cPR["content"]=$out;
00488          break;
00489          case "explain":
00490          default:
00491             while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00492                $out.="<BR>".t3lib_div::view_array($row);
00493             }
00494             $cPR["header"]='Explain SQL query';
00495             $cPR["content"]=$out;
00496          break;
00497       }
00498       return $cPR;
00499    }
00500 
00509    function csvValues($row,$delim=",",$quote='"')  {
00510       return t3lib_div::csvValues($row,$delim,$quote);
00511    }
00512 
00519    function tableWrap($str)   {
00520       return '<table border=0 cellpadding=10 cellspacing=0 class="bgColor4"><tr><td nowrap><pre>'.$str.'</pre></td></tr></table>';
00521    }
00522 
00528    function search() {
00529       global $TCA;
00530       $SET = t3lib_div::_GP("SET");
00531       $swords = $SET["sword"];
00532 
00533       $limit=200;
00534       $showAlways=0;
00535       if ($swords)   {
00536          reset($TCA);
00537          while(list($table)=each($TCA))   {
00538                // Get fields list
00539             t3lib_div::loadTCA($table);
00540             $conf=$TCA[$table];
00541 
00542             reset($conf["columns"]);
00543             $list=array();
00544             while(list($field,)=each($conf["columns"]))  {
00545                $list[]=$field;
00546             }
00547                // Get query
00548             $qp = $GLOBALS['TYPO3_DB']->searchQuery(array($swords), $list, $table);
00549 
00550                // Count:
00551             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, $qp.t3lib_BEfunc::deleteClause($table));
00552             list($count) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
00553             if($count || $showAlways)  {
00554                   // Output header:
00555                $out.="<strong>TABLE:</strong> ".$GLOBALS["LANG"]->sL($conf["ctrl"]["title"])."<BR>";
00556                $out.="<strong>Results:</strong> ".$count."<BR>";
00557 
00558                   // Show to limit
00559                if ($count) {
00560                   $rowArr = array();
00561                   $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,'.$conf['ctrl']['label'], $table, $qp.t3lib_BEfunc::deleteClause($table), '', '', $limit);
00562                   while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00563                      $rowArr[]=$this->resultRowDisplay($row,$conf,$table);
00564                      $lrow=$row;
00565                   }
00566                   $out.="<table border=0 cellpadding=2 cellspacing=1>".$this->resultRowTitles($lrow,$conf,$table).implode(chr(10),$rowArr)."</table>";
00567                }
00568                $out.="<HR>";
00569             }
00570          }
00571       }
00572       return $out;
00573    }
00574 
00583    function resultRowDisplay($row,$conf,$table) {
00584       $out='<tr class="bgColor4">';
00585       reset($row);
00586       while(list($fN,$fV)=each($row))  {
00587          $TDparams = " nowrap";
00588          $fVnew = t3lib_BEfunc::getProcessedValueExtra($table,$fN,$fV);
00589          $out.='<td'.$TDparams.'>'.htmlspecialchars($fVnew).'</td>';
00590       }
00591       $params = '&edit['.$table.']['.$row["uid"].']=edit';
00592       $out.='<td nowrap><A HREF="#" onClick="top.launchView(\''.$table.'\','.$row["uid"].',\''.$GLOBALS["BACK_PATH"].'\');return false;"><img src="'.$GLOBALS["BACK_PATH"].'gfx/zoom2.gif" width="12" height="12" alt="" /></a><A HREF="#" onClick="'.t3lib_BEfunc::editOnClick($params,$GLOBALS["BACK_PATH"],t3lib_div::getIndpEnv("REQUEST_URI").t3lib_div::implodeArrayForUrl("SET",t3lib_div::_POST("SET"))).'"><img src="'.$GLOBALS["BACK_PATH"].'gfx/edit2.gif" width="11" height="12" border="0" alt=""></a></td>
00593       </tr>
00594       ';
00595       return $out;
00596    }
00597 
00606    function resultRowTitles($row,$conf,$table)  {
00607       $out='<tr class="bgColor5">';
00608       reset($row);
00609       while(list($fN,$fV)=each($row))  {
00610          if (strlen($fV)<50)     {$TDparams = " nowrap";} else {$TDparams = "";}
00611          $out.='<td'.$TDparams.'><strong>'.$GLOBALS["LANG"]->sL($conf["columns"][$fN]["label"]?$conf["columns"][$fN]["label"]:$fN,1).'</strong></td>';
00612       }
00613       $out.='<td nowrap></td>
00614       </tr>
00615       ';
00616       return $out;
00617    }
00618 }
00619 
00620 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_fullsearch.php"]) {
00621    include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_fullsearch.php"]);
00622 }
00623 ?>

Generated on Sun Oct 3 01:05:48 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913