Location: PHPKode > scripts > PageLogic > pagelogic/class_pagelogic.php
<?php

include_once "class_iniparser.php";

//######################################################################
//##### TITLE       :: CLASS PAGELOGIC
//##### FILE        :: class_pagelogic.php
//##### PROJECT	    :: WebVision
//##### RELATED DOCUMENT ::
//##### DESCRIPTION   ::
//#####     To read in .ini file.
//#####     Extract data into a 2-dimensional array
//#####         1st index: category
//#####         2nd index: items under category
//##### AUTHOR      :: Mark Quah
//##### REVISION 	::
//######################################################################
define("PLOGIC_OPENFORM", 1);   // don't close form

class PAGELOGIC extends INIParser
{
    //#-----------------------------------------------------------------
    //#---- FUNCTION :: PageLogic($file_name="", $parse_state, $debug_level)
    //#---- DESCRIPTION  ::
    //#----      Initialize INIParser
    //#---- INPUT ::
    //#----      file_name  : file name to be parsed
    //#----                   if file name is omitted, can read in from strings
    //#----       parse_state:  0: wait for PARSESTART BEFORE PARSING
    //#----                     1: jump straight into parsing
    //#----      debug_level:  0 -9:  0: no output, 9 a lot of output
    //#---- OUTPUT ::
    //#----      error_code : OK success, else error message
    //#---- OTHERS ::
    //#----      none
    //#-----------------------------------------------------------------
    function PageLogic($file_name = "", $parse_state=1, $debug_level=1)
    {   //----- INITIALIZE DATA
        $this->INIParser($file_name,$parse_state, $debug_level);
    } // end AUTH

    //#-----------------------------------------------------------------
    //#---- FUNCTION :: FormLogic($action_cat, $action)
    //#---- DESCRIPTION  ::
    //#----      Base on the input $action, call up respective funciton in
    //#----      in $action_cat
    //#----       eg, [LOGIC]
    //#----           FUNC1=ThisPage
    //#----           FUNC2=NextPage, nextpage.php, nextpage_support.php
    //#----           FUNC3=http://www.phpclasses.org
    //#---- If action == "FUNC1", the function ThisPage() will be called
    //#---- If action == "FUNC2", the following code will be executed:
    //#----         include_once "nextpage.php";
    //#----         include_once "nextpage_support.php";
    //#----         NextPage();
    //#---- If action == "FUNC", it will jump to www.phpclassess.org
    //#---- INPUT ::
    //#----      action_cat : catgory used to process logic
    //#----      action     : value used to process logic
    //#---- OUTPUT ::
    //#----      Execute relevant code
    //#---- OTHERS ::
    //#----      none
    //#-----------------------------------------------------------------
    function FormLogic($action_cat, $action)
    {
        $this->Debug(7, "FORMLOGIC: $action_cat  $action");
        $value = $this->item[$action_cat][$action];
        if ($value == "")
            $value = $this->item[$action_cat]['DEFAULT'];
        // extract function name
        $func_name=strtok($value, ",");
        // extract include file if any
        do
        {   $file_name = trim(strtok(","));
            if ($file_name)
                include $file_name;
        } while ( $file_name );
        // run the function
        if (substr($func_name, 0, 7) == "http://")
            echo "<script>location='$func_name'</script>";
        else
        {
            $this->Debug(7, "FUNCTION: [$func_name]<BR>");
            $func_name();
        }
    }

    function CreateForm($form_name, $action_file="", $method="POST", $extension="")
    {   global $HTTP_SERVER_VARS;
        $action_file = ($action_file==""?$HTTP_SERVER_VARS['PHP_SELF']:$action_file);
        // assemble form statement
        $out = sprintf("<FORM NAME='%s' METHOD='%s' ACTION='%s' %s>",
                $form_name, $method,$action_file , $extension);
        $out .= sprintf("<INPUT TYPE=hidden NAME=%s>", $action_field);
        $this->item[$form_name]['FORM']= $out;
        // initialize other data
        $this->item[$form_name]['ITEM']="";
        $this->item[$form_name]['JSSCRIPT']="";
    }

    function CreateJSScript($form_name, $script_name, $field_array=array())
    {
        // assemble function name
        $arg = is_array($field_array) ? implode(", ", $field_array): "";
        $out .= "function $script_name($arg)\n{";
        // assemble field code
        if ( is_array($field_array) )
        {   foreach ($field_array as $value)
            {   $out .= sprintf("    document.forms['%s'].elements['%s'].value = %s;\n",
                        $form_name, $value, $value);
            }
        }
        // submit the form
        $out .= "    document.forms.$form_name.submit();\n";
        $out .= "    return true;\n";
        $out .= "}\n\n";
        $this->item[$form_name]['JSSCRIPT'] .=$out;
    } // end of SetForm

    function AddFormItem($form_name, $name, $value)
    {   $tmp_str = "<INPUT TYPE=HIDDEN NAME='$name' VALUE='$value'>\n";
        $this->item[$form_name]['ITEM'] .= $tmp_str;
    }

    function GenHiddenForm($form_name, $option = 0)
    {   // JSSCRIPT
        $out = "<script>\n";
        $out .= $this->item[$form_name]['JSSCRIPT'];
        $out .= "</script>\n";
        // <FORM ...
        $out .= $this->item[$form_name]['FORM'];
        // <INPUT TYPE=HIDDEN ...
        $out .= $this->item[$form_name]['ITEM'];
        // </FORM>
        if ( ! $option & PLOGIC_OPENFORM )
            $out .= "</FORM>\n";
        return $out;
    }

} // end CLASS PARSER

?>

Return current item: PageLogic