Пример #1
0
function m_save_team($edit = 0)
{
    global $xoopsSecurity, $xoopsModuleConfig;
    $query = '';
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
        if ($k == 'XOOPS_TOKEN_REQUEST' || $k == 'action' || $k == 'sbt') {
            continue;
        }
        $query .= $query == '' ? "{$k}=" . urlencode($v) : "&{$k}=" . urlencode($v);
    }
    $action = $edit ? '?action=edit&id=' . $id : '?action=new&';
    if (!$xoopsSecurity->check()) {
        redirectMsg('teams.php?action=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $query, __('Session token expired!', 'match'), 1);
        die;
    }
    if ($name == '' || $category <= 0) {
        redirectMsg('teams.php?action=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $query, __('Please fill all required data!', 'match'), 1);
    }
    if ($edit) {
        //Verificamos que el trabajo sea válido
        if ($id <= 0) {
            redirectMsg('./teams.php', __('Team ID not valid!', 'match'), 1);
            die;
        }
        //Verificamos que el trabajo exista
        $team = new MCHTeam($id);
        if ($team->isNew()) {
            redirectMsg('./teams.php', __('Specified team does not exists!', 'match'), 1);
            die;
        }
    } else {
        $team = new MCHTeam();
    }
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    // Check if work exists already
    if ($edit) {
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("mch_teams") . " WHERE name='{$name}' and category='{$category}' and id_team<>'{$id}'";
    } else {
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("mch_teams") . " WHERE name='{$name}' and category='{$category}'";
    }
    list($num) = $db->fetchRow($db->query($sql));
    if ($num > 0) {
        redirectMsg("teams.php" . $action . $query, __('A team with same name already exists!', 'match'), 1);
        die;
    }
    //Genera $nameid Nombre identificador
    $found = false;
    $i = 0;
    if ($name != $team->getVar('name') || empty($nameid)) {
        do {
            $nameid = TextCleaner::sweetstring($name) . ($found ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('mch_teams') . " WHERE nameid = '{$nameid}'";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                $found = true;
                $i++;
            } else {
                $found = false;
            }
        } while ($found == true);
    }
    $team->setVar('name', $name);
    $team->setVar('nameid', $nameid);
    $team->setVar('info', $info);
    $team->setVar('category', $category);
    $team->setVar('active', $active);
    $team->setVar('created', $created);
    //Logo
    include_once RMCPATH . '/class/uploader.php';
    $folder = XOOPS_UPLOAD_PATH . '/teams';
    if ($edit) {
        $image = $team->getVar('logo');
        $filename = $team->getVar('logo');
    } else {
        $filename = '';
    }
    //Obtenemos el tamaño de la imagen
    $imgSize = $xoopsModuleConfig['logo_size'];
    $up = new RMFileUploader($folder, $xoopsModuleConfig['logo_file_size'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('logo')) {
        if (!$up->upload()) {
            redirectMsg('./teams.php' . $action . $query, $up->getErrors(), 1);
            die;
        }
        if ($edit && $team->getVar('logo') != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/teams/' . $team->getVar('logo'));
        }
        $filename = $up->getSavedFileName();
        $fullpath = $up->getSavedDestination();
        // Redimensionamos la imagen
        $redim = new RMImageResizer($fullpath, $fullpath);
        //Redimensionar
        $redim->resizeWidth($imgSize);
    }
    $team->setVar('logo', $filename);
    if (!$team->save()) {
        redirectMsg('./teams.php' . $action . $query, __('Errors ocurred while trying to update database!', 'match') . $team->errors(), 1);
        die;
    } else {
        redirectMsg('./teams.php?id=' . $team->id(), __('Team saved successfully!', 'match'), 0);
        die;
    }
}