-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert-cat-example.php
More file actions
151 lines (121 loc) · 4.2 KB
/
Copy pathinsert-cat-example.php
File metadata and controls
151 lines (121 loc) · 4.2 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
<?php
/************************
Example Client App for inserting categories
*************************/
// Make sure we're being called from the command line, not a web interface
if (PHP_SAPI !== 'cli')
{
die('This is a command line only application.');
}
// We are a valid entry point.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// ----------------------
class InsertCatExampleCli extends JApplicationCli {
public function __construct() {
parent::__construct();
$this->config = new JConfig();
$this->dbo = JDatabase::getInstance(
array(
'driver' => $this->config->dbtype,
'host' => $this->config->host,
'user' => $this->config->user,
'password' => $this->config->password,
'database' => $this->config->db,
'prefix' => $this->config->dbprefix,
)
);
// important for storing several rows
JFactory::$application = $this;
}
public function doExecute() {
$category_titles = array("Test Title 1", "Test Title 2", "Test Title 3");
// add categories to database
for ($i=0; $i<count($category_titles); $i++) {
$this->insertCategory($category_titles[$i]);
}
}
private function insertCategory($cat_title) {
$tbl_cat = JTable::getInstance("Category");
// get existing aliases from categories table
$tab = $this->dbo->quoteName($this->config->dbprefix . "categories");
$conditions = array(
$this->dbo->quoteName("extension") . " LIKE 'com_content'"
);
$query = $this->dbo->getQuery(true);
$query
->select($this->dbo->quoteName("alias"))
->from($tab)
->where($conditions);
$this->dbo->setQuery($query);
$cat_from_db = $this->dbo->loadObjectList();
$category_existing = False;
$new_cat_alias = JFilterOutput::stringURLSafe($cat_title);
foreach ($cat_from_db as $cdb) {
if ($cdb->alias == $new_cat_alias) {
$category_existing = True;
echo "category already existing: " . $new_cat_alias . "\n";
}
}
// ----------------
if (!$category_existing) {
$values = [
"id" => null,
"title" => $cat_title,
"path" => $new_cat_alias,
"access" => 1,
"extension" => "com_content",
"published" => 1,
"language" => "*",
"created_user_id" => 0,
"params" => array (
"category_layout" => "",
"image" => "",
),
"metadata" => array (
"author" => "",
"robots" => "",
),
];
$tbl_cat->setLocation(1, "last-child");
if (!$tbl_cat->bind($values)) {
JError::raiseWarning(500, $row->getError());
return FALSE;
}
if (!$tbl_cat->check()) {
JError::raiseError(500, $article->getError());
return FALSE;
}
if (!$tbl_cat->store(TRUE)) {
JError::raiseError(500, $article->getError());
return FALSE;
}
$tbl_cat->rebuildPath($tbl_cat->id);
echo "category inserted: " . $tbl_cat->id . " - " . $new_cat_alias . "\n";
}
}
}
try {
JApplicationCli::getInstance('InsertCatExampleCli')->execute();
}
catch (Exception $e) {
// An exception has been caught, just echo the message.
fwrite(STDOUT, $e->getMessage() . "\n");
exit($e->getCode());
}
?>