Merge pull request #8531 from PalanteJon/CRM-16888
[civicrm-core.git] / CRM / Contact / Form / Task / Map.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * This class provides the functionality to map
36 * the address for group of
37 * contacts.
38 */
39 class 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 *
45 * @var boolean
46 */
47 protected $_single = FALSE;
48
49 /**
50 * Build all the data structures needed to build the form.
51 */
52 public function preProcess() {
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);
66 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
67
68 $type = 'Contact';
69 if ($cid) {
70 $ids = array($cid);
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 /**
113 * Build the form object.
114 */
115 public function buildQuickForm() {
116 $this->addButtons(array(
117 array(
118 'type' => 'done',
119 'name' => ts('Done'),
120 'isDefault' => TRUE,
121 ),
122 )
123 );
124 }
125
126 /**
127 * Process the form after the input has been submitted and validated.
128 */
129 public function postProcess() {
130 }
131
132 /**
133 * Assign smarty variables to the template that will be used by google api to plot the contacts.
134 *
135 * @param array $ids
136 * @param int $locationId
137 * Location_id.
138 * @param CRM_Core_Page $page
139 * @param bool $addBreadCrumb
140 * @param string $type
141 */
142 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
143 $config = CRM_Core_Config::singleton();
144
145 CRM_Utils_System::setTitle(ts('Map Location(s)'));
146 $page->assign('query', 'CiviCRM Search Query');
147 $page->assign('mapProvider', $config->mapProvider);
148 $page->assign('mapKey', urlencode($config->mapAPIKey));
149 if ($type == 'Contact') {
150 $imageUrlOnly = FALSE;
151
152 // google needs image url, CRM-6564
153 if ($config->mapProvider == 'Google' || $config->mapProvider == 'OpenStreetMaps') {
154 $imageUrlOnly = TRUE;
155 }
156 $locations = CRM_Contact_BAO_Contact_Location::getMapInfo($ids, $locationId, $imageUrlOnly);
157 }
158 else {
159 $locations = CRM_Event_BAO_Event::getMapInfo($ids);
160 }
161
162 if (empty($locations)) {
163 CRM_Core_Error::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
164 }
165
166 if (empty($config->mapProvider)) {
167 CRM_Core_Error::statusBounce(ts('You need to configure a Mapping Provider before using this feature (Administer > System Settings > Mapping and Geocoding).'));
168 }
169 if ($addBreadCrumb) {
170 $session = CRM_Core_Session::singleton();
171 $redirect = $session->readUserContext();
172 if ($type == 'Contact') {
173 $bcTitle = ts('Contact');
174 }
175 else {
176 $bcTitle = ts('Event Info');
177 $action = CRM_Utils_Request::retrieve('action', 'String',
178 $page, FALSE
179 );
180 if ($action) {
181 $args = 'reset=1&action=preview&id=';
182 }
183 else {
184 $args = 'reset=1&id=';
185 }
186 $session->pushUserContext(CRM_Utils_System::url('civicrm/event/info', "{$args}{$ids}"));
187 }
188 CRM_Utils_System::appendBreadCrumb($bcTitle, $redirect);
189 }
190
191 $page->assign_by_ref('locations', $locations);
192
193 // only issue a javascript warning if we know we will not
194 // mess the poor user with too many warnings
195 if (count($locations) <= 3) {
196 $page->assign('geoCodeWarn', TRUE);
197 }
198 else {
199 $page->assign('geoCodeWarn', FALSE);
200 }
201
202 $sumLat = $sumLng = 0;
203 $maxLat = $maxLng = -400;
204 $minLat = $minLng = 400;
205 foreach ($locations as $location) {
206 $sumLat += $location['lat'];
207 $sumLng += $location['lng'];
208
209 if ($location['lat'] > $maxLat) {
210 $maxLat = $location['lat'];
211 }
212 if ($location['lat'] < $minLat) {
213 $minLat = $location['lat'];
214 }
215
216 if ($location['lng'] > $maxLng) {
217 $maxLng = $location['lng'];
218 }
219 if ($location['lng'] < $minLng) {
220 $minLng = $location['lng'];
221 }
222 }
223
224 $center = array(
225 'lat' => (float ) $sumLat / count($locations),
226 'lng' => (float ) $sumLng / count($locations),
227 );
228 $span = array(
229 'lat' => (float ) ($maxLat - $minLat),
230 'lng' => (float ) ($maxLng - $minLng),
231 );
232 $page->assign_by_ref('center', $center);
233 $page->assign_by_ref('span', $span);
234 }
235
236 }