Anatoliy
Well-known member
It feels like I'm somewhere really close, but still not there yet. )
$input is highlighted with red underline so I try to use $params instead.
the code works, shows items in default order. But It doesn't get data from Post() and doesn't sort differently.
Code:
public function actionShorts(ParameterBag $params)
{
if ($this->isPost())
{
if ($params['av_ttm_order'])
{
$this->app()->response()->setCookie('av_ttm_order', $params['av_ttm_order']);
}
if ($params['av_ttm_direction'])
{
$this->app()->response()->setCookie('av_ttm_direction', $params['av_ttm_direction']);
}
return $this->redirect($this->buildLink('av-ttm/shorts/'));
}
$orderField = $params['av_ttm_order'] ?: $this->app()->request()->getCookie('av_ttm_order', 'reply_count');
$orderDir = $params['av_ttm_direction'] ?: $this->app()->request()->getCookie('av_ttm_direction', 'DESC');
$page = $this->filterPage();
$perPage = 20;
/** @var \AV\ThreadTitless\Repository\ThreadTitle $repo */
$repo = $this->repository('AV\ThreadTitles:ThreadTitle');
$finder = $repo->findTooShortTitles()
->order($orderField, $orderDir)
->limitByPage($page, $perPage);
$viewParams = [
'data' => $finder->fetch(),
'tooShortTotal' => $finder->total(),
'page' => $page,
'perPage' => $perPage
];
return $this->view('AV\ThreadTitles:TitleController\Shorts', 'av_ttm_shorts', $viewParams);
}
Code:
public function findTooShortTitles()
{
$finder = $this->finder('\XF:Thread');
$expression = $finder->expression('CHAR_LENGTH(%s)', 'title');
$finder
->where($expression, '<', $this->options()->av_threadTitles_min)
->where('discussion_state', 'visible')
->where('discussion_type', '!=', 'redirect');
return $finder;
}
Code:
<xf:css>
.block-container{
padding:10px 20px;
margin-top: 2px;
}
.margin0 {margin:0;}
.gray{color:#8c8c8c;}
</xf:css>
<xf:title>Threads with too short titles</xf:title>
<span>Found <b>{{$tooShortTotal}}</b> threads with too short titles.</span>
<br /><br />
<div class="block">
<div class="filterBar-header">
<div class="filterBar">
<a class="filterBar-menuTrigger" data-xf-click="menu" role="button" tabindex="0" aria-expanded="false" aria-haspopup="true">Sort by:</a>
<div class="menu menu--wide" data-menu="menu" aria-hidden="true">
<div class="menu-content">
<div class="js-filterMenuBody">
<xf:form action="{{link('av-ttm/shorts/')}}" method="post" class="">
<div class="menu-row menu-row--separated">
<div class="inputGroup u-inputSpacer">
<select name="av_ttm_order" class="input">
<option value="title_length">Title length</option>
<option value="thread_id">Thread ID</option>
<option value="reply_count">Reply count</option>
<option value="view_count">View count</option>
<option value="creation_date">Creation date</option>
</select>
<span class="inputGroup-splitter"></span>
<select name="av_ttm_direction" class="input">
<option value="asc" selected="selected">Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
</div>
<div class="menu-footer">
<span class="menu-footer-controls">
<button type="submit" class="button--primary button"><span class="button-text">Sort</span></button>
</span>
</div>
<input type="hidden" name="apply" value="1">
</xf:form>
</div>
</div>
</div>
</div>
</div>
<div class="block-body">
<br />
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$tooShortTotal}" link="av-ttm/shorts/" wrapperclass="block" />
<xf:foreach loop="$data" value="$short">
<div class="block-container">
<h3 class="margin0">
<a href="{{ link_type('public', 'threads', $short) }}" target="new">{$short.title}</a>
</h3>
<span class="gray">id:</span> {{$short.thread_id}}, <span class="gray">replies:</span> {{$short.reply_count}}, <span class="gray">views:</span> {{$short.view_count}}, <xf:date time="{$short.post_date}" /> - <xf:date time="{$short.last_post_date}" />
</div>
</xf:foreach>
</div>
<br />
<xf:pagenav page="{$page}" perpage="{$perPage}" total="{$tooShortTotal}" link="av-ttm/shorts/" wrapperclass="block" />