Source for file template.php
Documentation is available at template.php
* @package Joomla.Administrator
* @subpackage com_templates
* @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_templates
* Internal method to get file properties.
* @param string $path The base path.
* @param string $name The file name.
protected function getFile($path, $name)
* Method to get a list of all the files to edit in a template.
* @return array A nested array of relevant files.
jimport('joomla.filesystem.folder');
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
// Load the core and/or local language file(s).
$lang->load('tpl_' .
$template->element, $client->path, null, false, true) ||
$lang->load('tpl_' .
$template->element, $client->path .
'/templates/' .
$template->element, null, false, true);
$app->enqueueMessage(JText::_('COM_TEMPLATES_DIRECTORY_NOT_WRITABLE'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_TEMPLATE_FOLDER_NOT_FOUND'), 'error');
* Get the directory tree.
* @param string $dir The path of the directory to scan
foreach ($dirFiles as $key =>
$value)
if (!in_array($value, array(".", "..")))
if (is_dir($dir .
$value .
'/'))
$ext =
pathinfo($dir .
$value, PATHINFO_EXTENSION);
$imageTypes =
explode(',', $params->get('image_formats'));
$sourceTypes =
explode(',', $params->get('source_formats'));
$fontTypes =
explode(',', $params->get('font_formats'));
$archiveTypes =
explode(',', $params->get('compressed_formats'));
$types =
array_merge($imageTypes, $sourceTypes, $fontTypes, $archiveTypes);
$info =
$this->getFile('/' .
$relativePath, $value);
* Method to auto-populate the model state.
* Note. Calling getState in this method will result in recursion.
$pk =
$app->input->getInt('id');
* Method to get the template information.
* @return mixed Object if successful, false if not and internal error is set.
// Get the template information.
$query =
$db->getQuery(true)
->select('extension_id, client_id, element, name')
->where($db->quoteName('extension_id') .
' = ' . (int)
$pk)
->where($db->quoteName('type') .
' = ' .
$db->quote('template'));
$result =
$db->loadObject();
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'warning');
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'), 'error');
* Method to check if new template name already exists
* @return boolean true if name is not used, false otherwise
$query =
$db->getQuery(true)
->where('name = ' .
$db->quote($this->getState('new_name')));
return ($db->loadResult() ==
0);
* Method to check if new template name already exists
* @return string name of current template
* Method to check if new template name already exists
* @return boolean true if name is not used, false otherwise
jimport('joomla.filesystem.folder');
$fromPath =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
// Delete new folder if it exists
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
// Copy all files from $fromName template to $newName folder
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
* Method to delete tmp folder
* @return boolean true if delete successful, false otherwise
// Clear installation messages
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
// Delete temporary directory
* Method to rename the template in the XML files and rename the language files
* @return boolean true if successful, false otherwise
// Get list of language files
foreach ($files as $file)
$result =
JFile::move($file, $newFile) &&
$result;
$xmlFile =
$this->getState('to_path') .
'/templateDetails.xml';
$pattern[] =
'#<name>\s*' .
$oldName .
'\s*</name>#i';
$replace[] =
'<name>' .
$newName .
'</name>';
$pattern[] =
'#<language(.*)' .
$oldName .
'(.*)</language>#';
$replace[] =
'<language${1}' .
$newName .
'${2}</language>';
$result =
JFile::write($xmlFile, $contents) &&
$result;
* 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)
// Codemirror or Editor None should be enabled
$query =
$db->getQuery(true)
->from('#__extensions as a')
'(a.name =' .
$db->quote('plg_editors_codemirror') .
' AND a.enabled = 1) OR (a.name =' .
$db->quote('plg_editors_none') .
$state =
$db->loadResult();
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning');
$form =
$this->loadForm('com_templates.source', 'source', array('control' =>
'jform', 'load_data' =>
$loadData));
* Method to get the data that should be injected in the form.
* @return mixed The data for the form.
* Method to get a single record.
* @return mixed Object on success, false on failure.
$filePath =
JPath::clean($client->path .
'/templates/' .
$this->template->element .
'/' .
$fileName);
$item->extension_id =
$this->getState('extension.id');
$item->filename =
$fileName;
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'), 'error');
* Method to store the source file contents.
* @param array $data The source data to save.
* @return boolean True on success, false otherwise and internal error set.
public function save($data)
$filePath =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$fileName);
// Include the extension plugins for the save events.
// Try to make the template file writable.
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE'), 'warning');
$app->enqueueMessage(JText::_('COM_TEMPLATES_CHECK_FILE_OWNERSHIP'), 'warning');
$return =
JFile::write($filePath, $data['source']);
// Try to make the template file unwritable.
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_UNWRITABLE'), 'error');
$app->enqueueMessage(JText::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName), 'error');
$explodeArray =
explode('.', $fileName);
$ext =
end($explodeArray);
$app->enqueueMessage(JText::sprintf('COM_TEMPLATES_COMPILE_LESS', $fileName));
* @param string $name The name of override.
* @param string $path Location of override.
* @return object containing override name and path.
* Get a list of overrides.
* @return array containing overrides.
$componentPath =
JPath::clean($client->path .
'/components/');
$modulePath =
JPath::clean($client->path .
'/modules/');
foreach ($components as $component)
$viewPath =
JPath::clean($componentPath .
'/' .
$component .
'/views/');
foreach ($views as $view)
foreach ($modules as $module)
foreach ($layouts as $layout)
* @param string $override The override location.
* @return boolean true if override creation is successful, false otherwise
jimport('joomla.filesystem.folder');
$explodeArray =
explode(DIRECTORY_SEPARATOR, $override);
$name =
end($explodeArray);
if (stristr($name, 'mod_') !=
false)
$htmlPath =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/html/' .
$name);
elseif (stristr($override, 'com_') !=
false)
$folderExplode =
explode(DIRECTORY_SEPARATOR, $override);
$size =
count($folderExplode);
$url =
JPath::clean($folderExplode[$size -
3] .
'/' .
$folderExplode[$size -
1]);
$htmlPath =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/html/' .
$url);
$htmlPath =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/html/layouts/joomla/' .
$name);
$app->enqueueMessage(JText::_('COM_TEMPLATES_OVERRIDE_EXISTS'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_ERROR'), 'error');
if (stristr($name, 'mod_') !=
false)
$return =
JFolder::copy($override .
'/tmpl', $htmlPath, '', true);
elseif (stristr($override, 'com_') !=
false)
$return =
JFolder::copy($override .
'/tmpl', $htmlPath .
'/', '', true);
$return =
JFolder::copy($override, $htmlPath, '', true);
$app->enqueueMessage(JText::_('COM_TEMPLATES_OVERRIDE_FAILED'), 'error');
* Compile less using the less compiler under /build.
* @param string $input The relative location of the less file.
* @return boolean true if compilation is successful, false otherwise
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$explodeArray =
explode('/', $inFile);
$fileName =
end($explodeArray);
// Load the RAD layer to use its LESS compiler
$less->compileFile($path .
$inFile, $path .
'css/' .
$outFile .
'.css');
$app->enqueueMessage($e->getMessage(), 'error');
* Delete a particular file.
* @param string $file The relative location of the file.
* @return boolean True if file deletion is successful, false otherwise
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_DELETE_FAIL'), 'error');
* @param string $name The name of file.
* @param string $type The extension of the file.
* @param string $location Location for the new file.
* @return boolean true if file created successfully, false otherwise
public function createFile($name, $type, $location)
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
if (!fopen(JPath::clean($path .
'/' .
$location .
'/' .
$name .
'.' .
$type), 'x'))
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_CREATE_ERROR'), 'error');
* @param string $file The name of the file.
* @param string $location Location for the new file.
* @return boolean True if file uploaded successfully, false otherwise
jimport('joomla.filesystem.folder');
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
JLoader::register('TemplateHelper', JPATH_COMPONENT_ADMINISTRATOR .
'/helpers/template.php');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_UPLOAD_ERROR'), 'error');
* @param string $name The name of the new folder.
* @param string $location Location for the new folder.
* @return boolean True if override folder is created successfully, false otherwise
jimport('joomla.filesystem.folder');
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_EXISTS'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_CREATE_ERROR'), 'error');
* @param string $location The name and location of the folder.
* @return boolean True if override folder is deleted successfully, false otherwise
jimport('joomla.filesystem.folder');
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$location);
$app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_NOT_EXISTS'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_DELETE_ERROR'), 'error');
* @param string $file The name and location of the old file
* @param string $name The new name of the file.
* @return string Encoded string containing the new file location.
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$explodeArray =
explode('.', $fileName);
$type =
end($explodeArray);
$explodeArray =
explode('/', $fileName);
$newName =
str_replace(end($explodeArray), $name .
'.' .
$type, $fileName);
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
if (!rename($path .
$fileName, $path .
$newName))
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_RENAME_ERROR'), 'error');
* Get an image address, height and width.
* @return array an associative array containing image address, height and width.
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
if (stristr($client->path, 'administrator') ==
false)
$folder =
'/administrator/templates/';
$uri =
JUri::root(true) .
$folder .
$template->element;
$image['address'] =
$uri .
$fileName;
$image['path'] =
$fileName;
$image['height'] =
$JImage->getHeight();
$image['width'] =
$JImage->getWidth();
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_IMAGE_FILE_NOT_FOUND'), 'error');
* @param string $file The name and location of the file
* @param string $w width.
* @param string $h height.
* @param string $x x-coordinate.
* @param string $y y-coordinate.
* @return boolean true if image cropped successfully, false otherwise.
public function cropImage($file, $w, $h, $x, $y)
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$relPath);
$image =
$JImage->crop($w, $h, $x, $y, true);
$app->enqueueMessage($e->getMessage(), 'error');
* @param string $file The name and location of the file
* @param string $width The new width of the image.
* @param string $height The new height of the image.
* @return boolean true if image resize successful, false otherwise.
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$relPath);
$image =
$JImage->resize($width, $height, true, 1);
$app->enqueueMessage($e->getMessage(), 'error');
* @return object object containing the id of the template.
$query =
$db->getQuery(true);
$query->select('id, client_id');
$query->from('#__template_styles');
$query->where($db->quoteName('template') .
' = ' .
$db->quote($this->template->name));
$result =
$db->loadObject();
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'warning');
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'), 'warning');
* @return mixed array on success, false on failure
$explodeArray =
explode('/', $relPath);
$fileName =
end($explodeArray);
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$relPath);
if (stristr($client->path, 'administrator') ==
false)
$folder =
'/administrator/templates/';
$uri =
JUri::root(true) .
$folder .
$template->element;
$font['address'] =
$uri .
$relPath;
$font['rel_path'] =
$relPath;
$font['name'] =
$fileName;
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND'), 'error');
* Check the admin template.
* @return object object containing the id of the template.
$query =
$db->getQuery(true);
$query->from('#__template_styles');
$query->where($db->quoteName('template') .
' = ' .
$db->quote('hathor'));
$result =
$db->loadObject();
catch
(RuntimeException $e)
$app->enqueueMessage($e->getMessage(), 'error');
* @param string $newName The name of the copied file
* @param string $location The final location where the file is to be copied
* @param string $file The name and location of the file
* @return boolean true if image resize successful, false otherwise.
public function copyFile($newName, $location, $file)
$explodeArray =
explode('.', $relPath);
$ext =
end($explodeArray);
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/');
$newPath =
JPath::clean($path .
'/' .
$location .
'/' .
$newName .
'.' .
$ext);
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
$app->enqueueMessage(JText::sprintf('COM_TEMPLATES_FILE_COPY_SUCCESS', $newName .
'.' .
$ext));
* Get the compressed files.
* @return array if file exists, false otherwise
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$relPath);
if ($zip->open($path) ===
true)
for ($i =
0; $i <
$zip->numFiles; $i++
)
$entry =
$zip->getNameIndex($i);
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND'), 'error');
* Extract contents of a archive file.
* @param string $file The name and location of the file
* @return boolean true if image extraction is successful, false otherwise.
$explodeArray =
explode('/', $relPath);
$fileName =
end($explodeArray);
$folderPath =
stristr($relPath, $fileName, true);
$path =
JPath::clean($client->path .
'/templates/' .
$template->element .
'/' .
$folderPath .
'/');
if ($zip->open(JPath::clean($path .
'/' .
$fileName)) ===
true)
for ($i =
0; $i <
$zip->numFiles; $i++
)
$entry =
$zip->getNameIndex($i);
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_EXISTS'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL'), 'error');
$app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_NOT_FOUND'), 'error');
Documentation generated on Tue, 19 Nov 2013 15:15:27 +0100 by phpDocumentor 1.4.3