Skip to content

Commit

Permalink
[4.1] Improve the child template creation (#36250)
Browse files Browse the repository at this point in the history
* styles

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Benjamin Trenkle <bembelimen@users.noreply.github.com>

* Freely select to also copy styles from parent or childs (of the same parent)

* cleanup

* Update administrator/components/com_templates/tmpl/template/default_modal_child_body.php

Co-authored-by: Quy <quy@fluxbb.org>

* update

Signed-off-by: dgrammatiko <d.grammatiko@gmail.com>

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Richard Fath <richard67@users.noreply.github.com>

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/tmpl/template/default_modal_child_body.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/language/en-GB/com_templates.ini

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/View/Template/HtmlView.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/Controller/TemplateController.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/Model/TemplateModel.php

Co-authored-by: Quy <quy@fluxbb.org>

* sprintf

* txt

* oops

* Update administrator/language/en-GB/com_templates.ini

Co-authored-by: Quy <quy@fluxbb.org>

* Update administrator/components/com_templates/src/Model/TemplateModel.php
  • Loading branch information
dgrammatiko authored Apr 4, 2022
1 parent 8d7536a commit 49b0fb5
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1008,10 +1008,12 @@ public function child()

$this->input->set('installtype', 'folder');
$newNameRaw = $this->input->get('new_name', null, 'string');

// Only accept letters, numbers and underscore for template name
$newName = preg_replace('/[^a-zA-Z0-9_]/', '', $newNameRaw);
$templateID = (int) $this->input->getInt('id', 0);
$file = (string) $this->input->get('file', '', 'cmd');
$newName = preg_replace('/[^a-zA-Z0-9_]/', '', $newNameRaw);
$templateID = (int) $this->input->getInt('id', 0);
$file = (string) $this->input->get('file', '', 'cmd');
$extraStyles = (array) $this->input->get('style_ids', [], 'array');

$this->setRedirect('index.php?option=com_templates&view=template&id=' . $templateID . '&file=' . $file);

Expand Down Expand Up @@ -1083,6 +1085,12 @@ public function child()
$this->setMessage(Text::sprintf('COM_TEMPLATES_CHILD_SUCCESS', $newName));
$model->cleanup();

if (\count($extraStyles) > 0)
{
$model->setState('stylesToCopy', $extraStyles);
$model->copyStyles();
}

return true;
}
}
107 changes: 100 additions & 7 deletions administrator/components/com_templates/src/Model/TemplateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Joomla\Component\Templates\Administrator\Helper\TemplateHelper;
use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
use Joomla\Database\ParameterType;
use Joomla\Utilities\ArrayHelper;

/**
* Template model class.
Expand Down Expand Up @@ -2045,7 +2046,6 @@ private function getBasePath()
JPATH_ROOT . '/' . ($this->template->client_id === 0 ? '' : 'administrator/') . 'templates/' . $this->template->element;
}


/**
* Method to create the templateDetails.xml for the child template
*
Expand Down Expand Up @@ -2169,6 +2169,8 @@ public function child()
$media->addChild('folder', 'css');
$media->addChild('folder', 'js');
$media->addChild('folder', 'images');
$media->addChild('folder', 'html');
$media->addChild('folder', 'scss');

$xml->name = $template->element . '_' . $newName;
$xml->inheritable = 0;
Expand All @@ -2189,24 +2191,115 @@ public function child()
}

// Create an empty media folder structure
if (!Folder::create($toPath . '/media'))
if (!Folder::create($toPath . '/media')
|| !Folder::create($toPath . '/media/css')
|| !Folder::create($toPath . '/media/js')
|| !Folder::create($toPath . '/media/images')
|| !Folder::create($toPath . '/media/html/tinymce')
|| !Folder::create($toPath . '/media/scss'))
{
return false;
}

if (!Folder::create($toPath . '/media/css'))
return true;
}

/**
* Method to get the parent template existing styles
*
* @return array array of id,titles of the styles
*
* @since __DEPLOY_VERSION__
*/
public function getAllTemplateStyles()
{
$template = $this->getTemplate();

if (!$template->xmldata->inheritable)
{
return false;
return [];
}

if (!Folder::create($toPath . '/media/js'))
$db = $this->getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName(['id', 'title']))
->from($db->quoteName('#__template_styles'))
->where($db->quoteName('client_id') . ' = :client_id', 'AND')
->where($db->quoteName('template') . ' = :template')
->orWhere($db->quoteName('parent') . ' = :parent')
->bind(':client_id', $template->client_id, ParameterType::INTEGER)
->bind(':template', $template->element)
->bind(':parent', $template->element);

$db->setQuery($query);

return $db->loadObjectList();
}

/**
* Method to copy selected styles to the child template
*
* @return boolean true if name is not used, false otherwise
*
* @since __DEPLOY_VERSION__
*/
public function copyStyles()
{
$app = Factory::getApplication();
$template = $this->getTemplate();
$newName = strtolower($this->getState('new_name'));
$applyStyles = $this->getState('stylesToCopy');

// Get a db connection.
$db = $this->getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query->select($db->quoteName(['title', 'params']))
->from($db->quoteName('#__template_styles'))
->whereIn($db->quoteName('id'), ArrayHelper::toInteger($applyStyles));
// Reset the query using our newly populated query object.
$db->setQuery($query);

try
{
$parentStyle = $db->loadObjectList();
}
catch (\Exception $e)
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_STYLE_NOT_FOUND'), 'error');

return false;
}

if (!Folder::create($toPath . '/media/images'))
foreach ($parentStyle as $style)
{
return false;
$query = $db->getQuery(true);
$styleName = Text::sprintf('COM_TEMPLATES_COPY_CHILD_TEMPLATE_STYLES', ucfirst($template->element . '_' . $newName), $style->title);

// Insert columns and values
$columns = ['id', 'template', 'client_id', 'home', 'title', 'inheritable', 'parent', 'params'];
$values = [0, $db->quote($template->element . '_' . $newName), (int) $template->client_id, $db->quote('0'), $db->quote($styleName), 0, $db->quote($template->element), $db->quote($style->params)];

$query
->insert($db->quoteName('#__template_styles'))
->columns($db->quoteName($columns))
->values(implode(',', $values));

$db->setQuery($query);

try
{
$db->execute();
}
catch (\Exception $e)
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_READ'), 'error');

return false;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function display($tpl = null)
$this->preview = $this->get('Preview');
$this->pluginState = PluginHelper::isEnabled('installer', 'override');
$this->updatedList = $this->get('UpdatedList');
$this->styles = $this->get('AllTemplateStyles');
$this->stylesHTML = '';

$params = ComponentHelper::getParams('com_templates');
$imageTypes = explode(',', $params->get('image_formats'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,52 @@

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;

Factory::getDocument()->getWebAssetManager()->usePreset('choicesjs');

// Generate a list of styles for the child creation modal
$options = [];

if (count($this->styles) > 0)
{
foreach ($this->styles as $style)
{
$options[] = HTMLHelper::_('select.option', $style->id, $style->title, 'value', 'text');
}
}

$fancySelectData = [
'autocomplete' => 'off',
'autofocus' => false,
'class' => '',
'description' => '',
'disabled' => false,
'group' => false,
'id' => 'style_ids',
'hidden' => false,
'hint' => '',
'label' => '',
'labelclass' => '',
'onchange' => '',
'onclick' => '',
'multiple' => true,
'pattern' => '',
'readonly' => false,
'repeat' => false,
'required' => false,
'size' => 4,
'spellcheck' => false,
'validate' => '',
'value' => '0',
'options' => $options,
'dataAttributes' => [],
'dataAttribute' => '',
'name' => 'style_ids[]',
];
?>
<div id="template-manager-copy" class="container-fluid">
<div class="mt-2">
Expand All @@ -28,6 +72,19 @@
</small>
</div>
</div>
<div class="control-group">
<div class="control-label">
<label for="style_ids">
<?php echo Text::_('COM_TEMPLATES_TEMPLATE_CHILD_STYLE_LABEL'); ?>
</label>
</div>
<div class="controls">
<?php echo LayoutHelper::render('joomla.form.field.list-fancy-select', $fancySelectData); ?>
<small class="form-text">
<?php echo Text::_('COM_TEMPLATES_TEMPLATE_NEW_STYLE_DESC'); ?>
</small>
</div>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions administrator/language/en-GB/com_templates.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ COM_TEMPLATES_CONFIG_POSITIONS_LABEL="Preview Module Positions"
COM_TEMPLATES_CONFIG_SOURCE_LABEL="Valid Source Formats"
COM_TEMPLATES_CONFIG_UPLOAD_LABEL="Max. Upload Size (MB)"
COM_TEMPLATES_CONFIGURATION="Template: Options"
COM_TEMPLATES_COPY_CHILD_TEMPLATE_STYLES="%s, copy of %s"
COM_TEMPLATES_COPY_SUCCESS="New template called %s was installed."
COM_TEMPLATES_CROP_AREA_ERROR="Crop area not selected."
COM_TEMPLATES_DIRECTORY_NOT_WRITABLE="The template folder is not writable. Some features may not work."
Expand Down Expand Up @@ -218,6 +219,8 @@ COM_TEMPLATES_TAB_OVERRIDES="Create Overrides"
COM_TEMPLATES_TAB_UPDATED_FILES="Updated Files"
COM_TEMPLATES_TEMPLATE_CHILD="Child Template"
COM_TEMPLATES_TEMPLATE_CHILD_NAME_LABEL="Child Template Name"
COM_TEMPLATES_TEMPLATE_CHILD_STYLE_LABEL="Additional Template Styles"
COM_TEMPLATES_TEMPLATE_NEW_STYLE_DESC="Duplicate existing styles for the new child template."
COM_TEMPLATES_TEMPLATE_CLOSE="Close"
COM_TEMPLATES_TEMPLATE_COPY="Copy Template"
COM_TEMPLATES_TEMPLATE_CORE_FILENAME="Original file &quot;%s&quot;."
Expand Down

0 comments on commit 49b0fb5

Please sign in to comment.