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_User_FavoriteController
9{
10	function setUp()
11	{
12		global $prefs;
13
14		if ($prefs['user_favorites'] !== 'y') {
15			throw new Services_Exception(tr('Feature disabled'), 403);
16		}
17	}
18
19	function action_list($input)
20	{
21		global $user;
22
23		if (! $user) {
24			return [];
25		}
26
27		$relationlib = TikiLib::lib('relation');
28		$favorites = [];
29		foreach ($relationlib->get_relations_from('user', $user, 'tiki.user.favorite') as $relation) {
30			$favorites[$relation['relationId']] = $relation['type'] . ':' . $relation['itemId'];
31		}
32
33		return $favorites;
34	}
35
36	function action_toggle($input)
37	{
38		global $user;
39
40		if (! $user) {
41			throw new Services_Exception(tr('Must be authenticated'), 403);
42		}
43
44		$type = $input->type->none();
45		$object = $input->object->none();
46		$target = $input->target->int();
47
48		if (! $type || ! $object) {
49			throw new Services_Exception(tr('Invalid input'), 400);
50		}
51
52		$relationlib = TikiLib::lib('relation');
53
54		$tx = TikiDb::get()->begin();
55
56		$relations = $this->action_list($input);
57		$relationId = $this->getCurrentRelation($relations, $user, $type, $object);
58
59		if ($type == 'trackeritem') {
60			$parentobject = TikiLib::lib('trk')->get_tracker_for_item($object);
61		} else {
62			$parentobject = 'not implemented';
63		}
64
65		if ($target) {
66			if (! $relationId) {
67				$relationId = $relationlib->add_relation('tiki.user.favorite', 'user', $user, $type, $object);
68				$relations[$relationId] = "$type:$object";
69
70				$item_user = $this->getItemUser($type, $object);
71
72				TikiLib::events()->trigger(
73					'tiki.social.favorite.add',
74					[
75						'type' => $type,
76						'object' => $object,
77						'parentobject' => $parentobject,
78						'user' => $user,
79						'item_user' => $item_user,
80					]
81				);
82			}
83		} else {
84			if ($relationId) {
85				$relationlib->remove_relation($relationId);
86				unset($relations[$relationId]);
87				TikiLib::events()->trigger(
88					'tiki.social.favorite.remove',
89					[
90						'type' => $type,
91						'object' => $object,
92						'parentobject' => $parentobject,
93						'user' => $user,
94					]
95				);
96			}
97		}
98
99		$tx->commit();
100
101		return [
102			'list' => $relations,
103		];
104	}
105
106	private function getCurrentRelation($relations, $user, $type, $object)
107	{
108		foreach ($relations as $id => $key) {
109			if ($key === "$type:$object") {
110				return $id;
111			}
112		}
113	}
114
115	private function getItemUser($type, $object)
116	{
117		global $user;
118
119		$item_user = null;
120
121		if ($type == 'forum post') {
122			$commentslib = TikiLib::lib('comments');
123			$forum_id = $commentslib->get_comment_forum_id($object);
124			$forum_info = $commentslib->get_forum($forum_id);
125			$thread_info = $commentslib->get_comment($object, null, $forum_info);
126			$item_user = $thread_info['userName'];
127		} elseif ($type == 'article') {
128			$artlib = TikiLib::lib('art');
129			$res = $artlib->get_article($object);
130			$item_user = $res['author'];
131		}
132
133		return $item_user;
134	}
135}
136