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