1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11$section = 'admin';
12require_once('tiki-setup.php');
13$access->check_feature(['feature_wiki_templates','feature_cms_templates','feature_file_galleries_templates'], '', 'features', true);
14
15$templateslib = TikiLib::lib('template');
16
17$auto_query_args = ['templateId'];
18
19//get_strings tra('Content Templates')
20
21if (! isset($_REQUEST["templateId"])) {
22	$_REQUEST["templateId"] = 0;
23}
24$smarty->assign('templateId', $_REQUEST["templateId"]);
25if ($_REQUEST["templateId"]) {
26	$info = $templateslib->get_template($_REQUEST["templateId"]);
27	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'html')) {
28		$info["section_html"] = 'y';
29	} else {
30		$info["section_html"] = 'n';
31	}
32	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'wiki')) {
33		$info["section_wiki"] = 'y';
34	} else {
35		$info["section_wiki"] = 'n';
36	}
37	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'wiki_html')) {
38		$info["section_wiki_html"] = 'y';
39	} else {
40		$info["section_wiki_html"] = 'n';
41	}
42	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'file_galleries')) {
43		$info["section_file_galleries"] = 'y';
44	} else {
45		$info["section_file_galleries"] = 'n';
46	}
47	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'newsletters')) {
48		$info["section_newsletters"] = 'y';
49	} else {
50		$info["section_newsletters"] = 'n';
51	}
52	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'events')) {
53		$info["section_events"] = 'y';
54	} else {
55		$info["section_events"] = 'n';
56	}
57	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'admins')) {
58		$info["section_admins"] = 'y';
59	} else {
60		$info["section_admin"] = 'n';
61	}
62	if ($templateslib->template_is_in_section($_REQUEST["templateId"], 'cms')) {
63		$info["section_cms"] = 'y';
64	} else {
65		$info["section_cms"] = 'n';
66	}
67} else {
68	$info = [];
69	$info["name"] = '';
70	$info['template_type'] = 'static';
71	$info["content"] = '';
72	$info["section_cms"] = 'n';
73	$info["section_html"] = 'n';
74	$info["section_wiki"] = 'n';
75	$info["section_wiki_html"] = 'n';
76	$info["section_file_galleries"] = 'n';
77	$info["section_newsletters"] = 'n';
78	$info["section_event"] = 'n';
79}
80$cat_type = 'template';
81$cat_objid = $_REQUEST['templateId'];
82include_once("categorize_list.php");
83
84$smarty->assign_by_ref('info', $info);
85if (isset($_REQUEST["remove"]) && $access->checkCsrfForm(tr('Remove template?'))) {
86	$result = $templateslib->remove_template($_REQUEST["remove"]);
87	if ($result && $result->numRows()) {
88		Feedback::success(tr('Template removed'));
89	} else {
90		Feedback::error(tr('Template not removed'));
91	}
92}
93if (isset($_REQUEST["removesection"]) && $access->checkCsrfForm(tr('Remove section?'))) {
94	$result = $templateslib->remove_template_from_section($_REQUEST["rtemplateId"], $_REQUEST["removesection"]);
95	if ($result && $result->numRows()) {
96		Feedback::success(tr('Section removed'));
97	} else {
98		Feedback::error(tr('Section not removed'));
99	}
100}
101$smarty->assign('preview', 'n');
102if (isset($_REQUEST["preview"])) {
103	TikiLib::lib('access')->check_permission('edit_content_templates', 'Edit template', 'template', $_REQUEST['templateId']);
104
105	$smarty->assign('preview', 'y');
106	if (isset($_REQUEST["section_html"]) && $_REQUEST["section_html"] == 'on') {
107		$info["section_html"] = 'y';
108		$parsed = nl2br($_REQUEST["content"]);
109	} else {
110		$info["section_html"] = 'n';
111		$parsed = TikiLib::lib('parser')->parse_data($_REQUEST["content"], ['is_html' => $info['section_wiki_html'] === 'y']);
112	}
113	$smarty->assign('parsed', $parsed);
114	if (isset($_REQUEST["section_wiki"]) && $_REQUEST["section_wiki"] == 'on') {
115		$info["section_wiki"] = 'y';
116	} else {
117		$info["section_wiki"] = 'n';
118	}
119	if (isset($_REQUEST["section_file_galleries"]) && $_REQUEST["section_file_galleries"] == 'on') {
120		$info["section_file_galleries"] = 'y';
121	} else {
122		$info["section_file_galleries"] = 'n';
123	}
124	if (isset($_REQUEST["section_newsletters"]) && $_REQUEST["section_newsletters"] == 'on') {
125		$info["section_newsletters"] = 'y';
126	} else {
127		$info["section_newsletters"] = 'n';
128	}
129	if (isset($_REQUEST["section_events"]) && $_REQUEST["section_events"] == 'on') {
130		$info["section_events"] = 'y';
131	} else {
132		$info["section_events"] = 'n';
133	}
134	if (isset($_REQUEST["section_cms"]) && $_REQUEST["section_cms"] == 'on') {
135		$info["section_cms"] = 'y';
136	} else {
137		$info["section_cms"] = 'n';
138	}
139	$info["content"] = $_REQUEST["content"];
140	$info["name"] = $_REQUEST["name"];
141	$info['page_name'] = $_REQUEST['page_name'];
142	$info['template_type'] = $_REQUEST['template_type'];
143	$smarty->assign('info', $info);
144
145	$cookietab = 2;
146}
147if (isset($_REQUEST["save"]) && $access->checkCsrf()) {
148	$type = $_REQUEST['template_type'];
149
150	if ($type == 'page') {
151		$content = 'page:' . $_REQUEST['page_name'];
152	} else {
153		$content = $_REQUEST["content"];
154	}
155	if (isset($_REQUEST["name"]) && $_REQUEST["name"] != "") {
156		$tid = $templateslib->replace_template($_REQUEST["templateId"], $_REQUEST["name"], $content, $type);
157		$smarty->assign("templateId", '0');
158		$info["name"] = '';
159		$info["content"] = '';
160		$info["section_cms"] = 'n';
161		$info["section_wiki"] = 'n';
162		$info["section_wiki_html"] = 'n';
163		$info["section_file_galleries"] = 'n';
164		$info["section_newsletters"] = 'n';
165		$info["section_events"] = 'n';
166		$info["section_html"] = 'n';
167		$smarty->assign('info', $info);
168		if ($tid) {
169			Feedback::success(tr('Template %0 created or modified', htmlspecialchars($_REQUEST["name"])));
170		} else {
171			Feedback::error(tr('Template %0 not created or modified', htmlspecialchars($_REQUEST["name"])));
172		}
173
174		if (isset($_REQUEST["section_cms"]) && $_REQUEST["section_cms"] == 'on') {
175			$templateslib->add_template_to_section($tid, 'cms');
176		} else {
177			$templateslib->remove_template_from_section($tid, 'cms');
178		}
179		if (isset($_REQUEST["section_wiki"]) && $_REQUEST["section_wiki"] == 'on') {
180			$templateslib->add_template_to_section($tid, 'wiki');
181		} else {
182			$templateslib->remove_template_from_section($tid, 'wiki');
183		}
184		if (isset($_REQUEST["section_wiki_html"]) && $_REQUEST["section_wiki_html"] == 'on') {
185			$templateslib->add_template_to_section($tid, 'wiki_html');
186		} else {
187			$templateslib->remove_template_from_section($tid, 'wiki_html');
188		}
189		if (isset($_REQUEST["section_file_galleries"]) && $_REQUEST["section_file_galleries"] == 'on') {
190			$templateslib->add_template_to_section($tid, 'file_galleries');
191		} else {
192			$templateslib->remove_template_from_section($tid, 'file_galleries');
193		}
194		if (isset($_REQUEST["section_newsletters"]) && $_REQUEST["section_newsletters"] == 'on') {
195			$templateslib->add_template_to_section($tid, 'newsletters');
196		} else {
197			$templateslib->remove_template_from_section($tid, 'newsletters');
198		}
199		if (isset($_REQUEST["section_events"]) && $_REQUEST["section_events"] == 'on') {
200			$templateslib->add_template_to_section($tid, 'events');
201		} else {
202			$templateslib->remove_template_from_section($tid, 'events');
203		}
204		if (isset($_REQUEST["section_html"]) && $_REQUEST["section_html"] == 'on') {
205			$templateslib->add_template_to_section($tid, 'html');
206		} else {
207			$templateslib->remove_template_from_section($tid, 'html');
208		}
209
210		$cat_type = 'template';
211		$cat_objid = $tid;
212		$cat_desc = '';
213		$cat_name = $_REQUEST["name"];
214		$cat_href = "tiki-admin_content_templates.php?templateId=" . $cat_objid;
215		include_once("categorize.php");
216
217		// Locking: only needed on new templates, ajax locks existing ones
218		if ($prefs['lock_content_templates'] === 'y' && empty($_REQUEST['templateId'])) {
219			if (! empty($_REQUEST['locked'])) {
220				TikiLib::lib('attribute')->set_attribute('template', $tid, 'tiki.object.lock', $_REQUEST['locked']);
221			}
222		}
223
224		$cookietab = 1;
225	} else {
226		$smarty->assign("templateId", '0');
227		$info["name"] = '';
228		$info["content"] = (isset($_REQUEST["content"]) && $_REQUEST["content"] != '') ? $_REQUEST["content"] : '' ;
229		$info["section_cms"] = (isset($_REQUEST["section_cms"]) && $_REQUEST["section_cms"] == 'on') ? 'y' : 'n';
230		$info["section_wiki"] = (isset($_REQUEST["section_wiki"]) && $_REQUEST["section_wiki"] == 'on') ? 'y' : 'n';
231		$info["section_wiki_html"] = (isset($_REQUEST["section_wiki_html"]) && $_REQUEST["section_wiki_html"] == 'on') ? 'y' : 'n';
232		$info["section_file_galleries"] = (isset($_REQUEST["section_file_galleries"]) && $_REQUEST["section_file_galleries"] == 'on') ? 'y' : 'n';
233		$info["section_newsletters"] = (isset($_REQUEST["section_newsletters"]) && $_REQUEST["section_newsletters"] == 'on') ? 'y' : 'n' ;
234		$info["section_events"] = (isset($_REQUEST["section_events"]) && $_REQUEST["section_events"] == 'on') ? 'y' : 'n';
235		$info["section_html"] = (isset($_REQUEST["section_html"]) && $_REQUEST["section_html"] == 'on') ? 'y' : 'n';
236		$smarty->assign('info', $info);
237		$smarty->assign('emptyname', "true");
238
239		$cookietab = 2;
240	}
241}
242if (! isset($_REQUEST["sort_mode"])) {
243	$sort_mode = 'created_desc';
244} else {
245	$sort_mode = $_REQUEST["sort_mode"];
246}
247if (! isset($_REQUEST["offset"])) {
248	$offset = 0;
249} else {
250	$offset = $_REQUEST["offset"];
251}
252$smarty->assign_by_ref('offset', $offset);
253if (isset($_REQUEST["find"])) {
254	$find = $_REQUEST["find"];
255} else {
256	$find = '';
257}
258$smarty->assign('find', $find);
259$smarty->assign_by_ref('sort_mode', $sort_mode);
260$channels = $templateslib->list_all_templates($offset, $maxRecords, $sort_mode, $find);
261$smarty->assign_by_ref('cant_pages', $channels["cant"]);
262
263// wysiwyg decision
264if ($_REQUEST['templateId']) {
265	$info['is_html'] = $info['section_wiki_html'] === 'y' ? 1 : 0;
266	$info['wysiwyg'] = $info['section_wiki_html'];
267}
268include 'lib/setup/editmode.php';
269$info['section_wiki_html'] = $_SESSION['wysiwyg'];	//$info['is_html'] ? 'y' : 'n';
270
271// Handles switching editor modes
272$editlib = TikiLib::lib('edit');
273if (isset($_REQUEST['mode_normal']) && $_REQUEST['mode_normal'] == 'y') {
274	// Parsing page data as first time seeing html page in normal editor
275	$smarty->assign('msg', "Parsing html to wiki");
276	$info['content'] = $editlib->parseToWiki($_REQUEST["content"]);
277	$smarty->assign('parsed', $parsed);
278} elseif (isset($_REQUEST['mode_wysiwyg']) && $_REQUEST['mode_wysiwyg'] == 'y') {
279	// Parsing page data as first time seeing wiki page in wysiwyg editor
280	$smarty->assign('msg', "Parsing wiki to html");
281	$info['content'] = $editlib->parseToWysiwyg($_REQUEST["content"]);
282	$smarty->assign('parsed', $parsed);
283}
284
285// check edit/create perms
286if ($_REQUEST['templateId']) {
287	$perms = Perms::get(['type' => 'template', 'object' => $_REQUEST['templateId']]);
288	$canEdit = $perms->edit_content_templates;
289	if ($prefs['lock_content_templates'] === 'y' && $canEdit) {	// check for locked
290		$lockedby = TikiLib::lib('attribute')->get_attribute('template', $_REQUEST['templateId'], 'tiki.object.lock');
291		if ($lockedby && $lockedby === $user && $perms->lock_content_templates || ! $lockedby || $perms->admin_content_templates) {
292			$canEdit = true;
293		} else {
294			$canEdit = false;
295		}
296	}
297} else {
298	$canEdit = ($tiki_p_admin_content_templates === 'y') || ($tiki_p_admin === 'y');	// create
299}
300$smarty->assign('canEdit', $canEdit);
301
302$smarty->assign_by_ref('channels', $channels["data"]);
303$wikilib = TikiLib::lib('wiki');
304
305// disallow robots to index page:
306$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
307
308// Display the template
309$smarty->assign('mid', 'tiki-admin_content_templates.tpl');
310$smarty->display("tiki.tpl");
311