forked from 13pixlar/sticky-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-sticky-list.php
2139 lines (1778 loc) · 104 KB
/
class-sticky-list.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
/*
Attention developers
There is a fully documented and commented version of this file on Github
https://github.com/13pixlar/sticky-list
Preimum features (to be implemented):
Multi page form support
Multiple file upload
Fully compatible with Gravity Forms “limit entries” and “No Duplicates” features [done]
Export list to .csv from front end (based of search results)
Choose who can view/edit/delete and who can see the entries
Abillity to control all settings from within the shortcode
*/
if (class_exists("GFForms")) {
GFForms::include_addon_framework();
class StickyList extends GFAddOn {
protected $_version = "1.5.2";
protected $_min_gravityforms_version = "1.8.19.2";
protected $_slug = "sticky-list";
protected $_path = "gravity-forms-sticky-list/sticky-list.php";
protected $_full_path = __FILE__;
protected $_title = "Gravity Forms Sticky List";
protected $_short_title = "Sticky List";
protected $_capabilities_form_settings = 'gravityforms_stickylist';
protected $_capabilities_uninstall = 'gravityforms_stickylist_uninstall';
protected $_capabilities = array('gravityforms_stickylist', 'gravityforms_stickylist_uninstall');
public function init(){
parent::init();
// Add localization
$this->stickylist_localize();
// Add setting to fields settings tab
add_action("gform_field_standard_settings", array( $this, "stickylist_field_settings"), 10, 2);
// Add the Sticky List shortcode
add_shortcode( 'stickylist', array( $this, 'stickylist_shortcode' ) );
// Add supporting scripts to field settings page
add_action("gform_editor_js", array($this, "editor_script"));
// Add field settings page tooltips
add_filter("gform_tooltips", array( $this, "add_stickylist_tooltips"));
// Add css
add_action("wp_enqueue_scripts", array( $this, "register_plugin_styles"), 5);
// View or Edit entries
add_filter("gform_pre_render", array($this,"pre_entry_action"));
add_action("gform_after_submission", array($this, "post_edit_entry"), 10, 2);
// Delete entries
$this->maybe_delete_entry();
// Add notification options
add_action("gform_notification_ui_settings", array($this, "stickylist_gform_notification_ui_settings"), 10, 3 );
add_action("gform_pre_notification_save", array($this, "stickylist_gform_pre_notification_save"), 10, 2 );
add_filter("gform_disable_notification", array($this, "stickylist_gform_disable_notification" ), 10, 4 );
add_filter("gform_notification", array($this, "stickylist_modify_notification" ), 10, 3 );
// Add confirmation options
add_action("gform_confirmation_ui_settings", array($this, "stickylist_gform_confirmation_ui_settings"), 10, 3 );
add_action("gform_pre_confirmation_save", array($this, "stickylist_gform_pre_confirmation_save"), 10, 2 );
add_filter("gform_confirmation", array($this, "stickylist_gform_confirmation"), 10, 4);
// Update connected Wordpress post if exsists
add_filter("gform_post_data", array( $this, "stickylist_gform_post_data" ), 10, 3 );
// Make sure required file fields validate when prepopulated whith existing file during edit
add_filter('gform_validation', array( $this, "stickylist_validate_fileupload" ) );
// Allow edits of entries in forms with entry limit
add_filter( 'gform_pre_validation', array( $this, "stickylist_validate_limit" ) );
// Allow edits of entries in forms with no duplicate fields
add_filter( 'gform_is_duplicate', array( $this, "stickylist_validate_duplicate" ), 10, 4 );
}
// This helps with initiating the add-on
private static $_instance = null;
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new StickyList();
}
return self::$_instance;
}
/**
* Sticky List localization function
*
*/
function stickylist_localize() {
load_plugin_textdomain('sticky-list', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Sticky List field settings function
*
*/
function stickylist_field_settings($position, $form_id){
// Get the form
$form = GFAPI::get_form($form_id);
// Get form settings
$settings = $this->get_form_settings($form);
// Only show settings if Sticky List is enabled for this form
if(isset($settings["enable_list"]) && true == $settings["enable_list"]){
// Show below everything else
if($position == -1){ ?>
<li class="list_setting">
Sticky List
<br>
<input type="checkbox" id="field_list_value" onclick="SetFieldProperty('stickylistField', this.checked);" /><label class="inline" for="field_list_value"><?php _e('Show in list', 'sticky-list'); ?> <?php gform_tooltip("form_field_list_value") ?></label>
<br>
<input type="checkbox" id="field_nowrap_value" onclick="SetFieldProperty('stickylistFieldNoWrap', this.checked);" /><label class="inline" for="field_nowrap_value"><?php _e('Don\'t wrap text from this field', 'sticky-list'); ?> <?php gform_tooltip("form_field_nowrap_value") ?></label>
<br>
<input type="checkbox" id="field_nopopulate_value" onclick="SetFieldProperty('stickylistFieldNoPopulate', this.checked);" /><label class="inline" for="field_nopopulate_value"><?php _e('Don\'t populate this field on edit', 'sticky-list'); ?> <?php gform_tooltip("form_field_nopopulate_value") ?></label>
<br>
<label class="inline" for="field_list_text_value"><?php _e('Column label', 'sticky-list'); ?> <?php gform_tooltip("form_field_text_value") ?></label><br><input class="fieldwidth-3" type="text" id="field_list_text_value" onkeyup="SetFieldProperty('stickylistFieldLabel', this.value);" />
</li>
<?php
}
}
}
/**
* Format date field according to user preference
*
*/
function format_the_date($timestamp,$format) {
switch ( $format ) {
case 'mdy' :
$format_name = 'm/d/Y';
break;
case 'dmy' :
$format_name = 'd/m/Y';
break;
case 'dmy_dash' :
$format_name = 'd-m-Y';
break;
case 'dmy_dot' :
$format_name = 'd.m.Y';
break;
case 'ymd_slash' :
$format_name = 'Y/m/d';
break;
case 'ymd_dash' :
$format_name = 'Y-m-d';
break;
case 'ymd_dot' :
$format_name = 'Y.m.d';
break;
case '' :
$format_name = 'Y-m-d';
break;
}
return date($format_name, strtotime($timestamp));
}
/**
* Sticky List field settings JQuery function
*
*/
function editor_script(){
?>
<script type='text/javascript'>
// Bind to the load field settings event to initialize the inputs
jQuery(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_list_value").attr("checked", field["stickylistField"] == true);
jQuery("#field_nowrap_value").attr("checked", field["stickylistFieldNoWrap"] == true);
jQuery("#field_nopopulate_value").attr("checked", field["stickylistFieldNoPopulate"] == true);
jQuery("#field_list_text_value").val(field["stickylistFieldLabel"]);
});
</script>
<?php
}
/**
* Sticky List field settings tooltips function
*
*/
function add_stickylist_tooltips($tooltips){
$tooltips["form_field_list_value"] = __('<h6>Show field in list</h6>Check this box to show this field in the list.','sticky-list');
$tooltips["form_field_nowrap_value"] = __('<h6>Don\'t wrap whitespace</h6>Check this box to prevent wraping of text from this field','sticky-list');
$tooltips["form_field_nopopulate_value"] = __('<h6>Don\'t populate this field</h6>Check this box to prevent this field from being populated with the saved on edit','sticky-list');
$tooltips["form_field_text_value"] = __('<h6>Header text</h6>Use this field to override the default text header.','sticky-list');
return $tooltips;
}
/**
* Helper function to get current user
*
*/
public function stickylist_get_current_user() {
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
// If we didnt get a user ID we might be on buddypress
if( $current_user_id == NULL && function_exists("bp_loggedin_user_id") ) {
$current_user_id = bp_loggedin_user_id();
}
return $current_user_id;
}
/**
* Helper function to get form settings
*
*/
function get_sticky_setting($setting_key, $settings) {
if(isset($settings[$setting_key])) {
$setting = $settings[$setting_key];
}else{
$setting = "";
}
return $setting;
}
/**
* Sticky List shortcode function
*
*/
function stickylist_shortcode( $atts ) {
$shortcode_id = shortcode_atts( array(
'id' => '1',
'user' => '',
'showto' => '',
'field' => '',
'value' => '',
'test' => ''
), $atts );
// Get the form ID from shortcode
$form_id = $shortcode_id['id'];
// Get the user ID from shortcode
$user_id = $shortcode_id['user'];
// Get the user ID from shortcode
$showto = $shortcode_id['showto'];
// Get the filter field from shortcode
$filterField = $shortcode_id['field'];
// Get the filter value from shortcode
$filterValue = $shortcode_id['value'];
// Get the form
$form = GFAPI::get_form($form_id);
// Get form settings
$settings = $this->get_form_settings($form);
// Setting variables
$enable_list = $this->get_sticky_setting("enable_list", $settings);
$show_entries_to = $this->get_sticky_setting("show_entries_to", $settings);
$max_entries = $this->get_sticky_setting("max_entries", $settings);
$value_separator = $this->get_sticky_setting("value_separator", $settings);
$enable_clickable = $this->get_sticky_setting("enable_clickable", $settings);
$enable_postlink = $this->get_sticky_setting("enable_postlink", $settings);
$link_label = $this->get_sticky_setting("link_label", $settings);
$enable_view = $this->get_sticky_setting("enable_view", $settings);
$enable_view_label = $this->get_sticky_setting("enable_view_label", $settings);
$enable_edit = $this->get_sticky_setting("enable_edit", $settings);
$enable_edit_label = $this->get_sticky_setting("enable_edit_label", $settings);
$new_entry_id = $this->get_sticky_setting("new_entry_id", $settings);
$enable_delete = $this->get_sticky_setting("enable_delete", $settings);
$enable_delete_label = $this->get_sticky_setting("enable_delete_label", $settings);
$confirm_delete = $this->get_sticky_setting("confirm_delete", $settings);
$confirm_delete_text = $this->get_sticky_setting("confirm_delete_text", $settings);
$enable_duplicate = $this->get_sticky_setting("enable_duplicate", $settings);
$enable_duplicate_label = $this->get_sticky_setting("enable_duplicate_label", $settings);
$enable_pdf = $this->get_sticky_setting("enable_pdf", $settings);
$pdf_label = $this->get_sticky_setting("pdf_label", $settings);
$pdf_id = $this->get_sticky_setting("pdf_id", $settings);
$action_column_header = $this->get_sticky_setting("action_column_header", $settings);
$enable_sort = $this->get_sticky_setting("enable_sort", $settings);
$initial_sort = $this->get_sticky_setting("initial_sort", $settings);
$initial_sort_direction = $this->get_sticky_setting("initial_sort_direction", $settings);
$enable_search = $this->get_sticky_setting("enable_search", $settings);
$embedd_page = $this->get_sticky_setting("embedd_page", $settings);
$enable_pagination = $this->get_sticky_setting("enable_pagination", $settings);
$page_entries = $this->get_sticky_setting("page_entries", $settings);
// If a Custom embed url is set we override the selected embedd page
if(isset($settings["custom_embedd_page"]) && $settings["custom_embedd_page"] != "") $embedd_page = $settings["custom_embedd_page"];
// If a showto value is set in the shortcode we override the value in settings
if(isset($showto) && $showto != "") $show_entries_to = $showto;
// Only render list if Sticky List is enabled for this form
if($enable_list){
// Get current user or get user ID from shortcode
if($user_id != "") {
$current_user_id = $user_id;
$show_only_to_specific_user = true;
}else{
$current_user_id = $this->stickylist_get_current_user();
}
//Set max nr of entries to be shown
if($max_entries == "") { $max_entries = 999999; }
// Set sorting variables
if($enable_sort && $initial_sort) {
if($initial_sort == "date_added") {
$sorting = array();
}else{
$sorting = array('key' => $initial_sort, 'direction' => $initial_sort_direction );
}
}else{
$sorting = array();
}
// Set paging variables
$paging = array('offset' => 0, 'page_size' => $max_entries );
// Get entries to show depending on settings
// Show only to creator
if($show_entries_to === "creator" || isset($show_only_to_specific_user)){
$search_criteria["field_filters"][] = array("key" => "status", "value" => "active");
// Admin can allways see all entries
if (!current_user_can('administrator')) {
$search_criteria["field_filters"][] = array("key" => "created_by", "value" => $current_user_id);
}
$entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
// Show to all logged in users
}elseif($show_entries_to === "loggedin"){
if(is_user_logged_in()) {
$search_criteria["field_filters"][] = array("key" => "status", "value" => "active");
$entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
}
// Show to everyone
}elseif($show_entries_to === "everyone"){
$search_criteria["field_filters"][] = array("key" => "status", "value" => "active");
$entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
// Show to selected user role
}else{
$user = wp_get_current_user();
// Check if the current user has the selectecd role OR is admin
if( in_array( $show_entries_to, (array) $user->roles ) || in_array( "administrator", (array) $user->roles )) {
$search_criteria["field_filters"][] = array("key" => "status", "value" => "active");
$entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
}
}
// If a filter is set in the shortcode we filter out all entries that do not match
if(!empty($entries) && $filterField) {
foreach ($entries as $id => $data) {
if($data[$filterField] != $filterValue) {
unset($entries[$id]);
}
}
}
// Allow for entries filtering
if(!empty($entries)) {
$entries = apply_filters( 'filter_entries', $entries );
}
// If we have some entries, lets loop trough them and start building the output html
if(!empty($entries)) {
// Maybe reverse sort order
if($initial_sort == "date_added" && $initial_sort_direction == "ASC") {
$entries = array_reverse($entries);
}
// This vaiable will hold all html for the form
$list_html = "<div id='sticky-list-wrapper_$form_id' class='sticky-list-wrapper'>";
// If sorting and searching is enabled, show search box
if($enable_sort && $enable_search) {
$list_html .= "<input class='search' placeholder='" . __("Search", "sticky-list") . "' />";
}
$list_html .= "<table class='sticky-list'><thead><tr>";
// Get all fields
$fields = $form["fields"];
// Make a counter for use in sorting
$i = 0;
// Make table header
foreach ($fields as $field) {
if(isset($field["stickylistField"]) && $field["stickylistField"] != "") {
// If we have a custom field label we use that, if not we use the fields standard label
if(isset($field["stickylistFieldLabel"]) && $field["stickylistFieldLabel"] != "") {
$label = $field["stickylistFieldLabel"];
}else{
$label = $field["label"];
}
$class_label = "header-" . str_replace(" ", "-", strtolower($label));
$list_html .= "<th class='sort $class_label' data-sort='column-$i'>$label</th>";
// Increment sorting counter
$i++;
}
}
// If view, edit, delete, postlink, duplicate or pdf is enabled we need an extra column
if($enable_view || $enable_edit || $enable_delete || $enable_postlink || $enable_duplicate || $enable_pdf) {
$list_html .= "<th class='sticky-action'>$action_column_header</th>";
}
$list_html .= "</tr></thead><tbody class='list'>";
// Make table rows
foreach ($entries as $entry) {
$entry_id = $entry["id"];
// Make is_read class
$is_read = ($entry["is_read"]) ? "is_read" : "not_read" ;
$list_html .= "<tr class='" . $is_read . "'>";
// Recycle the sorting counter we used above
$i=0;
// Loop trough all the fields
foreach( $form["fields"] as $field ) {
// If the field is active
if (isset($field["stickylistField"]) && $field["stickylistField"] != "") {
// ...we get the value for it
$field_value = RGFormsModel::get_lead_field_value( $entry, $field );
// Set a class variable to use in list table
$tdClass = "stickylist-" . $field["type"];
// If nowrap is set for this field we add a class to it
$nowrap = "";
if(isset($field["stickylistFieldNoWrap"]) && $field["stickylistFieldNoWrap"] != "") {
$nowrap = " sticky-nowrap";
}
// Set $custom_file_upload to true if this is a custom field file upload
if($field["type"] == "post_custom_field" && $field["inputType"] == "fileupload") { $custom_file_upload = true; }else{ $custom_file_upload = false; }
// If the field is a product field
if ($field["type"] == "product" || $field["type"] == "shipping" || $field["type"] == "option") {
// ...and the value is an array
if(is_array($field_value)) {
// If this product field is an option field
if($field["type"] == "option") {
// Remove the price from the string
foreach ($field_value as &$option) {
$option = substr($option, 0, strpos($option, "|"));
}
// Remove empty values and implode the array
$field_value = array_filter($field_value);
$field_value = implode(", ", $field_value);
}else{
// Get the total number of products (last item in array)
$field_value = end($field_value);
}
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}else{
// Remove the price from the string
$field_value = substr($field_value, 0, strpos($field_value, "|"));
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
}
// If the value is an array (i.e. address field, name field, etc)
elseif(is_array($field_value)) {
// Sort the array by key so that the fields are shown in the correct order
ksort($field_value);
$field_values = "";
if($value_separator == "") $value_separator = " ";
// Concatenate field values into string separated by a space
foreach ($field_value as $field => $value) {
if(!empty($value)) $field_values .= $value . $value_separator;
}
$field_values = trim($field_values, $value_separator);
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_values</td>";
}
// If the field is a file field we use strtok to remove any metadata used by post_image filed (meta data is stored after "|" in string)
elseif ($field["type"] == "fileupload" || $field["type"] == "post_image" || $custom_file_upload == true ) {
$field_value = strtok($field_value, "|");
$file_name = basename($field_value);
// Make file clickable or not
if($enable_clickable && $field_value != "") {
$list_html .= "<td class='column-$i $nowrap $tdClass'><a href='$field_value'>$file_name</a></td>";
}else{
$list_html .= "<td class='column-$i $nowrap $tdClass'>$file_name</td>";
}
}
// If the field is a date field we need to format it
elseif ($field["type"] == "date" && $field_value != "") {
$field_value = $this->format_the_date($field_value,$field["dateFormat"]);
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
// If the field is an URL we need to format it
elseif ($field["type"] == "website" && $field_value != "") {
$tdClass = "stickylist-url";
$list_html .= "<td class='column-$i $nowrap $tdClass'><a href='$field_value'>$field_value</a></td>";
}
// If the field is an number field we need to format it
elseif ($field["type"] == "number" && $field_value != "") {
$tdClass = "stickylist-number";
if ($field["numberFormat"] == "decimal_comma") {
$field_value = number_format($field_value,2,",",".");
$field_value = str_replace(",00", "", $field_value);
}elseif ($field["numberFormat"] == "decimal_dot") {
$field_value = number_format($field_value,2,".",",");
$field_value = str_replace(".00", "", $field_value);
}elseif ($field->numberFormat == "currency") {
$currency = new RGCurrency( GFCommon::get_currency() );
$field_value = $currency->to_money( $field_value );
}
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
// If the field is a post category we need to remove the ID from the string
elseif ($field["type"] == "post_category" && $field_value != "") {
$tdClass = "stickylist-category";
$field_value = strtok($field_value, ":");
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
// If the field is a list field we need to unserialize it, flatten the array and implode it into a string
elseif ($field["type"] == "list" && $field_value != "") {
if(is_array(maybe_unserialize($field_value))) {
$list = maybe_unserialize($field_value);
$field_value = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($list)), FALSE);
$field_value = implode(", ", $field_value);
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
}
// All other fields
else{
$list_html .= "<td class='column-$i $nowrap $tdClass'>$field_value</td>";
}
// Increment sorting counter
$i++;
}
}
// If view, edit, delete, postlink, duplicate or pdf is enabled we need a cell with appropiate links
if($enable_view || $enable_edit || $enable_delete || $enable_postlink || $enable_duplicate){
$list_html .= "<td class='sticky-action'>";
// Only show view link if view is enabled
if($enable_view) {
$list_html .= "
<form action='$embedd_page' method='post'>
<button class='sticky-list-view submit'>$enable_view_label</button>
<input type='hidden' name='mode' value='view'>
<input type='hidden' name='view_id' value='$entry_id'>
</form>";
}
// Only show edit link if edit is enabled
if($enable_edit) {
// ...and current user is the creator OR has the capability to edit others posts
if($entry["created_by"] == $this->stickylist_get_current_user() || current_user_can('edit_others_posts') || current_user_can('stickylist_edit_entries')) {
$list_html .= "
<form action='$embedd_page' method='post'>
<button class='sticky-list-edit submit'>$enable_edit_label</button>
<input type='hidden' name='mode' value='edit'>
<input type='hidden' name='edit_id' value='$entry_id'>
</form>";
}
}
// Only show delete link if delete is enabled
if($enable_delete) {
// ...and current user is the creator OR has the capability to delete others posts
if($entry["created_by"] == $this->stickylist_get_current_user() || current_user_can('delete_others_posts') || current_user_can('stickylist_delete_entries')) {
$list_html .= "
<button class='sticky-list-delete submit'>$enable_delete_label</button>
<input type='hidden' name='delete_id' class='sticky-list-delete-id' value='$entry_id'>
";
// If the entry is connected to a post we add a hidden field with the post ID
if($entry["post_id"] != null ) {
$delete_post_id = $entry["post_id"];
$list_html .= "<input type='hidden' name='delete_post_id' class='sticky-list-delete-post-id' value='$delete_post_id'>";
}
}
}
// Only show post link if postlink is enabled
if($enable_postlink && $entry["post_id"] != NULL) {
$permalink = get_permalink($entry["post_id"]);
$list_html .= "<button class='sticky-list-postlink submit' onclick='document.location.href=\"$permalink\"'>$link_label</button>";
}
// Only show duplicate link if duplicate is enabled
if($enable_duplicate) {
$list_html .= "
<form action='$embedd_page' method='post'>
<button class='sticky-list-duplicate submit'>$enable_duplicate_label</button>
<input type='hidden' name='mode' value='duplicate'>
<input type='hidden' name='duplicate_id' value='$entry_id'>
</form>";
}
if($enable_pdf) {
$list_html .= "
<a href='". get_bloginfo('url') ."/pdf/$pdf_id/$entry_id/' target='_blank'><button type='button' class='sticky-list-view submit'>$pdf_label</button></a>";
}
$list_html .= "</td>";
}
$list_html .= "</tr>";
}
$list_html .= "</tbody></table>";
// If paignation is enabled we add the paignation container if there are more entries than what would fit in a page
if($enable_pagination && $page_entries < count($entries)) {
$list_html .= "<ul class='pagination'></ul>";
}
$list_html .= "</div>";
// If list sorting or pagination is enabled
if($enable_sort || $enable_pagination) {
// Build sort fields string
$sort_fileds = "";
for ($a=0; $a<$i; $a++) {
$sort_fileds .= "'column-$a',";
}
// Include list.js
$list_html .= "<script src='" . plugins_url( 'gravity-forms-sticky-list/js/list.min.js' ) . "'></script>";
// Include list.js pagination plugin
if($enable_pagination) {
$list_html .= "<script src='" . plugins_url( 'gravity-forms-sticky-list/js/list.pagination.min.js' ) . "'></script>";
}
// If both sort and paignation is enabled
if($enable_sort && $enable_pagination) {
$list_html .= "<script>var options = { valueNames: [$sort_fileds], page: $page_entries, plugins: [ ListPagination({ outerWindow: 1 }) ] };var userList = new List('sticky-list-wrapper_$form_id', options); function callback() { window.listUpdated(); } userList.on('updated', callback);</script><style>table.sticky-list th:not(.sticky-action) {cursor: pointer;}</style>";
// If only sort is enabled
}elseif($enable_sort && !$enable_pagination) {
$list_html .= "<script>var options = { valueNames: [$sort_fileds] };var userList = new List('sticky-list-wrapper_$form_id', options);</script><style>table.sticky-list th:not(.sticky-action) {cursor: pointer;}</style>";
// If only paignation is enabled
}elseif(!$enable_sort && $enable_pagination) {
$list_html .= "<script>var options = { valueNames: ['xxx'], page: $page_entries, plugins: [ ListPagination({ outerWindow: 1 }) ] };var userList = new List('sticky-list-wrapper_$form_id', options); function callback() { window.listUpdated(); } userList.on('updated', callback);</script></style>";
}
}
// If delete is enabled we need to insert ajax scripts to help with deletion
if($enable_delete) {
// Set som variables to use in the ajax function
$ajax_delete = plugin_dir_url( __FILE__ ) . 'ajax-delete.php';
$ajax_spinner = plugin_dir_url( __FILE__ ) . 'img/ajax-spinner.gif';
$delete_failed = __('Delete failed','sticky-list');
$list_html .= "
<img src='$ajax_spinner' style='display: none;'>
<script>
jQuery(document).ready(function($) {
window.listUpdated = function(){
$('#sticky-list-wrapper_$form_id').on('click', '.sticky-list-delete', function(){
event.stopImmediatePropagation()
var delete_id = $(this).siblings('.sticky-list-delete-id').val();
var delete_post_id = $(this).siblings('.sticky-list-delete-post-id').val();
var current_button = $(this);
var current_row = current_button.parent().parent();
var confirm_delete = $confirm_delete;
if(confirm_delete == 1) {
var confirm_dialog = confirm('$confirm_delete_text');
}
if (confirm_dialog == true || confirm_delete != 1) {
current_button.html('<img src=\'$ajax_spinner\'>');
$.post( '', { mode: 'delete', delete_id: delete_id, delete_post_id: delete_post_id, form_id: '$form_id' })
.done(function() {
current_button.html('');
current_row.css({
background: '#fbdcdc',
color: '#fff'
});
current_row.hide('slow', function() {
current_row.remove();
remaining_rows = $('#sticky-list-wrapper_$form_id tbody tr');
if(remaining_rows.length === 0) {
$('#sticky-list-wrapper_$form_id table').html('" . esc_js($settings["empty_list_text"]) . "');
}
});
})
.fail(function() {
current_button.html('$delete_failed');
})
}
});
}
window.listUpdated();
});
</script>
";
} else {
// declare global function to fix call to undeclared function window.listUpdated()
$list_html .= '
<script>
window.listUpdated = function(){}
</script>
';
}
// If we dont have any entries, show the "Empty list" text to the user
}else{
$list_html = $settings["empty_list_text"] . "<br>";
}
return $list_html;
}
}
/**
* Add Sticky List stylesheet and responsive js
*
*/
public function register_plugin_styles() {
wp_register_style( 'stickylist', plugins_url( 'gravity-forms-sticky-list/css/sticky-list_styles.css' ) );
wp_enqueue_style( 'stickylist' );
wp_register_script( 'stickylist_js', plugins_url( 'gravity-forms-sticky-list/js/sticky-list_responsive.js' ), array( 'jquery' ) );
wp_enqueue_script( 'stickylist_js' );
}
/**
* Performs actions when entrys are clicked in the list
*
*/
public function pre_entry_action($form) {
// Maybe enable get requests
$enable_get_requests = $this->get_sticky_setting("enable_get_requests", $this->get_form_settings($form));
if ($enable_get_requests) {
$_POST = array_merge($_POST, $_GET);
}
if( isset($_POST["mode"]) == "edit" || isset($_POST["mode"]) == "view" || isset($_POST["mode"]) == "duplicate") {
// No limits on edit/views
$form["limitEntries"] = false;
// Add lib to help with ajax in multi page forms
echo "<script src='" . plugins_url( 'gravity-forms-sticky-list/js/arrive.min.js' ) . "'></script>";
if($_POST["mode"] == "edit") {
$edit_id = $_POST["edit_id"];
$form_fields = GFAPI::get_entry($edit_id);
}
if($_POST["mode"] == "view") {
$view_id = $_POST["view_id"];
$form_fields = GFAPI::get_entry($view_id);
}
if($_POST["mode"] == "duplicate") {
$duplicate_id = $_POST["duplicate_id"];
$form_fields = GFAPI::get_entry($duplicate_id);
}
// If we have an entry that is active
if(!is_wp_error($form_fields) && $form_fields["status"] == "active") {
// ...and the current user is the creator OR has the capability to edit others posts OR is viewing the entry OR is duplicating the entry
if($form_fields["created_by"] == $this->stickylist_get_current_user() || current_user_can('edit_others_posts') || current_user_can('stickylist_edit_entries') || $_POST["mode"] == "view" || $_POST["mode"] == "duplicate") {
// Loop trough the form fields and check for upload- and category fields. If found, store ID in array
foreach ($form["fields"] as $fkey => &$fvalue) {
if($fvalue["type"] == 'fileupload' || $fvalue["type"] == "post_image") {
$uploads[] = $fvalue["id"];
}elseif ($fvalue["type"] == "post_custom_field" && $fvalue["inputType"] == "fileupload") {
$uploads[] = $fvalue["id"];
}elseif ($fvalue["type"] == 'post_category') {
$categories[] = $fvalue["id"];
}
}
if (!isset($uploads)) $uploads = "";
if (!isset($categories)) $categories = "";
// This variable will hold upload fields
$upload_inputs = false;
// Buid array of field id's that should not be populated on edit
$no_populate = array();
foreach ($form["fields"] as $test_field) {
$no_populate[$test_field->id] = $test_field->stickylistFieldNoPopulate;
}
// Loop trough all the fields
foreach ($form_fields as $key => &$value) {
// If the key is numeric we need to change it from [X.X] to [input_X_X]
if (is_numeric($key)) {
// Skip the field if No Populate is set
if (strpos($key, ".")) {
$field_key = substr($key, 0, strpos($key, "."));
}else{
$field_key = $key;
}
if($no_populate[$field_key])
continue;
// If the current field is a list field we need to unserialize it and flatten the array
if(is_array(maybe_unserialize($value))) {
$list = maybe_unserialize($value);
$value = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($list)), FALSE);
}
// If the current field is a post category field we need to remove all but the id from it (id is stored after : in string)
if (is_array($categories) && in_array( $key, $categories ) ) {
$value = substr( $value, strpos( $value, ':') + 1);
}
// If the value is numeric we might need to format if
if (is_numeric($value)) {
$field = GFFormsModel::get_field($form, $key);
if ($field->type == "number") {
if ($field->numberFormat == "decimal_comma") {
$value = str_replace(".", ",", $value);
}
}
}
// Format the key
$new_key = str_replace(".", "_", "input_$key");
$form_fields[$new_key] = $form_fields[$key];
// If the current field is an upload field we build the html to display it
if (is_array($uploads) && in_array( $key, $uploads ) ) {
if ($value != "") {
// Use strtok to remove any metadata used by post_image field (meta data is stored after "|" in string)
$path = strtok($value, "|");
$file = basename($path);
$delete_icon = plugin_dir_url( __FILE__ ) . 'img/delete.png';
// Only show the remove icon if we are in edit mode
if ($_POST["mode"] == "edit") {
$show_delete = " <a title=\"" . __("Remove","sticky-list") . "\" class=\"remove-entry\"><img alt=\"" . __("Remove","sticky-list") . "\" src=\"$delete_icon\"></a>";
}else{
$show_delete = "";
}
$upload_inputs = true;
?>
<script>
jQuery(document).ready(function($) {
$('input[name="<?php echo $new_key ?>"]').before('<div class="file_<?php echo $key?>"><a href="<?php echo $path?>"><?php echo $file?></a><?php echo $show_delete?><input name="file_<?php echo $key?>" type="hidden" value="<?php echo $value?>"></div>');
$(document).arrive("form", function(el) {
$('input[name="<?php echo $new_key ?>"]').before('<div class="file_<?php echo $key?>"><a href="<?php echo $path?>"><?php echo $file?></a><?php echo $show_delete?><input name="file_<?php echo $key?>" type="hidden" value="<?php echo $value?>"></div>');
});
});
</script>
<?php
}
}
// Unset old key
unset($form_fields[$key]);
}
}
// Add is_submit_id field
$form_id = $form['id'];
// ...only for the form that was acutally submitted
if (isset($_POST["mode"])) $form_fields["is_submit_$form_id"] = "1";
if (isset($_POST["gform_submit"]) && $_POST["gform_submit"] == $form_id) $form_fields["is_submit_$form_id"] = "1";
// Get current form settings
$settings = $this->get_form_settings($form);
// Maybe mark as read
$mark_read_view = $this->get_sticky_setting("mark_read_view", $settings);
$mark_read_edit = $this->get_sticky_setting("mark_read_edit", $settings);
if($_POST["mode"] == "edit") if($mark_read_edit) GFAPI::update_entry_property( $form_fields["id"], "is_read", 1);
if($_POST["mode"] == "view") if($mark_read_view) GFAPI::update_entry_property( $form_fields["id"], "is_read", 1);
// Get update text
if(isset($settings["update_text"])) $update_text = $settings["update_text"]; else $update_text = ""; ?>
<!-- Add JQuery to help with view/update/delete -->
<script>
jQuery(document).ready(function($) {
var thisForm = $('#gform_<?php echo $form_id;?>');
<?php // If we are in edit mode we insert two hidden fields with entry id and mode = edit
if($_POST["mode"] == "edit") { ?>
var edit = '<input type="hidden" name="action" value="edit" />';
var edit_id = '<input type="hidden" name="edit_id" value="<?php echo $edit_id; ?>" />';
var mode = '<input type="hidden" name="mode" value="edit" />';
thisForm.append(edit, edit_id, mode);
$("#gform_submit_button_<?php echo $form_id;?>").val('<?php echo $update_text; ?>');
$(document).arrive("form", function(el) {
$(el).append(edit, edit_id, mode);
$("#gform_submit_button_<?php echo $form_id;?>").val('<?php echo $update_text; ?>');
});
<?php
}
// If we are in view mode we insert two hidden fields with entry id and mode = view
if($_POST["mode"] == "view") { ?>
var view_id = '<input type="hidden" name="view_id" value="<?php echo $view_id; ?>" />';
var mode = '<input type="hidden" name="mode" value="view" />';
thisForm.append(view_id, mode);
formFieldsWrapper = $("#gform_wrapper_<?php echo $form_id;?> .gform_fields");
formFieldsWrapper.find('input, select, textarea').not("input[type=file]").attr("disabled","disabled");
$("#gform_submit_button_<?php echo $form_id;?>").remove();
$(document).arrive("form", function(el) {
$(el).append(view_id, mode);
$("#gform_submit_button_<?php echo $form_id;?>").remove();
});
<?php
}
// If we have a post ID it means that there is a post field present. We then insert a hidden field with the post ID for use later
if($form_fields["post_id"] != null ) { ?>
thisForm.append('<input type="hidden" name="post_id" value="<?php echo $form_fields["post_id"];?>" />');
$(document).arrive("form", function(el) {