2 declare(strict_types
=1);
3 namespace wcf\acp\page
;
4 use wcf\data\template\group\TemplateGroupList
;
5 use wcf\data\template\Template
;
6 use wcf\data\template\TemplateList
;
7 use wcf\page\AbstractPage
;
8 use wcf\system\exception\IllegalLinkException
;
11 use wcf\util\StringUtil
;
14 * Compares two templates.
16 * @author Tim Duesterhus
17 * @copyright 2001-2018 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Acp\Form
21 class TemplateDiffPage
extends AbstractPage
{
25 public $activeMenuItem = 'wcf.acp.menu.link.template';
31 public $templateID = 0;
37 public $template = null;
40 * template id of the template to compare with
46 * template to compare with
49 public $parent = null;
52 * differences between both templates
58 * template group hierarchy
61 public $templateGroupHierarchy = [];
66 public function readParameters() {
67 parent
::readParameters();
69 if (isset($_REQUEST['id'])) $this->templateID
= intval($_REQUEST['id']);
70 $this->template
= new Template($this->templateID
);
71 if (!$this->template
->templateID
) {
72 throw new IllegalLinkException();
74 if (isset($_REQUEST['parentID'])) $this->parentID
= intval($_REQUEST['parentID']);
75 $this->parent
= new Template($this->parentID
);
76 if ($this->parent
->templateID
) {
77 if ($this->parent
->templateName
!= $this->template
->templateName ||
$this->parent
->application
!= $this->template
->application
) {
78 throw new IllegalLinkException();
86 public function readData() {
89 // read out template groups
90 $templateGroupList = new TemplateGroupList();
91 $templateGroupList->readObjects();
93 // build template group hierarchy (template groups that are parents of the template group of the selected template)
94 $this->templateGroupHierarchy
= [];
95 $templateGroup = $templateGroupList->search($this->template
->templateGroupID
);
96 while ($templateGroup !== null) {
97 $this->templateGroupHierarchy
[$templateGroup->templateGroupID
] = ['group' => $templateGroup, 'hasTemplate' => false];
98 $templateGroup = $templateGroupList->search($templateGroup->parentTemplateGroupID
);
100 $this->templateGroupHierarchy
[0] = ['group' => [], 'hasTemplate' => false];
102 // find matching templates in the hierarchy
103 $templateList = new TemplateList();
104 $templateList->getConditionBuilder()->add('templateName = ?', [$this->template
->templateName
]);
105 $templateList->getConditionBuilder()->add('application = ?', [$this->template
->application
]);
106 $templateList->getConditionBuilder()->add('(template.templateGroupID IN(?) OR template.templateGroupID IS NULL)', [array_keys($this->templateGroupHierarchy
)]);
107 $templateList->readObjects();
108 foreach ($templateList as $template) {
109 $this->templateGroupHierarchy
[$template->templateGroupID ?
: 0]['hasTemplate'] = $template->templateID
;
112 // a valid parent template was given, calculate diff
113 if ($this->parent
->templateID
) {
114 $a = explode("\n", StringUtil
::unifyNewlines($this->parent
->getSource()));
115 $b = explode("\n", StringUtil
::unifyNewlines($this->template
->getSource()));
116 $this->diff
= new Diff($a, $b);
123 public function assignVariables() {
124 parent
::assignVariables();
126 WCF
::getTPL()->assign([
127 'templateID' => $this->templateID
,
128 'template' => $this->template
,
129 'parentID' => $this->parentID
,
130 'parent' => $this->parent
,
131 'diff' => $this->diff
,
132 'templateGroupHierarchy' => array_reverse($this->templateGroupHierarchy
, true)