снипет просто супер как раз то что сам собирался писать так как на странице 10вызовов дито ... а с дито это большая нагрузка на сервак , так что автору респект =)
но в 1 блоке мне нуна было еще было выводить тв.... а снипет это не делал так что пришлось немного переделал,..
делал быстро но вроде производительность не очень потерялась но у себя эту версию снипета как отдельную поставил так как в любом случае нагрузка больше чем у оригинала а сам снипет:
<?php
global $_lang;
if(!function_exists('NL_paginate_link'))
{
function NL_paginate_link($link = "",$title,$itemclass = "",$liclass = "")
{
if($link!="") return "<li".($liclass!=""?" class=\"$liclass\"":"")."><a href=\"$link\"".($itemclass!=""?" class=\"$itemclass\"":"").">".$title."</a></li>";
else return "<li".($liclass!=""?" class=\"$liclass\"":"")."><span".($itemclass!=""?" class=\"$itemclass\"":"").">".$title."</span></li>";
}
}
if(!function_exists('NL_paginate'))
{
function NL_paginate($link,$total_items, $curr_page, $items_per_page, $ptag = "nl_page", $max_pages = 10)
{
global $_lang;
if(!isset($total_items) || $total_items <= 0) return "";
//$start = ($curr_page - 1) * $items_per_page;
//$end = (($start-1+$items_per_page) >= $total_items) ? $total_items: ($start-1+$items_per_page);
$tmp = explode("?",$link);
$link = $tmp[0];
$exta = "";
if(count($tmp)>0)
{
$a = explode("&",str_replace("&","&",$tmp[1]));
$tmp = array();
for($i=0;$i<count($a);$i++)
{
$s = explode("=",$a[$i]);
if($s[0]!=$ptag) $tmp[] = $a[$i];
}
$exta = implode("&",$tmp);
}
$total_pages = ceil($total_items/$items_per_page);
$page_links = array();
$page_links[] = NL_paginate_link(($curr_page>1?$link."?".$exta:""), $_lang['first'],($curr_page==1?"disabled":""),($curr_page==1?"disabled":""));
$page_links[] = NL_paginate_link(($curr_page>1?$link."?".$exta."&".$ptag."=".($curr_page-1):""), $_lang['prev'],($curr_page==1?"disabled":""),($curr_page==1?"disabled":""));
if($total_pages > $max_pages)
{
$mp = $max_pages;
$mp2 = ceil($max_pages/2);
$start = ($curr_page > $mp2 ? $curr_page - $mp2: 1);
if($total_pages - $start < $mp) $start = $total_pages - $mp + 1;
if($start < 1) $start = 1;
}else
{
$mp = $total_pages;
$start = 1;
}
for($i=0;$i<$mp;$i++)
{
$page_links[] = NL_paginate_link(($curr_page!=$start+$i?$link."?".$exta."&".$ptag."=".($start+$i):""), ($start+$i),($curr_page==($start+$i)?"current":""),($curr_page==($start+$i)?"current":""));
}
$page_links[] = NL_paginate_link(($curr_page<$total_pages?$link."?".$exta."&".$ptag."=".($curr_page+1):""), $_lang['next'],($curr_page==$total_pages?"disabled":""),($curr_page>=$total_pages?"disabled":""));
$page_links[] = NL_paginate_link(($curr_page<$total_pages?$link."?".$exta."&".$ptag."=".$total_pages:""), $_lang['last'],($curr_page==$total_pages?"disabled":""),($curr_page>=$total_pages?"disabled":""));
return "<div class=\"nl_paginate\">\n<ul>\n".implode("\n",$page_links)."\n</ul>\n</div>";
}
}
/// Base options
$mybase = $modx->config['base_path']."assets/snippets/newslist/";
/// Snipet parameters
/*
Snippet parameter - language
*/
$language = (isset($language))? $language : "english";
/*
Snippet parameter - ID category
*/
$cat_id = (isset($cat_id) ? intval($cat_id) : 0);
if($cat_id == 0) return "";
/*
Snippet parameter - turn on/off pagination
0 - off, 1 - top, 2 - bottom, 3 - both
*/
$paging = (isset($paging) ? intval($paging) : 0);
/*
Snippet parameter - items per page
*/
$per_page = (isset($per_page) ? intval($per_page) : 10);
if($per_page<=0) $per_page = 10;
/*
Snippet parameter - table field to sort
*/
$sort_field = (isset($sort_field) ? $sort_field : "createdon" );
/*
Snippet parameter - sort direction
*/
$sort_dir = (isset($sort_dir) ? $sort_dir : "DESC" );
/*
Snippet parameter - unique ID
*/
$uid = (isset($uid) ? $uid : "");
/*
Snippet parameter - news item template
*/
if(isset($item_tpl) && $item_tpl!="")
{
$tpl = $modx->getChunk($item_tpl);
if($tpl == "") $tpl = $_lang['default_template'];
}
/*
Snippet parameter - extended where filter (SQL format)
Example: "pub_date>='1234567890' AND introtext LIKE '%test%'"
*/
$ext_where = (isset($ext_where) ? $ext_where : "");
if($ext_where!="") $ext_where = " " . $ext_where;
/*
Snippet parameter - label for link to first page
*/
$first_txt = (isset($first_txt) ? $first_txt : "");
/*
Snippet parameter - label for link to previous page
*/
$perv_txt = (isset($prev_txt) ? $prev_txt : "");
/*
Snippet parameter - label for link to next page
*/
$next_txt = (isset($next_txt) ? $next_txt : "");
/*
Snippet parameter - label for link to last page
*/
$last_txt = (isset($last_txt) ? $last_txt : "");
// Snippet code
include_once($mybase . "lang/english.inc.php");
if($language != "english" && file_exists($mybase . "lang/".$language.".inc.php"))
{
include_once($mybase . "lang/".$language.".inc.php");
}
//
$page_tag = "nl".($uid ? "_".$uid : "")."_page";
$nl_page = (int) (isset($_REQUEST[$page_tag]) ? (int) $_REQUEST[$page_tag] : 1);
if($nl_page <=0) $nl_page = 1;
if($tpl == "") $tpl = $_lang['default_template'];
$table = $modx->getFullTableName('site_content');
if($first_txt!="") $_lang['first'] = $first_txt;
if($prev_txt!="") $_lang['prev'] = $prev_txt;
if($next_txt!="") $_lang['next'] = $next_txt;
if($last_txt!="") $_lang['last'] = $last_txt;
/// Start snippet
if($paging > 0)
{
$sql = "SELECT COUNT(*) FROM ".$table." WHERE parent='$cat_id' AND published=1 AND deleted=0".$ext_where;
$newscount = (int) $modx->db->getValue( $modx->db->query($sql) ); // total news counter
if($newscount <= 0) return "";
$tmp = $_GET;
$params = "";
unset($tmp['id']);
unset($tmp[$page_tag]);
foreach($tmp as $key=>$value)
{
$params .= ($params == "" ? "" : "&") . $key."=".$value;
}
$paginate_body = NL_paginate($modx->makeURL($modx->documentObject['id'],'',$params),$newscount,$nl_page,$per_page,$page_tag);
}else
{
$paginate_body = "";
}
$output = "";
if($paging == 1 || $paging == 3) $output .= $paginate_body;
$tvname='tags';
$sql = "SELECT * FROM ".$table." WHERE parent='$cat_id' AND published=1 AND deleted=0".$ext_where." ORDER BY ".$sort_field." ".$sort_dir." LIMIT ".(($nl_page-1)*$per_page).",".$per_page;
$res = $modx->db->query($sql);
if($res)
{
$output.= "<div class=\"nl_news\">\n";
$output.=mysql_resultTo2DAssocArrayTVTpl($res,$tpl);
$output.= "\n</div>";
}
if($paging == 2 || $paging == 3) $output .= $paginate_body;
return $output;
function mysql_resultTo2DAssocArrayTVTpl($result,$tpl) {
global $modx;
$i=0;
$ret = array();
$ids=array();
$tag=array();
$tag_text_id=array();
$output='';
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $key => $value) {
if($key=='id'){ $ids[$i]=$value;}
if($i==0){$tag[]='[+'.$key.'+]';}
$tag_text_id[$ids[$i]][]=$value;
$ret[$i][$key] = $value;
}
$i++;
}
mysql_free_result($result);
$ids=implode($ids,",");
$tb1 = $modx->getFullTableName("site_tmplvar_contentvalues");
$tb2 = $modx->getFullTableName("site_tmplvars");
$query = "SELECT stv.name,stc.contentid,stc.value";
$query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
$query .= 'WHERE stc.contentid IN ('.$ids.') ORDER BY stc.contentid ASC;';
unset($tb1,$tb2,$ids);
$restv = $modx->db->query($query);
$i=0;
$tag_tag_text_id=array();
$tv_tag_id=array();
while ($tvrow = mysql_fetch_assoc($restv)) {
foreach ($tvrow as $key => $value) {
$tv_ret[$i][$key] = $value;
}
$tag_tag_text_id[$tvrow['contentid']][]=$tvrow['value'];
$tv_tag_id[$tvrow['contentid']][]='[+tv'.$tvrow['name'].'+]';
$i++;
}
mysql_free_result($restv);
for ($i = 0; $i < count($ret); $i++){
$output.= str_replace(array_merge($tag,$tv_tag_id[$ret[$i]['id']]),array_merge($tag_text_id[$ret[$i]['id']],$tag_tag_text_id[$ret[$i]['id']]),$tpl);
}
unset($tag,$tv_tag_id,$tag_text_id,$tag_tag_text_id,$tvrow);
return $output;
}
return $output;
?>