Location: PHPKode > projects > TEMS -Time and Expense Management System > tems/baseclass/PageBase.php
<?php

/* * *******************************************************************************
 * TES is a Time and Expense Management program developed by
 * Initechs, LLC. Copyright (C) 2009 - 2010 Initechs LLC.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY INITECHS, INITECHS DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 *
 * You can contact Initechs headquarters at 1841 Piedmont Road, Suite 301,
 * Marietta, GA, USA. or at email address hide@address.com
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display od the "Initechs" logo.
 * If the display of the logo is not reasonably feasible for technical reasons,
 * the Appropriate Legal Notices must display the words "Powered by Initechs".
 * ****************************************************************************** */

$basedir = dirname(__FILE__) . '/..';
require_once("$basedir/baseclass/PageSection.php");
require_once("$basedir/baseclass/History.php");
require_once("$basedir/baseclass/ExtractDisplayStructure.php");
require_once("$basedir/baseclass/GetAuthorization.php");

class dbObj extends DBCommonFunctions {

}

abstract class PageBase extends PageSection {

    protected function ListPage($ControlDS, $heading='') {
        $SearchDisplayDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->searchLayout);
        $searchLayoutArray = $SearchDisplayDS->layoutArray;

        $DisplayControlDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->displayLayout);
        $listLayoutArray = $DisplayControlDS->layoutArray;
        $heading = $heading != '' ? $heading : $DisplayControlDS->heading;

        $where = $this->buildWhereClause($ControlDS, $searchLayoutArray);

        $DataSet = $this->LoadListPageData($ControlDS, $DisplayControlDS, $where);

        $start = $this->setRecordOffset();

        $Data = new dbObj();
        $TotalRecs = $Data->countRows($ControlDS->table, $where);

        $navigation = $this->buildNavigation($start, $TotalRecs);

        $str = '';

        $str .= $this->buildSearchForm($ControlDS, $searchLayoutArray, $heading);
        $str .= $this->showTable($ControlDS, $listLayoutArray, $DataSet, $navigation);

        unset($_SESSION['error']);
        unset($_SESSION['message']);
        return $str;
    }

    protected function CreatePage($ControlDS, $heading='') {

        $DisplayControlDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->displayLayout);
        $layoutArray = $DisplayControlDS->layoutArray;

        if ((isset($_GET['uid'])
                and ($_GET['uid']) != '')
                and (isset($_GET['load'])
                and ($_GET['load']) == 'Yes')) {
            if ($this->LoadPageData($ControlDS, $DisplayControlDS, $_GET['uid'], 'nohistory') == null)
                return getMessage('er0001');
        }

        if (!isset($_SESSION['error'])
                and ((!isset($_GET['load'])
                or ($_GET['load']) <> 'Yes')))
            unset($_SESSION['postdata']);

        if (!isset($_SESSION['error']))
            $this->loadParentKeyValues($ControlDS);

        $str = $this->showPageWithData($ControlDS, $DisplayControlDS, $heading);
        unset($_SESSION['error']);
        unset($_SESSION['message']);
        return $str;
    }

    protected function EditPage($ControlDS, $heading='') {
        $DisplayControlDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->displayLayout);
        $layoutArray = $DisplayControlDS->layoutArray;

        if ((!isset($_GET['uid'])
                or ($_GET['uid']) == ''))
            return getMessage('er0001');

        $auth = new userauthorization();
        if (!$auth->checkauthority_for_display($ControlDS, $_GET['uid']))
            return getMessage('er0041');

        if ((isset($_GET['load'])
                and ($_GET['load']) == 'Yes')) {
            if ($this->LoadPageData($ControlDS, $DisplayControlDS, $_GET['uid']) == null)
                return getMessage('er0001');
        }

        $str = $this->showPageWithData($ControlDS, $DisplayControlDS, $heading);
        unset($_SESSION['error']);
        unset($_SESSION['message']);
        return $str;
    }

    protected function DeletePage($ControlDS, $heading='') {
        $DisplayControlDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->displayLayout);
        $layoutArray = $DisplayControlDS->layoutArray;

        if (!isset($_GET['uid'])
                or ($_GET['uid']) == '')
            return getMessage('er0001');

        $auth = new userauthorization();
        if (!$auth->checkauthority_for_display($ControlDS, $_GET['uid']))
            return getMessage('er0079');

        if ($this->LoadPageData($ControlDS, $DisplayControlDS, $_GET['uid'], 'nohistory') == null)
            return getMessage('er0001');

        $str = $this->showPageWithData($ControlDS, $DisplayControlDS, $heading);
        unset($_SESSION['error']);
        unset($_SESSION['message']);
        return $str;
    }

    protected function BrowsePage($ControlDS, $heading='') {
        $DisplayControlDS = new DisplayControlStructure($ControlDS->folder, $ControlDS->displayLayout);
        $layoutArray = $DisplayControlDS->layoutArray;

        if (!isset($_GET['uid'])
                or ($_GET['uid']) == '')
            return getMessage('er0001');

        $auth = new userauthorization();

        if (!$auth->checkauthority_for_display($ControlDS, $_GET['uid']))
            return getMessage('er0042');

        if ($this->LoadPageData($ControlDS, $DisplayControlDS, $_GET['uid']) == null)
            return getMessage('er0001');

        $str = $this->showPageWithData($ControlDS, $DisplayControlDS, $heading);
        unset($_SESSION['error']);
        unset($_SESSION['message']);
        unset($_SESSION['postdata']);
        return $str;
    }

    protected function LoadPageData($ControlDS, $DisplayControlDS, $uid, $history='') {
        global $basedir;

        $callFunc = '';

        $DbObj = new dbObj();
        $RowData = $DbObj->fetchRow($ControlDS->table, 'uid', $uid);

        if ($RowData == null)
            return null;

        // Augment the DataSet with fields that are not in the table. These information comes from specific function calls.

        unset($callFunc); // To avoid warning of $callFunc not defined - see few lines below.
        foreach ($DisplayControlDS->layoutArray as $field => $fieldAttribute) {
            if (isset($fieldAttribute['functionName'])) {
                require_once("$basedir/$ControlDS->folder/configfunctions.php");
                if (!isset($callFunc))
                    $callFunc = new DBConfigFunctions();

                $functionName = $fieldAttribute['functionName'];
                $fieldvalue = $callFunc->$functionName($RowData);
                $RowData[$field] = $fieldvalue;
            }
        }

        $_SESSION['postdata'] = $RowData;

        if (strtolower(substr($history, 0, 2) <> 'no')) {
            $historyObj = new History();
            $historyObj->addToHistory("$uid");
        }

        return $RowData;
    }

    protected function loadParentKeyValues($ControlDS) {
        if (!isset($_SESSION['parentkey'][$ControlDS->module]))
            return;

        foreach ($_SESSION['parentkey'][$ControlDS->module] as $key => $value)
            $_SESSION['postdata'][$key] = $value;
    }

    protected function buildWhereClause($ControlDS, $displayLayout) {
        $dateFormat = getUserDateFormat();

        if ($displayLayout == '')
            return;

        $where = '';
        foreach ($displayLayout as $fieldName => $fieldDetail) {
            $fieldValue = '';

            if ((array_search($fieldName, $ControlDS->parent_keys) !== false)
                    and (isset($_SESSION['parentkey'][$ControlDS->module][$fieldName])))
                $fieldValue = $_SESSION['parentkey'][$ControlDS->module][$fieldName];
            else if (isset($_GET[$fieldName])
                    and trim($_GET[$fieldName]) <> '')
                $fieldValue = htmlspecialchars(trim($_GET[$fieldName]));

            if ((strtolower($fieldDetail['fieldType']) == 'datefield')
                    and ($fieldValue <> ''))
                $fieldValue = convertdate($fieldValue, $dateFormat, 'ymd');
            if (($fieldValue <> '')
                    and ($fieldValue <> '*')) {
                if ((!isset($fieldDetail['nontable']))
                        or (strtolower($fieldDetail['nontable']) <> 'yes')) {
                    if ($where <> '')
                        $where .= " AND ";
                    $where .= "$fieldName like '$fieldValue'";
                }
            }
        }

        $loggedinUser = loggedUserID();
        $Auth = new userauthorization();

        $authority = $Auth->chkauthorityLevel($ControlDS->module, 'List');

        if ($authority == 'none') {
            if ($where <> '')
                $where .= " AND ";
            $where .= "createby = ''";
        }
        else if ($authority == 'own') {
            if ($where <> '')
                $where .= " AND ";
            $where .= "(createby = '$loggedinUser'";
            if (($ControlDS->table == 'expensedetails')
                    or ($ControlDS->table == 'expenseheaders')
                    or ($ControlDS->table == 'times')
                    or ($ControlDS->table == 'users'))
                $where .= " OR users_id = '$loggedinUser'";
            $where .= ")";
        }

        return $where;
    }

    protected function buildSortBy() {
        $sortBy = isset($_GET['sort']) ? $_GET['sort'] : '';
        return $sortBy;
    }

    protected function buildAscDec() {
        $ascdec = isset($_GET['ascdec']) ? $_GET['ascdec'] : '';
        return $ascdec;
    }

    protected function setRecordOffset() {
        $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0;
        return $offset;
    }

    protected function AddBrowseButton(&$DataSet, $dataIndex, $ControlDS, $uid, $dataRow) {
        $DataSet[$dataIndex]['browserec'] = "<a href=index.php?module=$ControlDS->module&action=Browse&uid=$uid>"
                                                . "<img border=0 width=20 height=20 src='images/browse.png'>" . '</a>';
        return;
    }

    protected function AddAddlData(&$DataSet, $dataIndex, $dataRow, $ControlDS, $uid) {
        return;
    }

    protected function ExtendDataSet_byNonDbField(&$DataSet, $ControlDS, $layoutArray) {
        // Augment the DataSet with fields that are not in the table. These information comes from specific function calls.

        global $basedir;
        $callFunc = '';

        unset($callFunc); // To avoid warning of $callFunc not defined - see few lines below.
        foreach ($layoutArray as $field => $fieldAttribute) {
            if (isset($fieldAttribute['functionName'])) {
                require_once("$basedir/$ControlDS->folder/configfunctions.php");
                if (!isset($callFunc))
                    $callFunc = new DBConfigFunctions();

                $functionName = $fieldAttribute['functionName'];
                foreach ($DataSet as $dataIndex => $dataRow) {
                    $DataSet[$dataIndex][$field] = $callFunc->$functionName($dataRow);
                }
            }
        }
    }

    protected function ReplaceDataSetValues_byHumanReadable(&$DataSet, $ControlDS, $layoutArray) {
        $dateFormat = getUserDateFormat();

        $Data = new dbObj();

        foreach ($DataSet as $dataIndex => $dataRow) {
            foreach ($dataRow as $field => $value) {
                if (!isset($layoutArray[$field]))
                    continue;

                if (($layoutArray[$field]['fieldType'] == 'optionField')
                        or ($layoutArray[$field]['fieldType'] == 'radioField'))
                    $value = changeLiteral($layoutArray[$field]['validValues'][$value]);

                if ($layoutArray[$field]['fieldType'] == 'dateField')
                    $value = cvtDateIso2Dsp($value, $dateFormat);

                if (isset($layoutArray[$field]['lookup'])) {
                    $refTable = $layoutArray[$field]['lookup']['table'];
                    $refId = $layoutArray[$field]['lookup']['field'];
                    $refRow = $Data->fetchRow($refTable, $refId, $value);
                    $refModule = $layoutArray[$field]['lookup']['module'];
                    $value = "<a href=index.php?module=$refModule&action=Browse&uid={$refRow['uid']}>" . $value . '</a>';
                }
                $DataSet[$dataIndex][$field] = $value;
            }

            if (!isset($dataRow['uid']))
                $dataRow['uid'] = 0;

            $this->AddBrowseButton(&$DataSet, $dataIndex, $ControlDS, $dataRow['uid'], $dataRow);
            $this->AddAddlData(&$DataSet, $dataIndex, $dataRow, $ControlDS, $dataRow['uid']);
        }
    }

    protected function LoadListPageData($ControlDS, $DisplayControlDS, $where='') {
        global $ini_array;
        $recs_per_page = $ini_array['display']['records_per_page'];

        $layoutArray = $DisplayControlDS->layoutArray;

        $ControlDS = $this->overrideUIDS($ControlDS);

        $start = $this->setRecordOffset();
        $sortBy = $this->buildSortBy();
        $ascdec = $this->buildAscDec();

        $Data = new dbObj();
        $DataSet = $Data->listRows($ControlDS->table, $where, $sortBy, $ascdec, $start, $recs_per_page);

        $this->ExtendDataSet_byNonDbField(&$DataSet, $ControlDS, $layoutArray);
        $this->ReplaceDataSetValues_byHumanReadable(&$DataSet, $ControlDS, $layoutArray);

        return $DataSet;
    }

    protected function overrideUIDS($ControlDS) {
        return $ControlDS;
    }

}

?>
Return current item: TEMS -Time and Expense Management System