-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_informea.php
565 lines (515 loc) · 18.7 KB
/
functions_informea.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
<?php
/**
* Author: Cristian Romanescu <cristi _at_ eaudeweb dot ro>
* Created: 201309171048
*/
class InforMEA {
/**
* Retrieve treaty by its odata_name identifier
* @param $odata_name string as comes from OData protocol
* @return stdClass Treaty object or NULL
*/
static function get_treaty_by_odata_name($odata_name) {
global $wpdb;
return $wpdb->get_row(
$wpdb->prepare('SELECT * FROM ai_treaty WHERE odata_name = %s', $odata_name)
);
}
/**
* Retrieve organization by primary key
* @param $id int Organization ID
* @return stdClass Organization object
*/
static function get_organization($id) {
global $wpdb;
return $wpdb->get_row(
$wpdb->prepare('SELECT * FROM ai_organization WHERE id = %d', $id)
);
}
/**
* Retrieve InforMEA enabled treaties
* @return array Array of treaty objects
*/
static function get_treaties_enabled($order_by = 'a.`order`') {
global $wpdb;
$sql = $wpdb->prepare('SELECT a.* FROM ai_treaty a WHERE a.enabled = 1 AND a.use_informea = 1', array());
$sql .= " ORDER BY $order_by";
return $wpdb->get_results($sql);
}
/**
* Count the total number of InforMEA enabled treaties
* @return int Count
*/
static function get_treaties_enabled_count() {
global $wpdb;
$sql = $wpdb->prepare('SELECT count(*) FROM ai_treaty a WHERE a.enabled = 1 AND a.use_informea = 1', array());
return $wpdb->get_var($sql);
}
/**
* Retrieve the primary treaty topics from the database
* @return array Array of string with the terms
*/
static function get_treaties_enabled_primary_topics() {
global $wpdb;
$sql = $wpdb->prepare("SELECT DISTINCT (a.theme) AS theme FROM ai_treaty a
WHERE TRIM(a.theme) <> '' AND a.enabled = 1 AND a.use_informea = 1 ORDER BY theme",
array()
);
return $wpdb->get_col($sql);
}
/**
* Retrieve the unique regions in use within a treaty
* @return array Array of object classes from ai_region table
*/
static function get_treaties_enabled_regions_in_use() {
global $wpdb;
return $wpdb->get_results('SELECT a.* FROM ai_region a INNER JOIN ai_treaty_region b ON a.id = b.id_region GROUP BY a.id ORDER BY a.id');
}
/**
* Retrieve the treaty member parties.
*
* @param $id_treaty integer ID of the treaty
* @return array Array with membership information
*/
static function get_treaty_member_parties($id_treaty) {
global $wpdb;
$ret = array();
$rows = $wpdb->get_results(
$wpdb->prepare('
SELECT a.*, b.name AS country, b.icon_medium AS flag
FROM ai_treaty_country a
INNER JOIN ai_country b ON a.id_country = b.id
WHERE a.id_treaty = %d
ORDER BY b.name', $id_treaty), OBJECT_K
);
foreach($rows as $id => $row) {
$update = array_key_exists($id, $ret);
$ob = $update ? $ret[$id] : new stdClass();
$ob->country = $row->country;
$ob->entryIntoForce = '';
$ob->signed = '';
$ob->notes = $row->notes;
switch($row->status) {
case 'entryIntoForce':
$ob->entryIntoForce = $row->date;
$ob->status = 'Party';
break;
case 'signature':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Signature';
}
break;
case 'succesion':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Succession';
}
break;
case 'ratification':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Ratification';
}
break;
case 'acceptance':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Acceptance';
}
break;
case 'approval':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Approval';
}
break;
case 'accesion':
$ob->signed = $row->date;
if(!$update) {
$ob->status = 'Accesion';
}
break;
}
$ret[$row->id_country] = $ob;
}
return $ret;
}
static function get_treaty_cop_meetings($id_treaty) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare("
SELECT a.* FROM ai_event a
INNER JOIN ai_decision b ON b.id_meeting = a.id
WHERE a.id_treaty = %d AND LOWER(a.`type`) IN ('cop', 'mop')
GROUP BY a.id
ORDER BY a.`start` DESC
", $id_treaty
)
);
}
static function get_treaty_decisions_count($id_treaty) {
global $wpdb;
return $wpdb->get_var(
$wpdb->prepare('SELECT COUNT(*) FROM ai_decision a WHERE a.id_treaty = %d', $id_treaty)
);
}
static function get_treaty_decisions_by_cop($id_event) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare('
SELECT a.id, a.short_title, a.number, a.type, a.status
FROM ai_decision a
INNER JOIN ai_event b ON a.id_meeting = b.id
WHERE b.id = %d
ORDER BY b.start DESC, a.`display_order` ASC
', $id_event)
);
}
/**
* Retrieve articles for a treaty
*
* @param $id_treaty integer Treaty ID
* @return array Array with article objects
*/
static function get_treaty_articles($id_treaty) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare(
'SELECT a.* FROM ai_treaty_article a WHERE a.id_treaty = %d ORDER BY a.order',
$id_treaty
), OBJECT_K
);
}
/**
* Retrieve all paragraphs for a treaty
*
* @param $id_treaty integer Treaty ID
* @return array Array of paragraph objects keyed by article ID
*/
static function get_treaty_paragraphs($id_treaty) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare('
SELECT a.id, a.id_treaty_article AS id_article, a.official_order, a.`order`, a.`indent`, a.`content`
FROM ai_treaty_article_paragraph a
INNER JOIN ai_treaty_article b ON b.id = a.id_treaty_article
WHERE b.id_treaty = %d
ORDER BY a.`order`',
$id_treaty
), OBJECT_K
);
}
/**
* Load all tags for a treaty, broken down by paragraph
* @param $id_treaty integer ID of the treaty
* @return array id_paragraph as key, array of tags as values
*/
private static function get_treaty_paragraph_tags($id_treaty) {
global $wpdb;
$rows = $wpdb->get_results(
$wpdb->prepare('
SELECT c.*, a.id_treaty_article_paragraph AS id_paragraph
FROM ai_treaty_article_paragraph_vocabulary a
INNER JOIN ai_treaty_article b ON a.id_treaty_article_paragraph = b.id
INNER JOIN voc_concept c ON a.id_concept = c.id
WHERE b.id_treaty = %d;
', $id_treaty)
);
$ret = array();
foreach($rows as $row) {
$ret[$row->id_paragraph][$row->id] = $row;
}
return $ret;
}
/**
* @param $id_treaty integer ID of the treaty
* @return array id_paragraph as key, array of tags as values
*/
private static function get_treaty_article_tags($id_treaty) {
global $wpdb;
$rows = $wpdb->get_results(
$wpdb->prepare('
SELECT c.*, a.id_treaty_article AS id_article
FROM ai_treaty_article_vocabulary a
INNER JOIN ai_treaty_article b ON a.id_treaty_article = b.id
INNER JOIN voc_concept c ON a.id_concept = c.id
WHERE b.id_treaty = %d;
', $id_treaty)
);
$ret = array();
foreach($rows as $row) {
$ret[$row->id_article][$row->id] = $row;
}
return $ret;
}
/**
* This method loads all the treaty articles, paragraphs and tags.
* @param $id_treaty integer ID of the treaty
* @return array Array of articles. Each article has a property tags and one paragraphs.
* array(id_article => article)
* article->tags = array(id_concept => term)
* article->paragraphs = array(id_paragraph => paragraph)
* paragraph->tags = array(id_concept => term)
*/
static function load_full_treaty_text($id_treaty) {
$paragraphs = self::get_treaty_paragraphs($id_treaty);
$paragraph_tags = self::get_treaty_paragraph_tags($id_treaty);
foreach($paragraphs as $id_paragraph => &$row) {
if(array_key_exists($id_paragraph, $paragraph_tags)) {
$row->tags = $paragraph_tags[$id_paragraph];
}
}
$grouped_paragraphs = array();
foreach($paragraphs as $row) {
$grouped_paragraphs[$row->id_article][] = $row;
}
$articles = self::get_treaty_articles($id_treaty);
$article_tags = self::get_treaty_article_tags($id_treaty);
// Assign paragraphs to articles
foreach($articles as $id_article => &$row) {
$row->tags = FALSE;
// Article tags
if(array_key_exists($id_article, $article_tags)) {
$row->tags = $article_tags[$id_article];
}
$row->paragraphs = FALSE;
if(array_key_exists($id_article, $grouped_paragraphs)) {
$row->paragraphs = $grouped_paragraphs[$id_article];
}
}
return $articles;
}
/**
* Retrieve the list of countries. Statically cached.
*
* @return array Array of objects with country information
*/
static function get_countries() {
static $ret = array();
if(empty($ret)) {
global $wpdb;
$ret = $wpdb->get_results('SELECT * FROM ai_country ORDER BY name', OBJECT_K);
}
return $ret;
}
/**
* Retrieve a country by its ISO code.
*
* @param $code string ISO code
* @return mixed Country object or FALSE if not found
*/
static function get_country($code) {
$countries = self::get_countries();
foreach($countries as $c) {
if($c->code == $code || $c->code2l == $code) {
return $c;
}
}
return FALSE;
}
/**
* Return the top terms used to tag a treaty (articles, paragraphs, decisions etc.).
*
* @param $id_treaty integer Treaty ID
* @param $limit integer Number of terms to return
*
* @return array Array of terms;
*/
static function get_treaty_popular_tags($id_treaty, $limit = 10) {
global $wpdb;
$ret = array();
$tmp = array();
// Treaty / Article / Paragraph / Tags
$rows = $wpdb->get_results(
$wpdb->prepare('SELECT a.id_concept, COUNT(a.id_concept) AS c
FROM ai_treaty_article_paragraph_vocabulary a
INNER JOIN ai_treaty_article b ON a.id_treaty_article_paragraph = b.id
WHERE b.id_treaty = %d GROUP BY a.id_concept HAVING COUNT(a.id_concept) > 1
ORDER BY count(a.id_concept) DESC', $id_treaty),
OBJECT_K
);
foreach($rows as $id => $row) {
if(!array_key_exists($id, $tmp)) {
$tmp[$id] = 0;
}
$tmp[$id] += $row->c;
}
// Treaty / Article / Tags
$rows = $wpdb->get_results(
$wpdb->prepare('SELECT a.id_concept, COUNT(a.id_concept) AS c
FROM ai_treaty_article_vocabulary a
INNER JOIN ai_treaty_article b ON a.id_treaty_article = b.id
WHERE b.id_treaty = %d GROUP BY a.id_concept HAVING COUNT(a.id_concept) > 1
ORDER BY count(a.id_concept) DESC', $id_treaty), OBJECT_K
);
foreach($rows as $id => $row) {
if(!array_key_exists($id, $tmp)) {
$tmp[$id] = 0;
}
$tmp[$id] += $row->c;
}
// Treaty / Tags
$rows = $wpdb->get_results(
$wpdb->prepare('SELECT a.id_concept, COUNT(a.id_concept) AS c
FROM ai_treaty_vocabulary a WHERE a.id_treaty = %d GROUP BY a.id_concept
HAVING COUNT(a.id_concept) > 1 ORDER BY count(a.id_concept) DESC', $id_treaty),
OBJECT_K
);
foreach($rows as $id => $row) {
if(!array_key_exists($id, $tmp)) {
$tmp[$id] = 0;
}
$tmp[$id] += $row->c;
}
// Treaty / Decisions / Paragraphs / Tags
$rows = $wpdb->get_results(
$wpdb->prepare('SELECT a.id_concept, COUNT(a.id_concept) AS c
FROM ai_decision_paragraph_vocabulary a
INNER JOIN ai_decision_paragraph b ON a.id_decision_paragraph = b.id
INNER JOIN ai_decision c ON b.id_decision = c.id
WHERE c.id_treaty = %d GROUP BY a.id_concept HAVING COUNT(a.id_concept) > 1
ORDER BY count(a.id_concept) DESC', $id_treaty), OBJECT_K
);
foreach($rows as $id => $row) {
if(!array_key_exists($id, $tmp)) {
$tmp[$id] = 0;
}
$tmp[$id] += $row->c;
}
// Treaty / Decisions / Tags
$rows = $wpdb->get_results(
$wpdb->prepare('
SELECT a.id_concept, COUNT(a.id_concept) AS c
FROM ai_decision_vocabulary a INNER JOIN ai_decision b ON a.id_decision = b.id
WHERE b.id_treaty = %d GROUP BY a.id_concept HAVING COUNT(a.id_concept) > 1
ORDER BY count(a.id_concept) DESC', $id_treaty), OBJECT_K
);
foreach($rows as $id => $row) {
if(!array_key_exists($id, $tmp)) {
$tmp[$id] = 0;
}
$tmp[$id] += $row->c;
}
// Build an array of terms and set weight on each term
$tmp = array_slice($tmp, 0, $limit, TRUE);
if(count($tmp)) {
$objects = $wpdb->get_results(
sprintf(
'SELECT * FROM voc_concept WHERE id IN (%s)',
implode(',', array_keys($tmp))
),
OBJECT_K
);
// Attach weight
$keys = array_keys($tmp);
$c = count($keys);
foreach($keys as $weight => $id_term) {
$term = $objects[$id_term];
$term->weight = ($c - $weight);
$ret[] = $term;
}
}
return $ret;
}
/**
* Retrieve the list of countries that have NFPs for the specified treaty
* @param $id_treaty integer ID of the treaty
* @return array List of country classes
*/
static function get_treaty_nfp_countries($id_treaty) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare('
SELECT c.*
FROM ai_people a
INNER JOIN ai_people_treaty b ON a.id = b.id_people
INNER JOIN ai_country c ON a.id_country = c.id
WHERE b.id_treaty = %d GROUP BY c.id ORDER BY c.name', $id_treaty
), OBJECT_K
);
}
/**
* Retrieve the total number of focal points for a treaty.
*
* @param $id_treaty integer ID of the treaty
* @return integer Global focal points count
*/
static function get_treaty_nfp_count($id_treaty) {
global $wpdb;
return $wpdb->get_var(
$wpdb->prepare('SELECT COUNT(*) FROM ai_people_treaty WHERE id_treaty = %d', $id_treaty)
);
}
/**
* Retrieve the NFPs for a treaty within a country.
*
* @param $id_treaty integer ID of the treaty
* @param $iso string Country ISO code
*
* @return array Array of people objects
*/
static function get_treaty_country_nfp($id_treaty, $iso) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare('
SELECT a.*
FROM ai_people a
INNER JOIN ai_people_treaty b ON a.id = b.id_people
INNER JOIN ai_country c ON a.id_country = c.id
WHERE b.id_treaty = %d AND (c.code = %s OR c.code2l = %s)
', $id_treaty, $iso, $iso)
);
}
/**
* Load national focal point by its ID
*
* @param $id_people integer People ID
* @return mixed stdClass Object with 'treaties' property that keeps all the treaties for the person
*/
static function get_nfp($id_people) {
global $wpdb;
$ret = $wpdb->get_row(
$wpdb->prepare('
SELECT a.*, b.icon_medium, b.icon_large, b.name AS country_name, LOWER(b.code) AS country_code
FROM ai_people a
LEFT JOIN ai_country b ON a.id_country = b.id
WHERE a.id = %d
', $id_people)
);
if(!empty($ret)) {
$ret->treaties = $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 = %d
', $id_people)
);
}
return $ret;
}
/**
* Build NFP name field based on what is filled-in.
*
* @param $nfp stdClass Person object
* @return string Name
*/
static function format_nfp_name($nfp) {
$name = '';
if(!empty($nfp->prefix)) {
$name .= $nfp->prefix . ' ';
}
if(!empty($nfp->first_name)) {
if(strlen($name)) {
$name .= ' ';
}
$name .= $nfp->first_name;
}
if(!empty($nfp->last_name)) {
if(strlen($name)) {
$name .= ' ';
}
$name .= $nfp->last_name;
}
return $name;
}
}