Ejemplo n.º 1
0
 public function addProduct()
 {
     $input = JRequest::get('post');
     $db = JFactory::getDBO();
     $app = JFactory::getApplication();
     if ($this->context == null) {
         $this->context = JeproshopContext::getContext();
     }
     $input_data = $input['jform'];
     $this->date_add = date('Y-m-d H:i:s');
     $this->date_upd = date('Y-m-d H:i:s');
     $shop_list_ids = array();
     if (JeproshopShopModelShop::isTableAssociated('product')) {
         $shop_list_ids = JeproshopShopModelShop::getContextListShopIds();
         if (count($this->shop_list_id) > 0) {
             $shop_list_ids = $this->shop_list_id;
         }
     }
     if (JeproshopShopModelShop::checkDefaultShopId('product')) {
         $this->default_shop_id = min($shop_list_ids);
     }
     $languages = JeproshopLanguageModelLanguage::getLanguages(false);
     $reference = JeproshopTools::isReference($input_data['reference']) ? $input_data['reference'] : '';
     $ean13 = JeproshopTools::isEan13($input_data['ean13']) ? $input_data['ean13'] : '';
     $upc = JeproshopTools::isUpc($input_data['upc']) ? $input_data['upc'] : '';
     $product_redirect_id = JeproshopTools::isUnsignedInt($input_data['product_redirected_id']) ? (int) $input_data['product_redirected_id'] : 0;
     $available_for_order = isset($input_data['available_for_order']) ? 1 : 0;
     $show_price = isset($input_data['show_price']) ? 1 : 0;
     $online_only = isset($input_data['online_only']) ? 1 : 0;
     $query = "INSERT INTO " . $db->quoteName('#__jeproshop_product') . "(" . $db->quoteName('reference') . ", " . $db->quoteName('ean13') . ", " . $db->quoteName('upc') . ", " . $db->quoteName('published') . ", ";
     $query .= $db->quoteName('redirect_type') . ", " . $db->quoteName('visibility') . ", " . $db->quoteName('condition') . ", " . $db->quoteName('available_for_order') . ", " . $db->quoteName('show_price') . ", ";
     $query .= $db->quoteName('online_only') . ", " . $db->quoteName('default_shop_id') . ", " . $db->quoteName('product_redirected_id') . ", " . $db->quoteName('date_add') . ", " . $db->quoteName('date_upd') . " ) VALUES(";
     $query .= $db->quote($reference, true) . ", " . $db->quote($ean13, true) . ", " . $db->quote($upc, true) . ", " . $db->quote($input_data['published'], true) . ", " . $db->quote($input_data['redirect_type'], true) . ", ";
     $query .= $db->quote($input_data['visibility'], true) . ", " . $db->quote($input_data['condition']) . ", " . (int) $available_for_order . ", " . (int) $show_price . ", " . (int) $online_only . ", " . $this->default_shop_id . ", ";
     $query .= $product_redirect_id . ", " . $db->quote($this->date_add) . ", " . $db->quote($this->date_upd) . ") ";
     $db->setQuery($query);
     if ($db->query()) {
         $product_id = $db->insertid();
         if (JeproshopShopModelShop::isTableAssociated('product')) {
             /* Shop fields */
             foreach ($shop_list_ids as $shop_id) {
                 $query = "INSERT INTO " . $db->quoteName('#__jeproshop_product_shop') . "( " . $db->quoteName('product_id') . ", ";
                 $query .= $db->quoteName('shop_id') . ", " . $db->quoteName('online_only') . ", " . $db->quoteName('published') . ", ";
                 $query .= $db->quoteName('redirect_type') . ", " . $db->quoteName('product_redirected_id') . ", " . $db->quoteName('available_for_order');
                 $query .= ", " . $db->quoteName('condition') . ", " . $db->quoteName('show_price') . ", " . $db->quoteName('visibility') . ", ";
                 $query .= $db->quoteName('date_add') . ", " . $db->quoteName('date_upd') . ") VALUES( " . (int) $product_id . ", " . (int) $shop_id . ", ";
                 $query .= (int) $online_only . ", " . (int) $input_data['published'] . ", " . $db->quote($input_data['redirect_type']) . ", ";
                 $query .= (int) $input_data['product_redirected_id'] . ", " . (int) $available_for_order . ", " . $db->quote($input_data['condition']) . ", ";
                 $query .= (int) $show_price . ", " . $db->quote($input_data['visibility']) . ", " . $db->quote($this->date_add) . ", " . $db->quote($this->date_upd) . ") ";
                 $db->setQuery($query);
                 if ($db->query()) {
                     /* Multilingual fields */
                     foreach ($languages as $language) {
                         $query = "INSERT INTO " . $db->quoteName('#__jeproshop_product_lang') . "(" . $db->quoteName('product_id') . ", " . $db->quoteName('shop_id') . ", " . $db->quoteName('lang_id') . ", " . $db->quoteName('description') . ", ";
                         $query .= $db->quoteName('short_description') . ", " . $db->quoteName('name') . ") VALUES (" . (int) $product_id . ", " . (int) $shop_id . ", " . (int) $language->lang_id . ", " . $db->quote($input_data['description_' . $language->lang_id]);
                         $query .= ", " . $db->quote($input_data['short_description_' . $language->lang_id]) . ", ";
                         $query .= $db->quote($input_data['name_' . $language->lang_id]) . ")";
                         $db->setQuery($query);
                         $db->query();
                     }
                 }
             }
         }
         $product = new JeproshopProductModelProduct($product_id);
         if ($product->getType() == JeproshopProductModelProduct::VIRTUAL_PRODUCT) {
             JeproshopStockAvailableModelStockAvailable::setProductDependsOnStock((int) $product->product_id, 1);
             if ($product->published && !JeproshopSettingModelSetting::getValue('virtual_product_feature_active')) {
                 JeproshopSettingModelSetting::updateValue('virtual_product_feature_active', '1');
             }
         } else {
             JeproshopStockAvailableModelStockAvailable::setProductOutOfStock((int) $product->product_id, 2);
         }
         $product->setGroupReduction();
         return $product;
     }
     return null;
 }