- md5
- 3220bcf8defef9b535a2f6011b9d571f
- sha1
- e8c923605de214d305e616074bafad784e943f19
- sha256
- 0f5036fcc274dd66e812cd0e9daf41fcec7957c8ae92b85d59d91359fe121f4e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Table;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;
use Joomla\Utilities\ArrayHelper;
/**
* Core content table
*
* @since 3.1
*/
class CoreContent extends Table
{
/**
* Indicates that columns fully support the NULL value in the database
*
* @var boolean
* @since 4.0.0
*/
protected $_supportNullValue = true;
/**
* Constructor
*
* @param DatabaseDriver $db A database connector object
*
* @since 3.1
*/
public function __construct(DatabaseDriver $db)
{
parent::__construct('#__ucm_content', 'core_content_id', $db);
}
/**
* Overloaded bind function
*
* @param array $array Named array
* @param mixed $ignore An optional array or space separated list of properties
* to ignore while binding.
*
* @return mixed Null if operation was satisfactory, otherwise returns an error string
*
* @see Table::bind()
* @since 3.1
*/
public function bind($array, $ignore = '')
{
if (isset($array['core_params']) && \is_array($array['core_params']))
{
$registry = new Registry($array['core_params']);
$array['core_params'] = (string) $registry;
}
if (isset($array['core_metadata']) && \is_array($array['core_metadata']))
{
$registry = new Registry($array['core_metadata']);
$array['core_metadata'] = (string) $registry;
}
if (isset($array['core_images']) && \is_array($array['core_images']))
{
$registry = new Registry($array['core_images']);
$array['core_images'] = (string) $registry;
}
if (isset($array['core_urls']) && \is_array($array['core_urls']))
{
$registry = new Registry($array['core_urls']);
$array['core_urls'] = (string) $registry;
}
if (isset($array['core_body']) && \is_array($array['core_body']))
{
$registry = new Registry($array['core_body']);
$array['core_body'] = (string) $registry;
}
return parent::bind($array, $ignore);
}
/**
* Overloaded check function
*
* @return boolean True on success, false on failure
*
* @see Table::check()
* @since 3.1
*/
public function check()
{
try
{
parent::check();
}
catch (\Exception $e)
{
$this->setError($e->getMessage());
return false;
}
if (trim($this->core_title) === '')
{
$this->setError(Text::_('JLIB_CMS_WARNING_PROVIDE_VALID_NAME'));
return false;
}
if (trim($this->core_alias) === '')
{
$this->core_alias = $this->core_title;
}
$this->core_alias = ApplicationHelper::stringURLSafe($this->core_alias);
if (trim(str_replace('-', '', $this->core_alias)) === '')
{
$this->core_alias = Factory::getDate()->format('Y-m-d-H-i-s');
}
// Not Null sanity check
if (empty($this->core_images))
{
$this->core_images = '{}';
}
if (empty($this->core_urls))
{
$this->core_urls = '{}';
}
// Check the publish down date is not earlier than publish up.
if ($this->core_publish_up !== null
&& $this->core_publish_down !== null
&& $this->core_publish_down < $this->core_publish_up
&& $this->core_publish_down > $this->_db->getNullDate())
{
// Swap the dates.
$temp = $this->core_publish_up;
$this->core_publish_up = $this->core_publish_down;
$this->core_publish_down = $temp;
}
// Clean up keywords -- eliminate extra spaces between phrases
// and cr (\r) and lf (\n) characters from string
if (!empty($this->core_metakey))
{
// Only process if not empty
// Array of characters to remove
$bad_characters = array("\n", "\r", "\"", '<', '>');
// Remove bad characters
$after_clean = StringHelper::str_ireplace($bad_characters, '', $this->core_metakey);
// Create array using commas as delimiter
$keys = explode(',', $after_clean);
$clean_keys = array();
foreach ($keys as $key)
{
if (trim($key))
{
// Ignore blank keywords
$clean_keys[] = trim($key);
}
}
// Put array back together delimited by ", "
$this->core_metakey = implode(', ', $clean_keys);
}
return true;
}
/**
* Override JTable delete method to include deleting corresponding row from #__ucm_base.
*
* @param integer $pk primary key value to delete. Must be set or throws an exception.
*
* @return boolean True on success.
*
* @since 3.1
* @throws \UnexpectedValueException
*/
public function delete($pk = null)
{
$baseTable = Table::getInstance('Ucm', 'JTable', array('dbo' => $this->getDbo()));
return parent::delete($pk) && $baseTable->delete($pk);
}
/**
* Method to delete a row from the #__ucm_content table by content_item_id.
*
* @param integer $contentItemId value of the core_content_item_id to delete. Corresponds to the primary key of the content table.
* @param string $typeAlias Alias for the content type
*
* @return boolean True on success.
*
* @since 3.1
* @throws \UnexpectedValueException
*/
public function deleteByContentId($contentItemId = null, $typeAlias = null)
{
$contentItemId = (int) $contentItemId;
if ($contentItemId === 0)
{
throw new \UnexpectedValueException('Null content item key not allowed.');
}
if ($typeAlias === null)
{
throw new \UnexpectedValueException('Null type alias not allowed.');
}
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('core_content_id'))
->from($db->quoteName('#__ucm_content'))
->where(
[
$db->quoteName('core_content_item_id') . ' = :contentItemId',
$db->quoteName('core_type_alias') . ' = :typeAlias',
]
)
->bind(':contentItemId', $contentItemId, ParameterType::INTEGER)
->bind(':typeAlias', $typeAlias);
$db->setQuery($query);
if ($ucmId = $db->loadResult())
{
return $this->delete($ucmId);
}
else
{
return true;
}
}
/**
* Overrides Table::store to set modified data and user id.
*
* @param boolean $updateNulls True to update fields even if they are null.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function store($updateNulls = true)
{
$date = Factory::getDate();
$user = Factory::getUser();
if ($this->core_content_id)
{
// Existing item
$this->core_modified_time = $date->toSql();
$this->core_modified_user_id = $user->get('id');
$isNew = false;
}
else
{
// New content item. A content item core_created_time and core_created_user_id field can be set by the user,
// so we don't touch either of these if they are set.
if (!(int) $this->core_created_time)
{
$this->core_created_time = $date->toSql();
}
if (empty($this->core_created_user_id))
{
$this->core_created_user_id = $user->get('id');
}
if (!(int) $this->core_modified_time)
{
$this->core_modified_time = $this->core_created_time;
}
if (empty($this->core_modified_user_id))
{
$this->core_modified_user_id = $this->core_created_user_id;
}
$isNew = true;
}
$oldRules = $this->getRules();
if (empty($oldRules))
{
$this->setRules('{}');
}
$result = parent::store($updateNulls);
return $result && $this->storeUcmBase($updateNulls, $isNew);
}
/**
* Insert or update row in ucm_base table
*
* @param boolean $updateNulls True to update fields even if they are null.
* @param boolean $isNew if true, need to insert. Otherwise update.
*
* @return boolean True on success.
*
* @since 3.1
*/
protected function storeUcmBase($updateNulls = true, $isNew = false)
{
// Store the ucm_base row
$db = $this->getDbo();
$query = $db->getQuery(true);
$languageId = ContentHelper::getLanguageId($this->core_language);
// Selecting "all languages" doesn't give a language id - we can't store a blank string in non mysql databases, so save 0 (the default value)
if (!$languageId)
{
$languageId = 0;
}
if ($isNew)
{
$query->insert($db->quoteName('#__ucm_base'))
->columns(
[
$db->quoteName('ucm_id'),
$db->quoteName('ucm_item_id'),
$db->quoteName('ucm_type_id'),
$db->quoteName('ucm_language_id'),
]
)
->values(
implode(
',',
$query->bindArray(
[
$this->core_content_id,
$this->core_content_item_id,
$this->core_type_id,
$languageId,
]
)
)
);
}
else
{
$query->update($db->quoteName('#__ucm_base'))
->set(
[
$db->quoteName('ucm_item_id') . ' = :coreContentItemId',
$db->quoteName('ucm_type_id') . ' = :typeId',
$db->quoteName('ucm_language_id') . ' = :languageId',
]
)
->where($db->quoteName('ucm_id') . ' = :coreContentId')
->bind(':coreContentItemId', $this->core_content_item_id, ParameterType::INTEGER)
->bind(':typeId', $this->core_type_id, ParameterType::INTEGER)
->bind(':languageId', $languageId, ParameterType::INTEGER)
->bind(':coreContentId', $this->core_content_id, ParameterType::INTEGER);
}
$db->setQuery($query);
return $db->execute();
}
/**
* Method to set the publishing state for a row or list of rows in the database
* table. The method respects checked out rows by other users and will attempt
* to checkin rows that it can after adjustments are made.
*
* @param mixed $pks An optional array of primary key values to update. If not set the instance property value is used.
* @param integer $state The publishing state. eg. [0 = unpublished, 1 = published]
* @param integer $userId The user id of the user performing the operation.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function publish($pks = null, $state = 1, $userId = 0)
{
$k = $this->_tbl_key;
// Sanitize input.
$pks = ArrayHelper::toInteger($pks);
$userId = (int) $userId;
$state = (int) $state;
// If there are no primary keys set check to see if the instance key is set.
if (empty($pks))
{
if ($this->$k)
{
$pks = array($this->$k);
}
// Nothing to set publishing state on, return false.
else
{
$this->setError(Text::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
return false;
}
}
$pksImploded = implode(',', $pks);
// Get the DatabaseQuery object
$query = $this->_db->getQuery(true);
// Update the publishing state for rows with the given primary keys.
$query->update($this->_db->quoteName($this->_tbl))
->set($this->_db->quoteName('core_state') . ' = :state')
->whereIn($this->_db->quoteName($k), $pks)
->bind(':state', $state, ParameterType::INTEGER);
// Determine if there is checkin support for the table.
$checkin = false;
if ($this->hasField('core_checked_out_user_id') && $this->hasField('core_checked_out_time'))
{
$checkin = true;
$query->extendWhere(
'AND',
[
$this->_db->quoteName('core_checked_out_user_id') . ' IS NULL',
$this->_db->quoteName('core_checked_out_user_id') . ' = :userId',
],
'OR'
)
->bind(':userId', $userId, ParameterType::INTEGER);
}
$this->_db->setQuery($query);
try
{
$this->_db->execute();
}
catch (\RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
// If checkin is supported and all rows were adjusted, check them in.
if ($checkin && \count($pks) === $this->_db->getAffectedRows())
{
// Checkin the rows.
foreach ($pks as $pk)
{
$this->checkin($pk);
}
}
// If the JTable instance value is in the list of primary keys that were set, set the instance.
if (\in_array($this->$k, $pks))
{
$this->core_state = $state;
}
$this->setError('');
return true;
}
}
The same content is in 2 releases, Joomla 4.0.0-beta to 4.0.0-beta2. See them all, or every version of this file.