SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35
  1. #26
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, I've just made a quick little test of my old php4 and new php5 code
    I used memory_get_usage, i don't know how accurate this function is, but when i tested the same page, I had the following results
    php4+pear+smarty: 2,4mb
    php5+PDO: 380kb

    That's pretty nice result... I'm not going back to pear and smarty for sure.

  2. #27
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know which one is the worst, PEAR or Smarty?

    Proberly both...

  3. #28
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more question

    I have a lot of pages with the same html structure.
    So, the template looks like for example
    [TABLE]
    [NAVIGATION]

    If I make one template for each page, it will be difficult to make changes (I would have to change a lot of files).

    So, the current navigation snippet may look like ($this refers to the controller, templates are rendered directly by the controller, not additional view classed)
    PHP Code:
    <?php if ($this->registry->NewsModel->countResultSets() > 1): ?>
    So, it calls the model directly.

    To make the template a generic one, I would have to move the Model call somewhere else. The question is "where" ?

    I could wrap the call to the model to some additional method in the appropriate controller (each page already has its own controller), like
    PHP Code:
    class NewsController {

        public function 
    getSets() {
            return 
    $this->registry->NewsModel->countResultSets();
        }

    and then the template would be changed to
    PHP Code:
    <?php if ($this->getSets() > 1): ?>
    Is this generally OK or I should do it in some other way? Thanks

  4. #29
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > and then the template would be changed to

    Well, no since the following conditional call is to the Controller which is wrong, since it should be to the Model component, as I understand MVC?

    Take a look at the following for example to see how I do it,

    PHP Code:
    // ...
    final class BodyActionHandler extends ActionHandler {
            public function 
    __construct() {    
                
    $this -> id 'body';
            }
            
            public function 
    executeIDataspace $nodesContext $context ) {
                
    $page = new PagedSetWebPage$nodes$context -> get'request' ), '/admin/users/action/show/' );
                
    $page -> addObserver( new UsersDomain$context -> get'connection' ) ) );
                
    $page -> render'en/admin/users/body.tpl' );
            }
        }
        
        final class 
    UsersDomain implements IAcceptee {
            private 
    $users null;
            
            public function 
    __construct$session ) {
                
    $this -> users $session -> create'usercollectiondependent' );
            }
            
            public function 
    getName() {
                return 
    'users';
            }
            
            public function 
    push$acceptable ) { 
                
    $total_rows $this -> users -> totalRows();
                
    $page $acceptable -> request() -> get'page' );
                
    $limit 15;
                
                if( 
    $page ) {
                    
    $page $page -1;
                }
                
                
    $this -> users -> addFilter( new DateFormatFilter'created' ) );
                
    $this -> users -> orderBy"order by u.created desc" );
                
    $this -> users -> limitBy"limit ".$page $limit.", ".$limit );
                
    $this -> users -> getAll();
                
                
    $acceptable -> import( new Parameters( array( 'users' => $this -> users -> getCollection() ) ) );
                
    $acceptable -> set'range'$total ceil$total_rows $limit ) );
            }
        } 
    And, the template in question,

    Code:
    <div class="heading">
    		<div class="heading" style="padding:0% 1%;width:98%;">
    			<span>
    				<h1>Users</h1>
    			</span>
    			<div>
    				<span class="column-spacer" style="padding:0% 4px;width:49%;text-align:left;">
    					<?php echo $this -> get( 'breadcrumb' ); ?>
    				</span>
    				<span class="column-spacer" style="width:50%;text-align:right;">
    					<ul>
    						<li><a href="/admin/users/action/search/" title="Search Users">Search</a></li>
    					<li><a href="/admin/users/action/create/" title="Create User">Create</a></li>
    					<li><a href="/admin/users/privileges/" title="Administer User Privileges">Privileges</a></li>
    					</ul>
    				</span>
    			</div>
    			
    			<div class="heading">
    				<div class="heading">
    					<div class="heading-cell" style="padding:0% 0.5%;width:9%;text-align:right;"><p>ID</p></div>
    					<div class="heading-cell" style="padding:0% 0.5%;width:29%;text-align:left;"><p>Activated</p></div>
    					<div class="heading-cell" style="padding:0% 0.5%;width:29%;text-align:left;"><p>Contact</p></div>
    					<div class="heading-cell" style="padding:0% 0.5%;width:29%;text-align:center;"><p>Actions</p></div>
    				</div>
    				
    				<div class="heading" style="margin:0%;">
    					<?php if( $this -> has( 'users' ) ): ?>
    					<?php foreach( $this -> get( 'users' ) as $user ): ?>
    						<div class="body-cell" style="padding:0% 0.5%;width:9%;text-align:right;"><p><?php echo $user -> get( 'id' ); ?></p></div>
    						<div class="body-cell" style="padding:0% 0.5%;width:29%;text-align:left;"><p><?php echo $user -> get( 'created' ); ?></p></div>
    						<div class="body-cell" style="padding:0% 0.5%;width:29%;text-align:left;"><p><a href="/admin/users/action/view/id/<?php echo $user -> get( 'id' ); ?>/"><?php echo $user -> get( 'email' ); ?></a></p></div>
    						<div class="body-cell" style="padding:0% 0.5%;width:29%;text-align:center;">
    							<div class="body-cell" style="margin:0%;width:50%;text-align:center;"><p><a href="/admin/users/action/modify/id/<?php echo $user -> get( 'id' ); ?>/">Modify</a></p></div>
    							<div class="body-cell" style="margin:0%;width:50%;text-align:center;"><p><a href="/admin/users/action/remove/id/<?php echo $user -> get( 'id' ); ?>/">Remove</a></p></div>
    						</div>
    					<?php endforeach; ?>
    					<?php else: ?>
    						<div class="body-cell" style="padding:0% 0.5%;width:99%;text-align:center;"><p>No records found in search.</p></div>
    					<?php endif; ?>
    				</div>
    				
    				<div class="heading" style="margin:0%;">
    					<div class="footing-cell" style="padding:0% 0.5%;width:99%;text-align:center;">
    						<p><?php echo $this -> previousPage(); ?> Page <?php echo $this -> currentPage(); ?> of <?php echo $this -> totalPages(); ?> <?php echo $this -> nextPage(); ?></p>
    					</div>
    				</div>
    			</div>
    			
    		</div>
    	</div>
    Does that help you?

  5. #30
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Does that help you?
    No It's a complex example and I don't know where to look at to get the answer to my question... There are many different objects doing 'something'...

  6. #31
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand how the example I posted is too complex? Sure there are a number of objects in use but they're all doing their own responsible thing

    What exactly do you or don't you follow? I'm sure I can post a Hello World example for you, or look over the previous posts I've made as I've posted a lot of script which I use today, and that the example I posted yersterday (still) depends on.

    I'll be back...

  7. #32
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Ok, I assume that the template you showed is a general template used with many pages. So I would ask, for example, about this call in your view: $this -> totalPages();
    Can you post what this method is doing? I'd like to see, how your view knows what to display. That is, where is the given model chosen, so that generic method totalPages always returns the proper amount.

    thanks!

  8. #33
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have any of the script on me at the moment, so I'll need to get back to you on that, however there is an interface, which has this class method to be supported by a Model object,

    PHP Code:
    // ...
    public function push$acceptable ) { ... } 
    The parameter in question - $acceptable - is your View, so it's a case of dumping your Model data to the View via this $acceptable parameter; That is how the View gets to know about your Model data in effect.

    The call you ask about, is in regards to the View it's self, since I just do a file inclusion - the template - from within a WebPage::render( $template ) for example? The Model that works on the paged results stores the required variables to the View when ready, via $acceptable

    I'll be sure to take some scripts with me the next time I'm on-line...

  9. #34
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is some more script for you to ponder over

    PHP Code:
    final class PagedSetWebPage extends Page {
            public function 
    __constructIDataspace $nodes$request$url ) {
                
    parent::__construct$nodes ); 
                
    $this -> request $request;
                
    $this -> set'url'$url );
            }
            
            public function 
    nextPage() {
                
    $current $this -> currentPage() +1;
                if( 
    $current <= $this -> totalPages() ) {
                    echo 
    '<a href="'.$this -> get'url' ).'page/'.( $current ).'/">Next</a>';
                } else {
                    echo 
    'Next';
                }
            }
            
            public function 
    currentPage() {
                return 
    $this -> request -> get'page' );
            }
            
            public function 
    previousPage() {
                
    $current $this -> currentPage() -1
                if( 
    $current ) {
                    echo 
    '<a href="'.$this -> get'url' ).'page/'.( $current ).'/">Prev</a>';
                } else {
                    echo 
    'Prev';
                }
            }
            
            public function 
    searchIndex() {
                return 
    $this -> currentPage() -1;
            }
            
            public function 
    totalPages() {
                return 
    $this -> get'range' );
            }
            
            public function 
    render$template ) {
                
                foreach( 
    $this -> observers as $observer ) {
                    
    $observer -> push$this );
                }
                
                include_once( 
    $template );
            }
        }

    interface 
    IAcceptee {
        
            
    /**
             *                                 Provides a method of implementation
             *                                of the Visitor pattern.
             *
             * @param     object $acceptable    Expected typeof object.
             * @return     void                None declared.
             */
             
            
    public function push$acceptable );
        }
        
        interface 
    IAcceptance {
        
            
    /**
             *                                Provides a generic manner to allow
             *                                the use of implementation of the Visitor
             *                                pattern.
             *
             * @param    object $acceptee    Expected typeof IAcceptee.
             * @return    void                None declared.
             */ 
             
            
    public function acceptIAcceptee $acceptee );
        } 
    And, just for the hell of it...

    PHP Code:
    final class SecurityFactors {
            private 
    $plugins = array();
            
            public function 
    __construct() {
                
    $this -> addPlugin'LoginDurationSolution' );
                
    $this -> addPlugin'LoginSolution' );
            }
            
            public function 
    addPlugin$plugin ) {
                
    $this -> plugins[] = new PluginSupervisor$plugin );
            }
            
            public function 
    getPlugins() {
                return 
    $this -> plugins;
            }
        }
        
        final class 
    LoginSolution implements IDispatchPlugin {
            private 
    $plugin null;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin = new GenericLoginPlugin$plugin );
                
    $this -> plugin -> setRedirect'/admin/login/' );
            }
            
            public function 
    processContext $context$action ) {
                
    $this -> plugin -> process$context$action );
            }
        }
        
        final class 
    LoginDurationSolution implements IDispatchPlugin {
            private 
    $plugin null;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin = new LoginDurationPlugin$plugin );
                
    $this -> plugin -> setRedirect'/admin/login/' );
            }
            
            public function 
    processContext $context$action ) {
                
    $this -> plugin -> process$context$action );
            }
        }

    final class 
    PluginSupervisor {
            private 
    $plugin null;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin $plugin;
            }
            
            public function 
    applyRule$action ) {
                
    $plugin $this -> plugin;
                return new 
    $plugin$action );
            }
        } 
    PHP Code:
    include_once( 'interfaces/plugins.php' );
        
        final class 
    GenericLogInPlugin implements IDispatchPlugin {
            private 
    $plugin null;
            private 
    $redirect;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin $plugin;
            }
            
            public function 
    setRedirect$redirect ) {
                
    $this -> redirect $redirect;
            }
            
            public function 
    processContext $context$action ) { 
                
    $username $context -> get'request' ) -> get'session' ) -> get'username' );
                
    $password $context -> get'request' ) -> get'session' ) -> get'password' );
                
                
    $user = new UserRecord$context -> get'connection' ), new UserStatement() );
                if( 
    $user -> authenticate$username$password ) ) {
                    return 
    $this -> plugin -> process$context$action );
                }
                
                
    header'location:'.$this -> redirect );
                exit();
            }
        }

    include_once( 
    'interfaces/plugins.php' );
        
        final class 
    LoginDurationPlugin implements IDispatchPlugin {
            private 
    $plugin null;
            private 
    $redirect;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin $plugin;
            }
            
            public function 
    setRedirect$redirect ) {
                
    $this -> redirect $redirect;
            }
            
            public function 
    processContext $context$action ) {
                
    $setting $context -> get'configuration' ) -> get'login' );
                
    $user = new UserRecord$context -> get'connection' ), new UserStatement() );
                
    $user -> where"email = '".$context -> get'request' ) -> get'session' ) -> get'username' )."'" );
                
    $user -> get();
                
                if( 
    $user -> id ) {
                    
    $remainder time() - $user -> login;
                    if( 
    $setting['duration'] > $remainder ) {
                        
    $user -> acknowledgetime() ); 
                        return 
    $this -> plugin -> process$context$action );
                    } 
                }
                
                
    header'location:'.$this -> redirect );
                exit();
            }
        }

    include_once( 
    'interfaces/plugins.php' );
        
        final class 
    BasicDigestLogInPlugin implements IDispatchPlugin {
            private 
    $plugin null;
            
            public function 
    __construct$plugin ) {
                
    $this -> plugin $plugin;
            }
            
            public function 
    processContext $context$action ) { 
                if( isset( 
    $_SERVER['PHP_AUTH_USER'] ) ) { 
                    
    $user = new UserRecord$context -> get'connection' ), new UserStatement() );
                    if( 
    $user -> authenticate$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) {
                        return 
    $this -> plugin -> process$context$action );
                    }
                }
                
                
    header'WWW-Authenticate: Basic realm="Members"' );
                
    header'HTTP/1.0 401 Unauthorized' );
                exit();
            }
        }

    interface 
    IDispatchPlugin {
            public function 
    processContext $context$action );
        } 
    PHP Code:
    class UserRecord extends Record {
            protected 
    $fields = array(
                
    'id',
                
    'email',
                
    'login',
                
    'created',
                
    'password',
                
    'privilegeid' );
                
            public 
    $id false;
            
            public function 
    __constructIConnection $connIStatement $statement ) {
                
    parent::__construct$conn$statement );
            }
            
            public function 
    acknowledge$timestamp ) {
                
    $sql sprintf$this -> statement -> acknowledgeStatement(),
                    (int) 
    $timestamp,
                    (int) 
    $this -> id );
                    
                try { 
                    
    $result $this -> conn -> fetch$sql );
                    if( 
    $error $result -> isError() ) {
                        throw new 
    Exception
                            
    'database server returned an error: '.$error
                        
    );
                    }
                } catch( 
    Exception $e ) {
                    throw 
    $e;
                }
            }
            
            public function 
    authenticate$username$password$is_hashed false ) {
                
    $username $this -> conn -> escape$username );
                
    $password $this -> conn -> escape$password );
                
    $this -> where"users.email = '".$username."'" );
                
                try {
                    
    $this -> get(); 
                    if( 
    $is_hashed ) {
                        if( 
    $this -> id && $this -> password == (string) md5$password ) ) {
                            return 
    true;
                        }
                    } else {
                        if( 
    $this -> id && $this -> password == (string) $password ) {
                            return 
    true;
                        }
                    }
                } catch( 
    Exception $e ) {
                    throw 
    $e;
                }
                return 
    false;
            }
            
            public function 
    insert() { ... }
            public function 
    update() { ... }
        }
        
        class 
    UserGateway extends Gateway {
            public function 
    __constructIConnection $connIStatement $statement ) {
                
    parent::__construct$conn$statement );
            }
        }
        
        final class 
    UserStatement implements IStatement {
            public function 
    selectStatement() {
                return 
    "select
                        *
                   from users"
    ;
            }
            
            public function 
    insertStatement() {
                return 
    "insert into users ( id, email, login, created, password, privilegeid ) values ( '', '%s', %s, '%s', '%s', %s );";
            }
            
            public function 
    updateStatement() {}
            
            public function 
    deleteStatement() {}
            public function 
    totalRowsStatement() {
                return 
    "select
                        count(*) as count
                   from users"
    ;
            }
            
            public function 
    selectByIdStatement() {
                return 
    "select
                        *
                   from users
                  where id = %s"
    ;
            }
            
            public function 
    acknowledgeStatement() {
                return 
    "update users set
                        login = %s
                  where id = %s"
    ;
            }
        } 
    The Context $context is an object you pass around which acts as a container for other objects, but in my case Context has minimal logic

  10. #35
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the code! I will read now...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •