Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharticle_create.php
More file actions
Latest commit
177 lines (146 loc) · 3.98 KB
/
Copy patharticle_create.php
File metadata and controls
177 lines (146 loc) · 3.98 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @package Cli_Scripts
* @author Dmitry Rekun <d.rekuns@gmail.com>
* @copyright Copyright (C) 2020 JPathRu. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
useJoomla\CMS\Application\CliApplication;
useJoomla\CMS\Application\SiteApplication;
useJoomla\CMS\Factory;
useJoomla\CMS\MVC\Model\BaseDatabaseModel;
// We are a valid entry point.
const_JEXEC = 1;
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines.
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_oncedirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_onceJPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_onceJPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_onceJPATH_LIBRARIES . '/cms.php';
// Import the configuration.
require_onceJPATH_CONFIGURATION . '/configuration.php';
// System configuration.
$config = newJConfig;
define('JDEBUG', $config->debug);
/**
* This script will create an article with custom fields.
*
* @since 1.0
*/
class ArticleCreateCli extends CliApplication
{
/**
* Site application.
*
* @var SiteApplication
* @since 1.0
*/
protected$app;
/**
* Entry point for CLI script.
*
* @return void
*
* @since 1.0
*/
publicfunctiondoExecute()
{
$this->out('Starting article creation');
try
{
$this->app = Factory::getApplication('site');
$this->createArticle();
}
catch (Exception$e)
{
$this->out('Oops...' . $e->getMessage() . ' :: ' . $e->getTraceAsString());
return;
}
$this->out('Article was created successfully');
}
/**
* Creates an article.
*
* @return void
*
* @since 1.0
* @throws \Exception
* @throws \RuntimeException
*/
privatefunctioncreateArticle()
{
define('JPATH_COMPONENT', JPATH_ADMINISTRATOR . '/components/com_content');
BaseDatabaseModel::addIncludePath(JPATH_COMPONENT . '/models/', 'ContentModel');
/** @var ContentModelArticle $model */
$model = BaseDatabaseModel::getInstance('Article', 'ContentModel');
$article = [
'id' => 0,
'title' => 'Article Title', // Title
'alias' => '', // Empty alias to avoid notice warnings
'introtext' => 'Article Text', // Text
'catid' => 2, // Category
'state' => 0, // Publishing state
'language' => '*', // Language
'access' => $this->app->get('access', 1) // Access level
];
// Load the form.
$form = $model->getForm($article, false);
if (!$form)
{
thrownew \RuntimeException('Error getting form: ' . $model->getError());
}
// Validate the form.
if (!$model->validate($form, $article))
{
thrownew \RuntimeException('Error validating article: ' . $model->getError());
}
// Emulate save task.
$this->app->input->set('task', 'save');
// Save an article.
if (!$model->save($article))
{
thrownew \RuntimeException('Error saving article: ' . $model->getError());
}
$this->saveArticleFields($model->getItem()->id);
}
/**
* Saves article custom fields.
*
* @param integer $articleId Article ID.
*
* @return void
*
* @since 1.0
* @throws \RuntimeException
*/
privatefunctionsaveArticleFields($articleId)
{
\JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');
/** @var FieldsModelField $model */
$model = BaseDatabaseModel::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
// Define the fields: Field ID => Field value.
$fields = [
1 => 'Text',
3 => ['Value 3', 'Value 1'],
4 => [2, 1]
];
// Save the fields.
foreach ($fieldsas$key => $value)
{
$model->setFieldValue($key, $articleId, $value);
}
}
}
CliApplication::getInstance('ArticleCreateCli')->execute();