Source for file contact.php
Documentation is available at contact.php
* @package Joomla.Administrator
* @subpackage com_contact
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Item Model for a Contact.
* @package Joomla.Administrator
* @subpackage com_contact
* The type alias for this content type.
* Method to perform batch operations on an item or a set of items.
* @param array $commands An array of commands to perform.
* @param array $pks An array of item ids.
* @param array $contexts An array of item contexts.
* @return boolean Returns true on success, false on failure.
public function batch($commands, $pks, $contexts)
// Remove any values of zero.
// Set some needed variables.
$this->tableClassName =
get_class($this->table);
$this->type =
$this->contentType->getTypeByTable($this->tableClassName);
if ($this->type ===
false)
$this->type =
$type->getTypeByAlias($this->typeAlias);
$typeAlias =
$this->type->type_alias;
$typeAlias =
$this->type->type_alias;
$this->tagsObserver =
$this->table->getObserverOfClass('JTableObserverTags');
if (!empty($commands['category_id']))
$result =
$this->batchCopy($commands['category_id'], $pks, $contexts);
elseif ($cmd ==
'm' &&
!$this->batchMove($commands['category_id'], $pks, $contexts))
if (!empty($commands['assetgroup_id']))
if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts))
if (!empty($commands['language_id']))
if (!$this->batchLanguage($commands['language_id'], $pks, $contexts))
if (!empty($commands['tag']))
if (!$this->batchTag($commands['tag'], $pks, $contexts))
if (strlen($commands['user_id']) >
0)
if (!$this->batchUser($commands['user_id'], $pks, $contexts))
$this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
* 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 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->name);
$this->table->name =
$data['0'];
$this->table->alias =
$data['1'];
// Reset the ID because we are making a copy
$this->table->catid =
$categoryId;
// TODO: Deal with ordering?
//$this->table->ordering = 1;
if (!$this->table->check())
$this->setError($this->table->getError());
parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
if (!$this->table->store())
$this->setError($this->table->getError());
$newId =
$this->table->get('id');
// Add the new ID to the array
* Batch change a linked user.
* @param integer $value The new value matching a User ID.
* @param array $pks An array of row IDs.
* @param array $contexts An array of item contexts.
* @return boolean True if successful, false otherwise and internal error is set.
protected function batchUser($value, $pks, $contexts)
if ($this->user->authorise('core.edit', $contexts[$pk]))
$this->table->user_id = (int)
$value;
static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
if (!$this->table->store())
$this->this->setError($table->getError());
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
* 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->published != -
2)
return $user->authorise('core.delete', 'com_contact.category.' . (int)
$record->catid);
* 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 against the category.
if (!empty($record->catid))
return $user->authorise('core.edit.state', 'com_contact.category.' . (int)
$record->catid);
// Default to component settings if category not known.
return parent::canEditState($record);
* Returns a Table object, always creating it
* @param type $type The table type to instantiate
* @param string $prefix A prefix for the table class name. Optional.
* @param array $config Configuration array for model. Optional.
* @return JTable A database object
public function getTable($type =
'Contact', $prefix =
'ContactTable', $config =
array())
* Method to get the row 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_contact.contact', 'contact', array('control' =>
'jform', 'load_data' =>
$loadData));
// Modify the form based on access controls.
// Disable fields for display.
$form->setFieldAttribute('featured', 'disabled', 'true');
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('published', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is a record you can edit.
$form->setFieldAttribute('featured', 'filter', 'unset');
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('published', 'filter', 'unset');
* 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 metadata field to an array.
$registry->loadString($item->metadata);
$item->metadata =
$registry->toArray();
// Load associated contact items
$item->associations =
array();
foreach ($associations as $tag =>
$association)
$item->associations[$tag] =
$association->id;
$item->tags->getTagIds($item->id, 'com_contact.contact');
* 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.
// Prime some default values.
$data->set('catid', $app->input->get('catid', $app->getUserState('com_contact.contacts.filter.category_id'), 'int'));
* Method to save the form data.
* @param array The form data.
* @return boolean True on success.
public function save($data)
// Alter the title for save as copy
if ($app->input->get('task') ==
'save2copy')
list
($name, $alias) =
$this->generateNewTitle($data['catid'], $data['alias'], $data['name']);
$links =
array('linka', 'linkb', 'linkc', 'linkd', 'linke');
foreach ($links as $link)
if ($data['params'][$link])
// 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_contact.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_contact.item') .
',' .
$db->quote($key));
if ($error =
$db->getErrorMsg())
* Prepare and sanitise the table prior to saving.
if (empty($table->alias))
$table->created =
$date->toSql();
// Set ordering to the last item if not set
if (empty($table->ordering))
$query =
$db->getQuery(true);
$query->select('MAX(ordering)');
$query->from('#__contact_details');
$max =
$db->loadResult();
$table->ordering =
$max +
1;
$table->modified =
$date->toSql();
$table->modified_by =
$user->get('id');
// Increment the content version number.
* A protected method to get a set of ordering conditions.
* @param JTable $table A record object.
* @return array An array of conditions to add to add to ordering queries.
$condition[] =
'catid = ' . (int)
$table->catid;
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_CONTACT_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_contact');
$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);
* Method to toggle the featured setting of contacts.
* @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)
$query =
$db->getQuery(true);
$query->update('#__contact_details');
$query->set('featured = ' . (int)
$value);
$query->where('id IN (' .
implode(',', $pks) .
')');
// Clean component's cache
* Method to change the title & alias.
* @param integer $parent_id The id of the parent.
* @param string $alias The alias.
* @param string $title The title.
* @return array Contains the modified title and alias.
// Alter the title & alias
while ($table->load(array('alias' =>
$alias, 'catid' =>
$category_id)))
if ($name ==
$table->name)
return array($name, $alias);
Documentation generated on Tue, 19 Nov 2013 14:56:40 +0100 by phpDocumentor 1.4.3