Esempio n. 1
0
/**
* @desc Almacena las imágenes en la base de datos
**/
function shop_save_image($edit = 0)
{
    global $xoopsModuleConfig, $xoopsSecurity;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    $ruta = "action=images&page={$page}&bname={$bname}&id={$id}";
    //Verificamos que el trabajo sea válido
    if ($id <= 0) {
        redirectMsg('products.php', __('You must specify a product ID!', 'shop'), 1);
        die;
    }
    if (!$xoopsSecurity->check()) {
        redirectMsg('products.php?' . $ruta, __('Session token expired!', 'shop'), 1);
        die;
    }
    //Verificamos que el trabajo exista
    $product = new ShopProduct($id);
    if ($product->isNew()) {
        redirectMsg('products.php', __('Specified product does not exists!', 'shop'), 1);
        die;
    }
    if ($edit) {
        //Verificamos que la imagen sea válida
        if ($idimg <= 0) {
            redirectMsg('products.php?' . $ruta, __('You must specify an image ID!', 'shop'), 1);
            die;
        }
        //Verificamos que la imagen exista
        $img = new ShopImage($idimg);
        if ($img->isNew()) {
            redirectMsg('products.php?' . $ruta, __('Specified image does not exists!', 'shop'), 1);
            die;
        }
    } else {
        $img = new ShopImage();
    }
    $img->setVar('title', $title);
    $img->setVar('description', $description);
    $img->setVar('product', $product->id());
    //Imagen
    include_once RMCPATH . '/class/uploader.php';
    $folder = XOOPS_UPLOAD_PATH . '/minishop';
    $folderths = XOOPS_UPLOAD_PATH . '/minishop/ths';
    if ($edit) {
        $image = $img->getVar('file');
        $filename = $img->getVar('file');
    } else {
        $filename = '';
    }
    //Obtenemos el tamaño de la imagen
    $thSize = explode("|", $xoopsModuleConfig['thssize']);
    $imgSize = explode("|", $xoopsModuleConfig['imgsize']);
    $up = new RMFileUploader($folder, $xoopsModuleConfig['maxsize'] * 1024, array('jpg', 'png', 'gif'));
    if ($up->fetchMedia('file')) {
        if (!$up->upload()) {
            redirectMsg('products.php?action=' . ($edit ? 'edit' : 'new') . '&' . $ruta, $up->getErrors(), 1);
            die;
        }
        if ($edit && $img->getVar('file') != '') {
            @unlink(XOOPS_UPLOAD_PATH . '/minishop/' . $img->getVar('file'));
            @unlink(XOOPS_UPLOAD_PATH . '/minishop/ths/' . $img->getVar('file'));
        }
        $filename = $up->getSavedFileName();
        $fullpath = $up->getSavedDestination();
        // Redimensionamos la imagen
        $redim = new RMImageResizer($fullpath, $fullpath);
        if ($xoopsModuleConfig['imgredim']) {
            $redim->resizeAndCrop($imgSize[0], $imgSize[1]);
        } else {
            $redim->resizeWidthOrHeight($imgSize[0], $imgSize[1]);
        }
        $redim->setTargetFile($folderths . "/{$filename}");
        if ($xoopsModuleConfig['thsredim']) {
            $redim->resizeAndCrop($thSize[0], $thSize[1]);
        } else {
            $redim->resizeWidthOrHeight($thSize[0], $thSize[1]);
        }
    }
    $img->setVar('file', $filename);
    RMEvents::get()->run_event('shop.save.image', $img);
    if (!$img->save()) {
        redirectMsg('products.php?action=images&' . $ruta, __('Errors ocurred while trying to save the image', 'shop') . '<br />' . $img->errors(), 1);
        die;
    } else {
        redirectMsg('products.php?' . $ruta, __('Database updated successfully!', 'shop'), 0);
        die;
    }
}