1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13 use Joomla\String\StringHelper;
14 use Joomla\Utilities\ArrayHelper;
15
16 17 18 19 20
21 class JTableCorecontent extends JTable
22 {
23 24 25 26 27 28 29
30 public function __construct($db)
31 {
32 parent::__construct('#__ucm_content', 'core_content_id', $db);
33 }
34
35 36 37 38 39 40 41 42 43 44 45 46
47 public function bind($array, $ignore = '')
48 {
49 if (isset($array['core_params']) && is_array($array['core_params']))
50 {
51 $registry = new Registry($array['core_params']);
52 $array['core_params'] = (string) $registry;
53 }
54
55 if (isset($array['core_metadata']) && is_array($array['core_metadata']))
56 {
57 $registry = new Registry($array['core_metadata']);
58 $array['core_metadata'] = (string) $registry;
59 }
60
61 if (isset($array['core_images']) && is_array($array['core_images']))
62 {
63 $registry = new Registry($array['core_images']);
64 $array['core_images'] = (string) $registry;
65 }
66
67 if (isset($array['core_urls']) && is_array($array['core_urls']))
68 {
69 $registry = new Registry($array['core_urls']);
70 $array['core_urls'] = (string) $registry;
71 }
72
73 if (isset($array['core_body']) && is_array($array['core_body']))
74 {
75 $registry = new Registry($array['core_body']);
76 $array['core_body'] = (string) $registry;
77 }
78
79 return parent::bind($array, $ignore);
80 }
81
82 83 84 85 86 87 88 89
90 public function check()
91 {
92 if (trim($this->core_title) === '')
93 {
94 $this->setError(JText::_('JLIB_CMS_WARNING_PROVIDE_VALID_NAME'));
95
96 return false;
97 }
98
99 if (trim($this->core_alias) === '')
100 {
101 $this->core_alias = $this->core_title;
102 }
103
104 $this->core_alias = JApplicationHelper::stringURLSafe($this->core_alias);
105
106 if (trim(str_replace('-', '', $this->core_alias)) === '')
107 {
108 $this->core_alias = JFactory::getDate()->format('Y-m-d-H-i-s');
109 }
110
111 if (empty($this->core_images))
112 {
113 $this->core_images = '{}';
114 }
115 if (empty($this->core_urls))
116 {
117 $this->core_urls = '{}';
118 }
119
120 if ($this->core_publish_down < $this->core_publish_up && $this->core_publish_down > $this->_db->getNullDate())
121 {
122
123 $temp = $this->core_publish_up;
124 $this->core_publish_up = $this->core_publish_down;
125 $this->core_publish_down = $temp;
126 }
127
128
129
130 if (!empty($this->core_metakey))
131 {
132
133
134
135 $bad_characters = array("\n", "\r", "\"", '<', '>');
136
137
138 $after_clean = StringHelper::str_ireplace($bad_characters, '', $this->core_metakey);
139
140
141 $keys = explode(',', $after_clean);
142
143 $clean_keys = array();
144
145 foreach ($keys as $key)
146 {
147 if (trim($key))
148 {
149
150 $clean_keys[] = trim($key);
151 }
152 }
153
154 $this->core_metakey = implode(', ', $clean_keys);
155 }
156
157 return true;
158 }
159
160 161 162 163 164 165 166 167 168 169
170 public function delete($pk = null)
171 {
172 $baseTable = JTable::getInstance('Ucm');
173
174 return parent::delete($pk) && $baseTable->delete($pk);
175 }
176
177 178 179 180 181 182 183 184 185 186 187
188 public function deleteByContentId($contentItemId = null, $typeAlias = null)
189 {
190 if ($contentItemId === null || ((int) $contentItemId) === 0)
191 {
192 throw new UnexpectedValueException('Null content item key not allowed.');
193 }
194
195 if ($typeAlias === null)
196 {
197 throw new UnexpectedValueException('Null type alias not allowed.');
198 }
199
200 $db = $this->getDbo();
201 $query = $db->getQuery(true);
202 $query->select($db->quoteName('core_content_id'))
203 ->from($db->quoteName('#__ucm_content'))
204 ->where($db->quoteName('core_content_item_id') . ' = ' . (int) $contentItemId)
205 ->where($db->quoteName('core_type_alias') . ' = ' . $db->quote($typeAlias));
206 $db->setQuery($query);
207
208 if ($ucmId = $db->loadResult())
209 {
210 return $this->delete($ucmId);
211 }
212 else
213 {
214 return true;
215 }
216 }
217
218 219 220 221 222 223 224 225 226
227 public function store($updateNulls = false)
228 {
229 $date = JFactory::getDate();
230 $user = JFactory::getUser();
231
232 if ($this->core_content_id)
233 {
234
235 $this->core_modified_time = $date->toSql();
236 $this->core_modified_user_id = $user->get('id');
237 $isNew = false;
238 }
239 else
240 {
241
242
243 if (!(int) $this->core_created_time)
244 {
245 $this->core_created_time = $date->toSql();
246 }
247
248 if (empty($this->core_created_user_id))
249 {
250 $this->core_created_user_id = $user->get('id');
251 }
252
253 $isNew = true;
254 }
255
256 $oldRules = $this->getRules();
257
258 if (empty($oldRules))
259 {
260 $this->setRules('{}');
261 }
262
263 $result = parent::store($updateNulls);
264
265 return $result && $this->storeUcmBase($updateNulls, $isNew);
266 }
267
268 269 270 271 272 273 274 275 276 277
278 protected function storeUcmBase($updateNulls = false, $isNew = false)
279 {
280
281 $db = $this->getDbo();
282 $query = $db->getQuery(true);
283 $languageId = JHelperContent::getLanguageId($this->core_language);
284
285
286 if (!$languageId)
287 {
288 $languageId = '0';
289 }
290
291 if ($isNew)
292 {
293 $query->insert($db->quoteName('#__ucm_base'))
294 ->columns(array($db->quoteName('ucm_id'), $db->quoteName('ucm_item_id'), $db->quoteName('ucm_type_id'), $db->quoteName('ucm_language_id')))
295 ->values(
296 $db->quote($this->core_content_id) . ', '
297 . $db->quote($this->core_content_item_id) . ', '
298 . $db->quote($this->core_type_id) . ', '
299 . $db->quote($languageId)
300 );
301 }
302 else
303 {
304 $query->update($db->quoteName('#__ucm_base'))
305 ->set($db->quoteName('ucm_item_id') . ' = ' . $db->quote($this->core_content_item_id))
306 ->set($db->quoteName('ucm_type_id') . ' = ' . $db->quote($this->core_type_id))
307 ->set($db->quoteName('ucm_language_id') . ' = ' . $db->quote($languageId))
308 ->where($db->quoteName('ucm_id') . ' = ' . $db->quote($this->core_content_id));
309 }
310
311 $db->setQuery($query);
312
313 return $db->execute();
314 }
315
316 317 318 319 320 321 322 323 324 325 326 327 328
329 public function publish($pks = null, $state = 1, $userId = 0)
330 {
331 $k = $this->_tbl_key;
332
333
334 $pks = ArrayHelper::toInteger($pks);
335 $userId = (int) $userId;
336 $state = (int) $state;
337
338
339 if (empty($pks))
340 {
341 if ($this->$k)
342 {
343 $pks = array($this->$k);
344 }
345
346 else
347 {
348 $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
349
350 return false;
351 }
352 }
353
354 $pksImploded = implode(',', $pks);
355
356
357 $query = $this->_db->getQuery(true);
358
359
360 $query->update($this->_db->quoteName($this->_tbl))
361 ->set($this->_db->quoteName('core_state') . ' = ' . (int) $state)
362 ->where($this->_db->quoteName($k) . 'IN (' . $pksImploded . ')');
363
364
365 $checkin = false;
366
367 if (property_exists($this, 'core_checked_out_user_id') && property_exists($this, 'core_checked_out_time'))
368 {
369 $checkin = true;
370 $query->where(
371 ' ('
372 . $this->_db->quoteName('core_checked_out_user_id') . ' = 0 OR ' . $this->_db->quoteName('core_checked_out_user_id') . ' = ' . (int) $userId
373 . ')'
374 );
375 }
376
377 $this->_db->setQuery($query);
378
379 try
380 {
381 $this->_db->execute();
382 }
383 catch (RuntimeException $e)
384 {
385 $this->setError($e->getMessage());
386
387 return false;
388 }
389
390
391 if ($checkin && count($pks) === $this->_db->getAffectedRows())
392 {
393
394 foreach ($pks as $pk)
395 {
396 $this->checkin($pk);
397 }
398 }
399
400
401 if (in_array($this->$k, $pks))
402 {
403 $this->core_state = $state;
404 }
405
406 $this->setError('');
407
408 return true;
409 }
410 }
411