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 Services_Tracker_CalendarController
9{
10	function setUp()
11	{
12		Services_Exception_Disabled::check('calendar_fullcalendar');
13	}
14
15	function action_list($input)
16	{
17		global $prefs, $user, $tikilib;
18
19		$unifiedsearchlib = TikiLib::lib('unifiedsearch');
20		$index = $unifiedsearchlib->getIndex();
21
22		$start = 'tracker_field_' . $input->beginField->word();
23		$end = 'tracker_field_' . $input->endField->word();
24
25		if ($resource = $input->resourceField->word()) {
26			$resource = 'tracker_field_' . $resource;
27		}
28
29		if ($coloring = $input->coloringField->word()) {
30			$coloring = 'tracker_field_' . $coloring;
31		}
32
33		$query = $unifiedsearchlib->buildQuery([]);
34
35		if (is_numeric($input->start->string())) {
36			$useTimestamp = true;
37			$from = $input->start->int();
38			$to = $input->end->int();
39		} else {
40			$useTimestamp = false;
41			$timezone = $input->timezone->string();
42			preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/', $input->start->string(), $matches);
43
44			if ($input->start->string() === $matches[0]) {
45				$from = strtotime($input->start->iso8601() . ' ' . $timezone);
46				$to = strtotime($input->end->iso8601() . ' ' . $timezone);
47			} else {
48				$from = strtotime($input->start->isodate());
49				$to = strtotime($input->end->isodate());
50			}
51		}
52
53		$query->filterRange($from, $to, [$start, $end]);
54		$query->setRange(0, $prefs['unified_lucene_max_result']);
55
56		if ($body = $input->filters->none()) {
57			$builder = new Search_Query_WikiBuilder($query, $input);
58			$builder->apply(WikiParser_PluginMatcher::match($body));
59		}
60
61		$result = $query->search($index);
62
63		$response = [];
64
65		$fields = [];
66		if ($definition = Tracker_Definition::get($input->trackerId->int())) {
67			foreach ($definition->getPopupFields() as $fieldId) {
68				if ($field = $definition->getField($fieldId)) {
69					$fields[] = $field;
70				}
71			}
72		}
73
74		$smarty = TikiLib::lib('smarty');
75		$smarty->loadPlugin('smarty_modifier_sefurl');
76		$trklib = TikiLib::lib('trk');
77		foreach ($result as $row) {
78			$item = Tracker_Item::fromId($row['object_id']);
79			$description = '';
80			foreach ($fields as $field) {
81				if ($item->canViewField($field['fieldId'])) {
82					$val = trim($trklib->field_render_value(
83						[
84							'field' => $field,
85							'item' => $item->getData(),
86							'process' => 'y',
87						]
88					));
89					if ($val) {
90						if (count($fields) > 1) {
91							$description .= "<h5>{$field['name']}</h5>";
92						}
93						$description .= $val;
94					}
95				}
96			}
97
98			$colormap = base64_decode($input->colormap->word());
99
100			$dtStart = $this->getTimestamp($row[$start]);
101			$dtEnd = $this->getTimestamp($row[$end]);
102
103			$response[] = [
104				'id' => $row['object_id'],
105				'trackerId' => isset($row['tracker_id']) ? $row['tracker_id'] : null,
106				'title' => $row['title'],
107				'description' => $description,
108				'url' => smarty_modifier_sefurl($row['object_id'], $row['object_type']),
109				'allDay' => false,
110				'start' => $useTimestamp ? $dtStart : TikiLib::date_format("c", $dtStart, $user, 5, false),
111				'end' => $useTimestamp ? $dtEnd : TikiLib::date_format("c", $dtEnd, $user, 5, false),
112				'editable' => $item->canModify(),
113				'color' => $this->getColor(isset($row[$coloring]) ? $row[$coloring] : '', $colormap),
114				'textColor' => '#000',
115				'resourceId' => ($resource && isset($row[$resource])) ? strtolower($row[$resource]) : '',
116				'resourceEditable' => true,
117			];
118		}
119
120		return $response;
121	}
122
123	private function getTimestamp($value)
124	{
125		if (preg_match('/^\d{14}$/', $value)) {
126			// Facing a date formated as YYYYMMDDHHIISS as indexed in lucene
127			// Always stored as UTC
128			return date_create_from_format('YmdHise', $value . 'UTC')->getTimestamp();
129		} elseif (is_numeric($value)) {
130			return $value;
131		} else {
132			return strtotime($value . ' UTC');
133		}
134	}
135
136	private function getColor($value, $colormap)
137	{
138		static $colors = ['#6cf', '#6fc', '#c6f', '#cf6', '#f6c', '#fc6'];
139		static $map = [];
140
141		if (empty($map) && ! empty($colormap)) {
142			foreach (explode('|', $colormap) as $color) {
143				$colorMapParts = explode(',', $color);
144				$map[trim($colorMapParts[0])] = trim($colorMapParts[1]);
145			}
146		}
147
148		if (! isset($map[$value])) {
149			$color = array_shift($colors);
150			$colors[] = $color;
151			$map[$value] = $color;
152		}
153
154		return $map[$value];
155	}
156}
157