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
8function wikiplugin_bloglist_info()
9{
10	return [
11		'name' => tra('Blog List'),
12		'documentation' => 'PluginBlogList',
13		'description' => tra('Display posts from a site blog'),
14		'prefs' => [ 'feature_blogs', 'wikiplugin_bloglist' ],
15		'iconname' => 'list',
16		'introduced' => 1,
17		'params' => [
18			'Id' => [
19				'required' => true,
20				'name' => tra('Blog ID'),
21				'description' => tra('The ID number of the blog on the site to list posts from. More than one blog can be provided, separated by colon. Example: 1:5. Limitation: if more than one blog is provided, the private posts (drafts) are not shown.'),
22				'filter' => 'striptags',
23				'default' => '',
24				'profile_reference' => 'blog',
25				'since' => '1'
26			],
27			'Items' => [
28				'required' => false,
29				'name' => tra('Maximum Items'),
30				'description' => tra('Maximum number of entries to list (no maximum set by default)'),
31				'filter' => 'digits',
32				'default' => '',
33				'since' => '3.0'
34			],
35			'author' => [
36				'required' => false,
37				'name' => tra('Author'),
38				'description' => tra('Only display posts created by this user (all posts listed by default)'),
39				'default' => '',
40				'since' => '3.5',
41			],
42			'simpleList' => [
43				'required' => false,
44				'name' => tra('Simple List'),
45				'description' => tra('Show simple list of date, title and author (default) or formatted list of blog
46					posts'),
47				'default' => 'y',
48				'since' => '3.5',
49				'options' => [
50					['text' => '', 'value' => ''],
51					['text' => tra('Yes'), 'value' => 'y'],
52					['text' => tra('No'), 'value' => 'n']
53				],
54			],
55			'charCount' => [
56				'required' => false,
57				'name' => tra('Character Count'),
58				'description' => tra('Number of characters to display if not a simple list (defaults to all)'),
59				'filter' => 'digits',
60				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
61				'default' => '',
62				'since' => '12.0',
63			],
64			'wordBoundary' => [
65				'required' => false,
66				'name' => tra('Word Boundary'),
67				'description' => tra('If not a simple list and Character Count is non-zero, then marking this as yes will
68					break on word boundaries only.'),
69				'default' => 'y',
70				'since' => '12.0',
71				'options' => [
72					['text' => tra('Yes'), 'value' => 'y'],
73					['text' => tra('No'), 'value' => 'n']
74				],
75				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
76			],
77			'ellipsis' => [
78				'required' => false,
79				'name' => tra('Ellipsis'),
80				'description' => tra('If not a simple list and Character Count is non-zero, then marking this as yes will
81					put ellipsis (...) at end of text (default).'),
82				'default' => 'y',
83				'since' => '12.0',
84				'options' => [
85					['text' => '', 'value' => ''],
86					['text' => tra('Yes'), 'value' => 'y'],
87					['text' => tra('No'), 'value' => 'n']
88				],
89				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
90			],
91			'more' => [
92				'required' => false,
93				'name' => tra('More'),
94				'description' => tra('If not a simple list and Character Count is non-zero, then marking this as yes
95					will put a "More" link to the full entry (default).'),
96				'default' => 'y',
97				'since' => '12.0',
98				'options' => [
99					['text' => tra('Yes'), 'value' => 'y'],
100					['text' => tra('No'), 'value' => 'n']
101				],
102				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
103			],
104			'showIcons' => [
105				'required' => false,
106				'name' => tra('Show Icons'),
107				'description' => tra('If not a simple list, marking this as no will prevent the "edit" and "print" type
108					icons from displaying (default is to show the icons)'),
109				'default' => 'y',
110				'since' => '12.0',
111				'options' => [
112					['text' => tra('Yes'), 'value' => 'y'],
113					['text' => tra('No'), 'value' => 'n']
114				],
115				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
116			],
117			'useExcerpt' => [
118				'required' => false,
119				'name' => tra('Use Excerpt'),
120				'description' => tra('If the blog has "Use post excerpt" enabled then use excerpts where available (default)'),
121				'default' => 'y',
122				'since' => '13.2',
123				'options' => [
124					['text' => '', 'value' => ''],
125					['text' => tra('Yes'), 'value' => 'y'],
126					['text' => tra('No'), 'value' => 'n']
127				],
128				'parentparam' => ['name' => 'simpleList', 'value' => 'n'],
129			],
130			'dateStart' => [
131				'required' => false,
132				'name' => tra('Start Date'),
133				'description' => tra('Earliest date to select posts from.') . ' (<code>YYYY-MM-DD</code>)',
134				'filter' => 'date',
135				'default' => '',
136				'since' => '3.5',
137			],
138			'dateEnd' => [
139				'required' => false,
140				'name' => tra('End Date'),
141				'description' => tra('Latest date to select posts from.') . ' (<code>YYYY-MM-DD</code>)',
142				'filter' => 'date',
143				'default' => '',
144				'since' => '3.5',
145			],
146			'containerClass' => [
147				'required' => false,
148				'name' => tra('Container Class'),
149				'description' => tr(
150					'CSS Class to add to the container %0DIV.article%1. (Default=%0wikiplugin_bloglist%1)',
151					'<code>',
152					'</code>'
153				),
154				'filter' => 'text',
155				'default' => 'wikiplugin_bloglist',
156				'accepted' => tra('Valid CSS class'),
157				'since' => '3.5',
158			],
159		],
160	];
161}
162
163function wikiplugin_bloglist($data, $params)
164{
165	global $user;
166	$tikilib = TikiLib::lib('tiki');
167	$smarty = TikiLib::lib('smarty');
168
169	if (! isset($params['Id'])) {
170		Feedback::error(tra('Missing blog ID for Bloglist plugin'));
171		return '';
172	}
173	// Sanitize $params['Id'])
174	$params['Id'] = preg_filter('/[^0-9:]*/', '', $params['Id']);
175
176	if (! isset($params['Items'])) {
177		$params['Items'] = -1;
178	}
179	if (! isset($params['offset'])) {
180		$params['offset'] = 0;
181	}
182	if (! isset($params['sort_mode'])) {
183		$params['sort_mode'] = 'created_desc';
184	}
185	if (! isset($params['find'])) {
186		$params['find'] = '';
187	}
188	if (! isset($params['author'])) {
189		$params['author'] = '';
190	}
191	if (! isset($params['simpleList'])) {
192		$params['simpleList'] = 'y';
193	}
194	if (! isset($params['isHtml'])) {
195		$params['isHtml'] = 'n';
196	}
197	if (! isset($params['useExcerpt'])) {
198		$params['useExcerpt'] = 'y';
199	}
200
201	if (isset($params['dateStart'])) {
202		$dateStartTS = strtotime($params['dateStart']);
203	}
204	if (isset($params['dateEnd'])) {
205		$dateEndTS = strtotime($params['dateEnd']);
206	}
207	$dateStartTS = ! empty($dateStartTS) ? $dateStartTS : 0;
208	$dateEndTS = ! empty($dateEndTS) ? $dateEndTS : $tikilib->now;
209
210	if (! isset($params['containerClass'])) {
211		$params['containerClass'] = 'wikiplugin_bloglist';
212	}
213	$smarty->assign('container_class', $params['containerClass']);
214
215	if (! isset($params['wordBoundary'])) {
216		$params['wordBoundary'] = '';
217	}
218	if (! isset($params['ellipsis'])) {
219		$params['ellipsis'] = '';
220	}
221	if (! isset($params['more'])) {
222		$params['more'] = '';
223	}
224
225	if ($params['simpleList'] == 'y') {
226		$bloglib = TikiLib::lib('blog');
227		$blogItems = $bloglib->list_posts($params['offset'], $params['Items'], $params['sort_mode'], $params['find'], $params['Id'], $params['author'], '', $dateStartTS, $dateEndTS);
228		$smarty->assign_by_ref('blogItems', $blogItems['data']);
229		$template = 'wiki-plugins/wikiplugin_bloglist.tpl';
230	} else {
231		$bloglib = TikiLib::lib('blog');
232
233		$blogItems = $bloglib->list_blog_posts($params['Id'], false, $params['offset'], $params['Items'], $params['sort_mode'], $params['find'], $dateStartTS, $dateEndTS);
234
235		if (isset($params['charCount']) && $params['charCount'] > 0) {
236			$blogItems = $bloglib->mod_blog_posts($blogItems, $params['charCount'], $params['wordBoundary'], $params['ellipsis'], $params['more']);
237		}
238
239		$blog_data = TikiLib::lib('blog')->get_blog($params['Id']);
240		$smarty->assign('blog_data', $blog_data);
241
242		$smarty->assign('ownsblog', $user && $user == $blog_data["user"] ? 'y' : 'n');
243
244		if ($params['showIcons'] == 'n') {
245			$smarty->assign('excerpt', 'y');
246		}
247
248		if ($params['useExcerpt'] === 'y' && $blog_data['use_excerpt'] === 'y') {
249			$smarty->assign('use_excerpt', 'y');
250			$smarty->assign('excerpt', 'n');	// no real idea why this gets assigned depending on showIcons above but it prevents excerpts being shown
251		}
252
253		$smarty->assign('show_heading', 'n');
254		$smarty->assign('use_author', 'y');
255		$smarty->assign('add_date', 'y');
256		$smarty->assign_by_ref('listpages', $blogItems['data']);
257		$template = 'tiki-view_blog.tpl';
258	}
259	$ret = $smarty->fetch($template);
260	return '~np~' . $ret . '~/np~';
261}
262