コード例 #1
0
ファイル: coachs.php プロジェクト: laiello/bitcero-modules
function saveCoach($edit = 0)
{
    global $db, $mc, $xoopsSecurity;
    $nameid = '';
    $teams = array();
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->check()) {
        redirectMsg('coachs.php' . ($edit ? "?op=edit&id={$id}" : "?op=new"), __('El identificador de sesión ha expirado', 'admin_team'), 1);
        break;
    }
    if ($edit) {
        $id = TCFunctions::post('id');
        if ($id <= 0) {
            redirectMsg('coachs.php', __('Id no válido', 'admin_team'), 1);
            die;
        }
        $coach = new TCCoach($id);
        if ($coach->isNew()) {
            redirectMsg('coachs.php', __('El entrenador 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_coachs") . " WHERE nameid='{$nameid}' AND id_coach<>'" . $coach->id() . "'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_coachs") . " WHERE name='{$name}' AND id_coach<>'" . $coach->id() . "'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('coachs.php?op=edit&id=' . $coach->id(), __('Ya existe un entrenador con ese nombre', 'admin_team'), 1);
            die;
        }
    } else {
        $coach = new TCCoach();
        $i = 0;
        do {
            $nameid = TextCleaner::getInstance()->sweetstring($name) . ($i > 0 ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_coachs") . " WHERE nameid='{$nameid}'";
            list($num) = $db->fetchRow($db->query($sql));
            $i++;
        } while ($num > 0);
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("coach_coachs") . " WHERE name='{$name}'";
        list($num) = $db->fetchRow($db->query($sql));
        if ($num > 0) {
            redirectMsg('coachs.php?op=new&id=' . $cat->id(), __('Ya existe un entreandor 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/coachs', $mc['filesize'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('image')) {
        if (!$up->upload()) {
            if ($edit) {
                redirectMsg('./coachs.php?op=new', $up->getErrors(), 1);
                die;
            } else {
                redirectMsg('./coachs.php?op=edit&id=' . $coach->id(), $up->getErrors(), 1);
                die;
            }
        }
        if ($edit && $coach->image() != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/teams/coachs/' . $coach->image());
            @unlink(XOOPS_UPLOAD_PATH . '/teams/coachs/ths/' . $coach->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/coachs/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 ? $coach->image() : '';
    }
    $coach->setName($name);
    $coach->setNameId($nameid);
    $coach->setBio($bio);
    $coach->setImage($filename);
    $coach->setRole($role);
    $coach->setTeams($teams);
    if (!$edit) {
        $coach->setCreated(time());
    }
    if ($coach->save()) {
        redirectMsg('coachs.php', __('Base de datos actualizada correctamente', 'admin_template'), 0);
    } else {
        redirectMsg('coachs.php?op=' . ($edit ? 'edit&id=' . $coach->id() : 'new'), __('Error al actualizar la base de datos', 'admin_template') . '<br />' . $coach->errors());
    }
}