Location: PHPKode > projects > TEMS -Time and Expense Management System > tems/baseclass/PageElement.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__) . '/..';

abstract class PageElement {

    protected function textField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        static $showListSeq = 1;

        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";
        if ($addlAtrb == 'disabled') {
            $str = "$fieldValue";
            return $str;
        }

        $str = "<input $class
			id='inputstring$showListSeq'
			type='text'
			name='$fieldName'
			value='$fieldValue'
			size='$size'
			maxlength='$fieldlen'";

        if ($fieldAction == 'haveselect') {
            $str .= " onkeyup=\"showHints(this.value, '{$fieldDetail['lookup']['table']}', '{$fieldDetail['lookup']['field']}', 'txtHint$showListSeq', '#inputstring$showListSeq')\"";
            $str .= " onblur=\"clearHints('txtHint$showListSeq')\"";
        }

        $str .= ">";

        if ((trim($addlStr) <> '')
                and (strtoupper(trim($addlStr))) <> 'NOCAL')
            $str .= $addlStr;

        if ($fieldAction == 'haveselect') {  // to show search button.
            $buttonHeading = changeLiteral('Search');
            $str .= "\n <input type = 'button' value='$buttonHeading'
			onClick=\"showList('{$fieldDetail['lookup']['module']}', '{$fieldDetail['lookup']['table']}', '{$fieldDetail['lookup']['field']}', '{$ControlDS->formName}', '{$fieldName}')\">";
            $str .= "\n<div id='txtHint$showListSeq' class='suggestionList'></div>";
            ++$showListSeq;
        }
        if (($fieldAction == 'editable')
                and (isset($fieldDetail['helptext']))
                and ($fieldDetail['helptext']) <> '')
            $str .= $fieldDetail['helptext'];
        $str .= $this->fieldError($fieldName);
        return $str;
    }

    protected function textArea($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";
        $row = 1 + ($size - ($size % 20)) / 20;
        $str = "<input $class type='textarea'
			name='$fieldName'
			value='$fieldValue'
			row='$row'
			col='20'
			maxlength='$fieldlen'";

        if (trim($addlAtrb) <> '')
            $str .= " $addlAtrb='$addlAtrb'";
        $str .= ">";
        if ((trim($addlStr) <> '')
                and (strtoupper(trim($addlStr))) <> 'NOCAL')
            $str .= $addlStr;
        $str .= $this->fieldError($fieldName);
        return $str;
    }

    protected function passwordField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";
        $str = "<input $class type='password'
	 		name='$fieldName'
			value='$fieldValue'
			size='$size'
			maxlength='$fieldlen'";
        if (trim($addlAtrb) <> '')
            $str .= " $addlAtrb='$addlAtrb'";
        $str .= ">";

        if (($fieldAction == 'editable')
                and (isset($fieldDetail['helptext']))
                and ($fieldDetail['helptext']) <> '')
            $str .= $fieldDetail['helptext'];
        $str .= $this->fieldError($fieldName);
        return $str;
    }

    protected function dateField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        $dateFormat = getUserDateFormat();

        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";

        if ($fieldValue <> null) {
            $dateField = $fieldValue;
            $fieldValue = cvtDateIso2Dsp($dateField, $dateFormat);
            $fieldValue .= cvtTime2Dsp($dateField);
        }
        if ($addlAtrb == 'disabled') {
            $str = "$fieldValue";
            return $str;
        }
        $str = '';
        if (strtoupper(trim($addlStr)) <> 'NOCAL')
            $str .= "<script language=\"JavaScript\">
							var cal = new CalendarPopup();
					</script>";
        $str .= "<input	$class type='text'
				name='$fieldName'
				value='$fieldValue'
				size='$size'
				maxlength= '$fieldlen'";

        if (trim($addlAtrb) <> '')
            $str .= " $addlAtrb='$addlAtrb'";
        $str .= ">";

        $CalRetDateFmt = 'MM/dd/yyyy';
        $LblDateFmt = 'MM/DD/YYYY';
        $dateFormat = strtolower($dateFormat);

        if ($dateFormat == 'mdy') {
            $CalRetDateFmt = 'MM/dd/yyyy';
            $LblDateFmt = 'MM/DD/YYYY';
        } elseif ($dateFormat == 'dmy') {
            $CalRetDateFmt = 'dd/MM/yyyy';
            $LblDateFmt = 'DD/MM/YYYY';
        } elseif ($dateFormat == 'ymd') {
            $CalRetDateFmt = 'yyyy/MM/dd';
            $LblDateFmt = 'YYYY/MM/DD';
        }

        if (strtoupper(trim($addlStr)) <> 'NOCAL')
            $str .= "<A HREF='#'
			onClick=\"cal.select(document.forms[0].$fieldName,'anchor1', '$CalRetDateFmt');
			return false;\"
			NAME='anchor1'
			ID='anchor1'><img border=0 width=20 height=20 src='images/calendar.png'></A>";
        $str .= $LblDateFmt;
        $str .= $this->fieldError($fieldName);
        return $str;
    }

    protected function optionField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";

        if ($addlAtrb == 'disabled') {
            $str = "$addlStr[$fieldValue]";
            return changeLiteral($str);
        } else {
            $fieldValue <> '' ? $selected = $fieldValue : $selected = $this->postValue($fieldName);
            If ($selected == null)
                $selected = '*';

            $str = "\n<select $class name='$fieldName'>";
            foreach ($addlStr as $key => $value) {
                $str .= "\n<option";
                if ($key == $selected)
                    $str .= " selected='selected'";
                if (trim($addlAtrb) <> '')
                    $str .= " $addlAtrb='$addlAtrb'";

                $value = changeLiteral($value);
                $str .= " value='$key'>$value</option>";
            }

            $str .= "\n</select>";
            $str .= $this->fieldError($fieldName);
        }

        return $str;
    }

    protected function radioField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";

        $str = '';

        if ($addlAtrb == 'disabled') {
            $str = "$addlStr[$fieldValue]";
            return changeLiteral($str);
        }

        $fieldValue <> '' ? $selected = $fieldValue : $selected = $this->postValue($fieldName);

        if ($selected == '')
            $selected = '*';

        $str .= "\n<table>\n<tr>";

        foreach ($addlStr as $key => $value) {
            $str .= "\n<td style='border:0px;'>";
            $str .= "<input $class type='radio'
						name='$fieldName'
						value='$key'";
            if ($key == $selected)
                $str .= " checked='checked'";

            if ($addlAtrb <> '')
                $str .= " $addlAtrb='$addlAtrb'";

            $value = changeLiteral($value);
            $str .= ">$value";
            $str .= "</td>";
        }

        $str .= "\n</tr>\n</table>";
        $str .= $this->fieldError($fieldName);

        return $str;
    }

    protected function searchField($fieldName, $fieldDetail, $fieldAction='', $addlAtrb='', $addlStr='', $ControlDS='') {
        global $basedir;
        $dateFormat = getUserDateFormat();

        $str = '';
        $fieldValue = $this->prvSearchValue($fieldName);
        unset($_SESSION['error'][$fieldName]);

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

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

            $functionName = $fieldDetail['functionName'];
            $fieldValue = $callFunc->$functionName();
        }

        if ((strtolower($fieldDetail['fieldType']) == 'datefield')
                and ($fieldValue <> null))
            $fieldValue = cvtDateIso2Dsp($fieldValue, $dateFormat);

        if (strtolower($fieldAction) == 'viewonly')
            $addlAtrb = 'disabled';

        $str .= $this->$fieldDetail['fieldType']($fieldName, $fieldValue, $fieldDetail['displaysize'], $fieldDetail['fieldlen'], $fieldAction, $addlAtrb, $addlStr, $fieldDetail, $ControlDS);

        if (($addlAtrb == 'disabled')
                and ($fieldDetail['displaytype'] == 'viewonly'))
            $str .= "\n<input type='hidden' name='$fieldName' value='$fieldValue'>";
        return $str;
    }

    protected function prvSearchValue($field) {
        if (isset($_SESSION['parentkey'][$_GET['module']][$field]))
            return (htmlspecialchars($_SESSION['parentkey'][$_GET['module']][$field]));

        if (!isset($_GET[$field]))
            return null;

        return (htmlspecialchars($_GET[$field]));
    }

    protected function fileField($fieldName, $fieldValue, $size, $fieldlen, $fieldAction='', $addlAtrb='', $addlStr='', $fieldDetail='', $ControlDS='') {
        static $showListSeq = 1;

        $this->fieldError($fieldName) <> '' ? $class = "class='error_field'" : $class = "";

        if ($fieldValue)
            $str = "<img border=0px width=120px height=40px src='images/$fieldValue'>";

        if ($addlAtrb == 'disabled')
            return $str;

        $str .= "<input $class
			id='inputstring$showListSeq'
			type='file'
			name='$fieldName'
			accept='image'";
        $str .= ">";

        if (($fieldAction == 'editable')
                and (isset($fieldDetail['helptext']))
                and ($fieldDetail['helptext']) <> '')
            $str .= $fieldDetail['helptext'];
        $str .= $this->fieldError($fieldName);
        return $str;
    }

    protected function hiddenField($fieldName, $fieldType='') {
        $dateFormat = getUserDateFormat();

        $field_value = $this->postValue($fieldName);
        if (strtolower($fieldType) == 'datefield')
            $field_value = cvtDateIso2Dsp($field_value, $dateFormat);

        $str = "<input	type='hidden'
			name='$fieldName'
			value='$field_value'>";
        return $str;
    }

    protected function postValue($field) {
        if (!isset($_SESSION['postdata'][$field]))
            return null;

        return (htmlspecialchars($_SESSION['postdata'][$field]));
    }

    protected function fieldError($fieldName) {
        if (isset($_SESSION['error'][$fieldName]))
            return "\n\t<div class='error_msg'>{$_SESSION['error'][$fieldName]}</div>";
    }

    protected function showMessage($fieldName='') {
        if ($fieldName == '')
            $fieldName = 'record';
        if (isset($_SESSION['message'][$fieldName]))
            return "{$_SESSION['message'][$fieldName]}";
    }

    protected function printField($fieldValue, $style='', $fieldType='textField', $validValues='', $fieldSize=0) {
        $dateFormat = getUserDateFormat();

        if (strtolower(trim($fieldType)) == 'datefield')
            $fieldValue = cvtDateIso2Dsp($fieldValue, $dateFormat);

        if ((strtolower(trim($fieldType)) == 'optionfield')
                or (strtolower(trim($fieldType)) == 'radiofield'))
            $fieldValue = $validValues[$fieldValue];

        if ($fieldSize > 0)
            $fieldValue = substr($fieldValue, 0, $fieldSize - 1);

        if ($fieldValue == '')
            $fieldValue = '&nbsp;';

        if ($style <> '')
            $style = "style='{$style}'";

        $str = "<td $style>$fieldValue</td>";

        return $str;
    }

    protected function printColHeading($fieldValue, $align='') {
        $str = '<td ';
        if ($align <> '')
            $str .= "align=$align";
        $str .= ">$fieldValue</td>";
        return $str;
    }

}

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