/** * Stores the posted Product, if any * @return boolean True on success, null on noop, false otherwise */ static function store_product() { global $_ARRAYLANG; if (!isset($_POST['bstore'])) { return null; } $product_name = contrexx_input2raw($_POST['product_name']); $product_code = contrexx_input2raw($_POST['product_code']); // Multiple Categories $category_id = isset($_POST['shopCategoriesAssigned']) ? contrexx_input2raw(join(',', $_POST['shopCategoriesAssigned'])) : ''; $customer_price = $_POST['customer_price']; $reseller_price = $_POST['reseller_price']; $discount_active = !empty($_POST['discount_active']); $discount_price = $_POST['discount_price']; //DBG::log("ShopManager::store_product(): customer_price $customer_price, reseller_price $reseller_price, discount_price $discount_price"); $vat_id = $_POST['vat_id']; $short = contrexx_input2raw($_POST['short']); $long = contrexx_input2raw($_POST['long']); $stock = $_POST['stock']; $stock_visible = !empty($_POST['stock_visible']); $uri = contrexx_input2raw($_POST['uri']); $active = !empty($_POST['articleActive']); $b2b = !empty($_POST['B2B']); $b2c = !empty($_POST['B2C']); $date_start = contrexx_input2raw($_POST['date_start']); $date_end = contrexx_input2raw($_POST['date_end']); $manufacturer_id = isset($_POST['manufacturer_id']) ? contrexx_input2int($_POST['manufacturer_id']) : 0; $minimum_order_quantity = $_POST['minimum_order_quantity']; // Currently not used on the detail page // $flags = (isset($_POST['Flags']) // ? join(' ', $_POST['Flags']) : ''); $distribution = $_POST['distribution']; // Different meaning of the "weight" field for downloads! // The getWeight() method will treat purely numeric values // like the validity period (in days) the same as a weight // without its unit and simply return its integer value. $weight = $distribution == 'delivery' ? Weight::getWeight($_POST['weight']) : $_POST['accountValidity']; // Assigned frontend groups for protected downloads $usergroup_ids = isset($_POST['groupsAssigned']) ? implode(',', $_POST['groupsAssigned']) : ''; $discount_group_count_id = $_POST['discount_group_count_id']; $discount_group_article_id = $_POST['discount_group_article_id']; //DBG::log("ShopManager::store_product(): Set \$discount_group_article_id to $discount_group_article_id"); $keywords = contrexx_input2raw($_POST['keywords']); for ($i = 1; $i <= 3; ++$i) { // Images outside the above directory are copied to the shop image folder. // Note that the image paths below do not include the document root, but // are relative to it. $picture = contrexx_input2raw($_POST['productImage' . $i]); // Ignore the picture if it's the default image! // Storing it would be pointless. // Images outside the above directory are copied to the shop image folder. // Note that the image paths below do not include the document root, but // are relative to it. if ($picture == self::$defaultImage || !self::moveImage($picture)) { $picture = ''; } // Update the posted path (used below) $_POST['productImage' . $i] = $picture; } // add all to pictures DBstring $imageName = base64_encode($_POST['productImage1']) . '?' . base64_encode($_POST['productImage1_width']) . '?' . base64_encode($_POST['productImage1_height']) . ':' . base64_encode($_POST['productImage2']) . '?' . base64_encode($_POST['productImage2_width']) . '?' . base64_encode($_POST['productImage2_height']) . ':' . base64_encode($_POST['productImage3']) . '?' . base64_encode($_POST['productImage3_width']) . '?' . base64_encode($_POST['productImage3_height']); // Note that the flags of the Product *MUST NOT* be changed // when inserting or updating the Product data, as the original // flags are needed for their own update later. $objProduct = null; $product_id = intval($_POST['id']); if ($product_id) { $objProduct = Product::getById($product_id); } $new = false; if (!$objProduct) { $new = true; $objProduct = new Product($product_code, $category_id, $product_name, $distribution, $customer_price, $active, 0, $weight); if (!$objProduct->store()) { return \Message::error($_ARRAYLANG['TXT_SHOP_PRODUCT_ERROR_STORING']); } // $product_id = $objProduct->id(); } // Apply the changes to all Products with the same Product code. // Note: This is disabled for the time being, as virtual categories are, too. // if ($product_code != '') { // $arrProduct = Products::getByCustomId($product_code); // } else { // $arrProduct = array($objProduct); // } // if (!is_array($arrProduct)) return false; // foreach ($arrProduct as $objProduct) { // Update each product $objProduct->code($product_code); // NOTE: Only change the parent ShopCategory for a Product // that is in a real ShopCategory. $objProduct->category_id($category_id); $objProduct->name($product_name); $objProduct->distribution($distribution); $objProduct->price($customer_price); $objProduct->active($active); // On the overview only: $objProduct->ord(); $objProduct->weight($weight); $objProduct->resellerprice($reseller_price); $objProduct->discount_active($discount_active); $objProduct->discountprice($discount_price); $objProduct->vat_id($vat_id); $objProduct->short($short); $objProduct->long($long); $objProduct->stock($stock); $objProduct->minimum_order_quantity($minimum_order_quantity); $objProduct->stock_visible($stock_visible); $objProduct->uri($uri); $objProduct->b2b($b2b); $objProduct->b2c($b2c); $objProduct->date_start($date_start); $objProduct->date_end($date_end); $objProduct->manufacturer_id($manufacturer_id); $objProduct->pictures($imageName); // Currently not used on the detail page // $objProduct->flags($flags); $objProduct->usergroup_ids($usergroup_ids); $objProduct->group_id($discount_group_count_id); $objProduct->article_id($discount_group_article_id); $objProduct->keywords($keywords); //DBG::log("ShopManager::store_product(): Product: reseller_price ".$objProduct->resellerprice()); // Remove old Product Attributes. // They are re-added below. $objProduct->clearAttributes(); // Add current product attributes if (isset($_POST['options']) && is_array($_POST['options'])) { foreach ($_POST['options'] as $valueId => $nameId) { $order = intval($_POST['productOptionsSortId'][$nameId]); $objProduct->addAttribute(intval($valueId), $order); } } // Mind that this will always be an *update*, see the call to // store() above. if (!$objProduct->store()) { return \Message::error($_ARRAYLANG['TXT_SHOP_PRODUCT_ERROR_STORING']); } // } // Add/remove Categories and Products to/from // virtual ShopCategories. // Note that this *MUST* be called *AFTER* the Product is updated // or inserted. // Virtual categories are disabled for the time being // Products::changeFlagsByProductCode( // $product_code, $flags // ); $objImage = new \ImageManager(); $arrImages = Products::get_image_array_from_base64($imageName); // Create thumbnails if not available, or update them foreach ($arrImages as $arrImage) { if (!empty($arrImage['img']) && $arrImage['img'] != ShopLibrary::noPictureName) { if (!$objImage->_createThumbWhq(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopPath() . '/', \Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopWebPath() . '/', $arrImage['img'], \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_max_width', 'Shop'), \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_max_height', 'Shop'), \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_quality', 'Shop'))) { \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_COULD_NOT_CREATE_THUMBNAIL'], $arrImage['img'])); } } } \Message::ok($new ? $_ARRAYLANG['TXT_DATA_RECORD_ADDED_SUCCESSFUL'] : $_ARRAYLANG['TXT_DATA_RECORD_UPDATED_SUCCESSFUL']); switch ($_POST['afterStoreAction']) { case 'newEmpty': \Cx\Core\Csrf\Controller\Csrf::redirect('index.php?cmd=Shop' . MODULE_INDEX . '&act=products&tpl=manage'); case 'newTemplate': \Cx\Core\Csrf\Controller\Csrf::redirect('index.php?cmd=Shop' . MODULE_INDEX . '&act=products&tpl=manage&id=' . $objProduct->id() . '&new=1'); } \Cx\Core\Csrf\Controller\Csrf::redirect('index.php?cmd=Shop' . MODULE_INDEX . '&act=products'); // Never reached return true; }