/**
 * Update product file
 *
 * @param array $product_file file data
 * @param int $file_id file ID for update, if empty - new file will be created
 * @param string $lang_code language code to update file description
 * @return boolean true on success, false - otherwise
 */
function fn_update_product_file($product_file, $file_id, $lang_code = DESCR_SL)
{
    if (!fn_company_products_check($product_file['product_id'], true)) {
        return false;
    }
    $uploaded_data = fn_filter_uploaded_data('base_file');
    $uploaded_preview_data = fn_filter_uploaded_data('file_preview');
    if (!empty($file_id) || !empty($uploaded_data[$file_id])) {
        db_query("UPDATE ?:products SET is_edp = 'Y' WHERE product_id = ?i", $product_file['product_id']);
        if (!empty($uploaded_data[$file_id])) {
            $product_file['file_name'] = empty($product_file['file_name']) ? $uploaded_data[$file_id]['name'] : $product_file['file_name'];
        }
        // Remove old file before uploading a new one
        if (!empty($file_id)) {
            $dir = $product_file['product_id'];
            $old_file = db_get_row('SELECT file_path, preview_path FROM ?:product_files WHERE product_id = ?i AND file_id = ?i', $product_file['product_id'], $file_id);
            if (!empty($uploaded_data) && !empty($old_file['file_path'])) {
                Storage::instance('downloads')->delete($dir . '/' . $old_file['file_path']);
            }
            if (!empty($uploaded_preview_data) && !empty($old_file['preview_path'])) {
                Storage::instance('downloads')->delete($dir . '/' . $old_file['preview_path']);
            }
        }
        // Update file data
        if (empty($file_id)) {
            $product_file['file_id'] = $file_id = db_query('INSERT INTO ?:product_files ?e', $product_file);
            foreach (fn_get_translation_languages() as $product_file['lang_code'] => $v) {
                db_query('INSERT INTO ?:product_file_descriptions ?e', $product_file);
            }
            $uploaded_id = 0;
        } else {
            db_query('UPDATE ?:product_files SET ?u WHERE file_id = ?i', $product_file, $file_id);
            db_query('UPDATE ?:product_file_descriptions SET ?u WHERE file_id = ?i AND lang_code = ?s', $product_file, $file_id, $lang_code);
            $uploaded_id = $file_id;
        }
        // Copy base file
        if (!empty($uploaded_data[$uploaded_id])) {
            fn_copy_product_files($file_id, $uploaded_data[$uploaded_id], $product_file['product_id']);
        }
        // Copy preview file
        if (!empty($uploaded_preview_data[$uploaded_id])) {
            fn_copy_product_files($file_id, $uploaded_preview_data[$uploaded_id], $product_file['product_id'], 'preview');
        }
    }
    return $file_id;
}
Beispiel #2
0
         $product_file['file_id'] = $file_id = db_query('INSERT INTO ?:product_files ?e', $product_file);
         foreach ((array) Registry::get('languages') as $product_file['lang_code'] => $v) {
             db_query('INSERT INTO ?:product_file_descriptions ?e', $product_file);
         }
     } else {
         db_query('UPDATE ?:product_files SET ?u WHERE file_id = ?i', $product_file, $_file_id);
         db_query('UPDATE ?:product_file_descriptions SET ?u WHERE file_id = ?i AND lang_code = ?s', $product_file, $_file_id, DESCR_SL);
         $file_id = $_file_id;
     }
     // Copy base file
     if (!empty($uploaded_data[$_file_id])) {
         fn_copy_product_files($file_id, $uploaded_data[$_file_id], $_REQUEST['product_id']);
     }
     // Copy preview file
     if (!empty($uploaded_preview_data[$_file_id])) {
         fn_copy_product_files($file_id, $uploaded_preview_data[$_file_id], $_REQUEST['product_id'], 'preview');
     }
     $suffix = ".update?product_id={$_REQUEST['product_id']}";
 }
 if ($mode == 'export_range') {
     if (!empty($_REQUEST['product_ids'])) {
         if (empty($_SESSION['export_ranges'])) {
             $_SESSION['export_ranges'] = array();
         }
         if (empty($_SESSION['export_ranges']['products'])) {
             $_SESSION['export_ranges']['products'] = array('pattern_id' => 'products');
         }
         $_SESSION['export_ranges']['products']['data'] = array('product_id' => $_REQUEST['product_ids']);
         unset($_REQUEST['redirect_url']);
         return array(CONTROLLER_STATUS_REDIRECT, "exim.export?section=products&pattern_id=" . $_SESSION['export_ranges']['products']['pattern_id']);
     }