3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class provides the functionality to map
36 * the address for group of
39 class CRM_Contact_Form_Task_Map
extends CRM_Contact_Form_Task
{
42 * Are we operating in "single mode", i.e. mapping address to one
47 protected $_single = FALSE;
50 * Build all the data structures needed to build the form.
52 public function preProcess() {
53 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
56 $lid = CRM_Utils_Request
::retrieve('lid', 'Positive',
59 $eid = CRM_Utils_Request
::retrieve('eid', 'Positive',
62 $profileGID = CRM_Utils_Request
::retrieve('profileGID', 'Integer',
65 $this->assign('profileGID', $profileGID);
66 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
71 $this->_single
= TRUE;
73 // this does a check and ensures that the user has permission on this profile
75 $profileIDs = CRM_Profile_Page_Listings
::getProfileContact($profileGID);
76 if (!in_array($cid, $profileIDs)) {
77 CRM_Core_Error
::fatal();
81 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
82 $urlParams = 'force=1';
83 if (CRM_Utils_Rule
::qfKey($qfKey)) {
84 $urlParams .= "&qfKey=$qfKey";
86 $session = CRM_Core_Session
::singleton();
87 $urlString = "civicrm/contact/search/$context";
88 if ($context == 'search') {
89 $urlString = 'civicrm/contact/search';
91 $url = CRM_Utils_System
::url($urlString, $urlParams);
92 $session->replaceUserContext($url);
101 $ids = CRM_Profile_Page_Listings
::getProfileContact($profileGID);
104 parent
::preProcess();
105 $ids = $this->_contactIds
;
108 self
::createMapXML($ids, $lid, $this, TRUE, $type);
109 $this->assign('single', $this->_single
);
113 * Build the form object.
115 public function buildQuickForm() {
119 'name' => ts('Done'),
126 * Process the form after the input has been submitted and validated.
128 public function postProcess() {
132 * Assign smarty variables to the template that will be used by google api to plot the contacts.
135 * @param int $locationId
137 * @param CRM_Core_Page $page
138 * @param bool $addBreadCrumb
139 * @param string $type
141 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
142 $config = CRM_Core_Config
::singleton();
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;
151 // google needs image url, CRM-6564
152 if ($config->mapProvider
== 'Google' ||
$config->mapProvider
== 'OpenStreetMaps') {
153 $imageUrlOnly = TRUE;
155 $locations = CRM_Contact_BAO_Contact_Location
::getMapInfo($ids, $locationId, $imageUrlOnly);
158 $locations = CRM_Event_BAO_Event
::getMapInfo($ids);
161 if (empty($locations)) {
162 CRM_Core_Error
::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
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).'));
168 if ($addBreadCrumb) {
169 $session = CRM_Core_Session
::singleton();
170 $redirect = $session->readUserContext();
171 if ($type == 'Contact') {
172 $bcTitle = ts('Contact');
175 $bcTitle = ts('Event Info');
176 $action = CRM_Utils_Request
::retrieve('action', 'String',
180 $args = 'reset=1&action=preview&id=';
183 $args = 'reset=1&id=';
185 $session->pushUserContext(CRM_Utils_System
::url('civicrm/event/info', "{$args}{$ids}"));
187 CRM_Utils_System
::appendBreadCrumb($bcTitle, $redirect);
190 $page->assign_by_ref('locations', $locations);
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);
198 $page->assign('geoCodeWarn', FALSE);
201 $sumLat = $sumLng = 0;
202 $maxLat = $maxLng = -400;
203 $minLat = $minLng = 400;
204 foreach ($locations as $location) {
205 $sumLat +
= $location['lat'];
206 $sumLng +
= $location['lng'];
208 if ($location['lat'] > $maxLat) {
209 $maxLat = $location['lat'];
211 if ($location['lat'] < $minLat) {
212 $minLat = $location['lat'];
215 if ($location['lng'] > $maxLng) {
216 $maxLng = $location['lng'];
218 if ($location['lng'] < $minLng) {
219 $minLng = $location['lng'];
224 'lat' => (float ) $sumLat / count($locations),
225 'lng' => (float ) $sumLng / count($locations),
228 'lat' => (float ) ($maxLat - $minLat),
229 'lng' => (float ) ($maxLng - $minLng),
231 $page->assign_by_ref('center', $center);
232 $page->assign_by_ref('span', $span);