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_ContentSource_ActivityStreamSource implements Search_ContentSource_Interface
9{
10	private $lib;
11	private $relationlib;
12	private $tikilib;
13	private $source;
14
15	function __construct($source = null)
16	{
17		global $prefs;
18		$this->lib = TikiLib::lib('activity');
19		$this->source = $source;
20		$this->relationlib = TikiLib::lib('relation');
21		$this->tikilib = TikiLib::lib('tiki');
22	}
23
24	function getDocuments()
25	{
26		return $this->lib->getActivityList();
27	}
28
29	function getDocument($objectId, Search_Type_Factory_Interface $typeFactory)
30	{
31		global $prefs;
32
33		if (! $info = $this->lib->getActivity($objectId, $typeFactory)) {
34			return false;
35		}
36
37		$mapping = $this->lib->getMapping();
38
39		$document = [
40			'event_type' => $typeFactory->identifier($info['eventType']),
41			'modification_date' => $typeFactory->timestamp($info['eventDate']),
42			'date' => $typeFactory->timestamp($info['eventDate']),
43
44			'searchable' => $typeFactory->identifier('n'),
45		];
46
47		foreach ($info['arguments'] as $key => $value) {
48			$type = isset($mapping[$key]) ? $mapping[$key] : '';
49
50			if ($type) {
51				$document[$key] = $typeFactory->$type($value);
52			}
53		}
54
55		if (! isset($document['stream'])) {
56			$document['stream'] = $typeFactory->multivalue(['custom']);
57		}
58
59		if ($this->source && isset($document['type'], $document['object'])) {
60			$related = $this->source->getDocuments($info['arguments']['type'], $info['arguments']['object']);
61
62			if (count($related)) {
63				$first = reset($related);
64				$collectedFields = ['allowed_groups', 'categories', 'deep_categories', 'freetags', 'freetags_text', 'geo_located', 'geo_location', 'relations', 'relation_types'];
65
66				foreach ($collectedFields as $field) {
67					if (isset($first[$field])) {
68						$document[$field] = $first[$field];
69					}
70				}
71			}
72		}
73
74		if (isset($document['type'], $document['object'])) {
75			// Add tracker special perms
76			if ($info['arguments']['type'] == 'trackeritem') {
77				$item = TikiLib::lib('trk')->get_tracker_item($info['arguments']['object']);
78				if (empty($item)) {
79					return false;
80				}
81
82				$itemObject = Tracker_Item::fromInfo($item);
83
84				if (empty($itemObject) || ! $itemObject->getDefinition()) {	// ignore corrupted items, e.g. where trackerId == 0
85					return false;
86				}
87
88				$specialUsers = $itemObject->getSpecialPermissionUsers($info['arguments']['object'], 'View');
89
90				if ($specialUsers) {
91					$document['_extra_users'] = $specialUsers;
92				}
93			}
94		}
95
96		if ($prefs['monitor_individual_clear'] == 'y') {
97			$clearList = $this->getClearList($objectId);
98			$document['clear_list'] = $typeFactory->multivalue($clearList);
99		} else {
100			$document['clear_list'] = $typeFactory->multivalue([]);
101		}
102
103		return $document;
104	}
105
106	function getProvidedFields()
107	{
108		$mapping = $this->lib->getMapping();
109		return array_merge(['event_type', 'modification_date', 'clear_list', 'date'], array_keys($mapping));
110	}
111
112	function getGlobalFields()
113	{
114		return [
115			'date' => true,
116		];
117	}
118
119	private function getClearList($activityId)
120	{
121		$list = $this->relationlib->get_relations_to('activity', $activityId, 'tiki.monitor.cleared');
122		$out = [];
123		foreach ($list as $rel) {
124			if ($rel['type'] == 'user') {
125				$out[] = $rel['itemId'];
126			}
127		}
128
129		return $out;
130	}
131}
132