<?php
global $bp, $wpdb;

if (is_null($idEventCreate)) {
    $idEventCreate = $_GET['id'];
}

$event = $wpdb->get_row('select * from wp_jet_events where id = "' . $idEventCreate . '"');

$avatarExistBpthumb = '';
$avatarExistFull = '';

    if ($_POST['SubirImagen'] and $_POST['userAvatar'] != '' and $_POST['userAvatar'] != '.' and $_POST['userAvatar'] != '..' ) {
        //move Avatar files
        $avatar_folder_dir  =  BP_AVATAR_UPLOAD_PATH . '/event-avatars/' . $event->id . '/';
        
        if (file_exists($avatar_folder_dir)) {
            $av_dir             = opendir( $avatar_folder_dir );
            
            while (($full = readdir($av_dir))) {
                if ($full == '..' or $full == '.') {
                    continue;
                } else {
                    unlink($avatar_folder_dir . $full);
                }
                
            }
        }
        
        $sourcePath= str_replace("\\","/", getcwd())."/wp-content/uploads/avatars/signups/";
        $targetPath= str_replace("\\","/", getcwd())."/wp-content/uploads/event-avatars/" . $event->id . "/";
        
        if (!file_exists($targetPath)) {
        
            mkdir($targetPath, 0777, true);
        }
        
        chmod($targetPath, 0777);
        
        if( file_exists($sourcePath.$_POST['userAvatar']) and  $_POST['userAvatar'] != "") {
        
            rename($sourcePath.$_POST['userAvatar'], $targetPath.$_POST['userAvatar']);
            $userAvatar = str_replace("bpfull", "bpthumb", $_POST['userAvatar']);
        
            rename($sourcePath.$userAvatar, $targetPath.$userAvatar);
        }
    }
    
    if ($_POST['deleteImagen']) {
        
        $avatar_folder_dir  =  BP_AVATAR_UPLOAD_PATH . '/event-avatars/' . $event->id . '/';
        
        if (is_dir($avatar_folder_dir)) {
            
            $av_dir             = opendir( $avatar_folder_dir );
            while (($full = readdir($av_dir))) {
                if ($full == '..' or $full == '.') {
                    continue;
                } else {
                    unlink($avatar_folder_dir . $full);
                }
            
            }
        }
        
    }

?>
<script type="text/javascript" src="wp-content/themes/cvp-irontec/events/custom/js/scripts.js"></script>
<style>
li.iron-encabezado-li a.selected {
    background: whiteSmoke;
    color: #444;
    padding: 5px 7px;
}

li.iron-encabezado-li a:HOVER {
    text-decoration: none;
}
</style>

<div class="padder" id="jes-padder">
    
    <div id="content-middle">
    
    <div id="content-middle-add">

        <div id="item-body">
        
        <div id="form-event-avatar">
            <form action="<?php echo bp_get_root_domain() . '/eventos-create/?option=event-details&id=' . $event->id; ?>" name="event-settings-form" id="event-settings-form" class="standard-form" method="POST" enctype="multipart/form-data">
            
            <?php _e('Sube una imagen para ser utilizada como Avatar del evento. La imagen, se mostrará en la página del evento y en los resultados de búsqueda.', 'buddypress')?>
            
                <div class="picture">
                    <div id="dinamic_content_1" class="loadimg">
                        <iframe id="upload_target" name="upload_target"
                        style=" border: 1px solid #C3C3C3; width: 177px; overflow: hidden; height: 200px; border-radius: 10px; margin: 15px;"
                        src="<?php echo get_stylesheet_directory_uri().'/_inc/din-upload/din-upload.php?event-detail=true&id=' . $event->id; ?>" frameBorder="0" scrolling="no"></iframe>
                    </div>
                </div>
                <?php
                $avatar_folder_dir  =  BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/event-avatars/' . $event->id . '/';
                if (file_exists($avatar_folder_dir)) {
                    $av_dir             = opendir( $avatar_folder_dir );
                    $avatarExistBpthumb = readdir($av_dir);
                    $avatarExistFull    = str_replace('bpthumb', 'bpfull', $avatarExistBpthumb);
                }
                
                ?>
                
                <div class="inputs" style="margin-left: 15px;">
                    <input type="hidden" id="userAvatar" name="userAvatar" value="<?php echo isset($avatarExistFull) ? $avatarExistFull : ""; ?>" />
                    <input type="hidden" id="userAvatarMime" name="userAvatarMime" value="<?php echo isset($_POST["userAvatarMime"]) ? $_POST["userAvatarMime"] : ""; ?>" />
                    <input type="hidden" id="userAvatarSize" name="userAvatarSize" value="<?php echo isset($_POST["userAvatarSize"]) ? $_POST["userAvatarSize"] : ""; ?>" />
                    <input type="submit" name="SubirImagen" value="<?php _e('Subir Imagen', 'buddypress')?>" />
                    
                    <p style="margin: 8px;">
                        <?php _e('Si quieres eliminar el avatar existente, pero no quieres subir uno nuevo, por favor, utiliza el botón de eliminar avatar.', 'buddypress')?>
                    </p>
                    
                    <input type="submit" name="deleteImagen" value="<?php _e('Borrar imagen actual', 'buddypress')?>" />
                </div>
            </form>
            <?php
            if (isset($_POST['SubirImagen'])) {
                ?>
                <form action="<?php echo bp_get_root_domain() . '/eventos/?view=' . $event->slug . '&Admin=sendInvites';?>" style="display: block; float: right;padding-top: 15px;" method="GET">
                    <input type="hidden" name="view" value="<?php echo $event->slug; ?>" />
                    <input type="hidden" name="Admin" value="sendInvites" />
                    <input type="submit" name="SubirImagen" value="<?php _e('Enviar Invitaciones', 'buddypress')?>" />
                </form>
                <?php
            }
            ?>
        </div><!-- #form-event-avatar -->

        </div><!-- #item-body -->

    </div><!-- content-middle-add -->
    
    </div><!-- #content-middle -->
    
</div><!-- .padder -->