Created
September 26, 2020 17:12
-
-
Save sergant210/b9a88bd9404e1e21c231f8a719d97139 to your computer and use it in GitHub Desktop.
Скрипт создания ресурсов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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