<?php
global $wpdb, $bp;
/**
 * Template Name: Eventos-Create
 * Description: Eventos-Create
 **/

get_header();

locate_template( array( 'leftsidebar.php' ), true );
?>

<script type="text/javascript" src="wp-content/themes/cvp-irontec/events/custom/js/scripts.js"></script>

<div id="content">
    <div id="iron-encabezado-container">
        <h2 class="iron-encabezado-seccion"><?php echo __('Eventos','buddypress'); ?></h2>

        <ul class="iron-encabezado-ul">
            <?php if (xprofile_get_field_data('Privacidad', bp_loggedin_user_id()) != 'Privado') { ?>
                <li class="iron-encabezado-li"><a class="fancybox-groups" href="<?php echo bp_get_root_domain() . '/eventos-create/' ?>"><?php _e( 'Crear evento', 'buddypress' ) ?></a></li>
            <?php } ?>
            <li class="iron-encabezado-li">
            <?php
                if (xprofile_get_field_data('Privacidad', bp_loggedin_user_id()) != 'Privado') { ?>
                &middot;&nbsp;
            <?php } ?>
            <a href="<?php bp_root_domain() ?>/eventos/"><?php printf( __( 'Todos', 'buddypress' ) ) ?></a></li>
            <li class="iron-encabezado-li">&middot;&nbsp;<a href="<?php echo bp_loggedin_user_domain() . 'events/' ?>"><?php printf( __( 'Mis eventos', 'buddypress' )  ) ?></a></li>
            <li class="iron-encabezado-li">&middot;&nbsp;<a href="<?php echo bp_loggedin_user_domain() . 'events/invites/' ?>"><?php _e( 'Invitaciones', 'buddypress' ) ?></a></li>
        </ul>
    </div>

    <div class="padder" id="jes-padder">
    
        <div id="content-top"></div>

        <div id="content-middle">
        
            <link rel="stylesheet" href="wp-content/themes/cvp-irontec/_inc/css/jquery-ui.css" />
            <?php
            if (count($_POST) > 7) {
                
                $event_name = str_replace(' ', '-', $_POST['event-name']);
                
                $data = array(
                            'creator_id'          => wp_get_current_user()->ID,
                            'name'                => str_replace('\\', '', $_POST['event-name']),
                            'etype'               => $_POST['event-etype'],
                            'eventapproved'       => '1',
                            'slug'                => urlAmigables($_POST['event-name']),
                            'description'         => $_POST['event-desc'],
                            'placedaddress'       => $_POST['event-placedaddress'],
                            'placednote'          => $_POST['event-web'],
                            'edtsd'               => $_POST['event-edtsd'],
                            'edted'               => $_POST['event-edted'],
                            'edtsth'              => $_POST['event-edtsth'],
                            'edteth'              => $_POST['event-edteth'],
                            'edtstm'              => $_POST['event-edtstm'],
                            'edtetm'              => $_POST['event-edtetm'],
                            'edtsdunix'           => strtotime(str_replace('/', '-', $_POST['event-edtsd'])),
                            'edtedunix'           => strtotime(str_replace('/', '-', $_POST['event-edted'])),
                            'status'              => $_POST['event-status'],
                            'grouplink'           => $_POST['event-grouplink'],
                            'enablesocial'        => $_POST['1'],
                            'enable_forum'        => $_POST['0'],
                            'date_created'        => date('Y-m-d H-i-s'),
                            'notify_timed_enable' => ''
                            );
                            
                $wpdb->insert( 'wp_jet_events', $data );
                
                $idEventCreate = $wpdb->insert_id;
                
                $activity = array(
                                'user_id'           => wp_get_current_user()->ID,
                                'component'         => 'events',
                                'type'              => 'created_event',
                                'action'            => '<a href="' . wp_get_current_user()->user_url . '" title="' . wp_get_current_user()->display_name . '">' . wp_get_current_user()->display_name . '</a> created the event <a href="' . events_jes_check_slug($event_name) . '">' . $_POST['event-name'] . '</a>',
                                'primary_link'      => wp_get_current_user()->user_url,
                                'item_id'           => $idEventCreate,
                                'secondary_item_id' => '0',
                                'date_recorded'     => date('Y-m-d H-i-s')
                                );
                
                $wpdb->insert( 'wp_bp_activity' , $activity );
                
                $metadataLastActivity = array(
                                'event_id'   => $idEventCreate,
                                'meta_key'   => 'last_activity',
                                'meta_value' => date('Y-m-d H-i-s')
                                );
                $metadataIronEstado = array(
                                'event_id'   => $idEventCreate,
                                'meta_key'   => 'ironestado',
                                'meta_value' => xprofile_get_field_data( 'EstadoAnimo', wp_get_current_user()->ID)
                );
                $metadataMembers = array(
                                'event_id'   => $idEventCreate,
                                'meta_key'   => 'total_member_count',
                                'meta_value' => '1'
                );
                
                $wpdb->insert( 'wp_jet_events_eventmeta', $metadataLastActivity );
                $wpdb->insert( 'wp_jet_events_eventmeta', $metadataIronEstado );
                $wpdb->insert( 'wp_jet_events_eventmeta', $metadataMembers );
                
                if ( $idEventCreate ) {
                    $event = new JES_Events_Event( $idEventCreate );
                }
                
                if ( $event->id ) {
                    $member                = new JES_Events_Member;
                    $member->event_id      = $event->id;
                    $member->user_id       = $event->creator_id;
                    $member->is_admin      = 1;
                    $member->user_title    = __( 'Event Admin', 'buddypress' );
                    $member->is_confirmed  = 1;
                    $member->date_modified = gmdate( "Y-m-d H:i:s" );
                    $member->save();
                }
                ?>
                    <div class="item-body" id="custom-event-invites">
                    <?php
                    require_once(dirname(__FILE__) . '/events/custom/create/event-avatar.php');
                    ?>
                    </div><!-- .item-body #custom-event-invites -->
                <?php
            } elseif (isset($_GET['option']) and isset($_GET['id'])) {
                ?>
                    <div class="item-body" id="custom-event-invites">
                    <?php
                    require_once(dirname(__FILE__) . '/events/custom/create/event-avatar.php');
                    ?>
                    </div><!-- .item-body #custom-event-invites -->
                <?php
            }else {
            ?>
            <form action="<?php echo $bp->root_domain . '/eventos-create/?option=event-details' ?>" method="post" id="create-event-form" class="standard-form" enctype="multipart/form-data">
                <h3>
                    <?php _e( 'Create an Event', 'buddypress' ) ?>
                </h3>
            
                <div class="item-list-tabs no-ajax" id="event-create-tabs">
                    <ul>
                        <li class="current">
                            <span><?php _e('Detalles', 'buddypress'); ?></span>
                        </li>
                    </ul>
                </div>
                
                <div class="item-body" id="custom-event-details">
                
                    <div style="clear:left;"></div>
                    
                    <div style="display: none">
                        <div id="content-error" href="#content-error" style="margin: 15px; text-align: justify; font-size: 12px;">
                            <?php _e('uno o mas de los campos obligatorios esta vacio', 'buddypress'); ?>
                        </div>
                    </div>
                <?php
                require_once(dirname(__FILE__) . '/events/custom/create/event-details.php');
                ?>
                </div><!-- .item-body #custom-event-details -->
                <div class="item-body" id="custom-event-privacidad" style="display: none;">
                    <?php
                    require_once(dirname(__FILE__) . '/events/custom/create/event-privacity.php');
                    ?>
                </div><!-- .item-body #custom-event-privacidad -->
            </form>
            <?php }//end else
                if ($_POST['Terminar']){
                    $slugNewEvent = $wpdb->get_row('select slug from wp_jet_events where id = "' . $_POST['event'] . '"');
                    wp_redirect( home_url() . '/eventos/?view=' . $slugNewEvent->slug .'&Admin=avatar' );
                }
                ?>
        </div><!-- content-middle -->
        <div id="content-bottom"></div>
    </div><!-- .padder -->
</div><!-- #content -->

    <?php locate_template( array( 'sidebar.php' ), true ) ?>

<?php get_footer() ?>

<script>
(function($) {
    $( "#event-edtsd" ).datepicker({
        dayNames:      [ "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" ],
        dayNamesMin:   [ "Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa" ],
        monthNames:    [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Augosto", "Septimbre", "Octbre", "Novimbre", "Dicimbre" ],
        firstDay: 1,
        dateFormat: "dd/mm/yy"
    });
    $( "#event-edtsd" ).datepicker( "option", "showAnim", 'blind' );
    $( "#event-edted" ).datepicker({
        dayNames:      [ "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" ],
        dayNamesMin:   [ "Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa" ],
        monthNames:    [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Augosto", "Septimbre", "Octbre", "Novimbre", "Dicimbre" ],
        firstDay: 1,
        dateFormat: "dd/mm/yy"
    });
    $( "#event-edted" ).datepicker( "option", "showAnim", 'blind' );
})(jQuery);
</script>