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
8function wikiplugin_friend_info()
9{
10	return [
11		'name' => tra('Friend'),
12		'documentation' => 'PluginFriend',
13		'description' => tra('Friend and unfriend other users'),
14		'prefs' => ['wikiplugin_friend', 'feature_search'],
15		'format' => 'html',
16		'body' => tra('Add or Remove Friend Button'),
17		'filter' => 'int',
18		'introduced' => 15,
19		'iconname' => 'group',
20		'params' => [
21			'other_user' => [
22				'required' => false,
23				'name' => tra('Other User'),
24				'description' => tra("The user you wish to change relations with."),
25				'since' => '15.0',
26				'filter' => 'text',
27				'default' => '',
28			],
29			'add_button_text' => [
30				'required' => false,
31				'name' => tra('Button Text'),
32				'description' => tra("Button text that's displayed when friending a user."),
33				'since' => '15.0',
34				'filter' => 'text',
35				'default' => '',
36			],
37			'remove_button_text' => [
38				'required' => false,
39				'name' => tra('Button Text'),
40				'description' => tra("Button text that's displayed when un-friending a user."),
41				'since' => '15.0',
42				'filter' => 'text',
43				'default' => '',
44			],
45		],
46	];
47}
48
49function wikiplugin_friend($data, $params)
50{
51	extract($params, EXTR_SKIP);
52	global $user;
53
54	if (empty($other_user)) {
55		return;
56	}
57	if (empty($add_button_text)) {
58		$add_button_text = tra("Add to Friend Network");
59	}
60	if (empty($remove_button_text)) {
61		$remove_button_text = tra("Remove from Friend Network");
62	}
63
64	$relationlib = Tikilib::lib('relation');
65	$is_friend = $relationlib->get_relation_id("tiki.friend.follow", "user", $user, "user", $other_user);
66
67	if ($is_friend) {
68		$action = 'remove';
69	} else {
70		$action = 'add';
71	}
72
73	$smarty = TikiLib::lib('smarty');
74	$smarty->assign('add_button_text', $add_button_text);
75	$smarty->assign('remove_button_text', $remove_button_text);
76	$smarty->assign('userwatch', $other_user);
77	$smarty->assign('action', $action);
78	return $smarty->fetch('wiki-plugins/wikiplugin_friend.tpl');
79}
80