コード例 #1
0
ファイル: players.php プロジェクト: laiello/bitcero-modules
function savePlayer($edit = 0)
{
    global $db, $mc, $xoopsSecurity;
    $nameid = '';
    $teams = array();
    $idteam = 0;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->validateToken()) {
        redirectMsg('players.php?team=' . $idteam . ($edit ? "&op=edit&id={$id}" : "&op=new"), __('El identificador se sesión ha expirado', 'admin_team'), 1);
        break;
    }
    $idteam = TCFunctions::request('team');
    if ($idteam <= 0) {
        redirectMsg('teams.php', __('No se ha especificado un equipo', 'admin_team'), 1);
        die;
    }
    $team = new TCTeam($idteam);
    if ($team->isNew()) {
        redirectMsg('teams.php', __('El equipo especificado no existe', 'admin_team'), 1);
        die;
    }
    if ($edit) {
        $id = TCFunctions::post('id');
        if ($id <= 0) {
            redirectMsg('players.php?team=' . $idteam, __('No se ha especificado un jugador para editar', 'admin_team'), 1);
            die;
        }
        $player = new TCPLayer($id);
        if ($player->isNew()) {
            redirectMsg('players.php?team=' . $idteam, __('El jugador especificado no existe', '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_players") . " WHERE nameid='{$nameid}' AND id_play<>'" . $player->id() . "'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_players") . " WHERE name='{$name}' AND id_play<>'" . $player->id() . "'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('players.php?team=' . $idteam . '&op=edit&id=' . $player->id(), __('Ya existe otro jugador con el mismo nombre', 'admin_team'), 1);
            die;
        }
    } else {
        $player = new TCPlayer();
        $i = 0;
        do {
            $nameid = TextCleaner::getInstance()->sweetstring($name) . ($i > 0 ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_players") . " WHERE nameid='{$nameid}'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_players") . " WHERE name='{$name}'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('players.php?team=' . $idteam . '&op=new&id=' . $cat->id(), __('Ya existe otro jugador 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/players', $mc['filesize'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('image')) {
        if (!$up->upload()) {
            if ($edit) {
                redirectMsg('./players.php?team=' . $idteam . '&op=edit&id=' . $player->id(), $up->getErrors(), 1);
                die;
            } else {
                redirectMsg('./players.php?team=' . $idteam . '&op=new', $up->getErrors(), 1);
                die;
            }
        }
        if ($edit && $player->image() != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/teams/players/' . $player->image());
            @unlink(XOOPS_UPLOAD_PATH . '/teams/players/ths/' . $player->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/players/ths/{$filename}");
        switch ($mc['resize_method']) {
            case 1:
                //Recortar miniatura
                $redim->resizeAndCrop($mc['th_size'], $mc['th_size']);
                break;
            case 0:
                $redim->resizeWidth($mc['th_size']);
                break;
        }
    } else {
        $filename = $edit ? $player->image() : '';
    }
    $player->setName($name);
    $player->setNameId($nameid);
    $player->setBirth($birth);
    $player->setBio($bio);
    if (!$edit) {
        $player->setDate(time());
    }
    $player->setTeam($idteam);
    $player->setNumber($number);
    $player->setImage($filename);
    $player->setVar('dohtml', isset($dohtml) ? 1 : 0);
    $player->setVar('doxcode', isset($doxcode) ? 1 : 0);
    $player->setVar('doimage', isset($doimage) ? 1 : 0);
    $player->setVar('dosmiley', isset($dosmiley) ? 1 : 0);
    $player->setVar('dobr', isset($dobr) ? 1 : 0);
    if ($player->save()) {
        redirectMsg('players.php?team=' . $idteam, __('Base de datos actualizada correctamente', 'admin_team'), 0);
    } else {
        redirectMsg('players.php?team=' . $idteam . '&op=' . ($edit ? 'edit&id=' . $player->id() : 'new'), __('Ocurrieron errores al intentar actualizar la base de datos', 'admin_team') . '<br />' . $player->errors(), 0);
    }
}