1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Tiki_Profile_InstallHandler_Forum extends Tiki_Profile_InstallHandler
9{
10	function getData()
11	{
12		if ($this->data) {
13			return $this->data;
14		}
15
16		$data = $this->obj->getData();
17
18		$defaults = [
19			'parentId' => 0,
20			'description' => '',
21			'flood_interval' => 120,
22			'moderator' => 'admin',
23			'per_page' => 10,
24			'prune_max_age' => 3 * 24 * 3600,
25			'prune_unreplied_max_age' => 30 * 24 * 3600,
26			'topic_order' => 'lastPost_desc',
27			'thread_order' => '',
28			'section' => '',
29			'inbound_pop_server' => '',
30			'inbound_pop_port' => 110,
31			'inbound_pop_user' => '',
32			'inbound_pop_password' => '',
33			'outbound_address' => '',
34			'outbound_from' => '',
35			'approval_type' => 'all_posted',
36			'moderator_group' => '',
37			'forum_password' => '',
38			'attachments' => 'none',
39			'attachments_store' => 'db',
40			'attachments_store_dir' => '',
41			'attachments_max_size' => 10000000,
42			'forum_last_n' => 0,
43			'comments_per_page' => '',
44			'thread_style' => '',
45			'is_flat' => 'n',
46
47			'list_topic_reads' => 'n',
48			'list_topic_replies' => 'n',
49			'list_topic_points' => 'n',
50			'list_topic_last_post' => 'n',
51			'list_topic_last_post_title' => 'n',
52			'list_topic_last_post_avatar' => 'n',
53			'list_topic_author' => 'n',
54			'list_topic_author_avatar' => 'n',
55
56			'show_description' => 'n',
57
58			'enable_flood_control' => 'n',
59			'enable_inbound_mail' => 'n',
60			'enable_prune_unreplied' => 'n',
61			'enable_prune_old' => 'n',
62			'enable_vote_threads' => 'n',
63			'enable_outbound_for_inbound' => 'n',
64			'enable_outbound_reply_link' => 'n',
65			'enable_topic_smiley' => 'n',
66			'enable_topic_summary' => 'n',
67			'enable_ui_avatar' => 'n',
68			'enable_ui_rating_choice_topic' => 'n',
69			'enable_ui_flag' => 'n',
70			'enable_ui_posts' => 'n',
71			'enable_ui_level' => 'n',
72			'enable_ui_email' => 'n',
73			'enable_ui_online' => 'n',
74			'enable_password_protection' => 'n',
75			'forum_language' => '',
76		];
77
78		$data = Tiki_Profile::convertLists($data, ['enable' => 'y', 'list' => 'y',	'show' => 'y'], true);
79
80		$data = array_merge($defaults, $data);
81
82		$data = Tiki_Profile::convertYesNo($data);
83
84		return $this->data = $data;
85	}
86
87	function canInstall()
88	{
89		$data = $this->getData();
90
91		if (! isset($data['name'])) {
92			return false;
93		}
94
95		return true;
96	}
97
98	private static function getAttConverter()
99	{
100		return new Tiki_Profile_ValueMapConverter(
101			[
102				'none' => 'att_no',
103				'everyone' => 'att_all',
104				'allowed' => 'att_perm',
105				'admin' => 'att_admin',
106			]
107		);
108	}
109
110	function _install()
111	{
112		$comments = TikiLib::lib('comments');
113
114		$data = $this->getData();
115		$this->replaceReferences($data);
116
117		$attConverter = self::getAttConverter();
118
119		$id = $comments->replace_forum(
120			0,
121			$data['name'],
122			$data['description'],
123			$data['enable_flood_control'],
124			$data['flood_interval'],
125			$data['moderator'],
126			$data['mail'],
127			$data['enable_inbound_mail'],
128			$data['enable_prune_unreplied'],
129			$data['prune_unreplied_max_age'],
130			$data['enable_prune_old'],
131			$data['prune_max_age'],
132			$data['per_page'],
133			$data['topic_order'],
134			$data['thread_order'],
135			$data['section'],
136			$data['list_topic_reads'],
137			$data['list_topic_replies'],
138			$data['list_topic_points'],
139			$data['list_topic_last_post'],
140			$data['list_topic_author'],
141			$data['enable_vote_threads'],
142			$data['show_description'],
143			$data['inbound_pop_server'],
144			$data['inbound_pop_port'],
145			$data['inbound_pop_user'],
146			$data['inbound_pop_password'],
147			$data['outbound_address'],
148			$data['enable_outbound_for_inbound'],
149			$data['enable_outbound_reply_link'],
150			$data['outbound_from'],
151			$data['enable_topic_smiley'],
152			$data['enable_topic_summary'],
153			$data['enable_ui_avatar'],
154			$data['enable_ui_rating_choice_topic'],
155			$data['enable_ui_flag'],
156			$data['enable_ui_posts'],
157			$data['enable_ui_level'],
158			$data['enable_ui_email'],
159			$data['enable_ui_online'],
160			$data['approval_type'],
161			$data['moderator_group'],
162			$data['forum_password'],
163			$data['enable_password_protection'],
164			$attConverter->convert($data['attachments']),
165			$data['attachments_store'],
166			$data['attachments_store_dir'],
167			$data['attachments_max_size'],
168			$data['forum_last_n'],
169			$data['comments_per_page'],
170			$data['thread_style'],
171			$data['is_flat'],
172			$data['list_att_nb'],
173			$data['list_topic_last_post_title'],
174			$data['list_topic_last_post_avatar'],
175			$data['list_topic_author_avatar'],
176			$data['forum_language'],
177			$data['parentId']
178		);
179
180		return $id;
181	}
182
183	/**
184	 * Export forums
185	 *
186	 * @param Tiki_Profile_Writer $writer
187	 * @param int $forumId
188	 * @param bool $all
189	 * @return bool
190	 */
191	public static function export(Tiki_Profile_Writer $writer, $forumId, $all = false)
192	{
193		$forumlib = TikiLib::lib('comments');
194
195		if (isset($forumId) && ! $all) {
196			$listForums = [];
197			$listForums[] = $forumlib->get_forum($forumId);
198		} else {
199			$listForums = $listForums = $forumlib->list_forums();
200			$listForums = $listForums['data'];
201		}
202
203		if (empty($listForums[0]['forumId'])) {
204			return false;
205		}
206
207		foreach ($listForums as $forum) {
208			$writer->addObject(
209				'forum',
210				$forum['forumId'],
211				[
212					'name' => $forum['name'],
213					'description' => $forum['description'],
214					'enable_flood_control' => $forum['controlFlood'],
215					'flood_interval' => $forum['floodInterval'],
216					'moderator' => $forum['moderator'],
217					'mail' => $forum['mail'],
218					'enable_inbound_mail' => $forum['useMail'],
219					'section' => $forum['section'],
220					'enable_prune_unreplied' => $forum['usePruneUnreplied'],
221					'prune_unreplied_max_age' => $forum['pruneUnrepliedAge'],
222					'enable_prune_old' => $forum['usePruneOld'],
223					'prune_max_age' => $forum['pruneMaxAge'],
224					'per_page' => $forum['topicsPerPage'],
225					'topic_order' => $forum['topicOrdering'],
226					'thread_order' => $forum['threadOrdering'],
227					'attachments' => self::getAttConverter()->reverse($forum['att']),
228					'attachments_store' => $forum['att_store'],
229					'attachments_store_dir' => $forum['att_store_dir'],
230					'attachments_max_size' => $forum['att_max_size'],
231					'list_att_nb' => $forum['att_list_nb'],
232					'enable_ui_level' => $forum['ui_level'],
233					'enable_password_protection' => $forum['forum_use_password'],
234					'forum_password' => $forum['forum_password'],
235					'moderator_group' => $forum['moderator_group'],
236					'approval_type' => $forum['approval_type'],
237					'outbound_address' => $forum['outbound_address'],
238					'enable_outbound_for_inbound' => $forum['outbound_mails_for_inbound_mails'],
239					'enable_outbound_mail_reply_link' => $forum['outbound_mails_reply_link'],
240					'outbound_from' => $forum['outbound_from'],
241					'inbound_pop_server' => $forum['inbound_pop_server'],
242					'inbound_pop_port' => $forum['inbound_pop_port'],
243					'inbound_pop_user' => $forum['inbound_pop_user'],
244					'inbound_pop_password' => $forum['inbound_pop_password'],
245					'enable_topic_smiley' => $forum['topic_smileys'],
246					'enable_ui_avatar' => $forum['ui_avatar'],
247					'enable_ui_rating_choice_topic' => $forum['ui_rating_choice_topic'],
248					'enable_ui_flag' => $forum['ui_flag'],
249					'enable_ui_posts' => $forum['ui_posts'],
250					'enable_ui_email' => $forum['ui_email'],
251					'enable_ui_online' => $forum['ui_online'],
252					'enable_topic_summary' => $forum['topic_summary'],
253					'show_description' => $forum['show_description'],
254					'list_topic_replies' => $forum['topics_list_replies'],
255					'list_topic_reads' => $forum['topics_list_reads'],
256					'list_topic_points' => $forum['topics_list_pts'],
257					'list_topic_last_post' => $forum['topics_list_lastpost'],
258					'list_topic_last_post_title' => $forum['topics_list_lastpost_title'],
259					'list_topic_last_post_avatar' => $forum['topics_list_lastpost_avatar'],
260					'list_topic_author_avatar' => $forum['topics_list_author_avatar'],
261					'list_topic_author' => $forum['topics_list_author'],
262					'enable_vote_threads' => $forum['vote_threads'],
263					'forum_last_n' => $forum['forum_last_n'],
264					'thread_style' => $forum['threadStyle'],
265					'comments_per_page' => $forum['commentsPerPage'],
266					'is_flat' => $forum['is_flat'],
267				]
268			);
269		}
270		return true;
271	}
272
273	/**
274	 * Remove forum
275	 *
276	 * @param string $forumName
277	 * @return bool
278	 */
279	function remove($forumName)
280	{
281		if (! empty($forumName)) {
282			$comments = TikiLib::lib('comments');
283			$forum = $comments->list_forums(0, 1, 'forumId_desc', $forumName);
284			$forumId = ! empty($forum['data'][0]['forumId']) ? $forum['data'][0]['forumId'] : null;
285			if ($forumId && $comments->remove_forum($forumId)) {
286				return true;
287			}
288		}
289
290		return false;
291	}
292
293	/**
294	 * Get current forum data
295	 *
296	 * @param array $forum
297	 * @return mixed
298	 */
299	public function getCurrentData($forum)
300	{
301		$forumName = ! empty($forum['name']) ? $forum['name'] : '';
302		if (! empty($forumName)) {
303			$comments = TikiLib::lib('comments');
304			$forum = $comments->list_forums(0, 1, 'forumId_desc', $forumName);
305			$forumData = ! empty($forum['data'][0]) ? $forum['data'][0] : false;
306			return $forumData;
307		}
308		return false;
309	}
310}
311