[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/view/ -> view.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup View
   5       *
   6       * A view is the class in pLog that takes care of rendering the content and sending to the user. In pLog
   7       * each Action class must generate a view that can be sent to the client, which is obtained via the method
   8       * Action::getView()
   9       *
  10       * @see View
  11       * @see BlogView
  12       * @see SmartyView
  13       * @see AdminView
  14       * @see PluginTemplatedView
  15       * @see AdminPluginTemplatedView
  16       */
  17  
  18      
  19      lt_include( PLOG_CLASS_PATH."class/config/properties.class.php" );
  20      
  21      /**
  22       * constants that can be used for content types
  23       */
  24      define( "TEXT_HTML_CONTENT_TYPE", "text/html" );
  25      define( "TEXT_XML_CONTENT_TYPE", "text/xml" );
  26      
  27      /**
  28       * default content-type that is going to be sent in HTTP headers
  29       */
  30      define( "DEFAULT_VIEW_CHARSET", "iso-8859-1" );
  31      
  32      /**
  33       * default page where we should start if the "page" parameter is not
  34       * available
  35       */
  36      define( "VIEW_DEFAULT_START_PAGE", 1 );
  37      
  38      /**
  39       * name of the request parameter that contains the page number
  40       */
  41      define( "VIEW_DEFAULT_PAGE_PARAMETER", "page" );
  42  
  43      /**
  44       * \ingroup View
  45       *
  46       * Base class with basic methods related to views. This class should not be used directly since it does
  47       * not know anything about blogs, plugins or templates. New blog classes should either extend 
  48       * BlogView or PluginTemplatedView, while new admin view classes should extend AdminView or 
  49       * AdminPluginTemplatedView.
  50       *
  51       * This base View class does not know anything about cached views either.
  52       *
  53       * In case it is necessary to create a class extending directly from View, please implement your own rendering
  54       * logic in the View::render() method but do not forget to call it at some point in your render() method.
  55       */
  56      class View  
  57      {
  58  
  59          var $_params;
  60          var $_contentType;
  61          var $_headers;
  62          var $_charset;
  63          var $_request;
  64  
  65          /**
  66           * Constructor. Initializes the view with a default content type, character set, etc.
  67           */
  68  		function View()
  69          {
  70              
  71  
  72              $this->_params = new Properties();
  73              
  74              // set a default content type and character set for responses
  75              $this->_contentType = TEXT_HTML_CONTENT_TYPE;
  76              $this->_charset = DEFAULT_VIEW_CHARSET;
  77              $this->_headers = Array();
  78              
  79              // no form has caused any error when we initialize the view!
  80              $this->setValue( "formIsError", false );
  81              // and no form has caused any success yet either!
  82              $this->setValue( "formIsSuccess", false );
  83              
  84              // let's send an HTTP 200 header response... If somebody wants to overwrite it later
  85              // on, php should keep in mind that the valid one will be the last one so it is
  86              // fine to do this more than once and twice
  87              $this->addHeaderResponse( "HTTP/1.0 200 OK" );
  88              
  89              // initialize a request object, in case it is needed somewhere
  90              $this->_request = new Request( HttpVars::getRequest());
  91          }
  92  
  93          /**
  94           * Sets a single parameter for the view. These parameters will be passed to the template layer
  95           * once it gets processed.
  96           *
  97           * @param name Name of the parameter
  98           * @param value Value of the parameter
  99           */
 100           function setValue( $name, $value )
 101           {
 102              //$this->_params[$name] = $value;
 103              $this->_params->setValue( $name, $value );
 104           }
 105  
 106           /**
 107            * Returns the value identified by the key $key
 108            *
 109            * @param name The key
 110            * @return The value associated to that key
 111            */
 112           function getValue( $name )
 113           {
 114               return $this->_params->getValue( $name );
 115           }
 116           
 117           /**
 118            * sets the default content-type of the view. The default content type, if none specified, is
 119            * text/html
 120            *
 121            * @param contentType The new content type
 122            */
 123  		function setContentType( $contentType )
 124          {
 125              $this->_contentType = $contentType;
 126          }
 127          
 128          /**
 129           * sets the character set. If none is specified, the default content type is ISO-8859-1
 130           *
 131           * @param charset the character set
 132           */
 133  		function setCharset( $charset )
 134          {
 135              $this->_charset = $charset;
 136          }
 137          
 138          /**
 139           * Adds a new header string to the current list of headers
 140           *
 141           * @param headerString the new header string
 142           * @return Always true
 143           */
 144  		function addHeaderResponse( $headerString )
 145          {
 146              array_push( $this->_headers, $headerString );
 147              
 148              return true;
 149          }
 150          
 151          /**
 152           * sets the headers that are going to be sent to the client
 153           * from the values in the array. This will remove ALL the headers
 154           * that have been set so far!
 155           *
 156           * @param headers An array of strings
 157           * @return always true
 158           * @see addHeaderResponse
 159           */
 160  		function setHeaders( $headers = Array())
 161          {
 162              $this->_headers = $headers;
 163          }
 164          
 165          /**
 166           * prints out the content type and character set to the client, by setting the 
 167           * Content-Type HTTP response header
 168           *
 169           * @return always true.
 170           */
 171  		function sendContentType()
 172          {
 173              // build up the header and send it
 174              $header = "Content-Type: ".$this->_contentType.";charset=".$this->_charset;
 175              header( $header );
 176                  
 177              return true;
 178          }
 179          
 180          /**
 181           * sets an error message for the whole form, should that be needed
 182           *
 183           * @param message
 184           * @return Always true
 185           */
 186  		function setErrorMessage( $message )
 187          {
 188              $this->setValue( "viewErrorMessage", $message );
 189              $this->setError( true );
 190              
 191              return true;
 192          }
 193          
 194          /**
 195           * Whether the view has to show some error message or not. Views can
 196           * show success messages as well as error messages at the same time.
 197           *
 198           * @param error
 199           * @return Always true 
 200           */
 201  		function setError( $error = true )
 202          {
 203              $this->setValue( "viewIsError", $error );
 204              
 205              return true;
 206          }
 207          
 208          /**
 209           * Whether the view has to show some success message or not. Views can show
 210           * sucess messages as well as error messages as the same time!
 211           * 
 212           * @param success
 213           * @return Always true
 214           */
 215  		function setSuccess( $success = true )
 216          {
 217              $this->setValue( "viewIsSuccess", $success );
 218              
 219              return true;
 220          }
 221          
 222          /**
 223           * sets an success message for the whole form, should that be needed
 224           *
 225           * @param message
 226           * @param formName not used nor required (yet!)
 227           * @return Always true
 228           */
 229  		function setSuccessMessage( $message )
 230          {
 231              $this->setValue( "viewSuccessMessage", $message );
 232              $this->setSuccess( true );            
 233              
 234              return true;
 235          }
 236          
 237          /**
 238           * stores a value in the session, associated to one key, in case
 239           * the view wants to keep some value for later use such as filter settings
 240           * for persisten listings, etc.
 241           *
 242           * @param param
 243           * @param value
 244           * @return Always true
 245           */
 246  		function setSessionValue( $param, $value )
 247          {
 248              $session = HttpVars::getSession();
 249  
 250              // if there is no session data, there's nothing for us to set
 251              if( !is_array( $session )) 
 252                  return false;        
 253          
 254              $viewName = get_class( $this );
 255              $keyName = "{$viewName}_{$param}";
 256              $session["$keyName"] = $value;
 257              HttpVars::setSession( $session );
 258              
 259              return true;
 260          }
 261          
 262          /** 
 263           * retrieves a parameter from the session
 264           *
 265           * @param param
 266           * @param defaultValue
 267           * @return The value associated to the parameter or empty if not
 268           * found
 269           */
 270          function getSessionValue( $param, $defaultValue = "" )
 271          {
 272              $session = HttpVars::getSession();
 273  
 274              // if there is no session data, there's nothing for us to look for
 275              if( !is_array( $session )) 
 276                  return false;
 277              
 278              $viewName = get_class( $this );
 279              $keyName = "{$viewName}_{$param}";
 280  
 281              if(isset($session[$keyName]) && !empty($session[$keyName]) ){
 282                  return $session[$keyName];
 283              } else{
 284                  return $defaultValue;
 285              }
 286          }
 287          
 288  
 289         /**
 290           * gets the current page from the HTTP request
 291           *
 292           * @return the page number from the request
 293           * @static
 294           */
 295  		function getCurrentPageFromRequest()
 296          {
 297              lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php");        
 298              // get the value from the request
 299              $page = HttpVars::getRequestValue( VIEW_DEFAULT_PAGE_PARAMETER );
 300              // but first of all, validate it
 301              $val = new IntegerValidator();
 302              if( !$val->validate( $page ))
 303                  $page = VIEW_DEFAULT_START_PAGE;            
 304                              
 305              return $page;
 306          }        
 307  
 308          /**
 309           * Renders the view. Here we would ideally call a template engine, using the
 310           * values in $this->_params to fill the template 'context' and then display
 311           * everything. All classes extending the main View class (or any of its child classes
 312           * such as BlogView) are advised to call parent::render() as the first thing in their
 313           * own render() method.
 314           *
 315           * By default does nothing and it has no parameters
 316           */
 317          function render()
 318          {
 319              // send the headers we've been assigned if any, alognside the conten-type header
 320              foreach( $this->_headers as $header )
 321              header( $header );
 322  
 323              $this->sendContentType();            
 324          }
 325      }
 326  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics