Login   Register  
PHP Classes
elePHPant
Icontem

File: classes.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Prakash Khanchandani  >  Table Maintenance  >  classes.php  >  Download  
File: classes.php
Role: Auxiliary script
Content type: text/plain
Description: requires, defines, and some functions
Class: Table Maintenance
Manage forms for CRUD MySQL table records
Author: By
Last change:
Date: 2013-07-20 03:05
Size: 10,813 bytes
 

Contents

Class file image Download
<?php

/**
 * @author Prakash Khanchandani
 * @copyright 2013
 * @program classes.php
 * @description 
 * 				just has require statements for the class files,
 * 				the defines, and,
 * 				the common functions 
 */


require_once ("HTML/Template/ITX.php");
require_once ("mstrScripts.php");
require_once ("mstrTable.php");
require_once ("header.php");
require_once ("mstrFH.php");
require_once ("demoNotes.php");


define("Def_DbIp", "127.0.0.1");
define("Def_DbUser", "root");
define("Def_DbPassword", "");
define("Def_DbName", "test");


define("Def_commentSpan", '<span style="color:green;">');
define("Def_adtnlInfoSpan", '<span style="color:orange;">');


define("templatePath", "./");
define("host", "//localhost/xampp/banking/tableMaintenance/");
define("Def_LstBG", "rgb(245,245,245)");
define("Def_delBtnStl", "color:red; font-weight:bold;");
define("Def_MainFrmNm", "mainForm");


define("Def_normalPromptStyle", "prompt");
define("Def_notNullPromptStyle", "notNullPrompt");
define("Def_nullPromptStyle", "nullPrompt");
define("Def_promptUpdStyle", "error");
define("Def_blankAllowedEntryStyle", "entryBox");
define("Def_blankNotAllowedEntryStyle", "entryBoxWithBorder");
define("Def_MaxRowsToDspl", "25");
define("Def_PrevImageName",
    "//localhost/xampp/banking/tableMaintenance/bt-left-icon.png");
define("Def_NextImageName",
    "//localhost/xampp/banking/tableMaintenance/bt-right-icon.png");
define("labelReplacements", "./nmProperties.fl");


/**
 * IMPROVEMENTS THAT CAN BE DONE
 * ============================= 
 * every func computes the count of this->colDefs. Can be done once and stored in a protected class var  
 * change the name defaultRecordAlreadyExists(). Remember to change the names in all files where used.
 * change the name defaultCanDelete() to canDelete(). If client programs call explicit canDelete() you
 * will have to change it to parent:canDelete() and then the local canDelete().  
 */


function dateCheck($str)
{
    /* gets the date components in an array. Uses a standard php func to validate
    the date. Applies own validation that the year should be more than 1900.
    Returns either a false or returns the date in the internal notation - ymd.
    */
    $dtArray = strToDtComponents($str);

    if (!is_numeric($dtArray[2]) || !is_numeric($dtArray[1]) || !is_numeric($dtArray[0]))
        return false;

    if ($dtArray[0] < 1900)
        return false;

    if (!checkdate($dtArray[1], $dtArray[2], $dtArray[0]))
        return false;

    return $dtArray[0] . '-' . $dtArray[1] . '-' . $dtArray[2];
}


function strToDtComponents($str)
{
    /* extracts and returns y, m and d components, in that order, in an array.
    The string can be either in Indian format, dmy, or Gregorian, mdy.
    Depending on the session setting, this func will apply the correct
    interpretation. */
    // extract components depending on the date format setting in the session.
    if ($_SESSION['dateFormat'] == 'dmy')
        list($d, $m, $y) = extractComponents($str);
    else
        list($m, $d, $y) = extractComponents($str);
    // if the user has entered a 2 digit year, precede it with 20.
    if (strlen($y) == 1)
        $y = '200' . $y;
    elseif (strlen($y) == 2)
        $y = '20' . $y;
    elseif (strlen($y) == 3)
        $y = '2' . $y;
    /* pad m and d with 0 if required. Otherwise, for example, 1-1-2009 will not
    be less than 26-02-2009 when you compare them. */
    $m = str_pad($m, 2, '0', STR_PAD_LEFT);
    $d = str_pad($d, 2, '0', STR_PAD_LEFT);

    $dtArray = array($y, $m, $d);
    return $dtArray;
}


function extractComponents($strng, $pattern = "/[\s,.\/-]/")
{
    return preg_split($pattern, $strng, -1, PREG_SPLIT_NO_EMPTY);
}


function addToErrorMsg($msg)
{
    /* if an error message is already set
    add to it at the bottom, else set error msg */
    if (!isset($_SESSION['errorMsg']) or $_SESSION['errorMsg'] == '' or is_null($_SESSION['errorMsg'])) {
        $_SESSION['errorMsg'] = $msg;
    } else {
        $_SESSION['errorMsg'] = $_SESSION['errorMsg'] . "<br>" . $msg;
    }
    return;
}


function displayFormatDate($inDate)
{
    /*
    given the internal format date, ymd, 
    returns the date in the user's local format.
    */
    $dateComponents = explode("-", $inDate);
    if ($_SESSION['dateFormat'] == 'dmy')
        return $dateComponents[2] . "-" . $dateComponents[1] . "-" . $dateComponents[0];
    else
        return $dateComponents[1] . "-" . $dateComponents[2] . "-" . $dateComponents[0];
}


function m_str_replace($srch, $str)
{
    // remove occurences of each char in srch from str
    // if str = _('Y','N')_ and srch=_'()_, the return will be Y,N

    $count = strlen($srch);
    for ($i = 0; $i < $count; $i++) {
        $removeChar = substr($srch, $i, 1);
        $str = str_replace($removeChar, '', $str);
    }
    return $str;
}


function found_in_array($needle, $hayStack)
{
    return array_search($needle, $hayStack);
    /**
     * dont remember why i wrote the func below. Anyway, I am now returning the result of
     * array_search above and the code below is unused. Have left it as it is in case I
     * remember the reason for writing it in the first place. 
     */
    if (!is_array($hayStack))
        return false;

    $count = count($hayStack);
    for ($i = 0; $i < $count; $i++)
        if ($needle == $hayStack[$i])
            return $i + 1;

    return false;
}


function handleRequestOption()
{


    $self = $_SERVER['PHP_SELF'];


    switch ($_REQUEST['actn']) {
        case 'next':
        case 'prev':
        case 'cancel':
            $obj = createTableObject();
            return $obj;

        case 'select':
            // if 'id' not set, error
            if (!isset($_REQUEST['id'])) {
                addToErrorMsg($self);
                addToErrorMsg("no id for selected record");
                trmntMstMnt();
            }

            $obj = createTableObject();
            if ($obj->populateRecordValues() === false) {
                addToErrorMsg($self . " (handleRequestOptions)");
                trmntMstMnt();
            }
            return $obj;

        case 'delete':
            // if 'id' not set, error
            //echo '<pre>';print_r($_REQUEST);die();
            if (!isset($_REQUEST['id'])) {
                addToErrorMsg($self);
                addToErrorMsg("no id for selected record");
                trmntMstMnt();
            }
            $obj = createTableObject();
            if ($obj->populateRecordValues() === false) {
                addToErrorMsg($self . " (handleRequestOptions)");
                trmntMstMnt();
            }
            return $obj; // to get confirmation to delete.

        case 'dltConfirm':
            if (!isset($_REQUEST['id'])) {
                addToErrorMsg($self);
                addToErrorMsg("no id for selected record");
                trmntMstMnt();
            }
            $obj = createTableObject();
            /* populate values from request manually. */
            $obj->populateValuesFromRequest();

            if ($obj->deleteRecord() === false)
                trmntMstMnt();

            addToErrorMsg($obj->getId() . ': DELETED');

            $obj->getListAndColumns();
            return $obj;

        case 'add':
            $obj = createTableObject();
            /* validate the user inputs. The validation function
            would have set an error flag in case some 
            validations fail else the flag will be reset. */
            $obj->checkInputs();
            /* whether inputs are valid or not, return the object.
            if inputs valid, the form will be in read only mode
            and asking for confirmation, else user can make
            corrections. */
            return $obj;

        case 'addConfirm':
            $obj = createTableObject();
            /* during the add stage, the checkInputs call
            populated the colValu from $_REQUEST variables. 
            Secondly, the process of validating the remoteGL 
            also populated the colValu elements of the 
            individual account components. At this stage, 
            after addConfirm, we are not doing the validations 
            again. So, the populate functions have to be 
            called manually. */
            $obj->populateValuesFromRequest();

            if ($obj->addRecord() === false)
                trmntMstMnt();

            addToErrorMsg('CREATED');

            /* recreate the list. Technically, the function below
            can return an error, but by this time we have 
            executed it at least 3 times so why should it fail 
            now, the 4th? */
            $obj->getListAndColumns();
            return $obj;

        case 'update':
            // if 'id' not set, error
            if (!isset($_REQUEST['id'])) {
                addToErrorMsg($self);
                addToErrorMsg("no id for selected record");
                trmntMstMnt();
            }
            $obj = createTableObject();

            /* check the user inputs. If invalid, return object
            without any more checks */
            if ($obj->checkInputs() === false)
                return $obj;

            /* if inputs valid, check with record whether any
            changes made. The check function will leave an
            appropriate message. */
            $obj->checkIfValuesDifferent();

            return $obj;

        case 'updConfirm':
            // if 'id' not set, error
            if (!isset($_REQUEST['id'])) {
                addToErrorMsg($self);
                addToErrorMsg("no id for selected record");
                trmntMstMnt();
            }
            $obj = createTableObject();
            /* populate colDefs vars from $_REQUEST manually. */
            $obj->populateValuesFromRequest();

            if ($obj->updateRecord() === false)
                trmntMstMnt();

            addToErrorMsg($obj->getId() . ' - UPDATED');

            // recreate the list. Dont bother about its return value
            $obj->getListAndColumns();

            return $obj;

        default:
            addToErrorMsg($self);
            addToErrorMsg("unknown request action:" . $_REQUEST['actn']);
            trmntMstMnt();
    }
}


function trmntMstMnt()
{
    $form = new mstrFH('');
    /* you are passing a blank var above instead of an object
    so the form will only display the error msg. */
    $form->show();
    die();
}
?>