Merge pull request #15024 from civicrm/5.17
[civicrm-core.git] / CRM / Contact / Form / Task / Map.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * This class provides the functionality to map
36 * the address for group of
37 * contacts.
38 */
39class CRM_Contact_Form_Task_Map extends CRM_Contact_Form_Task {
40
41 /**
42 * Are we operating in "single mode", i.e. mapping address to one
43 * specific contact?
44 *
d51c6add 45 * @var bool
6a488035
TO
46 */
47 protected $_single = FALSE;
48
49 /**
fe482240 50 * Build all the data structures needed to build the form.
6a488035 51 */
00be9182 52 public function preProcess() {
6a488035
TO
53 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
54 $this, FALSE
55 );
56 $lid = CRM_Utils_Request::retrieve('lid', 'Positive',
57 $this, FALSE
58 );
59 $eid = CRM_Utils_Request::retrieve('eid', 'Positive',
60 $this, FALSE
61 );
62 $profileGID = CRM_Utils_Request::retrieve('profileGID', 'Integer',
63 $this, FALSE
64 );
65 $this->assign('profileGID', $profileGID);
edc80cda 66 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
67
68 $type = 'Contact';
69 if ($cid) {
be2fb01f 70 $ids = [$cid];
6a488035
TO
71 $this->_single = TRUE;
72 if ($profileGID) {
73 // this does a check and ensures that the user has permission on this profile
74 // CRM-11766
75 $profileIDs = CRM_Profile_Page_Listings::getProfileContact($profileGID);
76 if (!in_array($cid, $profileIDs)) {
77 CRM_Core_Error::fatal();
78 }
79 }
80 elseif ($context) {
81 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
82 $urlParams = 'force=1';
83 if (CRM_Utils_Rule::qfKey($qfKey)) {
84 $urlParams .= "&qfKey=$qfKey";
85 }
86 $session = CRM_Core_Session::singleton();
87 $urlString = "civicrm/contact/search/$context";
88 if ($context == 'search') {
89 $urlString = 'civicrm/contact/search';
90 }
91 $url = CRM_Utils_System::url($urlString, $urlParams);
92 $session->replaceUserContext($url);
93 }
94 }
95 elseif ($eid) {
96 $ids = $eid;
97 $type = 'Event';
98 }
99 else {
100 if ($profileGID) {
101 $ids = CRM_Profile_Page_Listings::getProfileContact($profileGID);
102 }
103 else {
104 parent::preProcess();
105 $ids = $this->_contactIds;
106 }
107 }
108 self::createMapXML($ids, $lid, $this, TRUE, $type);
109 $this->assign('single', $this->_single);
110 }
111
112 /**
fe482240 113 * Build the form object.
6a488035
TO
114 */
115 public function buildQuickForm() {
be2fb01f 116 $this->addButtons([
69078420
SL
117 [
118 'type' => 'done',
119 'name' => ts('Done'),
120 'isDefault' => TRUE,
121 ],
122 ]);
6a488035
TO
123 }
124
125 /**
fe482240 126 * Process the form after the input has been submitted and validated.
6a488035 127 */
6ea503d4
TO
128 public function postProcess() {
129 }
6a488035
TO
130
131 /**
fe482240 132 * Assign smarty variables to the template that will be used by google api to plot the contacts.
6a488035 133 *
00252851 134 * @param array $ids
77c5b619
TO
135 * @param int $locationId
136 * Location_id.
00252851 137 * @param CRM_Core_Page $page
138 * @param bool $addBreadCrumb
77b97be7 139 * @param string $type
6a488035 140 */
00be9182 141 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
6a488035
TO
142 $config = CRM_Core_Config::singleton();
143
144 CRM_Utils_System::setTitle(ts('Map Location(s)'));
145 $page->assign('query', 'CiviCRM Search Query');
146 $page->assign('mapProvider', $config->mapProvider);
147 $page->assign('mapKey', urlencode($config->mapAPIKey));
148 if ($type == 'Contact') {
149 $imageUrlOnly = FALSE;
150
151 // google needs image url, CRM-6564
152 if ($config->mapProvider == 'Google' || $config->mapProvider == 'OpenStreetMaps') {
153 $imageUrlOnly = TRUE;
154 }
155 $locations = CRM_Contact_BAO_Contact_Location::getMapInfo($ids, $locationId, $imageUrlOnly);
156 }
157 else {
158 $locations = CRM_Event_BAO_Event::getMapInfo($ids);
159 }
160
161 if (empty($locations)) {
162 CRM_Core_Error::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
163 }
164
c3c3e099
DG
165 if (empty($config->mapProvider)) {
166 CRM_Core_Error::statusBounce(ts('You need to configure a Mapping Provider before using this feature (Administer > System Settings > Mapping and Geocoding).'));
167 }
6a488035
TO
168 if ($addBreadCrumb) {
169 $session = CRM_Core_Session::singleton();
170 $redirect = $session->readUserContext();
171 if ($type == 'Contact') {
172 $bcTitle = ts('Contact');
173 }
174 else {
175 $bcTitle = ts('Event Info');
176 $action = CRM_Utils_Request::retrieve('action', 'String',
177 $page, FALSE
178 );
179 if ($action) {
180 $args = 'reset=1&action=preview&id=';
181 }
182 else {
183 $args = 'reset=1&id=';
184 }
185 $session->pushUserContext(CRM_Utils_System::url('civicrm/event/info', "{$args}{$ids}"));
186 }
187 CRM_Utils_System::appendBreadCrumb($bcTitle, $redirect);
188 }
189
190 $page->assign_by_ref('locations', $locations);
191
192 // only issue a javascript warning if we know we will not
193 // mess the poor user with too many warnings
194 if (count($locations) <= 3) {
195 $page->assign('geoCodeWarn', TRUE);
196 }
197 else {
198 $page->assign('geoCodeWarn', FALSE);
199 }
200
201 $sumLat = $sumLng = 0;
202 $maxLat = $maxLng = -400;
353ffa53 203 $minLat = $minLng = 400;
6a488035
TO
204 foreach ($locations as $location) {
205 $sumLat += $location['lat'];
206 $sumLng += $location['lng'];
207
208 if ($location['lat'] > $maxLat) {
209 $maxLat = $location['lat'];
210 }
211 if ($location['lat'] < $minLat) {
212 $minLat = $location['lat'];
213 }
214
215 if ($location['lng'] > $maxLng) {
216 $maxLng = $location['lng'];
217 }
218 if ($location['lng'] < $minLng) {
219 $minLng = $location['lng'];
220 }
221 }
222
be2fb01f 223 $center = [
6a488035
TO
224 'lat' => (float ) $sumLat / count($locations),
225 'lng' => (float ) $sumLng / count($locations),
be2fb01f
CW
226 ];
227 $span = [
353ffa53
TO
228 'lat' => (float ) ($maxLat - $minLat),
229 'lng' => (float ) ($maxLng - $minLng),
be2fb01f 230 ];
6a488035
TO
231 $page->assign_by_ref('center', $center);
232 $page->assign_by_ref('span', $span);
233 }
96025800 234
6a488035 235}