Location: PHPKode > scripts > ShipForm > shipform/ShipForm.php
<?php
//
// +----------------------------------------------------------------------+
// | Ship Form - class to copy data between forms                         |
// +----------------------------------------------------------------------+
// | With this class you can copy all data between forms. This class      |
// | use JavaScript and was tested under IE and Netscape.                 |
// | I suggest you to use this class with Abstract.php class beacuase     |
// | data model and functions used here are similar with this in          |
// | Abstract, for more info see: http://www.phazer.dk/php/               |
// | If you have any suggestions or find some bug write: hide@address.com   |
// +----------------------------------------------------------------------+
// | Authors: Radoslaw Maciaszek <hide@address.com>                         |
// +----------------------------------------------------------------------+
//
// $Id: ShipForm.php v 1.0 2002/07/08 14:16:02 wodzu Exp $
//

if(!defined('SHIP_STANDARD')) define('SHIP_STANDARD', 0);
if(!defined('SHIP_SUM'))      define('SHIP_SUM',      1);
if(!defined('SHIP_ADD_ALL'))  define('SHIP_ADD_ALL',  2);
if(!defined('SHIP_ADD_SUM'))  define('SHIP_ADD_SUM',  3);

require( 'AbstractForm.php' );

/**
* Base class 
* 
* @access   public
*/
class ShipForm {
    
    var $form_name;
    var $text_fields;
    var $radio_buttons;
    var $checkbox_fields;
    
    var $shipData = array();
    var $Ids = array();
    var $descrArray = array();
    
    var $_shipForm_name = "shipForm";
    
    // contructor
    function ShipForm( 
            
            // name f the form
            $form_name,
            
            // array nclude names of text inputs
            $text_fields = array(),
            
            // array include names of radio buttons
            $radio_buttons = array(),
            
            // associative array
            // which contains key => val
            // key is name of the group
            // val is array of indexes for key
            // see example below
            $checkbox_fields = array(),
            
            $selected_fields = array(),
            
            $clear = true
            
            ) {
        
        $this->form_name = $form_name;
        $this->text_fields = $text_fields;
        $this->radio_buttons = $radio_buttons;
        $this->checkbox_fields = $checkbox_fields;
        $this->selected_fields = $selected_fields;
        
        $this->clear = $clear;
    }
    
    function AddData($id, $descr, $valFromArray = array() ) {
        // if data id already exist make new id
        if(in_array($id,$this->Ids)) {
            $id = max( $this->Ids ) + 1;
        }
        array_push($this->Ids, $id);
        array_push($this->descrArray, $descr);
        
        $textArray = array();
        // todo change line feed in other OS
        foreach($this->text_fields as $field_name) {
            if(isset($valFromArray[$field_name])) {
                $textArray[] = ereg_replace( "\r\n", "'\r\n + '", 
                                addslashes( $valFromArray[$field_name]) );
            } else {
                $textArray[] = "";
            }
        }
        $textJsArray = $this->makeArray( $textArray );

        $radioArray = array();
        foreach($this->radio_buttons as $field_name) {
            if(isset($valFromArray[$field_name])) {
                $radioArray[] = $valFromArray[$field_name];
            } else {
                $radioArray[] = "0";
            }
        }
        $radioJsArray = $this->makeArray( $radioArray );

        $selectedArray = array();
        foreach($this->selected_fields as $field_name) {
            if(isset($valFromArray[$field_name])) {
                $selectedArray[] = $valFromArray[$field_name];
            } else {
                $selectedArray[] = "-1";
            }
        }
        $selectedJsArray = $this->makeArray( $selectedArray );

        $checkboxArray = array();
        foreach($this->checkbox_fields as $field_name => $fieldArray) {
            if(isset($valFromArray[$field_name])) {
                $indexedArray = array();
                if(is_array($valFromArray[$field_name])) {
                        $checkboxArray[$field_name] = 
                            $this->setIndexes($valFromArray[$field_name]);
                }
            } else {
                $checkboxArray[$field_name] = array();
            }
        }
        $checkboxJsArray = array();
        foreach( $checkboxArray as $key => $val ) {
            $checkboxJsArray[$key] = $this->makeArray( $val );
        }
        $checkboxAllJsArray =& $this->makeArray( $checkboxJsArray );
        
        $this->shipData[$id]  = "data['".$id."'] = new DataContainer( ";
        $this->shipData[$id] .= $textJsArray.", ";
        $this->shipData[$id] .= $radioJsArray.", ";
        $this->shipData[$id] .= $checkboxAllJsArray.", ";
        $this->shipData[$id] .= $selectedJsArray." );\n\n";
    }
    
    function setIndexes( $array ) {
        foreach( $array as $key => $val ) {
            if( $val == "on" ) $array[$key] = $key;
        }
        return $array;
    }
    
    function makeArray( $array ) {
        if(is_array($array)) {
            $strArray = " new Array( ";
            $s = "";
            foreach( $array as $val ) {
                if(strpos($val, "new")===false) {
                    $strArray .= $s."'".$val."'";
                } else {
                    $strArray .= $s.$val;
                }
                $s = ", ";
            }
            $strArray .= ")";
            return $strArray;
        } else return null;
    }
    
    // require class Abstract.php
    function printForm( $mode = SHIP_STANDARD ) {
        if(!empty($this->Ids)) {
            $f = new Form("", $this->_shipForm_name);
            TextOut( "Kopiuj dane od:", "bold" );
            LineFeed();
            foreach($this->Ids as $k => $id) {
                $f->CheckBox("copy[".$id."]", "",
                        "onClick=\"javascript: shipToForm(this)\";", $id );
                Space(2);
                TextOut( $this->descrArray[$k] );
                LineFeed();
            }
            if(($mode & SHIP_ADD_SUM) && count($this->Ids) > 1) {
                LineFeed();
                $f->CheckBox("set_clear", "",
                        "onClick=\"javascript: addAll(this)\";", $id );
                echo "<nobr>";
                TextOut( "Kopiuj od wszystkich", "bold" );
                echo "</nobr>";
            }
            $f->done();
        }
    }
    
    function done( $display = true ) {
        $js = "<SCRIPT language=\"JavaScript\" SRC=\"/js/ShipForm.js\"></SCRIPT>\n\n";
        $js .= "<SCRIPT LANGUAGE=\"JavaScript\"> \n<!-- Begin\n// configuration\n";
        $js .= "var clear_fields = ";
        if($this->clear) {
            $js .= "true";
        } else {
            $js .= "false";
        }
        $js .= ";\n";
        
        $js .= "var ship_form_str = 'document." . $this->_shipForm_name."';\n";
        $js .= "var main_form_str = 'document." . $this->form_name."';\n\n";
        
        $js .= "var text_fields = " . $this->makeArray($this->text_fields).";\n";
        $js .= "var radio_buttons = " . $this->makeArray($this->radio_buttons).";\n";
        $js .= "var selected_fields = " . $this->makeArray($this->selected_fields).";\n";
        $name_of_checkboxes = array();
        $js_checkboxes = array();
        foreach($this->checkbox_fields as $key => $value) {
            $name_of_checkboxes[] = $key;
            $js_checkboxes[] = "checkbox_fields['" . $key
                    . "'] = " . $this->makeArray($value).";\n";
        }
        
        $js .= "var checkbox_fields = " . $this->makeArray($name_of_checkboxes).";\n";
        foreach($js_checkboxes as $js_checkbox) {
            $js .= $js_checkbox;
        }
        $js .= "\n";
        
        if(!empty($this->Ids)) {
            $js .= "var data = " . $this->makeArray($this->Ids).";\n";
        }
        foreach($this->shipData as $shipThis) {
            $js .= $shipThis;
        }
        
        $js .= "//  End --> \n</script>\n";
        
        if($display) {
            echo $js;
        } else {
            return $js;
        }
    }
}

?>
Return current item: ShipForm