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