Этот коммит содержится в:
2017-01-26 05:21:02 +03:00
родитель a52e24ae45
Коммит b6f7196aa5
21 изменённых файлов: 681 добавлений и 12 удалений

Просмотреть файл

@@ -16,14 +16,44 @@ use PTEST\M\CAPTCHAModel;
class IndexController extends Controller {
public function indexAction() {
public function indexAction( $number=1 ) {
$view = new View('index');
$view->page_title = "Доска объявлений";
$model = new Model();
$view->pages_count = $model->countPages();
$view->current_page = $number;
$model = null;
$view->sort_by = 'date';
$view->sort_order = 'asc';
$view->ads = Model::getForPage( (int) $number );
$view->render();
}
public function pageAction( $number ) {
public function sortAction( $column, $order ,$number=1 ) {
$view = new View('index');
$view->page_title = "Доска объявлений";
$model = new Model();
$view->pages_count = $model->countPages();
$view->current_page = $number;
$model = null;
$view->sort_by = $column;
$view->sort_order = $order;
if ($column=='date')$column='pdate';
if ($column=='user')$column='username';
if ($column=='mail')$column='email';
$view->ads = Model::getForPage( (int) $number, $column, $order);
$view->render();
}
public function fullAction( $number ) {
$model = Model::findFirst( 'id = '. $number );
if ( ! $model ) {
$this->app->routeRun('error404');
}
$view = new View('full');
$view->ad = $model;
$model = null;
$view->render();
}
public function newAction() {

Просмотреть файл

@@ -28,6 +28,13 @@ class AdModel extends Model{
protected $table_pdate;
/**
* @return mixed
*/
public function getId() {
return (int) $this->table_id;
}
/**
* @param $username
*
@@ -74,7 +81,7 @@ class AdModel extends Model{
*/
public function setHomepage( $homepage ) {
if ( preg_match('|https?://.*\..*|i', $homepage ) ) {
$this->table_homepage = $homepage;
$this->table_homepage = strip_tags( $homepage );
return true;
} else {
return "Введите коректный адрес";
@@ -143,4 +150,21 @@ class AdModel extends Model{
public function getDate() {
return $this->table_pdate;
}
public function countPages() {
return ceil($this->count()/25);
}
/**
* @param int $page_number
* @param string $sort_by
* @param string $order
*
* @return mixed
*/
static public function getForPage( $page_number = 1, $sort_by = 'pdate', $order = 'ASC' ) {
$class_name = __CLASS__;
$offset = ($page_number - 1) * 25;
return $class_name::find(" 1 ORDER BY $sort_by $order LIMIT $offset,25");
}
}

Просмотреть файл

@@ -7,10 +7,8 @@ require __DIR__ . '/inc/head.php';
$this->content = <<<HTML
<article>
<h1 class="page-title">$this->page_title</h1>
<div class="content">
<p>К&nbsp;сожению, страница не&nbsp;найдена.
<p>Попробуйте проследовать на&nbsp;<a href="/" title="Главная">главную</a>.
</div>
</article>
HTML;

32
src/V/full.php Обычный файл
Просмотреть файл

@@ -0,0 +1,32 @@
<?php
$email_filter = function ($email) { return str_replace( [ '@', '.' ], [ ' &lt;at&gt; ', ' &lt;dot&gt; ' ], $email ); };
$h1 = function ($text) {$t = substr(trim($text),0,50); return substr($t, 0,strrpos($t,' ')).'&hellip;'; };
$text = function ($t) {return trim( preg_replace('/<br>(\s*)?<br>/','<p>',str_replace(["\n"],['<br>'], preg_replace("/[\t\ ]{2,}/",' ',$t))) ); };
$this->page_title = 'Объявление №'.$this->ad->getId().' '.$h1($this->ad->getText());
require __DIR__ . '/inc/head.php';
ob_start();
?>
<article>
<h1 class="page-title"><?php echo $this->page_title; ?></h1>
<a href="/" class="to-front">&larr;Главная</a>
<header>
<div>
<span><i class="i-date"></i><?php echo $this->ad->getDate(); ?></span>
<span><i class="i-user"></i><?php echo $this->ad->getUserName(); ?></span><br>
<span><i class="i-email"></i><?php echo $email_filter($this->ad->getEmail()); ?></span>
<?php
if ( ! empty( $this->ad->getHomepage() ) ) {
echo '<br><span><i class="i-homepage"></i><a href="' . $this->ad->getHomepage() . '" target="_blank">' . $this->ad->getHomepage() . '</a>';
}
?>
</div>
</header>
<p><?php echo $text( $this->ad->getText() ); ?>
</article>
<?php
$this->content = ob_get_contents();
ob_end_clean();
require __DIR__ . '/inc/body.php';

Просмотреть файл

@@ -3,7 +3,7 @@
<header class="header">
<h1 class="header__title"><?php echo $this->site_name; ?></h1>
</header>
<section role="main">
<section role="main" class="content">
<?php echo $this->content; ?>
</section>
<footer class="footer">

Просмотреть файл

@@ -6,5 +6,5 @@
<meta charset="utf-8">
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
<title><?php echo $this->page_title; ?></title>
<title><?php echo $this->site_name.'&mdash;'.$this->page_title; ?></title>
</head>

Просмотреть файл

@@ -1,3 +1,124 @@
<?php
require __DIR__ . '/inc/head.php';
$email_filter = function ($email) { return str_replace( [ '@', '.' ], [ ' &lt;at&gt; ', ' &lt;dot&gt; ' ], $email ); };
$h2 = function ($text) {$t = substr(trim($text),0,50); return substr($t, 0,strrpos($t,' ')).'&hellip;'; };
$preview = function ($text) {$t = substr( trim( str_replace(["\n"],['<br>'], preg_replace('/<br>(\s*)?<br>/','<p>',preg_replace("/[\t\ ]{2,}/",' ',$text))) ),0,1000); return substr($t, 0,strrpos($t,' ')); };
ob_start();
$one_href = '/date/desc/';
$two_href = '/user/asc/';
$three_href = '/mail/asc/';
$one_arr = '&uarr; ';
$two_arr = '&ensp;&nbsp;';
$three_arr = '&ensp;&nbsp;';
if ($this->sort_by == 'date' && $this->sort_order == 'desc') {
$one_href = '/';
$one_arr = '&darr; ';
}
if ($this->sort_by == 'user') {
$one_href = '/';
$one_arr = '&ensp;&nbsp;';
if ($this->sort_order == 'asc') {
$two_href = '/user/desc/';
$two_arr = '&uarr; ';
} else {
$two_href = '/user/asc/';
$two_arr = '&darr; ';
}
}
if ($this->sort_by == 'mail') {
$one_href = '/';
$one_arr = '&ensp;&nbsp;';
if ($this->sort_order == 'asc') {
$three_href = '/mail/desc/';
$three_arr = '&uarr; ';
} else {
$three_href = '/mail/asc/';
$three_arr = '&darr; ';
}
}
?>
<h1 class="page-title"><?php echo $this->page_title ?></h1>
<div class="sort">
<div class="sort__cell">Сортировка:</div>
<div class="sort__cell"><a href="<?php echo $one_href ?>"><?php echo $one_arr ?>Дата</a></div>
<div class="sort__cell"><a href="<?php echo $two_href ?>"><?php echo $two_arr ?>Пользователь</a></div>
<div class="sort__cell"><a href="<?php echo $three_href ?>"><?php echo $three_arr ?>e-mail</a></div>
</div>
<?php
foreach ($this->ads as $item){
?>
<article>
<header>
<div>
<h2><?php echo $h2( $item->getText() ); ?></h2>
<span><i class="i-date"></i><?php echo $item->getDate(); ?></span>
<span><i class="i-user"></i><?php echo $item->getUserName(); ?></span><br>
<span><i class="i-email"></i><?php echo $email_filter($item->getEmail()); ?></span>
<?php
if ( ! empty( $item->getHomepage() ) ) {
echo '<br><span><i class="i-homepage"></i><a href="' . $item->getHomepage() . '" target="_blank">' . $item->getHomepage() . '</a>';
}
?>
</div>
</header>
<p><?php echo $preview( $item->getText() ); ?>
<footer>
<a href="/full/<?php echo $item->getId(); ?>/">Подробнее</a>
</footer>
</article>
<?php
}
if ($this->pages_count>1) {
$pages = [];
$prefix_url = ($this->sort_by == 'date' && $this->sort_order == 'asc')?'':'/'.$this->sort_by.'/'.$this->sort_order;
$start = $this->current_page - 2;
$start = ($start<=0)?1:$start;
$end = $this->current_page + 2;
$end = ($end<=$this->pages_count)?$end:$this->pages_count;
if ((($end - $start) < 4) && ($this->pages_count > 4)) {
$end++;
if ((($end - $start) < 4) && ($this->pages_count > 4)) {
$end++;
}
};
$end = ($end<=$this->pages_count)?$end:$this->pages_count;
if ($this->current_page > 3) {
$pages[]=['В&nbsp;начало' , $prefix_url . '/'];
}
for ($i=$start;$i<=$end;$i++){
$pages[]=[ $i, $prefix_url . '/page/' . $i . '/'];
}
if ($this->current_page != $this->pages_count) {
$pages[]=['дальше' , $prefix_url . '/page/' . ( $this->current_page + 1 ). '/'];
}
?>
<footer>
<div class="pager"><?php
foreach ($pages as $value) {
if ((int)$this->current_page != (int)$value[0] ) {
?>
<div class="pager__page<?php if ((int)$value[0]==0)echo " pager__page--string";?>"><a href="<?php echo $value[1];?>"><?php echo $value[0];?></a></div>
<?php
} else { ?>
<div class="pager__page pager__page--current"><span><?php echo $value[0];?></span></div>
<?php
}
}
?>
</div>
</footer>
<?php
}
$this->content = ob_get_contents();
ob_end_clean();
require __DIR__ . '/inc/body.php';

Просмотреть файл

@@ -6,7 +6,8 @@ ob_start();
?>
<article>
<h1 class="page-title"><?php echo $this->page_title ?></h1>
<div class="content">
<a href="/" class="to-front">&larr;Главная</a>
<div>
<form action="" method="post">
<div class="form-element<?php echo ($this->errors->name)?' form-element--error':''; ?>">
<input class="form-element__input" type="text" name="name" placeholder="Имя" tabindex="1" value="<?php echo ($this->post_data->name)?$this->post_data->name:'';?>">

Просмотреть файл

@@ -29,7 +29,13 @@ $config = (object) [
$app = new PFRM\App( $config );
$app->setRoute( '/^\/CAPTCHA.png\?*\d*$/', 'CAPTCHA' );
$app->setRoute( '|^/page/(?<id>\d+)/$|', 'Index', 'page');
$app->setRoute( '|^/page/((?<id>\d+)/)?$|', 'Index', 'index');
$app->setRoute( '|^/(?<column>date)/(?<order>desc)/(page/(?<id>\d+)/)?$|', 'Index', 'sort');
$app->setRoute( '|^/(?<column>user)/(?<order>asc)/(page/(?<id>\d+)/)?$|', 'Index', 'sort');
$app->setRoute( '|^/(?<column>user)/(?<order>desc)/(page/(?<id>\d+)/)?$|', 'Index', 'sort');
$app->setRoute( '|^/(?<column>mail)/(?<order>asc)/(page/(?<id>\d+)/)?$|', 'Index', 'sort');
$app->setRoute( '|^/(?<column>mail)/(?<order>desc)/(page/(?<id>\d+)/)?$|', 'Index', 'sort');
$app->setRoute( '|^/full/(?<id>\d+)/$|', 'Index', 'full');
$app->setRoute( '|^/new/$|', 'Index', 'new');
$app->setRoute( '|^/view_table/((?<id>\d+)/)*$|', 'Index', 'table');
$app->setRoute( '/^.*$/', 'error404' );