Changeset 1430


Ignore:
Timestamp:
03/06/08 06:15:30 (4 years ago)
Author:
tinyau
Message:

Added 'id' as the valid filter and some code style changes. Fixed the problem of using id in custom permalink.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/htdocs/system/classes/theme.php

    r1392 r1430  
    1919        /** 
    2020         * We build the Post filters by analyzing the handler_var 
    21                 * data which is assigned to the handler ( by the Controller and 
    22                 * also, optionally, by the Theme ) 
    23                 */ 
     21         * data which is assigned to the handler ( by the Controller and 
     22         * also, optionally, by the Theme ) 
     23         */ 
    2424        public $valid_filters= array( 
    25          'content_type', 
     25                'content_type', 
    2626                'slug', 
    2727                'status', 
     
    3636                'nolimit', 
    3737                'fetch_fn', 
     38                'id', 
    3839        ); 
    3940 
     
    5051         * @param theme_dir       ( optional ) specify a theme directory 
    5152         */ 
    52         public function __construct($themedata) 
     53        public function __construct( $themedata ) 
    5354        { 
    5455                $this->name= $themedata->name; 
     
    7071        public function info( $theme ) 
    7172        { 
    72                 $xml_file= Site::get_path('user') . '/themes/' . $theme . '/theme.xml'; 
     73                $xml_file= Site::get_path( 'user' ) . '/themes/' . $theme . '/theme.xml'; 
    7374                if ( $xml_content= file_get_contents( $xml_file ) ) { 
    7475                        $theme_data= new SimpleXMLElement(  $xml_file ); 
     
    101102        { 
    102103                $handler= Controller::get_handler(); 
    103                 if( isset( $handler ) ) { 
    104                         Plugins::act('add_template_vars', $this, Controller::get_handler()->handler_vars); 
     104                if ( isset( $handler ) ) { 
     105                        Plugins::act( 'add_template_vars', $this, Controller::get_handler()->handler_vars ); 
    105106                } 
    106107        } 
     
    110111         * @param array $template_list The list of templates to search for 
    111112         */ 
    112         public function display_fallback($template_list) 
    113         { 
    114                 foreach( $template_list as $template ) { 
    115                         if( $this->template_exists( $template ) ) { 
     113        public function display_fallback( $template_list ) 
     114        { 
     115                foreach ( $template_list as $template ) { 
     116                        if ( $this->template_exists( $template ) ) { 
    116117                                $this->display( $template ); 
    117118                                return true; 
     
    150151                $where_filters= array(); 
    151152                $where_filters= array_intersect_key( Controller::get_handler()->handler_vars, array_flip( $this->valid_filters ) ); 
    152                 //$where_filters['status']= Post::status('published'); 
    153                 if (array_key_exists('tag', $where_filters)) { 
     153                //$where_filters['status']= Post::status( 'published' ); 
     154                if ( array_key_exists( 'tag', $where_filters ) ) { 
    154155                        $where_filters['tag_slug']=  $where_filters['tag']; 
    155                         unset($where_filters['tag']); 
     156                        unset( $where_filters['tag'] ); 
    156157                } 
    157158                if ( User::identify() ) { 
    158                         $where_filters['status']= Post::status('any'); 
     159                        $where_filters['status']= Post::status( 'any' ); 
    159160                } 
    160161                else { 
    161                         $where_filters['status']= Post::status('published'); 
    162                 } 
    163  
    164                 if( !isset( $posts ) ) { 
     162                        $where_filters['status']= Post::status( 'published' ); 
     163                } 
     164 
     165                if ( !isset( $posts ) ) { 
    165166                        $user_filters= Plugins::filter( 'template_user_filters', $user_filters ); 
    166167                        $user_filters= array_intersect_key( $user_filters, array_flip( $this->valid_filters ) ); 
     
    180181                        $type= $types[$post->content_type]; 
    181182                } 
    182                 elseif( $posts === false ) { 
    183                         $fallback= array('404'); 
     183                elseif ( $posts === false ) { 
     184                        $fallback= array( '404' ); 
    184185                        header( 'HTTP/1.0 404 Not Found' ); 
    185186                        // Replace template variables with the 404 rewrite rule 
     
    194195                        // Default fallbacks based on the number of posts 
    195196                        $fallback= array( '{$type}.{$id}', '{$type}.{$slug}', '{$type}.tag.{$posttag}' ); 
    196                         if( count( $posts ) > 1 ) { 
     197                        if ( count( $posts ) > 1 ) { 
    197198                                $fallback[]= '{$type}.multiple'; 
    198199                                $fallback[]= 'multiple'; 
     
    204205                } 
    205206 
    206                 $searches= array('{$id}','{$slug}','{$year}','{$month}','{$day}','{$type}','{$tag}',); 
     207                $searches= array( '{$id}','{$slug}','{$year}','{$month}','{$day}','{$type}','{$tag}', ); 
    207208                $replacements= array( 
    208                         (isset($post) && $post instanceof Post)?$post->id:'-', 
    209                         (isset($post) && $post instanceof Post)?$post->slug:'-', 
    210                         isset($year)?$year:'-', 
    211                         isset($month)?$month:'-', 
    212                         isset($day)?$day:'-', 
    213                         isset($type)?$type:'-', 
    214                         isset($tag)?$tag:'-', 
     209                        ( isset( $post ) && $post instanceof Post ) ? $post->id : '-', 
     210                        ( isset( $post ) && $post instanceof Post ) ? $post->slug : '-', 
     211                        isset( $year ) ? $year : '-', 
     212                        isset( $month ) ? $month : '-', 
     213                        isset( $day ) ? $day : '-', 
     214                        isset( $type ) ? $type : '-', 
     215                        isset( $tag ) ? $tag : '-', 
    215216                ); 
    216217                $fallback[]= 'home'; 
    217218                $fallback= Plugins::filter( 'template_fallback', $fallback ); 
    218                 $fallback= array_unique( str_replace($searches, $replacements, $fallback) ); 
    219                 for($z = 0; $z < count($fallback); $z++) { 
    220                         if( (strpos($fallback[$z], '{$posttag}') !== false) && (isset($post)) && ($post instanceof Post)) { 
     219                $fallback= array_unique( str_replace( $searches, $replacements, $fallback ) ); 
     220                for ( $z= 0; $z < count( $fallback ); $z++ ) { 
     221                        if ( ( strpos( $fallback[$z], '{$posttag}' ) !== false ) && ( isset( $post ) ) && ( $post instanceof Post ) ) { 
    221222                                $replacements= array(); 
    222                                 if( $alltags= $post->tags ) { 
    223                                         foreach($alltags as $tag_slug => $tag_text ) { 
    224                                                 $replacements[] = str_replace('{$posttag}', $tag_slug, $fallback[$z]); 
     223                                if ( $alltags= $post->tags ) { 
     224                                        foreach ( $alltags as $tag_slug => $tag_text ) { 
     225                                                $replacements[]= str_replace( '{$posttag}', $tag_slug, $fallback[$z] ); 
    225226                                        } 
    226                                         array_splice($fallback, $z, 1, $replacements); 
     227                                        array_splice( $fallback, $z, 1, $replacements ); 
    227228                                } 
    228229                                else { 
     
    248249                // Makes sure home displays only entries 
    249250                $default_filters= array( 
    250                  'content_type' => Post::type('entry'), 
     251                        'content_type' => Post::type( 'entry' ), 
    251252                ); 
    252253 
     
    289290                $commenter_email= ''; 
    290291                $commenter_url= ''; 
    291                 $commenter_content = ''; 
    292                 $user = User::identify(); 
     292                $commenter_content= ''; 
     293                $user= User::identify(); 
    293294                if ( isset( $_SESSION['comment'] ) ) { 
    294                         $details= Session::get_set('comment'); 
     295                        $details= Session::get_set( 'comment' ); 
    295296                        $commenter_name= $details['name']; 
    296297                        $commenter_email= $details['email']; 
    297298                        $commenter_url= $details['url']; 
    298                         $commenter_content = $details['content']; 
     299                        $commenter_content= $details['content']; 
    299300                } 
    300301                elseif ( $user ) { 
    301302                        $commenter_name= $user->username; 
    302303                        $commenter_email= $user->email; 
    303                         $commenter_url= Site::get_url('habari'); 
     304                        $commenter_url= Site::get_url( 'habari' ); 
    304305                } 
    305306                elseif ( isset( $_COOKIE[$cookie] ) ) { 
     
    307308                } 
    308309 
    309                 $this->commenter_name = $commenter_name; 
    310                 $this->commenter_email = $commenter_email; 
    311                 $this->commenter_url = $commenter_url; 
    312                 $this->commenter_content = $commenter_content; 
    313  
    314                 $this->comments_require_id = Options::get('comments_require_id'); 
     310                $this->commenter_name= $commenter_name; 
     311                $this->commenter_email= $commenter_email; 
     312                $this->commenter_url= $commenter_url; 
     313                $this->commenter_content= $commenter_content; 
     314 
     315                $this->comments_require_id= Options::get( 'comments_require_id' ); 
    315316 
    316317                return $this->act_display( $paramarray ); 
     
    331332                // Makes sure home displays only entries 
    332333                $default_filters= array( 
    333                  'content_type' => Post::type('entry'), 
     334                        'content_type' => Post::type( 'entry' ), 
    334335                ); 
    335336 
     
    346347        { 
    347348                $handler_vars= Controller::get_handler()->handler_vars; 
    348                 $y = isset( $handler_vars['year'] ); 
    349                 $m = isset( $handler_vars['month'] ); 
    350                 $d = isset( $handler_vars['day'] ); 
    351  
    352                 if($y&&$m&&$d) $paramarray['fallback'][]= 'year.{$year}.month.{$month}.day.{$day}'; 
    353                 if($y&&$m&&$d) $paramarray['fallback'][]= 'year.month.day'; 
    354                 if($m&&$d) $paramarray['fallback'][]= 'month.{$month}.day.{$day}'; 
    355                 if($y&&$m) $paramarray['fallback'][]= 'year.{$year}.month.{$month}'; 
    356                 if($y&&$d) $paramarray['fallback'][]= 'year.{$year}.day.{$day}'; 
    357                 if($m&&$d) $paramarray['fallback'][]= 'month.day'; 
    358                 if($y&&$d) $paramarray['fallback'][]= 'year.day'; 
    359                 if($y&&$m) $paramarray['fallback'][]= 'year.month'; 
    360                 if($m) $paramarray['fallback'][]= 'month.{$month}'; 
    361                 if($d) $paramarray['fallback'][]= 'day.{$day}'; 
    362                 if($y) $paramarray['fallback'][]= 'year.{$year}'; 
    363                 if($y) $paramarray['fallback'][]= 'year'; 
    364                 if($m) $paramarray['fallback'][]= 'month'; 
    365                 if($d) $paramarray['fallback'][]= 'day'; 
     349                $y= isset( $handler_vars['year'] ); 
     350                $m= isset( $handler_vars['month'] ); 
     351                $d= isset( $handler_vars['day'] ); 
     352 
     353                if ( $y && $m && $d ) { 
     354                        $paramarray['fallback'][]= 'year.{$year}.month.{$month}.day.{$day}'; 
     355                } 
     356                if ( $y && $m && $d) { 
     357                        $paramarray['fallback'][]= 'year.month.day'; 
     358                } 
     359                if ( $m && $d ) { 
     360                        $paramarray['fallback'][]= 'month.{$month}.day.{$day}'; 
     361                } 
     362                if ( $y && $m ) { 
     363                        $paramarray['fallback'][]= 'year.{$year}.month.{$month}'; 
     364                } 
     365                if ( $y && $d ) { 
     366                        $paramarray['fallback'][]= 'year.{$year}.day.{$day}'; 
     367                } 
     368                if ( $m && $d ) { 
     369                        $paramarray['fallback'][]= 'month.day'; 
     370                } 
     371                if ( $y && $d ) { 
     372                        $paramarray['fallback'][]= 'year.day'; 
     373                } 
     374                if ( $y && $m ) { 
     375                        $paramarray['fallback'][]= 'year.month'; 
     376                } 
     377                if ( $m ) { 
     378                        $paramarray['fallback'][]= 'month.{$month}'; 
     379                } 
     380                if ( $d ) { 
     381                        $paramarray['fallback'][]= 'day.{$day}'; 
     382                } 
     383                if ( $y ) { 
     384                        $paramarray['fallback'][]= 'year.{$year}'; 
     385                } 
     386                if ( $y ) { 
     387                        $paramarray['fallback'][]= 'year'; 
     388                } 
     389                if ( $m ) { 
     390                        $paramarray['fallback'][]= 'month'; 
     391                } 
     392                if ( $d ) { 
     393                        $paramarray['fallback'][]= 'day'; 
     394                } 
    366395                $paramarray['fallback'][]= 'multiple'; 
    367396                $paramarray['fallback'][]= 'home'; 
     
    413442                $this->add_template_vars(); 
    414443 
    415                 if( isset( Controller::get_handler()->handler_vars ) ) { 
     444                if ( isset( Controller::get_handler()->handler_vars ) ) { 
    416445                        foreach ( Controller::get_handler()->handler_vars as $key => $value ) { 
    417446                                $this->assign( $key, $value ); 
     
    427456                $this->add_template_vars(); 
    428457 
    429                 if( isset( Controller::get_handler()->handler_vars ) ) { 
     458                if ( isset( Controller::get_handler()->handler_vars ) ) { 
    430459                        foreach ( Controller::get_handler()->handler_vars as $key => $value ) { 
    431460                                $this->assign( $key, $value ); 
     
    448477         * Aggregates and echos the additional header code by combining Plugins and Stack calls. 
    449478         */ 
    450         public function theme_header( $theme ) { 
    451                 $output = Stack::get( 'template_stylesheet', '<link rel="stylesheet" type="text/css" href="%s" media="%s">'."\r\n" ); 
    452                 $output .= Stack::get( 'template_header_javascript', '<script src="%s" type="text/javascript"></script>'."\r\n" ); 
     479        public function theme_header( $theme ) 
     480        { 
     481                $output= Stack::get( 'template_stylesheet', '<link rel="stylesheet" type="text/css" href="%s" media="%s">'."\r\n" ); 
     482                $output.= Stack::get( 'template_header_javascript', '<script src="%s" type="text/javascript"></script>'."\r\n" ); 
    453483                return $output; 
    454484        } 
     
    457487         * Aggregates and echos the additional footer code by combining Plugins and Stack calls. 
    458488         */ 
    459         public function theme_footer( $theme ) { 
     489        public function theme_footer( $theme ) 
     490        { 
    460491                Plugins::act( 'template_footer', $theme ); 
    461                 $output = Stack::get( 'template_footer_javascript', ' <script src="%s" type="text/javascript"></script>'."\r\n" ); 
     492                $output= Stack::get( 'template_footer_javascript', ' <script src="%s" type="text/javascript"></script>'."\r\n" ); 
    462493                return $output; 
    463494        } 
     
    473504        { 
    474505                $matched_rule= URL::get_matched_rule(); 
    475                 if(is_object($matched_rule)) { 
     506                if ( is_object( $matched_rule ) ) { 
    476507                        // This is not a 404 
    477                         $rulename = $matched_rule->name; 
     508                        $rulename= $matched_rule->name; 
    478509                } 
    479510                else { 
    480511                        // If this is a 404 and no rewrite rule matched the request 
    481                         $rulename = ''; 
     512                        $rulename= ''; 
    482513                } 
    483514                switch ( $rulename ) { 
    484515                        case 'display_entry': 
    485516                        case 'display_page': 
    486                                 return URL::get( 'atom_entry', array( 'slug' => Controller::get_var('slug') ) ); 
     517                                return URL::get( 'atom_entry', array( 'slug' => Controller::get_var( 'slug' ) ) ); 
    487518                                break; 
    488519                        case 'display_entries_by_tag': 
    489                                 return URL::get( 'atom_feed_tag', array( 'tag' => Controller::get_var('tag') ) ); 
     520                                return URL::get( 'atom_feed_tag', array( 'tag' => Controller::get_var( 'tag' ) ) ); 
    490521                                break; 
    491522                        case 'display_home': 
     
    552583        public function __call( $function, $params ) 
    553584        { 
    554                 if(strpos($function, 'act_') === 0) { 
     585                if ( strpos( $function, 'act_' ) === 0 ) { 
    555586                        // The first parameter is an array, get it 
    556                         if(count($params) > 0) { 
    557                                 list($user_filters)= $params; 
     587                        if ( count( $params ) > 0 ) { 
     588                                list( $user_filters )= $params; 
    558589                        } 
    559590                        else { 
    560591                                $user_filters= array(); 
    561592                        } 
    562                         $action = substr($function, 4); 
    563                         Plugins::act('theme_action', $action, $this, $user_filters); 
     593                        $action= substr( $function, 4 ); 
     594                        Plugins::act( 'theme_action', $action, $this, $user_filters ); 
    564595                } 
    565596                else { 
    566                         $purposed = 'output'; 
    567                         if(preg_match('%^(.*)_(return|end)$%', $function, $matches)) { 
    568                                 $purposed = $matches[2]; 
    569                                 $function = $matches[1]; 
    570                         } 
    571                         array_unshift($params, $function, $this); 
    572                         $result = call_user_func_array(array('Plugins', 'theme'), $params); 
    573                         switch($purposed) { 
     597                        $purposed= 'output'; 
     598                        if ( preg_match( '%^(.*)_(return|end)$%', $function, $matches ) ) { 
     599                                $purposed= $matches[2]; 
     600                                $function= $matches[1]; 
     601                        } 
     602                        array_unshift( $params, $function, $this ); 
     603                        $result= call_user_func_array( array( 'Plugins', 'theme' ), $params ); 
     604                        switch( $purposed ) { 
    574605                                case 'return': 
    575606                                        return $result; 
    576607                                case 'end': 
    577                                         echo end($result); 
    578                                         return end($result); 
     608                                        echo end( $result ); 
     609                                        return end( $result ); 
    579610                                default: 
    580                                         $output = implode('', (array)$result); 
     611                                        $output= implode( '', ( array ) $result ); 
    581612                                        echo $output; 
    582613                                        return $output; 
     
    584615                } 
    585616        } 
    586  
    587617} 
    588  
    589  
    590  
    591618?> 
Note: See TracChangeset for help on using the changeset viewer.