PluginProbe
Interactive World Map / trunk
Interactive World Map vtrunk
trunk 3.2.0 3.4.4 3.4.8
interactive-world-map / maptools.php

maptools.php in Interactive World Map trunk, at maptools.php

294 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 $options = freeworld_html5map_plugin_get_options();
4 $option_keys = is_array($options) ? array_keys($options) : array();
5 $map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : array_shift($option_keys);
6
7 function freeworld_html5map_plugin_detect_csv_header(&$row) {
8 static $header = array(
9 'states' => null,
10 );
11
12 if (is_null($header['states'])) {
13 foreach($header as $k => $v)
14 $header[$k] = freeworld_html5map_plugin_get_csv_import_export_keys($k);
15 }
16
17 if (count($row) < 5) return null;
18
19 foreach($header as $type => $type_header_map) {
20 $type_header = array_keys($type_header_map);
21 if ($type_header[0] == $row[0] and $type_header[1] == $row[1] and $type_header[2] == $row[2]
22 and $type_header[3] == $row[3] and $type_header[4] == $row[4])
23 return $type;
24 }
25
26 return null;
27 }
28
29 function freeworld_html5map_plugin_detect_click_action(&$params, $id) {
30 if (empty($params['clickAction'])) {
31 if (!empty($params['link']))
32 $params['clickAction'] = preg_match('/javascript:[\w_]+_set_state_text[^\(]*\([^\)]+\);/', $params['link']) ? 'info' : 'link';
33 elseif (!empty($params['popup-id']) or !empty($params['popup_id']))
34 $params['clickAction'] = 'popup';
35 elseif ($id && !empty($map_options['info'][$id]))
36 $params['clickAction'] = 'info';
37 else
38 $params['clickAction'] = 'none';
39 }
40 switch ($params['clickAction']) {
41 case 'info':
42 $params['link'] = '#info';
43 $params['isNewWindow'] = false;
44 break;
45 case 'popup':
46 $params['link'] = '#popup';
47 $params['isNewWindow'] = false;
48 break;
49 case 'link':
50 break;
51 case 'none':
52 $params['link'] = '';
53 break;
54 }
55 }
56
57 function freeworld_html5map_plugin_import_csv(&$errors) {
58 $types = array('text/csv','text/comma-separated-values','application/vnd.ms-excel','application/csv','application/excel','application/vnd.msexcel','application/octet-stream');
59 $json_types = array('application/json');
60 $field_delimiters = array(
61 ',' => ',',
62 ';' => ';',
63 ':' => ':',
64 'sp'=> ' ',
65 'tb'=> "\t"
66 );
67 $text_delimiters = array(
68 "'" => "'",
69 '"' => '"',
70 'n' => null
71 );
72 $errors = array();
73
74 if (! is_uploaded_file($_FILES['csv-file']['tmp_name']))
75 $errors[] = __('File upload error!', 'freeworld-html5-map');
76 else {
77 if (in_array($_FILES['csv-file']['type'], $json_types)) {
78 $errors[] = sprintf(__('JSON import should be done on the "<a href="%s">Maps dashboard</a>" tab', 'freeworld-html5-map'), "admin.php?page=freeworld-html5-map-maps");
79 }
80 elseif (!in_array($_FILES['csv-file']['type'], $types)) {
81 $errors[] = __('Wrong file type!', 'freeworld-html5-map');
82 }
83 }
84 $fd = stripslashes($_REQUEST['field-delimiter']);
85 $td = stripslashes($_REQUEST['text-delimiter']);
86 if ( ! array_key_exists($fd, $field_delimiters)) {
87 $errors[] = __('Wrong field delimiter!', 'freeworld-html5-map');
88 }
89 else {
90 $fd = $field_delimiters[$fd];
91 }
92 if ( ! array_key_exists($td, $text_delimiters)) {
93 $errors[] = __('Wrong text delimiter!', 'freeworld-html5-map').$td;
94 }
95 else {
96 $td = $text_delimiters[$td];
97 }
98
99 if ($errors)
100 return false;
101
102 $fh = fopen($_FILES['csv-file']['tmp_name'], 'r');
103 $head = fgetcsv($fh, 0, $fd, $td);
104 if ( ! $head) {
105 $errors[] = __('Wrong csv file!', 'freeworld-html5-map');
106 return false;
107 }
108 if ( ! ($current_type = freeworld_html5map_plugin_detect_csv_header($head))) {
109 $errors[] = __('Wrong csv header!', 'freeworld-html5-map');
110 return false;
111 }
112 $data = array(
113 'states' => array(),
114 'groups' => array(),
115 'points' => array(),
116 );
117 while ($line = fgetcsv($fh, 0, $fd, $td)) {
118 if ($new_type = freeworld_html5map_plugin_detect_csv_header($line)) {
119 $current_type = $new_type;
120 $head = $line;
121 continue;
122 }
123 $data[$current_type][$line[0]] = array();
124 foreach ($head as $i => $k)
125 $data[$current_type][$line[0]][$k] = $line[$i];
126 }
127 fclose($fh);
128 unlink($_FILES['csv-file']['tmp_name']);
129
130 $all_options = freeworld_html5map_plugin_get_options();
131 $options_keys = array_keys($all_options);
132 $def_map_id = reset($options_keys);
133
134 $map_id = isset($_GET['map_id']) ? (int)$_GET['map_id'] : $def_map_id;
135 $map_options = &$all_options[$map_id];
136
137 if (!empty($data['states'])) {
138 $import_export_keys = freeworld_html5map_plugin_get_csv_import_export_keys('states');
139 unset($import_export_keys['id'], $import_export_keys['info']);
140 $fields = array_keys($import_export_keys);
141 $new_fields = array_values($import_export_keys);
142 $st_params = json_decode($map_options['map_data'], true);
143 foreach ($st_params as $id => &$params) {
144 if (!isset($data['states'][$id]))
145 continue;
146 if (isset($data['states'][$id]['info']))
147 $map_options['state_info'][preg_replace('/\D+/', '', $id)] = $data['states'][$id]['info'];
148 foreach ($fields as $i => $f) {
149 $params[$new_fields[$i]] = isset($data['states'][$id][$f]) ? $data['states'][$id][$f] : '';
150 }
151 freeworld_html5map_plugin_detect_click_action($params, $id);
152 unset($params['clickAction']);
153 unset($data['states'][$id]);
154 foreach (array('isNewWindow' ,'popup-id', 'group', '_hide_name', 'hidden', 'class') as $f) {
155 if ($params[$f] === '') unset($params[$f]);
156 }
157 }
158 unset($params);
159 $st_params = json_encode($st_params, defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : null);
160 if ($st_params)
161 $map_options['map_data'] = $st_params;
162 else
163 $errors[] = __('Failed to encode new data! Probably non UTF data.', 'freeworld-html5-map');
164
165 if (count($data['states'])) {
166 $errors[] = __('Some data left unimported! Looks like wrong map.', 'freeworld-html5-map');
167 }
168 }
169 if ($errors) {
170 return false;
171 }
172
173 $map_options['update_time'] = time();
174 freeworld_html5map_plugin_save_options($all_options);
175 return true;
176 }
177
178
179 if (isset($_POST['action']) && $_POST['action'] == 'freeworld-html5-map-import-csv') {
180 check_admin_referer('tools');
181
182 if (freeworld_html5map_plugin_import_csv($errors))
183 freeworld_html5map_plugin_messages(array(__('Configuration successfully imported!', 'freeworld-html5-map')), null);
184 else
185 freeworld_html5map_plugin_messages(null, $errors);
186 } elseif (isset($_POST['action']) && $_POST['action'] == 'freeworld-html5-map-save-js') {
187 check_admin_referer('tools');
188
189 $map_options = &$options[$map_id];
190 $map_options['customJs'] = stripslashes($_POST['custom_js']);
191 $map_options['update_time'] = time();
192 freeworld_html5map_plugin_save_options($options);
193 freeworld_html5map_plugin_messages(array(__('Custom js successfully updated!', 'freeworld-html5-map')), null);
194 }
195
196 echo "<div class=\"wrap freeworld-html5-map main full\"><h2>" . __('Tools', 'freeworld-html5-map') . "</h2>";
197 ?>
198 <script>
199 jQuery(function(){
200
201 jQuery('.freeworld-html5-map-acs-label').click(function() {
202 jQuery(this)
203 .toggleClass('freeworld-html5-map-closed')
204 .next().filter('.freeworld-html5-map-acs').toggleClass('freeworld-html5-map-closed');
205 });
206
207 jQuery('.csv-import').click(function() {
208 jQuery('input[name=csv-file]').click();
209 return false;
210 });
211
212 jQuery('input[name=csv-file]').change(function() {
213 var btn = jQuery('.csv-import');
214 var text = jQuery(btn).data('text') ? jQuery(btn).data('text') : jQuery(btn).text();
215
216 jQuery(btn).data('text',text);
217
218 jQuery('.csv-import').text(text + " (" + jQuery(this).val() + ")");
219 if (this.checkValidity && !this.checkValidity())
220 return;
221 jQuery('form[name=action_form_import] input[name=action]').val('freeworld-html5-map-import-csv');
222 jQuery('form[name=action_form_import]').submit();
223 });
224 });
225 </script>
226 <br>
227
228 <div class="left-block">
229 <form method="POST" class="" name="action_form_import" enctype="multipart/form-data">
230 <?php wp_nonce_field('tools'); ?>
231 <?php
232 freeworld_html5map_plugin_map_selector('tools', $map_id, $options);
233 echo "<br /><br />\n";
234 freeworld_html5map_plugin_nav_tabs('tools', $map_id);
235 ?>
236 <h3><?php _e('Batch export / import from CSV', 'freeworld-html5-map') ?></h3>
237 <input type="submit" class="button button-secondary export" value="<?php esc_attr_e('Export to CSV file', 'freeworld-html5-map'); ?>" />&nbsp;&nbsp;&nbsp;
238 <input type="button" class="button button-secondary csv-import" value="<?php esc_attr_e('Import from CSV file', 'freeworld-html5-map'); ?>" />
239 <br/><br/>
240 <label class="freeworld-html5-map-acs-label freeworld-html5-map-closed"><?php _e('Advanced CSV settings', 'freeworld-html5-map'); ?></label>
241 <div class="freeworld-html5-map-acs freeworld-html5-map-closed" style="padding-bottom: 10px">
242 <label class="title"><?php _e('Field delimiter:', 'freeworld-html5-map') ?></label> <select name="field-delimiter" style="width: 100px">
243 <option value=",">,</option>
244 <option value=";">;</option>
245 <option value=":">:</option>
246 <option value="sp"><?php _e('{SPACE}', 'freeworld-html5-map') ?></option>
247 <option value="tb"><?php _e('{TAB}', 'freeworld-html5-map') ?></option>
248 </select><br>
249 <label class="title"><?php _e('Text delimiter:', 'freeworld-html5-map') ?></label> <select name="text-delimiter" style="width: 100px">
250 <option value='"'>"</option>
251 <option value="'">'</option>
252 </select>
253 <input type="file" accept="text/csv,text/comma-separated-values" name="csv-file" style="display: none">
254 </div>
255 <br>
256 <p><?php printf(__('Note that this tool exports/imports area data only. If you need to make a full backup of all settings of the map including general settings, use the backup option provided on <a href="%s">"Maps"</a> page.', 'freeworld-html5-map'), '?page=freeworld-html5-map-maps'); ?></p>
257 <p class="help"><?php _e('* The term "area" means one of the following: continent, country, state, province, county or district, depending on the particular plugin.', 'freeworld-html5-map'); ?></p>
258 <input type="hidden" name="action" value="freeworld-html5-map-export-csv">
259 </form>
260
261 <br><br>
262 <form method="POST" class="" name="action_form_js" enctype="multipart/form-data">
263 <?php wp_nonce_field('tools'); ?>
264 <h3><?php _e('Custom JavaScript', 'freeworld-html5-map') ?></h3>
265 <p><?php _e('Here you can add any custom javascript to extend plugin\'s functionality', 'freeworld-html5-map') ?></p>
266 <label class="freeworld-html5-map-acs-label freeworld-html5-map-closed"><?php _e('Detailed description', 'freeworld-html5-map'); ?></label>
267 <div class="freeworld-html5-map-acs freeworld-html5-map-closed">
268 <p><?php _e('Any code placed here will be executed after the map is drawn.', 'freeworld-html5-map') ?></p>
269 <p><?php _e('Here are the list of plugin-related variables, that will be available for you:', 'freeworld-html5-map') ?><ul>
270 <li><b>map</b> - <?php _e('Map instance. All available features are listed <a href="https://docs.html5maps.com/html5-locator-maps/api">here</a>.', 'freeworld-html5-map') ?></li>
271 <li><b>containerId</b> - <?php _e('HTML id of the container that contains the map.', 'freeworld-html5-map') ?></li>
272 <li><b>mapId</b> - <?php _e('Id of the map, specified in shortcode.', 'freeworld-html5-map') ?></li>
273 </ul>
274 <p><?php _e('You can find out any id of the area or point by viewing the CSV export.', 'freeworld-html5-map'); ?></p>
275 <h4>Example:</h4>
276 <pre class="code">
277 <span class="v">map</span>.<span class="m">on</span>(<span class="s">'click'</span>, <span class="k">function</span> (<span class="v">event</span>, <span class="v">sid</span>, <span class="v">map</span>) {
278 <span class="k">var</span> <span class="v">name</span> = <span class="v">map</span>.<span class="m">fetchStateAttr</span>(<span class="v">sid</span>, <span class="s">'name'</span>);
279 <span class="v">console</span>.<span class="m">log</span>(<span class="s">'Clicked state is: '</span> + <span class="v">name</span>);
280 });</pre>
281 </div>
282 <div style="clear: both"></div>
283 <textarea name="custom_js" rows="20" style="width: 80%"><?php echo $options[$map_id]['customJs'] ? htmlspecialchars($options[$map_id]['customJs']) : '' ?></textarea>
284 <input type="hidden" name="action" value="freeworld-html5-map-save-js" />
285 <p class="submit"><input type="submit" value="<?php esc_attr_e('Save Changes', 'freeworld-html5-map'); ?>" class="button-primary" id="submit" name="submit"></p>
286 </form>
287 </div>
288 <div class="qanner">
289 <a href="https://www.fla-shop.com/products/wp-plugins/world/countries/?utm_source=world-map-plugin&utm_medium=dashboard&utm_campaign=qanner" target="_blank"><img src="<?php echo freeworld_html5map_plugin_get_static_url("html5maps_img.png") . "?r=".time();?>" border="0" width="161" height="601"></a>
290 </div>
291
292 <div class="clear"></div>
293 </div>
294