PhpWindowSystem - Demo Site
 
 
PhpConcept
 
 

Home

 
   
 

Other projects

 
  SourceForge Logo  
 


PhpWindowSystem
is a generic Window management PHP code that allow any PHP application to propose to the user a friendly graphical interface. Php Window System manage the main window, the title header, menu bars with sub-menus, footers, popup windows.

 
 
sample.class.php
 
 

 

<?
// --------------------------------------------------------------------------------
// PhpWindowSystem - Customer Document Sample Class
// --------------------------------------------------------------------------------
//
// Presentation :
//   PhpWindowSystem is a generic PHP window system management.
//   PhpWindowSystem is designed to take care of window displaying and
//   manage the associated events in order to offer a generic application
//   container.
//   More information can be obtained on
//   PhpConcept (http://www.phpconcept.net)
//   or by contacting the author Vincent Blavet (vincent@phpconcept.net)
//
// --------------------------------------------------------------------------------
// License :
//   PhpWindowSystem (PWS)
//   Copyright (C) 2002  Vincent Blavet (vincent@phpconcept.net)
//
//   This library is free software; you can redistribute it and/or
//   modify it under the terms of the GNU Lesser General Public
//   License as published by the Free Software Foundation; either
//   version 2.1 of the License, or (at your option) any later version.
//
//   This library is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public
//   License along with this library; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// --------------------------------------------------------------------------------
// CVS : $Id: sample.class.php,v 1.1 2002/08/26 22:04:25 vblavet Exp $
// --------------------------------------------------------------------------------

  // ----- Class objects
  //   The user application must at least create a class that inherit the
  //   PwsDocument properties. This class will be used for communicating between
  //   PWS and the application developer specific source code.
  
require_once('pws/core/pws-document.class.php');

  
// ----- Other user includes
  //   Here the application developer will include all the stuff needed for its
  //   application.
  //   In this sample we are using the PclZip library for managing zip archives.
  
require_once('pclzip.lib.php');

  
// --------------------------------------------------------------------------------
  // Class : MyAppliDocument
  // Description :
  // Attributes :
  // --------------------------------------------------------------------------------
  
class MyAppliDocument extends PwsDocument
  
{
    
// ----- User data
    //   All attributes of this class will be stored in the PHP session created by
    //   PWS. The application developer will use all these attributes to have a
    //   persistence of its data between events
    
var $user_data;

    
// --------------------------------------------------------------------------------
    // Method : MyAppliDocument()
    // Description :
    //   Constructor
    // --------------------------------------------------------------------------------
    
function MyAppliDocument()
    {
      
// ----- Call the Parent constructor
      //   This is mandatory, in order to have some informations on the document
      //   that will be used  by PWS (like the unic document number)
      
PwsDocument::PwsDocument();
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_pws_new()
    // Description :
    //   This method is associated with the event 'pws_new'.
    //   The 'pws_new' event is a reserved event, which is first processed by the
    //   event manager. The event manager creates a new object structure with
    //   the 'MyAppliDocument' class and then call this (optional method).
    //   If this method is not defined, the document is simply created.
    //
    //   If this method returns 1, then everything is OK, the event manager
    //   will process directly the next event waiting in the queue (if any).
    //   If this method returns 0, then everything is OK, the event manager
    //   will process the next event in a second HTTP request.
    //   If it returns a negative value, the object will be destroyed, and
    //   the new even ignored.
    // --------------------------------------------------------------------------------
    
function event_pws_new(&$p_event)
    {
      
$v_result=1;

      
// ---- Allocate a default name.
      //   By default PWS gives a default name to each document.
      //   In this sample we create a uniq filename by checking that the file
      //   does not already exists.
      //   We also give a limit of 1000 archives created by 'new'.
      
for ($i=1$this->name=''$i<1000 && $this->name==''$i++) {
        
$v_name 'archive-'.$i.'.zip';
        if (!
is_file($v_name)) {
          
$this->name $v_name;
        }
      }

      if (
$this->name != '') {
        
// ----- Any other stuff might be done here !!!!
        
touch($this->name);

        
// ----- Send a display event in order to display the newly created document
        //   It is the user responsability to send this event. If not send, there will be
        //   no refresh of the display (that may be usefull).
        
PwsEventSend('pws_display'$this->id);
      }
      else {
        
// ----- The file can not be created, so destroy the object
        
$v_result=-1;
      }

      return 
$v_result;
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_pws_open()
    // Description :
    //   This method is associated with the event 'pws_open'.
    //   When the PWS event manager receive a 'pws_open' event, it creates a new
    //   object with class MyAppliDocument, and then call this method for any other
    //   action to do to load the document data.
    //   Like event_pws_new(), if you finally don't want to open the document, you
    //   can return a negative value. Else you will return 0 or 1.
    //
    //   The 'pws_open' event will have (should have) a 'name' parameter which must
    //   uniquelly identify the document (in this sample the filename of the archive)
    // --------------------------------------------------------------------------------
    
function event_pws_open(&$p_event)
    {
      
// ----- Here we should check that this file is not already opened !

      // ----- Set the document name
      //   If we do not do that, the document will have the default name
      //   set by the PwsDocument constructor.
      
$this->name trim($p_event->parameters['name']);

      
// ----- Extract extension is .zip
      //   Here we check that the file is really a zip archive (good extension)
      
$v_extension substr(strrchr(basename($this->name), '.'), 1);
      if (
$v_extension != 'zip') {
        
// ----- TBC
        //   Please notice that this direct javascript call must be replaced
        //   in a near futur by an encapsulated PWS function
?>
<script language="Javascript1.2">
alert("File '<? echo $this->name?>' does not have a valid .zip extension");
</script>
<?php
        
return -1;
      }

      
// ----- Check that it is a valid archive
      
$v_archive = & new PclZip($this->name);
      
$v_list $v_archive->listContent();
      if ((
filesize($this->name) > 0) && (($v_list == 0) || (sizeof($v_list) == 0))) {
        
// ----- TBC
        //   Please notice that this direct javascript call must be replaced
        //   in a near futur by an encapsulated PWS function
?>
<script language="Javascript1.2">
alert("File '<? echo $this->name?>' is not a valid zip archive");
</script>
<?php
        
return -1;
      }

      
// ----- Display the document
      //   It is the user responsability to send this event. If not send, there will be
      //   no refresh of the display (that may be usefull).
      
PwsEventSendWithTarget('pws_display'$this->id'pws_main',
                             
'name''pws_main');

      return 
1;
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_pws_save_as()
    // Description :
    //   This method will be called when the event manager receive a 'pws_save_as'
    //   event. The event will have a 'name' parameter like the 'pws_open' event.
    //   Unlike event_pws_new() and event_pws_open() the object is already existant,
    //   returning a negative value will be understanding has an error.
    // --------------------------------------------------------------------------------
    
function event_pws_save_as(&$p_event)
    {
        
// ----- TBC
        //   Please notice that this direct javascript call must be replaced
        //   in a near futur by an encapsulated PWS function
?>
<script language="Javascript1.2">
alert("The event 'pws_save_as' is not implemented in this sample");
</script>
<?php

      
return 1;
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_pws_delete()
    // Description :
    //   This method will be called when the event manager receive a 'pws_delete'
    //   event for this document.
    //   If the method returns a positive value the object is deleted and removed
    //   from the opened document list. If the method returns a negative value
    //   the document is not destroyed.
    // --------------------------------------------------------------------------------
    
function event_pws_delete(&$p_event)
    {
      
// ----- Other event should be send to the application user to check the
      //       delete

      // ----- Remove the file
      
@unlink($this->name);

      return 
1;
    }
    
// --------------------------------------------------------------------------------

     // --------------------------------------------------------------------------------
    // Method : event_pws_display()
    // Description :
    //   This method is specific and is not used alone for a display.
    //   When the event manager receive the 'pws_display' event, it looks for
    //   the concerned window (here the main window) and the current document
    //   associated with the window.
    //   It then display all the objects in a hierarchical manner (each PWS graphical
    //   object as a parent and may have one or several childs).
    //   Each object has the responsability to display its graphical properties, and
    //   its childs inside (each PWS graphical object is a kind of container).
    //   A PWS document has for parent a PwsFrame and will display graphical info
    //   inside the frame.
    //
    //   However a display can be complex (not in this sample) and a document can
    //   define the methods event_pws_header_display(),  event_pws_style_display(),
    //   event_pws_script_display(), event_pws_pre_display() and
    //   event_pws_post_display(). Each of these methods generate HTML code at
    //   different stage of the HTML page (see documentation).
    // --------------------------------------------------------------------------------
    
function event_pws_display(&$p_event)
    {
      
// ----- Look if the archive does not exist in order to display an empty message
      
if (!is_file($this->name)) {
        echo 
"Empty archive<br></br><br></br><br></br><br></br><br></br><br></br>";
        return 
1;
      }

      
// ----- Open the zip file
      
$v_archive = & new PclZip($this->name);
      if (((
$v_list $v_archive->listContent()) == 0) || (sizeof($v_list)==0)) {
        echo 
"Empty archive<br></br><br></br><br></br><br></br><br></br><br></br>";
        return 
1;
      }

      
// ----- Display content
      
for ($i=0$i<sizeof($v_list); $i++)
      {
        
// ----- Compose the event to generate on a click
        
$v_java PwsComposeJavaEvent("remove"$this->id"",
                                      
'name'$v_list[$i]['filename'],
                                      
'index'$i);

        
// ----- Do the display with the generated URL
        //   Please notice the 'javascript:void();' for href and the 'return false;'
        //   added in the onClick()
        
echo '<a href="javascript:void();" onClick=\''.$v_java.'; return false;\'>'.$v_list[$i]['filename'].'</a> ('.$v_list[$i]['size'].' bytes)';
        echo 
"<br>";
      }

?>
<p align=center>
Click on a file to remove it from the archive.
<br>
Please notice that no confirmation will be asked !
</p>
<?
      
return 1;
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_add()
    // Description :
    //   'add' is a typical application developer defined event.
    //   When the event manager receive an 'add' event, it does not recognize a
    //   reserved even and then call (if defined) the method with name 'event_add'.
    //   So to define a custom event today, you just have to define a method in you
    //   document (MyAppliDocument) with name 'event_'+event name.
    // --------------------------------------------------------------------------------
    
function event_add(&$p_event)
    {

      
// ----- Check for archive size
      
if (filesize($this->name) > 100000) {
        
$v_message  'Sorry the archive is too big (>100Ko).\n';
        
$v_message .= 'This sample limit the adding function for security reason.\n';
        
$v_message .= "File '".$p_event->parameters['filename']."' will not be added.";

        
// ----- TBC
        //   Please notice that this direct javascript call must be replaced
        //   in a near futur by an encapsulated PWS function
?>
<script language="Javascript1.2">
alert("<?php echo $v_message?>");
</script>
<?php
      
}
      else {
        
// ----- Open the zip file
        
$v_archive = & new PclZip($this->name);
        if (
filesize($this->name)==0) {
          
$v_list $v_archive->create($p_event->parameters['filename']);
        }
        else {
          
$v_list $v_archive->add($p_event->parameters['filename']);
        }

        
// ----- Display the document
        
PwsEventSend('pws_display'0);
      }

      return 
1;
    }
    
// --------------------------------------------------------------------------------

    // --------------------------------------------------------------------------------
    // Method : event_remove()
    // Description :
   // --------------------------------------------------------------------------------
    
function event_remove(&$p_event)
    {
      
// ----- Remove the file from the zip file
      
$v_archive = & new PclZip($this->name);
      
$v_list $v_archive->deleteByIndex($p_event->parameters['index']);

      
// ----- Display the document
      
PwsEventSend('pws_display'0);

      return 
1;
    }
    
// --------------------------------------------------------------------------------

  
}
  
// End of class
  // --------------------------------------------------------------------------------


// End of File
// --------------------------------------------------------------------------------
?>