1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class JUcmContent extends JUcmBase
18 {
19 20 21 22 23 24
25 protected $table;
26
27 28 29 30 31 32
33 public $ucmData;
34
35 36 37 38 39 40 41 42 43
44 public function __construct(JTableInterface $table = null, $alias = null, JUcmType $type = null)
45 {
46 parent::__construct($alias, $type);
47
48 if ($table)
49 {
50 $this->table = $table;
51 }
52 else
53 {
54 $tableObject = json_decode($this->type->type->table);
55 $this->table = JTable::getInstance($tableObject->special->type, $tableObject->special->prefix, $tableObject->special->config);
56 }
57 }
58
59 60 61 62 63 64 65 66 67 68
69 public function save($original = null, JUcmType $type = null)
70 {
71 $type = $type ?: $this->type;
72 $ucmData = $original ? $this->mapData($original, $type) : $this->ucmData;
73
74
75 $this->store($ucmData['common']);
76
77
78 if (isset($ucmData['special']))
79 {
80 $table = $this->table;
81 $this->store($ucmData['special'], $table, '');
82 }
83
84 return true;
85 }
86
87 88 89 90 91 92 93 94 95 96
97 public function delete($pk, JUcmType $type = null)
98 {
99 $db = JFactory::getDbo();
100 $type = $type ?: $this->type;
101
102 if (is_array($pk))
103 {
104 $pk = implode(',', $pk);
105 }
106
107 $query = $db->getQuery(true)
108 ->delete('#__ucm_content')
109 ->where($db->quoteName('core_type_id') . ' = ' . (int) $type->type_id)
110 ->where($db->quoteName('core_content_item_id') . ' IN (' . $pk . ')');
111
112 $db->setQuery($query);
113 $db->execute();
114
115 return true;
116 }
117
118 119 120 121 122 123 124 125 126 127
128 public function mapData($original, JUcmType $type = null)
129 {
130 $contentType = isset($type) ? $type : $this->type;
131
132 $fields = json_decode($contentType->type->field_mappings);
133
134 $ucmData = array();
135
136 $common = is_object($fields->common) ? $fields->common : $fields->common[0];
137
138 foreach ($common as $i => $field)
139 {
140 if ($field && $field !== 'null' && array_key_exists($field, $original))
141 {
142 $ucmData['common'][$i] = $original[$field];
143 }
144 }
145
146 if (array_key_exists('special', $ucmData))
147 {
148 $special = is_object($fields->special) ? $fields->special : $fields->special[0];
149
150 foreach ($special as $i => $field)
151 {
152 if ($field && $field !== 'null' && array_key_exists($field, $original))
153 {
154 $ucmData['special'][$i] = $original[$field];
155 }
156 }
157 }
158
159 $ucmData['common']['core_type_alias'] = $contentType->type->type_alias;
160 $ucmData['common']['core_type_id'] = $contentType->type->type_id;
161
162 if (isset($ucmData['special']))
163 {
164 $ucmData['special']['ucm_id'] = $ucmData['common']['ucm_id'];
165 }
166
167 $this->ucmData = $ucmData;
168
169 return $this->ucmData;
170 }
171
172 173 174 175 176 177 178 179 180 181 182
183 protected function store($data, JTableInterface $table = null, $primaryKey = null)
184 {
185 $table = $table ?: JTable::getInstance('Corecontent');
186
187 $typeId = $this->getType()->type->type_id;
188 $primaryKey = $primaryKey ?: $this->getPrimaryKey($typeId, $data['core_content_item_id']);
189
190 if (!$primaryKey)
191 {
192
193 $baseData = array();
194 $baseData['ucm_type_id'] = $typeId;
195 $baseData['ucm_item_id'] = $data['core_content_item_id'];
196 $baseData['ucm_language_id'] = JHelperContent::getLanguageId($data['core_language']);
197
198 if (parent::store($baseData))
199 {
200 $primaryKey = $this->getPrimaryKey($typeId, $data['core_content_item_id']);
201 }
202 }
203
204 return parent::store($data, $table, $primaryKey);
205 }
206
207 208 209 210 211 212 213 214 215 216
217 public function getPrimaryKey($typeId, $contentItemId)
218 {
219 $db = JFactory::getDbo();
220 $queryccid = $db->getQuery(true);
221 $queryccid->select($db->quoteName('ucm_id'))
222 ->from($db->quoteName('#__ucm_base'))
223 ->where(
224 array(
225 $db->quoteName('ucm_item_id') . ' = ' . $db->quote($contentItemId),
226 $db->quoteName('ucm_type_id') . ' = ' . $db->quote($typeId),
227 )
228 );
229 $db->setQuery($queryccid);
230
231 return $db->loadResult();
232 }
233 }
234