Browse Source

Add social map that show ville and stage BT#15176

FIELD_TYPE_GEOLOCALIZATION now saves also the lat and long
from the address
Julio Montoya 6 years ago
parent
commit
f875486cd2

+ 15 - 4
main/inc/lib/extra_field.lib.php

@@ -2079,12 +2079,21 @@ class ExtraField extends Model
                             $field_details['display_text'],
                             ['id' => 'extra_'.$field_details['variable']]
                         );
+
+                        $form->addHidden(
+                            'extra_'.$field_details['variable'].'_coordinates',
+                            '',
+                            ['id' => 'extra_'.$field_details['variable'].'_coordinates']
+                        );
+
                         $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
                         $form->applyFilter('extra_'.$field_details['variable'], 'trim');
                         if ($freezeElement) {
                             $form->freeze('extra_'.$field_details['variable']);
                         }
 
+                        $dataValue = addslashes($dataValue);
+
                         $form->addHtml("
                             <script>
                                 $(document).ready(function() {
@@ -2166,9 +2175,11 @@ class ExtraField extends Model
                                             if (status == google.maps.GeocoderStatus.OK) {
                                                 if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                                                     map_{$field_details['variable']}.setCenter(results[0].geometry.location);
-                                                    if (!address) {
-                                                        $('#extra_{$field_details['variable']}').val(results[0].formatted_address);
-                                                    }
+                                                    //if (!address) {
+                                                        $('#extra_{$field_details['variable']}').val(results[0].formatted_address);                                                        
+                                                        $('#extra_{$field_details['variable']}_coordinates').val(
+                                                            results[0].geometry.location.lat()+','+results[0].geometry.location.lng());
+                                                    //}
                                                     var infowindow = new google.maps.InfoWindow({
                                                         content: '<b>' + $('#extra_{$field_details['variable']}').val() + '</b>',
                                                         size: new google.maps.Size(150, 50)
@@ -2203,7 +2214,7 @@ class ExtraField extends Model
                                         id="geolocalization_extra_'.$field_details['variable'].'"
                                         name="geolocalization_extra_'.$field_details['variable'].'"
                                         type="submit">
-                                        <em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').'
+                                        <em class="fa fa-map-marker"></em> '.get_lang('SearchGeolocalization').'
                                     </button>
                                     <button class="null btn btn-default" id="myLocation_extra_'.$field_details['variable'].'"
                                         name="myLocation_extra_'.$field_details['variable'].'"

+ 12 - 4
main/inc/lib/extra_field_value.lib.php

@@ -155,6 +155,18 @@ class ExtraFieldValue extends Model
             $dirPermissions = api_get_permissions_for_new_directories();
 
             switch ($extraFieldInfo['field_type']) {
+                case ExtraField::FIELD_TYPE_GEOLOCALIZATION:
+                    if (isset($params['extra_'.$extraFieldInfo['variable'].'_coordinates'])) {
+                        $value = $value.'::'.$params['extra_'.$extraFieldInfo['variable'].'_coordinates'];
+                    }
+                    $newParams = [
+                        'item_id' => $params['item_id'],
+                        'field_id' => $extraFieldInfo['id'],
+                        'value' => $value,
+                        'comment' => $comment,
+                    ];
+                    self::save($newParams, $showQuery);
+                    break;
                 case ExtraField::FIELD_TYPE_TAG:
                     if ($type == EntityExtraField::USER_FIELD_TYPE) {
                         UserManager::delete_user_tags(
@@ -182,9 +194,7 @@ class ExtraFieldValue extends Model
                     foreach ($currentTags as $extraFieldtag) {
                         $em->remove($extraFieldtag);
                     }
-
                     $em->flush();
-
                     $tagValues = is_array($value) ? $value : [$value];
                     $tags = [];
 
@@ -221,7 +231,6 @@ class ExtraFieldValue extends Model
                         $tag->setCount(count($tagUses) + 1);
                         $em->persist($tag);
                     }
-
                     $em->flush();
 
                     foreach ($tags as $tag) {
@@ -231,7 +240,6 @@ class ExtraFieldValue extends Model
                         $fieldRelTag->setTagId($tag->getId());
                         $em->persist($fieldRelTag);
                     }
-
                     $em->flush();
                     break;
                 case ExtraField::FIELD_TYPE_FILE_IMAGE:

+ 5 - 0
main/inc/lib/social.lib.php

@@ -2580,6 +2580,11 @@ class SocialManager extends UserManager
                         .' '.implode(',', $data).'</li>';
                 } else {
                     switch ($extraFieldInfo['field_type']) {
+                        case ExtraField::FIELD_TYPE_GEOLOCALIZATION:
+                            $data = explode('::', $data);
+                            $data = $data[0];
+                            $extra_information_value .= '<li class="list-group-item">'.ucfirst($extraFieldInfo['display_text']).': '.$data.'</li>';
+                            break;
                         case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
                             $id_options = explode('::', $data);
                             $value_options = [];

+ 87 - 0
main/social/map.php

@@ -0,0 +1,87 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * @package chamilo.social
+ *
+ * @author Julio Montoya <gugli100@gmail.com>
+ */
+$cidReset = true;
+
+require_once __DIR__.'/../inc/global.inc.php';
+
+api_block_anonymous_users();
+
+$extraField = new ExtraField('user');
+$infoStage = $extraField->get_handler_field_info_by_field_variable('terms_villedustage');
+$infoVille = $extraField->get_handler_field_info_by_field_variable('terms_ville');
+
+$users = who_is_online(0, 500);
+
+$data = [];
+if (!empty($users)) {
+    $tableUser = Database::get_main_table(TABLE_MAIN_USER);
+    $sql = "SELECT u.id, firstname, lastname, ev.value ville, ev2.value stage
+            FROM $tableUser u 
+            INNER JOIN extra_field_values ev
+            ON ev.item_id = u.id
+            INNER JOIN extra_field_values ev2
+            ON ev2.item_id = u.id
+            WHERE 
+                ev.field_id = ".$infoStage['id']." AND 
+                ev2.field_id = ".$infoVille['id']." AND 
+                (u.id in ('".implode("','", $users)."') )
+    ";
+    //2643 or u.id = 2692 or u.id = 2656
+    $result = Database::query($sql);
+    $data = Database::store_result($result, 'ASSOC');
+    foreach ($data as &$result) {
+        $result['complete_name'] = addslashes(api_get_person_name($result['firstname'], $result['lastname']));
+        $parts = explode('::', $result['ville']);
+        $parts2 = explode(',', $parts[1]);
+        $result['ville_lat'] = $parts2[0];
+        $result['ville_long'] = $parts2[1];
+
+        unset($result['ville']);
+
+        $parts = explode('::', $result['stage']);
+        $parts2 = explode(',', $parts[1]);
+        $result['stage_lat'] = $parts2[0];
+        $result['stage_long'] = $parts2[1];
+        unset($result['stage']);
+    }
+}
+
+$apiKey = api_get_configuration_value('google_api_key');
+$htmlHeadXtra[] = '<script type="text/javascript" src="https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/src/markerclusterer.js"></script>';
+$htmlHeadXtra[] = '<script type="text/javascript" src="https://jawj.github.com/OverlappingMarkerSpiderfier/bin/oms.min.js"></script>';
+
+$tpl = new Template(null);
+$tpl->assign('url', api_get_path(WEB_CODE_PATH).'social/profile.php');
+$tpl->assign(
+    'image_city',
+    Display::return_icon(
+        'accept.png',
+        '',
+        [],
+        ICON_SIZE_SMALL,
+        false,
+        true
+    )
+);
+$tpl->assign(
+    'image_stage',
+    Display::return_icon(
+        'delete.png',
+        '',
+        [],
+        ICON_SIZE_SMALL,
+        false,
+        true
+    )
+);
+
+$tpl->assign('places', json_encode($data));
+
+$layout = $tpl->get_template('social/map.tpl');
+$tpl->display($layout);

+ 87 - 0
main/template/default/social/map.tpl

@@ -0,0 +1,87 @@
+{% extends 'layout/layout_1_col.tpl'|get_template %}
+
+{% block content %}
+    <div id="map" style="width:100%; height:600px"></div>
+<script>
+function start()
+{
+    var options = {
+        center: new google.maps.LatLng(54.526, 15.255), // "Europe center"
+        zoom: 4,
+        mapTypeId: google.maps.MapTypeId.ROADMAP
+    };
+
+    var map = new google.maps.Map(document.getElementById("map"), options);
+    var oms = new OverlappingMarkerSpiderfier(map);
+    var cities = '{{ places }}';
+    cities = JSON.parse(cities);
+
+    var imageCity = {
+        url: '{{ image_city }}'
+    }
+
+    var stageCity = {
+        url:'{{ image_stage }}'
+    }
+
+    // Add markers
+    var markers = [];
+    if (cities.length) {
+        for (var i = 0; i < cities.length; i++) {
+            // Add ville
+            //cities[i]['type'] = 'ville';
+            var markerOptions = {
+                position: new google.maps.LatLng(cities[i]['ville_lat'], cities[i]['ville_long']),
+                title: cities[i]['complete_name'],
+                city: cities[i],
+                icon: imageCity,
+            };
+            var marker = new google.maps.Marker(markerOptions);
+            markers.push(marker);
+            oms.addMarker(marker);
+
+            // Add stage
+            //cities[i]['type'] = 'stage';
+            var markerOptions = {
+                position: new google.maps.LatLng(cities[i]['stage_lat'], cities[i]['stage_long']),
+                title: cities[i]['complete_name'],
+                city: cities[i],
+                icon: stageCity,
+            };
+            var marker = new google.maps.Marker(markerOptions);
+            markers.push(marker);
+            oms.addMarker(marker);
+        }
+
+        // Enable cluster
+        var markerClusterer = new MarkerClusterer(map, markers, {
+            maxZoom: 9, // maxZoom set when clustering will stop
+            imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
+        });
+
+        // Auto-boxing
+        var bounds = new google.maps.LatLngBounds();
+        for (var i = 0; i < markers.length; ++i) {
+            bounds.extend(markers[i].position);
+        }
+        map.fitBounds(bounds);
+
+        // window when clicking
+        var infoWindow = new google.maps.InfoWindow();
+        oms.addListener('click', function (marker, event) {
+            infoWindow.setContent('<a href="{{ url }}?u=' + marker.city['id'] + '">' + marker.city['complete_name'] + '</a>');
+            infoWindow.open(map, marker);
+        });
+
+        google.maps.event.addListener(markerClusterer, 'clusterclick', function (cluster) {
+            map.fitBounds(cluster.getBounds());
+            if (map.getZoom() > 14) {
+                map.setZoom(14);
+            }
+        });
+    }
+}
+</script>
+<script async defer type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyCDtuO5HrqpXlxcSYEN9R0y7sG_vEh6UH8&callback=start"></script>
+
+{% endblock %}