Skip to content

Instantly share code, notes, and snippets.

@sergant210
Created September 26, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergant210/b9a88bd9404e1e21c231f8a719d97139 to your computer and use it in GitHub Desktop.
Save sergant210/b9a88bd9404e1e21c231f8a719d97139 to your computer and use it in GitHub Desktop.
Скрипт создания ресурсов
<?php
// Пути для корня сайта
require_once __DIR__ . '/config.core.php';
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError', '', '');
// Вариант 1.
/*$doc = $modx->newObject('modDocument', [
'pagetitle' => 'О компании',
'longtitle' => 'О нашей компании',
'uri' => 'about.html',
'alias' => 'about',
'published' => true,
'publishedon' => date('Y-m-d H:i:s'),
'createdby' => 1,
'introtext' => 'Давным давно в одной далёкой галактике...',
'content' => 'Содержание ресурса',
]);
if (!$doc->save()) echo 'Ошибка создания документа';
*/
// Вариант 2.
$resourceData = [
'pagetitle' => 'Статья 3',
'uri' => 'blog/3',
'uri_override' => true,
'alias' => 'article3',
'parent' => 3,
'template' => 0,
'richtext' => 0,
'published' => true,
'syncsite' => true,
'introtext' => 'Давным давно в далёкой далёкой галактике...',
'content' => <<<HTML
<p>Lorem ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto, aspernatur doloribus nostrum obcaecati quam reiciendis tenetur vitae! Consequatur dolores doloribus fugit hic,
iste maiores, odio quam sunt suscipit ut voluptatem!</p>
HTML
];
/* Подгружаем пользователя с правами на создание ресурсов */
$modx->user = $modx->getObject('modUser', 1);
/** @var \modProcessorResponse $response */
$response = $modx->runProcessor('resource/create', $resourceData);
if ($response->isError()) {
echo $response->getMessage();
} else {
echo $response->getObject()['id']; // id добавленного документа
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment