UCMContent.php
5.67 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\UCM;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Table\TableInterface;
/**
* Base class for implementing UCM
*
* @since 3.1
*/
class UCMContent extends UCMBase
{
/**
* The related table object
*
* @var Table
* @since 3.1
*/
protected $table;
/**
* The UCM data array
*
* @var array
* @since 3.1
*/
public $ucmData;
/**
* Instantiate UCMContent.
*
* @param TableInterface $table The table object
* @param string $alias The type alias
* @param UCMType $type The type object
*
* @since 3.1
*/
public function __construct(TableInterface $table = null, $alias = null, UCMType $type = null)
{
parent::__construct($alias, $type);
if ($table)
{
$this->table = $table;
}
else
{
$tableObject = json_decode($this->type->type->table);
$this->table = Table::getInstance($tableObject->special->type, $tableObject->special->prefix, $tableObject->special->config);
}
}
/**
* Method to save the data
*
* @param array $original The original data to be saved
* @param UCMType $type The UCM Type object
*
* @return boolean true
*
* @since 3.1
*/
public function save($original = null, UCMType $type = null)
{
$type = $type ?: $this->type;
$ucmData = $original ? $this->mapData($original, $type) : $this->ucmData;
// Store the Common fields
$this->store($ucmData['common']);
// Store the special fields
if (isset($ucmData['special']))
{
$table = $this->table;
$this->store($ucmData['special'], $table, '');
}
return true;
}
/**
* Delete content from the Core Content table
*
* @param mixed $pk The string/array of id's to delete
* @param UCMType $type The content type object
*
* @return boolean True if success
*
* @since 3.1
*/
public function delete($pk, UCMType $type = null)
{
$db = \JFactory::getDbo();
$type = $type ?: $this->type;
if (is_array($pk))
{
$pk = implode(',', $pk);
}
$query = $db->getQuery(true)
->delete('#__ucm_content')
->where($db->quoteName('core_type_id') . ' = ' . (int) $type->type_id)
->where($db->quoteName('core_content_item_id') . ' IN (' . $pk . ')');
$db->setQuery($query);
$db->execute();
return true;
}
/**
* Map the original content to the Core Content fields
*
* @param array $original The original data array
* @param UCMType $type Type object for this data
*
* @return array $ucmData The mapped UCM data
*
* @since 3.1
*/
public function mapData($original, UCMType $type = null)
{
$contentType = isset($type) ? $type : $this->type;
$fields = json_decode($contentType->type->field_mappings);
$ucmData = array();
$common = is_object($fields->common) ? $fields->common : $fields->common[0];
foreach ($common as $i => $field)
{
if ($field && $field !== 'null' && array_key_exists($field, $original))
{
$ucmData['common'][$i] = $original[$field];
}
}
if (array_key_exists('special', $ucmData))
{
$special = is_object($fields->special) ? $fields->special : $fields->special[0];
foreach ($special as $i => $field)
{
if ($field && $field !== 'null' && array_key_exists($field, $original))
{
$ucmData['special'][$i] = $original[$field];
}
}
}
$ucmData['common']['core_type_alias'] = $contentType->type->type_alias;
$ucmData['common']['core_type_id'] = $contentType->type->type_id;
if (isset($ucmData['special']))
{
$ucmData['special']['ucm_id'] = $ucmData['common']['ucm_id'];
}
$this->ucmData = $ucmData;
return $this->ucmData;
}
/**
* Store data to the appropriate table
*
* @param array $data Data to be stored
* @param TableInterface $table JTable Object
* @param boolean $primaryKey Flag that is true for data that are using #__ucm_content as their primary table
*
* @return boolean true on success
*
* @since 3.1
*/
protected function store($data, TableInterface $table = null, $primaryKey = null)
{
$table = $table ?: Table::getInstance('Corecontent');
$typeId = $this->getType()->type->type_id;
$primaryKey = $primaryKey ?: $this->getPrimaryKey($typeId, $data['core_content_item_id']);
if (!$primaryKey)
{
// Store the core UCM mappings
$baseData = array();
$baseData['ucm_type_id'] = $typeId;
$baseData['ucm_item_id'] = $data['core_content_item_id'];
$baseData['ucm_language_id'] = ContentHelper::getLanguageId($data['core_language']);
if (parent::store($baseData))
{
$primaryKey = $this->getPrimaryKey($typeId, $data['core_content_item_id']);
}
}
return parent::store($data, $table, $primaryKey);
}
/**
* Get the value of the primary key from #__ucm_base
*
* @param string $typeId The ID for the type
* @param integer $contentItemId Value of the primary key in the legacy or secondary table
*
* @return integer The integer of the primary key
*
* @since 3.1
*/
public function getPrimaryKey($typeId, $contentItemId)
{
$db = \JFactory::getDbo();
$queryccid = $db->getQuery(true);
$queryccid->select($db->quoteName('ucm_id'))
->from($db->quoteName('#__ucm_base'))
->where(
array(
$db->quoteName('ucm_item_id') . ' = ' . $db->quote($contentItemId),
$db->quoteName('ucm_type_id') . ' = ' . $db->quote($typeId),
)
);
$db->setQuery($queryccid);
return $db->loadResult();
}
}