PHP Classes

File: mnuItm.php

Recommend this page to a friend!
  Classes of Prakash Khanchandani   Table Maintenance   mnuItm.php   Download  
File: mnuItm.php
Role: Example script
Content type: text/plain
Description: 2nd level example
Class: Table Maintenance
Manage forms for CRUD MySQL table records
Author: By
Last change:
Date: 11 years ago
Size: 3,998 bytes
 

Contents

Class file image Download
<?php

/**
 * @author Prakash Khanchandani
 * @copyright 2013
 * @program mnuItm.php
 * @description menu Items table maintenance
 * @specialities - enum values picked up from schema and validated
 * - form labels customised through properties
 */


session_start();
require_once (
"classes.php");


function
createTableObject()
{
   
$obj = new mnuItmTbl;
    if (
$obj->getListAndColumns() === false)
        return
false;
    else
        return
$obj;
}


class
mnuItmTbl extends mstrTable
{
    function
getListAndColumns()
    {
       
$this->tableName = 'menuItem';

       
$this->orderByClause = 'item';

       
$result = parent::getListAndColumns('item', 'isParent', 'URL');
        return
$result;
    }


    protected function
validateInput()
    {
       
/* check internal consistency. If the item is NOT a parent menu record, it should have
        a valid executable. */
       
$isParent = $this->getColDefsVal('isParent');
       
$URL = $this->getColDefsVal('URL');
        if (
$isParent === false or $URL === false) {
           
addToErrorMsg('could not get value of isParent/URL');
            return
false;
        }
        if (
$isParent == 'N') {
            if (
$URL == '' or strpos(strtolower($URL), '.php') === false) {
               
addToErrorMsg('invalid value for URL considering that isParent=N');
                return
false;
            }
        }
        return
true;
    }


    protected function
defaultRecordAlreadyExists()
    {
       
/* override the func in mstrTable.php. The parent func cannot work in this case
        since there is no index col with which the query can be formed; remember that
        the index column is an auto_increment one and therefore not really a part of
        the record. Check the item description and the URL independently. */
       
$item = $this->getColDefsVal('item');
       
$URL = $this->getColDefsVal('URL');
        if (
$item === false or $URL === false) {
           
addToErrorMsg('could not get value of item/URL');
            return
false;
        }
       
$script = new mstrScripts;
       
$result = $script->readMasterRecord('menuItem', "lower(item)=lower('$item')",
           
"limit 1");
        if (
$result === false) {
           
addToErrorMsg($script->getErrorMsg());
            return
false;
        } else {
            if (
count($result) > 0) {
                return
1;
            } else {
               
/*
                if the item is a parent, its URL need not be given. Check if a record
                with the same URL exists only if the URL is non blank. The check that
                URL cannot be blank for non-parents will be performed elsewhere in the
                flow. The check that item itself cannot be blank will also be
                performed elsewhere. */
               
if (trim($URL) <> '') {
                   
$result = $script->readMasterRecord('menuItem', "lower(url)=lower('$URL')",
                       
"limit 1");
                    if (
$result === false) {
                       
addToErrorMsg($script->getErrorMsg());
                        return
false;
                    } else {
                        if (
count($result) > 0) {
                            return
1;
                        }
                    }
                }
            }
        }
        return -
1; // for record not already present
   
}


    function
canDelete()
    {
       
/**
         * THERE IS NO TABLE IN THIS DEMO SET WHICH WILL ALLOW US TO DETERMINE WHETHER A
         * RECORD CAN BE DELETED. THEREFORE THIS FUNC ARBITRARILY RETURNS A YES. THE SAME
         * EFFECT WOULD BE ACHIEVED IF THIS FUNCTION WAS LEFT OUT ALTOGETHER.
         */
       
return "yes";
    }
}


if (!isset(
$_REQUEST['actn'])) {
   
$obj = createTableObject();
} else {
   
/* if the user has taken some action, handle it. */
   
$obj = handleRequestOption();
}


$form = new mstrFH($obj, 'b');
$form->setDemoNotes(mnuItmNotes());
$form->displayForm();
?>