- md5
- ad66bcb749e039ca205cd0bf253b7c59
- sha1
- ad170736c8bfa851cae0d3eafca4fe3f35b85c72
- sha256
- 93e73f3d58fec856c3fbb46af7785d1a57c7660bd4fe98d55932d828672d69f7
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @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\Component\Content\Administrator\Model;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\String\PunycodeHelper;
use Joomla\CMS\Table\Category;
use Joomla\CMS\Table\TableInterface;
use Joomla\CMS\UCM\UCMType;
use Joomla\CMS\Workflow\Workflow;
use Joomla\Component\Categories\Administrator\Helper\CategoriesHelper;
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
use Joomla\Component\Content\Administrator\Helper\ContentHelper;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Component\Workflow\Administrator\Table\StageTable;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
/**
* Item Model for an Article.
*
* @since 1.6
*/
class ArticleModel extends AdminModel
{
/**
* The prefix to use with controller messages.
*
* @var string
* @since 1.6
*/
protected $text_prefix = 'COM_CONTENT';
/**
* The type alias for this content type (for example, 'com_content.article').
*
* @var string
* @since 3.2
*/
public $typeAlias = 'com_content.article';
/**
* The context used for the associations table
*
* @var string
* @since 3.4.4
*/
protected $associationsContext = 'com_content.item';
/**
* Function that can be overriden to do any data cleanup after batch copying data
*
* @param TableInterface $table The table object containing the newly created item
* @param integer $newId The id of the new item
* @param integer $oldId The original item id
*
* @return void
*
* @since 3.8.12
*/
protected function cleanupPostBatchCopy(TableInterface $table, $newId, $oldId)
{
// Check if the article was featured and update the #__content_frontpage table
if ($table->featured == 1)
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->insert($db->quoteName('#__content_frontpage'))
->values($newId . ', 0');
$db->setQuery($query);
$db->execute();
}
// Register FieldsHelper
\JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$oldItem = $this->getTable();
$oldItem->load($oldId);
$fields = FieldsHelper::getFields('com_content.article', $oldItem, true);
$fieldsData = array();
if (!empty($fields))
{
$fieldsData['com_fields'] = array();
foreach ($fields as $field)
{
$fieldsData['com_fields'][$field->name] = $field->rawvalue;
}
}
Factory::getApplication()->triggerEvent('onContentAfterSave', array('com_content.article', &$this->table, false, $fieldsData));
}
/**
* Batch change workflow stage or current.
*
* @param integer $value The workflow stage ID.
* @param array $pks An array of row IDs.
* @param array $contexts An array of item contexts.
*
* @return mixed An array of new IDs on success, boolean false on failure.
*
* @since 4.0.0
*/
protected function batchWorkflowStage($value, $pks, $contexts)
{
$user = Factory::getUser();
if (!$user->authorise('core.admin', 'com_content'))
{
$this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EXECUTE_TRANSITION'));
}
// Get workflow stage information
$stage = new StageTable($this->_db);
if (empty($value) || !$stage->load($value))
{
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error');
return false;
}
if (empty($pks))
{
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error');
return false;
}
$workflow = new Workflow(['extension' => 'com_content']);
// Update content state value and workflow associations
return ContentHelper::updateContentState($pks, $stage->condition)
&& $workflow->updateAssociations($pks, $value);
}
/**
* Batch move categories to a new category.
*
* @param integer $value The new category ID.
* @param array $pks An array of row IDs.
* @param array $contexts An array of item contexts.
*
* @return boolean True on success.
*
* @since 3.8.6
*/
protected function batchMove($value, $pks, $contexts)
{
if (empty($this->batchSet))
{
// Set some needed variables.
$this->user = Factory::getUser();
$this->table = $this->getTable();
$this->tableClassName = get_class($this->table);
$this->contentType = new UCMType;
$this->type = $this->contentType->getTypeByTable($this->tableClassName);
}
$categoryId = (int) $value;
if (!$this->checkCategoryId($categoryId))
{
return false;
}
PluginHelper::importPlugin('system');
// Parent exists so we proceed
foreach ($pks as $pk)
{
if (!$this->user->authorise('core.edit', $contexts[$pk]))
{
$this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
return false;
}
// Check that the row actually exists
if (!$this->table->load($pk))
{
if ($error = $this->table->getError())
{
// Fatal error
$this->setError($error);
return false;
}
else
{
// Not fatal error
$this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
continue;
}
}
$fields = FieldsHelper::getFields('com_content.article', $this->table, true);
$fieldsData = array();
if (!empty($fields))
{
$fieldsData['com_fields'] = array();
foreach ($fields as $field)
{
$fieldsData['com_fields'][$field->name] = $field->rawvalue;
}
}
// Set the new category ID
$this->table->catid = $categoryId;
// Check the row.
if (!$this->table->check())
{
$this->setError($this->table->getError());
return false;
}
// Store the row.
if (!$this->table->store())
{
$this->setError($this->table->getError());
return false;
}
// Run event for moved article
Factory::getApplication()->triggerEvent('onContentAfterSave', array('com_content.article', &$this->table, false, $fieldsData));
}
// Clean the cache
$this->cleanCache();
return true;
}
/**
* Method to test whether a record can be deleted.
*
* @param object $record A record object.
*
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canDelete($record)
{
if (!empty($record->id))
{
$stage = new StageTable($this->_db);
$workflow = new Workflow(['extension' => 'com_content']);
$assoc = $workflow->getAssociation($record->id);
if (!$stage->load($assoc->stage_id) || ($stage->condition != ContentComponent::CONDITION_TRASHED && !Factory::getApplication()->isClient('api')))
{
return false;
}
return Factory::getUser()->authorise('core.delete', 'com_content.article.' . (int) $record->id);
}
return false;
}
/**
* Method to test whether a record can have its state edited.
*
* @param object $record A record object.
*
* @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canEditState($record)
{
$user = Factory::getUser();
// Check for existing article.
if (!empty($record->id))
{
return $user->authorise('core.edit.state', 'com_content.article.' . (int) $record->id);
}
// New article, so check against the category.
if (!empty($record->catid))
{
return $user->authorise('core.edit.state', 'com_content.category.' . (int) $record->catid);
}
// Default to component settings if neither article nor category known.
return parent::canEditState($record);
}
/**
* Prepare and sanitise the table data prior to saving.
*
* @param \Joomla\CMS\Table\Table $table A Table object.
*
* @return void
*
* @since 1.6
*/
protected function prepareTable($table)
{
// Set the publish date to now
if ($table->state == Workflow::CONDITION_PUBLISHED && (int) $table->publish_up == 0)
{
$table->publish_up = Factory::getDate()->toSql();
}
if ($table->state == Workflow::CONDITION_PUBLISHED && intval($table->publish_down) == 0)
{
$table->publish_down = $this->getDbo()->getNullDate();
}
// Increment the content version number.
$table->version++;
// Reorder the articles within the category so the new article is first
if (empty($table->id))
{
$table->reorder('catid = ' . (int) $table->catid . ' AND state >= 0');
}
}
/**
* Method to change the published state of one or more records.
*
* @param array &$pks A list of the primary keys to change.
* @param integer $value The value of the published state.
*
* @return boolean True on success.
*
* @since 4.0.0
*/
public function publish(&$pks, $value = 1)
{
$input = Factory::getApplication()->input;
$user = Factory::getUser();
$table = $this->getTable();
$pks = (array) $pks;
$itrans = $input->get('publish_transitions', [], 'array');
// Include the plugins for the change of state event.
PluginHelper::importPlugin($this->events_map['change_state']);
$db = $this->getDbo();
$query = $db->getQuery(true);
$select = $db->quoteName(
[
'wt.id',
'wa.item_id'
]
);
$query ->select($select)
->from($db->quoteName('#__workflow_transitions', 'wt'))
->from($db->quoteName('#__workflow_stages', 'ws'))
->from($db->quoteName('#__workflow_stages', 'ws2'))
->from($db->quoteName('#__workflow_associations', 'wa'))
->whereIn($db->quoteName('wt.from_stage_id'), [-1, $db->quoteName('wa.stage_id')])
->where($db->quoteName('wt.to_stage_id') . ' = ' . $db->quoteName('ws.id'))
->where($db->quoteName('wa.stage_id') . ' = ' . $db->quoteName('ws2.id'))
->where($db->quoteName('wt.workflow_id') . ' = ' . $db->quoteName('ws.workflow_id'))
->where($db->quoteName('wt.workflow_id') . ' = ' . $db->quoteName('ws2.workflow_id'))
->where($db->quoteName('wt.to_stage_id') . ' != ' . $db->quoteName('wa.stage_id'))
->whereIn($db->quoteName('wa.item_id'), $pks)
->where($db->quoteName('wa.extension') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('ws.condition') . ' = ' . (int) $value);
$transitions = $db->setQuery($query)->loadObjectList();
$items = [];
foreach ($transitions as $transition)
{
if ($user->authorise('core.execute.transition', 'com_content.transition.' . $transition->id))
{
if (!isset($itrans[$transition->item_id]) || $itrans[$transition->item_id] == $transition->id)
{
$items[$transition->item_id] = $transition->id;
}
}
}
// Access checks.
foreach ($pks as $i => $pk)
{
$table->reset();
if ($table->load($pk))
{
if (!isset($items[$pk]))
{
// Prune items that you can't change.
unset($pks[$i]);
Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror');
return false;
}
// If the table is checked out by another user, drop it and report to the user trying to change its state.
if ($table->hasField('checked_out') && $table->checked_out && ($table->checked_out != $user->id))
{
Log::add(Text::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), Log::WARNING, 'jerror');
// Prune items that you can't change.
unset($pks[$i]);
return false;
}
}
}
foreach ($pks as $i => $pk)
{
if (!$this->runTransition($pk, $items[$pk]))
{
return false;
}
}
// Clear the component's cache
$this->cleanCache();
return true;
}
/**
* Method to get a single record.
*
* @param integer $pk The id of the primary key.
*
* @return mixed Object on success, false on failure.
*/
public function getItem($pk = null)
{
if ($item = parent::getItem($pk))
{
// Convert the params field to an array.
$registry = new Registry($item->attribs);
$item->attribs = $registry->toArray();
// Convert the metadata field to an array.
$registry = new Registry($item->metadata);
$item->metadata = $registry->toArray();
// Convert the images field to an array.
$registry = new Registry($item->images);
$item->images = $registry->toArray();
// Convert the urls field to an array.
$registry = new Registry($item->urls);
$item->urls = $registry->toArray();
$item->articletext = trim($item->fulltext) != '' ? $item->introtext . "<hr id=\"system-readmore\">" . $item->fulltext : $item->introtext;
if (!empty($item->id))
{
$item->tags = new \JHelperTags;
$item->tags->getTagIds($item->id, 'com_content.article');
}
}
// Load associated content items
$assoc = Associations::isEnabled();
if ($assoc)
{
$item->associations = array();
if ($item->id != null)
{
$associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $item->id);
foreach ($associations as $tag => $association)
{
$item->associations[$tag] = $association->id;
}
}
}
return $item;
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return Form|boolean A Form object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_content.article', 'article', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
$jinput = Factory::getApplication()->input;
/*
* The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
* The back end uses id so we use that the rest of the time and set it to 0 by default.
*/
$id = $jinput->get('a_id', $jinput->get('id', 0));
// Determine correct permissions to check.
if ($id = $this->getState('article.id', $id))
{
// Existing record. Can only edit in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.edit');
// Existing record. Can only edit own articles in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.edit.own');
$table = $this->getTable();
if ($table->load(array('id' => $id)))
{
$workflow = new Workflow(['extension' => 'com_content']);
// Transition field
$assoc = $workflow->getAssociation($table->id);
$form->setFieldAttribute('transition', 'workflow_stage', (int) $assoc->stage_id);
}
}
else
{
// For new articles we load the potential state + associations
if ($formField = $form->getField('catid'))
{
$assignedCatids = (int) ($data['catid'] ?? $form->getValue('catid'));
$assignedCatids = is_array($assignedCatids)
? (int) reset($assignedCatids)
: (int) $assignedCatids;
// Try to get the category from the html code of the field
if (empty($assignedCatids))
{
$assignedCatids = $formField->getAttribute('default', null);
// Choose the first category available
$xml = new \DOMDocument;
libxml_use_internal_errors(true);
$xml->loadHTML($formField->__get('input'));
libxml_clear_errors();
libxml_use_internal_errors(false);
$options = $xml->getElementsByTagName('option');
if (!$assignedCatids && $firstChoice = $options->item(0))
{
$assignedCatids = $firstChoice->getAttribute('value');
}
}
// Activate the reload of the form when category is changed
$form->setFieldAttribute('catid', 'refresh-enabled', true);
$form->setFieldAttribute('catid', 'refresh-cat-id', $assignedCatids);
$form->setFieldAttribute('catid', 'refresh-section', 'article');
$workflow = $this->getWorkflowByCategory($assignedCatids);
$form->setFieldAttribute('transition', 'workflow_stage', (int) $workflow->stage_id);
}
// New record. Can only create in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.create');
}
$user = Factory::getUser();
// Check for existing article.
// Modify the form based on Edit State access controls.
if ($id != 0 && (!$user->authorise('core.edit.state', 'com_content.article.' . (int) $id))
|| ($id == 0 && !$user->authorise('core.edit.state', 'com_content')))
{
// Disable fields for display.
$form->setFieldAttribute('featured', 'disabled', 'true');
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('publish_up', 'disabled', 'true');
$form->setFieldAttribute('publish_down', 'disabled', 'true');
$form->setFieldAttribute('state', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is an article you can edit.
$form->setFieldAttribute('featured', 'filter', 'unset');
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('publish_up', 'filter', 'unset');
$form->setFieldAttribute('publish_down', 'filter', 'unset');
$form->setFieldAttribute('state', 'filter', 'unset');
}
// Prevent messing with article language and category when editing existing article with associations
$app = Factory::getApplication();
$assoc = Associations::isEnabled();
// Check if article is associated
if ($this->getState('article.id') && $app->isClient('site') && $assoc)
{
$associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $id);
// Make fields read only
if (!empty($associations))
{
$form->setFieldAttribute('language', 'readonly', 'true');
$form->setFieldAttribute('catid', 'readonly', 'true');
$form->setFieldAttribute('language', 'filter', 'unset');
$form->setFieldAttribute('catid', 'filter', 'unset');
}
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$app = Factory::getApplication();
$data = $app->getUserState('com_content.edit.article.data', array());
if (empty($data))
{
$data = $this->getItem();
// Pre-select some filters (Status, Category, Language, Access) in edit form if those have been selected in Article Manager: Articles
if ($this->getState('article.id') == 0)
{
$filters = (array) $app->getUserState('com_content.articles.filter');
$data->set(
'state',
$app->input->getInt(
'state',
((isset($filters['published']) && $filters['published'] !== '') ? $filters['published'] : null)
)
);
$data->set('catid', $app->input->getInt('catid', (!empty($filters['category_id']) ? $filters['category_id'] : null)));
$data->set('language', $app->input->getString('language', (!empty($filters['language']) ? $filters['language'] : null)));
$data->set('access',
$app->input->getInt('access', (!empty($filters['access']) ? $filters['access'] : $app->get('access')))
);
}
}
// If there are params fieldsets in the form it will fail with a registry object
if (isset($data->params) && $data->params instanceof Registry)
{
$data->params = $data->params->toArray();
}
$this->preprocessData('com_content.article', $data);
return $data;
}
/**
* Method to validate the form data.
*
* @param Form $form The form to validate against.
* @param array $data The data to validate.
* @param string $group The name of the field group to validate.
*
* @return array|boolean Array of filtered data if valid, false otherwise.
*
* @see JFormRule
* @see JFilterInput
* @since 3.7.0
*/
public function validate($form, $data, $group = null)
{
// Don't allow to change the users if not allowed to access com_users.
if (Factory::getApplication()->isClient('administrator') && !Factory::getUser()->authorise('core.manage', 'com_users'))
{
if (isset($data['created_by']))
{
unset($data['created_by']);
}
if (isset($data['modified_by']))
{
unset($data['modified_by']);
}
}
return parent::validate($form, $data, $group);
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success.
*
* @since 1.6
*/
public function save($data)
{
$input = Factory::getApplication()->input;
$filter = \JFilterInput::getInstance();
$db = $this->getDbo();
$user = Factory::getUser();
if (isset($data['metadata']) && isset($data['metadata']['author']))
{
$data['metadata']['author'] = $filter->clean($data['metadata']['author'], 'TRIM');
}
if (isset($data['created_by_alias']))
{
$data['created_by_alias'] = $filter->clean($data['created_by_alias'], 'TRIM');
}
if (isset($data['images']) && is_array($data['images']))
{
$registry = new Registry($data['images']);
$data['images'] = (string) $registry;
}
// Cast catid to integer for comparison
$catid = (int) $data['catid'];
// Check if New Category exists
if ($catid > 0)
{
$catid = CategoriesHelper::validateCategoryId($data['catid'], 'com_content');
}
// Save New Category
if ($catid == 0 && $this->canCreateCategory())
{
$table = array();
$table['title'] = $data['catid'];
$table['parent_id'] = 1;
$table['extension'] = 'com_content';
$table['language'] = $data['language'];
$table['published'] = 1;
// Create new category and get catid back
$data['catid'] = CategoriesHelper::createCategory($table);
}
if (isset($data['urls']) && is_array($data['urls']))
{
$check = $input->post->get('jform', array(), 'array');
foreach ($data['urls'] as $i => $url)
{
if ($url != false && ($i == 'urla' || $i == 'urlb' || $i == 'urlc'))
{
if (preg_match('~^#[a-zA-Z]{1}[a-zA-Z0-9-_:.]*$~', $check['urls'][$i]) == 1)
{
$data['urls'][$i] = $check['urls'][$i];
}
else
{
$data['urls'][$i] = PunycodeHelper::urlToPunycode($url);
}
}
}
unset($check);
$registry = new Registry($data['urls']);
$data['urls'] = (string) $registry;
}
// Alter the title for save as copy
if ($input->get('task') == 'save2copy')
{
$origTable = clone $this->getTable();
$origTable->load($input->getInt('id'));
if ($data['title'] == $origTable->title)
{
list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
$data['title'] = $title;
$data['alias'] = $alias;
}
else
{
if ($data['alias'] == $origTable->alias)
{
$data['alias'] = '';
}
}
}
$workflowId = 0;
$stageId = 0;
// Set status depending on category
if (empty($data['id']))
{
$workflow = $this->getWorkflowByCategory($data['catid']);
if (empty($workflow->id))
{
$this->setError(Text::_('COM_CONTENT_WORKFLOW_NOT_FOUND'));
return false;
}
$stageId = (int) $workflow->stage_id;
// B/C state
$data['state'] = (int) $workflow->condition;
}
// Calculate new status depending on transition
if (!empty($data['transition']))
{
// Check if the user is allowed to execute this transition
if (!$user->authorise('core.execute.transition', 'com_content.transition.' . (int) $data['transition']))
{
$this->setError(Text::_('COM_CONTENT_WORKFLOW_TRANSITION_NOT_ALLOWED'));
return false;
}
// Set the new state
$query = $db->getQuery(true);
$query ->select($db->quoteName(['ws.id', 'ws.condition']))
->from($db->quoteName('#__workflow_stages', 'ws'))
->from($db->quoteName('#__workflow_transitions', 'wt'))
->where($db->quoteName('wt.to_stage_id') . ' = ' . $db->quoteName('ws.id'))
->where($db->quoteName('wt.id') . ' = ' . (int) $data['transition'])
->where($db->quoteName('ws.published') . ' = 1')
->where($db->quoteName('wt.published') . ' = 1');
$stage = $db->setQuery($query)->loadObject();
if (empty($stage->id))
{
$this->setError(Text::_('COM_CONTENT_WORKFLOW_TRANSITION_NOT_ALLOWED'));
return false;
}
$data['state'] = (int) $stage->condition;
}
// Automatic handling of alias for empty fields
if (in_array($input->get('task'), array('apply', 'save', 'save2new')) && (!isset($data['id']) || (int) $data['id'] == 0))
{
if ($data['alias'] == null)
{
if (Factory::getApplication()->get('unicodeslugs') == 1)
{
$data['alias'] = \JFilterOutput::stringURLUnicodeSlug($data['title']);
}
else
{
$data['alias'] = \JFilterOutput::stringURLSafe($data['title']);
}
$table = \JTable::getInstance('Content', 'JTable');
if ($table->load(array('alias' => $data['alias'], 'catid' => $data['catid'])))
{
$msg = Text::_('COM_CONTENT_SAVE_WARNING');
}
list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
$data['alias'] = $alias;
if (isset($msg))
{
Factory::getApplication()->enqueueMessage($msg, 'warning');
}
}
}
$workflow = new Workflow(['extension' => 'com_content']);
if (parent::save($data))
{
if (isset($data['featured']))
{
$this->featured($this->getState($this->getName() . '.id'), $data['featured']);
}
// Let's check if we have workflow association (perhaps something went wrong before)
if (empty($stageId))
{
$assoc = $workflow->getAssociation($this->getState($this->getName() . '.id'));
// If not, reset the state and let's create the associations
if (empty($assoc->item_id))
{
$table = $this->getTable();
$table->load((int) $this->getState($this->getName() . '.id'));
$workflow = $this->getWorkflowByCategory($table->catid);
if (empty($workflow->id))
{
$this->setError(Text::_('COM_CONTENT_WORKFLOW_NOT_FOUND'));
return false;
}
$stageId = (int) $workflow->stage_id;
// B/C state
$table->state = $workflow->condition;
$table->store();
}
}
// If we have a new state, create the workflow association
if (!empty($stageId))
{
$workflow->createAssociation($this->getState($this->getName() . '.id'), (int) $stageId);
}
// Run the transition and update the workflow association
if (!empty($data['transition']))
{
$this->runTransition((int) $this->getState($this->getName() . '.id'), (int) $data['transition']);
}
return true;
}
return false;
}
/**
* Method to toggle the featured setting of articles.
*
* @param array $pks The ids of the items to toggle.
* @param integer $value The value to toggle to.
*
* @return boolean True on success.
*/
public function featured($pks, $value = 0)
{
// Sanitize the ids.
$pks = (array) $pks;
$pks = ArrayHelper::toInteger($pks);
if (empty($pks))
{
$this->setError(Text::_('COM_CONTENT_NO_ITEM_SELECTED'));
return false;
}
$table = $this->getTable('Featured', 'Administrator');
try
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->update($db->quoteName('#__content'))
->set('featured = ' . (int) $value)
->where('id IN (' . implode(',', $pks) . ')');
$db->setQuery($query);
$db->execute();
if ((int) $value == 0)
{
// Adjust the mapping table.
// Clear the existing features settings.
$query = $db->getQuery(true)
->delete($db->quoteName('#__content_frontpage'))
->where('content_id IN (' . implode(',', $pks) . ')');
$db->setQuery($query);
$db->execute();
}
else
{
// First, we find out which of our new featured articles are already featured.
$query = $db->getQuery(true)
->select('f.content_id')
->from('#__content_frontpage AS f')
->where('content_id IN (' . implode(',', $pks) . ')');
$db->setQuery($query);
$oldFeatured = $db->loadColumn();
// We diff the arrays to get a list of the articles that are newly featured
$newFeatured = array_diff($pks, $oldFeatured);
// Featuring.
$tuples = array();
foreach ($newFeatured as $pk)
{
$tuples[] = $pk . ', 0';
}
if (count($tuples))
{
$columns = array('content_id', 'ordering');
$query = $db->getQuery(true)
->insert($db->quoteName('#__content_frontpage'))
->columns($db->quoteName($columns))
->values($tuples);
$db->setQuery($query);
$db->execute();
}
}
}
catch (\Exception $e)
{
$this->setError($e->getMessage());
return false;
}
$table->reorder();
$this->cleanCache();
return true;
}
/**
* A protected method to get a set of ordering conditions.
*
* @param object $table A record object.
*
* @return array An array of conditions to add to ordering queries.
*
* @since 1.6
*/
protected function getReorderConditions($table)
{
return [
$this->_db->quoteName('catid') . ' = ' . (int) $table->catid,
];
}
/**
* Allows preprocessing of the \JForm object.
*
* @param Form $form The form object
* @param array $data The data to be merged into the form object
* @param string $group The plugin group to be executed
*
* @return void
*
* @since 3.0
*/
protected function preprocessForm(\JForm $form, $data, $group = 'content')
{
if ($this->canCreateCategory())
{
$form->setFieldAttribute('catid', 'allowAdd', 'true');
}
// Association content items
if (Associations::isEnabled())
{
$languages = LanguageHelper::getContentLanguages(false, true, null, 'ordering', 'asc');
if (count($languages) > 1)
{
$addform = new \SimpleXMLElement('<form />');
$fields = $addform->addChild('fields');
$fields->addAttribute('name', 'associations');
$fieldset = $fields->addChild('fieldset');
$fieldset->addAttribute('name', 'item_associations');
foreach ($languages as $language)
{
$field = $fieldset->addChild('field');
$field->addAttribute('name', $language->lang_code);
$field->addAttribute('type', 'modal_article');
$field->addAttribute('language', $language->lang_code);
$field->addAttribute('label', $language->title);
$field->addAttribute('translate_label', 'false');
$field->addAttribute('select', 'true');
$field->addAttribute('new', 'true');
$field->addAttribute('edit', 'true');
$field->addAttribute('clear', 'true');
$field->addAttribute('propagate', 'true');
}
$form->load($addform, false);
}
}
parent::preprocessForm($form, $data, $group);
}
/**
* Custom clean the cache of com_content and content modules
*
* @param string $group The cache group
* @param integer $client_id The ID of the client
*
* @return void
*
* @since 1.6
*/
protected function cleanCache($group = null, $client_id = 0)
{
parent::cleanCache('com_content');
parent::cleanCache('mod_articles_archive');
parent::cleanCache('mod_articles_categories');
parent::cleanCache('mod_articles_category');
parent::cleanCache('mod_articles_latest');
parent::cleanCache('mod_articles_news');
parent::cleanCache('mod_articles_popular');
}
/**
* Void hit function for pagebreak when editing content from frontend
*
* @return void
*
* @since 3.6.0
*/
public function hit()
{
return;
}
/**
* Is the user allowed to create an on the fly category?
*
* @return boolean
*
* @since 3.6.1
*/
private function canCreateCategory()
{
return Factory::getUser()->authorise('core.create', 'com_content');
}
/**
* Delete #__content_frontpage items if the deleted articles was featured
*
* @param object $pks The primary key related to the contents that was deleted.
*
* @return boolean
*
* @since 3.7.0
*/
public function delete(&$pks)
{
$return = parent::delete($pks);
if ($return)
{
// Now check to see if this articles was featured if so delete it from the #__content_frontpage table
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete($db->quoteName('#__content_frontpage'))
->where('content_id IN (' . implode(',', $pks) . ')');
$db->setQuery($query);
$db->execute();
$workflow = new Workflow(['extension' => 'com_content']);
$workflow->deleteAssociation($pks);
}
return $return;
}
/**
* Load the assigned workflow information by a given category ID
*
* @param int $catId The give category
*
* @return integer|boolean If found, the workflow ID, otherwise false
*/
protected function getWorkflowByCategory($catId)
{
$db = $this->getDbo();
// Search categories and parents (if requested) for a workflow
$category = new Category($db);
$categories = array_reverse($category->getPath($catId));
$workflow_id = 0;
foreach ($categories as $cat)
{
$cat->params = new Registry($cat->params);
$workflow_id = $cat->params->get('workflow_id');
if ($workflow_id == 'inherit')
{
$workflow_id = 0;
continue;
}
elseif ($workflow_id == 'use_default')
{
$workflow_id = 0;
break;
}
elseif ($workflow_id > 0)
{
break;
}
}
// Check if the workflow exists
if ($workflow_id > 0)
{
$query = $db->getQuery(true);
$query ->select(
$db->quoteName(
[
'w.id',
'ws.condition'
]
)
)
->select($db->quoteName('ws.id', 'stage_id'))
->from($db->quoteName('#__workflow_stages', 'ws'))
->from($db->quoteName('#__workflows', 'w'))
->where($db->quoteName('ws.workflow_id') . ' = ' . $db->quoteName('w.id'))
->where($db->quoteName('ws.default') . ' = 1')
->where($db->quoteName('w.published') . ' = 1')
->where($db->quoteName('ws.published') . ' = 1')
->where($db->quoteName('w.id') . ' = ' . (int) $workflow_id);
$workflow = $db->setQuery($query)->loadObject();
if (!empty($workflow->id))
{
return $workflow;
}
}
// Use default workflow
$query = $db->getQuery(true);
$query ->select(
$db->quoteName(
[
'w.id',
'ws.condition'
]
)
)
->select($db->quoteName('ws.id', 'stage_id'))
->from($db->quoteName('#__workflow_stages', 'ws'))
->from($db->quoteName('#__workflows', 'w'))
->where($db->quoteName('ws.default') . ' = 1')
->where($db->quoteName('ws.workflow_id') . ' = ' . $db->quoteName('w.id'))
->where($db->quoteName('w.published') . ' = 1')
->where($db->quoteName('ws.published') . ' = 1')
->where($db->quoteName('w.default') . ' = 1');
$workflow = $db->setQuery($query)->loadObject();
// Last check if we have a workflow ID
if (!empty($workflow->id))
{
return $workflow;
}
return false;
}
/**
* Runs transition for item.
*
* @param integer $pk Id of article
* @param integer $transition_id Id of transition
*
* @return boolean
*
* @since 4.0.0
*/
public function runTransition($pk, $transition_id)
{
$workflow = new Workflow(['extension' => 'com_content']);
$runTransaction = $workflow->executeTransition($pk, $transition_id);
if (!$runTransaction)
{
$this->setError(Text::_('COM_CONTENT_ERROR_UPDATE_STAGE'));
return false;
}
// B/C state change trigger for UCM
$context = $this->option . '.' . $this->name;
// Include the plugins for the change of stage event.
\JPluginHelper::importPlugin($this->events_map['change_state']);
// Trigger the change stage event.
Factory::getApplication()->triggerEvent($this->event_change_state, [$context, [$pk], $transition_id]);
return true;
}
}
No other release has this exact content. See every version of this file.