TYPO3 API  SVNRelease
class.t3lib_search_livesearch_queryParser.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2009-2011 Michael Klapper <michael.klapper@aoemedia.de>
00006  *  (c) 2010-2011 Jeff Segars <jeff@webempoweredchurch.org>
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *  A copy is found in the textfile GPL.txt and important notices to the license
00018  *  from the author is found in LICENSE.txt distributed with these scripts.
00019  *
00020  *
00021  *  This script is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  This copyright notice MUST APPEAR in all copies of the script!
00027  ***************************************************************/
00028 
00029 /**
00030  * Class for parsing query parameters in backend live search.
00031  *
00032  * @author Michael Klapper <michael.klapper@aoemedia.de>
00033  * @author Jeff Segars <jeff@webempoweredchurch.org>
00034  * @package TYPO3
00035  * @subpackage t3lib
00036  */
00037 class t3lib_search_livesearch_queryParser {
00038 
00039     /**
00040      * @var string
00041      */
00042     protected $commandKey = '';
00043 
00044     /**
00045      * @var string
00046      */
00047     protected $tableName = '';
00048 
00049     /**
00050      * @var string
00051      */
00052     const COMMAND_KEY_INDICATOR = '#';
00053 
00054     /**
00055      * @var string
00056      */
00057     const COMMAND_SPLIT_INDICATOR = ':';
00058 
00059     /**
00060      * Retrive the validated command key
00061      *
00062      * @return string Command name
00063      */
00064     protected function extractKeyFromQuery($query) {
00065         $keyAndValue = substr($query, 1);
00066         $key = explode(':', $keyAndValue);
00067         $this->commandKey = $key[0];
00068     }
00069 
00070     /**
00071      * Extract the search value from the full search query which contains also the command part.
00072      *
00073      * @param string $query For example #news:weather
00074      * @return string The extracted search value
00075      */
00076     public function getSearchQueryValue($query) {
00077         $this->extractKeyFromQuery($query);
00078         return str_replace(self::COMMAND_KEY_INDICATOR . $this->commandKey . self::COMMAND_SPLIT_INDICATOR, '', $query);
00079     }
00080 
00081     /**
00082      * Find the registerd table command and retrieve the matching table name.
00083      *
00084      * @param string $query
00085      * @return string Database Table name
00086      */
00087     public function getTableNameFromCommand($query) {
00088         $tableName = '';
00089         $this->extractKeyFromQuery($query);
00090         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch']) && array_key_exists($this->commandKey, $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'])) {
00091             $tableName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'][$this->commandKey];
00092         }
00093 
00094         return $tableName;
00095     }
00096 
00097     /**
00098      * Verify if an given query contains a page jump command.
00099      *
00100      * @param string $query A valid value looks like '#14'
00101      * @return integer
00102      */
00103     public function getId($query) {
00104         return str_replace(self::COMMAND_KEY_INDICATOR, '', $query);
00105     }
00106 
00107     /**
00108      * Verify if a given query contains a page jump command.
00109      *
00110      * @param string $query A valid value looks like '#14'
00111      * @return boolean
00112      */
00113     public function isValidPageJump($query) {
00114         $isValid = FALSE;
00115 
00116         if (preg_match('~^#(\d)+$~', $query)) {
00117             $isValid = TRUE;
00118         }
00119 
00120         return $isValid;
00121     }
00122 
00123     /**
00124      * Verify if an given query contains an registered command key.
00125      *
00126      * @param string $query
00127      * @return boolean
00128      */
00129     public function isValidCommand($query) {
00130         $isValid = FALSE;
00131         if (strpos($query, self::COMMAND_KEY_INDICATOR) === 0 &&
00132             strpos($query, self::COMMAND_SPLIT_INDICATOR) > 1 &&
00133             $this->getTableNameFromCommand($query)) {
00134             $isValid = TRUE;
00135         }
00136 
00137         return $isValid;
00138     }
00139 
00140     /**
00141      * Gets the command for the given table.
00142      *
00143      * @param string $tableName The table to find a command for.
00144      * @return string
00145      */
00146     public function getCommandForTable($tableName) {
00147         $commandArray = array_keys($GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'], $tableName);
00148         if (is_array($commandArray)) {
00149             $command = $commandArray[0];
00150         } else {
00151             $command = FALSE;
00152         }
00153 
00154         return $command;
00155     }
00156 
00157     /**
00158      * Gets the page jump command for a given query.
00159      *
00160      * @param string $query
00161      * @return string
00162      */
00163     public function getCommandForPageJump($query) {
00164         if ($this->isValidPageJump($query)) {
00165             $command = $this->getCommandForTable('pages');
00166             $id = $this->getId($query);
00167 
00168             $resultQuery = self::COMMAND_KEY_INDICATOR . $command . self::COMMAND_SPLIT_INDICATOR . $id;
00169         } else {
00170             $resultQuery = FALSE;
00171         }
00172 
00173         return $resultQuery;
00174     }
00175 }
00176 
00177 ?>