Completed
Pull Request — master (#565)
by
unknown
17:29
created

ContactController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
cc 1
eloc 9
nc 1
nop 5
crap 1.0028
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\Contacts\IManager;
27
use OCP\IConfig;
28
use OCP\IRequest;
29
use OCP\L10N\IFactory;
30
31
class ContactController extends Controller {
32
33
	/**
34
	 * API for contacts api
35
	 * @var IManager
36
	 */
37
	private $contacts;
38
39
	/**
40
	 * @var IConfig
41
	 */
42
	private $config;
43 2
44 2
	/**
45 2
	 * @var IFactory
46 2
	 */
47
	private $l10nFactory;
48
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request an instance of the request
53
	 * @param IManager $contacts
54
	 * @param IConfig $config
55 1
	 * @param IFactory $l10nFactory
56 1
	 */
57
	public function __construct($appName,
58 1
								IRequest $request,
59 1
								IManager $contacts,
60 1
								IConfig $config,
61 1
								IFactory $l10nFactory) {
62
		parent::__construct($appName, $request);
63
		$this->contacts = $contacts;
64 1
		$this->config = $config;
65 1
		$this->l10nFactory = $l10nFactory;
66
	}
67
68
69 1
	/**
70 1
	 * @param string $location
71 1
	 * @return JSONResponse
72 1
	 *
73
	 * @NoAdminRequired
74 1
	 */
75 1
	public function searchLocation($location) {
76 1
		$result = $this->contacts->search($location, ['FN', 'ADR']);
77
78 1
		$contacts = [];
79
		foreach ($result as $r) {
80
			if (!isset($r['ADR'])) {
81
				continue;
82
			}
83
84
			$name = $this->getNameFromContact($r);
85
			if (is_string($r['ADR'])) {
86
				$r['ADR'] = [$r['ADR']];
87
			}
88 1
89 1
			foreach ($r['ADR'] as $address) {
90
				$address = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
91 1
				$contacts[] = [
92 1
					'label' => $address,
93 1
					'name' => $name
94 1
				];
95
			}
96
		}
97 1
98 1
		return new JSONResponse($contacts);
99
	}
100
101
102 1
	/**
103 1
	 * @param string $search
104
	 * @return JSONResponse
105 1
	 *
106 1
	 * @NoAdminRequired
107
	 */
108 1
	public function searchAttendee($search) {
109
		$result = $this->contacts->search($search, ['FN', 'EMAIL']);
110
111
		$defaultLang = $this->l10nFactory->findLanguage();
112
		$contacts = [];
113
		foreach ($result as $r) {
114
			if (!isset($r['EMAIL'])) {
115
				continue;
116
			}
117
118 2
			$name = $this->getNameFromContact($r);
119 2
			if (is_string($r['EMAIL'])) {
120 2
				$r['EMAIL'] = [$r['EMAIL']];
121 2
			}
122 2
123
			$lang = $this->getPreferredLanguageFromUidOrDefault($r['UID'], $defaultLang);
124 2
125
			$contacts[] = [
126
					'email' => $r['EMAIL'],
127
					'name' => $name,
128
					'lang' => $lang
129
			];
130
		}
131
132
		return new JSONResponse($contacts);
133
	}
134
135
136
	/**
137
	 * Extract name from an array containing a contact's information
138
	 *
139
	 * @param array $r
140
	 * @return string
141
	 */
142
	private function getNameFromContact(array $r) {
143
		$name = '';
144
		if (isset($r['FN'])) {
145
			$name = $r['FN'];
146
		}
147
148
		return $name;
149
	}
150
151
	/**
152
	 * Returns the preferred language of a given user uid or $default instead
153
	 *
154
	 * @param string $uid
155
	 * @param string $default
156
	 * @return string
157
	 */
158
	private function getPreferredLanguageFromUidOrDefault($uid, $default) {
159
		return $this->config->getUserValue($uid, 'core', 'lang', $default);
160
	}
161
}
162