Source for file module.php
Documentation is available at module.php
* @package Joomla.Administrator
* @subpackage com_modules
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @package Joomla.Administrator
* @subpackage com_modules
* @var string The prefix to use with controller messages.
* @var string The help screen key for the module.
protected $helpKey =
'JHELP_EXTENSIONS_MODULE_MANAGER_EDIT';
* @var string The help screen base URL for the module.
* Method to auto-populate the model state.
* Note. Calling getState in this method will result in recursion.
$pk =
$app->input->getInt('id');
if ($extensionId = (int)
$app->getUserState('com_modules.add.module.extension_id'))
$this->setState('extension.id', $extensionId);
* Method to perform batch operations on a set of modules.
* @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.
if (!empty($commands['position_id']))
if (!empty($commands['position_id']))
$result =
$this->batchCopy($commands['position_id'], $pks, $contexts);
elseif ($cmd ==
'm' &&
!$this->batchMove($commands['position_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))
$this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
* Batch copy modules to a new position or current.
* @param integer $value The new value matching a module position.
* @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 batchCopy($value, $pks, $contexts)
if ($user->authorise('core.create', 'com_modules'))
if ($value ==
'noposition')
elseif ($value ==
'nochange')
$position =
$table->position;
$table->position =
$position;
// Alter the title if necessary
$table->title =
$data['0'];
// Reset the ID because we are making a copy
// Unpublish the new module
$newId =
$table->get('id');
// Add the new ID to the array
// Now we need to handle the module assignments
$query =
$db->getQuery(true)
->select($db->quoteName('menuid'))
->from($db->quoteName('#__modules_menu'))
->where($db->quoteName('moduleid') .
' = ' .
$pk);
$menus =
$db->loadColumn();
// Insert the new records into the table
foreach ($menus as $menu)
->insert($db->quoteName('#__modules_menu'))
->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')))
->values($newId .
', ' .
$menu);
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
* Batch move modules to a new position or current.
* @param integer $value The new value matching a module position.
* @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 batchMove($value, $pks, $contexts)
if ($user->authorise('core.edit', 'com_modules'))
if ($value ==
'noposition')
elseif ($value ==
'nochange')
$position =
$table->position;
$table->position =
$position;
// Alter the title if necessary
$table->title =
$data['0'];
// Unpublish the moved module
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
* 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 module.
return $user->authorise('core.edit.state', 'com_modules.module.' . (int)
$record->id);
// Default to component settings if module not known.
return parent::canEditState('com_modules');
* @param array &$pks An array of item ids.
* @return boolean Returns true on success, false on failure.
// Iterate the items to delete each one.
if (!$user->authorise('core.delete', 'com_modules.module.'.(int)
$pk) ||
$table->published != -
2)
if (!$table->delete($pk))
throw
new Exception($table->getError());
// Delete the menu assignments
$query =
$db->getQuery(true)
->delete('#__modules_menu')
->where('moduleid=' . (int)
$pk);
parent::cleanCache($table->module, $table->client_id);
throw
new Exception($table->getError());
* Method to duplicate modules.
* @param array &$pks An array of primary key IDs.
* @return boolean True if successful.
if (!$user->authorise('core.create', 'com_modules'))
throw
new Exception(JText::_('JERROR_CORE_CREATE_NOT_PERMITTED'));
if ($table->load($pk, true))
// Reset the id to create a new record.
if (preg_match('#\((\d+)\)$#', $table->title, $m))
$table->title =
preg_replace('#\(\d+\)$#', '(' .
($m[1] +
1) .
')', $table->title);
// Unpublish duplicate module
if (!$table->check() ||
!$table->store())
throw
new Exception($table->getError());
// $query = 'SELECT menuid'
// . ' FROM #__modules_menu'
// . ' WHERE moduleid = ' . (int) $pk
$query =
$db->getQuery(true)
->from('#__modules_menu')
->where('moduleid=' . (int)
$pk);
$this->_db->setQuery($query);
$rows =
$this->_db->loadColumn();
foreach ($rows as $menuid)
$tuples[] =
'(' . (int)
$table->id .
',' . (int)
$menuid .
')';
throw
new Exception($table->getError());
// Module-Menu Mapping: Do it in one query
$query =
'INSERT INTO #__modules_menu (moduleid,menuid) VALUES ' .
implode(',', $tuples);
$this->_db->setQuery($query);
catch
(RuntimeException $e)
* Method to change the title.
* @param integer $category_id The id of the category. Not used here.
* @param string $title The title.
* @param string $position The position.
* @return array Contains the modified title.
// Alter the title & alias
while ($table->load(array('position' =>
$position, 'title' =>
$title)))
* Method to get the client object
* 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 JForm A JForm object on success, false on failure
public function getForm($data =
array(), $loadData =
true)
// The folder and element vars are passed when saving the form.
$clientId =
$item->client_id;
// These variables are used to add data from the plugin XML files.
$this->setState('item.client_id', $clientId);
$this->setState('item.module', $module);
$form =
$this->loadForm('com_modules.module', 'module', array('control' =>
'jform', 'load_data' =>
$loadData));
$form->setFieldAttribute('position', 'client', $this->getState('item.client_id') ==
0 ?
'site' :
'administrator');
// Check for existing module
// Modify the form based on Edit State access controls.
if ($id !=
0 &&
(!$user->authorise('core.edit.state', 'com_modules.module.'.(int)
$id))
||
($id ==
0 &&
!$user->authorise('core.edit.state', 'com_modules'))
// Disable fields for display.
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('published', 'disabled', 'true');
$form->setFieldAttribute('publish_up', 'disabled', 'true');
$form->setFieldAttribute('publish_down', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is a record you can edit.
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('published', 'filter', 'unset');
$form->setFieldAttribute('publish_up', 'filter', 'unset');
$form->setFieldAttribute('publish_down', '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.
// This allows us to inject parameter settings into a new module.
$params =
$app->getUserState('com_modules.add.module.params');
$data->set('params', $params);
* 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)
$pk =
(!empty($pk)) ? (int)
$pk : (int)
$this->getState('module.id');
if (!isset
($this->_cache[$pk]))
// Attempt to load the row.
$return =
$table->load($pk);
// Check for a table object error.
if ($return ===
false &&
$error =
$table->getError())
// Check if we are creating a new extension.
if ($extensionId = (int)
$this->getState('extension.id'))
$query =
$db->getQuery(true)
->select('element, client_id')
->where('extension_id = ' .
$extensionId)
->where('type = ' .
$db->quote('module'));
$extension =
$db->loadObject();
catch
(RuntimeException $e)
$this->setError('COM_MODULES_ERROR_CANNOT_FIND_MODULE');
// Extension found, prime some module values.
$table->module =
$extension->element;
$table->client_id =
$extension->client_id;
$app->redirect(JRoute::_('index.php?option=com_modules&view=modules', false));
// Convert to the JObject before adding other data.
$properties =
$table->getProperties(1);
// Convert the params field to an array.
$registry->loadString($table->params);
$this->_cache[$pk]->params =
$registry->toArray();
// Determine the page assignment mode.
' FROM #__modules_menu' .
' WHERE moduleid = ' .
$pk
$assigned =
$db->loadColumn();
// If this is a new module, assign to all pages.
elseif (empty($assigned))
// For an existing module it is assigned to none.
elseif ($assigned[0] <
0)
$this->_cache[$pk]->assigned =
$assigned;
$this->_cache[$pk]->assignment =
$assignment;
$path =
JPath::clean($client->path .
'/modules/' .
$table->module .
'/' .
$table->module .
'.xml');
$this->_cache[$pk]->xml =
null;
return $this->_cache[$pk];
* Get the necessary data to load an item help screen.
* @return object An object with key, url, and local properties for loading the item help screen.
return (object)
array('key' =>
$this->helpKey, 'url' =>
$this->helpURL);
* Returns a reference to the a Table object, always creating it.
* @param string $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 =
'Module', $prefix =
'JTable', $config =
array())
* Prepare and sanitise the table prior to saving.
* @param JTable $table The database object
$table->position =
trim($table->position);
* Method to preprocess the form
* @param JForm $form A form object.
* @param mixed $data The data expected for the form.
* @param string $group The name of the plugin group to import (defaults to "content").
* @throws Exception if there is an error loading the form.
protected function preprocessForm(JForm $form, $data, $group =
'content')
$clientId =
$this->getState('item.client_id');
$module =
$this->getState('item.module');
$formFile =
JPath::clean($client->path .
'/modules/' .
$module .
'/' .
$module .
'.xml');
// Load the core and/or local language file(s).
$lang->load($module, $client->path, null, false, true)
||
$lang->load($module, $client->path .
'/modules/' .
$module, null, false, true);
if (!$form->loadFile($formFile, false, '//config'))
throw
new Exception(JText::_('JERROR_LOADFILE_FAILED'));
// Attempt to load the xml file.
throw
new Exception(JText::_('JERROR_LOADFILE_FAILED'));
// Get the help data from the XML file if present.
$help =
$xml->xpath('/extension/help');
$helpKey =
trim((string)
$help[0]['key']);
$helpURL =
trim((string)
$help[0]['url']);
// Load the default advanced params
$form->loadFile('advanced', false);
// Trigger the default form events.
parent::preprocessForm($form, $data, $group);
* Loads ContentHelper for filters before validating data.
* @param object $form The form to validate against.
* @param array $data The data to validate.
* @param string $group The name of the group(defaults to null).
* @return mixed Array of filtered data if valid, false otherwise.
public function validate($form, $data, $group =
null)
return parent::validate($form, $data, $group);
* Method to save the form data.
* @param array $data The form data.
* @return boolean True on success.
public function save($data)
$pk =
(!empty($data['id'])) ?
$data['id'] : (int)
$this->getState('module.id');
// Include the content modules for the onSave events.
// Load the row if saving an existing record.
// Alter the title and published state for Save as Copy
if ($input->get('task') ==
'save2copy')
$orig_data =
$input->post->get('jform', array(), 'array');
$orig_table->load((int)
$orig_data['id']);
if ($data['title'] ==
$orig_table->title)
$data['title'] .=
' ' .
JText::_('JGLOBAL_COPY');
if (!$table->bind($data))
// Prepare the row for saving
// Trigger the onExtensionBeforeSave event.
$result =
$dispatcher->trigger('onExtensionBeforeSave', array('com_modules.module', &$table, $isNew));
// Process the menu link mappings.
$assignment = isset
($data['assignment']) ?
$data['assignment'] :
0;
// Delete old module to menu item associations
$query =
$db->getQuery(true)
->delete('#__modules_menu')
->where('moduleid = ' . (int)
$table->id);
catch
(RuntimeException $e)
// If the assignment is numeric, then something is selected (otherwise it's none).
// Variable is numeric, but could be a string.
$assignment = (int)
$assignment;
// Logic check: if no module excluded then convert to display on all.
if ($assignment == -
1 &&
empty($data['assigned']))
// Check needed to stop a module being assigned to `All`
// and other menu items resulting in a module being displayed twice.
// Assign new module to `all` menu item associations.
->insert('#__modules_menu')
->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')))
->values((int)
$table->id .
', 0');
catch
(RuntimeException $e)
elseif (!empty($data['assigned']))
// Get the sign of the number.
$sign =
$assignment <
0 ? -
1 :
1;
// Preprocess the assigned array.
foreach ($data['assigned'] as &$pk)
$tuples[] =
'(' . (int)
$table->id .
',' . (int)
$pk *
$sign .
')';
'INSERT INTO #__modules_menu (moduleid, menuid) VALUES ' .
catch
(RuntimeException $e)
// Trigger the onExtensionAfterSave event.
$dispatcher->trigger('onExtensionAfterSave', array('com_modules.module', &$table, $isNew));
// Compute the extension id of this module in case the controller wants it.
$query =
$db->getQuery(true)
->from('#__extensions AS e')
->join('LEFT', '#__modules AS m ON e.element = m.module')
->where('m.id = ' . (int)
$table->id);
$extensionId =
$db->loadResult();
catch
(RuntimeException $e)
$this->setState('module.extension_id', $extensionId);
$this->setState('module.id', $table->id);
parent::cleanCache($table->module, $table->client_id);
* 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 add to ordering queries.
$condition[] =
'client_id = ' . (int)
$table->client_id;
$condition[] =
'position = ' .
$this->_db->quote($table->position);
* Custom clean cache method for different clients
* @param string $group The name of the plugin group to import (defaults to null).
* @param integer $client_id The client ID. [optional]
protected function cleanCache($group =
null, $client_id =
0)
parent::cleanCache('com_modules', $this->getClient());
Documentation generated on Tue, 19 Nov 2013 15:08:47 +0100 by phpDocumentor 1.4.3