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

Select multiple users? #3

Open
trhooper123 opened this issue Mar 9, 2016 · 3 comments
Open

Select multiple users? #3

trhooper123 opened this issue Mar 9, 2016 · 3 comments

Comments

@trhooper123
Copy link

Hello, Awesome plugin! I am using it for a school project where we have to assign pdf docs to 3 specific users. I found the lines in the php code and edited them to show three drop down boxes but it makes all three boxes the same whenever I save. Any tips or ideas??

<p><input type="file" name="upf_file" id="upf_file" /></p>
<p class="label"><label for="upf_user1"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user1" id="upf_user1">
    <?php
    $users = get_users();
    $upf_user1 = get_post_meta($post->ID, 'upf_user1', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user1 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<p class="label"><label for="upf_user2"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user2" id="upf_user2">
    <?php
    $users = get_users();
    $upf_user2 = get_post_meta($post->ID, 'upf_user2', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user2 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<p class="label"><label for="upf_user3"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user3" id="upf_user3">
    <?php
    $users = get_users();
    $upf_user3 = get_post_meta($post->ID, 'upf_user3', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user3 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   

<?php 
@fldtrace
Copy link
Owner

fldtrace commented Mar 9, 2016

it's not clear if the functionality is working when you save but in case it does and you want to save the selection you can look into something like this solution:
http://stackoverflow.com/questions/10957926/saving-dropdown-menu-selection-in-a-cookie

@trhooper123
Copy link
Author

It will let you select three things, then when you click update on the page it changes all three to the same user.

@trhooper123
Copy link
Author

I was able to get it working with choosing 3 users and having it save them successful. The only thing left is to have it display it for the users. I am only able to get it to show for the first user.

Here is the code for creating the three separate users

<p><input type="file" name="upf_file" id="upf_file" /></p>
<p class="label"><label for="upf_user1"><?php _e('Select a user', 'user-private-files');?></label></p>  
<select name="upf_user1" id="upf_user1">
    <?php
    $users = get_users();
    $upf_user1 = get_post_meta($post->ID, 'upf_user1', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user1 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>   
<select name="upf_user2" id="upf_user2">
    <?php
    $users = get_users();
    $upf_user2 = get_post_meta($post->ID, 'upf_user2', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user2 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>
</select>
<p class="label"><input type="checkbox" name="upf_notify" value="1"> <label for="upf_notify"><?php _e('Notify User', 'user-private-files');?></label></p>
<select name="upf_user3" id="upf_user3">
    <?php
    $users = get_users();
    $upf_user3 = get_post_meta($post->ID, 'upf_user3', true);
    foreach ($users as $user) { ?>
        <option value="<?php echo $user->ID;?>" <?php if ($upf_user3 == $user->user_login) echo 'selected="selected"';?>><?php echo $user->user_login;?></option>
        <?php
    }
    ?>

And for saving the three users

// if invalid $post object or post type is not 'userfile', return
if(!$post || get_post_type($post->ID) != 'userfile') return;

$user_info = get_userdata($_POST['upf_user1']);
add_post_meta($post_id, 'upf_user1', $user_info->user_login);
update_post_meta($post_id, 'upf_user1', $user_info->user_login);

$user_info = get_userdata($_POST['upf_user2']);
add_post_meta($post_id, 'upf_user2', $user_info->user_login);
update_post_meta($post_id, 'upf_user2', $user_info->user_login);

$user_info = get_userdata($_POST['upf_user3']);
add_post_meta($post_id, 'upf_user3', $user_info->user_login);
update_post_meta($post_id, 'upf_user3', $user_info->user_login);

And here is where I am stuck where it creates the file list

'userfile', 'meta_key' => 'upf_user1', 'meta_value' => $current_user->user_login, 'orderby' => 'date', 'order' => DESC );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@fldtrace @trhooper123 and others