<?php  
defined('ABSPATH') or die("Cannot access pages directly."); 

?>

<style type="text/css">
.red_pag_button a{
	text-decoration: none;
	margin-right: 10px;
}
</style>

<?php
    global $wpdb;
    include( plugin_dir_path( __FILE__ ) . 'settings.php');

//form submits:


//add folder
if( isset($_POST["add_folder"]) ){

$addfolder_folder = esc_sql( base64_decode( $_POST["option_folder"] ) );
if($_POST["option_folder"] == "/"){
    $addfolder_folder = "/";
}

$addfolder_access = esc_sql( $_POST["option_access"] );
$addfolder_id = esc_sql( $_GET["id"] );

    
    $query = "SELECT * FROM `" . $table_name . "` WHERE `folder` = '$addfolder_folder' AND `type` = '$addfolder_id' ";
    $results = $wpdb->get_results($query);

    if( sizeof($results) > 0){

                $wpdb->update(
                    $table_name,
                    array(
                        'folder' => $addfolder_folder,
                        'type'   =>  $addfolder_id,
                        'access' =>  $addfolder_access  
                    ),
                    array( 'folder' => $addfolder_folder, 'type'   =>  $addfolder_id ),
                    array(
                        '%s',   
                        '%s',
                        '%s'
                    ),
                    array( '%s', '%s' )
                );


    }else{

                    $wpdb->insert(
                            $table_name,
                            array(
                                'folder' => $addfolder_folder,
                                'type'   =>  $addfolder_id,
                                'access' =>  $addfolder_access,
                                'meta'   => ''
                            ),
                            array(
                                '%s',
                                '%s',
                                '%s',
                                '%s'
                            )
                        );


    }//end of else


}
//end of add folder


//delete folder
if( isset($_GET["perform"]) && $_GET["perform"] == "delete" ){

$delfolder_folder = esc_sql( base64_decode( $_GET["folder_delete"] ) );
$delfolder_id = esc_sql( $_GET["id"] );

    $query = "DELETE FROM `" . $table_name . "` WHERE `folder` = '$delfolder_folder' AND `type` = '$delfolder_id' ";
    $wpdb->query($query);

}
//end of delete folder


// make_default
if( isset($_GET["perform"]) && $_GET["perform"] == "make_default" ){

    $default_folder = esc_sql( base64_decode( $_GET["folder"] ) );
    $userid = esc_sql( $_GET["id"] );

    // echo $default_folder;

    $query = "UPDATE `" . $table_name . "` SET `meta` = '' WHERE  `type` = '$userid' ";
    $wpdb->query($query);

    update_user_meta($userid, "redfm_override_default_folder", $default_folder);

}
//end of delete folder



// email_template
if(isset($_POST["jaction"]) && $_POST["jaction"] == "email_template"){
   $userid = esc_attr( $_GET["id"] );
   $template_text = esc_attr($_POST["template_text"]);
   $template_text_user = esc_attr($_POST["template_text_user"]);
   //

   update_user_meta($userid, "fm_overrule_user_to_admin_email_template", $template_text);
   update_user_meta($userid, "fm_overrule_admin_to_user_email_template", $template_text_user);

}

// override templates?
if(isset($_POST["jaction"]) && $_POST["jaction"] == "override_default_folder_emails"){
   $userid = esc_attr( $_GET["id"] );
   $override = esc_attr($_POST["override"]);

   update_user_meta($userid, "fm_overrule_email_templates", $override);

}




if(isset($_POST["jaction"]) && $_POST["jaction"] == "admin_email_notification"){
   $userid = esc_attr( $_GET["id"] );
   $notification_setting = esc_attr($_POST["admin_email_notification"]);

   update_user_meta($userid, "fm_overrule_admin_email_notification", $notification_setting);
}




    $blogusers = get_users();

?>



<div id="fm_settings_wrapper">
<h2 class="section_title"> User's Access </h2><br>
<p style="border: 1px solid #ccc;padding: 10px;display: inline-block;background-color:#f5f5f5;">Use the shortcode: <code>[filemanager foldername="*"]</code> to show every user all their assigned folders.</p>
<br><br>

<div id="fm_users_table_wrapper">
<table class="widefat" id="fm_users_table">
<thead>
    <tr style="background-color:#F5F5F5;">
    	<th>User</th>
        <th>Role</th>
        <th>Folders</th>      
    </tr>
</thead>
<tfoot>
    <tr style="background-color:#F5F5F5;">
    	<th>User</th>
        <th>Role</th>
        <th>Folders</th>      

    </tr>
</tfoot>
<tbody>
    

    <?php foreach($blogusers as $key=>$value) { ?>
    <?php  
    	$usrdata = get_userdata( $value->ID );
    	
    ?>
     <tr>
     <td><?php echo $value->user_nicename ?></td>
     <td> <?php echo $usrdata->roles[0]; ?> </td>
     <td><a href="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$value->ID"); ?>">Folders</a></td>

     </tr>

     <?php } ?>

   
</tbody>
</table>
</div>



<?php if( isset( $_GET["action"] ) ){ ?>
    <?php if($_GET["action"] == "view_folders"){ ?>
        <?php
             $id=0;
             $id= esc_sql( $_GET["id"] );

             $query = "SELECT * FROM `" . $table_name . "` WHERE `type` = '$id' ";

             $allow_default_folders = get_option("red_fm_create_default_folders");

             if($allow_default_folders == "0"){
                    $query = "SELECT * FROM `" . $table_name . "` WHERE `type` = '$id' AND `meta` <> 'red_fm_default' ";
             }

             $results = $wpdb->get_results($query);
 
            $usrdata = get_userdata( $id );
        
        ?>




<hr>

<br/>

<h3> <?php echo $usrdata->user_nicename ?>'s Folders:  </h3>

<table class="widefat">
<thead>
    <tr style="background-color:#F5F5F5;">
        <th>Folder</th>
        <th>Access</th>
        <th>Action</th>   
        <th>Type</th> 
        <th>Change Type</th>  
    </tr>
</thead>
<tfoot>
    <tr style="background-color:#F5F5F5;">
        <th>Folder</th>
        <th>Access</th>
        <th>Action</th> 
        <th>Type</th> 
        <th>Change Type</th>
    </tr>
</tfoot>
<tbody>

    <?php foreach($results as $key=>$value){ ?>
    <?php  

        $meta = $value->meta;

        $fn = red_getFolderName( $value->folder, $directory_temp);

        if($value->folder == "/"){
               $fn = "/";
        }

    ?>
         <tr>
             <td><?php echo $fn; ?></td>
             <td> <?php echo red_defineAccess( $value->access ); ?> </td>
             <td><a href="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id&perform=delete&folder_delete=". base64_encode($value->folder) ); ?>">Delete</a></td>
             <td>
                <?php 
                    $is_default = false;

                    if($meta == "red_fm_default"){
                        echo __("Default Folder", "redfm"); 
                        $is_default = true;
                    }
                    

                    $override_default_folder = get_user_meta($id, "redfm_override_default_folder", true);
                    if($override_default_folder == $value->folder){
                        echo __("Default Folder", "redfm");
                        $is_default = true;
                    }
                ?>
                    
             </td>

             <td>  
                <?php
                    if(!$is_default){
                        $default_url = admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id&perform=make_default&folder=". base64_encode($value->folder) );
                        echo "<a class='button button-secondary menu-save make_default_link' href='{$default_url}'>Make Default</a>";
                    }  


                ?>
             </td>             


         </tr>
     <?php } ?>

   
</tbody>
</table>

<br/>

<h3>Add/Edit Folder:</h3>
<form action="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id"); ?>" method="POST">
        
        <input type="hidden" name="add_folder" value="1">
        <select name="option_folder" id="fm_option_folder">

        <?php foreach($directory_names as $key=>$value){ ?>
        
            <?php if($value != "/"){ ?>
                <option value="<?php echo base64_encode($directory_list[$key]); ?>"><?php echo $value; ?></option>  
            <?php }else{ ?>
                <option value="<?php echo "/"; ?>"><?php echo $value; ?></option> 
            <?php } ?>

        <?php } ?>

    </select>

    <select name="option_access">
        <option value="r">Read</option>
        <option value="rw">Read/Write</option>
        <option value="ru">Read/Upload</option>
        <option value="rp">Preview Files Only</option>
    </select>

    <input type="submit" class="button button-primary menu-save" value="Add Folder">

</form>


<small style="color:green;">Read/Upload will only let you read and upload files/folders, file editing/deleting is not allowed.</small>




    <?php } ?> <!-- if $_GET .. view folders -->

<?php } ?>    <!-- isset action -->




</div>

<?php  

$userid = esc_attr( $_GET["id"] );
$notification_setting = get_user_meta($userid, "fm_overrule_admin_email_notification", true);

if(!isset($notification_setting)){
    $notification_setting = 1;
}

?>


<?php if( isset( $_GET["action"] ) ){ ?>
    <?php if($_GET["action"] == "view_folders"){ ?>




        <div id="fm_default_folder_settings">
            <h2 class="section_title">Default Folder Settings</h2>


        <form action="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id"); ?>" method="POST">
            <input type="hidden" name="jaction" value="override_default_folder_emails">
            <h3>Override default email notification templates</h3>
            <?php  
                $override = get_user_meta($userid, "fm_overrule_email_templates", true);

            ?>
            <select name='override'>
                <option value='old' <?php echo $override == "old" ? "selected=selected" : ""; ?>>Use default email templates</option>
                <option value='new' <?php echo $override == "new" ? "selected=selected" : ""; ?>>Override Email Templates</option>
            </select>
            <input type="submit" class="button button-primary menu-save" value="Save">
        </form>

        <br>
        <hr>
        <br>

<?php if($override == "new"){ ?>
            <form action="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id"); ?>" method="POST">



                            <input type="hidden" name="jaction" value="email_template">
                            
                            <?php  
                                // $email_template = get_option("fm_overrule_admin_email_notification", "");
                                $email_template = get_user_meta($userid, "fm_overrule_user_to_admin_email_template", true);

                                $content   = $email_template;
                                $editor_id = 'email_template_editor';
                                $settings = array("textarea_name" => "template_text", "textarea_rows" => 6, "media_buttons" => false, 'teeny' => true);
                                echo "<h3>Admin Email:</h3>";
                                wp_editor( $content, $editor_id, $settings );

                            ?>
                            <small class='notification_email_label' style='color:green;'>you can use {operation} {username} and {files} shortcodes</small><br/>


                            <?php  
                                // $email_template = get_option("fm_overrule_admin_email_notification", "");
                                $email_template = get_user_meta($userid, "fm_overrule_admin_to_user_email_template", true);                                

                                $content   = $email_template;
                                $editor_id = 'email_template_editor_user';
                                $settings = array("textarea_name" => "template_text_user", "textarea_rows" => 6, "media_buttons" => false, 'teeny' => true);
                                echo "<h3>User Email:</h3>";
                                wp_editor( $content, $editor_id, $settings );

                            ?>
                            <small class='notification_email_label' style='color:green;'>you can use {operation} and {files} shortcodes</small><br/>




                            <br>




                <input type="submit" class="button button-primary menu-save" value="Save">
            </form>
            

<?php } ?>
            
        </div>









        <div id="fm_users_settings">
            <h2 class="section_title"><?php echo $usrdata->user_nicename ?>'s Settings:</h2>
            <form action="<?php echo admin_url("admin.php?page=red_fm_manager_users&action=view_folders&id=$id"); ?>" method="POST">
                <h3>Disable Default Folder Upload Notifications</h3>
                <input type="hidden" name="jaction" value="admin_email_notification">
                <select name="admin_email_notification">
                    <option value="1" <?php echo $notification_setting == 1 ? "selected" : ""; ?>>Default Behavior</option>
                    <option value="0" <?php echo $notification_setting == 0 ? "selected" : ""; ?>>Disable</option>
                </select>
                <input type="submit" class="button button-primary menu-save" value="Save">
            </form>
            <br>
            <small>Setting this to <b>disabled</b> will disable the email notification when admin uploads a file to the user's default folder.</small><br>
            <small>You can setup email notifications in the <a href="admin.php?page=red_fm_settings">Settings Page</a></small>
        </div>

    <?php } ?>
<?php } ?>


<style type="text/css">
    #fm_users_table_wrapper{
        padding: 0 5px 0 0;
    }


#fm_users_settings, #fm_default_folder_settings{
background-color: #fff;
    padding: 10px 10px;
    margin-top: 20px;
    max-width: 1600px;
    margin-right: 10px;
    box-shadow: 1px 1px 10px rgba(0,0,0,0.1);
    border: 1px solid #ccc;
}


.dataTables_wrapper .dataTables_paginate .paginate_button {
    color: #2b9199 !important;
    border: 1px solid #ccc;
    background-color: #f9f9f9;
}


#fm_settings_wrapper {
    background-color: #fff;
    padding: 10px 10px;
    margin-top: 20px;
    max-width: 1600px;
    margin-right: 10px;
    box-shadow: 1px 1px 10px rgba(0,0,0,0.1);
    border: 1px solid #ccc;
}


.dataTables_filter{
    margin-bottom: 10px;
}


.section_title{
    background-color: #333;
    display: inline-block;
    color: #fff;
    padding: 10px 10px 10px 10px;
    position: relative;
    left: -10px;
    top:-10px;
    margin-bottom: 0;
}

.wp-editor-wrap, .notification_email_label{
    position: relative;
    top: -40px;
}

</style>