Сергей Шлоков

Сергей Шлоков

Был в сети 06 ноября 2025, 19:54
Заказы не принимаю
Так в чанке и стоит плейсхолдер {%hp-[[+id]]%}. Но он обрабатывается обрабатывается плагином на событие onWebpagePreRender. А твой плагин обрывает обработку после события OnLoadWebDocument. Вот он не не парсится.
Число просмотров пишется в таблицу ресурсов в поле properties.
П.С. Вот у ребят как описано
hitspage
А вот плагин.
<?php
switch ($modx->event->name) {
    case 'OnWebPagePrerender':
        if($modx->hpCount === true) {
            // get a reference to the output
			$output = &$modx->resource->_output;
    		if (preg_match_all ("/{%hp-(.*?)%}/",$output , $hp_list)) {
			  // If the array is not empty, choose the number of comments on the resource id (column rid)
			  if (is_array($hp_list)) {
				  array_walk($hp_list[1], 'intval');
				  $hp = $modx->newQuery('modResource', array('id:IN' => $hp_list[1]));
				  $hp->select(array('modResource.id','modResource.properties'));
				  if ($hp->prepare() && $hp->stmt->execute()) {
					  $resultsHp = $hp->stmt->fetchAll(PDO::FETCH_ASSOC);
					  foreach ($resultsHp as $rHp) {
						  $objHP = json_decode($rHp['properties']);
						  if (in_array($rHp['id'],$hp_list[1])) {
							  $hp_list[1][array_search($rHp['id'],$hp_list[1])] =  intval($objHP->hitspage->hitts);
						  }
					  }
				  }
			  }
			  // Replace all your templates in the resource to the correct values
			  $output = str_replace($hp_list[0], $hp_list[1], $output);
		  }
        }
    break;
}
Пришлось лезть в код ajaxsnippet. Компонент состоит из сниппета и плагина, повешенного на событие OnLoadWebDocument. Из чего сделал вывод, что я ошибался насчет событий при ajax. События также выполняются. Но в данном случае, при ajax запросе плагин выполняется на событие OnLoadWebDocument и обрывает дальнейшее выполнение плагинов. Поэтому, плагин HitsPage не срабатывает на следующее событие OnWebPagePrerender.
Что тут можно сделать? Отредактировать плагин (а лучше скопировать под своим именем, чтоб при обновлении не затерлось) и внести перед строчкой в плагине AjaxSnippet'а
$response = array(
	'output' => $output,
	'key' => $_REQUEST['as_action'],
	'snippet' => $scriptProperties['snippet'],
);
вот этот код
if (preg_match_all ("/{%hp-(.*?)%}/",$output , $hp_list)) {
// If the array is not empty, choose the number of comments on the resource id (column rid)
	if (is_array($hp_list)) {
		 array_walk($hp_list[1], 'intval');
		  $hp = $modx->newQuery('modResource', array('id:IN' => $hp_list[1]));
		  $hp->select(array('modResource.id','modResource.properties'));
		  if ($hp->prepare() && $hp->stmt->execute()) {
			  $resultsHp = $hp->stmt->fetchAll(PDO::FETCH_ASSOC);
			  foreach ($resultsHp as $rHp) {
				  $objHP = json_decode($rHp['properties']);
				  if (in_array($rHp['id'],$hp_list[1])) {
					  $hp_list[1][array_search($rHp['id'],$hp_list[1])] =  intval($objHP->hitspage->hitts);
				  }
			  }
		  }
	  }
	  // Replace all your templates in the resource to the correct values
	  $output = str_replace($hp_list[0], $hp_list[1], $output);
}
Решение, конечно, не очень красивое, но должно работать.
Наверно как-то можно. Указать параметр &toPlaceholder=`MyPlaceholder`. А потом разобрать его в своем сниппете, например, HPCount (код можно взять из плагина Hitspage).
$output = $modx->getPlaceholder('MyPlaceholder');
if (preg_match_all ("/{%hp-(.*?)%}/",$output , $hp_list)) {
// If the array is not empty, choose the number of comments on the resource id (column rid)
	if (is_array($hp_list)) {
		 array_walk($hp_list[1], 'intval');
		  $hp = $modx->newQuery('modResource', array('id:IN' => $hp_list[1]));
		  $hp->select(array('modResource.id','modResource.properties'));
		  if ($hp->prepare() && $hp->stmt->execute()) {
			  $resultsHp = $hp->stmt->fetchAll(PDO::FETCH_ASSOC);
			  foreach ($resultsHp as $rHp) {
				  $objHP = json_decode($rHp['properties']);
				  if (in_array($rHp['id'],$hp_list[1])) {
					  $hp_list[1][array_search($rHp['id'],$hp_list[1])] =  intval($objHP->hitspage->hitts);
				  }
			  }
		  }
	  }
	  // Replace all your templates in the resource to the correct values
	  $output = str_replace($hp_list[0], $hp_list[1], $output);
	$modx->setPlaceholder('MyPlaceholder',$output);
}

А вызывать так
[[ajaxsnippet ....]] 
[[!HPCount ]]
[[+MyPlaceholder]]
Не проверял. Может нужно пилить. Только я не знаю, AjaxSnippet отработает плейсхолдер или нет, нужно пробовать.
Можно все сделать в одном сниппете. Если есть желание, то сам.
Ребята, подскажите как настроить интерпретатор для встроенного web сервера? Указываю ему папку с PHP, а он пишет, что PHP не установлен. В винде запускаю через RUN php.ini — открывается нормально. Кто-нибудь знает, почему Storm не видит PHP?