Example #1
0
 /**
  * Select a Product by ID from the database.
  * @static
  * @param       integer     $id             The Product ID
  * @return      Product                     The Product object on success,
  *                                          false otherwise
  * @global      ADONewConnection
  * @author      Reto Kohli <*****@*****.**>
  */
 static function getById($id)
 {
     global $objDatabase;
     if (!$id) {
         return NULL;
     }
     $arrSql = \Text::getSqlSnippets('`product`.`id`', FRONTEND_LANG_ID, 'Shop', array('name' => self::TEXT_NAME, 'short' => self::TEXT_SHORT, 'long' => self::TEXT_LONG, 'keys' => self::TEXT_KEYS, 'code' => self::TEXT_CODE, 'uri' => self::TEXT_URI));
     $query = "\n            SELECT `product`.`id`, `product`.`category_id`,\n                   `product`.`ord`, `product`.`active`, `product`.`weight`,\n                   `product`.`picture`,\n                   `product`.`normalprice`, `product`.`resellerprice`,\n                   `product`.`discountprice`, `product`.`discount_active`,\n                   `product`.`stock`, `product`.`stock_visible`,\n                   `product`.`distribution`,\n                   `product`.`date_start`, `product`.`date_end`,\n                   `product`.`manufacturer_id`,\n                   `product`.`b2b`, `product`.`b2c`,\n                   `product`.`vat_id`,\n                   `product`.`flags`,\n                   `product`.`usergroup_ids`,\n                   `product`.`group_id`, `product`.`article_id`,\n                   `product`.`minimum_order_quantity`, " . $arrSql['field'] . "\n              FROM `" . DBPREFIX . "module_shop" . MODULE_INDEX . "_products` AS `product`" . $arrSql['join'] . "\n             WHERE `product`.`id`={$id}";
     $objResult = $objDatabase->Execute($query);
     if (!$objResult) {
         return self::errorHandler();
     }
     if ($objResult->RecordCount() != 1) {
         return false;
     }
     $id = $objResult->fields['id'];
     $strCode = $objResult->fields['code'];
     if ($strCode === null) {
         $strCode = \Text::getById($id, 'Shop', self::TEXT_CODE)->content();
     }
     $strName = $objResult->fields['name'];
     if ($strName === null) {
         $strName = \Text::getById($id, 'Shop', self::TEXT_NAME)->content();
     }
     $strShort = $objResult->fields['short'];
     if ($strShort === null) {
         $strShort = \Text::getById($id, 'Shop', self::TEXT_SHORT)->content();
     }
     $strLong = $objResult->fields['long'];
     if ($strLong === null) {
         $strLong = \Text::getById($id, 'Shop', self::TEXT_LONG)->content();
     }
     $strUri = $objResult->fields['uri'];
     if ($strUri === null) {
         $strUri = \Text::getById($id, 'Shop', self::TEXT_URI)->content();
     }
     $strKeys = $objResult->fields['keys'];
     if ($strKeys === null) {
         $strKeys = \Text::getById($id, 'Shop', self::TEXT_KEYS)->content();
     }
     $objProduct = new Product($strCode, $objResult->fields['category_id'], $strName, $objResult->fields['distribution'], $objResult->fields['normalprice'], $objResult->fields['active'], $objResult->fields['ord'], $objResult->fields['weight'], $objResult->fields['id']);
     $objProduct->pictures = $objResult->fields['picture'];
     $objProduct->resellerprice = floatval($objResult->fields['resellerprice']);
     $objProduct->short = $strShort;
     $objProduct->long = $strLong;
     $objProduct->stock($objResult->fields['stock']);
     $objProduct->stock_visible($objResult->fields['stock_visible']);
     $objProduct->discountprice = floatval($objResult->fields['discountprice']);
     $objProduct->discount_active($objResult->fields['discount_active']);
     $objProduct->b2b($objResult->fields['b2b']);
     $objProduct->b2c($objResult->fields['b2c']);
     $objProduct->date_start($objResult->fields['date_start']);
     $objProduct->date_end($objResult->fields['date_end']);
     $objProduct->manufacturer_id = $objResult->fields['manufacturer_id'];
     $objProduct->uri = $strUri;
     $objProduct->vat_id = $objResult->fields['vat_id'];
     $objProduct->flags = $objResult->fields['flags'];
     $objProduct->usergroup_ids = $objResult->fields['usergroup_ids'];
     $objProduct->group_id = $objResult->fields['group_id'];
     $objProduct->article_id = $objResult->fields['article_id'];
     $objProduct->keywords = $strKeys;
     $objProduct->minimum_order_quantity = $objResult->fields['minimum_order_quantity'];
     // Fetch the Product Attribute relations
     $objProduct->arrRelations = Attributes::getRelationArray($objProduct->id);
     //die("dfhreh: ".$objProduct->category_id());
     return $objProduct;
 }
Example #2
0
 /**
  * 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;
 }