3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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', 'String', $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() {
116 $this->addButtons(array(
119 'name' => ts('Done'),
127 * Process the form after the input has been submitted and validated.
129 public function postProcess() {
133 * Assign smarty variables to the template that will be used by google api to plot the contacts.
136 * @param int $locationId
138 * @param CRM_Core_Page $page
139 * @param bool $addBreadCrumb
140 * @param string $type
142 public static function createMapXML($ids, $locationId, &$page, $addBreadCrumb, $type = 'Contact') {
143 $config = CRM_Core_Config
::singleton();
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;
152 // google needs image url, CRM-6564
153 if ($config->mapProvider
== 'Google' ||
$config->mapProvider
== 'OpenStreetMaps') {
154 $imageUrlOnly = TRUE;
156 $locations = CRM_Contact_BAO_Contact_Location
::getMapInfo($ids, $locationId, $imageUrlOnly);
159 $locations = CRM_Event_BAO_Event
::getMapInfo($ids);
162 if (empty($locations)) {
163 CRM_Core_Error
::statusBounce(ts('This address does not contain latitude/longitude information and cannot be mapped.'));
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).'));
169 if ($addBreadCrumb) {
170 $session = CRM_Core_Session
::singleton();
171 $redirect = $session->readUserContext();
172 if ($type == 'Contact') {
173 $bcTitle = ts('Contact');
176 $bcTitle = ts('Event Info');
177 $action = CRM_Utils_Request
::retrieve('action', 'String',
181 $args = 'reset=1&action=preview&id=';
184 $args = 'reset=1&id=';
186 $session->pushUserContext(CRM_Utils_System
::url('civicrm/event/info', "{$args}{$ids}"));
188 CRM_Utils_System
::appendBreadCrumb($bcTitle, $redirect);
191 $page->assign_by_ref('locations', $locations);
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);
199 $page->assign('geoCodeWarn', FALSE);
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'];
209 if ($location['lat'] > $maxLat) {
210 $maxLat = $location['lat'];
212 if ($location['lat'] < $minLat) {
213 $minLat = $location['lat'];
216 if ($location['lng'] > $maxLng) {
217 $maxLng = $location['lng'];
219 if ($location['lng'] < $minLng) {
220 $minLng = $location['lng'];
225 'lat' => (float ) $sumLat / count($locations),
226 'lng' => (float ) $sumLng / count($locations),
229 'lat' => (float ) ($maxLat - $minLat),
230 'lng' => (float ) ($maxLng - $minLng),
232 $page->assign_by_ref('center', $center);
233 $page->assign_by_ref('span', $span);