<?php

//Include the common file
require '../common.php';

//Check if the user is logged in
if (!$authentication->loggedIn() || !$authentication->isAdmin()) header("Location: login.php");

//Returns the number of rows
$rowCount = $db->rowCount("SELECT event_id FROM " . cfg('base', 'table_events') . " WHERE event_id = '" . $_GET['event_id'] . "'");

$event = $db->fetchRowAssoc("SELECT * FROM " . cfg('base', 'table_events') . " WHERE event_id = '" . $_GET['event_id'] . "'");

function event($language, $string)
{

 global $db;
 $event = $db->fetchRowAssoc("SELECT * FROM " . cfg('base', 'table_events') . " e, " . cfg('base', 'table_event_translations') . " t WHERE e.event_id = t.event_id AND t.language_code = '" . $language . "' AND e.event_id = '" . $_GET['event_id'] . "'");

 return $event[$string];
}

//Check if the form has been submitted
if (isset($_POST['submit'])) {

    foreach ($languages as $row) {
     $validate->required($_POST['event_title'.'_'.$row['language_code']], 'Enter event title ('.$row['language_code'].').');
    }

    if ($_FILES['image']['name']) {

        $result = $db->fetchRowAssoc("SELECT event_poster FROM " . cfg('base', 'table_events') . " WHERE event_id = '" . $_GET['event_id'] . "'");

        if ($result['event_poster']) {

            @unlink(cfg('upload', 'upload_path') . 'images/' . $result['event_poster']);

        }

        $image = $upload->uploadImage('image',url($_POST['event_title'.'_'.cfg('language','site_language')]), 698, 910, true, true);

    }

    if (!$error->hasErrors()) {

        $values = array(
            'event_guests'           => $_POST['event_guests'],
            'event_date'             => $_POST['event_date'],
            'event_location'         => $_POST['event_location'],
            'ticket_price'           => $_POST['ticket_price'],
            'event_status'           => $_POST['event_status']
        );

        if (isset($image)) {

            $values['event_poster'] = $image;

        }

        $where = array(
            'event_id' => $_GET['event_id']
        );

        $db->where($where);
        $db->update(cfg('base', 'table_events'), $values);

        $where = array(
            'event_id' => $_GET['event_id']
        );

        $db->where($where);
        $db->delete(cfg('base', 'table_category_events'));

        $values = array(
            'event_id'        => $_GET['event_id'],
            'category_id'    => $_POST['category_id']
        );

        $db->insert(cfg('base', 'table_category_events'), $values);

        foreach ($languages as $row) {
            $count = $db->rowCount("SELECT event_id FROM " . cfg('base', 'table_event_translations') . " WHERE language_code = '" . $row['language_code'] . "'");

            if ($count > 0) {
                $values = array(
                    'event_title'             => $_POST['event_title'.'_'.$row['language_code']],
                    'event_meta_keywords'    => $_POST['event_meta_keywords'.'_'.$row['language_code']],
                    'event_meta_description' => $_POST['event_meta_description'.'_'.$row['language_code']],
                    'reservation_details'     => $_POST['reservation_details'.'_'.$row['language_code']]
                );

                $where = array(
                    'event_id'       => $_GET['event_id'],
                    'language_code'    => $row['language_code']
                );

                $db->where($where);
                $db->update(cfg('base', 'table_event_translations'), $values);
            } else {
                $values = array(
                    'event_id'                 => $event_id,
                    'language_code'          => $row['language_code'],
                    'event_title'             => $_POST['event_title'.'_'.$row['language_code']],
                    'event_meta_keywords'    => $_POST['event_meta_keywords'.'_'.$row['language_code']],
                    'event_meta_description' => $_POST['event_meta_description'.'_'.$row['language_code']],
                    'reservation_details'     => $_POST['reservation_details'.'_'.$row['language_code']]
                );
                $db->insert(cfg('base', 'table_event_translations'), $values);
            }
        }

        $session->set('success', true);
        header("Location: edit_event.php?event_id=".$_GET['event_id']."");

    }

}

//Navigation class
$current_events = 'class="active"';

//Template values
$tpl->set('current_events', $current_events);
$tpl->set('rowCount', $rowCount);
$tpl->set('event', $event);

//Display the template
$tpl->display('admin/edit_event');
