We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42962
    • 1 Posts
    Здравствуйте.
    Сниппет:
    <?php
    /********************************
    	Название: Articles
    	Цель: Вывод статей 
    ********************************/
    
    
    //Входные параметры
    $id = isset($id)?$id:$modx->documentObject['id'];
    $mode = isset($mode)?$mode:'auto';
    $num = isset($num)?$num:5;
    $tpl = isset($tpl)?$tpl:'Articles.ArticleListTpl';
    $date_format = isset($date_format)?$date_format:'d-m-Y';
    $numwords = isset($numwords)?$numwords:50;
    
    $tv_name = isset($tv_name)?$tv_name:'documentTags';
    $tv_delimeter = isset($tv_delimeter)?$tv_delimeter:' ';
    
    //Условия отбора
    $author = $_GET["author"];//todo
    $month = $_GET["month"];
    $year = $_GET["year"];
    $tag  = $_GET["tag"];
    $page =  $_GET["page"];
    
    
    
    function getDocList ($id, $num, $page, $author, $month, $year, $tv_value, $tv_name, $tv_delimeter) {
    global $modx;
    	$all_doc = $modx->getDocumentChildrenTVarOutput($parentid = $id, array('id', 'publishedon', $tv_name));// Получить id и TV всех документов из контейнера . todo:потом сделать массивом по всем TV
    	if (!$all_doc) return array();
    	$array_id = array();
    	foreach ($all_doc as $doc) {
    		if ($tv_value) { //отбор по тегу
    			if (inlist($tv_value, $doc[$tv_name], $tv_delimeter)) {	$array_id[] = $doc['id'];}
    		} else { 
    			if (($month)&&($year)) { //отбор по дате
    				if (date('mY',$doc['publishedon']) == $month.$year) { $array_id[] = $doc['id'];}
    			} else {
    				$array_id[] = $doc['id'];
    			}
    		}
    	}
    
    	$totalDoc = count($array_id);
    	$totalPages = ceil ($totalDoc / $num) - 1; 
    	if ($page > $totalPages) $page = $totalPages;
    	$start = $page * $num;
    
    	$DocList = $modx->getDocuments($array_id, $published = 1, $deleted = 0, $fields='*', $where='', $sort='createdon', $dir='DESC', $limit = $start.",".$num); //Документы по отобранным id
    
    	if ($DocList) {
    		foreach ($DocList as  $key=>$doc) {
    			foreach ($all_doc as $doctv) {
    				if ($DocList[$key]['id'] == $doctv['id']) {
    					$DocList[$key][$tv_name] = $doctv[$tv_name];
    				}
    			}
    		}
    	} else return array();
    	//var_dump($DocList);
    
    /*
    	if ($DocList) {
    		foreach ($DocList as  $doc) {
    			foreach ($all_doc as $doctv) {
    				if ($doc['id'] == $doctv['id']) {
    					$doc[$tv_name] = $doctv[$tv_name];
    				}
    			}
    		}
    	} else return array();
    	//var_dump($DocList);
    */
    	$result['doclist'] = $DocList;
    	$result['page'] = $page;
    	$result['totalpages'] = $totalPages;
    	$result['totaldoc'] = $totalDoc;
    
    	return $result;
    } 
    
    function inlist($val, $list, $delimiter) {
    	$varray = explode($delimiter, $list);
    	return in_array($val, $varray);
    }
    
    function numeric($str) {
    	return (!ereg("^[0-9]+$", $str)) ? false : true;
    }
    
    function makeTagLinks ($id, $parent, $tv_param, $tv_name, $tv_delimeter, $separator) {
    global $modx;
    	$varray = explode($tv_delimeter, $tv_param[$tv_name]);
    	$cnt = count($varray);//todo: 0-1
    	$taglinks =''; 
    
    	for ($x=0;$x<$cnt;$x++) {
    		$ptag = '&tag='.urlencode(trim($varray[$x]));
    		$cnd_separator = ($x!=($cnt-1)) ? $separator : '';
    		$taglinks .= '<a href="[~'.$parent.'~]'.$ptag.'">'.$varray[$x].'</a>'.$cnd_separator;	
    	}
    	return $taglinks;
    }
    
    function auto_introtext($content, $numwords) {
    	$introtext = strip_tags($content);
    	$introtext = str_replace("\n", ' ', $introtext);
    	$allwords = explode(" ", $introtext);
    	$awa = array_slice($allwords, $skipwords=0, $numwords);
    	$ex = implode(" ",$awa).'...';
    	return $ex;
    }
    
    if ($mode == 'auto') {
    	$check = $modx->documentObject['isfolder']; //По простому
    	$mode = ($check['isfolder'])?'list':'once';
    	$tpl = ($check['isfolder'])?'Articles.ArticleListTpl':'Articles.ArticleOnceTpl';// Ясен пень что режим 'auto' подразумевает наличие этих чанков
    }
    
    switch ($mode) {
    
    case 'list':
    
    	$page = (numeric($page))?$page:0;
    	if (!$result = getDocList($id, $num, $page, $author, $month, $year ,$tag, $tv_name, $tv_delimeter)) break;
    	$DocList = $result['doclist'];
    	$page = $result['page'];
    	$totalPages = $result['totalpages'];
    	$totalDoc = $result['totaldoc'];
    
    	foreach($DocList as $key => $value) {
    		$taglinks = makeTagLinks($value['id'], $value['parent'], $tv_param = array($tv_name => $value[$tv_name]), $tv_name, $tv_delimeter, $separator=', ');
    
    		$author = $modx->getUserInfo($value["createdby"]); 
    		if ($value["menutitle"] != "") {
    			$title = $value["menutitle"];
    		} else $title = $value["pagetitle"];
    
    		$chunkArr['articles.id'] = $value['id'];
    		$chunkArr['articles.title'] = $title;
    		$chunkArr['articles.pub_date'] = date ($date_format, $value['publishedon']);
    		$chunkArr['articles.author'] = $author["fullname"];
    		$chunkArr['articles.taglinks'] = $taglinks;
    		$chunkArr['articles.content'] = ($value['introtext'])?$value['introtext']:auto_introtext($value["content"], $numwords);
    		$items .= $modx->parseChunk($tpl, $chunkArr, '[+', '+]'); // Собираем блоки статей
    	}
    
    	// Навигация
    	if ($totalDoc > $num) {
    		$param ='';
    		if ($tag) {
    			$param .= '&tag='.$tag;
    		}
    		if ($month) {
    			$param .= '&month='.$month;
    		}
    		if ($year) {
    			$param .= '&year='.$year;
    		}
    
    		$output = "	<div id=\"pagination\">";
    		if ($page == 0) {
    			$output .= "
    			<a href=\"[~[*id*]~]&page=".($page+1).$param."\">< Назад</a>
    			";
    		}
    		else if ($page == $totalPages) {
    			$output .= "
    			<a href=\"[~[*id*]~]&page=".($page-1).$param."\">Вперед ></a>
    			";
    		}
    		else {
    			$output .= "
    						<a href=\"[~[*id*]~]&page=".($page+1).$param."\">< Назад</a>
    						<a href=\"[~[*id*]~]&page=".($page-1).$param."\">Вперед ></a>
    					";
    		}
    
    		$output .= "
    					</div>
    		";
    
    	}
    	break;
    
    //Одна статья
    case 'once': 
    
    	$value = $modx->getDocument($id);
    	$tv_param = $modx->getTemplateVarOutput($idname=array($tv_name), $id, $published="1");
    	$taglinks = makeTagLinks($value['id'], $value['parent'], $tv_param, $tv_name, $tv_delimeter, $separator=', ');
    
    	$author = $modx->getUserInfo($value["createdby"]); 
    	if ($value["menutitle"] != "") {
    		$title = $value["menutitle"];
    	} else $title = $value["pagetitle"];
    
    	$chunkArr['articles.id'] = $value['id'];
    	$chunkArr['articles.title'] = $title;
    	$chunkArr['articles.pub_date'] = date ($date_format, $value['publishedon']);
    	$chunkArr['articles.author'] = $author["fullname"];
    	$chunkArr['articles.taglinks'] = $taglinks;
    	$chunkArr['articles.content'] = $value["content"];
    	$items = $modx->parseChunk($tpl, $chunkArr, '[+', '+]');
    
    	break; 
    
    }
    
    return $items.$output;
    ?>


    В 1.0.8 выдаёт ошибку:
    « MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « PHP Parse Error »

    PHP error debug
    Error: Function ereg() is deprecated
    Error type/ Nr.: - 8192
    File: Z:\home\test1.ru\www\manager\includes\document.parser.class.inc.php(814) : eval()'d code
    Line: 89

    Что то поменялось с API? как пофиксить?