Merge branch 'master' into next
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / page / TemplateDiffPage.class.php
1 <?php
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;
9 use wcf\system\WCF;
10 use wcf\util\Diff;
11 use wcf\util\StringUtil;
12
13 /**
14 * Compares two templates.
15 *
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
20 */
21 class TemplateDiffPage extends AbstractPage {
22 /**
23 * @inheritDoc
24 */
25 public $activeMenuItem = 'wcf.acp.menu.link.template';
26
27 /**
28 * template id
29 * @var integer
30 */
31 public $templateID = 0;
32
33 /**
34 * template object
35 * @var Template
36 */
37 public $template = null;
38
39 /**
40 * template id of the template to compare with
41 * @var integer
42 */
43 public $parentID = 0;
44
45 /**
46 * template to compare with
47 * @var Template
48 */
49 public $parent = null;
50
51 /**
52 * differences between both templates
53 * @var Diff
54 */
55 public $diff = null;
56
57 /**
58 * template group hierarchy
59 * @var array
60 */
61 public $templateGroupHierarchy = [];
62
63 /**
64 * @inheritDoc
65 */
66 public function readParameters() {
67 parent::readParameters();
68
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();
73 }
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();
79 }
80 }
81 }
82
83 /**
84 * @inheritDoc
85 */
86 public function readData() {
87 parent::readData();
88
89 // read out template groups
90 $templateGroupList = new TemplateGroupList();
91 $templateGroupList->readObjects();
92
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);
99 }
100 $this->templateGroupHierarchy[0] = ['group' => [], 'hasTemplate' => false];
101
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;
110 }
111
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);
117 }
118 }
119
120 /**
121 * @inheritDoc
122 */
123 public function assignVariables() {
124 parent::assignVariables();
125
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)
133 ]);
134 }
135 }