Views: 211
Rating: 0

TPersistentModule

Submitted By: marcus
Licence: PRADO

This component (designed for Prado 2.*) extends TModule by adding SESSION support for persistence across page.

Files

Info

Overview

This component (designed for Prado 2.*) extends TModule by adding SESSION support for persistence across page. This is useful when you have several pages that will share data. Saves you from doing the same SQL calls again and again on each page.


 

Installation / Usage

Simply extend your Module class from TPersistentModule instead of TModule. Any properties defined in your module will automatically persist across pages.

Note: Properties must be either public or protected. Properties marked as private will NOT persist.

<?php
/*
|-----------------------------------------------------------------------------------
| CLASS: TPersistentModule
|-----------------------------------------------------------------------------------
| DESCRIPTION: Extends TModule by using sessions to persist Module properties.
|-----------------------------------------------------------------------------------
| PROPERTY                        TYPE
|-----------------------------------------------------------------------------------
| -$nonPersistentProps               Array of property names to NOT persist.
| -$modulePropDefaults               Array of initial property values.
|-----------------------------------------------------------------------------------
| METHOD                        DESCR/RETURN
|-----------------------------------------------------------------------------------
| +__construct()                  Attempts to populate all module properties
|                              from session.
| +__destruct()                     Writes all module properties to session.
| -getUniqueSessionName()            Returns a unique string (id) for
|                              application/module.
| +paveModuleProps()               Resets all module properties to their
|                              default value.
| +registerNonPersistentProp($propName)   Takes a property name as a string and
|                              registers it so it WILL NOT be persisted.
| +isNonPersistentProp($propName)      Returns boolean.
|-----------------------------------------------------------------------------------
*/
class TPersistentModule extends TModule{
   private $nonPersistentProps = array();
   private $modulePropDefaults = array();

   public function onLoad($param){
      $this->registerNonPersistentProp('nonPersistentProps');
      $this->registerNonPersistentProp('modulePropDefaults');
      $this->registerNonPersistentProp('parameters'); //Prado property.
      $this->registerNonPersistentProp('children'); //Prado property.
   
      $usn = $this->getUniqueSessionName();
      foreach($this as $key => $value){
         if (!$this->isNonPersistentProp($key)){
            $this->modulePropDefaults[$key] = $value;
            if ($this->Session->has($usn.'.'.$key)){
               $this->{$key} = $this->Session->get($usn.'.'.$key);
            }
         }
      }
   }
   public function __destruct(){
      $usn = $this->getUniqueSessionName();
      foreach($this as $key => $value){
         if (!$this->isNonPersistentProp($key)){
            $this->Session->set($usn.'.'.$key, $value);
         }
      }
   }
   private function getUniqueSessionName(){
      return $this->getApplication()->getApplicationID().'.'.$this->getID();
   }
   public function paveModuleProps(){
      $ar = $this->modulePropDefaults;
      foreach($ar as $key => $value){
         $this->{$key} = $value;
      }
   }
   public function registerNonPersistentProp($propName){
      if (!$this->isNonPersistentProp($propName)){
         $this->nonPersistentProps[] = $propName;
      }
   }
   public function isNonPersistentProp($propName){
      return in_array($propName, $this->nonPersistentProps);
   }
}
?> 

 



Please note: This is currently a beta preview and has known bugs. Please see the PRADO forums if you have suggestions or problems using it.