-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.php
2345 lines (2117 loc) · 77.3 KB
/
utils.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
function whoowns_get_owner_data($id=null,$provide_links=false,$extra_data=false) {
$posts_id = array(false);
if (is_array($id))
$posts_id = $id;
elseif ($id)
$posts_id = array($id);
elseif (!$post)
return;
$owner_data = array();
foreach ($posts_id as $i=>$post_id) {
$post = get_post($post_id);
$owner_image_size = explode('x',get_option('whoowns_owner_image_size'));
$owner_data[$i] = new stdClass();
$owner_data[$i]->ID = $post->ID;
$owner_data[$i]->name = $post->post_title;
if ($provide_links)
$owner_data[$i]->link = get_post_permalink( $post->ID );
$res = get_post_custom($post->ID);
foreach ($res as $name=>$r) {
$term = str_replace('whoowns_','',$name);
switch ($term) {
default:
$trad = (!is_serialized($r[0]))
? __($r[0],'whoowns')
: maybe_unserialize($r[0]);
$owner_data[$i]->$term = $trad;
break;
}
}
$owner_data[$i]->image = get_the_post_thumbnail( $post->ID, $owner_image_size);
$owner_data[$i]->type = whoowns_get_owner_type($post->ID,$provide_links);
if ($extra_data) {
if ($owner_data[$i]->controlled_by) {
$owner_data[$i]->controlled_by = whoowns_get_owner_data( $owner_data[$i]->controlled_by, true);
$owner_data[$i]->controlled_by_final = ($owner_data[$i]->controlled_by_final == $owner_data[$i]->controlled_by->ID)
? $owner_data[$i]->controlled_by
: whoowns_get_owner_data( $owner_data[$i]->controlled_by_final, true);
}
if ($owner_data[$i]->controls_final) {
$controls_final = $PA = array();
$controls_final_tmp = whoowns_get_owner_data(array_keys($owner_data[$i]->controls_final),true);
if (!is_array($controls_final_tmp))
$controls_final_tmp = array( $controls_final_tmp );
foreach ($controls_final_tmp as $cf) {
$controls_final[$cf->ID] = $cf;
$controls_final[$cf->ID]->is_direct_control = $owner_data[$i]->controls_final[$cf->ID]['is_direct'];
$PAs[$cf->ID] = $cf->PA;
}
arsort($PAs);
$owner_data[$i]->controls_final = array();
foreach ($PAs as $cf_id=>$PA)
$owner_data[$i]->controls_final[$cf_id] = $controls_final[$cf_id];
$owner_data[$i]->controls_final_top = whoowns_show_controls_final_top($owner_data[$i]->controls_final, 3, '', true);
}
$owner_data[$i]->shareholders = whoowns_get_direct_shareholders( $post->ID, $provide_links,$extra_data );
$owner_data[$i]->main_actors = whoowns_get_main_actors_in_network( $post->ID, $provide_links );
#$owner_data[$i]->related_posts = whoowns_get_related_posts( $post->ID );
if ($ref_id = get_option('whoowns_reference_owner'))
$owner_data[$i]->participation_of_reference_owner = whoowns_get_final_participation_between_two_owners( $ref_id, $post->ID );
}
}
return (count($owner_data)==1) ? $owner_data[0] : $owner_data;
}
function whoowns_get_owner_type($post_id,$provide_links=false) {
if (!$post_id)
return false;
$type = get_the_terms($post_id,'whoowns_owner_types');
if (!$type)
return false;
$type = array_shift(array_values($type));
$type->name = __($type->name,'whoowns');
if ($provide_links)
$type->link = get_term_link($type);
return $type;
}
function whoowns_get_direct_shareholders($post_id,$provide_links=false,$full_data=false) {
if (!$post_id)
return false;
global $whoowns_tables, $wpdb;
$post_ids = (is_array($post_id))
? $post_id
: array($post_id);
$res = array();
foreach ($post_ids as $i=>$p) {
$sql = "SELECT b.id as share_id, a.post_title as shareholder_name, b.from_id as shareholder_id, b.share, b.relative_share FROM ".$wpdb->posts." a, ".$whoowns_tables->shares." b WHERE a.ID=b.from_id AND to_id='$p' ORDER BY b.share DESC";
$res[$i] = $wpdb->get_results($sql);
if ($res[$i]) {
if ($full_data || $provide_links) {
foreach ($res[$i] as $j=>$r) {
if ($full_data) {
$data = whoowns_get_owner_data($r->shareholder_id,$provide_links);
unset($data->ID,$data->name);
foreach ($r as $f=>$v)
$data->$f = $v;
$res[$i][$j] = $data;
} else {
$res[$i][$j]->shareholder_link = get_permalink($r->shareholder_id);
}
}
}
}
}
if (count($res)==1)
$res = $res[0];
return ($res) ? $res : array();
}
function whoowns_get_direct_controller($post_id,$full_data=false) {
if (!$post_id)
return false;
$shareholders = whoowns_get_direct_shareholders($post_id,$full_data,$full_data);
foreach ($shareholders as $s)
if ($s->relative_share>50)
return $s;
return false;
}
function whoowns_get_controllers($postid, $controllers=array()) {
// Prevent loop and return false in this case:
if (in_array($postid, array_slice($controllers,0,count($controllers)-1)))
return false;
if ($direct_controller = whoowns_get_direct_controller($postid)) {
$controllers[] = $direct_controller->shareholder_id;
return whoowns_get_controllers($direct_controller->shareholder_id, $controllers);
}
return $controllers;
}
function whoowns_generate_controllers_list($postid, $format="both") {
if (!$postid)
return false;
if ($controllers = whoowns_get_controllers($postid)) {
$html = in_array($format, array('html','both'));
$array = in_array($format, array('array','both'));
$list = new stdClass();
if ($array) {
$list->data = array();
}
if ($html) {
$list->html = "";
}
foreach ($controllers as $level=>$controller_id) {
$data = whoowns_get_owner_data($controller_id,true);
if ($array) {
$list->data[$controller_id] = $data;
}
if ($html) {
$spaces = str_repeat(" ",$level*3);
$list->html .= "<p>$spaces<span class='whoowns_list_owner_name'><span class='icon-angle-double-right'></span> <a href='#cy-full' onClick=\"whoowns_select_node(this,'".$data->ID."','".$data->link."')\">".$data->name."</a></span></p>";
}
}
return $list;
}
return false;
}
function whoowns_generate_direct_shareholders_list($postid, $format="both") {
if (!$postid)
return false;
if ($direct_shareholders = whoowns_get_direct_shareholders($postid,true)) {
$html = in_array($format, array('html','both'));
$array = in_array($format, array('array','both'));
$list = new stdClass();
if ($array) {
$list->data = $direct_shareholders;
}
if ($html) {
$list->html = "";
foreach ($direct_shareholders as $ds) {
$share_txt = whoowns_format_share_percentage($ds->share);
$rel_share_txt = whoowns_format_share_percentage($ds->relative_share);
$list->html .= "<p><span class='whoowns_list_owner_name'><span class='icon-angle-double-right'></span> <a href='#cy-full' onClick=\"whoowns_select_node(this,'".$ds->shareholder_id."','".$ds->shareholder_link."')\">".$ds->shareholder_name."</a></span> <span class='whoowns_list_shares'>($share_txt)</span></span></p>";
}
}
return $list;
} else {
$type = whoowns_get_owner_type($postid);
if ($type->slug=='private-enterprise') {
$list = new stdClass();
$list->html = "<p class='description'>".__("Information not available.", "whoowns")."</p>";
}
return $list;
}
return false;
}
function whoowns_generate_indirect_shareholders_list($postid, $format="both", $list="", $route=array()) {
if (!$postid)
return false;
if ($direct_shareholders = whoowns_get_direct_shareholders($postid,true)) {
$html = in_array($format, array('html','both'));
$array = in_array($format, array('array','both'));
if (!$list) {
//echo "<b>$postid</b><br>";
$list = new stdClass();
$list->level = array($postid => array());
$list->final_share = array($postid => array());
if ($html)
$list->html = "";
$route = array($postid);
}
if ($array) {
if (!$list->data)
$list->data = array();
}
if ($html) {
$lim = (count($route)>1)
? $list->level[$route[count($route)-2]][$postid]
: 1;
for ($i=0;$i<$lim;$i++)
$spaces .= "|".str_repeat(" ",3);
$spaces .= "+";
}
foreach ($direct_shareholders as $ds) {
//Preventing loops:
if (whoowns_verify_pair_redundance($route,$ds->shareholder_id))
return $list;
//$list->final_share[$ds->shareholder_id] = 100*($ds->relative_share/100 * $list->final_share[$postid]/100);
$list->final_share[$postid][$ds->shareholder_id] = (count($route)>1)
? 100*($ds->relative_share/100 * $list->final_share[$route[count($route)-2]][$postid]/100)
: $ds->relative_share;
/*if (!$list->level[$ds->shareholder_id])
$list->level[$ds->shareholder_id] = $list->level[$postid]+1;*/
$list->level[$postid][$ds->shareholder_id] = (count($route)>1)
? $list->level[$route[count($route)-2]][$postid]+1
: 1;
if ($array) {
$data = new stdClass();
$data->target_id = $postid;
$data->shareholder_id = $ds->shareholder_id;
$data->shareholder_name = $ds->shareholder_name;
$data->share = $ds->share;
$data->relative_share = $ds->relative_share;
$data->final_share = $list->final_share[$postid][$ds->shareholder_id];
$data->level = $list->level[$postid][$ds->shareholder_id];
$list->data[$postid][$ds->shareholder_id] = $data;
}
if ($html) {
$share_txt = whoowns_format_share_percentage($ds->share);
$rel_share_txt = whoowns_format_share_percentage($ds->relative_share);
$final_share_txt = whoowns_format_share_percentage($list->final_share[$postid][$ds->shareholder_id]);
$list->html .= "<p>$spaces <span class='whoowns_list_owner_name'><a href='#cy-full' onClick=\"whoowns_select_node(this,'".$ds->shareholder_id."','".$ds->shareholder_link."')\">".$ds->shareholder_name."</a></span> (<span class='whoowns_list_shares'><span class='whoowns_list_final_share'>$final_share_txt</span>; $rel_share_txt; $share_txt)</span></p>";
}
$list = whoowns_generate_indirect_shareholders_list($ds->shareholder_id,$format,$list, array_merge($route,array($ds->shareholder_id)));
//pR($direct_shareholders); exit;
}
}
return $list;
}
function whoowns_get_final_participation_between_two_owners( $ref_id, $post_id ) {
if ($participations = whoowns_generate_indirect_shareholders_list($post_id, 'array')) {
$shares = array();
foreach ($participations->data as $target_id => $shareholders) {
foreach ($shareholders as $shareholder_id => $data) {
if ($shareholder_id == $ref_id) {
$shares[$target_id] = new stdClass();
$is_direct = ($target_id == $post_id);
if (!$is_direct)
$shares[$target_id]->target = whoowns_get_owner_data($target_id,true);
$shares[$target_id]->final_share = $data->final_share;
$shares[$target_id]->is_direct = $is_direct;
}
}
}
if ($shares) {
$ret = new stdClass();
$ret->ref = whoowns_get_owner_data($ref_id,true);
$ret->shares = $shares;
return $ret;
}
}
return false;
}
function whoowns_update_metakey_controlled_by($postids) {
if (!$postids)
return false;
if (!is_array($postids))
$postids = array($postids);
foreach ($postids as $postid) {
$controllers = whoowns_get_controllers($postid);
// echo "<h1>$postid</h1>";print_r($controllers); // for debugging
if ($controllers[0]) {
update_post_meta($postid, 'whoowns_controlled_by', $controllers[0]);
update_post_meta($postid, 'whoowns_controlled_by_final', end($controllers));
} else {
delete_post_meta($postid, 'whoowns_controlled_by');
delete_post_meta($postid, 'whoowns_controlled_by_final');
}
}
}
add_action('whoowns-update-metakey-controlled-by', 'whoowns_update_metakey_controlled_by');
function whoowns_get_direct_participations($post_id,$provide_links=false,$min_share=false) {
global $whoowns_tables, $wpdb;
$post_ids = (is_array($post_id))
? $post_id
: array($post_id);
if (is_numeric($min_share))
$min_share = "AND b.relative_share>$min_share";
$res = array();
foreach ($post_ids as $i=>$p) {
$sql = "SELECT b.id as share_id, a.ID, b.share, b.relative_share FROM ".$wpdb->posts." a, ".$whoowns_tables->shares." b WHERE a.ID=b.to_id AND from_id='$p' $min_share ORDER BY a.post_title";
$res[$i] = $wpdb->get_results($sql);
if ($res[$i]) {
foreach ($res[$i] as $j=>$r) {
$data = whoowns_get_owner_data($r->ID,$provide_links);
unset($data->ID);
foreach ($r as $f=>$v)
$data->$f = $v;
$res[$i][$j] = $data;
}
}
}
if (count($res)==1)
$res = $res[0];
return ($res) ? $res : array();
}
function whoowns_has_participations($post_id,$min_share=false) {
global $whoowns_tables, $wpdb;
$post_ids = (is_array($post_id))
? $post_id
: array($post_id);
if (is_numeric($min_share))
$min_share = "AND relative_share>$min_share";
$res = array();
foreach ($post_ids as $i=>$p) {
$sql = "SELECT id as share_id FROM ".$whoowns_tables->shares." WHERE from_id='$p' $min_share";
$res[$i] = count($wpdb->get_results($sql));
}
if (count($res)==1)
$res = $res[0];
return ($res) ? $res : array();
}
function whoowns_get_directly_controlled($post_id) {
$participations = whoowns_get_direct_participations($post_id,true);
$res = array();
foreach ($participations as $s)
if ($s->relative_share>50)
$res[$s->ID] = $s;
return $res;
}
function whoowns_get_controlled($postid, $route=array()) {
if ($directly_controlled = array_keys(whoowns_get_directly_controlled($postid))) {
$controlled = $directly_controlled;
foreach ($directly_controlled as $dc_postid) {
//check inconsistencies to prevent LOOP:
if (in_array($dc_postid,$route))
return array();
$controlled = array_merge($controlled, whoowns_get_controlled($dc_postid, array_merge($route,$controlled)));
}
}
return ($controlled) ? $controlled : array();
}
function whoowns_update_metakey_controls_final($postids) {
if (!$postids)
return false;
if (!is_array($postids))
$postids = array($postids);
foreach ($postids as $postid) {
$directly_controlled = array_keys(whoowns_get_directly_controlled($postid));
$controlled = whoowns_get_controlled($postid);
$controls_final = array();
foreach ($controlled as $c_postid) {
$is_directly_controlled = in_array($c_postid, $directly_controlled);
if (!whoowns_get_directly_controlled($c_postid)) {
$controls_final[$c_postid] = array(
'post_id'=>$c_postid,
'is_direct'=>$is_directly_controlled );
}
}
#pR($controls_final);
if ($controls_final)
update_post_meta($postid, 'whoowns_controls_final', $controls_final);
else
delete_post_meta($postid, 'whoowns_controls_final');
}
}
add_action('whoowns-update-metakey-controls-final', 'whoowns_update_metakey_controls_final');
function whoowns_generate_controls_list($postid, $format="both") {
if (!$postid)
return false;
if ($controlled = whoowns_get_controlled($postid)) {
$directly_controlled = array_keys(whoowns_get_directly_controlled($postid));
foreach ($controlled as $c_postid) {
$is_directly_controlled = (in_array($c_postid, $directly_controlled))
? 'direct'
: 'indirect';
$controls[$is_directly_controlled][] = whoowns_get_owner_data($c_postid, true);
}
$html = in_array($format, array('html','both'));
$array = in_array($format, array('array','both'));
$list = new stdClass();
if ($array) {
$list->data = new stdClass();
$list->data->direct = $controls['direct'];
$list->data->indirect = $controls['indirect'];
}
if ($html) {
$list->html = "";
foreach ($controls as $is_direct=>$controls_sub) {
$is_direct_txt = ($is_direct == "direct")
? __("Directly controlled:", "whoowns")
: __("Indirectly controlled:", "whoowns");
$list->html .= "<h4>$is_direct_txt</h4>";
foreach ($controls_sub as $c) {
$list->html .= "<p><span class='whoowns_list_owner_name'><span class='icon-angle-double-right'> <a href='#cy-full' onClick=\"whoowns_select_node(this,'".$c->ID."','".$c->link."')\">".$c->name."</a></span></span></p>";
}
}
}
} else {
$list = new stdClass();
$list->html = "<p class='description'>".__("None.", "whoowns")."</p>";
}
return $list;
}
function whoowns_show_controls_final_top($controls_final, $num, $see_all_url='', $provide_link = false) {
if (!$controls_final)
return '';
$controls_final_top = array();
$k=0;
foreach($controls_final as $cf_id=>$cf) {
$controls_final_top[$k] = ($provide_link)
? '<a href="'.get_permalink($cf_id).'">'.get_the_title($cf_id).'</a>'
: get_the_title($cf_id);
$k++; if ($num>0 && $k==$num) break;
}
$html = implode(', ',$controls_final_top);
if ($num && count($controls_final)>$num) {
$html .= '...';
if ($see_all_url)
$html .= " (<a href='$see_all_url'>".__('see all','whoowns')."</a>)";
}
return $html;
}
function whoowns_generate_participations_list($postid, $format="both", $list="", $route=array()) {
if (!$postid)
return false;
if ($direct_participations = whoowns_get_direct_participations($postid,true)) {
$html = in_array($format, array('html','both'));
$array = in_array($format, array('array','both'));
if (!$list) {
$list = new stdClass();
$list->level = array($postid => array());
$list->final_share = array($postid => array());
if ($html)
$list->html = "";
$route = array($postid);
}
if ($array) {
if (!$list->data)
$list->data = array();
}
if ($html) {
$lim = (count($route)>1)
? $list->level[$route[count($route)-2]][$postid]
: 1;
for ($i=0;$i<$lim;$i++)
$spaces .= "|".str_repeat(" ",3);
$spaces .= "+";
}
foreach ($direct_participations as $dp) {
//Let's prevent loops:
if (whoowns_verify_pair_redundance($route,$dp->ID))
return $list;
//$list->final_share[$dp->ID] = 100*($dp->relative_share/100 * $list->final_share[$postid]/100);
$list->final_share[$postid][$dp->ID] = (count($route)>1)
? 100*($dp->relative_share/100 * $list->final_share[$route[count($route)-2]][$postid]/100)
: $dp->relative_share;
/*if (!$list->level[$dp->ID])
$list->level[$dp->ID] = $list->level[$postid]+1;*/
$list->level[$postid][$dp->ID] = (count($route)>1)
? $list->level[$route[count($route)-2]][$postid]+1
: 1;
if ($array) {
$data = new stdClass();
$data->shareholder_id = $postid;
$data->target_id = $dp->ID;
$data->target_name = $dp->name;
$data->share = $dp->share;
$data->relative_share = $dp->relative_share;
$data->final_share = $list->final_share[$postid][$dp->ID];
$data->level = $list->level[$postid][$dp->ID];
$list->data[$postid][$dp->ID] = $data;
}
if ($html) {
$share_txt = whoowns_format_share_percentage($dp->share);
$rel_share_txt = whoowns_format_share_percentage($dp->relative_share);
$final_share_txt = whoowns_format_share_percentage($list->final_share[$postid][$dp->ID]);
$list->html .= "<p>$spaces <span class='whoowns_list_owner_name'><a href='#cy-full' onClick=\"whoowns_select_node(this,'".$dp->ID."','".$dp->link."')\">".$dp->name."</a></span> (<span class='whoowns_list_shares'><span class='whoowns_list_final_share'>$final_share_txt</span>; $share_txt; $rel_share_txt)</span></p>";
}
$list = whoowns_generate_participations_list($dp->ID,$format,$list,array_merge($route,array($dp->ID)));
}
}
return $list;
}
function whoowns_get_participations_of_controlled_enterprises($postid,$controlled_postid='',$controlled_postids=array()) {
if (!$postid)
return false;
$controlled_enterprises = whoowns_get_controlled($postid);
if (in_array($controlled_postid,$controlled_enterprises)) {
if ($direct_participations = whoowns_get_direct_participations($controlled_postid,false,0)) {
foreach ($direct_participations as $dp) {
$controlled_postids += whoowns_get_participations_of_controlled_enterprises($controlled_postid,$dp->ID,$controlled_postids);
}
} else {
$controlled_postids[] = $controlled_postid;
}
} else {
if ($direct_participations = whoowns_get_direct_participations($postid,false,0)) {
foreach ($direct_participations as $dp) {
if (!$controlled_postid) {
$controlled_postids += whoowns_get_participations_of_controlled_enterprises($postid,$dp->ID,$controlled_postids);
} elseif ($dp->relative_share>0 && $dp->relative_share<=50) {
$controlled_postids[] = $dp->ID;
}
}
} else {
$controlled_postids[] = $postid;
}
}
return array_unique($controlled_postids);
}
function whoowns_update_shareholders($post_id, $submitted_shareholders = array()) {
global $whoowns_tables, $wpdb;
$actual_shareholders = whoowns_get_direct_shareholders($post_id);
$actual_direct_controller = whoowns_get_direct_controller($post_id);
$new_shareholders = $submitted_shareholders;
$changed = array();
foreach ($actual_shareholders as $actual_shareholder) {
$it_exists=false;
foreach ($submitted_shareholders as $i=>$submitted_shareholder) {
if ( $actual_shareholder->shareholder_id == $submitted_shareholder->shareholder_id ) {
$affected_rows = $wpdb->update(
$whoowns_tables->shares,
array(
'from_id' => $actual_shareholder->shareholder_id,
'to_id' => $post_id,
'share' => whoowns_set_decimal_symbol($submitted_shareholder->share,'.')
),
array( 'id' => $actual_shareholder->share_id ),
array(
'%d',
'%d',
'%f'
)
);
if (!$changed && $affected_rows>0)
$changed += array($actual_shareholder->shareholder_id);
$it_exists = true;
unset($new_shareholders[$i]);
break;
}
}
if (!$it_exists) {
$wpdb->delete(
$whoowns_tables->shares,
array( 'id' => $actual_shareholder->share_id )
);
$changed += array('-'.$actual_shareholder->shareholder_id);
}
}
if (count($new_shareholders)) {
foreach ($new_shareholders as $new_shareholder) {
if ($new_shareholder->shareholder_id) {
$wpdb->insert(
$whoowns_tables->shares,
array(
'from_id' => $new_shareholder->shareholder_id,
'to_id' => $post_id,
'share' => whoowns_set_decimal_symbol($new_shareholder->share,'.')
),
array(
'%d',
'%d',
'%f'
)
);
$changed += array('+'.$actual_shareholder->shareholder_id);
}
}
}
//After inserting the shareholders, I must calculate and update their relative participation IF there were changes in the values:
if (count($changed)>0) {
//Calculating and updating relative shares:
$recalculate_controls = array();
$relative_shares = whoowns_calculate_relative_shares($post_id);
$shareholders = whoowns_get_direct_shareholders($post_id);
foreach ($shareholders as $shareholder) {
$wpdb->update(
$whoowns_tables->shares,
array(
'relative_share' => $relative_shares[$shareholder->shareholder_id]
),
array('id' => $shareholder->share_id),
array(
'%f'
)
);
if ($shareholder->shareholder_id == $direct_controller_before->shareholder_id && $relative_shares[$shareholder->shareholder_id]<=50) {
// If a shareholder who was controller is not controller anymore:
$recalculate_controls[] = $shareholder->shareholder_id;
} elseif ($shareholder->shareholder_id != $direct_controller_before->shareholder_id && $relative_shares[$shareholder->shareholder_id]>50) {
// Or if a shareholder who was not controller is a controller now:
$recalculate_controls[] = $shareholder->shareholder_id;
if ($direct_controller_before)
$recalculate_controls[] = $direct_controller_before->shareholder_id;
}
}
// If there was some change in what shareholder controls this owner, we must update the metakey 'controls_final' of all the direct and indirect shareholders:
if ($recalculate_controls) {
$recalculate_controls = array_unique($recalculate_controls);
foreach ($recalculate_controls as $rc_postid) {
whoowns_update_metakey_controls_final(whoowns_get_controllers($rc_postid));
}
}
}
return $changed;
}
//This function prepares the system to update the whole universe related to an owner. It's called when something in the shareholders or revenue of owner $postid was changed.
function whoowns_init_owner_universe_update($postid, $changed_shares=array(), $was_deleted=false) {
global $whoowns_tables, $wpdb;
$net = whoowns_generate_full_network($postid);
// If some of the ancient shareholders was deleted, they should also be updated!
if (count($changed_shares)>0) {
foreach ($changed_shares as $changed_share) {
if ($changed_share[0]=='-')
$net = array_merge($net, whoowns_generate_full_network(substr($changed_share,1)));
}
$net = array_values(array_unique($net));
}
if ($was_deleted)
$net = array_diff($net, array($postid));
//Erasing the cache of all nodes of the network:
$wpdb->query( "DELETE FROM ".$whoowns_tables->networks_cache." WHERE post_id IN (".implode(', ',$net).")" );
whoowns_init_update($net);
}
// This is the function which configures the whoowns-update schedule for the owners in the $net array:
function whoowns_init_update($net) {
$whoowns_cron = get_option('whoowns_cron');
if (isset($whoowns_cron['postids']) && $whoowns_cron['postids']) {
$net = array_unique(array_merge($net,$whoowns_cron['postids']));
}
$whoowns_cron['stage'] = 1;
$whoowns_cron['status'] = 'waiting';
$whoowns_cron['postids'] = $net;
update_option('whoowns_cron', $whoowns_cron);
//If there are less owners than the "whoowns_cron_threshold" setting, the update will be run immediately:
if (whoowns_get_total_number_of_owners() <= get_option('whoowns_cron_threshold')) {
whoowns_update();
};
}
function whoowns_update() {
$whoowns_cron = get_option('whoowns_cron');
if (!$whoowns_cron || !$whoowns_cron['postids'])
return false;
if ($whoowns_cron['status']=='working') {
wp_schedule_single_event( time()+300, 'whoowns-update' );
return false;
} elseif ($whoowns_cron['status']=='concluded') {
$whoowns_cron['stage']++;
/*if ($whoowns_cron['stage']>6) {
update_option('whoowns_cron','');
return true;
}*/
}
$postids = $whoowns_cron['postids'];
$whoowns_cron['status'] = 'working';
update_option('whoowns_cron', $whoowns_cron);
switch ($whoowns_cron['stage']) {
case 1:
whoowns_update_metakey_controlled_by($postids);
break;
case 2:
whoowns_update_metakey_controls_final($postids);
break;
case 3:
whoowns_update_accumulated_power($postids,true);
break;
case 4:
whoowns_update_interchainers($postids);
break;
case 5:
whoowns_update_final_controllers($postids);
break;
case 6:
whoowns_batch_update_power_index_and_rank();
break;
}
if ($whoowns_cron['stage']==6) {
update_option('whoowns_cron','');
return true;
} else {
$whoowns_cron['status'] = 'concluded';
update_option('whoowns_cron', $whoowns_cron);
wp_schedule_single_event( time(), 'whoowns-update' );
}
return false;
}
add_action( 'whoowns-update','whoowns_update' );
function whoowns_update_network_cache($postid, $conclude) {
//repopulate 'nodes' and 'edges':
whoowns_prepare_network_data_for_visualization($postid);
//repopulate chain of participations and controllers (cy_list):
ob_start();
whoowns_show_list_view($postid);
$html = ob_get_contents();
ob_end_clean();
whoowns_save_cached($postid,array('cy_list'=>trim($html)));
//repopulate news (temporarily disabled -> let the news come when the post is first visited again)
//whoowns_update_network_related_news($postid);
// If this was the last postid of the targets, it's time to go to the next stage of action whoowns-update
if ($conclude) {
$whoowns_cron = get_option('whoowns_cron');
$whoowns_cron['status'] = 'concluded';
$whoowns_cron['stage']++;
update_option('whoowns_cron', $whoowns_cron);
wp_schedule_single_event( time(), 'whoowns-update' );
}
}
add_action( 'whoowns-update-network-cache','whoowns_update_network_cache' );
//This function gets the whole universe of points related to this reference. This is useful for calculating the accumulated power, interchainer and final controllers, not for the graphic. Use with care...
function whoowns_generate_full_network($postid) {
$full_net = whoowns_generate_directed_network($postid,array(),'all');
return array_values(array_unique($full_net));
}
//ACCUMULATED POWER:
function whoowns_calculate_accumulated_power($route, $postid) {
//First, check if it's not already scheduled:
//use wp_next_scheduled to deleted some next event which will do the same calculation... One other way is to use get_option('cron') and see if there is some scheduled event which falls in the universe of this event, so that it can be prevented with unschedule.
$direct_participations = whoowns_get_direct_participations($postid);
if (!is_array($direct_participations))
$direct_participations = array($direct_participations);
$value = whoowns_calculate_revenue($postid);
$power = $value;
#pR($route);
#pR($power);
foreach($direct_participations as $p) {
$route_tmp = $route;
$route_tmp[]=$p->ID;
if ($p->controlled_by)
$accounted_share = ($p->controlled_by == $postid)
? 1
: 0;
else
$accounted_share = floatval($p->relative_share)/100;
if ( $accounted_share>0 && !whoowns_verify_pair_redundance($route,$p->ID) ) {
#echo implode(',',$route).": $power | wB=".number_format($p->relative_share/100,2)."|w=".number_format($accounted_share,2)."<br>";
$power += $accounted_share * whoowns_calculate_accumulated_power($route_tmp,$p->ID);
#echo implode(',',$route).": $power | wB=".number_format($p->relative_share/100,2)."|w=".number_format($accounted_share,2)."<br>";
}
}
return $power;
}
function whoowns_update_accumulated_power($postids) {
if (!$postids)
return false;
global $wpdb;
if (!is_array($postids))
$postids = array($postids);
foreach ($postids as $postid) {
$PA = whoowns_calculate_accumulated_power(array($postid), $postid);
if ($PA>0)
update_post_meta($postid, 'whoowns_PA', $PA);
else
delete_post_meta($postid, 'whoowns_PA');
}
// pR("PA($postid) = $PA"); // for debugging
}
// INTERCHAINERS:
function whoowns_check_if_interchainer($postids, $save_metadata=false) {
if (!$postids)
return false;
if (!is_array($postids))
$postids = array($postids);
$is_interchainer = array();
foreach ($postids as $postid) {
//INTERCHAIN PARTICIPATIONS:
//Definition of non-controlled enterprises with interchain participations: The criteria is that the enterprise is not controlled by any other enterprise and is not an ultimate controller, but participates directly in more than one different chain (be it by it or by a directly controlled enterprise)
$type = whoowns_get_owner_type($postid);
if ($type && $type->slug!='private-enterprise') {
$is_interchainer[$postid] = false;
continue;
}
if (whoowns_get_direct_controller($postid)) {
$is_interchainer[$postid] = false;
continue;
}
$participations_of_controlled_enterprises = whoowns_get_participations_of_controlled_enterprises($postid);
$distincts = $participations_array = array();
foreach ($participations_of_controlled_enterprises as $pce_postid) {
$part = whoowns_generate_directed_network($pce_postid,array(),'participation',0.001);
foreach ($part as $p_postid) {
if (! whoowns_get_direct_participations($p_postid,false,0)) {
if (!$participations_array[$pce_postid])
$participations_array[$pce_postid] = array();
$participations_array[$pce_postid][] = $p_postid;
if (!in_array($p_postid,$distincts))
$distincts[] = $p_postid;
}
}
}
if ($is_interchainer[$postid] = (count($distincts)>1)) {
foreach ($participations_array as $p) {
if (count($p)==count($distincts)) {
$is_interchainer[$postid]=false;
break;
}
}
}
}
//pR($is_interchainer);exit; // for debugging
// Save the meta post information:
if ($save_metadata) {
foreach ($postids as $postid) {
if ($is_interchainer[$postid])
update_post_meta($postid,'whoowns_is_interchainer',1);
else
delete_post_meta($postid,'whoowns_is_interchainer');
}
return true;
} else
return (is_array($is_interchainer)) ? $is_interchainer : $is_interchainer[$postids[0]];
}
function whoowns_update_interchainers($postids) {
whoowns_check_if_interchainer($postids,true);
}
//FINAL CONTROLLERS:
/*This function can only be processed AFTER the accumulated power and the interchain definitions have been processed in the whole universe being considered*/
function whoowns_update_final_controllers($postids) {
if (!$postids || !is_array($postids))
return false;
global $wpdb;
$final_controllers = array();
// Definition of an enterprise as "final controller" - FIRST CRITERIA:
// It is not controlled by another enterprise and has no participations,
// or, if it has participations, it controls at least one other enterprise.
foreach ($postids as $postid) {
$type = whoowns_get_owner_type($postid);
$is_final_controller = false;
$direct_controller = whoowns_get_direct_controller($postid);
$direct_controller_type = whoowns_get_owner_type($direct_controller->shareholder_id);
if ( (!$type || $type->slug == 'private-enterprise') && (!$direct_controller_type || $direct_controller_type->slug!='private-enterprise') ) {
$is_final_controller = (
(whoowns_get_directly_controlled($postid)) ||
!whoowns_has_participations($postid, 0.0001)
);
//echo "<h1>$postid</h1>";pR($is_final_controller);exit; // for debugging
}
if ($is_final_controller) {
update_post_meta($postid,'whoowns_is_final_controller',1);
$final_controllers[$postid] = $postid;
} else
delete_post_meta($postid,'whoowns_is_final_controller');
}
$final_controllers_loop = $final_controllers;
//pR($final_controllers);exit; // for debugging
// Definition of the enterprise as "final controller" - SECOND CRITERIA:
// There can only be one single final controller in a chain. If there is
// another final controller with a higher PA, this enterprise is not a
// final controller anymore. If one enterprise is an interchainer, then
// we should compare the revenue of it and of other final_controllers
// in the chain to decide who will be the final controller:
$removed = $removed2 = array();
foreach ($final_controllers_loop as $fc_postid) {
$fc_PA = get_post_meta($fc_postid,'whoowns_PA',true);
$fc_is_interchainer = get_post_meta($fc_postid,'whoowns_is_interchainer',true);
$part = whoowns_generate_directed_network($fc_postid,array(),'participation',0.0001);
unset($part[0]);
$eliminate = array();
$t=true;
foreach ($part as $p_postid) {
if (in_array($p_postid,$final_controllers_loop)) {
$p_PA = get_post_meta($p_postid,'whoowns_PA',true);
$p_is_interchainer = get_post_meta($p_postid,'whoowns_is_interchainer',true);
$p_revenue = whoowns_calculate_own_and_controlled_revenues($p_postid);
$fc_revenue = whoowns_calculate_own_and_controlled_revenues($fc_postid);
//echo "<hr>p_id=$p_postid, p_PA=$p_PA, p_revenue=$p_revenue, p_is_interchainer=$p_is_interchainer<br>fc_id=$fc_postid, fc_PA=$fc_PA, fc_revenue=$fc_revenue, fc_is_interchainer=$fc_is_interchainer<br> < ".$final_controllers[$fc_postid];
if ( $p_PA>$fc_PA || (
$p_revenue > $fc_revenue &&
$fc_is_interchainer &&
!$p_is_interchainer
) ) {
unset($final_controllers[$fc_postid]);
$removed[] = $fc_postid;
break;
} else {
$eliminate[] = $p_postid;
}
}
}
if ($final_controllers[$fc_postid]) {
foreach ($eliminate as $e_postid) {
$removed2[] = $e_postid;
unset($final_controllers[$e_postid]);
}
}
}
//pR($final_controllers);exit; // for debugging
$sql = "SELECT post_id FROM ".$wpdb->prefix."postmeta WHERE meta_key = 'whoowns_is_final_controller' AND post_id IN (".implode(",",$postids).") AND post_id NOT IN (".implode(",",$final_controllers).")";
$extra_postids = $wpdb->get_col($sql);
if ($extra_postids)
foreach ($extra_postids as $extra_postid)
delete_post_meta($extra_postid, 'whoowns_is_final_controller');
foreach ($final_controllers as $fc_postid)
update_post_meta($fc_postid,'whoowns_is_final_controller',1);
}
function whoowns_calculate_revenue($postids) {
if (!$postids)
return false;
if (!is_array($postids))
$postids = array($postids);
$value = array();
foreach ($postids as $i=>$postid) {
$revenue = get_post_meta($postid, 'whoowns_revenue', true);
$months = (isset($revenue['months']) && intval($revenue['months']))
? intval($revenue['months'])
: 12;
$v = (isset($revenue['value'])) ? floatval($revenue['value']) : 0;
if ($v && $months<=12)
$value[$postid] = ($v/$months)*12;
}
return (count($postids)>1) ? $value : $value[$postids[0]];
}
function whoowns_calculate_own_and_controlled_revenues($postids) {
if (!$postids)
return false;
if (!is_array($postids))
$postids = array($postids);
$value = array();
foreach ($postids as $i=>$postid) {