-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMS.pm
483 lines (410 loc) · 16.9 KB
/
CMS.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
##########################################################################
# Copyright C 2007-2010 Six Apart Ltd.
# This program is free software: you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details. You should have received a copy of the GNU
# General Public License version 2 along with this program. If not, see
# <http://www.gnu.org/licenses/>.
package GeoType::CMS;
use strict;
use warnings;
use MT;
use GeoType::Util;
sub create_location {
my $app = shift;
my $entry_insert = $app->param('entry_insert');
my $edit_field = $app->param('edit_field');
$app->load_tmpl( 'dialog/create_location.tmpl', { entry_insert => $entry_insert, edit_field => $edit_field } );
}
sub verify_location {
my $app = shift;
my $address = $app->param('location_address');
my @coords = GeoType::Util::geocode( $app->blog, $address );
my $entry_insert = $app->param('entry_insert');
my $edit_field = $app->param('edit_field');
require GeoType::LocationAsset;
my $la = GeoType::LocationAsset->new;
$la->blog_id( $app->blog->id );
$la->latitude( $coords[1] );
$la->longitude( $coords[0] );
my $url = $la->thumbnail_url( Width => 600, Height => int( 600 / 1.61 ) );
$app->load_tmpl(
'dialog/verify_location.tmpl',
{
edit_field => $edit_field,
entry_insert => $entry_insert,
location_address => $address,
gecoded_url => $url,
location_latitude => $coords[1],
location_longitude => $coords[0],
}
);
}
sub insert_location {
my $app = shift;
my $address = $app->param('location_address');
my $name = $app->param('location_name');
my $latitude = $app->param('location_latitude');
my $longitude = $app->param('location_longitude');
require GeoType::LocationAsset;
my $la = GeoType::LocationAsset->new;
$la->blog_id( $app->blog->id );
$la->name($name);
$la->location($address);
$la->latitude($latitude);
$la->longitude($longitude);
$la->save or die $la->errstr;
if ( $app->param('entry_insert') ) {
require MT::CMS::Asset;
$app->param( 'id', $la->id );
return MT::CMS::Asset::insert($app);
}
return $app->redirect(
$app->uri(
'mode' => 'list_assets',
args => { 'blog_id' => $app->param('blog_id') }
)
);
}
sub source_asset_options {
my ( $cb, $app, $tmpl ) = @_;
my $old = q{<__trans phrase="File Options">};
my $new =
q{<mt:unless name="asset_is_location"><__trans phrase="File Options"><mt:else>Location Options</mt:else></mt:unless>};
$$tmpl =~ s/\Q$old\E/$new/;
}
sub param_asset_options {
my ( $cb, $app, $param, $tmpl ) = @_;
my $asset_id = $param->{asset_id};
require MT::Asset;
my $asset = MT::Asset->load($asset_id);
$param->{asset_is_location} = $asset->isa('GeoType::LocationAsset');
}
sub source_edit_entry {
my ( $cb, $app, $tmpl ) = @_;
my $old = q{<div id="feedback-field"};
my $new = q{<div id="location-field">
<mt:setvarblock name="location_header_action">
<a href="javascript:void(0)" class="add-new-category-link button" onclick="openDialog(this.form, 'list_assets', 'filter=class&filter_val=location&edit_field=location_list&blog_id=<$mt:var name="blog_id"$>&dialog_view=1')" title="<__trans phrase="Add location">"><__trans phrase="add"/></a>
</mt:setvarblock>
<mtapp:widget
id="entry-location-widget"
label="Locations"
header_action="$location_header_action">
<mt:var name="location_setting">
</mtapp:widget>
</div>};
$$tmpl =~ s/\Q$old\E/$new$old/;
}
sub param_edit_entry {
my ( $cb, $app, $param, $tmpl ) = @_;
my $blog_id = $app->blog->id;
my $header = $tmpl->getElementById('header_include');
my $html_head = $tmpl->createElement( 'setvarblock', { name => 'html_head', append => 1 } );
my $innerHTML = q{
<link rel='stylesheet' href="<mt:var name="static_uri">plugins/GeoType/geotype.css" />
<script type="text/javascript">
/* <![CDATA[ */
var locations = <mt:if name="location_list"><mt:var name="location_list" to_json="1"><mt:else>[]</mt:else></mt:if>;
function locationToStr (loc) {
var str = loc.id + "||";
var o = [];
for (var opt in loc.options) {
o.push (opt + '=' + loc.options[opt]);
}
return str + o.join (escape('&&'));
}
function buildLocationList () {
var elem = getByID ('location-list');
elem.innerHTML = '';
var html = '';
for (var i = 0; i < locations.length; i++) {
html = html + "<li class='pkg' onmouseover='DOM.addClassName(this, \"focus\")' onmouseout='DOM.removeClassName(this, \"focus\")' mt:id='" + locations[i].id + "'><strong><a href='javascript:void(0)' onclick='openLocationOptionsDialog(this.form, " + locations[i].id + ")'>" + locations[i].name + '</a></strong><a href="javascript:void(0);" onclick="removeLocation (' + locations[i].id + ')" mt:command="remove" class="delete" title="Remove"> <span>Remove</span></a></li>';
}
elem.innerHTML = html;
var value_elem = getByID ('location_list');
value_elem.value = locations.toJSON();
/*value_elem.value = locations.map (locationToStr).join ('==');
# alert ("Value set to " + value_elem.value);
# for (var i = 0; i < locations.length; i++) {
#
# }*/
if (locations.length) {
DOM.removeClassName ('location-list-preview', 'hidden');
}
else {
DOM.addClassName ('location-list-preview', 'hidden');
}
}
function insertLocation (id, name) {
// Skip out if the id already exists in the list
for (var i = 0; i < locations.length; i++) {
if (locations[i].id == id) {
return;
}
}
var new_location = new Object();
new_location.name = name;
new_location.id = id;
new_location.options = {};
locations[locations.length] = new_location;
buildLocationList();
}
function updateLocationOptions (id, options) {
for (var i = 0; i < locations.length; i++) {
if (locations[i].id == id) {
locations[i].options = options;
}
}
buildLocationList();
}
function removeLocation (id) {
var new_locations = new Array();
for (var i = 0; i < locations.length; i++) {
if (locations[i].id != id) {
new_locations[new_locations.length] = locations[i];
}
}
locations = new_locations;
buildLocationList();
}
function openLocationPreview (f) {
var location_list = getByID ('location_list').value;
return openDialog (f, 'preview_locations', 'blog_id=<$mt:var name="blog_id"$>&location_list=' + escape (location_list));
}
function openLocationOptionsDialog (f, id) {
var location;
for (var i = 0; i < locations.length; i++) {
if (locations[i].id == id) {
location = locations[i];
}
}
return openDialog (f, 'location_options', 'blog_id=<$mt:var name="blog_id"$>&location_id=' + location.id + "&location_options=" + escape(Object.toJSON(location.options)));
}
TC.attachLoadEvent (buildLocationList);
/* ]]> */
</script>
};
$html_head->innerHTML($innerHTML);
$tmpl->insertBefore( $html_head, $header );
$param->{location_setting} = q{
<ul class='category-list pkg' id='location-list'>
</ul>
<a href="javascript:void(0)" id="location-list-preview" class="pkg center button" style="text-align: center" onclick="openLocationPreview(this.form)" title="<__trans phrase="Preview Locations">"><__trans phrase="preview"/></a>
<input type="hidden" name="location_list" id="location_list" />
};
if ( $param->{id} ) {
require MT::ObjectAsset;
my @non_embedded_assets = MT::ObjectAsset->load(
{
object_id => $param->{id},
object_ds => 'entry',
embedded => 0,
}
);
my @location_list = ();
require MT::Asset;
for my $oa (@non_embedded_assets) {
my $a = MT::Asset->load( $oa->asset_id ) or next;
next unless ( $a->isa('GeoType::LocationAsset') );
my $e = MT::Entry->load( $oa->object_id );
push @location_list,
{ id => $a->id, name => $a->name, options => ( $e->location_options->{ $a->id } || {} ) };
}
$param->{location_list} = \@location_list;
}
}
sub param_asset_insert {
my ( $cb, $app, $param, $tmpl ) = @_;
my $plugin = $cb->plugin;
# proceed as normal unless we detect the simple association type
return 1 unless $app->param('edit_field') eq 'location_list';
my $block = $tmpl->getElementById('insert_script');
return 1 unless $block;
my $preview_html = '';
my $ctx = $tmpl->context;
if ( my $asset = $ctx->stash('asset') ) {
my $asset_id = $asset->id;
my $asset_name = $asset->name;
require MT::Util;
$asset_name = MT::Util::encode_js($asset_name);
$block->innerHTML(qq{top.insertLocation($asset_id, '$asset_name');});
}
}
sub post_save_entry {
my ( $cb, $app, $entry ) = @_;
my %locations;
my $inline_location = $app->param('inline_location');
if ($inline_location) {
require GeoType::LocationAsset;
require GeoType::Util;
my @coords = GeoType::Util::geocode( $app->blog, $inline_location );
my $la = GeoType::LocationAsset->new;
$la->blog_id( $entry->blog_id );
$la->name( $entry->title ); # just using the entry title for the location name when it's inline
$la->location($inline_location);
$la->latitude( $coords[1] );
$la->longitude( $coords[0] );
$la->save or die $la->errstr;
# set options in location hash
$locations{ $la->id } = $app->param('location_options');
}
else {
my $location_list = $app->param('location_list');
require JSON;
my $locations = JSON::jsonToObj($location_list);
foreach my $loc (@$locations) {
$locations{ $loc->{id} } = $loc->{options};
}
}
# foreach my $loc (split (/\s*,\s*/, $location_list)) {
# my ($id, $opts) = split (/\|\|/, $loc, 2);
# my %opts_hash = map { my ($k, $v) = split (/=/, $_, 2); $k => $v } $opts;
# $locations{$id} = { %opts_hash };
# }
my @ids = keys %locations;
my @asset_list = MT::Asset->load(
{ class => 'location' },
{
join => MT::ObjectAsset->join_on(
'asset_id',
{
object_id => $entry->id,
object_ds => $entry->datasource,
embedded => 0,
}
)
}
);
my %asset_ids = map { $_->id => 1 } @asset_list;
my @new_embeds;
# first go through & don't re-add anything that's on our list that's already there
for (@ids) {
push @new_embeds, $_ unless ( defined $asset_ids{$_} );
delete $asset_ids{$_} if ( defined $asset_ids{$_} );
}
require GeoType::LocationAsset;
for my $id (@new_embeds) {
my $la = GeoType::LocationAsset->load($id) or next;
my $oa = MT::ObjectAsset->set_by_key(
{
blog_id => $entry->blog_id,
object_id => $entry->id,
object_ds => $entry->datasource,
asset_id => $id,
embedded => 0,
}
) or return $cb->error( MT::ObjectAsset->errstr );
}
# remove any that got deleted
for my $id ( keys %asset_ids ) {
MT::ObjectAsset->remove(
{
blog_id => $entry->blog_id,
object_id => $entry->id,
object_ds => $entry->datasource,
asset_id => $id
}
);
}
$entry->location_options( \%locations );
$entry->save or return $cb->error( $entry->errstr );
1;
}
sub preview_locations {
my $app = shift;
my $blog = $app->blog;
my $location_list = $app->param('location_list');
require JSON;
my $locations = JSON::from_json($location_list);
my @ids = map { $_->{id} } @$locations;
my @locations;
require MT::Asset;
for my $id (@ids) {
next unless $id;
my $asset = MT::Asset->load($id) or next;
push @locations, $asset if ( $asset->isa('GeoType::LocationAsset') );
}
@locations = map {
{ id => $_->id, name => $_->name, geometry => $_->geometry, lat => $_->latitude, lng => $_->longitude }
} @locations;
my $plugin = MT->component('geotype');
my $map_type = $plugin->get_config_value( 'interactive_map_type', 'blog:' . $blog->id );
my $config = $plugin->get_config_hash( 'blog:' . $blog->id );
$map_type =
$map_type eq 'roadmap' ? 'G_NORMAL_MAP'
: $map_type eq 'satellite' ? 'G_SATELLITE_MAP'
: $map_type eq 'hybrid' ? 'G_HYBRID_MAP'
: $map_type eq 'terrain' ? 'G_PHYSICAL_MAP'
: 'G_NORMAL_MAP';
my $key = GeoType::Util::get_google_api_key($blog);
return $app->load_tmpl(
'dialog/preview_locations.tmpl',
{
map_type => $map_type,
google_api_key => $key,
location_list => \@locations,
%$config,
}
);
}
sub source_asset_list {
my ( $cb, $app, $tmpl ) = @_;
return 1 unless ( $app->param('edit_field') eq 'location_list' );
my $new = q{
<img src="<mt:var name="static_uri">images/status_icons/create.gif" alt="<__trans phrase="Add New Location">" width="9" height="9" />
<mt:unless name="asset_select"><mt:setvar name="entry_insert" value="1"></mt:unless>
<a href="<mt:var name="script_url">?__mode=create_location&blog_id=<mt:var name="blog_id">&dialog_view=1&entry_insert=1&edit_field=<mt:var name="edit_field" escape="url">&upload_mode=<mt:var name="upload_mode" escape="url">&<mt:if name="require_type">require_type=<mt:var name="require_type">&</mt:if>return_args=<mt:var name="return_args" escape="url"><mt:if name="user_id">&user_id=<mt:var name="user_id" escape="url"></mt:if>" ><__trans phrase="Add New Location"></a>
};
$$tmpl
=~ s{\Q<mt:setvarblock name="upload_new_file_link">\E.*\Q</mt:setvarblock>\E}{<mt:setvarblock name="upload_new_file_link">$new</mt:setvarblock>}ms;
}
sub location_options {
my $app = shift;
my $blog = $app->blog;
my $loc = $app->param('location');
# my ($id, $options) = split (/\|\|/, $loc, 2);
my $id = $app->param('location_id');
my $options = $app->param('location_options');
require JSON;
$options = JSON::from_json($options);
require MT::Asset;
my $location = MT::Asset->load($id);
my $location_hash = {
id => $location->id,
name => $location->name,
geometry => $location->geometry,
lat => $location->latitude,
lng => $location->longitude,
options => $options
};
$location_hash->{options}->{contents} ||= $location->description;
# my $location_opts = { map { "location_marker_opt_$_" => $options->{$_} } keys %$options };
# $location_opts->{location_marker_opt_contents} ||= $location->description;
my $plugin = MT->component('geotype');
my $map_type = $plugin->get_config_value( 'interactive_map_type', 'blog:' . $blog->id );
my $config = $plugin->get_config_hash( 'blog:' . $blog->id );
$map_type =
$map_type eq 'roadmap' ? 'G_NORMAL_MAP'
: $map_type eq 'satellite' ? 'G_SATELLITE_MAP'
: $map_type eq 'hybrid' ? 'G_HYBRID_MAP'
: $map_type eq 'terrain' ? 'G_PHYSICAL_MAP'
: 'G_NORMAL_MAP';
my $key = GeoType::Util::get_google_api_key($blog);
return $app->load_tmpl(
'dialog/location_options.tmpl',
{
location => $location_hash,
map_type => $map_type,
google_api_key => $key,
%$config,
%{ $location_hash->{options} },
# %$location_opts,
}
);
}
1;