Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
add pager
Browse files Browse the repository at this point in the history
  • Loading branch information
shonenada committed Jun 26, 2014
1 parent 68ddb17 commit 61933e1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions szuias/Controller/MenuList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ static public function get($mid) {
$page = self::$request->get('page') ? self::$request->get('page') : 1;
$pagesize = self::$app->config('pagesize');
$articles = Article::getListByMenuId($page, $pagesize, $mid, array(array('is_top', 'DESC'), array('sort', 'ASC'), array('created', 'DESC')));
$countOfArticles = Article::countByMids(array($mid));
$totalPage = ceil($countOfArticles[1] / $pagesize);
$pageFrom = $page > 3 ? $page - 2 : 1;
$pageTo = $totalPage < $page + 2 ? $totalPage : $page + 2;
return self::render('list.html', get_defined_vars());
}

Expand Down
62 changes: 62 additions & 0 deletions szuias/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,68 @@ public function translate($code) {
return $tras;
}

public function getNext() {
$order_by = array(array('is_top', 'DESC'), array('sort', 'ASC'), array('created', 'DESC'));
$order_str = "";
foreach($order_by as $o) {
if (in_array(strtoupper($o[1]), array('ASC', 'DESC'))) {
$order_str .= sprintf('n.%s %s%s', $o[0], $o[1], $o == $order_by[count($order_by) - 1] ? '' : ', ');
}
}
$dql = sprintf(
'SELECT n FROM %s n '.
'WHERE n.menu_id = %s '.
'AND n.is_deleted = false '.
'AND n.is_hide = false '.
'ORDER BY %s',
get_called_class(),
$this->menu_id, $order_str
);
$query = static::em()->createQuery($dql);
$result = $query->getResult();
$idx = 0;
while ($result[$idx]->id != $this->id) {
$idx++;
}
if (isset($result[$idx + 1])) {
return $result[$idx + 1];
}
else {
return null;
}
}

public function getPrev() {
$order_by = array(array('is_top', 'DESC'), array('sort', 'ASC'), array('created', 'DESC'));
$order_str = "";
foreach($order_by as $o) {
if (in_array(strtoupper($o[1]), array('ASC', 'DESC'))) {
$order_str .= sprintf('n.%s %s%s', $o[0], $o[1], $o == $order_by[count($order_by) - 1] ? '' : ', ');
}
}
$dql = sprintf(
'SELECT n FROM %s n '.
'WHERE n.menu_id = %s '.
'AND n.is_deleted = false '.
'AND n.is_hide = false '.
'ORDER BY %s',
get_called_class(),
$this->menu_id, $order_str
);
$query = static::em()->createQuery($dql);
$result = $query->getResult();
$idx = 0;
while ($result[$idx]->id != $this->id) {
$idx++;
}
if (isset($result[$idx - 1])) {
return $result[$idx - 1];
}
else {
return null;
}
}

static public function getListByTopMenu($size, $top_menu_id, $order_by=array(array('id', 'ASC'))) {
$top_menu = Menu::find($top_menu_id);
$records = array();
Expand Down
11 changes: 10 additions & 1 deletion szuias/templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ <h2>{{ top_menu.title }}</h2>
<div class="info">
{% if article.author.name %}{{ trans('Author') }}: {{ article.author.name }} |{% endif %}
{% if article.created %}{{ trans('Post Date') }}: {{ article.created | date('Y-m-d') }} | {% endif %}
{{ trans('Counts') }}: {{ article.view_count }}<br />
{{ trans('Counts') }}: {{ article.view_count }}
<br />
{% if article.prev %}
上一篇文章:<a href="{{ siteUrl('article/' ~ article.prev.id) }}">{{ article.prev.title }}</a>
{% endif %}
{% if article.prev and article.next %} | {% endif %}
{% if article.next %}
下一篇文章:<a href="{{ siteUrl('article/' ~ article.next.id) }}">{{ article.next.title }}</a>
<br />
{% endif %}
{% if loggedUser.hasPermission('/admin/content/' ~ article.id ~ '/edit', 'GET') %}
<a href="{{ siteUrl('/admin/content/' ~ article.id ~ '/edit') }}">[{{ trans('Edit') }}]</a>
{% endif %}
Expand Down
17 changes: 16 additions & 1 deletion szuias/templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ <h2>{{ top_menu.title }}</h2>
<div class="top-nav">
<a class="current">{{ menu.title }}</a>
</div>
<ul>
<ul class="list">
{% for a in articles %}
<li>{% if a.is_top %}<img src="{{ siteUrl('static/images/admin/icon_up.gif') }}" alt="置顶" title="置顶"> {% endif %}<a href="{{ a.redirect_url ? a.redirect_url : siteUrl('article/' ~ a.id) }}"{{ a.open_style == 1 ? 'target="_blank"' : '' }}>{{ a.title | ellipsis(50) }}</a> <span class="datetime">{{ a.created | date('Y-m-d') }}</span></li>
{% endfor %}
</ul>
<div id="pager" style="margin-top:20px; float: right; font-size: 12px; overflow: hidden;">
<ul class="pager-list">
{% if page > 1 %}
<li><a href="?page={{ page - 1 }}">Prev</a></li>
{% endif %}
{% for p in range(low=pageFrom, high=pageTo, step=1) %}
<li {% if p == page %} class="active"{% endif %}>
<a href="?page={{ p }}">{{ p }}</a>
</li>
{% endfor %}
{% if page < totalPage %}
<li><a href="?page={{ page + 1 }}">Next</a></li>
{% endif %}
</ul>
</div>
</div>

</div>
Expand Down

0 comments on commit 61933e1

Please sign in to comment.