forked from Iddah/informea.plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_classes_overloaded.php
1167 lines (1001 loc) · 41.4 KB
/
ai_classes_overloaded.php
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
add_action('wp_ajax_nopriv_get_decision_paragraph_tags', 'informea_get_decision_paragraph_tags');
add_action('wp_ajax_get_decision_paragraph_tags', 'informea_get_decision_paragraph_tags');
add_action('wp_ajax_nopriv_get_decision_paragraph_tags_html', 'informea_get_decision_paragraph_tags_html');
add_action('wp_ajax_get_decision_paragraph_tags_html', 'informea_get_decision_paragraph_tags_html');
add_action('wp_ajax_nopriv_countries_autocomplete', 'ajax_countries_autocomplete');
add_action('wp_ajax_countries_autocomplete', 'ajax_countries_autocomplete');
add_action('wp_ajax_nopriv_nfp_autocomplete', 'ajax_nfp_autocomplete');
add_action('wp_ajax_nfp_autocomplete', 'ajax_nfp_autocomplete');
add_action('wp_ajax_nopriv_country_mea_membership', 'ajax_country_mea_membership');
add_action('wp_ajax_country_mea_membership', 'ajax_country_mea_membership');
add_action('wp_ajax_nopriv_country_nfp', 'ajax_country_nfp');
add_action('wp_ajax_country_nfp', 'ajax_country_nfp');
add_action('wp_ajax_nopriv_country_sites_markers', 'ajax_country_sites_markers');
add_action('wp_ajax_country_sites_markers', 'ajax_country_sites_markers');
add_action('wp_ajax_nopriv_get_event_list', array('informea_meetings', 'ajax_get_event_list'));
add_action('wp_ajax_get_event_list', array('informea_meetings', 'ajax_get_event_list'));
add_action('wp_ajax_nopriv_get_event_list_html', array('informea_meetings', 'ajax_get_event_list_html'));
add_action('wp_ajax_get_event_list_html', array('informea_meetings', 'ajax_get_event_list_html'));
/* Ajax endpoints */
function ajax_countries_autocomplete() {
$page_data = new imea_countries_page(NULL);
$key = get_request_value('key');
$countries = $page_data->search_countries_by_name($key);
$arr = array();
foreach ($countries as $country) {
$arr[] = array('id' => $country->id, 'name' => $country->name);
}
header('Content-Type:application/json');
echo json_encode($arr);
die();
}
function ajax_nfp_autocomplete() {
$page_data = new imea_countries_page(NULL);
$key = get_request_value('key');
$objects = $page_data->search_nfp_by_name($key);
$arr = array();
foreach ($objects as $ob) {
$label = $ob->first_name . ' ' . $ob->last_name . ' (' . $ob->country_name . ')';
$arr[] = array('id_contact' => $ob->id, 'id_country' => $ob->id_country, 'label' => $label, 'id_treaty' => $ob->id_treaty);
}
header('Content-Type:application/json');
echo json_encode($arr);
die();
}
function ajax_country_sites_markers() {
$id_country = get_request_int('id');
header('Content-Type:application/json');
$icon_whc = sprintf('%s/wp-content/uploads/whc_marker.png', get_bloginfo('url'));
$icon_ramsar = sprintf('%s/wp-content/uploads/ramsar_marker.png', get_bloginfo('url'));
$whc_markers = array();
$ramsar_markers = array();
foreach(informea_countries::get_whc_sites($id_country) as $site) {
$ob = new stdClass();
$ob->the_id = $site->original_id;
$ob->clickable = TRUE;
$ob->icon = $icon_whc;
$ob->latitude = $site->latitude;
$ob->longitude = $site->longitude;
$ob->title = $site->name;
$ob->url = $site->url;
$whc_markers[] = $ob;
}
foreach(informea_countries::get_ramsar_sites($id_country) as $site) {
$ob = new stdClass();
$ob->id = str_replace('ramsar-', '', $site->original_id);
$ob->the_id = $site->original_id;
$ob->clickable = TRUE;
$ob->icon = $icon_ramsar;
$ob->latitude = $site->latitude;
$ob->longitude = $site->longitude;
$ob->title = $site->name;
$ob->url = $site->url;
$ramsar_markers[] = $ob;
}
echo json_encode(
array(
'whc' => $whc_markers,
'ramsar' => $ramsar_markers
)
);
die();
}
function ajax_country_mea_membership() {
$id_country = get_request_int('id');
$membership = informea_countries::get_treaty_membership($id_country);
header('Content-Type:text/html');
?>
<table>
<?php foreach($membership as $row): ?>
<tr>
<td class="text-center">
<i class="thumbnail <?php echo $row->odata_name; ?>"> </i>
</td>
<td><a href="<?php echo get_bloginfo('url') . '/treaties/' . $row->odata_name;?>"><?php echo $row->short_title; ?></a></td>
<td class="text-center"><?php echo mysql2date('Y', $row->date); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
die();
}
function ajax_country_nfp() {
$id_country = get_request_int('id');
$nfps = informea_countries::get_focal_points_by_treaty($id_country);
header('Content-Type:text/html');
?>
<table>
<?php foreach($nfps as $row): ?>
<tr>
<td>
<div class="thumbnail <?php echo $row->odata_name; ?>"></div>
</td>
<td>
<a href="<?php echo get_bloginfo('url') . '/treaties/' . $row->odata_name;?>"><?php echo $row->short_title; ?></a>
</td>
<td class="text-top">
<ul>
<?php foreach($row->focal_points as $p): ?>
<li>
<?php echo sprintf('%s %s %s', $p->prefix, $p->first_name, $p->last_name); ?>
<p class="note">
<?php echo informea_countries::get_focal_point_position($p); ?>
</p>
[ <a target="_blank" href="<?php echo sprintf('%s/nfp/%d', get_bloginfo('url'), $p->id);?>">contact</a> ]
</li>
<?php endforeach ?>
</ul>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php
die();
}
/**
* Generate JSON object with paragraph tags based on paragraph id
* @param @id_paragraph paragraph id from query string
* @return string JSON string
*/
function informea_get_decision_paragraph_tags() {
$id_paragraph = get_request_int('id_paragraph', 0);
if ($id_paragraph > 0) {
$arr = array();
$ob = new informea_decisions();
$tags = $ob->get_paragraph_tags($id_paragraph);
foreach ($tags as $tag) {
$arr[] = array('id' => $tag->id, 'term' => $tag->term);
}
header('Content-Type:application/json');
echo json_encode($arr);
}
die();
}
/**
* Generate HTML with paragraph tags based on paragraph id
* @param @id_paragraph paragraph id from query string
* @return string HTML data
*/
function informea_get_decision_paragraph_tags_html() {
$id_paragraph = get_request_int('id_paragraph', 0);
$ret = '';
if ($id_paragraph > 0) {
$ob = new informea_decisions();
$tags = $ob->get_paragraph_tags($id_paragraph);
if(count($tags)) {
foreach($tags as $tag) {
$ret .= sprintf('<a href="%s/terms/%s" target="_blank">%s</a><br />', get_bloginfo('url'), $tag->id, $tag->term);
}
} else {
$ret = 'This article has not been tagged';
}
}
header('Content-Type:text/html');
echo $ret;
die();
}
class informea_treaties extends imea_treaties_page {
public $expand;
function __construct($id_treaty = NULL, $arr_parameters = array()) {
parent::__construct($id_treaty, $arr_parameters);
}
static function get_treaty_from_request() {
$id = get_request_variable('id');
$treaty = self::get_treaty_by_odata_name($id);
if(empty($treaty)) { // Compatibility
$treaty = self::get_treaty_by_id($id);
}
return $treaty;
}
function handle_view() {
global $expand, $id_treay;
// Global
$this->expand = get_request_variable('expand', 'str', 'treaty');
$this->id_treaty = get_request_variable('id_treaty', 0);
if ($this->is_index()) {
get_template_part('imea_pages/treaties/page');
exit(0);
}
// Administrative
if (current_user_can('manage_options')) {
if ($this->get_action() == 'delete_paragraph') {
$id_paragraph = get_request_int('id_paragraph');
$this->delete_paragraph($id_paragraph);
}
if ($this->get_action() == 'delete_article') {
$id_article = get_request_int('id_article');
$this->delete_article($id_article);
}
}
// Print treaty page
if ($this->expand == 'print') {
get_template_part('imea_pages/treaties/page', 'treaty-print');
exit(0);
}
}
/**
* Retrieve the list of decisions for this treaty
* @param $id_treaty integer Treaty Identified
* @return integer Number of decisions for this treaty
*/
static function get_decisions_count($id_treaty) {
global $wpdb;
$sql = $wpdb->prepare(
"SELECT COUNT(*) AS cnt
FROM ai_decision
WHERE id_treaty = %d AND status <> 'retired'
ORDER BY published DESC",
$id_treaty
);
return $wpdb->get_var($sql);
}
/**
* Retrieve decisions grouped by meeting
* @param $id_treaty integer Treaty identified
* @return array
*/
static function group_decisions_by_meeting($id_treaty) {
if($id_treaty != 5) {
return parent::group_decisions_by_meeting($id_treaty);
} else {
global $wpdb;
$ret = array();
// Hack! Stockholm do not have id_meeting, darn it!
$meetings = $wpdb->get_results(
'SELECT DISTINCT meeting_title AS title
FROM ai_decision
WHERE id_treaty=5
ORDER BY meeting_title DESC'
);
foreach($meetings as &$row) {
$row->decisions = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM ai_decision WHERE id_treaty=5 AND meeting_title=%s', $row->title
));
$ret[] = $row;
}
return $ret;
}
}
/**
* @param string $odata_name
* @return stdClass
*/
static function get_treaty_by_odata_name($odata_name) {
global $wpdb;
return $wpdb->get_row("SELECT * FROM ai_treaty WHERE odata_name = '$odata_name' AND use_informea=1");
}
/**
* Return number of treaties from a region
* @param $region to check
* @return number of treaties
*/
function region_has_treaties($region) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ai_treaty WHERE region = %s AND use_informea=1", $region));
}
/**
* Retrieve active treaties all or by title
* @param $title Treaty title
* @return WP SQL result object
*/
static function get_treaties($title = null) {
global $wpdb;
if ($title) {
return $wpdb->get_results("SELECT * FROM ai_treaty WHERE enabled = 1 AND use_informea=1 AND (short_title LIKE '%$title%' OR long_title LIKE '%$title%') ORDER BY short_title");
} else {
return $wpdb->get_results("SELECT * FROM ai_treaty WHERE enabled = 1 AND use_informea=1 ORDER BY short_title");
}
}
static function get_treaties_keyed_by_id() {
$ret = array();
$rows = self::get_treaties();
foreach($rows as $row) {
$ret[$row->id] = $row;
}
return $rows;
}
static function generate_vcard($id_contact) {
$contact = self::get_contact_for_id($id_contact);
if(!empty($contact)) {
header("Content-type: text/x-vcard; charset=utf-8");
header("Content-Disposition: filename=\"" . $contact->first_name . " " . $contact->last_name . ".vcf\"");
echo "BEGIN:VCARD\n";
echo "VERSION:2.1\n";
echo "N:" . $contact->last_name . ";" . $contact->first_name . ";;" . $contact->prefix . "\n";
echo "FN:" . $contact->first_name . " " . $contact->last_name . "\n";
echo "ORG:" . $contact->institution . ";" . $contact->department . "\n";
echo "TITLE:" . $contact->position . "\n";
echo "TEL;WORK;VOICE:" . $contact->telephone . "\n";
echo "TEL;WORK;FAX:" . $contact->fax . "\n";
echo "EMAIL;PREF;INTERNET:" . $contact->email . "\n";
echo "ADR;WORK:;;" . str_replace("\r\n", " ", $contact->address) . ";;;;" . $contact->country_name . "\n";
echo "END:VCARD\n";
} else {
die('No such entity');
}
}
static function send_message_to_nfp_validate() {
$errors = array();
$cname = get_request_value('cname');
if(empty($cname)) {
$errors[] = 'Your name is required';
}
$email = get_request_value('email');
if(empty($email)) {
$errors[] = 'Your e-mail address is required';
} else {
if(!is_email($email)) {
$errors[] = 'Invalid e-mail address';
}
}
$message = get_request_value('message');
if(empty($message)) {
$errors[] = 'Message cannot be empty';
}
$id = get_request_variable('id_nfp');
$contact = self::get_contact_for_id($id);
if(empty($contact)) {
$errors[] = 'Invalid contact. Contact is not in our database';
}
$options = get_option('informea_options');
$private_key = $options['recaptcha_private'];
$resp = recaptcha_check_answer($private_key, $_SERVER['REMOTE_ADDR'],
get_request_value('recaptcha_challenge_field'),
get_request_value('recaptcha_response_field')
);
if (!$resp->is_valid) {
$errors[] = 'Invalid security. Please enter the correct captcha keywords';
}
return $errors;
}
static function send_message_to_nfp() {
$errors = array();
$id = get_request_variable('id_nfp');
$contact = self::get_contact_for_id($id);
$copy = get_request_boolean('copy');
$to = $contact->email;
$subject = __('Contact request sent from InforMEA portal', 'informea');
$body = get_request_value('message');
$cname = get_request_value('cname');
$email = get_request_value('email');
$fullbody = $body;
$fullbody .= "\n\n\n";
$fullbody .= sprintf(
__('This email was automatically send by the InforMEA portal (http://www.informea.org) because a person (%s <%s>) requested to contact you via our portal', 'informea'),
$cname, $email
);
$header = sprintf('From: %s <%s>', $cname, $email);
if (mail($to, $subject, $fullbody, $header)) {
if($copy) {
$contact_prefix = $contact->prefix;
$contact_first_name = $contact->first_name;
$contact_last_name = $contact->last_name;
$subject = __("Copy of contact request sent from InforMEA portal", 'informea');
$fullbody = "This email was automatically sent by the InforMEA portal (http://www.informea.org) because you requested to contact $contact_prefix $contact_first_name $contact_last_name via our portal. \n\n Your message was: $body";
if (!mail($email, $subject, $fullbody, $header)) {
$errors[] = __("Cannot send you the copy of the email.", 'informea');
}
}
} else {
$errors[] = 'Cannot send e-mail. Please contact informea.org administrators through feedback form and raise this issues';
}
return $errors;
}
/**
* Retrieve treaties list by theme based on region
* @param $region string Region to get treaties from
* @return array with WP SQL result objects grouped by theme
*/
function get_treaties_by_region_by_theme($region = '') {
global $wpdb;
if (strtolower($region) == 'global') {
$region = '';
}
$data = $wpdb->get_results($wpdb->prepare("SELECT a.*, b.depository AS depository
FROM ai_treaty a
INNER JOIN ai_organization b ON a.id_organization = b.id
WHERE a.enabled = 1 AND a.use_informea=1 AND a.region = %s ORDER BY a.`theme`, a.`order`", $region));
$ret = array();
foreach ($data as &$row) {
if (!isset($ret[$row->theme])) {
$ret[$row->theme] = array();
}
$ret[$row->theme][] = $row;
}
return $ret;
}
static function get_cloud_terms_for_treaty_page($id, $limit = 20) {
$ret = array();
if (!empty($id)) {
$ret = self::get_popular_terms($id, $limit);
}
return $ret;
}
function tab_decisions_meeting_title($meeting) {
return !empty($meeting->abbreviation) ? $meeting->abbreviation : $meeting->title;
}
function decisions_with_index_page() {
global $wpdb;
return $wpdb->get_col(
"SELECT DISTINCT(a.id) FROM ai_decision a
INNER JOIN ai_decision_paragraph b ON a.id = b.id_decision
UNION
SELECT id FROM ai_decision
WHERE (summary IS NOT NULL AND TRIM(summary) <> '') OR (body IS NOT NULL AND TRIM(body) <> '')");
}
function page_decisions_overview_decision_link($decision, $treaty) {
static $ids = NULL;
if($ids === NULL) {
$ids = $this->decisions_with_index_page();
}
$text = ucwords(strtolower(self::get_title($decision)));
if(!in_array($decision->id, $ids)) {
return $text;
} else {
$no = $decision->number;
$url = sprintf('%s/treaties/%s/decisions/%d', get_bloginfo('url'), $treaty->odata_name, $decision->id);
return sprintf('<a class="title" name="decision-%d" href="%s">%s</a>', $no, $url, $text);
}
}
/**
* Retrieve featured item "of the day".
*
* @param int $interval (Optional) Interval in seconds to preserve featured item. Default 24h (86400 seconds)
* @param bool $reset (Optional) Force reset the featured item. Default FALSE
* @return Featured object
*/
static function get_featured_treaty($interval = 86400,$reset = FALSE) {
$option = get_option('informea_options');
$treaty = NULL;
if (isset($option['featured_treaty'])) {
$d = $option['featured_treaty_timestamp'];
if (time() - $d < $interval) {
$treaty = $option['featured_treaty'];
}
}
if (empty($treaty) || $reset) {
$treaty = self::get_random_treaty_having_data();
$option['featured_treaty'] = $treaty;
$option['featured_treaty_timestamp'] = time();
update_option('informea_options', $option);
}
return $treaty;
}
static function get_random_treaty() {
$ob = new self();
$treaties = $ob->get_treaties();
return $treaties[array_rand($treaties)];
}
static function get_random_treaty_having_data() {
global $wpdb;
return $wpdb->get_row('
SELECT z.* FROM ai_decision x
INNER JOIN ai_people_treaty y ON x.id_treaty = y.id_treaty
INNER JOIN ai_treaty z ON x.id_treaty = z.id
GROUP BY z.id
ORDER BY RAND() LIMIT 1'
);
}
static function ui_secondary_theme($treaty) {
if (!empty($treaty->theme_secondary)) {
echo sprintf('<div class="clear"></div><span class="theme">(%s)</span>', $treaty->theme_secondary);
}
}
function admin_delete_article() {
if ($this->get_action() == 'delete_article' && current_user_can('manage_options')) {
if(!check_admin_referer('treaty_delete_article')) {
die('Security check');
}
$id_article = get_request_int('id_article');
$this->delete_article($id_article);
return __('Article has been successfully deleted', 'informea');
}
return FALSE;
}
function admin_delete_article_paragraph() {
if ($this->get_action() == 'delete_article_paragraph' && current_user_can('manage_options')) {
if(!check_admin_referer('treaty_delete_paragraph')) {
die('Security check');
}
$id_paragraph = get_request_int('id_paragraph');
$this->delete_paragraph($id_paragraph);
return __('Paragraph has been successfully deleted', 'informea');
}
return FALSE;
}
static function admin_article_edit_url($article) {
echo sprintf(
'%sadmin.php?page=informea_treaties&act=treaty_edit_article&id_treaty=%s&id_treaty_article=%s',
admin_url(), $article->id_treaty, $article->id);
}
static function article_url($treaty, $article) {
echo sprintf(
'%s/treaties/%s?id_treaty_article=%s#article_%s',
get_bloginfo('url'), $treaty->odata_name, $article->id, $article->id);
}
static function paragraph_url($treaty, $article, $paragraph) {
echo sprintf(
'%s/treaties/%s?id_treaty_article=7#article_7_paragraph_2305',
get_bloginfo('url'), $treaty->odata_name, $article->id, $article->id, $paragraph->id);
}
static function admin_article_add_paragraph_url($article) {
echo sprintf(
'%sadmin.php?page=informea_treaties&act=treaty_add_article_paragraph&id_treaty=%s&id_treaty_article=%s',
admin_url(), $article->id_treaty, $article->id);
}
static function admin_paragraph_edit_url($article, $paragraph) {
echo sprintf(
'%sadmin.php?page=informea_treaties&act=treaty_edit_article_paragraph&id_treaty=%s&id_treaty_article=%s&id_treaty_article_paragraph=%s',
admin_url(), $article->id_treaty, $article->id, $paragraph->id);
}
static function admin_paragraph_insert_below_url($article, $paragraph) {
echo sprintf(
'%sadmin.php?page=informea_treaties&act=treaty_add_article_paragraph&id_treaty=%s&id_treaty_article=%s&order=%s',
admin_url(), $article->id_treaty, $article->id, ($paragraph->order + 1));
}
static function decision_url($treaty, $decision) {
echo sprintf(
'%s/treaties/%s/decisions?showall=1#decision-%s',
get_bloginfo('url'), $treaty->odata_name, $decision->id);
}
static function decision_paragraph_url($treaty, $decision, $paragraph) {
echo self::get_decision_paragraph_url($treaty, $decision, $paragraph);;
}
static function get_decision_paragraph_url($treaty, $decision, $paragraph) {
return sprintf(
'%s/treaties/%s/decisions/%s#paragraph-%s',
get_bloginfo('url'), $treaty->odata_name, $decision->id, $paragraph->id);
}
static function nfp_vcard_url($nfp) {
echo sprintf('%s/download?entity=vcard&id=%s', get_bloginfo('url'), $nfp->id);
}
static function nfp_contact_url($nfp) {
echo sprintf('%s/nfp/%s', get_bloginfo('url'), $nfp->id);
}
}
class informea_decisions extends imea_decisions_page {
function __construct($arr_parameters = array()) {
parent::__construct($arr_parameters);
}
/**
* Retrieve the list of treaties
*/
function get_treaties_list() {
global $wpdb;
$ret = array();
// Get the themes
$sql = "SELECT DISTINCT a.theme FROM ai_treaty a INNER JOIN ai_decision b ON b.id_treaty = a.id WHERE a.enabled = 1 AND a.use_informea=1 ORDER BY a.theme";
$rows = $wpdb->get_results($sql);
foreach ($rows as $row) {
$ret[$row->theme] = array();
}
$sql = "SELECT a.*, a.logo_medium, a.theme FROM ai_treaty a INNER JOIN ai_decision c ON c.id_treaty = a.id WHERE enabled = 1 AND use_informea=1 GROUP BY a.id ORDER BY a.order";
$rows = $wpdb->get_results($sql);
foreach ($rows as $row) {
$ret[$row->theme][] = $row;
}
return $ret;
}
function get_paragraph_tags($id_paragraph) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare('SELECT b.* FROM ai_decision_paragraph_vocabulary a INNER JOIN voc_concept b ON a.id_concept = b.id WHERE a.id_decision_paragraph=%d', $id_paragraph));
}
static function tag_decision_paragraphs_url($treaty, $decision) {
echo sprintf('%s/admin.php?page=informea_decisions&act=decision_edit_decision&id_treaty=%s&id_decision=%s',
admin_url(), $treaty->id, $decision->id);
}
static function edit_decision_url($treaty, $decision) {
echo sprintf('%s/admin.php?page=informea_decisions&act=decision_edit&id_treaty=%s&id_decision=%s',
admin_url(), $treaty->id, $decision->id);
}
static function page_decision_index_decision_content($decision) {
$paragraphs = self::get_paragraphs($decision->id);
if(count($paragraphs)):
?>
<ul class="paragraphs">
<?php foreach ($paragraphs as $paragraph) :
$tags = self::get_decision_paragraph_tags($paragraph->id);
?>
<li data-id="<?php echo $paragraph->id; ?>"<?php echo count($tags) ? ' class="smallipop"': ''; ?>>
<a name="paragraph-<?php echo $paragraph->id; ?>"></a>
<span class="gray">[…]</span> <?php echo $paragraph->content; ?> <span class="gray">[…]</span>
<?php if(count($tags)): ?>
<span class="smallipop-hint">
<?php foreach($tags as $tag): ?>
<a href="<?php echo sprintf('%s/terms/%s', get_bloginfo('url'), $tag->id); ?>"><?php echo $tag->term;?></a>
<br />
<?php endforeach; ?>
</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
elseif(!empty($decision->body)):
echo $decision->body;
elseif (!empty($decision->summary)):
echo '<strong>Summary</strong>:<br />' . $decision->summary;
endif;
}
}
class informea_countries extends imea_countries_page {
function __construct($id_country = NULL, $arr_parameters = array()) {
parent::__construct($id_country, $arr_parameters);
}
static function get_id_from_request() {
$id = get_request_variable('id');
if(!is_numeric($id)) {
$country = self::get_country_by_iso($id);
$id = $country->id;
}
return $id;
}
static function get_treaties_with_membership() {
global $wpdb;
return $wpdb->get_results('SELECT b.* FROM ai_treaty_country a
INNER JOIN ai_treaty b ON b.`id` = a.`id_treaty` WHERE b.use_informea=1
GROUP BY b.`id` ORDER BY b.`short_title`');
}
/**
* Access ai_country
* @return Rows from the table
*/
function _get_country() {
global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM ai_country WHERE id = '%s'", $this->id_country);
$this->country = $wpdb->get_row($sql);
if ($this->country) {
$sql = $wpdb->prepare('SELECT a.*, b.year FROM ai_treaty a
JOIN ai_treaty_country b ON b.id_treaty = a.id
WHERE a.enabled = TRUE AND b.id_country = %d AND a.use_informea=1 ORDER BY a.short_title', $this->id_country);
$this->mea_membership = $wpdb->get_results($sql);
}
}
/**
* Retrieve the list of national focal points grouped by treaty
* @param integer $id_country Country ID. If NULL, internal ID is used
* @return array Array of treaty objects having set property focal_points as array of National Focal Points.
* @global $wpdb WordPress database
*/
static function get_focal_points_by_treaty($id_country = NULL) {
global $wpdb;
$treaties = $wpdb->get_results(
$wpdb->prepare(
'SELECT * FROM ai_treaty WHERE use_informea=1 AND id IN (SELECT DISTINCT(id_treaty) FROM view_people_treaty WHERE id_country=%d GROUP BY id_treaty)', $id_country
), OBJECT_K
);
$rows = $wpdb->get_results(
$wpdb->prepare('SELECT * FROM view_people_treaty WHERE id_country=%d ORDER BY country_name, first_name, last_name', $id_country)
);
foreach ($rows as $row) {
$treaty = $treaties[$row->id_treaty];
if (!isset($treaty->focal_points)) {
$treaty->focal_points = array();
}
$treaty->focal_points[] = $row;
}
return $treaties;
}
static function get_focal_point_position($nfp, $prefix = '(', $suffix = ')') {
if(!empty($nfp->position)) {
return sprintf('%s%s%s', $prefix, $nfp->position, $suffix);
} else if(!empty($nfp->institution)) {
return sprintf('%s%s%s', $prefix, $nfp->institution, $suffix);
} else if(!empty($nfp->department)) {
return sprintf('%s%s%s', $prefix, $nfp->department, $suffix);
}
return FALSE;
}
static function get_focal_point_name($nfp) {
$ret = '';
if(!empty($nfp->prefix)) {
$ret .= $nfp->prefix . ' ';
}
if(!empty($nfp->first_name)) {
$ret .= $nfp->first_name . ' ';
}
if(!empty($nfp->last_name)) {
$ret .= $nfp->last_name;
}
return $ret;
}
static function get_focal_point_treaties($nfp) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare(
'SELECT a.* FROM ai_treaty a INNER JOIN ai_people_treaty b ON a.id = b.id_treaty WHERE b.id_people=%s',
$nfp->id
)
);
}
/**
* Retrieve national reports for a country, group by treaty
*
* @param integer $id_country Country ID
* @return array Array of treaties having property national_reports array with ai_country_report objects
*/
static function get_national_reports($id_country = NULL) {
global $wpdb;
$treaties = $wpdb->get_results(
$wpdb->prepare("SELECT b.* FROM ai_country_report a INNER JOIN ai_treaty b ON a.id_treaty = b.id WHERE b.enabled = TRUE AND b.use_informea=1 AND a.id_country=%d GROUP BY b.id", $id_country)
, OBJECT_K
);
$rows = $wpdb->get_results(
$wpdb->prepare("SELECT a.*, b.title AS meeting_title FROM ai_country_report a LEFT JOIN ai_event b ON a.id_event = b.id WHERE a.id_country=%d ORDER BY a.submission DESC", $id_country)
);
foreach ($rows as $row) {
$treaty = $treaties[$row->id_treaty];
if (!isset($treaty->national_reports)) {
$treaty->national_reports = array();
}
$treaty->national_reports[] = $row;
}
return $treaties;
}
/**
* Retrieve national plans for a country, group by treaty
*
* @param integer $id_country Country ID
* @return array Array of treaties having property national_plans array with ai_country_plan objects
*/
static function get_national_plans($id_country = NULL) {
global $wpdb;
$treaties = $wpdb->get_results(
$wpdb->prepare("SELECT b.* FROM ai_country_plan a INNER JOIN ai_treaty b ON a.id_treaty = b.id WHERE b.enabled = TRUE AND b.use_informea=1 AND a.id_country=%d GROUP BY b.id", $id_country)
, OBJECT_K
);
$rows = $wpdb->get_results(
$wpdb->prepare("SELECT a.*, b.title AS meeting_title FROM ai_country_plan a LEFT JOIN ai_event b ON a.id_event = b.id WHERE a.id_country=%d ORDER BY a.submission DESC", $id_country)
);
foreach ($rows as $row) {
$treaty = $treaties[$row->id_treaty];
if (!isset($treaty->national_plans)) {
$treaty->national_plans = array();
}
$treaty->national_plans[] = $row;
}
return $treaties;
}
static function generate_parties_download_csv() {
$language = get_request_value('lang', 'en');
$split = ('en' == $language) ? ',' : ';';
$newline = "\r\n";
$page_data = new imea_countries_page(null);
$data = $page_data->index_grid();
$columns = $data['column'];
$countries = $data['countries'];
$signatures = $data['signatures'];
$line = 'Country';
foreach ($columns as $column) {
$line .= $split . $column->short_title;
}
$line .= $newline;
echo ($line);
foreach ($countries as $country) {
$line = $country->name;
foreach ($columns as $column) {
$id_treaty = $column->id;
$id_country = $country->id;
$coldata = '';
if (isset($signatures[$id_treaty])) {
$tmparr = $signatures[$id_treaty];
if (isset($tmparr[$id_country])) {
$coldata = $tmparr[$id_country];
}
}
$line .= $split . $coldata;
}
$line .= $newline;
echo ($line);
}
}
}
class informea_meetings extends imea_meetings_page {
public static function get_event_types() {
return array(
'' => '-- All --',
'cop' => 'COP/MOP',
'conference' => 'Conference',
'working' => 'Working',
'workshop' => 'Workshop',
'symposia' => 'Symposia',
'expert' => 'Expert',
'subsidiary' => 'Subsidiary'
);
}
public static function ajax_get_event_list() {
$rows = self::get_event_list();
header('Content-Type:application/json');
echo json_encode($rows);
die();
}
public static function ajax_get_event_list_html() {
$rows = self::get_event_list();
$ret = '';
header('Content-Type:text/html');
foreach($rows as $row) {
$ret .= self::event_to_html($row) . "\n";
}
echo $ret;
die();
}
public static function event_to_html($e, $fe_type = NULL) {
$cop_class = (($e->type == 'cop' || $e->type == 'mop') && $fe_type != 'cop') ? ' cop' : '';
?>
<li>
<div class="date">
<div class="month"><?php echo format_mysql_date($e->start, 'M'); ?></div>
<div class="day"><?php echo format_mysql_date($e->start, 'j'); ?></div>
<div class="year"><?php echo format_mysql_date($e->start, 'Y'); ?></div>
</div>
<div class="description<?php echo $cop_class; ?>">
<h3><?php echo $e->title; ?></h3>
<div class="clear"></div>
<ul class="info">
<li>
<a href="<?php echo self::url_treaty_filter($e->id_treaty); ?>" title="See all <?php echo $e->treaty; ?> meetings for this year"><?php echo $e->treaty; ?></a>
</li>
<?php if(!empty($e->event_url)) : ?>
<li>
<div class="info"><a target="_blank" href="<?php echo $e->event_url;?>">View info</a></div>
</li>
<?php endif; ?>
<?php if(!empty($e->type)) : ?>
<li>
<div class="info"><span class="type"><?php echo ucfirst($e->type); ?></div>
</li>
<?php endif; ?>
<?php if(!empty($e->kind)) : ?>
<li>
<div class="info"><?php imea_meetings_page::decode_kind($e); ?></div>
</li>
<?php endif; ?>
</ul>
</div>
<div class="clear"></div>
</li>
<?php
}
public static function url_treaty_filter($id_treaty) {
$page_size = get_request_int('fe_page_size', 50);
$fe_type = get_request_value('fe_type');