Created
October 5, 2020 11:29
-
-
Save atsu666/aed686b7759412dfa50254b38e69d4eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acms\Plugins\AccessRanking\GET\Entry; | |
use Acms\Plugins\AccessRanking\POST\DeleteAccessLog; | |
use ACMS_Filter; | |
use ACMS_GET_Entry_Summary; | |
use SQL; | |
use DB; | |
use SQL_Select; | |
use Template; | |
/** | |
* php/ACMS/User/GET/Sample.php | |
* | |
* テンプレート上では、標準のGETモジュールと同様に、 | |
* '<!-- BEGIN_MODULE Entry_AccessRanking --><!--END_MODULE Entry_AccessRanking -->' で呼び出されます。 | |
*/ | |
class AccessRanking extends ACMS_GET_Entry_Summary | |
{ | |
public $_axis = array( | |
'bid' => 'self', | |
'cid' => 'self', | |
); | |
public $_scope = array( | |
'eid' => 'global', | |
); | |
/** | |
* コンフィグの取得 | |
* | |
* @return array | |
*/ | |
public function initVars() | |
{ | |
return array( | |
'order' => array( | |
$this->order ? $this->order : config('entry_access-ranking_order'), | |
config('entry_access-ranking_order2'), | |
), | |
'limit' => intval(config('entry_access-ranking_limit', '10')), | |
'offset' => intval(config('entry_access-ranking_offset', '0')), | |
'indexing' => config('entry_access-ranking_indexing'), | |
'secret' => config('entry_access-ranking_secret'), | |
'notfound' => config('mo_entry_access-ranking_notfound'), | |
'notfoundStatus404' => config('entry_access-ranking_notfound_status_404'), | |
'noimage' => config('entry_access-ranking_noimage'), | |
'pagerDelta' => config('entry_access-ranking_pager_delta'), | |
'pagerCurAttr' => config('entry_access-ranking_pager_cur_attr'), | |
'unit' => config('entry_access-ranking_unit'), | |
'newtime' => config('entry_access-ranking_newtime'), | |
'imageX' => intval(config('entry_access-ranking_image_x')), | |
'imageY' => intval(config('entry_access-ranking_image_y')), | |
'imageTrim' => config('entry_access-ranking_image_trim'), | |
'imageZoom' => config('entry_access-ranking_image_zoom'), | |
'imageCenter' => config('entry_access-ranking_image_center'), | |
'entryFieldOn' => config('entry_access-ranking_entry_field'), | |
'categoryInfoOn' => config('entry_access-ranking_category_on'), | |
'categoryFieldOn' => config('entry_access-ranking_category_field_on'), | |
'userInfoOn' => config('entry_access-ranking_user_on'), | |
'userFieldOn' => config('entry_access-ranking_user_field_on'), | |
'blogInfoOn' => config('entry_access-ranking_blog_on'), | |
'blogFieldOn' => config('entry_access-ranking_blog_field_on'), | |
'pagerOn' => config('entry_access-ranking_pager_on'), | |
'simplePagerOn' => config('entry_access-ranking_simple_pager_on'), | |
'mainImageOn' => config('entry_access-ranking_image_on'), | |
'detailDateOn' => config('entry_access-ranking_date'), | |
'fullTextOn' => config('entry_access-ranking_fulltext'), | |
'fulltextWidth' => config('entry_access-ranking_fulltext_width'), | |
'fulltextMarker' => config('entry_access-ranking_fulltext_marker'), | |
'tagOn' => config('entry_access-ranking_tag'), | |
'hiddenCurrentEntry' => config('entry_access-ranking_hidden_current_entry'), | |
'loop_class' => config('entry_access-ranking_loop_class'), | |
); | |
} | |
/** | |
* sqlの組み立て | |
* | |
* @return SQL_Select | |
*/ | |
public function buildQuery() | |
{ | |
$deleteAccessLog = new DeleteAccessLog(); | |
$deleteAccessLog->post(); | |
$span = config('entry_access-ranking_date-span') ? config('entry_access-ranking_date-span') : 30; | |
$SUB = SQL::newSelect('access'); | |
$SUB->addSelect('access_entry_id'); | |
$SUB->addSelect('access_entry_id', 'access_amount', null, 'COUNT'); | |
if (config('entry_access-ranking_include-today') === 'true') { | |
$SUB->addWhereBw('access_datetime', date('Y-m-d', strtotime("-" . $span . "day")), date('Y-m-d H:i:s')); | |
} else { | |
$SUB->addWhereBw('access_datetime', date('Y-m-d', strtotime("-" . $span . "day")), date('Y-m-d')); | |
} | |
$SUB->addGroup('access_entry_id'); | |
$SQL = SQL::newSelect('entry'); | |
$SQL->addInnerJoin($SUB, 'access_entry_id', 'entry_id', 'ranking'); | |
$SQL->addLeftJoin('blog', 'blog_id', 'entry_blog_id'); | |
$SQL->addLeftJoin('category', 'category_id', 'entry_category_id'); | |
$this->filterQuery($SQL); | |
$this->orderQuery($SQL); | |
$this->setAmount($SQL); // limitする前のクエリから全件取得のクエリを準備しておく | |
$this->limitQuery($SQL); | |
return $SQL->get(dsn()); | |
} | |
/** | |
* orderクエリ組み立て | |
* | |
* @param SQL_Select & $SQL | |
* @return void | |
*/ | |
public function orderQuery(&$SQL) | |
{ | |
$order = $this->config['order']; | |
if ($order[0] === 'access-asc') { | |
$SQL->addOrder('access_amount', 'ASC'); | |
if (isset($order[1])) { | |
ACMS_Filter::entryOrder($SQL, $order[1], $this->uid, $this->cid); | |
} | |
} else if ($order[0] === 'access-desc') { | |
$SQL->addOrder('access_amount', 'DESC'); | |
if (isset($order[1])) { | |
ACMS_Filter::entryOrder($SQL, $order[1], $this->uid, $this->cid); | |
} | |
} else if ($sortFd = ACMS_Filter::entryOrder($SQL, $order, $this->uid, $this->cid)) { | |
$SQL->setGroup($sortFd); | |
} | |
$SQL->addGroup('entry_id'); | |
} | |
/** | |
* エントリー数取得sqlの準備 | |
* | |
* @param SQL_Select $SQL | |
* @return void | |
*/ | |
public function setAmount($SQL) | |
{ | |
$temp = clone $SQL; | |
$this->amount = SQL::newSelect($temp, 'count'); | |
$this->amount->setSelect('DISTINCT(entry_id)', 'access_amount', null, 'COUNT'); | |
} | |
/** | |
* テンプレートの組み立て | |
* | |
* @param Template & $Tpl | |
* @return array | |
*/ | |
public function buildEntries(&$Tpl) | |
{ | |
$extraVars = array('access_amount' => 'access_amount'); | |
$gluePoint = count($this->entries); | |
$eagerLoad = $this->eagerLoad(); | |
foreach ( $this->entries as $i => $row ) { | |
$i++; | |
$this->buildSummary($Tpl, $row, $i, $gluePoint, $this->config, $extraVars, $eagerLoad); | |
} | |
} | |
/** | |
* フルスペックページャーの組み立て | |
* | |
* @param Template & $Tpl | |
* @return array | |
*/ | |
function buildFullspecPager(& $Tpl) | |
{ | |
$vars = array(); | |
if (isset($this->config['order'][0]) && 'random' === $this->config['order'][0]) { | |
return $vars; | |
} | |
if ( !isset($this->config['pagerOn']) || $this->config['pagerOn'] !== 'on' ) { | |
return $vars; | |
} | |
$itemsAmount = intval(DB::query($this->amount->get(dsn()), 'one')); | |
$itemsAmount -= $this->config['offset']; | |
$vars += $this->buildPager($this->page, $this->config['limit'], $itemsAmount, intval($this->config['pagerDelta']), $this->config['pagerCurAttr'], $Tpl); | |
return $vars; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment