openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

SearchAction.class.php (6904B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 use cms\model\Acl;
      6 use cms\model\Project;
      7 use cms\model\User;
      8 use cms\model\Value;
      9 use cms\model\Template;
     10 use cms\model\BaseObject;
     11 use cms\model\File;
     12 
     13 
     14 
     15 use Session;
     16 use \Html;
     17 
     18 
     19 
     20 // OpenRat Content Management System
     21 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
     22 //
     23 // This program is free software; you can redistribute it and/or
     24 // modify it under the terms of the GNU General Public License
     25 // as published by the Free Software Foundation; either version 2
     26 // of the License, or (at your option) any later version.
     27 //
     28 // This program is distributed in the hope that it will be useful,
     29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     31 // GNU General Public License for more details.
     32 //
     33 // You should have received a copy of the GNU General Public License
     34 // along with this program; if not, write to the Free Software
     35 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     36 
     37 
     38 define('SEARCH_FLAG_ID'         , 1);
     39 define('SEARCH_FLAG_NAME'       , 2);
     40 define('SEARCH_FLAG_FILENAME'   , 4);
     41 define('SEARCH_FLAG_DESCRIPTION', 8);
     42 define('SEARCH_FLAG_VALUE'      ,16);
     43 
     44 
     45 /**
     46  * Action-Klasse fuer die Suchfunktion.
     47  * 
     48  * @author $Author$
     49  * @version $Revision$
     50  * @package openrat.actions
     51  */
     52 class SearchAction extends BaseAction
     53 {
     54 	public $security = Action::SECURITY_USER;
     55 
     56 	
     57 	/**
     58 	 * leerer Kontruktor
     59 	 */
     60 	function __construct()
     61 	{
     62         parent::__construct();
     63 	}
     64 
     65 
     66 	
     67 	public function editView()
     68 	{
     69 		$user = Session::getUser();
     70 		$this->setTemplateVar( 'users'     ,User::listAll() );
     71 		$this->setTemplateVar( 'act_userid',$user->userid   );
     72 	}
     73 	
     74 	/**
     75 	 * Durchf?hren der Suche
     76 	 * und Anzeige der Ergebnisse
     77 	 */
     78 	public function resultView()
     79 	{
     80 		$suchText    = $this->getRequestVar('text');
     81 		$searchFlags = 0;
     82 		
     83 		if	( $this->hasRequestVar('id'         ) ) $searchFlags |= SEARCH_FLAG_ID;
     84 		if	( $this->hasRequestVar('filename'   ) ) $searchFlags |= SEARCH_FLAG_FILENAME;
     85 		if	( $this->hasRequestVar('name'       ) ) $searchFlags |= SEARCH_FLAG_NAME;
     86 		if	( $this->hasRequestVar('description') ) $searchFlags |= SEARCH_FLAG_DESCRIPTION;
     87 		if	( $this->hasRequestVar('content'    ) ) $searchFlags |= SEARCH_FLAG_VALUE;
     88 			
     89 		$this->performSearch($suchText, $searchFlags);
     90 
     91 				/*
     92 			case 'lastchange_user':
     93 				$e = new Value();
     94 				
     95 				$language = Session::getProjectLanguage();
     96 				$e->languageid = $language->languageid;
     97 				
     98 				$listObjectIds = $e->getObjectIdsByLastChangeUserId( $this->getRequestVar('userid') );
     99 				break;
    100 		}*/
    101 	}
    102 	
    103 	
    104 	
    105 	/**
    106 	 * Durchf?hren der Suche
    107 	 * und Anzeige der Ergebnisse
    108 	 */
    109 	public function quicksearchView()
    110 	{
    111 		global $conf;
    112 
    113 		$text = $this->getRequestVar('search');
    114 		
    115 		$flag        = $conf['search']['quicksearch']['flag'];
    116 		$searchFlags = 0;
    117 		if	( $flag['id'         ] ) $searchFlags |= SEARCH_FLAG_ID; 
    118 		if	( $flag['name'       ] ) $searchFlags |= SEARCH_FLAG_NAME; 
    119 		if	( $flag['filename'   ] ) $searchFlags |= SEARCH_FLAG_FILENAME; 
    120 		if	( $flag['description'] ) $searchFlags |= SEARCH_FLAG_DESCRIPTION; 
    121 		if	( $flag['content'    ] ) $searchFlags |= SEARCH_FLAG_VALUE;
    122 		 
    123 		$this->performSearch($text, $searchFlags);
    124 	}
    125 	
    126 	
    127 	
    128 	/**
    129 	 * Durchf?hren der Suche
    130 	 * und Anzeige der Ergebnisse
    131 	 */
    132 	private function performSearch($searchText, $searchFlag)
    133 	{
    134 		$listObjectIds   = array();
    135 		$listTemplateIds = array();
    136 	
    137         $resultList = array();
    138 
    139 
    140         if	( $searchFlag & SEARCH_FLAG_ID )
    141         {
    142             if   ( BaseObject::available( intval($searchText) ) )
    143                 $listObjectIds[] = intval( $searchText );
    144 
    145             if  ( $this->userIsAdmin() ) {
    146 
    147                 $user = new User( intval($searchText) );
    148 
    149                 try {
    150                     $user->load();
    151 
    152                     $userResult = array( 'url'  => '',
    153                         'type' => 'user',
    154                         'id' => $user->userid,
    155                         'name' => $user->name,
    156                         'desc' => $user->desc,
    157                         'lastchange_date' => 0 );
    158                     $resultList[] = $userResult;
    159                 }
    160                 catch( \ObjectNotFoundException $e) {
    161                     ; // userid is unknown
    162                 }
    163             }
    164         }
    165 
    166         if	( $searchFlag & SEARCH_FLAG_NAME )
    167         {
    168             if  ( $this->userIsAdmin() ) {
    169 
    170                 $user = User::loadWithName($searchText);
    171                 if (is_object($user)) {
    172                     $userResult = array('url' => '',
    173                         'type' => 'user',
    174                         'id' => $user->userid,
    175                         'name' => $user->name,
    176                         'desc' => $user->desc,
    177                         'lastchange_date' => 0);
    178                     $resultList[] = $userResult;
    179                 }
    180             }
    181 
    182             $listObjectIds += BaseObject::getObjectIdsByName( $searchText );
    183         }
    184 
    185         if	( $searchFlag & SEARCH_FLAG_DESCRIPTION )
    186         {
    187             $listObjectIds += BaseObject::getObjectIdsByDescription( $searchText );
    188         }
    189 
    190         if	( $searchFlag & SEARCH_FLAG_FILENAME )
    191         {
    192             $listObjectIds += BaseObject::getObjectIdsByFilename( $searchText );
    193 
    194             $listObjectIds += File::getObjectIdsByExtension( $searchText );
    195         }
    196 
    197         // Inhalte durchsuchen
    198         if	( $searchFlag & SEARCH_FLAG_VALUE )
    199         {
    200             $e = new Value();
    201             $listObjectIds += $e->getObjectIdsByValue( $searchText );
    202 
    203             $listTemplateIds += Template::getTemplateIdsByValue( $searchText );
    204         }
    205 
    206         $resultList = array_merge( $resultList, $this->explainResult( $listObjectIds, $listTemplateIds ) );
    207 
    208         $this->setTemplateVar( 'result',$resultList );
    209     }
    210 	
    211 	
    212 	/**
    213 	 *
    214 	 */
    215 	private function explainResult( $listObjectIds, $listTemplateIds )
    216 	{
    217 		$resultList = array();
    218 	
    219 		foreach( $listObjectIds as $objectid )
    220 		{
    221 			$o = new BaseObject( $objectid );
    222 			$o->load();
    223             if ($o->hasRight( Acl::ACL_READ ))
    224                 $resultList[] = array(
    225                     'id'              => $objectid,
    226                     'type'            => $o->getType(),
    227                     'name'            => $o->name,
    228                     'lastchange_date' => $o->lastchangeDate,
    229                     'desc'            => $o->desc
    230                 );
    231 		}
    232 	
    233 		foreach( $listTemplateIds as $templateid )
    234 		{
    235 			$t = new Template( $templateid );
    236 			$t->load();
    237 			$p = new Project( $t->projectid );
    238 			$o = new BaseObject( $p->getRootObjectId() );
    239 			if ($o->hasRight( Acl::ACL_READ ))
    240                 $resultList[] = array(
    241                     'id'  => $templateid,
    242                     'type'=> 'template',
    243                     'name'=> $t->name,
    244                     'desc'=> '',
    245                     'lastchange_date'=> 0
    246                 );
    247 		}
    248 	
    249 		return $resultList;
    250 	}
    251 	
    252 }
    253 
    254 ?>