Merge pull request #5429 from atif-shaikh/CRM-16114
[civicrm-core.git] / CRM / Contact / Form / Task / Map.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class provides the functionality to map
38 * the address for group of
39 * contacts.
40 */
41class CRM_Contact_Form_Task_Map extends CRM_Contact_Form_Task {
42
43 /**
44 * Are we operating in "single mode", i.e. mapping address to one
45 * specific contact?
46 *
47 * @var boolean
48 */
49 protected $_single = FALSE;
50
51 /**
fe482240 52 * Build all the data structures needed to build the form.
6a488035
TO
53 *
54 * @return void
6a488035 55 */
00be9182 56 public function preProcess() {
6a488035
TO
57 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
58 $this, FALSE
59 );
60 $lid = CRM_Utils_Request::retrieve('lid', 'Positive',
61 $this, FALSE
62 );
63 $eid = CRM_Utils_Request::retrieve('eid', 'Positive',
64 $this, FALSE
65 );
66 $profileGID = CRM_Utils_Request::retrieve('profileGID', 'Integer',
67 $this, FALSE
68 );
69 $this->assign('profileGID', $profileGID);
70 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
71
72 $type = 'Contact';
73 if ($cid) {
74 $ids = array($cid);
75 $this->_single = TRUE;
76 if ($profileGID) {
77 // this does a check and ensures that the user has permission on this profile
78 // CRM-11766
79 $profileIDs = CRM_Profile_Page_Listings::getProfileContact($profileGID);
80 if (!in_array($cid, $profileIDs)) {
81 CRM_Core_Error::fatal();
82 }
83 }
84 elseif ($context) {
85 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
86 $urlParams = 'force=1';
87 if (CRM_Utils_Rule::qfKey($qfKey)) {
88 $urlParams .= "&qfKey=$qfKey";
89 }
90 $session = CRM_Core_Session::singleton();
91 $urlString = "civicrm/contact/search/$context";
92 if ($context == 'search') {
93 $urlString = 'civicrm/contact/search';
94 }
95 $url = CRM_Utils_System::url($urlString, $urlParams);
96 $session->replaceUserContext($url);
97 }
98 }
99 elseif ($eid) {
100 $ids = $eid;
101 $type = 'Event';
102 }
103 else {
104 if ($profileGID) {
105 $ids = CRM_Profile_Page_Listings::getProfileContact($profileGID);
106 }
107 else {
108 parent::preProcess();
109 $ids = $this->_contactIds;
110 }
111 }
112 self::createMapXML($ids, $lid, $this, TRUE, $type);
113 $this->assign('single', $this->_single);
114 }
115
116 /**
fe482240 117 * Build the form object.
6a488035 118 *
6a488035
TO
119 *
120 * @return void
121 */
122 public function buildQuickForm() {
123 $this->addButtons(array(
124 array(
125 'type' => 'done',
126 'name' => ts('Done'),
127 'isDefault' => TRUE,
128 ),
129 )
130 );
131 }
132
133 /**
fe482240 134 * Process the form after the input has been submitted and validated.
6a488035 135 *
6a488035 136 *
355ba699 137 * @return void
6a488035 138 */
6ea503d4
TO
139 public function postProcess() {
140 }
6a488035
TO
141
142 /**
fe482240 143 * Assign smarty variables to the template that will be used by google api to plot the contacts.
6a488035 144 *
77b97be7 145 * @param $ids
77c5b619
TO
146 * @param int $locationId
147 * Location_id.
77b97be7
EM
148 * @param $page
149 * @param $addBreadCrumb
150 * @param string $type
151 *
0059fee7 152 * @return void
a6c01b45 153 * the location of the file we have created
6a488035 154 */
00be9182 155 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
6a488035
TO
156 $config = CRM_Core_Config::singleton();
157
158 CRM_Utils_System::setTitle(ts('Map Location(s)'));
159 $page->assign('query', 'CiviCRM Search Query');
160 $page->assign('mapProvider', $config->mapProvider);
161 $page->assign('mapKey', urlencode($config->mapAPIKey));
162 if ($type == 'Contact') {
163 $imageUrlOnly = FALSE;
164
165 // google needs image url, CRM-6564
166 if ($config->mapProvider == 'Google' || $config->mapProvider == 'OpenStreetMaps') {
167 $imageUrlOnly = TRUE;
168 }
169 $locations = CRM_Contact_BAO_Contact_Location::getMapInfo($ids, $locationId, $imageUrlOnly);
170 }
171 else {
172 $locations = CRM_Event_BAO_Event::getMapInfo($ids);
173 }
174
175 if (empty($locations)) {
176 CRM_Core_Error::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
177 }
178
179 if ($addBreadCrumb) {
180 $session = CRM_Core_Session::singleton();
181 $redirect = $session->readUserContext();
182 if ($type == 'Contact') {
183 $bcTitle = ts('Contact');
184 }
185 else {
186 $bcTitle = ts('Event Info');
187 $action = CRM_Utils_Request::retrieve('action', 'String',
188 $page, FALSE
189 );
190 if ($action) {
191 $args = 'reset=1&action=preview&id=';
192 }
193 else {
194 $args = 'reset=1&id=';
195 }
196 $session->pushUserContext(CRM_Utils_System::url('civicrm/event/info', "{$args}{$ids}"));
197 }
198 CRM_Utils_System::appendBreadCrumb($bcTitle, $redirect);
199 }
200
201 $page->assign_by_ref('locations', $locations);
202
203 // only issue a javascript warning if we know we will not
204 // mess the poor user with too many warnings
205 if (count($locations) <= 3) {
206 $page->assign('geoCodeWarn', TRUE);
207 }
208 else {
209 $page->assign('geoCodeWarn', FALSE);
210 }
211
212 $sumLat = $sumLng = 0;
213 $maxLat = $maxLng = -400;
353ffa53 214 $minLat = $minLng = 400;
6a488035
TO
215 foreach ($locations as $location) {
216 $sumLat += $location['lat'];
217 $sumLng += $location['lng'];
218
219 if ($location['lat'] > $maxLat) {
220 $maxLat = $location['lat'];
221 }
222 if ($location['lat'] < $minLat) {
223 $minLat = $location['lat'];
224 }
225
226 if ($location['lng'] > $maxLng) {
227 $maxLng = $location['lng'];
228 }
229 if ($location['lng'] < $minLng) {
230 $minLng = $location['lng'];
231 }
232 }
233
234 $center = array(
235 'lat' => (float ) $sumLat / count($locations),
236 'lng' => (float ) $sumLng / count($locations),
237 );
238 $span = array(
353ffa53
TO
239 'lat' => (float ) ($maxLat - $minLat),
240 'lng' => (float ) ($maxLng - $minLng),
6a488035
TO
241 );
242 $page->assign_by_ref('center', $center);
243 $page->assign_by_ref('span', $span);
244 }
96025800 245
6a488035 246}