Skip to content

Instantly share code, notes, and snippets.

@alooze
Created April 13, 2015 23:27
Show Gist options
  • Save alooze/a587aba95581f9ced0de to your computer and use it in GitHub Desktop.
Save alooze/a587aba95581f9ced0de to your computer and use it in GitHub Desktop.
Плагин для быстрой нарезки чанков из полного шаблона
//<?php
/**
* OnBeforeTempFormSave
*/
$e = $modx->event;
switch($e->name) {
case 'OnBeforeTempFormSave':
global $template;
if (strpos($template, '@@') !== false) {
$do = preg_match_all('#@@CHUNK([^@]*)?@@#', $template, $pAr);
$srch = $pAr[0];
foreach ($pAr[1] as $key=>$code) {
list($nil, $name, $target, $empty, $chunkCode) = explode('~', $code);
// сохраняем чанк с полученным кодом
$res = $modx->db->select('*', $modx->getFullTableName('site_htmlsnippets'), 'name="'.$modx->db->escape($name).'"');
if ($cnt = $modx->db->getRecordCount($res)) {
// такой чанк уже есть
// собираем обратно код с текстом ошибки
$err = 'Чанк с названием '.$name.' уже существует в системе!';
$repl[$key] = str_replace('~~', '~'.$err.'~', $srch[$key]);
} else {
$ins['name'] = $modx->db->escape($name);
$ins['snippet'] = $chunkCode;
$modx->db->insert($ins, $modx->getFullTableName('site_htmlsnippets'));
$repl[$key] = '{{'.$name.'}}';
if ($target != '') {
$task[$target]['srch'] = $srch[$key];
$task[$target]['repl'] = $repl[$key];
}
}
}
// заменяем чанки в текущем шаблоне
$_POST['post'] = str_replace($srch, $repl, $_POST['post']);
$template = str_replace($srch, $repl, $template);
// для остальных шаблонов проделываем ту же операцию
// foreach ($task as $target => $taskAr) {
// if ($target == 'ALL' || $target == '*') {
// $res = $modx->db->select('*', $modx->getFullTableName('site_templates'));
// }
// }
return;
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment