[+comment.createdby:userinfo=`username`:ifempty=`[+comment.custom.name:ifempty=`[+jot.guestname+]`:esc+]`+][+comment.createdby:isnt=`0`:then=``:+]
function CJotDataDb() {
global $modx;
$this->tbl["check"] = $GLOBALS['table_prefix']."jot_fields";
$this->tbl["content"] = $modx->getFullTableName('jot_content');
$this->tbl["subscriptions"] = $modx->getFullTableName('jot_subscriptions');
$this->tbl["fields"] = $modx->getFullTableName('jot_fields');
$this->tbl["manager_users"] = $modx->getFullTableName('manager_users');
$this->tbl["user_attributes"] = $modx->getFullTableName('user_attributes');
$this->tbl["web_users"] = $modx->getFullTableName('web_users');
$this->tbl["web_user_attributes"] = $modx->getFullTableName('web_user_attributes');
$this->cache["userpostcount"] = array();
$this->isNew = false;
}function GetComments($docid,$tagid,$viewtype,$sort,$offset,$length) {
global $modx;
$tbl = $this->tbl["content"];
$where = NULL;
if ($length > 0 ) { $limit = " limit $offset, $length"; }
$orderby = " order by createdon desc ";
$tblcustom = "";
if (strlen($sort) > 3) {
$orderby = array();
$tblcustom = array();
$obparts = explode(",", $sort);
$c = 0;
foreach ($obparts as $obpart) {
$x = explode(":", $obpart);
if($x[0]{0} == "#") {
$c++;
$fld = str_replace("#","",$x[0]);
$tblcustom[] = "left join " . $this->tbl["fields"] . " as " . "c" . $c . " on c". $c . ".id = a.id and c". $c . ".label = '" . $fld . "'";
$orderby[] = "c" . $c . ".content" . " " . $this->getOrderByDirection($x[1]);
} else {
$orderby[] = $x[0] . " " . $this->getOrderByDirection($x[1]);
}
}
$orderby = " order by " . implode(", ",$orderby);
$tblcustom = implode(" ",$tblcustom);
}
$user_data_tbl="left join " . $this->tbl["manager_users"] . " as mu on mu.id=a.createdby "
."left join " . $this->tbl["user_attributes"] . " as mua on mua.id=mu.id "
."left join " . $this->tbl["web_users"] . " as wu on wu.id=-a.createdby "
."left join " . $this->tbl["web_user_attributes"] . " as wua on wua.id=wu.id ";
switch ($viewtype) {
case 2:
$where = " and a.published >= 0 "; // Mixed
break;
case 0:
$where = " and a.published >= 0 "; // Unpublished
break;
case 1:
default:
$where = " and a.published = 1 "; // Published
}
if ($docid == '*')
$sql = "select a.*, b.published, mu.*, mua.*, wu.*, wua.* AS docpublished from " . $tbl . " as a " . $tblcustom . " "
.$user_data_tbl."
left join modx_site_content AS b ON a.uparent = b.id
where tagid = '" . $tagid ."' and mode = '0' and b.published='1' " . $where . $orderby . $limit;
else
$sql = "select a.*, mu.*, mua.*, wu.*, wua.* from " . $tbl . " as a " . $tblcustom . " ".$user_data_tbl." where uparent = '$docid' and tagid = '" . $tagid ."' and mode = '0' " . $where . $orderby . $limit;
#print $sql;
return $this->GetCommentsArray($sql);
}function GetCommentsArray($query) {
global $modx;
$rs = $modx->db->query($query);
$comments = array();
$ids = array();
$array_fields_my=array(); // массив полей
$all_fields=$modx->db->getColumnNames($rs); // получаем массив полей
foreach($all_fields as $key=>$field) // перебираем массив полей ик повторяющимя полям приписываем префикс web - надо, чтобы поля веб юзера не перекрывали полей менеджера
{
if(isset($array_fields_my[$field])) $array_fields_my['web_'.$field]=$key;
else $array_fields_my[$field]=$key;
}
$array_fields_my=array_flip($array_fields_my);
while ($row = $modx->db->getRow($rs,'num')) {
$ids[] = $row["id"];
foreach ($array_fields_my as $key=>$value) // получаем массив с нужными полями уже со значениями
{
$row[$value]=$row[$key];
unset($row[$key]);
}
$comments[] = $row;
}
$custom = $this->getCustomFieldsArray($ids);
$arrComments = array();
foreach($comments as $comment) {
$comment["custom"] = $custom[$comment["id"]];
$comment["userpostcount"] = $this->getUserPostCount($comment["createdby"],$comment["uparent"],$comment["tagid"]);
$arrComments[] = $comment;
}
return $arrComments;
}[+comment.username:ifempty=`[+comment.web_username:ifempty=`[+comment.custom.name:ifempty=`[+jot.guestname+]`:esc+]`+]`+]Это позволяет снизить запросы на количество комментариев на странице и вдобавок получить всю инфу о пользователе. У меня количество запросов упало на 20 штук!
."left join " . $this->tbl["user_attributes"] . " as mua on mua.id=mu.id "
$where = " and a.published >= 0 "; // Unpublished
if ($docid == '*') $sql = "select a.*, b.published, mu.*, mua.*, wu.*, wua.* AS docpublished from " . $tbl . " as a " . $tblcustom . " " .$user_data_tbl." left join modx_site_content AS b ON a.uparent = b.id where tagid = '" . $tagid ."' and mode = '0' and b.published='1' " . $where . $orderby . $limit;
while ($row = $modx->db->getRow($rs,'num')) {
$ids[] = $row["id"];
$user_data_tbl="left join " . $modx->getFullTableName('manager_users') . " as mu on mu.id=a.createdby "
."left join " . $modx->getFullTableName('user_attributes') . " as mua on mua.internalKey=mu.id ";
$webuser_data_tbl="left join " . $modx->getFullTableName('web_users') . " as wu on wu.id=-a.createdby "
."left join " . $modx->getFullTableName('web_user_attributes') . " as wua on wua.internalKey=wu.id ";
$sql = "(select a.*,mu.username,mua.fullname,mua.email,mua.role,mua.gender,mua.country,mua.photo
from " . $tbl . " as a " .$user_data_tbl." ".$tblcustom. " where a.createdby>=0 and uparent = '" . $docid . "' and tagid = '" . $tagid ."' and mode = '0' " . $where . ")
union (select a.*,wu.username,wua.fullname,wua.email,wua.role,wua.gender,wua.country,wua.photo
from " . $tbl . " as a " .$webuser_data_tbl." ".$tblcustom. " where a.createdby<0 and uparent = '" . $docid . "' and tagid = '" . $tagid ."' and mode = '0' " . $where . ")"
. $orderby . $limit;
Здесь ошибка, должно быть равенство:
$where = " and a.published >= 0 "; // Unpublished
Это какой-то левый запрос в таблицу без префикса, его тут быть не должно:
if ($docid == '*') $sql = "select a.*, b.published, mu.*, mua.*, wu.*, wua.* AS docpublished from " . $tbl . " as a " . $tblcustom . " " .$user_data_tbl." left join modx_site_content AS b ON a.uparent = b.id where tagid = '" . $tagid ."' and mode = '0' and b.published='1' " . $where . $orderby . $limit;
В этом месте отваливаются custom-поля, так массив уже не ассоциативный:
;while ($row = $modx->db->getRow($rs,'num')) { $ids[] = $row["id"]