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 Search_Formatter_ValueFormatter_Categorylist extends Search_Formatter_ValueFormatter_Abstract
9{
10	private $requiredParents = [];
11	private $excludeParents = [];
12	private $singleList = 'y';
13	private $separator;
14
15	function __construct($arguments)
16	{
17		if (! empty($arguments['requiredParents'])) {
18			$this->requiredParents = explode(',', $arguments['requiredParents']);
19		} else {
20			$this->requiredParents = 'all';
21		}
22
23		if (isset($arguments['excludeParents'])) {
24			$this->excludeParents = explode(',', $arguments['excludeParents']);
25		}
26
27		if (isset($arguments['singleList'])) {
28			$this->singleList = $arguments['singleList'];
29		}
30
31		if (isset($arguments['separator'])) {
32			$this->separator = $arguments['separator'];
33		}
34
35		if (isset($arguments['levelSeparator'])) {
36			$this->levelSeparator = $arguments['levelSeparator'];
37		} else {
38			$this->levelSeparator = ":";
39		}
40
41		if (isset($arguments['useFullPath'])) {
42			$this->useFullPath = $arguments['useFullPath'];
43		} else {
44			$this->useFullPath = "n";
45		}
46	}
47
48	function render($name, $value, array $entry)
49	{
50		$smarty = TikiLib::lib('smarty');
51		$smarty->loadPlugin('smarty_function_object_link');
52
53		$arr = TikiLib::lib('categ')->getCategories();
54		$list = '';
55
56		// if coming from category field _text version
57		if (! is_array($value)) {
58			$value = explode(' ', $value);
59		}
60
61		foreach ($arr as $arx) {
62			$myArr[$arx['categId']] = ['parentId' => $arx['parentId'],'name' => $arx['name'], 'tepath' => $arx['tepath']];
63		}
64
65		if ($this->singleList == 'y') {
66			foreach ($value as $ar) {
67				if ($ar == 'orphan') {
68					break;
69				}
70
71				$p_info = $myArr[$ar];
72
73				$showCat = $this->shouldShow($p_info['tepath']);
74
75				if ($showCat) {
76					if ($this->useFullPath == 'y') {
77						$foundRoot = false;
78						$printedPath = "";
79						foreach ($p_info['tepath'] as $key => $value) {
80							if ($foundRoot || $this->requiredParents == "all") {
81								$params = ['type' => 'category', 'id' => $key];
82								$link = smarty_function_object_link($params, $smarty->getEmptyInternalTemplate());
83								if (empty($printedPath)) {
84									$printedPath = $link;
85								} else {
86									$printedPath .= $this->levelSeparator . $link;
87								}
88							} elseif (in_array($key, $this->requiredParents)) {
89								$foundRoot = true;
90							}
91						}
92					} else {
93						$printedPath = $p_info['name'];
94					}
95
96					if (! empty($this->separator)) {
97						$list .= $printedPath . $this->separator;
98					} else {
99						if (empty($list)) {
100							$list = "<ul class=\"categoryLinks\">";
101						}
102						$list .= ' <li>' . $printedPath . "</li>";
103					}
104				}
105			}
106			if (! empty($this->separator)) {
107				$g = 0 - strlen($this->separator);
108				$list = substr($list, 0, $g);
109			} elseif (! empty($list)) {
110				$list .= "</ul>";
111			}
112		} else {
113			$parent  = [];
114
115			foreach ($value as $ar) {
116				if ($ar == 'orphan') {
117					break;
118				}
119
120				$p_info = $myArr[$ar];
121
122				$showCat = $this->shouldShow($p_info['parentId']);
123
124				if ($showCat) {
125					$parent[$p_info['parentId']][] = $ar;
126				}
127			}
128
129			foreach ($parent as $k => $v) {
130				if (empty($this->separator)) {
131					$list .= "<h5>{$myArr[$k]['name']}</h5><ul class=\"categoryLinks\">";
132					foreach ($v as $t) {
133						$params = ['type' => 'category', 'id' => $t];
134						$link = smarty_function_object_link($params, $smarty->getEmptyInternalTemplate());
135						$list .= "<li>" . $link . "</li>";
136					}
137					$list .= "</ul>";
138				} else {
139					$list .= "{$myArr[$k]['name']}: ";
140					foreach ($v as $t) {
141						$params = ['type' => 'category', 'id' => $t];
142						$link = smarty_function_object_link($params, $smarty->getEmptyInternalTemplate());
143						$list .= $link . $this->separator;
144					}
145				}
146				if (! empty($this->separator)) {
147					$g = 0 - strlen($this->separator);
148					$list = substr($list, 0, $g);
149					$list .= "<br />";
150				}
151			}
152		}
153		return '~np~' . $list . '~/np~';
154	}
155
156	private function shouldShow($categoryPath)
157	{
158		//if it's not an array, it's simply an id
159		if (! is_array($categoryPath)) {
160			//set category path as an array with its only item as the id for both key and value.
161			$categoryPath = [$categoryPath => $categoryPath];
162		}
163
164		$showCat = false;
165		if ($this->requiredParents == 'all') {
166			$showCat = true;
167		}
168		foreach ($categoryPath as $key => $val) {
169			if (in_array($key, $this->requiredParents)) {
170				$showCat = true;
171			}
172		}
173		foreach ($categoryPath as $key => $val) {
174			if (in_array($key, $this->excludeParents)) {
175				$showCat = false;
176			}
177		}
178
179		return $showCat;
180	}
181}
182