openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

SearchAction.class.php (5935B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 use cms\base\Configuration as C;
      6 use cms\model\Content;
      7 use cms\model\Permission;
      8 use cms\model\BaseObject;
      9 use cms\model\File;
     10 use cms\model\Project;
     11 use cms\model\Template;
     12 use cms\model\User;
     13 use cms\model\Value;
     14 use util\Session;
     15 
     16 
     17 // OpenRat Content Management System
     18 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
     19 //
     20 // This program is free software; you can redistribute it and/or
     21 // modify it under the terms of the GNU General Public License
     22 // as published by the Free Software Foundation; either version 2
     23 // of the License, or (at your option) any later version.
     24 //
     25 // This program is distributed in the hope that it will be useful,
     26 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     27 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     28 // GNU General Public License for more details.
     29 //
     30 // You should have received a copy of the GNU General Public License
     31 // along with this program; if not, write to the Free Software
     32 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     33 
     34 
     35 
     36 
     37 /**
     38  * Action-Klasse fuer die Suchfunktion.
     39  * 
     40  * @author $Author$
     41  * @version $Revision$
     42  * @package openrat.actions
     43  */
     44 class SearchAction extends BaseAction
     45 {
     46 	const FLAG_ID          =  1;
     47 	const FLAG_NAME        =  2;
     48 	const FLAG_FILENAME    =  4;
     49 	const FLAG_DESCRIPTION =  8;
     50 	const FLAG_VALUE       = 16;
     51 
     52 	/**
     53 	 * leerer Kontruktor
     54 	 */
     55 	function __construct()
     56 	{
     57         parent::__construct();
     58 	}
     59 
     60 
     61 	
     62 	public function editView()
     63 	{
     64 		$user = $this->currentUser;
     65 		$this->setTemplateVar( 'users'     ,User::listAll() );
     66 		$this->setTemplateVar( 'act_userid',$user->userid   );
     67 	}
     68 	
     69 	/**
     70 	 * Query the search
     71 	 *
     72 	 * @param $searchText string search query text
     73 	 * @param $searchFlag int field selector
     74 	 */
     75 	protected function performSearch($searchText, $searchFlag)
     76 	{
     77 		$listObjectIds   = array();
     78 		$listTemplateIds = array();
     79 	
     80         $resultList = array();
     81 
     82 
     83         if	( $searchFlag & self::FLAG_ID )
     84         {
     85             if   ( BaseObject::available( intval($searchText) ) ) {
     86 
     87 				$listObjectIds[intval($searchText)] = intval( $searchText );
     88 			}
     89 
     90             if  ( $this->userIsAdmin() ) {
     91 
     92                 $user = new User( intval($searchText) );
     93 
     94                 try {
     95                     $user->load();
     96 
     97                     $userResult = array( 'url'  => '',
     98                         'type' => 'user',
     99                         'id' => $user->userid,
    100                         'name' => $user->fullname,
    101                         'desc' => $user->desc,
    102                         'lastchange_date' => 0 );
    103                     $resultList['u'.$user->userid] = $userResult;
    104                 }
    105                 catch( \util\exception\ObjectNotFoundException $content) {
    106                     ; // userid is unknown
    107                 }
    108             }
    109         }
    110 
    111         if	( $searchFlag & self::FLAG_NAME )
    112         {
    113             if  ( $this->userIsAdmin() ) {
    114 
    115                 $user = User::loadWithName($searchText,User::AUTH_TYPE_INTERNAL);
    116                 if (is_object($user)) {
    117                     $userResult = array('url' => '',
    118                         'type' => 'user',
    119                         'id' => $user->userid,
    120                         'name' => $user->fullname,
    121                         'desc' => $user->desc,
    122                         'lastchange_date' => 0);
    123                     $resultList['u'.$user->userid] = $userResult;
    124                 }
    125             }
    126 
    127             $listObjectIds += BaseObject::getObjectIdsByName( $searchText );
    128         }
    129 
    130         if	( $searchFlag & self::FLAG_DESCRIPTION )
    131         {
    132             $listObjectIds += BaseObject::getObjectIdsByDescription( $searchText );
    133         }
    134 
    135         if	( $searchFlag & self::FLAG_FILENAME )
    136         {
    137             $listObjectIds += BaseObject::getObjectIdsByFilename( $searchText );
    138 
    139             $listObjectIds += File::getObjectIdsByExtension( $searchText );
    140         }
    141 
    142         // Inhalte durchsuchen
    143         if	( $searchFlag & self::FLAG_VALUE )
    144         {
    145             $listObjectIds += Content::getObjectIdsByValue( $searchText );
    146 
    147             $listTemplateIds += Template::getTemplateIdsByValue( $searchText );
    148         }
    149 
    150         $resultList = array_merge( $resultList, $this->explainResult( $listObjectIds, $listTemplateIds ) );
    151 
    152         $this->setTemplateVar( 'result',$resultList );
    153     }
    154 
    155 
    156 	/**
    157 	 * Transforms the found objects into an array of search results.
    158 	 *
    159 	 * @param $listObjectIds Object ids
    160 	 * @param $listTemplateIds template ids
    161 	 * @return array
    162 	 * @throws \util\exception\ObjectNotFoundException
    163 	 */
    164 	private function explainResult( $listObjectIds, $listTemplateIds )
    165 	{
    166 		$resultList = array();
    167 	
    168 		foreach( $listObjectIds as $objectid )
    169 		{
    170 			$o = new BaseObject( $objectid );
    171 			$o->load();
    172             if ($o->hasRight( Permission::ACL_READ ))
    173                 $resultList['o'.$objectid] = [
    174                     'id'              => $objectid,
    175                     'type'            => $o->getType(),
    176                     'name'            => $o->filename,
    177                     'lastchange_date' => $o->lastchangeDate,
    178                     'desc'            => ''
    179                 ];
    180 		}
    181 	
    182 		foreach( $listTemplateIds as $templateid )
    183 		{
    184 			$t = new Template( $templateid );
    185 			$t->load();
    186 			$p = new Project( $t->projectid );
    187 			$rootObject = new BaseObject( $p->getRootObjectId() );
    188 			if ($rootObject->hasRight( Permission::ACL_PROP )) // only project admins may read the templates
    189                 $resultList['t'.$templateid] = [
    190                     'id'  => $templateid,
    191                     'type'=> 'template',
    192                     'name'=> $t->name,
    193                     'desc'=> '',
    194                     'lastchange_date'=> 0
    195                 ];
    196 		}
    197 	
    198 		return $resultList;
    199 	}
    200 
    201 
    202 	/**
    203 	 * The search is executable for all users.
    204 	 * But the search results are filtered.
    205 	 *
    206 	 * @return true
    207 	 */
    208 	public function checkAccess() {
    209 		return true;
    210 	}
    211 }