Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multiple file viewing & editing support #33

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Sticky List is an add-on for the WordPress plugin <a href="http://www.gravityfor
* Fully localized. You can <a href="https://github.com/13pixlar/sticky-list/tree/master/languages">add your translation</a>
* Fully supported and maintained
* Completely free and open source
* Support for multiple uploads view & update in file field

**Note:** <a href="http://www.gravityforms.com/" target="_blank">Gravity Forms</a> version 1.8.19.2+ is required for this plugin.

Expand All @@ -44,7 +45,6 @@ Sticky List is an add-on for the WordPress plugin <a href="http://www.gravityfor
* Graphic shortcode builder
* Export list to .csv from front end
* Abillity to control ALL settings from within the shortcode
* Support for multiple uploads in file field
* Support for full multi page forms

#### Usage
Expand Down Expand Up @@ -212,9 +212,9 @@ Make sure that the plugin is activated **and** that your Gravity Forms version i

Make sure that your user/role has the correct capabilities. You can use a <a href="https://wordpress.org/plugins/user-role-editor/">role editor plugin</a> to check this. The capabilities you are looking for are `gravityforms_stickylist` and `gravityforms_stickylist_uninstall`.

<h5>File uploads can't be edited</h5>
<h5>File uploads can't be edited(Resolved)</h5>

Sticky List does not support multi file uploads (where you can upload multiple files to a single field). Multi file uploads are in the roadmap for a future release. In the meantime you can use single file uploads which are supported.
~~Sticky List does not support multi file uploads (where you can upload multiple files to a single field). Multi file uploads are in the roadmap for a future release. In the meantime you can use single file uploads which are supported.~~

<h5>Can I display a thumbnail/icon instead of the file name in the list?</h5>

Expand Down Expand Up @@ -281,6 +281,10 @@ Then create a field in your form with a checkbox that says "Approved". Note the

<h3>Changelog</h3>

**2.0**
* Fixed some bugs.
* Added multiple file viewing & editing support

**1.5.2**
* Fixed an issue that would not show confirmations based on conditional logic
* Added some responsivness to list
Expand Down
109 changes: 92 additions & 17 deletions class-sticky-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class StickyList extends GFAddOn {

protected $_version = "1.5.2";
protected $_version = "2.0";
protected $_min_gravityforms_version = "1.8.19.2";
protected $_slug = "sticky-list";
protected $_path = "gravity-forms-sticky-list/sticky-list.php";
Expand Down Expand Up @@ -343,6 +343,8 @@ function stickylist_shortcode( $atts ) {
if (!current_user_can('administrator')) {
$search_criteria["field_filters"][] = array("key" => "created_by", "value" => $current_user_id);
}
//var_dump($search_criteria);
//var_dump($sorting);

$entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);

Expand Down Expand Up @@ -384,6 +386,9 @@ function stickylist_shortcode( $atts ) {
$entries = apply_filters( 'filter_entries', $entries );
}

//Allow for entries filtering whenever the entries are empty or not
$entries = apply_filters( 'filter_entries_for_all', $entries,$form_id);

// If we have some entries, lets loop trough them and start building the output html
if(!empty($entries)) {

Expand All @@ -400,7 +405,7 @@ function stickylist_shortcode( $atts ) {
$list_html .= "<input class='search' placeholder='" . __("Search", "sticky-list") . "' />";
}

$list_html .= "<table class='sticky-list'><thead><tr>";
$list_html .= "<div class='sticky-list-table-wrapper'><table class='sticky-list'><thead><tr>";

// Get all fields
$fields = $form["fields"];
Expand Down Expand Up @@ -659,7 +664,7 @@ function stickylist_shortcode( $atts ) {

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>";
<a class='gf-sticky-pdf' 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>";
Expand All @@ -668,7 +673,7 @@ function stickylist_shortcode( $atts ) {
$list_html .= "</tr>";
}

$list_html .= "</tbody></table>";
$list_html .= "</tbody></table></div>";

// 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)) {
Expand Down Expand Up @@ -697,15 +702,15 @@ function stickylist_shortcode( $atts ) {

// 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>";
$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>";
$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>";
}
}

Expand Down Expand Up @@ -768,7 +773,14 @@ function stickylist_shortcode( $atts ) {
});
</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{
Expand Down Expand Up @@ -811,6 +823,7 @@ public function pre_entry_action($form) {

// Add lib to help with ajax in multi page forms
echo "<script src='" . plugins_url( 'gravity-forms-sticky-list/js/arrive.min.js' ) . "'></script>";
echo "<script src='" . plugins_url( 'gravity-forms-sticky-list/js/sticky-list_multiple_file_view_edit.js' ) . "'></script>";

if($_POST["mode"] == "edit") {
$edit_id = $_POST["edit_id"];
Expand Down Expand Up @@ -911,18 +924,57 @@ public function pre_entry_action($form) {
$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
if(strpos($value, '[') === 0){//Enable Multi-File Upload
$value_json = json_decode($value);
$file_preview_html="";
if($show_delete != ""){// if this page is in edit mode, change the show_delete html
$show_delete = " <a title=\"" . __("Remove","sticky-list") . "\" class=\"remove-entry-for-multi-file-upload\"><img alt=\"" . __("Remove","sticky-list") . "\" src=\"$delete_icon\"></a>";
}
$field_files_arr_string = "";
foreach ($value_json as $key => $value) {
$field_files_arr_string .= ",{\"existed_filename\":\"".$value."\"}";
$file_obj = basename($value);
$file_preview_html .= '<div class="gform_preview_file"><a class="gform_preview_file_link" target="_blank" href="'.$value.'">'.$file_obj.'</a> '.$show_delete.'</div>';
}
if(strlen($field_files_arr_string)>0){
//remove the first comma
$field_files_arr_string = substr($field_files_arr_string,1);
//wrap with []
$field_files_arr_string = "[".$field_files_arr_string."]";
}
?>
<script>
jQuery(document).ready(function($) {
var $gform_uploaded_files = $('#gform_uploaded_files_<?php echo $form['id'] ?>');
var gform_uploaded_files_val = $gform_uploaded_files.val();
var gform_uploaded_files_val_json={};
if(gform_uploaded_files_val){
gform_uploaded_files_val_json = $.parseJSON(gform_uploaded_files_val);
}

gform_uploaded_files_val_json["input_<?php echo $field_key ?>"] = (<?php echo $field_files_arr_string ?>);
$gform_uploaded_files.val($.toJSON(gform_uploaded_files_val_json));
validateFilesSizeAfterInitialization('gform_multifile_upload_<?php echo $form['id'] ?>_<?php echo $field_key ?>');

$('#gform_preview_<?php echo $form['id'] ?>_<?php echo $field_key ?>').append('<?php echo $file_preview_html ?>');
});
</script>
<?php
}else{//Single-File Upload
?>
<script>
jQuery(document).ready(function($) {
$('input[name="<?php echo $new_key ?>"]').before('<div class="file_<?php echo $key?>"><a target="_blank" 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 target="_blank" 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
}
}
}

Expand Down Expand Up @@ -1035,6 +1087,29 @@ public function post_edit_entry($entry, $form) {
$old_entry = $original_entry;
$new_entry = $entry;

$gform_uploaded_files = $_POST["gform_uploaded_files"];
$gform_uploaded_files_obj = json_decode(stripslashes($gform_uploaded_files));
if ( is_object( $gform_uploaded_files_obj ) ){
$gform_uploaded_files_obj_vars = get_object_vars( $gform_uploaded_files_obj );
foreach( $gform_uploaded_files_obj_vars as $gform_uploaded_file_key => $gform_uploaded_file_value ){

$file_field_index = substr($gform_uploaded_file_key,6);
$new_entry_file_string = $new_entry[$file_field_index];
$new_entry_file_arr = json_decode($new_entry_file_string,true);

foreach( $gform_uploaded_file_value as $_key => $_value ){
if(property_exists($_value,"existed_filename")){
$_value_existed_filename = $_value->existed_filename;
$new_entry_file_arr[$_key]=$_value_existed_filename;
}
}
$correct_files = json_encode($new_entry_file_arr);
$new_entry[$file_field_index] = $correct_files;
$old_entry[$file_field_index] = $correct_files;
$entry[$file_field_index] = $correct_files;
}
}

// If we have an original entry
if($original_entry && is_wp_error($original_entry) == false) {

Expand Down
24 changes: 24 additions & 0 deletions css/sticky-list_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ div[class^="file_"] a.remove-entry img {
div[class^="file_"] + input[type="file"] {
display: none;
}

.sticky-list thead .sort:after{
content:"\21C5";
font-weight: normal;
margin-left: 5px;
}

.sticky-list thead .asc:after{
content:"\2193";
font-weight: normal;
margin-left: 5px;
}

.sticky-list thead .desc:after{
content:"\2191";
font-weight: normal;
margin-left: 5px;
}
.remove-entry-for-multi-file-upload:hover{
cursor: pointer;
}
.sticky-list-table-wrapper{
overflow-x: auto;
}
70 changes: 70 additions & 0 deletions js/sticky-list_multiple_file_view_edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
jQuery(document).ready(function($) {

});

function validateFilesSizeAfterInitialization(uploadElementId) {
var $ = jQuery;
var $uploadElement = $("#"+uploadElementId);
var settings = $($uploadElement).data('settings');
if(settings){
var button = typeof settings.browse_button == "string" ? $("#" + settings.browse_button) : $(settings.browse_button);

var form_id = settings.multipart_params.form_id;
var field_id = settings.multipart_params.field_id;
//Count the existing files
var totalCount = 0;
var filesJson = $('#gform_uploaded_files_' + form_id).val();
if(filesJson){
filesJson = $.parseJSON(filesJson);
var currentFieldArr = filesJson["input_"+field_id];
if(currentFieldArr)totalCount=currentFieldArr.length;
}

var max = parseInt(settings.gf_vars.max_files,10);

if( max > 0 && totalCount >= max){
var gf_strings = typeof gform_gravityforms != 'undefined' ? gform_gravityforms.strings : {};

var messagesID = settings.gf_vars.message_id;
var message = gf_strings.max_reached;
button.prop("disabled", true);
$("#" + messagesID).prepend("<li>" + $('<div/>').text(message).html() + "</li>");
}
}
}

jQuery(document).on("click",".remove-entry-for-multi-file-upload",function () {
var $ = jQuery;
var $this = $(this);

var gform_preview_id = $this.closest(".gform_preview_file").parent().attr("id");
var current_file_name = $this.closest(".gform_preview_file").first().find(".gform_preview_file_link").first().attr("href");
var gform_preview_id_arr = gform_preview_id.split("_");
var form_id = gform_preview_id_arr[gform_preview_id_arr.length-2];
var field_id = gform_preview_id_arr[gform_preview_id_arr.length-1];
$this.closest(".gform_preview_file").remove();

var $gform_uploaded_files = $('#gform_uploaded_files_'+form_id);
var gform_uploaded_files_val = $gform_uploaded_files.val();
if(gform_uploaded_files_val){
var gform_uploaded_files_val_json = $.parseJSON(gform_uploaded_files_val);
var current_field_json = gform_uploaded_files_val_json["input_"+field_id];
var temp_field_files_arr = [];
var old_field_files_length = current_field_json.length;
current_field_json.forEach(function (element) {
var existed_filename = element.existed_filename;
if(current_file_name!=existed_filename){
temp_field_files_arr.push(element);
}
});
gform_uploaded_files_val_json["input_"+field_id] = temp_field_files_arr;
var new_field_files_length = temp_field_files_arr.length;
$gform_uploaded_files.val($.toJSON(gform_uploaded_files_val_json));
if(new_field_files_length<old_field_files_length){
var settings = $($("#gform_multifile_upload_"+form_id+"_"+field_id)).data('settings');
var button = typeof settings.browse_button == "string" ? $("#" + settings.browse_button) : $(settings.browse_button);
button.prop("disabled", false);
$("#gform_multifile_messages_"+form_id+"_"+field_id).children().remove();
}
}
});