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_Formula_Function_ArticleInfo extends Math_Formula_Function
9{
10	function evaluate($element)
11	{
12		global $prefs;
13
14		if (count($element) != 3) {
15			$this->error(tra('Expecting three arguments for article-info.'));
16		}
17
18		$supported = [ 'rating', 'age-second', 'age-hour', 'age-day', 'age-week', 'age-month', 'view-count' ];
19		if (! in_array($element[2], $supported)) {
20			$this->error(tra('Unsupported property. Supported properties are: ') . implode(', ', $supported));
21		}
22
23		if ($prefs['feature_articles'] != 'y') {
24			$this->error(tra('The Articles feature is not activated.'));
25		}
26
27		$type = $this->evaluateChild($element[0]);
28		$object = $this->evaluateChild($element[1]);
29		$property = $element[2];
30
31		if ($type == 'article') {
32			$artlib = TikiLib::lib('art');
33			$article = $artlib->get_article($object, false);
34
35			if ($property == 'rating') {
36				return $article['rating'];
37			} elseif ($property == 'view-count') {
38				return $article[ 'nbreads' ];
39			} elseif (substr($property, 0, 4) == 'age-') {
40				$age = time() - $article['publishDate'];
41
42				switch ($property) {
43					case 'age-hour':
44						return max(0, floor($age / 3600));
45					case 'age-day':
46						return max(0, floor($age / (3600 * 24)));
47					case 'age-week':
48						return max(0, floor($age / (3600 * 24 * 7)));
49					case 'age-month':
50						return max(0, floor($age / (3600 * 24 * 30)));
51					default:
52						return max(0, $age);
53				}
54			}
55		} elseif ($type !== 0) {
56			$this->error('Only available for articles.');
57		}
58	}
59}
60