Exemplo n.º 1
0
/**
* Form to create or edit products
*
* @param int Indicates if we are editing an existing product or creating new one
*/
function shop_new_product($edit = 0)
{
    global $xoopsModuleConfig, $xoopsModule, $xoopsConfig, $xoopsSecurity;
    if ($edit) {
        $id = rmc_server_var($_REQUEST, 'id', 0);
        if ($id <= 0) {
            redirectMsg('products.php', __('You must provide a valid product ID!', 'shop'), 1);
            die;
        }
        $product = new ShopProduct($id);
        if ($product->isNew()) {
            redirectMsg('products.php', __('Specified product does not exists!', 'shop'), 1);
            die;
        }
    }
    define('RMCSUBLOCATION', 'new_product');
    xoops_cp_location('<a href="./">' . $xoopsModule->name() . '</a> &raquo; ' . ($edit ? sprintf(__('Editing %s', 'shop'), $product->getVar('name')) : __('Create Product', 'shop')));
    xoops_cp_header();
    ShopFunctions::include_required_files();
    RMTemplate::get()->assign('xoops_pagetitle', $edit ? sprintf(__('Edit "%s"', 'shop'), $product->getVar('name')) : __('Create Product', 'shop'));
    RMTemplate::get()->add_style('admin.css', 'shop');
    RMTemplate::get()->add_local_script('dashboard.js', 'shop');
    $form = new RMForm($edit ? __('Edit Product', 'shop') : __('New Product', 'shop'), 'frmProduct', 'products.php');
    $form->setExtra('enctype="multipart/form-data"');
    $form->addElement(new RMFormText(__('Name', 'shop'), 'name', 50, 200, $edit ? $product->getVar('name') : ''), true);
    $form->addElement(new RMFormEditor(__('Description', 'shop'), 'description', '100%', '250px', $edit ? $product->getVar("description", 'e') : '', $xoopsModuleConfig['editor']), true);
    $form->addElement(new RMFormText(__('Price', 'shop'), 'price', 10, 100, $edit ? $product->getVar('price') : ''));
    $form->addElement(new RMFormText(__('Buy link', 'shop'), 'buy', 50, 255, $edit ? $product->getVar('buy') : 'http://'));
    $categories = array();
    ShopFunctions::categos_list($categories);
    if ($edit) {
        $pcats = $product->get_categos();
    } else {
        $pcats = array();
    }
    $cats = new RMFormCheck('');
    foreach ($categories as $c) {
        $cats->addOption(str_repeat("&#8212;", $c['indent']) . ' ' . $c['name'], 'cats[]', $c['id_cat'], in_array($c['id_cat'], $pcats) ? 1 : 0);
    }
    $form->addElement(new RMFormLabel(__('Categories', 'shop'), '<div class="cats_field">' . $cats->render() . '</div>'));
    $ele = new RMFormRadio(__('Type', 'shop'), 'type', 1);
    $ele->addOption(__('Normal', 'shop'), 0, 1);
    $ele->addOption(__('Digital', 'shop'), 1, 0);
    $form->addElement($ele);
    unset($ele);
    $form->addElement(new RMFormYesNo(__('Available', 'shop'), 'available', 1));
    $form->addElement(new RMFormFile(__('Default image', 'shop'), 'image'));
    if ($edit && $product->getVar('image') != '') {
        $form->addElement(new RMFormLabel(__('Current Image', 'shop'), '<img src="' . XOOPS_UPLOAD_URL . '/minishop/ths/' . $product->getVar('image') . '" />'));
    }
    $metas = $edit ? $product->get_meta() : array();
    ob_start();
    include RMTemplate::get()->get_template('admin/shop_products_form.php', 'module', 'shop');
    $metas_tpl = ob_get_clean();
    $form->addElement(new RMFormLabel(__('Custom Fields', 'shop'), $metas_tpl));
    $buts = new RMFormButtonGroup('');
    $buts->addButton('cancel', __('Cancel', 'shop'), 'button', 'onclick="window.location.href=\'products.php\'"');
    $buts->addButton('sbt', $edit ? __('Save Changes', 'shop') : __('Create Product', 'shop'), 'submit');
    $form->addElement($buts);
    $form->addElement(new RMFormHidden('action', $edit ? 'saveedit' : 'save'));
    if ($edit) {
        $form->addElement(new RMFormHidden('id', $product->id()));
    }
    $form->display();
    xoops_cp_footer();
}