Source for file article.php
Documentation is available at article.php
* @package Joomla.Administrator
* @subpackage com_content
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
require_once JPATH_COMPONENT_ADMINISTRATOR .
'/helpers/content.php';
* Item Model for an Article.
* @package Joomla.Administrator
* @subpackage com_content
* @var string The prefix to use with controller messages.
* The type alias for this content type (for example, 'com_content.article').
* Batch copy items to a new category or current.
* @param integer $value The new category.
* @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.
protected function batchCopy($value, $pks, $contexts)
$categoryId = (int)
$value;
if (!parent::checkCategoryId($categoryId))
// Parent exists so we let's proceed
// Pop the first ID off the stack
// Check that the row actually exists
if (!$this->table->load($pk))
if ($error =
$this->table->getError())
// Alter the title & alias
$data =
$this->generateNewTitle($categoryId, $this->table->alias, $this->table->title);
$this->table->title =
$data['0'];
$this->table->alias =
$data['1'];
// Reset the ID because we are making a copy
$this->table->catid =
$categoryId;
// TODO: Deal with ordering?
// Get the featured state
$featured =
$this->table->featured;
if (!$this->table->check())
parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
if (!$this->table->store())
$newId =
$this->table->get('id');
// Add the new ID to the array
// Check if the article was featured and update the #__content_frontpage table
$query =
$db->getQuery(true)
->insert($db->quoteName('#__content_frontpage'))
->values($newId .
', 0');
* 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.
if ($record->state != -
2)
return $user->authorise('core.delete', 'com_content.article.' . (int)
$record->id);
* 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.
// Check for existing article.
return $user->authorise('core.edit.state', 'com_content.article.' . (int)
$record->id);
// New article, so check against the category.
elseif (!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('com_content');
* Prepare and sanitise the table data prior to saving.
* @param JTable A JTable object.
// Set the publish date to now
if ($table->state ==
1 && (int)
$table->publish_up ==
0)
if ($table->state ==
1 &&
intval($table->publish_down) ==
0)
$table->publish_down =
$db->getNullDate();
// Increment the content version number.
// Reorder the articles within the category so the new article is first
$table->reorder('catid = ' . (int)
$table->catid .
' AND state >= 0');
* Returns a Table object, always creating it.
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
public function getTable($type =
'Content', $prefix =
'JTable', $config =
array())
* Method to get a single record.
* @param integer 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->loadString($item->attribs);
$item->attribs =
$registry->toArray();
// Convert the metadata field to an array.
$registry->loadString($item->metadata);
$item->metadata =
$registry->toArray();
// Convert the images field to an array.
$registry->loadString($item->images);
$item->images =
$registry->toArray();
// Convert the urls field to an array.
$registry->loadString($item->urls);
$item->urls =
$registry->toArray();
$item->articletext =
trim($item->fulltext) !=
'' ?
$item->introtext .
"<hr id=\"system-readmore\" />" .
$item->fulltext :
$item->introtext;
$item->tags->getTagIds($item->id, 'com_content.article');
// Load associated content items
$item->associations =
array();
foreach ($associations as $tag =>
$association)
$item->associations[$tag] =
$association->id;
* 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 mixed A JForm object on success, false on failure
public function getForm($data =
array(), $loadData =
true)
$form =
$this->loadForm('com_content.article', 'article', array('control' =>
'jform', 'load_data' =>
$loadData));
// The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
if ($jinput->get('a_id'))
$id =
$jinput->get('a_id', 0);
// The back end uses id so we use that the rest of the time and set it to 0 by default.
$id =
$jinput->get('id', 0);
// Determine correct permissions to check.
// 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');
// New record. Can only create in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.create');
// 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
if ($app->isSite() &&
$assoc &&
$this->getState('article.id'))
$form->setFieldAttribute('language', 'readonly', 'true');
$form->setFieldAttribute('catid', 'readonly', 'true');
$form->setFieldAttribute('language', 'filter', 'unset');
$form->setFieldAttribute('catid', 'filter', 'unset');
* Method to get the data that should be injected in the form.
* @return mixed The data for the form.
// Check the session for previously entered form data.
$data =
$app->getUserState('com_content.edit.article.data', array());
// Prime some default values.
$data->set('catid', $app->input->getInt('catid', $app->getUserState('com_content.articles.filter.category_id')));
* Method to save the form data.
* @param array The form data.
* @return boolean True on success.
public function save($data)
if (isset
($data['images']) &&
is_array($data['images']))
$registry->loadArray($data['images']);
$data['images'] = (string)
$registry;
if (isset
($data['urls']) &&
is_array($data['urls']))
foreach ($data['urls'] as $i =>
$url)
if ($url !=
false &&
($i ==
'urla' ||
$i ==
'urlb' ||
$i ==
'urlc'))
$registry->loadArray($data['urls']);
$data['urls'] = (string)
$registry;
// Alter the title for save as copy
if ($app->input->get('task') ==
'save2copy')
list
($title, $alias) =
$this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
if (isset
($data['featured']))
// Adding self to the association
$associations =
$data['associations'];
foreach ($associations as $tag =>
$id)
unset
($associations[$tag]);
// Detecting all item menus
$all_language =
$item->language ==
'*';
if ($all_language &&
!empty($associations))
$associations[$item->language] =
$item->id;
// Deleting old association for these items
$query =
$db->getQuery(true)
->delete('#__associations')
->where('context=' .
$db->quote('com_content.item'))
->where('id IN (' .
implode(',', $associations) .
')');
if ($error =
$db->getErrorMsg())
if (!$all_language &&
count($associations))
// Adding new association for these items
->insert('#__associations');
foreach ($associations as $id)
$query->values($id .
',' .
$db->quote('com_content.item') .
',' .
$db->quote($key));
if ($error =
$db->getErrorMsg())
* Method to toggle the featured setting of articles.
* @param array The ids of the items to toggle.
* @param integer The value to toggle to.
* @return boolean True on success.
public function featured($pks, $value =
0)
$table =
$this->getTable('Featured', 'ContentTable');
$query =
$db->getQuery(true)
->update($db->quoteName('#__content'))
->set('featured = ' . (int)
$value)
->where('id IN (' .
implode(',', $pks) .
')');
// Adjust the mapping table.
// Clear the existing features settings.
$query =
$db->getQuery(true)
->delete($db->quoteName('#__content_frontpage'))
->where('content_id IN (' .
implode(',', $pks) .
')');
// first, we find out which of our new featured articles are already featured.
$query =
$db->getQuery(true)
->from('#__content_frontpage AS f')
->where('content_id IN (' .
implode(',', $pks) .
')');
$old_featured =
$db->loadColumn();
// we diff the arrays to get a list of the articles that are newly featured
foreach ($new_featured as $pk)
$columns =
array('content_id', 'ordering');
$query =
$db->getQuery(true)
->insert($db->quoteName('#__content_frontpage'))
->columns($db->quoteName($columns))
* A protected method to get a set of ordering conditions.
* @param object A record object.
* @return array An array of conditions to add to add to ordering queries.
$condition[] =
'catid = ' . (int)
$table->catid;
* Auto-populate the model state.
* Note. Calling getState in this method will result in recursion.
protected function preprocessForm(JForm $form, $data, $group =
'content')
// Association content items
// force to array (perhaps move to $this->loadFormData())
$addform =
new SimpleXMLElement('<form />');
$fields =
$addform->addChild('fields');
$fields->addAttribute('name', 'associations');
$fieldset =
$fields->addChild('fieldset');
$fieldset->addAttribute('name', 'item_associations');
$fieldset->addAttribute('description', 'COM_CONTENT_ITEM_ASSOCIATIONS_FIELDSET_DESC');
foreach ($languages as $tag =>
$language)
if (empty($data['language']) ||
$tag !=
$data['language'])
$field =
$fieldset->addChild('field');
$field->addAttribute('name', $tag);
$field->addAttribute('type', 'modal_article');
$field->addAttribute('language', $tag);
$field->addAttribute('label', $language->title);
$field->addAttribute('translate_label', 'false');
$field->addAttribute('edit', 'true');
$field->addAttribute('clear', 'true');
$form->load($addform, false);
parent::preprocessForm($form, $data, $group);
* Custom clean the cache of com_content and content modules
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');
Documentation generated on Tue, 19 Nov 2013 14:53:58 +0100 by phpDocumentor 1.4.3