-
Notifications
You must be signed in to change notification settings - Fork 1
/
legi-display.php
2226 lines (1931 loc) · 85.7 KB
/
legi-display.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
/**
Plugin Name: Legi Display
Plugin Tag: law, legifrance, french, france, code
Description: <p>Display French Codes and laws once the XML files are retrieved from ftp://[email protected]/</p><p>See http://rip.journal-officiel.gouv.fr/index.php/pages/LO for the licence</p>
Version: 1.0.5
Framework: SL_Framework
Author: SedLex
Author URI: http://www.sedlex.fr/
Author Email: [email protected]
Framework Email: [email protected]
Plugin URI: http://wordpress.org/plugins/legi-display/
License: GPL3
*/
//Including the framework in order to make the plugin work
require_once('core.php') ;
/** ====================================================================================================================================================
* This class has to be extended from the pluginSedLex class which is defined in the framework
*/
class legi_display extends pluginSedLex {
/** ====================================================================================================================================================
* Plugin initialization
*
* @return void
*/
static $instance = false;
protected function _init() {
global $wpdb ;
// Name of the plugin (Please modify)
$this->pluginName = 'Legi Display' ;
// The structure of the SQL table if needed (for instance, 'id_post mediumint(9) NOT NULL, short_url TEXT DEFAULT '', UNIQUE KEY id_post (id_post)')
$this->tableSQL = "id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, id_code VARCHAR(25), titre_code VARCHAR(255), info_code TEXT DEFAULT '', id_level1 VARCHAR(25), id_level2 VARCHAR(25), id_level3 VARCHAR(25), id_level4 VARCHAR(255), id_level5 VARCHAR(25), id_level6 VARCHAR(25), id_level7 VARCHAR(25), id_level8 VARCHAR(255), id_level9 VARCHAR(25), id_level10 VARCHAR(25), textes_id_level TEXT DEFAULT '', date_debut DATE, date_fin DATE, id_article VARCHAR(25), num_article VARCHAR(25), state VARCHAR(25), version TEXT DEFAULT '', lien_autres_articles TEXT DEFAULT '', texte TEXT DEFAULT '', PRIMARY KEY (id)" ;
// The name of the SQL table (Do no modify except if you know what you do)
$this->table_name = $wpdb->prefix . "pluginSL_" . get_class() ;
//Configuration of callbacks, shortcode, ... (Please modify)
// For instance, see
// - add_shortcode (http://codex.wordpress.org/Function_Reference/add_shortcode)
// - add_action
// - http://codex.wordpress.org/Function_Reference/add_action
// - http://codex.wordpress.org/Plugin_API/Action_Reference
// - add_filter
// - http://codex.wordpress.org/Function_Reference/add_filter
// - http://codex.wordpress.org/Plugin_API/Filter_Reference
// Be aware that the second argument should be of the form of array($this,"the_function")
// For instance add_action( "wp_ajax_foo", array($this,"bar")) : this function will call the method 'bar' when the ajax action 'foo' is called
// Important variables initialisation (Do not modify)
$this->path = __FILE__ ;
$this->pluginID = get_class() ;
// activation and deactivation functions (Do not modify)
register_activation_hook(__FILE__, array($this,'install'));
register_deactivation_hook(__FILE__, array($this,'deactivate'));
register_uninstall_hook(__FILE__, array('legi_display','uninstall_removedata'));
add_action( 'wp_ajax_nopriv_updateContenuLegi', array( $this, 'updateContenuLegi'));
add_action( 'wp_ajax_updateContenuLegi', array( $this, 'updateContenuLegi'));
add_action('wp_head', array( $this, 'add_meta_tags'));
add_shortcode( 'legi', array( $this, 'legi_shortcode' ) );
add_filter('template_include',array($this,'display_code_change_template'), 1);
add_filter('the_posts', array($this, 'display_code_define_post'));
$url = $this->parse_legi_uri() ;
if (is_array($url)) {
remove_action('template_redirect', 'redirect_canonical');
}
}
/** ====================================================================================================================================================
* In order to uninstall the plugin, few things are to be done ...
* (do not modify this function)
*
* @return void
*/
static public function uninstall_removedata () {
global $wpdb ;
// DELETE OPTIONS
delete_option('legi_display'.'_options') ;
if (is_multisite()) {
delete_site_option('legi_display'.'_options') ;
}
// DELETE SQL
if (function_exists('is_multisite') && is_multisite()){
$old_blog = $wpdb->blogid;
$old_prefix = $wpdb->prefix ;
// Get all blog ids
$blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM ".$wpdb->blogs));
foreach ($blogids as $blog_id) {
switch_to_blog($blog_id);
$wpdb->query("DROP TABLE ".str_replace($old_prefix, $wpdb->prefix, $wpdb->prefix . "pluginSL_" . 'legi_display')) ;
}
switch_to_blog($old_blog);
} else {
$wpdb->query("DROP TABLE ".$wpdb->prefix . "pluginSL_" . 'legi_display' ) ;
}
// DELETE FILES if needed
SLFramework_Utils::rm_rec(WP_CONTENT_DIR."/sedlex/legi-display/");
SLFramework_Utils::rm_rec(WP_CONTENT_DIR."/uploads/legi/");
$plugins_all = get_plugins() ;
$nb_SL = 0 ;
foreach($plugins_all as $url => $pa) {
$info = pluginSedlex::get_plugins_data(WP_PLUGIN_DIR."/".$url);
if ($info['Framework_Email']=="[email protected]"){
$nb_SL++ ;
}
}
if ($nb_SL==1) {
SLFramework_Utils::rm_rec(WP_CONTENT_DIR."/sedlex/");
}
}
/**====================================================================================================================================================
* Function called when the plugin is activated
* For instance, you can do stuff regarding the update of the format of the database if needed
* If you do not need this function, you may delete it.
*
* @return void
*/
public function _update() {
SLFramework_Debug::log(get_class(), "Update the plugin." , 4) ;
}
/**====================================================================================================================================================
* Function called to return a number of notification of this plugin
* This number will be displayed in the admin menu
*
* @return int the number of notifications available
*/
public function _notify() {
return 0 ;
}
/** ====================================================================================================================================================
* Init javascript for the public side
* If you want to load a script, please type :
* <code>wp_enqueue_script( 'jsapi', 'https://www.google.com/jsapi');</code> or
* <code>wp_enqueue_script('legi_display_script', plugins_url('/script.js', __FILE__));</code>
* <code>$this->add_inline_js($js_text);</code>
* <code>$this->add_js($js_url_file);</code>
*
* @return void
*/
function _public_js_load() {
global $post ;
global $wpdb ;
if (!isset($post->post_content)){
return ;
}
if (preg_match_all("/#LEGI#([\w]*)-([\w]*)#LEGI#/ui", $post->post_content, $matches, PREG_SET_ORDER)) {
wp_enqueue_script('jquery-ui-slider', '', array('jquery'), false );
ob_start() ;
foreach ($matches as $m) {
$result = $wpdb->get_results("SELECT * FROM ".$this->table_name." WHERE id_article='".$m[2]."'") ;
foreach($result as $r) {
// GESTION DU SLIDER POUR AFFICHER PLUSIEURS VERSIONS
if ($r->version!="") {
$version = @unserialize($r->version) ;
if ($version!==false) {
$ver = array() ;
foreach($version as $v) {
$ver[$v['date_debut']] = array('state'=>$v['state'], 'date_fin'=>$v['date_fin'], 'num_article'=>$v['num_article'], 'id_article'=>$v['id_article']) ;
}
ksort($ver) ;
if (count($ver)>1) {
?>
var legi_identifiant1 = "<?php echo $m[2];?>" ;
var legi_identifiant2 = "<?php echo $m[2];?>" ;
function updateContenuLegi(identifiant){
if ((identifiant==legi_identifiant1)&&(identifiant==legi_identifiant2)){
return ;
}
legi_identifiant1 = identifiant ;
legi_identifiant2 = identifiant ;
jQuery('#LegiContent').html("<p>En attente du texte de l'article ... <img src='<?php echo plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/ajax-loader.gif" ?>'/></p>")
var arguments = {
id1: identifiant,
action: 'updateContenuLegi'
}
var ajaxurl2 = "<?php echo admin_url()."admin-ajax.php"?>" ;
jQuery.post(ajaxurl2, arguments, function(response) {
jQuery('#LegiContent').html(response) ;
});
}
function compareContenuLegi(identifiant1, identifiant2){
if ((identifiant1==legi_identifiant1)&&(identifiant2==legi_identifiant2)){
return ;
}
legi_identifiant1 = identifiant1 ;
legi_identifiant2 = identifiant2 ;
jQuery('#LegiContent').html("<p>En attente de la comparaison des articles ... <img src='<?php echo plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/ajax-loader.gif" ?>'/></p>")
var arguments = {
id1: identifiant1,
id2: identifiant2,
action: 'updateContenuLegi'
}
var ajaxurl2 = "<?php echo admin_url()."admin-ajax.php"?>" ;
jQuery.post(ajaxurl2, arguments, function(response) {
jQuery('#LegiContent').html(response) ;
});
}
<?php
$incr = floor(100/(count($ver))) ;
$pos = 0 ;
$ii = 0 ;
$list_bornes = array() ;
foreach($ver as $k => $info) {
$ii++ ;
if ($ii==1){
$list_bornes[$info['id_article']] = array($pos, $pos+$incr);
$pos += $incr ;
} elseif ($ii==count($ver)){
$incr = 100-$pos ;
$list_bornes[$info['id_article']] = array($pos, 100);
$mid_val = floor(($pos+100)/2) ;
} else {
$list_bornes[$info['id_article']] = array($pos, $pos+$incr);
$pos += $incr ;
}
}
echo '
jQuery(function() {
jQuery( "#legi_activerComparaison" ).removeAttr("checked");
jQuery( "#legi_slider" ).slider({
value: '.$mid_val.',
slide: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.value<'.$lb[1].')&&(ui.value>='.$lb[0].')){'."\r\n" ;
echo ' jQuery("#date_'.$lk.'").addClass("date_slider_highlight");'."\r\n" ;
foreach ($list_bornes as $t_lk=>$t_lb) {
if ($lk!=$t_lk){
echo ' jQuery("#date_'.$t_lk.'").removeClass("date_slider_highlight");'."\r\n" ;
}
}
echo '}'."\r\n" ;
}
echo '
},
stop: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.value<'.$lb[1].')&&(ui.value>='.$lb[0].')){'."\r\n" ;
echo ' jQuery( "#legi_slider" ).slider({value:'.floor(($lb[1]+$lb[0])/2).'});'."\r\n" ;
echo ' updateContenuLegi("'.$lk.'");'."\r\n" ;
echo '}'."\r\n" ;
}
echo '
}
});
});';
?>
function activerComparaison() {
if (jQuery( "#legi_activerComparaison" ).is(':checked')) {
currentValue = jQuery( "#legi_slider" ).slider("value") ;
jQuery( "#legi_slider" ).slider("destroy") ;
<?php
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((currentValue<'.$lb[1].')&&(currentValue>='.$lb[0].')){'."\r\n" ;
foreach ($list_bornes as $lk2=>$lb2) {
if (($mid_val-2<$lb2[1])&&($mid_val-2>=$lb2[0])){
echo ' compareContenuLegi("'.$lk.'", "'.$lk2.'");'."\r\n" ;
}
}
echo '}'."\r\n" ;
}
echo '
jQuery( "#legi_slider" ).slider({
values: [currentValue+2,'.($mid_val-2).'],
slide: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.values[0]<'.$lb[1].')&&(ui.values[0]>='.$lb[0].')){'."\r\n" ;
echo ' jQuery("#date_'.$lk.'").addClass("date_slider_highlight");'."\r\n" ;
foreach ($list_bornes as $t_lk=>$t_lb) {
if ($lk!=$t_lk){
echo ' jQuery("#date_'.$t_lk.'").removeClass("date_slider_highlight");'."\r\n" ;
}
}
echo '}'."\r\n" ;
}
echo '
},
stop: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.values[0]<'.$lb[1].')&&(ui.values[0]>='.$lb[0].')){'."\r\n" ;
echo ' otherValue = jQuery( "#legi_slider" ).slider("values", 1) ; '."\r\n" ;
echo ' jQuery( "#legi_slider" ).slider({values:['.(floor(($lb[1]+$lb[0])/2)+2).', otherValue]});'."\r\n" ;
foreach ($list_bornes as $lk2=>$lb2) {
echo 'if ((ui.values[1]<'.$lb2[1].')&&(ui.values[1]>='.$lb2[0].')){'."\r\n" ;
echo ' otherValue = jQuery( "#legi_slider" ).slider("values", 0) ; '."\r\n" ;
echo ' jQuery( "#legi_slider" ).slider({values:[otherValue,'.(floor(($lb2[1]+$lb2[0])/2)-2).']});'."\r\n" ;
echo ' compareContenuLegi("'.$lk.'", "'.$lk2.'");'."\r\n" ;
echo '}'."\r\n" ;
}
echo '}'."\r\n" ;
}
echo '
}
});';
?>
} else {
currentValue = jQuery( "#legi_slider" ).slider("values", 0) ;
jQuery( "#legi_slider" ).slider("destroy") ;
<?php
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((currentValue<'.$lb[1].')&&(currentValue>='.$lb[0].')){'."\r\n" ;
echo ' updateContenuLegi("'.$lk.'");'."\r\n" ;
echo '}'."\r\n" ;
}
echo '
jQuery( "#legi_slider" ).slider({
value: currentValue-2,
slide: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.value<'.$lb[1].')&&(ui.value>='.$lb[0].')){'."\r\n" ;
echo ' jQuery("#date_'.$lk.'").addClass("date_slider_highlight");'."\r\n" ;
foreach ($list_bornes as $t_lk=>$t_lb) {
if ($lk!=$t_lk){
echo ' jQuery("#date_'.$t_lk.'").removeClass("date_slider_highlight");'."\r\n" ;
}
}
echo '}'."\r\n" ;
}
echo '
},
stop: function( event, ui ) {
';
foreach ($list_bornes as $lk=>$lb) {
echo 'if ((ui.value<'.$lb[1].')&&(ui.value>='.$lb[0].')){'."\r\n" ;
echo ' jQuery( "#legi_slider" ).slider({value:'.floor(($lb[1]+$lb[0])/2).'});'."\r\n" ;
echo ' updateContenuLegi("'.$lk.'");'."\r\n" ;
echo '}'."\r\n" ;
}
echo '
}
});';
?>
}
}
<?php
}
}
}
// FIN SLIDER
}
}
$java = ob_get_clean() ;
$this->add_inline_js($java) ;
}
return ;
}
/** ====================================================================================================================================================
* Init css for the public side
* If you want to load a style sheet, please type :
* <code>$this->add_inline_css($css_text);</code>
* <code>$this->add_css($css_url_file);</code>
*
* @return void
*/
function _public_css_load() {
$this->add_inline_css($this->get_param('css')) ;
return ;
}
/** ====================================================================================================================================================
* Init javascript for the admin side
* If you want to load a script, please type :
* <code>wp_enqueue_script( 'jsapi', 'https://www.google.com/jsapi');</code> or
* <code>wp_enqueue_script('legi_display_script', plugins_url('/script.js', __FILE__));</code>
* <code>$this->add_inline_js($js_text);</code>
* <code>$this->add_js($js_url_file);</code>
*
* @return void
*/
function _admin_js_load() {
return ;
}
/** ====================================================================================================================================================
* Init css for the admin side
* If you want to load a style sheet, please type :
* <code>$this->add_inline_css($css_text);</code>
* <code>$this->add_css($css_url_file);</code>
*
* @return void
*/
function _admin_css_load() {
return ;
}
/** ====================================================================================================================================================
* Called when the content is displayed
*
* @param string $content the content which will be displayed
* @param string $type the type of the article (e.g. post, page, custom_type1, etc.)
* @param boolean $excerpt if the display is performed during the loop
* @return string the new content
*/
function _modify_content($content, $type, $excerpt) {
global $wpdb ;
global $_POST ;
$crawler = "" ;
if (!$this->get_param('allow_crawler')){
$crawler = " rel='nofollow'" ;
}
$modified = false ;
// RECHERCHE
if (preg_match_all("/#LEGI#([\w]*)_RECHERCHE#LEGI#/ui", $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
ob_start() ;
// GESTION DU MODULE de RECHERCHE
$titre_code = $wpdb->get_var("SELECT titre_code FROM ".$this->table_name." WHERE id_code='".$m[1]."' LIMIT 1") ;
echo "<div id='legi_breadcrumb'><a".$crawler." href='".$this->id_code_to_url($m[1])."'>".$titre_code."</a></div>" ;
echo "<div id='legi_recherche'><form action='".$this->id_code_to_url($m[1])."recherche/' method='post'><p>Rechercher dans le ".$titre_code." <input name='legi_rech_string'></form></p></div>" ;
// AFFICHAGE DES RESULTATS
if (isset($_POST['legi_rech_string'])) {
echo "<h4>Résultat de la recherche</h4>" ;
$search_str = strip_tags(html_entity_decode($_POST['legi_rech_string'])) ;
$search_str = trim(preg_replace("@[^\w -]@ui", " ", $search_str)) ;
$search_results = $this->search_words($m[1], explode(" ", $search_str)) ;
if (count($search_results)==0) {
echo "<p>Aucun résultat</p>" ;
} else {
if (count($search_results)!=20) {
echo "<p>Les articles retournant un résultat pour '<em>$search_str</em>' sont (les articles sont affichés par ordre de pertinence):<p>" ;
} else {
echo "<p>Les 20 premiers articles retournant un résultat pour '<em>$search_str</em>' sont (les articles sont affichés par ordre de pertinence):<p>" ;
}
echo "<ul>" ;
foreach($search_results as $id=>$sr) {
$num_art = $wpdb->get_var("SELECT num_article FROM ".$this->table_name." WHERE id_article='".$id."'") ;
echo "<li><a".$crawler." href='".$this->id_article_to_url($id)."'>".$num_art."</a></li>" ;
}
echo "</ul>" ;
}
} else {
echo "Aucune recherche effectuée" ;
}
$content = str_replace($m[0], ob_get_clean(), $content) ;
$modified = true ;
}
}
// CODE
if (preg_match_all("/#LEGI#([\w]*)#LEGI#/ui", $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
ob_start() ;
// GESTION DU MODULE de RECHERCHE
$titre_code = $wpdb->get_var("SELECT titre_code FROM ".$this->table_name." WHERE id_code='".$m[1]."' LIMIT 1") ;
echo "<div id='legi_recherche'><form action='".$this->id_code_to_url($m[1])."recherche/' method='post'><p>Rechercher dans le ".$titre_code." <input name='legi_rech_string'></form></p></div>" ;
// AFFICHAGE TREE
$array_to_display = $this->get_code_section($m[1]) ;
SLFramework_Treelist::render($array_to_display, true, null, "code_hiera") ;
$content = str_replace($m[0], ob_get_clean(), $content) ;
$modified = true ;
}
$info = @unserialize($wpdb->get_var("SELECT info_code FROM ".$this->table_name." WHERE id_code='".$m[1]."' LIMIT 1")) ;
}
// ARTICLE
if (preg_match_all("/#LEGI#([\w]*)-([\w]*)#LEGI#/ui", $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
ob_start() ;
$result = $wpdb->get_results("SELECT * FROM ".$this->table_name." WHERE id_article='".$m[2]."'") ;
foreach($result as $r) {
// GESTION DU MODULE de RECHERCHE
echo "<div id='legi_breadcrumb'><a".$crawler." href='".$this->id_code_to_url($r->id_code)."'>".$r->titre_code."</a></div>" ;
echo "<div id='legi_recherche'><form action='".$this->id_code_to_url($r->id_code)."recherche/' method='post'><p>Rechercher dans le ".$r->titre_code." <input name='legi_rech_string'></form></p></div>" ;
// GESTION DU SLIDER POUR AFFICHER PLUSIEURS VERSIONS
if ($r->version!="") {
$version = @unserialize($r->version) ;
if ($version!==false) {
$ver = array() ;
foreach($version as $v) {
$ver[$v['date_debut']] = array('state'=>$v['state'], 'date_fin'=>$v['date_fin'], 'num_article'=>$v['num_article'], 'id_article'=>$v['id_article']) ;
}
ksort($ver) ;
if (count($ver)>1) {
echo '<div id="legi_slider_date" style="position:relative;padding:0px;margin:0px;margin-left:5%;width:90%;height:30px;background-color:#EEEEEE;">' ;
$incr = floor(100/(count($ver))) ;
$pos = 0 ;
$ii = 0 ;
foreach($ver as $k => $info) {
$ii++ ;
if ($ii==1){
echo "<div style='position:absolute;left:0%;top:0px;width:".$incr."%;border-right:1px solid black;height:30px;height:30px;' class='date_slider' id='date_".$info['id_article']."'>".$k."</div>" ;
$pos += $incr ;
} elseif ($ii==count($ver)){
$incr = 100-$pos ;
echo "<div style='position:absolute;left:".$pos."%;top:0px;width:".$incr."%;height:30px;border-left:1px solid black;' class='date_slider date_slider_highlight' id='date_".$info['id_article']."'>".$k."</div>" ;
$mid_val = floor(($pos+100)/2) ;
} else {
echo "<div style='position:absolute;left:".$pos."%;top:0px;width:".$incr."%;border-right:1px solid black;height:30px;border-left:1px solid black;height:30px;' class='date_slider' id='date_".$info['id_article']."'>".$k."</div>" ;
$pos += $incr ;
}
}
echo '</div>' ;
echo '<div id="legi_slider" style="padding:0px;margin:0px;margin-left:5%;width:90%;height:10px;"></div>' ;
echo '<div id="legi_compare" style="padding:0px;margin:0px;margin-left:5%;width:90%;height:10px;text-align:right;color:#AAAAAA;">Activer la comparaison <input id="legi_activerComparaison" onclick="activerComparaison()" type="checkbox"></div>' ;
}
}
}
// FIN SLIDER
echo "<p> </p><div id='LegiContent'>".$this->mise_en_forme_article($r)."</div>";
}
$info = @unserialize($r->info_code) ;
$array_to_display = $this->get_code_section($m[1],$m[2]) ;
SLFramework_Treelist::render($array_to_display, true, null, "code_hiera") ;
$content = str_replace($m[0], ob_get_clean(), $content) ;
$modified = true ;
}
}
// NOTICE
if ($modified) {
$content .= "<p> </p>" ;
$content .= "<div class='notice_legi'>" ;
$content .= "<p>Ces informations sont issues de la base de données \"<a href='http://www.data.gouv.fr/fr/dataset/legi-codes-lois-et-reglements-consolides'>LEGI</a>\" mise à disposition par LégiFrance, la direction de l'information légale et administrative et les services du Premier ministre.</p>" ;
$content .= "<p>Les données sont reproduites sans modification, si ce n'est celles nécessaires à la mise en forme de la page.</p>" ;
if ((isset($info))&&(is_array($info))) {
$content .= "<p>La dernière mise à jour de ce code a été réalisé le ".$info['derniere_maj'].".</p>" ;
}
$content .= "</div>" ;
}
return $content;
}
/** ====================================================================================================================================================
* Search text into a code
*
* @return array of results
*/
function search_words($code, $array_words) {
global $wpdb ;
$initial_results = array() ;
// On regarde d'abord tous les articles qui contiennet au moins un mot de plus de trois lettres
foreach($array_words as $aw) {
if (strlen($aw)>2) {
$result = $wpdb->get_results("SELECT id_article, texte FROM ".$this->table_name." WHERE id_code='".$code."' AND state='VIGUEUR' AND (texte LIKE '%".$aw."%' OR texte LIKE '%".htmlentities($aw)."%')") ;
foreach($result as $r) {
if (!isset($initial_results[$r->id_article])) {
$texte = strip_tags(html_entity_decode($r->texte)) ;
$texte = trim(preg_replace("@[^\w -]@ui", " ", $texte)) ;
$initial_results[$r->id_article] = $texte ;
}
}
}
}
// Maintenant, pour chaque texte trouvé, on calcul un score de proximite
$score_results = array() ;
$max_score_cible = 0 ;
foreach($initial_results as $k_ir => $ir) {
$score = 0 ;
$score_cible = 0 ;
foreach($array_words as $aw) {
if (strlen($aw)>0) {
$score_cible += strlen($aw)*log(2) ;
$score += strlen($aw)*log(1+mb_substr_count($ir, $aw)) ;
}
}
if ($max_score_cible<$score_cible) {
$max_score_cible = $score_cible ;
}
$score_results[$k_ir] = $score ;
}
// On ne garde que les 20 meilleurs résultats
arsort($score_results) ;
$score_results = array_slice($score_results, 0, 20, true) ;
// On normalise les resultats
$normalized_results = array() ;
foreach ($score_results as $k => $s) {
$normalized_results[$k] = floor($s/$max_score_cible*100) ;
}
return $normalized_results ;
}
/** ====================================================================================================================================================
* Add a button in the TinyMCE Editor
*
* To add a new button, copy the commented lines a plurality of times (and uncomment them)
*
* @return array of buttons
*/
function add_tinymce_buttons() {
$buttons = array() ;
$buttons[] = array(__('List the available LEGI code that you have imported', $this->pluginID), '[legi]', '', plugin_dir_url("/").'/'.str_replace(basename( __FILE__),"",plugin_basename( __FILE__)).'img/legi.png') ;
return $buttons ;
}
/**====================================================================================================================================================
* Function to instantiate the class and make it a singleton
* This function is not supposed to be modified or called (the only call is declared at the end of this file)
*
* @return void
*/
public static function getInstance() {
if ( !self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/** ====================================================================================================================================================
* Define the default option values of the plugin
* This function is called when the $this->get_param function do not find any value fo the given option
* Please note that the default return value will define the type of input form: if the default return value is a:
* - string, the input form will be an input text
* - integer, the input form will be an input text accepting only integer
* - string beggining with a '*', the input form will be a textarea
* - boolean, the input form will be a checkbox
*
* @param string $option the name of the option
* @return variant of the option
*/
public function get_default_option($option) {
switch ($option) {
// Alternative default return values (Please modify)
case 'css' : return "*" ; break ;
case 'folder_code' : return "code_fr" ; break ;
case 'allow_crawler' : return false ; break ;
}
return null ;
}
/** ====================================================================================================================================================
* The admin configuration page
* This function will be called when you select the plugin in the admin backend
*
* @return void
*/
public function configuration_page() {
global $wpdb;
global $blog_id ;
$blog_fold = "" ;
if (is_multisite()) {
$blog_fold = $blog_id."/" ;
}
SLFramework_Debug::log(get_class(), "Print the configuration page." , 4) ;
?>
<div class="plugin-titleSL">
<h2><?php echo $this->pluginName ?></h2>
</div>
<div class="plugin-contentSL">
<?php
//===============================================================================================
// After this comment, you may modify whatever you want
// We check rights
$this->check_folder_rights( array(array(WP_CONTENT_DIR."/sedlex/test/", "rwx")) ) ;
$tabs = new SLFramework_Tabs() ;
if (isset($_POST['import_code'])) {
ob_start() ;
$code = $_POST['import_code'] ;
if (preg_match("/[A-Z0-9]{20}/i", $code)) {
$result = $this->import_code($code) ;
echo $result['msg'] ;
} else {
echo __('The code does not seems to be an appropriate code.', $this->pluginID) ;
}
if (is_file(WP_CONTENT_DIR."/sedlex/legi-display/".$blog_fold.$code.".array")) {
@unlink(WP_CONTENT_DIR."/sedlex/legi-display/".$blog_fold.$code.".array") ;
echo "<p>".sprintf(__("The cache file %s has been deleted: it will be recreated in the next rendering of this code.", $this->pluginID), "<code>".WP_CONTENT_DIR."/sedlex/legi-display/".$blog_fold.$code.".array"."</code>")."</p>" ;
}
$import_in_progress = new SLFramework_Box (__("Code that has just been imported", $this->pluginID), ob_get_clean()) ;
}
ob_start() ;
echo "<p>".__('Choose a code to import/update in the following list:', $this->pluginID)."</p>" ;
$list_code = $this->list_code() ;
if (($list_code !== false)&&(count($list_code)>0)) {
echo "<p><form method='POST'><select name='import_code'>" ;
echo "<option value='none'> </option>" ;
foreach ($list_code as $lc_k => $lc) {
echo "<option value='".$lc_k."'>".$lc."</option>" ;
}
echo "</select> <input type='submit' name='submit' class='button-primary validButton' value='".__('Import', $this->pluginID)."'/></form></p>" ;
} else {
echo "<p><code>".__('No code detected in your upload folder.', $this->pluginID)."</code></p>" ;
}
$import = new SLFramework_Box (__("Import", $this->pluginID), ob_get_clean()) ;
ob_start() ;
echo "<p>".__('Here is the list of code that has already been imported:', $this->pluginID)."</p>" ;
if (($list_code !== false)&&(count($list_code)>0)) {
$found = false ;
foreach ($list_code as $lc_k => $lc) {
$nb = $wpdb->get_var("SELECT COUNT(*) FROM ".$this->table_name." WHERE id_code='".esc_sql($lc_k)."'") ;
if ($nb>0) {
echo "<p style='padding-left:30px'><code>".$lc."</code> ($nb)</p>" ;
$found = true ;
}
}
if (!$found) {
echo "<p><code>".__('No code has been imported.', $this->pluginID)."</code></p>" ;
}
} else {
echo "<p><code>".__('No code has been imported.', $this->pluginID)."</code></p>" ;
}
$imported = new SLFramework_Box (__("Already imported", $this->pluginID), ob_get_clean()) ;
ob_start() ;
if (isset($_POST['import_code'])) {
echo $import_in_progress->flush() ;
}
echo $imported->flush() ;
echo $import->flush() ;
$tabs->add_tab(__('Import a code', $this->pluginID), ob_get_clean()) ;
// HOW To
ob_start() ;
echo "<p>".sprintf(__('This plugin Display French Codes and laws once the XML files are retrieved from %s. See %s for the licence.', $this->pluginID), "<a href='ftp://[email protected]/'>ftp://[email protected]/</a>", "<a href='http://rip.journal-officiel.gouv.fr/index.php/pages/LO'>http://rip.journal-officiel.gouv.fr/index.php/pages/LO</a>") ."</p>" ;
$howto1 = new SLFramework_Box (__("Purpose of that plugin", $this->pluginID), ob_get_clean()) ;
ob_start() ;
$upload_dir = wp_upload_dir();
echo "<p>".sprintf(__('At first, download the file %s on %s.', $this->pluginID), "<code>LicenceFreemium_legi_legi_global_20xxxxxx-xxxxxx.tar.gz</code>", "<a href='ftp://[email protected]/'>ftp://[email protected]/</a>" )."</p>" ;
echo "<p>".sprintf(__('Unpack this file on your website (for instance, with %s if your are on Windows or any other compatible software).', $this->pluginID), "<code>7-Zip</code>")."</p>" ;
echo "<p>".sprintf(__('The unpacked files should be stored without modification on you website, in the %s folder (and therefore, the path should be something like this %s).', $this->pluginID), "<code>".$upload_dir['basedir']."</code>", "<code>".$upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur/...</code>")."</p>" ;
echo "<p>".__('Once this done, we should be able to import code in the Import tab.', $this->pluginID)."</p>" ;
$howto2 = new SLFramework_Box (__("How to import the codes?", $this->pluginID), ob_get_clean()) ;
ob_start() ;
echo "<p>".sprintf(__('After the importation, you may insert in a page the shortcode %s in order to create a list of all imported codes. There is also a button in the page editor to insert such shortcode.', $this->pluginID), '<code>[legi]</code>')."</p>" ;
$howto3 = new SLFramework_Box (__("How to consult a code?", $this->pluginID), ob_get_clean()) ;
ob_start() ;
echo $howto1->flush() ;
echo $howto2->flush() ;
echo $howto3->flush() ;
$tabs->add_tab(__('How To', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_how.png") ;
ob_start() ;
$params = new SLFramework_Parameters($this, "tab-parameters") ;
$params->add_title(__('Sub-Dir', $this->pluginID)) ;
$params->add_param('folder_code', __('The name of the folder that will be used for displaying the French Code:', $this->pluginID)) ;
$params->add_comment(sprintf(__("If the folder is %s, the url to access the code would be something like %s.", $this->pluginID), "<code>code_fr</code>", "<code>".site_url()."/code_fr/xxxxx/xxxxx</code>")) ;
$params->add_comment(sprintf(__("For consistency, you may create a page accessible through this url and add a shortcode %s to display the list of all code you imported.", $this->pluginID), "<code>[legi]</code>")) ;
$params->add_title(__('Web-Crawlers', $this->pluginID)) ;
$params->add_param('allow_crawler', sprintf(__('Allow the web crawlers (%s) to index the codes/articles:', $this->pluginID), $this->pluginID)) ;
$params->add_comment(__("It is recommended to de-activate this option as most of the crawlers penalize websites with a lot of new/similar contents.", $this->pluginID)) ;
$params->add_title(__('Appearance', $this->pluginID)) ;
$params->add_param('css', __('The CSS to be used:', $this->pluginID)) ;
$params->add_comment(__("The default value is:", $this->pluginID)) ;
$params->add_comment_default_value('css') ;
$params->flush() ;
$tabs->add_tab(__('Parameters', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_param.png") ;
$frmk = new coreSLframework() ;
if (((is_multisite())&&($blog_id == 1))||(!is_multisite())||($frmk->get_param('global_allow_translation_by_blogs'))) {
ob_start() ;
$plugin = str_replace("/","",str_replace(basename(__FILE__),"",plugin_basename( __FILE__))) ;
$trans = new SLFramework_Translation($this->pluginID, $plugin) ;
$trans->enable_translation() ;
$tabs->add_tab(__('Manage translations', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_trad.png") ;
}
ob_start() ;
$plugin = str_replace("/","",str_replace(basename(__FILE__),"",plugin_basename( __FILE__))) ;
$trans = new SLFramework_Feedback($plugin, $this->pluginID) ;
$trans->enable_feedback() ;
$tabs->add_tab(__('Give feedback', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_mail.png") ;
ob_start() ;
// A list of plugin slug to be excluded
$exlude = array('wp-pirate-search') ;
// Replace sedLex by your own author name
$trans = new SLFramework_OtherPlugins("sedLex", $exlude) ;
$trans->list_plugins() ;
$tabs->add_tab(__('Other plugins', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_plug.png") ;
echo $tabs->flush() ;
// Before this comment, you may modify whatever you want
//===============================================================================================
?>
<?php echo $this->signature ; ?>
</div>
<?php
}
/** ====================================================================================================================================================
* To list the available code
*
* @return void
*/
function list_code() {
$upload_dir = wp_upload_dir();
if (!is_dir($upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur")) {
return false ;
}
$result = $this->list_code_rec($upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur") ;
asort($result) ;
return $result ;
}
/** ====================================================================================================================================================
* To list the available code recursive
*
* @return void
*/
function list_code_rec($path) {
if (!is_dir($path)) {
return array() ;
}
if (is_dir($path.DIRECTORY_SEPARATOR."texte".DIRECTORY_SEPARATOR."version")) {
$list_file = scandir($path.DIRECTORY_SEPARATOR."texte".DIRECTORY_SEPARATOR."version") ;
foreach ($list_file as $ld) {
if (($ld!=".")&&($ld!="..")) {
if (is_file($path.DIRECTORY_SEPARATOR."texte".DIRECTORY_SEPARATOR."version".DIRECTORY_SEPARATOR.$ld)) {
if (preg_match("/[A-Z0-9]{20}\.xml/i", $ld)) {
$vxml = simplexml_load_file ($path.DIRECTORY_SEPARATOR."texte".DIRECTORY_SEPARATOR."version".DIRECTORY_SEPARATOR.$ld) ;
$titre = (string)$vxml->META->META_SPEC->META_TEXTE_VERSION->TITREFULL ;
$id_code = (string)$vxml->META->META_COMMUN->ID ;
return array($id_code=>$titre) ;
}
}
}
}
}
if (is_dir($path.DIRECTORY_SEPARATOR."article")) {
//ERROR car on aurait du sortir avant ...
// Cela signifie qu'il n'y avait pas de xml decrivant le code
return array() ;
}
$list_code = array() ;
$list_dir = scandir($path) ;
foreach ($list_dir as $ld) {
if (($ld!=".")&&($ld!="..")) {
$new_path = $path . DIRECTORY_SEPARATOR . $ld ;
if (is_dir($new_path)) {
$list_code_new = $this->list_code_rec($new_path) ;
$list_code = array_merge($list_code, $list_code_new) ;
}
}
}
return $list_code ;
}
/** ====================================================================================================================================================
* To import a code from the XML files
*
* @return void
*/
function import_code($id_code) {
$id_code = trim($id_code) ;
$upload_dir = wp_upload_dir();
if (!is_dir($upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur/")) {
return array("success"=>false , "msg"=>sprintf(__("No folder %s may be found in %s: Please dowload the XML file on %s and upload the appropriate folder here.", $this->pluginID), "<code>/legi/global/code_et_TNC_en_vigueur/code_en_vigueur/</code>", "<code>".$upload_dir['basedir']."</code>", "<a href='ftp://[email protected]/'>ftp://[email protected]/</a>"));
}
// We check if the code is in the correct format such as LEGITEXT000006069414
if (strlen($id_code)!=20) {
return array("success"=>false , "msg"=>sprintf(__("The ID of the code should be 20 characters long (here, we have a %s characters long ID).", $this->pluginID), strlen($id_code)));
}
$lev1 = substr($id_code, 0,4) ;
$lev2 = substr($id_code, 4,4) ;
$lev3 = substr($id_code, 8,2) ;
$lev4 = substr($id_code, 10,2) ;
$lev5 = substr($id_code, 12,2) ;
$lev6 = substr($id_code, 14,2) ;
$lev7 = substr($id_code, 16,2) ;
$lev8 = substr($id_code, 18,2) ;
$path = $upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur/".$lev1."/".$lev2."/".$lev3."/".$lev4."/".$lev5."/".$lev6."/".$lev7."/".$id_code."/" ;
if (!is_dir($path)) {
return array("success"=>false , "msg"=>sprintf(__("No folder %s may be found in %s. Please make sure that the ID is correct.", $this->pluginID), "<code>/".$lev1."/".$lev2."/".$lev3."/".$lev4."/".$lev5."/".$lev6."/".$lev7."/".$id_code."/</code>", "<code>".$upload_dir['basedir']."/legi/global/code_et_TNC_en_vigueur/code_en_vigueur/</code>", "<a href='ftp://[email protected]/'>ftp://[email protected]/</a>"));
}
// Recupérer info sur code
$info_code = serialize(array('status'=>'', 'derniere_maj'=>'' )) ;
if (is_file($path."texte/version/".$id_code.".xml")) {
$oxml = simplexml_load_file ($path."texte/version/".$id_code.".xml") ;
if ($oxml!==false) {
$status_code = (string)$oxml->META->META_SPEC->META_TEXTE_VERSION->ETAT ;
$derniere_maj = (string)$oxml->META->META_SPEC->META_TEXTE_CHRONICLE->DERNIERE_MODIFICATION ;
$info_code = serialize(array('status'=>$status_code, 'derniere_maj'=>$derniere_maj )) ;
}
}
$nb_entry = $this->import_code_rec($path."article", $info_code) ;
return array("success"=>true , "msg"=>sprintf(__("%s entries have been imported (i.e. %s new entries and %s updated entries).", $this->pluginID), $nb_entry['new']+$nb_entry['update'], $nb_entry['new'], $nb_entry['update']));
}
/** ====================================================================================================================================================
* To import a code from the XML files (reccursive)
*
* @return integer nb of entry imported
*/
function import_code_rec($path, $info_code) {
global $wpdb ;
if (!is_dir($path)) {
return array('new'=>0, 'update'=>0) ;
}
$nb_entry = array('new'=>0, 'update'=>0) ;
$list_dir = scandir($path) ;
foreach ($list_dir as $ld) {
if (($ld!=".")&&($ld!="..")) {
$new_path = $path . DIRECTORY_SEPARATOR . $ld ;
if (is_dir($new_path)) {
$nb_entry_new = $this->import_code_rec($new_path, $info_code) ;
$nb_entry['new'] += $nb_entry_new['new'] ;
$nb_entry['update'] += $nb_entry_new['update'] ;
}
if (is_file($new_path)) {