Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.1] Improve the child template creation #161

Closed
joomlapl-bot opened this issue Apr 4, 2022 · 0 comments
Closed

[4.1] Improve the child template creation #161

joomlapl-bot opened this issue Apr 4, 2022 · 0 comments

Comments

@joomlapl-bot
Copy link
Collaborator

PR w związku ze zmianą oryginału joomla/joomla-cms#36250 Poniżej zmiany w oryginale:

Click to expand the diff!
diff --git a/administrator/components/com_templates/src/Controller/TemplateController.php b/administrator/components/com_templates/src/Controller/TemplateController.php
index 8a4d93d1c0fc..15643a5cfe77 100644
--- a/administrator/components/com_templates/src/Controller/TemplateController.php
+++ b/administrator/components/com_templates/src/Controller/TemplateController.php
@@ -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);
 
@@ -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;
 	}
 }
diff --git a/administrator/components/com_templates/src/Model/TemplateModel.php b/administrator/components/com_templates/src/Model/TemplateModel.php
index bd5f3fd1c354..3c7791f6e110 100644
--- a/administrator/components/com_templates/src/Model/TemplateModel.php
+++ b/administrator/components/com_templates/src/Model/TemplateModel.php
@@ -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.
@@ -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
 	 *
@@ -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;
@@ -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;
diff --git a/administrator/components/com_templates/src/View/Template/HtmlView.php b/administrator/components/com_templates/src/View/Template/HtmlView.php
index 124608c1da84..8aa3f7cf0c7f 100644
--- a/administrator/components/com_templates/src/View/Template/HtmlView.php
+++ b/administrator/components/com_templates/src/View/Template/HtmlView.php
@@ -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'));
diff --git a/administrator/components/com_templates/tmpl/template/default_modal_child_body.php b/administrator/components/com_templates/tmpl/template/default_modal_child_body.php
index a689ee470e73..3b9b83e6414a 100644
--- a/administrator/components/com_templates/tmpl/template/default_modal_child_body.php
+++ b/administrator/components/com_templates/tmpl/template/default_modal_child_body.php
@@ -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">
@@ -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>
diff --git a/administrator/language/en-GB/com_templates.ini b/administrator/language/en-GB/com_templates.ini
index 1b2211b71069..00979d8b018f 100644
--- a/administrator/language/en-GB/com_templates.ini
+++ b/administrator/language/en-GB/com_templates.ini
@@ -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."
@@ -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;."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants