ContactController::searchLocation()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
rs 9.2088
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 5.0073
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\IRequest;
28
29
class ContactController extends Controller {
30
31
	/**
32
	 * API for contacts api
33
	 * @var IManager
34
	 */
35
	private $contacts;
36
37
38
	/**
39
	 * @param string $appName
40
	 * @param IRequest $request an instance of the request
41
	 * @param IManager $contacts
42
	 */
43 2
	public function __construct($appName, IRequest $request, IManager $contacts) {
44 2
		parent::__construct($appName, $request);
45 2
		$this->contacts = $contacts;
46 2
	}
47
48
49
	/**
50
	 * @param string $location
51
	 * @return JSONResponse
52
	 *
53
	 * @NoAdminRequired
54
	 */
55 1
	public function searchLocation($location) {
56 1
		$result = $this->contacts->search($location, ['FN', 'ADR']);
57
58 1
		$contacts = [];
59 1
		foreach ($result as $r) {
60 1
			if (!isset($r['ADR'])) {
61 1
				continue;
62
			}
63
64 1
			$name = $this->getNameFromContact($r);
65 1
			if (is_string($r['ADR'])) {
66
				$r['ADR'] = [$r['ADR']];
67
			}
68
69 1
			foreach ($r['ADR'] as $address) {
70 1
				$address = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
71 1
				$contacts[] = [
72 1
					'label' => $address,
73 1
					'name' => $name
74
				];
75
			}
76
		}
77
78 1
		return new JSONResponse($contacts);
79
	}
80
81
82
	/**
83
	 * @param string $search
84
	 * @return JSONResponse
85
	 *
86
	 * @NoAdminRequired
87
	 */
88 1
	public function searchAttendee($search) {
89 1
		$result = $this->contacts->search($search, ['FN', 'EMAIL']);
90
91 1
		$contacts = [];
92 1
		foreach ($result as $r) {
93 1
			if (!isset($r['EMAIL'])) {
94 1
				continue;
95
			}
96
97 1
			$name = $this->getNameFromContact($r);
98 1
			if (is_string($r['EMAIL'])) {
99
				$r['EMAIL'] = [$r['EMAIL']];
100
			}
101
102 1
			$contacts[] = [
103 1
					'email' => $r['EMAIL'],
104 1
					'name' => $name
105
			];
106
		}
107
108 1
		return new JSONResponse($contacts);
109
	}
110
111
112
	/**
113
	 * Extract name from an array containing a contact's information
114
	 *
115
	 * @param array $r
116
	 * @return string
117
	 */
118 2
	private function getNameFromContact(array $r) {
119 2
		$name = '';
120 2
		if (isset($r['FN'])) {
121 2
			$name = $r['FN'];
122
		}
123
124 2
		return $name;
125
	}
126
}
127