<?php

/*
This file is part of CMS Made Simple module: Tourney.
Copyright(C) 2014-2015 Tom Phane <*****@*****.**>
Refer to licence and other details at the top of file Tourney.module.php
More info at http://dev.cmsmadesimple.org/projects/tourney
*/
if (!$this->CheckAccess('adview')) {
    $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $this->PrettyMessage('lackpermission', FALSE)));
}
$funcs = new tmtXML();
$res = $funcs->ExportXML($this, $params['bracket_id']);
if ($res === TRUE) {
    exit;
} else {
    $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $this->PrettyMessage($res, FALSE)));
}
/*
This file is part of CMS Made Simple module: Tourney.
Copyright (C) 2014-2015 Tom Phane <*****@*****.**>
Refer to licence and other details at the top of file Tourney.module.php
More info at http://dev.cmsmadesimple.org/projects/tourney
*/
if (!$this->CheckAccess('admod')) {
    $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $this->PrettyMessage('lackpermission', FALSE)));
}
$file_data = $_FILES[$id . 'xmlfile'];
if ($file_data['type'] != 'text/xml' || $file_data['size'] <= 0 || $file_data['error'] != 0) {
    $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $this->PrettyMessage('err_import_failed', FALSE)));
}
$success = FALSE;
$funcs = new tmtXML();
$bracketdata = $funcs->ImportXML($file_data['tmp_name']);
if ($bracketdata && !empty($bracketdata['count'])) {
    //TODO convert imported data from UTF-8 to site-encoding, if needed
    //$bracketdata['version'] = string e.g. '0.1[.2]' CHECKME use this ?
    //$bracketdata['date'] = string CHECKME use this ?
    $pref = cms_db_prefix();
    for ($i = 1; $i <= $bracketdata['count']; $i++) {
        $fields = array_keys($bracketdata['bracket' . $i]['properties']);
        $values = array_values($bracketdata['bracket' . $i]['properties']);
        //don't 'force' NULL for fields which default to something else
        foreach ($values as $indx => $val) {
            if ($val === NULL) {
                unset($values[$indx]);
                unset($fields[$indx]);
            }
        if ($content) {
            @ob_clean();
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
            header('Cache-Control: private', FALSE);
            header('Content-Description: File Transfer');
            header('Content-Type: ' . $ft);
            header('Content-Length: ' . strlen($content));
            header('Content-Disposition: attachment; filename="' . $fn . '"');
            echo $content;
            if (!$message) {
                exit;
            }
            $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $message));
        }
    }
} elseif (isset($params['export'])) {
    $vals = array_flip($params['selitems']);
    //convert strings
    $vals = array_flip($vals);
    if ($vals) {
        $funcs = new tmtXML();
        $res = $funcs->ExportXML($this, $params['selitems']);
        if ($res === TRUE) {
            exit;
        }
        $this->Redirect($id, 'defaultadmin', '', array('tmt_message' => $this->PrettyMessage($res, FALSE)));
    }
}
$this->Redirect($id, 'defaultadmin');