<?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 mix_id FROM " . cfg('base', 'table_mixes') . " WHERE mix_id = '" . $_GET['mix_id'] . "'");

$mix = $db->fetchRowAssoc("SELECT * FROM " . cfg('base', 'table_mixes') . " WHERE mix_id = '" . $_GET['mix_id'] . "'");

function mix($language, $string)
{

 global $db;
 $mix = $db->fetchRowAssoc("SELECT * FROM " . cfg('base', 'table_mixes') . " s, " . cfg('base', 'table_mix_translations') . " t WHERE s.mix_id = t.mix_id AND t.language_code = '" . $language . "' AND s.mix_id = '" . $_GET['mix_id'] . "'");

 return $mix[$string];
}

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

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

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

        $result = $db->fetchRowAssoc("SELECT cover FROM " . cfg('base', 'table_mixes') . " WHERE mix_id = '" . $_GET['mix_id'] . "'");

        if ($result['cover']) {

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

        }

        $image = $upload->uploadImage('image',url($_POST['mix_title'.'_'.cfg('language','site_language')]), 197, 168, true, true);

    }

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

        $values = array(
            'author'               => $_POST['author'],
            'tracklist'               => $_POST['tracklist'],
            'download_url'         => $_POST['download_url'],
            'zippy_www'            => $_POST['zippy_www'],
            'zippy_file'           => $_POST['zippy_file'],
            'mix_status'           => $_POST['mix_status']
        );

        if (isset($image)) {

            $values['cover'] = $image;

        }

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

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

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

            if ($count > 0) {
                $values = array(
                    'mix_id'                 => $mix_id,
                    'language_code'          => $row['language_code'],
                    'mix_title'             => $_POST['mix_title'.'_'.$row['language_code']],
                    'mix_meta_keywords'      => $_POST['mix_meta_keywords'.'_'.$row['language_code']],
                    'mix_meta_description'   => $_POST['mix_meta_description'.'_'.$row['language_code']],
                    'mix_description'         => $_POST['mix_description'.'_'.$row['language_code']]
                );

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

                $db->where($where);
                $db->update(cfg('base', 'table_mix_translations'), $values);
            } else {
                $values = array(
                    'mix_id'                 => $mix_id,
                    'language_code'          => $row['language_code'],
                    'mix_title'             => $_POST['mix_title'.'_'.$row['language_code']],
                    'mix_meta_keywords'      => $_POST['mix_meta_keywords'.'_'.$row['language_code']],
                    'mix_meta_description'   => $_POST['mix_meta_description'.'_'.$row['language_code']],
                    'mix_description'         => $_POST['mix_description'.'_'.$row['language_code']]
                );
                $db->insert(cfg('base', 'table_mix_translations'), $values);
            }
        }

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

    }

}

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

//Template values
$tpl->set('current_mixes', $current_mixes);
$tpl->set('rowCount', $rowCount);
$tpl->set('mix', $mix);

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