Documentation/Index/GoogleMapsIntegration: CustomGoogleMapsDialog.inc

Plik CustomGoogleMapsDialog.inc, 4.9 KB (dodany przez TS, 4 years temu)
xx
Line 
1<?php
2
3/**
4 * CustomGoogleMapsDialog
5 *
6 * @author Tomasz Świenty
7 * @version 0.1
8 * @copyright Copyright (c) eDokumenty
9 */
10final class CustomGoogleMapsDialog extends DialogForm {
11
12
13    protected static $cMethods = [
14        'updateGPSLocation'
15    ];
16
17
18    /**
19     * CustomGoogleMapsDialog constructor.
20     * @param $name
21     * @param null $caption
22     * @param int $mode
23     */
24    public function __construct($name, $caption = NULL, $mode = BS_DIALOG) {
25
26        parent::__construct($name, Translator::translate('Google Maps'), BS_DIALOG);
27
28        $this->path = App::makeRelativePath(__FILE__);
29
30        $this->width = '780px';
31        $this->height = '740px';
32
33        if ($this->isOpened() AND !$this->noNeedForCreate() AND !Request::hasMethodCall($this->name, array_keys(self::$cMethods))) {
34            $this->create();
35        }
36
37    }
38
39
40    /**
41     * @return array
42     */
43    public function getCallableMethods() {
44
45        return self::$cMethods;
46
47    }
48
49
50    /**
51     * @param $params
52     * @throws CustomErrorException
53     */
54    public function updateGPSLocation($params) {
55
56        $pData = JSON::toArray($params);
57        $mainad = (isset($pData['mainad']) AND (filter_var($pData['mainad'], FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) !== FALSE)) ? $pData['mainad'] : FALSE;
58        if (!$mainad) {
59            throw new CustomErrorException(Translator::translate('Brak identyfikatora adresu'));
60        }
61
62        $gps = isset($pData['gps'], $pData['gps']['lat'], $pData['gps']['lng']) ? $pData['gps'] : FALSE;
63        if (!$gps) {
64            throw new CustomErrorException(Translator::translate('Brak lokalizacji GPS'));
65        }
66
67        require_once(MODEL_PATH.'Contacts/PAddress.inc');
68        $address = new PAddress($mainad);
69        $address->set('lat___', $pData['gps']['lat']);
70        $address->set('lng___', $pData['gps']['lng']);
71        $address->save();
72    }
73
74
75    /**
76     * @param null $params
77     * @param null $data
78     * @throws CustomErrorException
79     */
80    public function open($params = NULL, &$data = NULL) {
81
82        $pData = JSON::toArray($params);
83
84        $keyval = (isset($pData['keyval']) AND is_array($pData['keyval'])) ? $pData['keyval'] : FALSE;
85        if (!$keyval) {
86            throw new CustomErrorException(Translator::translate('Wybierz klientów z listy'));
87        }
88
89        parent::open($params, $data);
90        $this->setSData(['contid' => $keyval]);
91        $this->create();
92
93    }
94
95
96    /**
97     * @return bool
98     */
99    protected function create() {
100
101        if (!$this->isOpened()) {
102            return FALSE;
103        }
104
105        $sData = $this->getSData();
106        $contacts = PgManager::getInstance()->query('SELECT contid, name_1, f_addr, mainad FROM contacts_view WHERE contid IN ('.implode(', ', $sData['contid']).')', FALSE, PGSQL_ASSOC);
107
108        $mapCanvas = new Panel($this->name.'mapCanvas');
109        $mapCanvas->width = '100%';
110        $mapCanvas->height = '100%';
111        $mapCanvas->position = 'relative';
112
113        $map = \ReadyCls\Google\Google::createMapInstance($this->name.'map', $mapCanvas->name);
114        $map->create();
115
116        foreach ($contacts as $contact) {
117            $map->addMarkerByLocation($contact['f_addr'], $contact['name_1'], FALSE, 'App.'.$this->name.'markerDropCallback('.$contact['mainad'].', marker);');
118        }
119
120        $this->add($mapCanvas);
121
122        // dostępne metody https://developers.google.com/maps/documentation/javascript/reference/marker#Marker
123        JScript::add('
124            App.'.$this->name.'markerDropCallback = function(mainad, marker) {                   
125                '.JScript::confirmAction(Translator::translate('Czy zmienić lokaliację GPS'), 'App.'.$this->name.'markerDropCallbackConfirm(mainad, marker)', NULL, FALSE).'                   
126            }
127           
128            App.'.$this->name.'markerDropCallbackConfirm = function(mainad, marker) {                               
129                $A1(\''.$this->name.'\', \''.$this->name.'\', '.(int)array_search('updateGPSLocation', self::$cMethods).', ({mainad:mainad, gps:marker.getPosition().toJSON()}).toJSONString(), \'\', 0, true);
130            }           
131        ');
132
133        return TRUE;
134
135    }
136
137}
138
139/**
140 * CustomGoogleMapsDialog
141 *
142 * @author Tomasz Świenty
143 * @version 0.1
144 * @copyright Copyright (c) eDokumenty
145 */
146final class CustomGoogleMapsDialogInitializer {
147
148
149    /**
150     * @param $args
151     * @throws WidgetException
152     */
153    public static function init($args) {
154
155        if (!isset($args['keyval']) OR !$args['keyval']) {
156            throw new WidgetException($args['listName'], Translator::translate('Wybierz klientów'));
157        }
158
159        $HWND = Application::registerDialog(Application::getShortName('CustomGoogleMapsDialog'), 'CustomGoogleMapsDialog', './scripts/CustomGoogleMapsDialog.inc');
160
161        JScript::add('App.openDialogEx(\''.$HWND.'\', null, ('.JSON::encode($args).').toJSONString())');
162
163    }
164
165}
166
167if (isset($args['keyval'])) {
168    CustomGoogleMapsDialogInitializer::init($args);
169}