예제 #1
0
/**
* @desc Almacena la información del trabajo en la base de datos
**/
function saveWorks($edit = 0)
{
    global $xoopsSecurity, $xoopsModuleConfig;
    $query = '';
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
        if ($k == 'XOOPS_TOKEN_REQUEST' || $k == 'op' || $k == 'sbt') {
            continue;
        }
        $query .= $query == '' ? "{$k}=" . urlencode($v) : "&{$k}=" . urlencode($v);
    }
    if (!$xoopsSecurity->check()) {
        redirectMsg('./works.php?op=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $query, __('Session token expired!', 'works'), 1);
        die;
    }
    if ($edit) {
        //Verificamos que el trabajo sea válido
        if ($id <= 0) {
            redirectMsg('./works.php?' . $query, __('Work ID not valid!', 'works'), 1);
            die;
        }
        //Verificamos que el trabajo exista
        $work = new PWWork($id);
        if ($work->isNew()) {
            redirectMsg('./works.php?' . $query, __('Specified work does not exists!', 'works'), 1);
            die;
        }
    } else {
        $work = new PWWork();
    }
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    // Check if work exists already
    if ($edit) {
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("pw_works") . " WHERE title='{$title}' and id_work<>'{$id}'";
    } else {
        $sql = "SELECT COUNT(*) FROM " . $db->prefix("pw_works") . " WHERE title='{$title}'";
    }
    list($num) = $db->fetchRow($db->query($sql));
    if ($num > 0) {
        redirectMsg("works.php?" . $query, __('A work with same name already exists!', 'works'), 1);
        die;
    }
    $work->setTitle($title);
    $work->set_title_id(TextCleaner::sweetstring($title));
    $work->setDescShort(substr(stripcslashes($short), 0, 255));
    $work->setDesc($desc);
    $work->setCategory($catego);
    $work->setClient($client);
    $work->setComment($comment);
    $work->setNameSite($site);
    $work->setUrl(formatURL($url));
    $work->setStart($start);
    $work->setPeriod($period);
    $work->setCost($cost);
    $work->setMark($mark);
    $work->setPublic($public);
    $work->setRating($rating);
    $work->isNew() ? $work->setCreated(time()) : '';
    $work->setVar('dohtml', isset($dohtml) ? 1 : 0);
    $work->setVar('doxcode', isset($doxcode) ? 1 : 0);
    $work->setVar('dobr', isset($dobr) ? 1 : 0);
    $work->setVar('dosmiley', isset($dosmiley) ? 1 : 0);
    $work->setVar('doimage', isset($doimage) ? 1 : 0);
    //Imagen
    include_once RMCPATH . '/class/uploader.php';
    $folder = XOOPS_UPLOAD_PATH . '/works';
    $folderths = XOOPS_UPLOAD_PATH . '/works/ths';
    if ($edit) {
        $image = $work->image();
        $filename = $work->image();
    } else {
        $filename = '';
    }
    //Obtenemos el tamaño de la imagen
    $thSize = $xoopsModuleConfig['image_main_ths'];
    $imgSize = $xoopsModuleConfig['image_main'];
    $up = new RMFileUploader($folder, $xoopsModuleConfig['size_image'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('image')) {
        if (!$up->upload()) {
            redirectMsg('./works.php?id=' . $id . '&op=' . ($edit ? 'edit' : 'new'), $up->getErrors(), 1);
            die;
        }
        if ($edit && $work->image() != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/works/' . $work->image());
            @unlink(XOOPS_UPLOAD_PATH . '/works/ths/' . $work->image());
        }
        $filename = $up->getSavedFileName();
        $fullpath = $up->getSavedDestination();
        // Redimensionamos la imagen
        $redim = new RMImageResizer($fullpath, $fullpath);
        switch ($xoopsModuleConfig['redim_image']) {
            case 0:
                //Recortar miniatura
                $redim->resizeWidth($imgSize[0]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeAndCrop($thSize[0], $thSize[1]);
                break;
            case 1:
                //Recortar imagen grande
                $redim->resizeWidthOrHeight($imgSize[0], $imgSize[1]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeWidth($thSize[0]);
                break;
            case 2:
                //Recortar ambas
                $redim->resizeAndCrop($imgSize[0], $imgSize[1]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeAndCrop($thSize[0], $thSize[1]);
                break;
            case 3:
                //Redimensionar
                $redim->resizeWidth($imgSize[0]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeWidth($thSize[0]);
                break;
        }
    }
    $work->setImage($filename);
    if (!$work->save()) {
        redirectMsg('./works.php?' . $query, __('Errors ocurred while trying to update database!', 'works') . $work->errors(), 1);
        die;
    } else {
        redirectMsg('./works.php?op=edit&id=' . $work->id(), __('Database updated successfully!', 'works'), 0);
        die;
    }
}