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
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14/**
15 * @return array
16 */
17function module_article_archives_info()
18{
19	return [
20		'name' => tra('Article Archives'),
21		'description' => tra('Shows links to the published articles for each month.'),
22		'prefs' => ['feature_articles'],
23		'params' => [
24			'more' => [
25				'name' => tra('More'),
26				'description' => tra('If set to "y", displays a button labelled "More..." that links to a paginated view of the selected articles.') . " " . tr('Default: "n".'),
27				'filter' => 'word',
28			],
29			'categId' => [
30				'name' => tra('Category filter'),
31				'description' => tra('If set to a category identifier, only consider the articles in the specified category.') . " " . tra('Example value: 13.') . " " . tr('Not set by default.'),
32				'filter' => 'int',
33				'profile_reference' => 'category',
34			],
35			'topic' => [
36				'name' => tra('Topic filter (by names)'),
37				'description' => tra('If set to a list of article topic names separated by plus signs, only consider the articles in the specified article topics. If the string is preceded by an exclamation mark ("!"), the effect is reversed, i.e. articles in the specified article topics are not considered.') . " " . tra('Example values:') . ' Switching to Tiki, !Switching to Tiki, Tiki upgraded to version 6+Our project is one year old, !Tiki upgraded to version 6+Our project is one year old+Mr. Jones is appointed as CEO.' . " " . tr('Not set by default.')
38			],
39			'topicId' => [
40				'name' => tra('Topic filter (by identifiers)'),
41				'description' => tra('If set to a list of article topic identifiers separated by plus signs, only consider the articles in the specified article topics. If the string is preceded by an exclamation mark ("!"), the effect is reversed, i.e. articles in the specified article topics are not considered.') . " " . tra('Example values: 13, !13, 1+3, !1+5+7.') . " " . tr('Not set by default.'),
42				'profile_reference' => 'article_topic',
43			],
44			'type' => [
45				'name' => tra('Types filter'),
46				'description' => tra('If set to a list of article type names separated by plus signs, only consider the articles of the specified types. If the string is preceded by an exclamation mark ("!"), the effect is reversed, i.e. articles of the specified article types are not considered.') . " " . tra('Example values: Event, !Event, Event+Review, !Event+Classified+Article.') . " " . tr('Not set by default.'),
47			],
48			'langfilter' => [
49				'name' => tra('Language filter'),
50				'description' => tra('If set to a language code, only consider the articles in the specified language.') . " " . tra('Example values:') . ' en, fr.' . " " . tr('Not set by default.'),
51			],
52		],
53		'common_params' => ['nonums']
54	];
55}
56
57/**
58 * @param $mod_reference
59 * @param $module_params
60 */
61function module_article_archives($mod_reference, $module_params)
62{
63	$tikilib = TikiLib::lib('tiki');
64	$smarty = TikiLib::lib('smarty');
65	$artlib = TikiLib::lib('art');
66
67	$urlParams = [
68		'topicId' => 'topic',
69		'topic' => 'topicName',
70		'categId' => 'categId',
71		'type' => 'type',
72		'langfilter' => 'lang',
73		'showImg' => null,
74		'showDate' => null,
75		'showHeading' => null,
76	];
77
78	foreach ($urlParams as $p => $v) {
79		if (isset($$p)) {
80			continue;
81		}
82		$$p = isset($module_params[$p]) ? $module_params[$p] : '';
83	}
84
85	foreach ($urlParams as $p => $v) {
86		$smarty->assign($p, $$p);
87	}
88
89	$ranking = $artlib->list_articles(0, -1, 'publishDate_desc', '', '', date("U"), '', $type, $topicId, 'y', $topic, $categId, '', '', $langfilter);
90
91	// filter the month from the data
92	$artc_archive = [];
93	foreach ($ranking['data'] as $key => &$rk_data) {
94		if (isset($artc_archive[date('F Y', $rk_data['publishDate'])])) {
95			$artc_archive[date('F Y', $rk_data['publishDate'])]['item_count']++;
96		} else {
97			$artc_archive[date('F Y', $rk_data['publishDate'])] = [
98				'title' => date('F Y', $rk_data['publishDate']),
99				'start_month' => mktime(0, 0, 0, date('m', $rk_data['publishDate']), 1, date('Y', $rk_data['publishDate'])),
100				'end_month' => mktime(0, 0, -1, date('m', $rk_data['publishDate']) + 1, 1, date('Y', $rk_data['publishDate'])),
101				'item_count' => 1];
102		}
103	}
104
105	$smarty->assign('more', isset($module_params['more']) ? $module_params['more'] : 'n');
106	$smarty->assign('modArticleArchives', $artc_archive);
107	$smarty->assign('arch_count', 'y');
108}
109