Completed
Push — fix_untranslated_strings ( 07522f...8348da )
by
unknown
17:53
created

ContactController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
/**
3
 * ownCloud - 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
	public function __construct($appName, IRequest $request, IManager $contacts) {
44
		parent::__construct($appName, $request);
45
		$this->contacts = $contacts;
46
	}
47
48
49
	/**
50
	 * @param string $location
51
	 * @return JSONResponse
52
	 *
53
	 * @NoAdminRequired
54
	 * @NoCSRFRequired
55
	 */
56
	public function searchLocation($location) {
57
		$result = $this->contacts->search($location, ['FN', 'ADR']);
58
59
		$contacts = [];
60
		foreach ($result as $r) {
61
			if (!isset($r['ADR'])) {
62
				continue;
63
			}
64
65
			$name = $this->getNameFromContact($r);
66
			if (is_string($r['ADR'])) {
67
				$r['ADR'] = [$r['ADR']];
68
			}
69
70
			foreach ($r['ADR'] as $address) {
71
				$address = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
72
				$contacts[] = [
73
					'label' => $address,
74
					'name' => $name
75
				];
76
			}
77
		}
78
79
		return new JSONResponse($contacts);
80
	}
81
82
83
	/**
84
	 * @param string $search
85
	 * @return JSONResponse
86
	 *
87
	 * @NoAdminRequired
88
	 * @NoCSRFRequired
89
	 */
90
	public function searchAttendee($search) {
91
		$result = $this->contacts->search($search, ['FN', 'EMAIL']);
92
93
		$contacts = [];
94
		foreach ($result as $r) {
95
			if (!isset($r['EMAIL'])) {
96
				continue;
97
			}
98
99
			$name = $this->getNameFromContact($r);
100
			if (is_string($r['EMAIL'])) {
101
				$r['EMAIL'] = [$r['EMAIL']];
102
			}
103
104
			foreach ($r['EMAIL'] as $email) {
105
				$contacts[] = [
106
					'email' => $email,
107
					'name' => $name
108
				];
109
			}
110
		}
111
112
		return new JSONResponse($contacts);
113
	}
114
115
116
	/**
117
	 * Extract name from an array containing a contact's information
118
	 *
119
	 * @param array $r
120
	 * @return string
121
	 */
122
	private function getNameFromContact(array $r) {
123
		$name = '';
124
		if (isset($r['FN'])) {
125
			$name = $r['FN'];
126
		}
127
128
		return $name;
129
	}
130
}