3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to map
20 * the address for group of
23 class CRM_Contact_Form_Task_Map
extends CRM_Contact_Form_Task
{
26 * Are we operating in "single mode", i.e. mapping address to one
31 protected $_single = FALSE;
34 * Build all the data structures needed to build the form.
36 public function preProcess() {
37 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
40 $lid = CRM_Utils_Request
::retrieve('lid', 'Positive',
43 $eid = CRM_Utils_Request
::retrieve('eid', 'Positive',
46 $profileGID = CRM_Utils_Request
::retrieve('profileGID', 'Integer',
49 $this->assign('profileGID', $profileGID);
50 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
55 $this->_single
= TRUE;
57 // this does a check and ensures that the user has permission on this profile
59 $profileIDs = CRM_Profile_Page_Listings
::getProfileContact($profileGID);
60 if (!in_array($cid, $profileIDs)) {
61 CRM_Core_Error
::statusBounce(ts('Contact not found when building list of contacts in the profile'));
65 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
66 $urlParams = 'force=1';
67 if (CRM_Utils_Rule
::qfKey($qfKey)) {
68 $urlParams .= "&qfKey=$qfKey";
70 $session = CRM_Core_Session
::singleton();
71 $urlString = "civicrm/contact/search/$context";
72 if ($context == 'search') {
73 $urlString = 'civicrm/contact/search';
75 $url = CRM_Utils_System
::url($urlString, $urlParams);
76 $session->replaceUserContext($url);
85 $ids = CRM_Profile_Page_Listings
::getProfileContact($profileGID);
89 $ids = $this->_contactIds
;
92 self
::createMapXML($ids, $lid, $this, TRUE, $type);
93 $this->assign('single', $this->_single
);
97 * Build the form object.
99 public function buildQuickForm() {
103 'name' => ts('Done'),
110 * Process the form after the input has been submitted and validated.
112 public function postProcess() {
116 * Assign smarty variables to the template that will be used by google api to plot the contacts.
119 * @param int $locationId
121 * @param CRM_Core_Page $page
122 * @param bool $addBreadCrumb
123 * @param string $type
125 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
126 $config = CRM_Core_Config
::singleton();
128 CRM_Utils_System
::setTitle(ts('Map Location(s)'));
129 $page->assign('query', 'CiviCRM Search Query');
130 $page->assign('mapProvider', $config->mapProvider
);
131 $page->assign('mapKey', urlencode($config->mapAPIKey
));
132 if ($type == 'Contact') {
133 $imageUrlOnly = FALSE;
135 // google needs image url, CRM-6564
136 if ($config->mapProvider
== 'Google' ||
$config->mapProvider
== 'OpenStreetMaps') {
137 $imageUrlOnly = TRUE;
139 $locations = CRM_Contact_BAO_Contact_Location
::getMapInfo($ids, $locationId, $imageUrlOnly);
142 $locations = CRM_Event_BAO_Event
::getMapInfo($ids);
145 if (empty($locations)) {
146 CRM_Core_Error
::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
149 if (empty($config->mapProvider
)) {
150 CRM_Core_Error
::statusBounce(ts('You need to configure a Mapping Provider before using this feature (Administer > System Settings > Mapping and Geocoding).'));
152 if ($addBreadCrumb) {
153 $session = CRM_Core_Session
::singleton();
154 $redirect = $session->readUserContext();
155 if ($type == 'Contact') {
156 $bcTitle = ts('Contact');
159 $bcTitle = ts('Event Info');
160 $action = CRM_Utils_Request
::retrieve('action', 'String',
164 $args = 'reset=1&action=preview&id=';
167 $args = 'reset=1&id=';
169 $session->pushUserContext(CRM_Utils_System
::url('civicrm/event/info', "{$args}{$ids}"));
171 CRM_Utils_System
::appendBreadCrumb($bcTitle, $redirect);
174 $page->assign_by_ref('locations', $locations);
176 // only issue a javascript warning if we know we will not
177 // mess the poor user with too many warnings
178 if (count($locations) <= 3) {
179 $page->assign('geoCodeWarn', TRUE);
182 $page->assign('geoCodeWarn', FALSE);
185 $sumLat = $sumLng = 0;
186 $maxLat = $maxLng = -400;
187 $minLat = $minLng = 400;
188 foreach ($locations as $location) {
189 $sumLat +
= $location['lat'];
190 $sumLng +
= $location['lng'];
192 if ($location['lat'] > $maxLat) {
193 $maxLat = $location['lat'];
195 if ($location['lat'] < $minLat) {
196 $minLat = $location['lat'];
199 if ($location['lng'] > $maxLng) {
200 $maxLng = $location['lng'];
202 if ($location['lng'] < $minLng) {
203 $minLng = $location['lng'];
208 'lat' => (float ) $sumLat / count($locations),
209 'lng' => (float ) $sumLng / count($locations),
212 'lat' => (float ) ($maxLat - $minLat),
213 'lng' => (float ) ($maxLng - $minLng),
215 $page->assign_by_ref('center', $center);
216 $page->assign_by_ref('span', $span);