/** * Add or update a product * * @global string $currentIndex Current URL in order to keep current Tab */ public function submitAddproduct($token = NULL) { global $currentIndex; $className = 'Product'; $rules = call_user_func(array($this->className, 'getValidationRules'), $this->className); $defaultLanguage = new Language(intval(Configuration::get('PS_LANG_DEFAULT'))); $languages = Language::getLanguages(); /* Check required fields */ foreach ($rules['required'] as $field) { if (($value = Tools::getValue($field)) == false and $value != '0') { if (Tools::getValue('id_' . $this->table) and $field == 'passwd') { continue; } $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is required'); } } /* Check multilingual required fields */ foreach ($rules['requiredLang'] as $fieldLang) { if (!Tools::getValue($fieldLang . '_' . $defaultLanguage->id)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . '</b> ' . $this->l('is required at least in') . ' ' . $defaultLanguage->name; } } /* Check fields sizes */ foreach ($rules['size'] as $field => $maxLength) { if ($value = Tools::getValue($field) and Tools::strlen($value) > $maxLength) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is too long') . ' (' . $maxLength . ' ' . $this->l('chars max') . ')'; } } if (isset($_POST['description_short'])) { $saveShort = $_POST['description_short']; $_POST['description_short'] = strip_tags($_POST['description_short']); } /* Check description short size without html */ foreach ($languages as $language) { if ($value = Tools::getValue('description_short_' . $language['id_lang'])) { if (Tools::strlen(strip_tags($value)) > 400) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), 'description_short') . ' (' . $language['name'] . ')</b> ' . $this->l('is too long') . ' : 400 ' . $this->l('chars max') . ' (' . $this->l('count now') . ' ' . Tools::strlen(strip_tags($value)) . ')'; } } } /* Check multilingual fields sizes */ foreach ($rules['sizeLang'] as $fieldLang => $maxLength) { foreach ($languages as $language) { if ($value = Tools::getValue($fieldLang . '_' . $language['id_lang']) and Tools::strlen($value) > $maxLength) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . ' (' . $language['name'] . ')</b> ' . $this->l('is too long') . ' (' . $maxLength . ' ' . $this->l('chars max') . ')'; } } } if (isset($_POST['description_short'])) { $_POST['description_short'] = $saveShort; } /* Check fields validity */ foreach ($rules['validate'] as $field => $function) { if ($value = Tools::getValue($field)) { if (!Validate::$function($value)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is invalid'); } } } /* Check multilingual fields validity */ foreach ($rules['validateLang'] as $fieldLang => $function) { foreach ($languages as $language) { if ($value = Tools::getValue($fieldLang . '_' . $language['id_lang'])) { if (!Validate::$function($value)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . ' (' . $language['name'] . ')</b> ' . $this->l('is invalid'); } } } } $productCats = ''; if (!isset($_POST['categoryBox']) or !sizeof($_POST['categoryBox'])) { $this->_errors[] = $this->l('product must be in at least one Category'); } foreach ($languages as $language) { if ($value = Tools::getValue('tags_' . $language['id_lang'])) { if (!Validate::isTagsList($value)) { $this->_errors[] = $this->l('Tags list') . ' (' . $language['name'] . ') ' . $this->l('is invalid'); } } } if (!sizeof($this->_errors)) { $id = intval(Tools::getValue('id_' . $this->table)); $tagError = true; /* Update an existing product */ if (isset($id) and !empty($id)) { $object = new $this->className($id); if (Validate::isLoadedObject($object)) { $this->copyFromPost($object, $this->table); if ($object->update()) { $this->updateAccessories($object); $this->updateDownloadProduct($object); if (!$this->updatePackItems($object)) { $this->_errors[] = Tools::displayError('an error occurred while adding products to the pack'); } elseif (!$object->updateCategories($_POST['categoryBox'], true)) { $this->_errors[] = Tools::displayError('an error occurred while linking object') . ' <b>' . $this->table . '</b> ' . Tools::displayError('to categories'); } elseif (!$this->updateTags($languages, $object)) { $this->_errors[] = Tools::displayError('an error occurred while adding tags'); } elseif ($id_image = $this->addProductImage($object, Tools::getValue('resizer'))) { Hook::updateProduct($object); Search::indexation(false); if (Tools::getValue('resizer') == 'man' && isset($id_image) and is_int($id_image) and $id_image) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&edit=' . strval(Tools::getValue('productCreated')) . '&id_image=' . $id_image . '&imageresize&toconf=4&submitAddAndStay=' . ((Tools::isSubmit('submitAdd' . $this->table . 'AndStay') or Tools::getValue('productCreated') == 'on') ? 'on' : 'off') . '&token=' . ($token ? $token : $this->token)); } // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay') or $id_image and $id_image !== true) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&addproduct&conf=4&tabs=' . intval(Tools::getValue('tabs')) . '&token=' . ($token ? $token : $this->token)); } // Default behavior (save and back) Tools::redirectAdmin($currentIndex . '&id_category=' . intval(Tools::getValue('id_category')) . '&conf=4&token=' . ($token ? $token : $this->token)); } } else { $this->_errors[] = Tools::displayError('an error occurred while updating object') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')'; } } else { $this->_errors[] = Tools::displayError('an error occurred while updating object') . ' <b>' . $this->table . '</b> (' . Tools::displayError('cannot load object') . ')'; } } else { $object = new $this->className(); $this->copyFromPost($object, $this->table); if ($object->add()) { $this->updateAccessories($object); if (!$this->updatePackItems($object)) { $this->_errors[] = Tools::displayError('an error occurred while adding products to the pack'); } $this->updateDownloadProduct($object); if (!sizeof($this->_errors)) { if (!$object->updateCategories($_POST['categoryBox'])) { $this->_errors[] = Tools::displayError('an error occurred while linking object') . ' <b>' . $this->table . '</b> ' . Tools::displayError('to categories'); } else { if (!$this->updateTags($languages, $object)) { $this->_errors[] = Tools::displayError('an error occurred while adding tags'); } elseif ($id_image = $this->addProductImage($object)) { Hook::addProduct($object); Search::indexation(false); if (Tools::getValue('resizer') == 'man' && isset($id_image) and is_int($id_image) and $id_image) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&id_image=' . $id_image . '&imageresize&toconf=3&submitAddAndStay=' . (Tools::isSubmit('submitAdd' . $this->table . 'AndStay') ? 'on' : 'off') . '&token=' . ($token ? $token : $this->token)); } // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&addproduct&conf=3&tabs=' . intval(Tools::getValue('tabs')) . '&token=' . ($token ? $token : $this->token)); } // Default behavior (save and back) Tools::redirectAdmin($currentIndex . '&id_category=' . intval(Tools::getValue('id_category')) . '&conf=3&token=' . ($token ? $token : $this->token)); } } } else { $object->delete(); } } else { $this->_errors[] = Tools::displayError('an error occurred while creating object') . ' <b>' . $this->table . '</b>'; } } } }
/** * Add or update a product * * @global string $currentIndex Current URL in order to keep current Tab */ public function submitAddProduct($token = NULL) { global $cookie, $currentIndex, $link; $className = 'Product'; $rules = call_user_func(array($this->className, 'getValidationRules'), $this->className); $defaultLanguage = new Language((int) Configuration::get('PS_LANG_DEFAULT')); $languages = Language::getLanguages(false); /* Check required fields */ foreach ($rules['required'] as $field) { if (($value = Tools::getValue($field)) == false and $value != '0') { if (Tools::getValue('id_' . $this->table) and $field == 'passwd') { continue; } $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is required'); } } /* Check multilingual required fields */ foreach ($rules['requiredLang'] as $fieldLang) { if (!Tools::getValue($fieldLang . '_' . $defaultLanguage->id)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . '</b> ' . $this->l('is required at least in') . ' ' . $defaultLanguage->name; } } /* Check fields sizes */ foreach ($rules['size'] as $field => $maxLength) { if ($value = Tools::getValue($field) and Tools::strlen($value) > $maxLength) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is too long') . ' (' . $maxLength . ' ' . $this->l('chars max') . ')'; } } if (isset($_POST['description_short'])) { $saveShort = $_POST['description_short']; $_POST['description_short'] = strip_tags($_POST['description_short']); } /* Check description short size without html */ $limit = (int) Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT'); if ($limit <= 0) { $limit = 400; } foreach ($languages as $language) { if ($value = Tools::getValue('description_short_' . $language['id_lang'])) { if (Tools::strlen(strip_tags($value)) > $limit) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), 'description_short') . ' (' . $language['name'] . ')</b> ' . $this->l('is too long') . ' : ' . $limit . ' ' . $this->l('chars max') . ' (' . $this->l('count now') . ' ' . Tools::strlen(strip_tags($value)) . ')'; } } } /* Check multilingual fields sizes */ foreach ($rules['sizeLang'] as $fieldLang => $maxLength) { foreach ($languages as $language) { if ($value = Tools::getValue($fieldLang . '_' . $language['id_lang']) and Tools::strlen($value) > $maxLength) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . ' (' . $language['name'] . ')</b> ' . $this->l('is too long') . ' (' . $maxLength . ' ' . $this->l('chars max') . ')'; } } } if (isset($_POST['description_short'])) { $_POST['description_short'] = $saveShort; } /* Check fields validity */ foreach ($rules['validate'] as $field => $function) { if ($value = Tools::getValue($field)) { if (!Validate::$function($value)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $field, $className) . '</b> ' . $this->l('is invalid'); } } } /* Check multilingual fields validity */ foreach ($rules['validateLang'] as $fieldLang => $function) { foreach ($languages as $language) { if ($value = Tools::getValue($fieldLang . '_' . $language['id_lang'])) { if (!Validate::$function($value)) { $this->_errors[] = $this->l('the field') . ' <b>' . call_user_func(array($className, 'displayFieldName'), $fieldLang, $className) . ' (' . $language['name'] . ')</b> ' . $this->l('is invalid'); } } } } /* Categories */ $productCats = ''; if (!Tools::isSubmit('categoryBox') or !sizeof(Tools::getValue('categoryBox'))) { $this->_errors[] = $this->l('product must be in at least one Category'); } if (!is_array(Tools::getValue('categoryBox')) or !in_array(Tools::getValue('id_category_default'), Tools::getValue('categoryBox'))) { $this->_errors[] = $this->l('product must be in the default category'); } /* Tags */ foreach ($languages as $language) { if ($value = Tools::getValue('tags_' . $language['id_lang'])) { if (!Validate::isTagsList($value)) { $this->_errors[] = $this->l('Tags list') . ' (' . $language['name'] . ') ' . $this->l('is invalid'); } } } if (!sizeof($this->_errors)) { $id = (int) Tools::getValue('id_' . $this->table); $tagError = true; /* Update an existing product */ if (isset($id) and !empty($id)) { $object = new $this->className($id); if (Validate::isLoadedObject($object)) { $this->_removeTaxFromEcotax(); $this->copyFromPost($object, $this->table); if ($object->update()) { if ($id_reason = (int) Tools::getValue('id_mvt_reason') and (int) Tools::getValue('mvt_quantity') > 0 and $id_reason > 0) { $reason = new StockMvtReason((int) $id_reason); $qty = Tools::getValue('mvt_quantity') * $reason->sign; if (!$object->addStockMvt($qty, (int) $id_reason, NULL, NULL, (int) $cookie->id_employee)) { $this->_errors[] = Tools::displayError('An error occurred while updating qty.'); } } $this->updateAccessories($object); $this->updateDownloadProduct($object); if (!$this->updatePackItems($object)) { $this->_errors[] = Tools::displayError('An error occurred while adding products to the pack.'); } elseif (!$object->updateCategories($_POST['categoryBox'], true)) { $this->_errors[] = Tools::displayError('An error occurred while linking object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('To categories'); } elseif (!$this->updateTags($languages, $object)) { $this->_errors[] = Tools::displayError('An error occurred while adding tags.'); } elseif ($id_image = $this->addProductImage($object, Tools::getValue('resizer'))) { $currentIndex .= '&image_updated=' . (int) Tools::getValue('id_image'); Hook::updateProduct($object); Search::indexation(false, $object->id); if (Tools::getValue('resizer') == 'man' && isset($id_image) and is_int($id_image) and $id_image) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&edit=' . strval(Tools::getValue('productCreated')) . '&id_image=' . $id_image . '&imageresize&toconf=4&submitAddAndStay=' . ((Tools::isSubmit('submitAdd' . $this->table . 'AndStay') or Tools::getValue('productCreated') == 'on') ? 'on' : 'off') . '&token=' . ($token ? $token : $this->token)); } // Save and preview if (Tools::isSubmit('submitAddProductAndPreview')) { $preview_url = $link->getProductLink($this->getFieldValue($object, 'id'), $this->getFieldValue($object, 'link_rewrite', (int) $cookie->id_lang), Category::getLinkRewrite($this->getFieldValue($object, 'id_category_default'), (int) $cookie->id_lang)); if (!$object->active) { $admin_dir = dirname($_SERVER['PHP_SELF']); $admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1); $token = Tools::encrypt('PreviewProduct' . $object->id); if (strpos($preview_url, '?') === false) { $preview_url .= '?'; } else { $preview_url .= '&'; } $preview_url .= 'adtoken=' . $token . '&ad=' . $admin_dir; } Tools::redirectAdmin($preview_url); } elseif (Tools::isSubmit('submitAdd' . $this->table . 'AndStay') or $id_image and $id_image !== true) { // Save and stay on same form // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&addproduct&conf=4&tabs=' . (int) Tools::getValue('tabs') . '&token=' . ($token ? $token : $this->token)); } } // Default behavior (save and back) Tools::redirectAdmin($currentIndex . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&conf=4&token=' . ($token ? $token : $this->token) . '&onredirigeici'); } } else { $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')'; } } else { $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> (' . Tools::displayError('Cannot load object') . ')'; } } else { $object = new $this->className(); $this->_removeTaxFromEcotax(); $this->copyFromPost($object, $this->table); if ($object->add()) { $this->updateAccessories($object); if (!$this->updatePackItems($object)) { $this->_errors[] = Tools::displayError('An error occurred while adding products to the pack.'); } $this->updateDownloadProduct($object); if (!sizeof($this->_errors)) { if (!$object->updateCategories($_POST['categoryBox'])) { $this->_errors[] = Tools::displayError('An error occurred while linking object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('To categories'); } elseif (!$this->updateTags($languages, $object)) { $this->_errors[] = Tools::displayError('An error occurred while adding tags.'); } elseif ($id_image = $this->addProductImage($object)) { Hook::addProduct($object); Search::indexation(false, $object->id); // Save and preview if (Tools::isSubmit('submitAddProductAndPreview')) { $preview_url = $link->getProductLink($this->getFieldValue($object, 'id'), $this->getFieldValue($object, 'link_rewrite', (int) $cookie->id_lang), Category::getLinkRewrite($this->getFieldValue($object, 'id_category_default'), (int) $cookie->id_lang)); if (!$object->active) { $admin_dir = dirname($_SERVER['PHP_SELF']); $admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1); $token = Tools::encrypt('PreviewProduct' . $object->id); $preview_url .= '&adtoken=' . $token . '&ad=' . $admin_dir; } Tools::redirectAdmin($preview_url); } if (Tools::getValue('resizer') == 'man' && isset($id_image) and is_int($id_image) and $id_image) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&id_image=' . $id_image . '&imageresize&toconf=3&submitAddAndStay=' . (Tools::isSubmit('submitAdd' . $this->table . 'AndStay') ? 'on' : 'off') . '&token=' . ($token ? $token : $this->token)); } // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin($currentIndex . '&id_product=' . $object->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&addproduct&conf=3&tabs=' . (int) Tools::getValue('tabs') . '&token=' . ($token ? $token : $this->token)); } // Default behavior (save and back) Tools::redirectAdmin($currentIndex . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&conf=3&token=' . ($token ? $token : $this->token)); } } else { $object->delete(); } } else { $this->_errors[] = Tools::displayError('An error occurred while creating object.') . ' <b>' . $this->table . '</b>'; } } } }