Controller -
Свернутый текст
class guestMVCbook
{
public $username;
public $message;
public $error;
function __construct()
{
$this->username = $this->validate($_POST['username']);
$this->message = $this->validate($_POST['message']);
$this->datetime = date('Y-m-d H:i:s');
$this->tplusr = '{USERNAME}';
$this->tplmsg = '{USERCOMMENT}';
$this->block = '{USERCOMMENTS}';
$this->errors = array();
$this->error = false;
}
function validate($var)
{
return mysql_real_escape_string(trim($var));
}
function setErrors()
{
if(empty($this->username))
{
$this->errors['username'][] = 'Поле для имени не может быть пустым';
}
if(empty($this->message))
{
$this->errors['message'][] = 'Поле для сообщения не может быть пустым';
}
if(mb_strlen($this->username) < 3)
{
$this->errors['username'][] = 'Имя должно содержать более 3 символов';
}
}
function get_Request()
{
$model = new Template_model();
if(isset($_POST['Submit']))
{
$this->setErrors();
if(!$this->errors)
{
$this->greenColour();
}
} else {
$model->display_standart();
}
}
function process_data()
{
$model = new Template_model();
$this->get_Request();
if($this->errors)
{
foreach($this->errors as $value => $errors);
foreach($errors as $err);
$model->display_error($value,$err);
}
}
function greenColour()
{
$query = ("INSERT INTO `guestMVC` (`username`, `tplusr`, `message`, `tplmsg`, `block`, `datetime`) VALUES (
'".$this->username."', '".$this->tplusr."', '".$this->message."', '".$this->tplmsg."',
'".$this->block."', '".$this->datetime."')") or die(mysql_error());
$mysql = mysql_query($query);
echo 'Сообщение успешно добавлено <a href="index.php?page=guest">Назад</a>';
}
}
Model -
Свернутый текст
class Template_model
{
public $html;
public $form;
function __construct()
{
$this->html = file_get_contents('class/view/view.tpl');
$this->query = mysql_query("SELECT `username`, `tplusr`, `message`, `tplmsg`, `block`,`datetime` FROM `guestMVC`
ORDER BY `datetime` DESC LIMIT ".(int)$this->now_comments.",".(int)$this->per_page."") or die(mysql_error());
$this->count = mysql_query("SELECT COUNT(*) as count FROM `guestMVC`") or die(mysql_error());
}
function navigation()
{
$count = mysql_fetch_assoc($this->count);
$this->per_page = 7;
$this->now_comments = $this->p * $this->per_page;
$this->max_comments = round($count[count] / $this->per_page);
$this->p = ($_GET['p'] > $this->max_comments) ? '0' : (int)abs($_GET['p']) ;
if($this->p != $this->max_comments)
{
$a = '<a href="index.php?page=guest&p=' . ($this->p + 1) .'">Вперед</a>';
}
if($this->p == $this->max_comments)
{
$a = '<a>Вперед</a>';
}
if($this->p == 0)
{
$ah = '<a>Назад</a>';
}
if($this->p != 0)
{
$ah = '<a href="index.php?page=guest&p=' . ($this->p - 1) .'">Назад</a>';
}
$this->html = str_replace('{NAVIGATION}', $ah .' '. $a , $this->html);
}
function display_standart()
{
Template_model::setCurrentBlock(USERCOMMENTS);
$this->for = preg_match_all("/(?:<div id=\"mvcform\">)(?:[\D\d]+)(?:<\/div>)/u", $this->html, $this->form);
$this->navigation();
$query = mysql_query("SELECT `username`, `tplusr`, `message`, `tplmsg`, `block`,`datetime` FROM `guestMVC`
ORDER BY `datetime` DESC LIMIT ".(int)$this->now_comments.",".(int)$this->per_page."") or die(mysql_error());
while($sql = mysql_fetch_assoc($query))
{
foreach($this->blk[1] as $ss=>$qq)
{
$this->html = str_replace($this->blk[0], ' ', $this->html); // Стираем блок USERCOMMENTS
$bb = str_replace($sql['tplusr'], $sql['username'], $qq); // Добавляем имена
$qqq = str_replace($sql['tplmsg'], $sql['message'], $bb); // Добавляем сообщения
$this->html = str_replace($this->form[0], ' ', $this->html); // Стираем форму
$this->html .= str_replace($qq,$qqq, $qq); // Выводим блок в шаблон
$this->html .= $this->form[0][0]; // Добавляем форму в конец, после комментариев
}
}
echo $this->html; // Выводим шаблон
}
function display_error($type, $err)
{
Template_model::setCurrentBlock(USERCOMMENTS);
$this->for = preg_match_all("/(?:<div id=\"mvcform\">)(?:[\D\d]+)(?:<\/div>)/u", $this->html, $this->form);
$this->navigation();
$query = mysql_query("SELECT `username`, `tplusr`, `message`, `tplmsg`, `block`,`datetime` FROM `guestMVC`
ORDER BY `datetime` DESC LIMIT ".(int)$this->now_comments.",".(int)$this->per_page."") or die(mysql_error());
while($sql = mysql_fetch_assoc($query))
{
foreach($this->blk[1] as $ss=>$qq)
{
$this->html = str_replace($this->blk[0], ' ', $this->html); // Стираем блок USERCOMMENTS
$bb = str_replace($sql['tplusr'], $sql['username'], $qq); // Добавляем имена
$qqq = str_replace($sql['tplmsg'], $sql['message'], $bb); // Добавляем сообщения
$this->html = str_replace($this->form[0], ' ', $this->html); // Стираем форму
$this->html .= str_replace($qq,$qqq, $qq); // Выводим блок в шаблон
$this->forma = $this->form[0][0];
if($type == 'username')
{
$this->formaa = str_replace('<input type="hidden" name="usererrors">','<span style="color: red;">'. $err .'</span>',
$this->forma);
}
elseif($type == 'message')
{
$this->formaa = str_replace('<input type="hidden" name="messageerrors">',
'<span style="color: red; position: absolute; margin-top: 50px;">'. $err .'</span>', $this->forma);
}
}
}
$this->html .= $this->formaa; // Добавляем форму в конец, после комментариев
echo $this->html; // Выводим шаблон
}
function setCurrentBlock($BlockName)
{
$coun = mysql_query("SELECT COUNT(*) as count FROM `guestMVC`") or die(mysql_error());
$count = mysql_fetch_assoc($coun);
$this->blck = preg_match_all("/(?:\{$BlockName\})([\D]+)(?:\{\/$BlockName\})/u", $this->html, $this->blk);
$this->html = str_replace('{COUNTCOMMENTS}', $count['count'], $this->html); //Выводим количество записей в книге
return $this->blk;
}
}