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/**
9 * Handler class for InGroup
10 *
11 * Letter key: ~N~
12 *
13 */
14class Tracker_Field_InGroup extends Tracker_Field_Abstract
15{
16	public static function getTypes()
17	{
18		return [
19			'N' => [
20				'name' => tr('In Group'),
21				'description' => tr('Indicate if the user associated with the item is a member of a specified group.'),
22				'readonly' => true,
23				'help' => 'In Group Field',
24				'prefs' => ['trackerfield_ingroup'],
25				'tags' => ['advanced'],
26				'default' => 'n',
27				'params' => [
28					'groupName' => [
29						'name' => tr('Group Name'),
30						'description' => tr('Name of the group to verify'),
31						'filter' => 'groupname',
32						'legacy_index' => 0,
33					],
34					'type' => [
35						'name' => tr('Display'),
36						'description' => tr('How to display the result'),
37						'filter' => 'alpha',
38						'options' => [
39							'' => tr('Yes/No'),
40							'date' => tr('Join date'),
41							'expire' => tr('Expiration date')
42						],
43						'legacy_index' => 1,
44					],
45				],
46			],
47		];
48	}
49
50	function getFieldData(array $requestData = [])
51	{
52		return [];
53	}
54
55	function renderInput($context = [])
56	{
57		$this->renderOutput($context);	// read only
58	}
59
60	function renderOutput($context = [])
61	{
62		$trklib = TikiLib::lib('trk');
63		$itemUsers = $trklib->get_item_creators($this->getConfiguration('trackerId'), $this->getItemId());
64
65		if (! empty($itemUsers)) {
66			if (! isset($trklib->tracker_infocache['users_group'][$this->getOption('groupName')])) {
67				$userlib = TikiLib::lib('user');
68				$trklib->tracker_infocache['users_group'][$this->getOption('groupName')] = $userlib->get_users_created_group($this->getOption('groupName'), null, true);
69			}
70			foreach ($itemUsers as $itemUser) {
71				if (isset($trklib->tracker_infocache['users_group'][$this->getOption('groupName')][$itemUser])) {
72					if ($this->getOption('type') == 'date') {
73						$value = $trklib->tracker_infocache['users_group'][$this->getOption('groupName')][$itemUser]['created'];
74					} elseif ($this->getOption('type') == 'expire') {
75						$value = $trklib->tracker_infocache['users_group'][$this->getOption('groupName')][$itemUser]['expire'];
76					} else {
77						$value = 'Yes';
78					}
79				} else {
80					if ($this->getOption('type') == 'date' || $this->getOption('type') == 'expire') {
81						$value = '';
82					} else {
83						$value = 'No';
84					}
85				}
86				if (! empty($value) && $value != 'No') {
87					break;
88				}
89			}
90		}
91
92		if ($this->getOption('type') === 'date' || $this->getOption('type') == 'expire') {
93			if (! empty($value)) {
94				return TikiLib::lib('tiki')->get_short_date($value);
95			}
96		} else {
97			return tra($value);
98		}
99	}
100}
101