/
codersite
/
plg_radicalform_cs_createarticle
Обзор
Документация
Войти
/
codersite
/
plg_radicalform_cs_createarticle
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/Extension/CsCreatearticle.php
139 строк
5 KB
Mitriy_Bug
edit readme
06 май 2026, 15:23
06 май 2026, 15:23
af0243e
Код
Авторство
О чём код?
<?php namespace CS\Plugin\Radicalform\CsCreatearticle\Extension; defined('_JEXEC') or die; use Exception; use Joomla\CMS\Form\Form; use Joomla\CMS\Log\Log; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Database\DatabaseInterface; use Joomla\Event\Event; use Joomla\Event\SubscriberInterface; use Joomla\CMS\Factory; class CsCreatearticle extends CMSPlugin implements SubscriberInterface { /** * Application object * * @var CMSApplication * @since 1.0.0 */ protected $app; /** * Returns an array of events this subscriber will listen to. * * @return array * @since 6.0.0 */ public static function getSubscribedEvents(): array { return [ 'onBeforeSendRadicalForm' => 'onBeforeSendRadicalForm', ]; } public function onBeforeSendRadicalForm(Event $event): void { if ($this->app->isClient('administrator')) { return; } $input = $this->app->getInput()->post->getArray(); $params = $this->params; $formID = trim($params->get('id_form', '')); if ($formID !== '' && $formID !== $input['rfFormID']) { return; } try { $article = $this->createArticle($input, $params); } catch (Exception $e) { Log::add($e->getMessage(), Log::ERROR, 'plg_' . $this->_type . '_' . $this->_name); } } private function createArticle(array $data, object $params) : array { $articleFields = $params->get('article_fields', []); $fieldsData = []; foreach ($articleFields as $item) { if (!empty($item->form_field) && !empty($item->article_field)) { $fieldsData[(int) $item->article_field] = (string) $data[$item->form_field]; } } if (!defined('JPATH_COMPONENT')) { define('JPATH_COMPONENT', JPATH_ADMINISTRATOR . '/components/com_content'); } $contentPath = JPATH_ADMINISTRATOR . '/components/com_content'; /** * Поскольку вызываем модель из необычного места - ей нужно помочь * и указать откуда подгружать формы. Из com_content она * грузит их самостоятельно */ Form::addFormPath($contentPath . '/models/forms'); Form::addFormPath($contentPath . '/model/form'); Form::addFieldPath($contentPath . '/models/fields'); Form::addFieldPath($contentPath . '/model/field'); Form::addFormPath($contentPath . '/forms'); $mvcFactory = $this->getApplication()->bootComponent('com_content')->getMVCFactory(); $model = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]); $article = [ 'id' => 0, 'title' => htmlspecialchars($data[$params->get('article_name', 'name')]), 'alias' => '', 'articletext' => htmlspecialchars($data[$params->get('article_content', 'massage')]), 'catid' => $params->get('article_category', 0), 'state' => $params->get('article_state', 0), 'language' => '*', 'created_by' => $params->get('article_created_by', 1), ]; // Load the form. $form = $model->getForm($article, false); if (!$form) { throw new \RuntimeException('Error getting form: ' . $model->getError()); } // Validate the form. if (!$model->validate($form, $article)) { throw new \RuntimeException('Error validating article: ' . $model->getError()); } // Emulate save task. $this->getApplication()->getInput()->set('task', 'save'); // Save an article. if (!$model->save($article)) { throw new \RuntimeException('Error saving article: ' . $model->getError()); } $item = $model->getItem(); $article = $mvcFactory->createTable('Article', 'Administrator'); $article->load($item->id); if ($article->id) { if (!empty($fieldsData)) { $db = Factory::getContainer()->get(DatabaseInterface::class); foreach ($fieldsData as $fieldId => $fieldValue) { $query = $db->getQuery(true); $query->insert($db->quoteName('#__fields_values')) ->columns([$db->quoteName('field_id'), $db->quoteName('item_id'), $db->quoteName('value')]) ->values($fieldId . ', ' . $article->id . ', ' . $db->quote($fieldValue)); $db->setQuery($query)->execute(); } } if ($article->store()) { return [$item->id, $item->alias]; } } return [$item->id, $item->alias]; } }