2 namespace wcf\acp\page
;
3 use wcf\data\template\group\TemplateGroupList
;
4 use wcf\data\template\Template
;
5 use wcf\data\template\TemplateList
;
6 use wcf\page\AbstractPage
;
7 use wcf\system\exception\IllegalLinkException
;
10 use wcf\util\StringUtil
;
13 * Compares two templates.
15 * @author Tim Duesterhus
16 * @copyright 2001-2018 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\Acp\Form
20 class TemplateDiffPage
extends AbstractPage
{
24 public $activeMenuItem = 'wcf.acp.menu.link.template';
30 public $templateID = 0;
36 public $template = null;
39 * template id of the template to compare with
45 * template to compare with
48 public $parent = null;
51 * differences between both templates
57 * template group hierarchy
60 public $templateGroupHierarchy = [];
65 public function readParameters() {
66 parent
::readParameters();
68 if (isset($_REQUEST['id'])) $this->templateID
= intval($_REQUEST['id']);
69 $this->template
= new Template($this->templateID
);
70 if (!$this->template
->templateID
) {
71 throw new IllegalLinkException();
73 if (isset($_REQUEST['parentID'])) $this->parentID
= intval($_REQUEST['parentID']);
74 $this->parent
= new Template($this->parentID
);
75 if ($this->parent
->templateID
) {
76 if ($this->parent
->templateName
!= $this->template
->templateName ||
$this->parent
->application
!= $this->template
->application
) {
77 throw new IllegalLinkException();
85 public function readData() {
88 // read out template groups
89 $templateGroupList = new TemplateGroupList();
90 $templateGroupList->readObjects();
92 // build template group hierarchy (template groups that are parents of the template group of the selected template)
93 $this->templateGroupHierarchy
= [];
94 $templateGroup = $templateGroupList->search($this->template
->templateGroupID
);
95 while ($templateGroup !== null) {
96 $this->templateGroupHierarchy
[$templateGroup->templateGroupID
] = ['group' => $templateGroup, 'hasTemplate' => false];
97 $templateGroup = $templateGroupList->search($templateGroup->parentTemplateGroupID
);
99 $this->templateGroupHierarchy
[0] = ['group' => [], 'hasTemplate' => false];
101 // find matching templates in the hierarchy
102 $templateList = new TemplateList();
103 $templateList->getConditionBuilder()->add('templateName = ?', [$this->template
->templateName
]);
104 $templateList->getConditionBuilder()->add('application = ?', [$this->template
->application
]);
105 $templateList->getConditionBuilder()->add('(template.templateGroupID IN(?) OR template.templateGroupID IS NULL)', [array_keys($this->templateGroupHierarchy
)]);
106 $templateList->readObjects();
107 foreach ($templateList as $template) {
108 $this->templateGroupHierarchy
[$template->templateGroupID ?
: 0]['hasTemplate'] = $template->templateID
;
111 // a valid parent template was given, calculate diff
112 if ($this->parent
->templateID
) {
113 $a = explode("\n", StringUtil
::unifyNewlines($this->parent
->getSource()));
114 $b = explode("\n", StringUtil
::unifyNewlines($this->template
->getSource()));
115 $this->diff
= new Diff($a, $b);
122 public function assignVariables() {
123 parent
::assignVariables();
125 WCF
::getTPL()->assign([
126 'templateID' => $this->templateID
,
127 'template' => $this->template
,
128 'parentID' => $this->parentID
,
129 'parent' => $this->parent
,
130 'diff' => $this->diff
,
131 'templateGroupHierarchy' => array_reverse($this->templateGroupHierarchy
, true)