Example #1
0
function saveTeam($edit = 0)
{
    global $db, $mc, $xoopsSecurity;
    $nameid = '';
    $coachs = array();
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->validateToken()) {
        redirectMsg('teams.php' . ($edit ? "?op=edit&id={$id}" : "?op=new"), __('Identficador de sesión expiró', 'admin_team'), 1);
        break;
    }
    if ($edit) {
        $id = TCFunctions::post('id');
        if ($id <= 0) {
            die;
            redirectMsg('teams.php', __('Id no válido', 'admin_team'), 1);
            die;
        }
        $team = new TCTeam($id);
        if ($team->isNew()) {
            redirectMsg('teams.php', __('No existe el equipo especificado', 'admin_team'), 1);
            die;
        }
        $i = 0;
        do {
            $nameid = $nameid != '' && $i == 0 ? $nameid : $util->sweetstring($name) . ($i > 0 ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_teams") . " WHERE nameid='{$nameid}' AND id_team<>'" . $team->id() . "'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_teams") . " WHERE name='{$name}' AND cat={$cat} AND id_team<>'" . $team->id() . "'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('teams.php?op=edit&id=' . $coach->id(), __('Ya existe un equipo con el mismo nombre', 'admin_team'), 1);
            die;
        }
    } else {
        $team = new TCTeam();
        $i = 0;
        do {
            $nameid = TextCleaner::getInstance()->sweetstring($name) . ($i > 0 ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_teams") . " WHERE nameid='{$nameid}'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_teams") . " WHERE name='{$name}' AND cat='{$cat}'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('teams.php?op=new', __('Ya existe un equipo con el mismo nombre', 'admin_team'), 1);
            die;
        }
    }
    // Cargamos la imágen
    include_once RMCPATH . '/class/uploader.php';
    $up = new RMFileUploader(XOOPS_UPLOAD_PATH . '/teams', $mc['filesize'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('image')) {
        if (!$up->upload()) {
            if ($edit) {
                redirectMsg('./teams.php?op=new', $up->getErrors(), 1);
                die;
            } else {
                redirectMsg('./teams.php?op=edit&id=' . $team->id(), $up->getErrors(), 1);
                die;
            }
        }
        if ($edit && $team->image() != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/teams/' . $team->image());
            @unlink(XOOPS_UPLOAD_PATH . '/teams/ths/' . $team->image());
        }
        $filename = $up->getSavedFileName();
        $fullpath = $up->getSavedDestination();
        // Redimensionamos la imagen
        $redim = new RMImageResizer($fullpath, $fullpath);
        $redim->resizeWidth($mc['img_size']);
        $redim->setTargetFile(XOOPS_UPLOAD_PATH . "/teams/ths/{$filename}");
        $redim->resizeWidth($mc['th_size']);
    } else {
        $filename = $edit ? $team->image() : '';
    }
    $team->setCategory($cat);
    $team->setName($name);
    $team->setNameId($nameid);
    $team->setDesc($desc);
    $team->setImage($filename);
    if (!$edit) {
        $team->setCreated(time());
    }
    $team->setCoachs($coachs);
    $team->setVar('dohtml', isset($dohtml) ? 1 : 0);
    $team->setVar('doxcode', isset($doxcode) ? 1 : 0);
    $team->setVar('doimage', isset($doimage) ? 1 : 0);
    $team->setVar('dosmiley', isset($dosmiley) ? 1 : 0);
    $team->setVar('dobr', isset($dobr) ? 1 : 0);
    if ($team->save()) {
        redirectMsg('teams.php', __('Base de datos actualizada correctamente', 'admin_team'), 0);
    } else {
        redirectMsg('teams.php?op=' . ($edit ? "edit&id=" . $team->id() : "new"), __('No se pudo actualizar la base de datos', 'admin_team') . '<br />' . $team->errors(), 0);
    }
}