<?php
/**
* Set of most common classes implementing UI of IS
* @package default
* @author Kamil Dudka <xdudka00@gmail.com>
*/
/**
* Set of base classes used by almost all pages of IS
*/
include_once ("base.php");
/**
* Set of most common classes accessing to MySQL db
*/
include_once ("db.php");
/**
* TopBar widget. It is usualy displayed at top of page.
* @package default
*/
class TopBar extends HtBlock {
public function __construct ()
{
UserAuth::singleton();
parent::__construct ("div", NULL, "#topBar");
HtInline::factory ("div", $this, "#masterHead")-> addText (HtTitle::TITLE_PREFIX);
HtInline::factory ("div", $this, "#subHead")->addText ("Předprodej jízdenek na autobus...");
$this->loginStatus= HtInline::factory ("div", $this, "#loginStatus");
}
public function draw()
{
$szUser= UserAuth::singleton()-> getUser();
$szLoginStatus= "Uživatel: ";
$szLoginStatus.= (empty ($szUser)) ? "<b>nepřihlášen</b>":
"<b>".$szUser."</b> ".
"[ <a href=\"".Page::linkToWebRoot()."/login".Page::phpFileExt()."?op=logout\">odhlásit</a> ]";
$this->loginStatus-> addText($szLoginStatus);
parent::draw();
}
private $loginStatus;
};
/**
* Menu base class. Simple menu is created by derivating from this class.
* @package default
*/
class Menu extends HtBlock {
public function __construct ($szTitle)
{
parent::__construct ("div", NULL, ".menu");
HtInline::factory ("div", $this, ".menuTitle")-> addText ($szTitle);
$this->list= HtBlock::factory ("ul", $this);
}
public static function relToAbs ($szHref)
{
return Page::linkToWebRoot()."/".$szHref;
}
public function addItem ($szHerf, $szText, $szStyle="")
{
$li= HtInline::factory ("li", $this->list, $szStyle);
if (!isset($szHerf) && !isset($szText))
$li-> addText(' ');
else
HtInline::factory ("a", $li, "", Array("href" => $szHerf))-> addText($szText);
return $li;
}
public function getInnerList ()
{
return $this->list;
}
private $list;
};
class MainMenu extends Menu {
public function __construct ($szTitle, $szFile, $layout, $szPage="")
{
parent::__construct ($szTitle);
$layout-> addToBegin ($this);
$this->szPage= $szPage;
$this->szFile= $szFile;
}
public function addMainMenuItem ($szPage, $szText)
{
$szPostfix= (empty($szPage)) ? '':"?page=$szPage";
$this->addItem (Menu::relToAbs($this->szFile.Page::phpFileExt().$szPostfix), $szText, ($this->szPage== $szPage) ? '.menuItemSelected':'');
}
public function draw()
{
$this->addItem (NULL, NULL);
$this->addItem (Menu::relToAbs("login".Page::phpFileExt()."?op=setPasswd"), "Změnit heslo", ($this->szPage=='setPasswd') ? '.menuItemSelected':'');
$this->addItem (Menu::relToAbs("login".Page::phpFileExt()."?op=logout"), "Odhlásit");
parent::draw();
}
private $szPage;
private $szFile;
};
class MenuManager extends MainMenu {
public function __construct ($layout, $page="")
{
parent::__construct ('Manažer', 'manager', $layout, $page);
$this->addMainMenuItem ('dealers', 'Prodejci');
$this->addMainMenuItem ('transporters', 'Dopravci');
$this->addMainMenuItem ('users', 'Správa uživatelů');
$this->addMainMenuItem ('firms', 'Import/export firem');
}
};
class MenuTransporter extends MainMenu {
public function __construct ($layout, $page="")
{
parent::__construct ('Dopravce', 'transporter', $layout, $page);
/*$this->addMainMenuItem ('', 'Statistika');*/
$this->addMainMenuItem ('routes', 'Spoje');
}
};
class MenuDealer extends MainMenu {
public function __construct ($layout, $page="")
{
parent::__construct ('Prodejce', 'dealer', $layout, $page);
/*$this->addMainMenuItem ('', 'Statistika');*/
$this->addMainMenuItem ('routes', 'Dostupné spoje');
}
public function draw()
{
$szCart='Nákupní košík';
$szOrders='Objednávky';
$szUser= UserAuth::singleton()-> getUser();
$q= new DbQuery (
"SELECT COUNT(cart.seat_id) AS cnt ".
"FROM cart WHERE user='$szUser'"
);
if ($q->getNumRows()) {
$res= $q->fetchArray();
$cnt= $res['cnt'];
if ($cnt)
$szCart= '<b>Nákupní košík ('.$cnt.')</b>';
}
$q= new DbQuery (
"SELECT ico FROM users WHERE user='$szUser'"
);
$row= $q->fetchArray();
$ico= $row['ico'];
$q= new DbQuery (
"SELECT COUNT(*) AS cnt ".
"FROM users JOIN orders USING (user) ".
"WHERE ico='$ico'"
);
$res= $q->fetchArray();
$cnt= $res['cnt'];
if ($cnt)
$szOrders.= ' ('.$cnt.')';
$this->addMainMenuItem ('cart', $szCart);
$this->addMainMenuItem ('orders', $szOrders);
parent::draw();
}
};
/**
* Page layout base class.
* @package default
*/
class Layout extends HtBlock {
public function __construct()
{
parent::__construct ("div", NULL, "#layout");
$this->begin= new TextList;
$this->end= new TextList;
}
public function addToBegin (Text $text)
{
$this->begin-> add($text);
}
public function addToEnd (Text $text)
{
$this->end-> add($text);
}
public function drawBegin()
{
Page::singleton()-> drawBegin();
parent::drawBegin();
$this->begin-> draw();
}
public function drawEnd()
{
$this->end-> draw();
parent::drawEnd();
Page::singleton()-> drawEnd();
}
private $begin;
private $end;
};
/**
* Basic page layout template
* @package default
*/
class BasicLayout extends Layout {
public function __construct()
{
parent::__construct();
$this->addToBegin (new TopBar);
}
};
class QueryResult extends HtBlock {
public function __construct($parent, $colsHead, $colsStyles= Array())
{
parent::__construct ("table", $parent, ".queryResult", Array(
"cellpadding" => "0",
"cellspacing" => "0"
));
$tr= HtInline::factory ("tr", $this, ".head");
foreach ($colsHead as $szThText)
HtInline::factory ("td", $tr)-> addText ($szThText);
$this->bOdd= true;
$this->colsStyles= $colsStyles;
}
public function addRow ($tableRow)
{
$tr= HtBlock::factory ("tr", $this, $this->bOdd ? ".odd" : ".even");
$i=0;
foreach ($tableRow as $szTdText) {
HtInline::factory ("td", $tr, @$this->colsStyles[$i])-> addText ($szTdText);
$i++;
}
$this->bOdd= !($this->bOdd);
}
protected $bOdd;
protected $colsStyles;
};
class QueryResultGroupBy extends QueryResult {
public function __construct ($parent, $colsHead, $colsStyles, $colsGrouped)
{
parent::__construct ($parent, $colsHead, $colsStyles);
$this->colsGrouped= $colsGrouped;
$this->counter= 0;
$this->bOddGroup= true;
}
public function addRow ($tableRow, $bKeyRow)
{
$bOdd=$this->counter %2;
if ($bKeyRow) {
$this->iAddedToKey= 1;
$this->bOddGroup= !($this->bOddGroup);
} else {
$this->iAddedToKey ++;
}
if ($this->counter && $bKeyRow)
$szStyle= $bOdd ? '.oddGroup' : '.evenGroup';
else
$szStyle= $bOdd ? '.odd' : '.even';
$tr= HtBlock::factory ("tr", $this, $szStyle);
$i=0;
foreach ($tableRow as $szTdText) {
if (@$this->colsGrouped[$i]) {
if ($bKeyRow) {
$td= HtInline::factory ("td", $tr, @$this->colsStyles[$i]);
$td-> addText ($szTdText);
$this->keyTds[$i]= $td;
} else {
$td= $this->keyTds[$i];
$td->setAttr ('rowspan', $this->iAddedToKey);
}
$td= $this->keyTds[$i];
$td->setStyle (($this->bOddGroup) ? '.odd' : '.even');
} else
HtInline::factory ("td", $tr, @$this->colsStyles[$i])-> addText ($szTdText);
$i++;
}
$this->counter ++;
}
public function getKeyArray() { return $this->keyTds; }
private $colsGrouped;
private $keyTds;
private $iAddedToKey;
private $counter;
private $bOddGroup;
};
class QueryResultMulticol extends HtBlock {
public function __construct($parent, $colsHead, $colsStyles=Array(), $numRows, $maxColCount, $rowCount=8)
{
$colCount= max (1, min ($maxColCount, (int) ($numRows/$rowCount)));
$this->rowCount= (int) $numRows/$colCount;
$this->row=0;
$this->col=0;
parent::__construct ('table', $parent, '.multiCol', Array(
'cellpadding' => 0,
'cellspacing' => 0
));
$tr= HtBlock::factory ('tr', $this);
for ($i=0; $i<$colCount; $i++) {
$td= HtBlock::factory ('td', $tr);
$this->colTable[$i]= new QueryResult ($td, $colsHead, $colsStyles);
}
}
public function addRow ($tableRow)
{
if ($this->row++ >=$this->rowCount) {
$this->row= 1;
$this->col++;
}
$queryResult= $this->colTable[$this->col];
$queryResult-> addRow ($tableRow);
}
private $colTable;
private $rowCount;
private $row;
private $col;
};
class SimpleForm extends HtBlock {
const ERR_INPUT_MESSAGE= 'Špatně vyplněná políčka formuláře jsou označena <span class="errInput">červeně</span>.';
public function __construct ($parent, $szAction, $szSubmitText, $arrayHidden= Array(), $bErrInput=false)
{
parent::__construct ('div', $parent, '.simpleForm');
$form= HtBlock::factory ('form', $this, '', Array (
'action' => $szAction,
'method' => "post"
));
$this->table= HtBlock::factory ('table', $form, '', Array (
'cellpadding' => '8',
'cellspacing' => '0'
));
$this->szSubmitText= $szSubmitText;
$this->arrayHidden= $arrayHidden;
$this->bErrInput= $bErrInput;
if ($bErrInput) {
$tr= HtInline::factory ('tr', $this->table);
HtInline::factory ('td', $tr, '.errInputMsg', Array('colspan'=>2))-> addText(self::ERR_INPUT_MESSAGE);
}
}
public static function lastValue ($szName)
{
$value= @@$_POST[$szName];
if (ini_get ("magic_quotes_gpc"))
$value= stripslashes($value);
return htmlspecialchars($value);
}
public function addInput ($szText, $szName, $szType, $szValue, $iLength, $szStyle, $bErrInput=false)
{
if (!$this->bErrInput)
$bErrInput= false;
if (!isset($szValue))
$szValue= self::lastValue($szName);
$tr= HtInline::factory ('tr', $this->table);
HtInline::factory ('th', $tr, $bErrInput ? '.errInput':'')-> addText($szText);
$td= HtInline::factory ('td', $tr);
HtSingle::factory ('input', $td, $szStyle, Array (
'name' => $szName,
'type' => $szType,
'value' => $szValue,
'size' => $iLength,
'maxlength' => $iLength
));
}
public function addCustomInput ($szText, Text $input, $bErrInput=false)
{
if (!$this->bErrInput)
$bErrInput= false;
$tr= HtInline::factory ('tr', $this->table);
HtInline::factory ('th', $tr, $bErrInput ? '.errInput':'')-> addText($szText);
HtInline::factory ('td', $tr)-> add($input);
}
public function draw()
{
$tr= HtInline::factory ('tr', $this->table);
$td= HtInline::factory ('td', $tr, '.submit', Array('colspan'=>'2'));
foreach ($this->arrayHidden as $szName => $szValue)
HtSingle::factory ('input', $td, '', Array (
'name' => $szName,
'type' => 'hidden',
'value' => $szValue
));
HtSingle::factory ('input', $td, '', Array (
'type' => 'submit',
'value' => $this->szSubmitText
));
parent::draw();
}
private $table;
private $szSubmitText;
private $arrayHidden;
private $bErrInput;
};
abstract class CustomInput extends TextList {
public function __construct($szName)
{
parent::__construct();
$this->szName= $szName;
}
public static function chkNum ($num) {
return ereg('[0-9]+', $num);
}
abstract public function isValid();
protected function lastValueCustom ($szPostfix='')
{
return SimpleForm::lastValue ($this->szName.$szPostfix);
}
protected $szName;
};
class DateInput extends CustomInput {
public function __construct($szName)
{
parent::__construct($szName);
}
public function isValid()
{
$month= $this->lastValueCustom ('_month');
$day= $this->lastValueCustom ('_day');
$year= $this->lastValueCustom ('_year');
return
parent::chkNum($month) && parent::chkNum($day) && parent::chkNum($year) &&
(((0<=$year) && ($year<100)) || ((2000<=$year) && ($year<2100))) &&
checkdate ($month, $day, $year);
}
public function draw()
{
$day= $this->lastValueCustom ('_day');
$month= $this->lastValueCustom ('_month');
$year= $this->lastValueCustom ('_year');
if (empty($day) && empty($month) && empty($year)) {
$day= date('d', time());
$month= date('m', time());
$year= date('Y', time());
}
HtSingle::factory('input', $this, '.day', Array (
'name' => $this->szName.'_day',
'type' => 'text',
'value' => $day,
'size' => 2,
'maxlength' => 2
));
$this-> addText('<b>.</b>');
HtSingle::factory('input', $this, '.month', Array (
'name' => $this->szName.'_month',
'type' => 'text',
'value' => $month,
'size' => 2,
'maxlength' => 2
));
$this-> addText('<b>.</b>');
HtSingle::factory('input', $this, '.year', Array (
'name' => $this->szName.'_year',
'type' => 'text',
'value' => $year,
'size' => 4,
'maxlength' => 4
));
parent::draw();
}
};
class TimeInput extends CustomInput {
public function __construct($szName)
{
parent::__construct($szName);
}
public function isValid()
{
$hour= $this->lastValueCustom ('_hour');
$min= $this->lastValueCustom ('_minutte');
return
parent::chkNum($hour) && (0<=$hour) && ($hour<=23) &&
parent::chkNum($min) && (0<=$min) && ($min<=59);
}
public function draw()
{
$hour= $this->lastValueCustom ('_hour');
$mins= $this->lastValueCustom ('_minutte');
if (empty($hour) && empty($mins)) {
$hour= date('H', time());
$mins= date('i', time());
}
HtSingle::factory('input', $this, '.day', Array (
'name' => $this->szName.'_hour',
'type' => 'text',
'value' => $hour,
'size' => 2,
'maxlength' => 2
));
$this-> addText('<b>:</b>');
HtSingle::factory('input', $this, '.month', Array (
'name' => $this->szName.'_minutte',
'type' => 'text',
'value' => $mins,
'size' => 2,
'maxlength' => 2
));
parent::draw();
}
};
class DurationInput extends CustomInput {
public function __construct($szName)
{
parent::__construct($szName);
}
public function isValid()
{
$num= $this->lastValueCustom();
return parent::chkNum ($num) && ($num);
}
public function draw()
{
HtSingle::factory('input', $this, '.duration', Array (
'name' => $this->szName,
'type' => 'text',
'value' => $this->lastValueCustom(),
'size' => 4,
'maxlength' => 4
));
$this-> addText('min');
parent::draw();
}
};
class PriceInput extends CustomInput {
public function __construct($szName)
{
parent::__construct($szName);
}
public function isValid()
{
$num= $this->lastValueCustom();
return parent::chkNum ($num) && ($num);
}
public function draw()
{
HtSingle::factory('input', $this, '.duration', Array (
'name' => $this->szName,
'type' => 'text',
'value' => $this->lastValueCustom(),
'size' => 4,
'maxlength' => 4
));
$this-> addText('Kč');
parent::draw();
}
};
class LoginWidget extends SimpleForm {
public function __construct($parent)
{
parent::__construct ($parent,
Page::linkToWebRoot().'/login'.Page::phpFileExt().'?op=login',
'Přihlásit'
);
$this->addInput ('Uživatel', 'user', 'text', '', 16, 'width:180px');
$this->addInput ('Heslo', 'passwd', 'password', '', 16, 'width:180px');
}
};
class SetPasswdWidget extends SimpleForm {
public function __construct($parent, $szAction, $bCurrentPasswd)
{
parent::__construct($parent, $szAction, 'Změnit heslo', Array ('setPasswd'=>'true'));
if ($bCurrentPasswd)
$this-> addInput ('Váše současné heslo', 'currentPasswd', 'password', '', 16, 'width:180px;');
$this-> addInput ('Nové heslo', 'passwd1', 'password', '', 16, 'width:180px;');
$this-> addInput ('Nové heslo pro kontrolu', 'passwd2', 'password', '', 16, 'width:180px;');
}
};
class SimpleInfo extends HtBlock {
public function __construct ($parent)
{
parent::__construct ('table', $parent, '.simpleInfo', Array(
'cellspacing' => 0,
'cellpadding' => 3
));
}
public function addPair ($name, $value)
{
$tr= HtInline::factory ('tr', $this);
HtInline::factory ('th', $tr)-> addText(isset($name) ? $name.':' : ' ');
HtInline::factory ('td', $tr)-> addText($value);
}
};
class SimpleInfoMulticol extends HtBlock {
public function __construct ($parent, $count)
{
parent::__construct ('table', $parent, '.multiCol', Array(
'cellspacing' => 0,
'cellpadding' => 0
));
$tr= HtBlock::factory ('tr', $this);
for ($i=0; $i<$count; $i++) {
$td= HtBlock::factory ('td', $tr);
$this->colTable[$i]= new SimpleInfo($td);
}
}
public function addPair ($colNumber, $name, $value)
{
$simpleInfo= $this->colTable[$colNumber];
$simpleInfo-> addPair ($name, $value);
}
private $colTable;
};
class StupidIe implements Text {
public static function patchIfNeeded()
{
if (ereg('MSIE ', $browser= $_SERVER['HTTP_USER_AGENT']))
self::patch();
}
private static function patch()
{
$page= Page::singleton();
$page-> addToHead(new self);
$page-> addBodyAttr('onload','stupidIePatch();');
}
public function draw()
{ ?>
<script type="text/javascript">
function stupidIePatch()
{
x= document.getElementById("pageContent"); x.style.width= "100%";
}
</script>
<?php
}
};
?>