Просмотры, комментарии всех дочерних тикетов
Может кому-то пригАдится пригодится сниппет для подсчета всех дочерних тикетов, их комментариев и количества просмотров. Выводит информацию по всем дочерним тикетам, вне зависимости от иерархии и вложенности. Написан на очень скорую руку (точнее, переделан и собран из сниппетов Василия), так что, почти Франкенштейн… Мне сгодился для форума на сайте, построенного на Тикетсах, в силу того, что есть вложенные разделы тикетов в другие разделы тикетов.
Итак, frankensteinTicketsInfoCount:
Итак, frankensteinTicketsInfoCount:
<?php
/* @var pdoFetch $pdoFetch */
$pdoFetch = $modx->getService('pdoFetch');
$pdoFetch->setConfig($scriptProperties);
$pdoFetch->addTime('pdoTools loaded');
$tpl = $modx->getOption('tpl', $scriptProperties, 'tpl.Tickets.comment.list.row');
$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n");
// Define threads of comments
if (!empty($parents) || !empty($resources) || !empty($threads)) {
$where = array();
$options = array(
'innerJoin' => array(
'Thread' => array(
'class' => 'TicketThread',
'on' => '`Ticket`.`id` = `Thread`.`resource`',
),
'View' => array(
'class' => 'TicketView',
'on' => '`Ticket`.`id` = `View`.`parent`',)
),
'groupby' => '`Ticket`.`id`',
'select' => array(
'Thread' => '`Thread`.`comments`',
'View' => 'COUNT(`View`.`parent`) as `views`',
),
'showUnpublished' => !empty($showUnpublished),
'showDeleted' => !empty($showDeleted),
'depth' => isset($depth)
? (int)$depth
: 10,
);
if (!empty($parents)) {
$options['parents'] = $parents;
}
if (!empty($resources)) {
$options['resources'] = $resources;
}
if (!empty($threads)) {
$threads = array_map('trim', explode(',', $threads));
$threads_in = $threads_out = array();
foreach ($threads as $v) {
if (!is_numeric($v)) {
continue;
}
if ($v[0] == '-') {
$threads_out[] = abs($v);
}
else {
$threads_in[] = abs($v);
}
}
if (!empty($threads_in)) {
$where['Thread.id:IN'] = $threads_in;
}
if (!empty($threads_out)) {
$where['Thread.id:NOT IN'] = $threads_out;
}
}
$rows = $pdoFetch->getCollection('Ticket', $where, $options);
$cvt = array();
$output = array();
$cvt['comments'] = 0;
$cvt['views'] = 0;
$cvt['tickets'] = 0;
foreach ($rows as $row) {
$cvt['comments'] += $row['comments'];
$cvt['views'] += $row['views'];
$cvt['tickets'] += 1;
}
$tpl = $pdoFetch->defineChunk($output);
$output[] = empty($tpl)
? '<pre>' . $pdoFetch->getChunk('', $cvt) . '</pre>'
: $pdoFetch->getChunk($tpl, $cvt, $pdoFetch->config['fastMode']);
}
$pdoFetch->addTime('Returning processed chunks');
$output = implode($outputSeparator, $output);
$log = '';
if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) {
$log .= '<pre class="getCommentsLog">' . print_r($pdoFetch->getTime(), 1) . '</pre>';
}
// Return output
$output .= $log;
if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) {
$output = $pdoFetch->getChunk($tplWrapper, array('output' => $output), $pdoFetch->config['fastMode']);
}
if (!empty($toPlaceholder)) {
$modx->setPlaceholder($toPlaceholder, $output);
}
else {
return $output;
}Принимаемые параметры видно из кода, если нужно кому-то, опишу. Комментарии: 10
Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
Грубоговоря:
Делаем плагин, разбиваем ссылку (site.ru/forum/thread/subthread/post), ищем ресурс по array('alias' => 'thread') и ставим ему +1. Это явно быстрее отработает нежели код в шапке, учитывая что Вы его вызывать будите для каждого tplRow раздела форума (наверно). Можно вообще это на аякс повесить, после загрузки страницы.
Ну и при выводе просто выводить это поле, total_views, например.
Про доп.поля в БД
Про разбив урла и поиск ресурса