-
Notifications
You must be signed in to change notification settings - Fork 1
/
newarticle.php
executable file
·150 lines (129 loc) · 3.49 KB
/
newarticle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @package Plg_Radicalform_NewArticle
* @author Dmitry Rekun <d.rekuns@gmail.com>
* @copyright Copyright (C) 2020 - 2023 JPathRu. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
\defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Registry\Registry;
Log::addLogger(
['text_file' => 'plg_radicalform_newarticle.php'],
Log::ALL,
['plg_radicalform_newarticle']
);
/**
* Base plugin class.
*
* @since 1.0
*/
class PlgRadicalformNewarticle extends CMSPlugin
{
/**
* Application object
*
* @var CMSApplication
* @since 1.0
*/
protected $app;
/**
* Database object
*
* @var JDatabaseDriver
* @since 1.0
*/
protected $db;
/**
* Affects constructor behavior.
* If true, language files will be loaded automatically.
*
* @var boolean
* @since 1.0
*/
protected $autoloadLanguage = true;
/**
* Handles event when Radical Form sends data.
*
* @param array $formInput Form input.
* @param array $input Full input.
* @param Registry $params Plugin parameters.
*
* @return void
*
* @since 1.0
*/
public function onBeforeSendRadicalForm($formInput, $input, $params)
{
$data = $formInput;
$formID = trim($this->params->get('form_id', ''));
if ($formID !== '' && $formID !== $input['rfFormID'])
{
return;
}
try
{
$this->createArticle($data);
}
catch (Exception $e)
{
Log::add($e->getMessage(), Log::ERROR, 'plg_' . $this->_type . '_' . $this->_name);
}
}
/**
* Creates an article.
*
* @param array $data Form data to create an article.
*
* @return void
*
* @since 1.0
* @throws \Exception
*/
private function createArticle(array $data)
{
$contentPath = JPATH_ADMINISTRATOR . '/components/com_content';
BaseDatabaseModel::addIncludePath($contentPath . '/models/', 'ContentModel');
Form::addFormPath($contentPath . '/models/forms');
Form::addFormPath($contentPath . '/model/form');
Form::addFieldPath($contentPath . '/models/fields');
Form::addFieldPath($contentPath . '/model/field');
Form::addFormPath($contentPath . '/forms');
/** @var ContentModelArticle $model */
$model = BaseDatabaseModel::getInstance('Article', 'ContentModel');
$titleName = trim($this->params->get('article_title'));
$textName = trim($this->params->get('article_text'));
$title = !empty($data[$titleName]) ? $data[$titleName] : 'Dummy Title';
$text = !empty($data[$textName]) ? $data[$textName] : 'Dummy Text';
$article = [
'id' => 0,
'title' => $title,
'alias' => '',
'introtext' => $text,
'catid' => (int) $this->params->get('catid', 0),
'state' => (int) $this->params->get('state', 0),
'created_by' => (int) $this->params->get('created_by', 0),
'language' => '*',
'access' => Factory::getApplication()->get('access', 1)
];
$form = $model->getForm($article, false);
if (!$form)
{
throw new \RuntimeException('Error getting form: ' . $model->getError());
}
if (!$model->validate($form, $article))
{
throw new \RuntimeException('Error validating article: ' . $model->getError());
}
Factory::getApplication()->input->set('task', 'save');
if (!$model->save($article))
{
throw new \RuntimeException('Error saving article: ' . $model->getError());
}
}
}