/** * @since 1.5.0 * @see ObjectModel::delete() */ public function delete() { if (!parent::delete()) { return false; } Carrier::cleanPositions(); return Db::getInstance()->delete('cart_rule_carrier', 'id_carrier = ' . (int) $this->id) && Db::getInstance()->delete('module_carrier', 'id_reference = ' . (int) $this->id_reference) && $this->deleteTaxRulesGroup(Shop::getShops(true, null, true)); }
public function postProcess() { if (Tools::getValue('submitAdd' . $this->table)) { /* Checking fields validity */ $this->validateRules(); if (!count($this->errors)) { $id = (int) Tools::getValue('id_' . $this->table); /* Object update */ if (isset($id) && !empty($id)) { try { if ($this->tabAccess['edit'] === '1') { $current_carrier = new Carrier($id); if (!Validate::isLoadedObject($current_carrier)) { throw new PrestaShopException('Cannot load Carrier object'); } // Duplicate current Carrier $new_carrier = $current_carrier->duplicateObject(); if (Validate::isLoadedObject($new_carrier)) { // Set flag deteled to true for historization $current_carrier->deleted = true; $current_carrier->update(); // Fill the new carrier object $this->copyFromPost($new_carrier, $this->table); $new_carrier->position = $current_carrier->position; $new_carrier->update(); $this->updateAssoShop($new_carrier->id); $new_carrier->copyCarrierData((int) $current_carrier->id); $this->changeGroups($new_carrier->id); // Call of hooks Hook::exec('actionCarrierUpdate', array('id_carrier' => (int) $current_carrier->id, 'carrier' => $new_carrier)); $this->postImage($new_carrier->id); $this->changeZones($new_carrier->id); $new_carrier->setTaxRulesGroup((int) Tools::getValue('id_tax_rules_group')); Tools::redirectAdmin(self::$currentIndex . '&id_' . $this->table . '=' . $current_carrier->id . '&conf=4&token=' . $this->token); } else { $this->errors[] = Tools::displayError('An error occurred while updating an object.') . ' <b>' . $this->table . '</b>'; } } else { $this->errors[] = Tools::displayError('You do not have permission to edit this.'); } } catch (PrestaShopException $e) { $this->errors[] = $e->getMessage(); } } else { if ($this->tabAccess['add'] === '1') { // Create new Carrier $carrier = new Carrier(); $this->copyFromPost($carrier, $this->table); $carrier->position = Carrier::getHigherPosition() + 1; if ($carrier->add()) { if (($_POST['id_' . $this->table] = $carrier->id) && $this->postImage($carrier->id) && $this->_redirect) { $carrier->setTaxRulesGroup((int) Tools::getValue('id_tax_rules_group'), true); $this->changeZones($carrier->id); $this->changeGroups($carrier->id); $this->updateAssoShop($carrier->id); Tools::redirectAdmin(self::$currentIndex . '&id_' . $this->table . '=' . $carrier->id . '&conf=3&token=' . $this->token); } } else { $this->errors[] = Tools::displayError('An error occurred while creating an object.') . ' <b>' . $this->table . '</b>'; } } else { $this->errors[] = Tools::displayError('You do not have permission to add this.'); } } } parent::postProcess(); } else { if (isset($_GET['isFree' . $this->table])) { $this->processIsFree(); } else { /* if ((Tools::isSubmit('submitDel'.$this->table) && in_array(Configuration::get('PS_CARRIER_DEFAULT'), Tools::getValue('carrierBox'))) || (isset($_GET['delete'.$this->table]) && Tools::getValue('id_carrier') == Configuration::get('PS_CARRIER_DEFAULT'))) $this->errors[] = $this->l('Please set another carrier as default before deleting this one.'); else { */ // if deletion : removes the carrier from the warehouse/carrier association if (Tools::isSubmit('delete' . $this->table)) { $id = (int) Tools::getValue('id_' . $this->table); // Delete from the reference_id and not from the carrier id $carrier = new Carrier((int) $id); Warehouse::removeCarrier($carrier->id_reference); } else { if (Tools::isSubmit($this->table . 'Box') && count(Tools::isSubmit($this->table . 'Box')) > 0) { $ids = Tools::getValue($this->table . 'Box'); array_walk($ids, 'intval'); foreach ($ids as $id) { // Delete from the reference_id and not from the carrier id $carrier = new Carrier((int) $id); Warehouse::removeCarrier($carrier->id_reference); } } } parent::postProcess(); Carrier::cleanPositions(); //} } } }
/** * @since 1.5.0 * @see ObjectModel::delete() */ public function delete() { if (!parent::delete()) { return false; } Carrier::cleanPositions(); return Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'cart_rule_carrier WHERE id_carrier = ' . (int) $this->id) && $this->deleteTaxRulesGroup(Shop::getShops(true, null, true)); }
/** * Manage page processing */ public function postProcess() { if (!isset($this->table)) { return false; } // set token $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token; // Sub included tab postProcessing $this->includeSubTab('postProcess', array('status', 'submitAdd1', 'submitDel', 'delete', 'submitFilter', 'submitReset')); /* Delete object image */ if (isset($_GET['deleteImage'])) { if (Validate::isLoadedObject($object = $this->loadObject())) { /** @var ObjectModel $object */ if ($object->deleteImage()) { Tools::redirectAdmin(self::$currentIndex . '&add' . $this->table . '&' . $this->identifier . '=' . Tools::getValue($this->identifier) . '&conf=7&token=' . $token); } } $this->_errors[] = Tools::displayError('An error occurred during image deletion (cannot load object).'); } elseif (isset($_GET['delete' . $this->table])) { if ($this->tabAccess['delete'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject()) && isset($this->fieldImageSettings)) { /** @var ObjectModel $object */ // check if request at least one object with noZeroObject if (isset($object->noZeroObject) && count(call_user_func(array($this->className, $object->noZeroObject))) <= 1) { $this->_errors[] = Tools::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools::displayError('You cannot delete all of the items.'); } else { if ($this->deleted) { $object->deleteImage(); $object->deleted = 1; if (method_exists($object, 'cleanPositions')) { $object->cleanPositions(); } if ($object->update()) { Tools::redirectAdmin(self::$currentIndex . '&conf=1&token=' . $token); } } elseif ($object->delete()) { if (method_exists($object, 'cleanPositions')) { $object->cleanPositions(); } Tools::redirectAdmin(self::$currentIndex . '&conf=1&token=' . $token); } $this->_errors[] = Tools::displayError('An error occurred during deletion.'); } } else { $this->_errors[] = Tools::displayError('An error occurred while deleting object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); } } elseif ((isset($_GET['status' . $this->table]) || isset($_GET['status'])) && Tools::getValue($this->identifier)) { if ($this->tabAccess['edit'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject())) { /** @var ObjectModel $object */ if ($object->toggleStatus()) { Tools::redirectAdmin(self::$currentIndex . '&conf=5' . (($id_category = (int) Tools::getValue('id_category')) && Tools::getValue('id_product') ? '&id_category=' . $id_category : '') . '&token=' . $token); } else { $this->_errors[] = Tools::displayError('An error occurred while updating status.'); } } else { $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } } elseif (isset($_GET['position'])) { /** @var ObjectModel $object */ if ($this->tabAccess['edit'] !== '1') { $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } elseif (!Validate::isLoadedObject($object = $this->loadObject())) { $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } elseif (!$object->updatePosition((int) Tools::getValue('way'), (int) Tools::getValue('position'))) { $this->_errors[] = Tools::displayError('Failed to update the position.'); } else { Tools::redirectAdmin(self::$currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_identifier = (int) Tools::getValue($this->identifier)) ? '&' . $this->identifier . '=' . $id_identifier : '') . '&token=' . $token); } } elseif (Tools::getValue('submitDel' . $this->table)) { if ($this->tabAccess['delete'] === '1') { if (isset($_POST[$this->table . 'Box'])) { /** @var ObjectModel $object */ $object = new $this->className(); if (isset($object->noZeroObject) && (count(call_user_func(array($this->className, $object->noZeroObject))) <= 1 || count($_POST[$this->table . 'Box']) == count(call_user_func(array($this->className, $object->noZeroObject))))) { $this->_errors[] = Tools::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools::displayError('You cannot delete all of the items.'); } else { $result = true; if ($this->deleted) { foreach (Tools::getValue($this->table . 'Box') as $id) { /** @var ObjectModel $toDelete */ $toDelete = new $this->className($id); $toDelete->deleted = 1; $result = $result && $toDelete->update(); } } else { $result = $object->deleteSelection(Tools::getValue($this->table . 'Box')); } if ($result) { Tools::redirectAdmin(self::$currentIndex . '&conf=2&token=' . $token); } $this->_errors[] = Tools::displayError('An error occurred while deleting selection.'); } // clean carriers positions Carrier::cleanPositions(); } else { $this->_errors[] = Tools::displayError('You must select at least one element to delete.'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); } } elseif (Tools::getValue('submitAdd' . $this->table)) { /* Checking fields validity */ $this->validateRules(); if (!count($this->_errors)) { $id = (int) Tools::getValue($this->identifier); /* Object update */ if (isset($id) && !empty($id)) { if ($this->tabAccess['edit'] === '1' || $this->table == 'employee' && $this->context->employee->id == Tools::getValue('id_employee') && Tools::isSubmit('updateemployee')) { /** @var ObjectModel $object */ $object = new $this->className($id); if (Validate::isLoadedObject($object)) { /* Specific to objects which must not be deleted */ if ($this->deleted && $this->beforeDelete($object)) { /** @var ObjectModel $objectNew */ // Create new one with old objet values $objectNew = new $this->className($object->id); $objectNew->id = null; $objectNew->date_add = ''; $objectNew->date_upd = ''; // Update old object to deleted $object->deleted = 1; $object->update(); // Update new object with post values $this->copyFromPost($objectNew, $this->table); $result = $objectNew->add(); if (Validate::isLoadedObject($objectNew)) { $this->afterDelete($objectNew, $object->id); } } else { $this->copyFromPost($object, $this->table); $result = $object->update(); $this->afterUpdate($object); } if ($object->id) { $this->updateAssoShop($object->id); } if (!$result) { $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')'; } elseif ($this->postImage($object->id) && !count($this->_errors)) { if ($this->table == 'group') { $this->updateRestrictions($object->id); } $parent_id = (int) Tools::getValue('id_parent', 1); // Specific back redirect if ($back = Tools::getValue('back')) { Tools::redirectAdmin(urldecode($back) . '&conf=4'); } // Specific scene feature if (Tools::getValue('stay_here') == 'on' || Tools::getValue('stay_here') == 'true' || Tools::getValue('stay_here') == '1') { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&updatescene&token=' . $token); } // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&update' . $this->table . '&token=' . $token); } // Save and back to parent if (Tools::isSubmit('submitAdd' . $this->table . 'AndBackToParent')) { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=4&token=' . $token); } // Default behavior (save and back) Tools::redirectAdmin(self::$currentIndex . ($parent_id ? '&' . $this->identifier . '=' . $object->id : '') . '&conf=4&token=' . $token); } } else { $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } } else { if ($this->tabAccess['add'] === '1') { /** @var ObjectModel $object */ $object = new $this->className(); $this->copyFromPost($object, $this->table); if (!$object->add()) { $this->_errors[] = Tools::displayError('An error occurred while creating object.') . ' <b>' . $this->table . ' (' . Db::getInstance()->getMsgError() . ')</b>'; } elseif (($_POST[$this->identifier] = $object->id) && $this->postImage($object->id) && !count($this->_errors) && $this->_redirect) { $parent_id = (int) Tools::getValue('id_parent', 1); $this->afterAdd($object); $this->updateAssoShop($object->id); if ($this->table == 'group') { $this->updateRestrictions($object->id); // assign group access to every categories $categories = Category::getCategories($this->context->language->id, true); $rowList = array(); $a = 0; foreach ($categories as $category) { foreach ($category as $categ_id => $categ) { if ($categ_id != 1) { $rowList[] = array('id_category' => $categ_id, 'id_group' => $object->id); } } } Db::getInstance()->insert('category_group', $rowList); } // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=3&update' . $this->table . '&token=' . $token); } // Save and back to parent if (Tools::isSubmit('submitAdd' . $this->table . 'AndBackToParent')) { Tools::redirectAdmin(self::$currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=3&token=' . $token); } // Default behavior (save and back) Tools::redirectAdmin(self::$currentIndex . ($parent_id ? '&' . $this->identifier . '=' . $object->id : '') . '&conf=3&token=' . $token); } } else { $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } } } $this->_errors = array_unique($this->_errors); } elseif (isset($_POST['submitReset' . $this->table])) { $filters = $this->context->cookie->getFamily($this->table . 'Filter_'); foreach ($filters as $cookieKey => $filter) { if (strncmp($cookieKey, $this->table . 'Filter_', 7 + Tools::strlen($this->table)) == 0) { $key = Tools::substr($cookieKey, 7 + Tools::strlen($this->table)); /* Table alias could be specified using a ! eg. alias!field */ $tmpTab = explode('!', $key); $key = count($tmpTab) > 1 ? $tmpTab[1] : $tmpTab[0]; if (array_key_exists($key, $this->fieldsDisplay)) { unset($this->context->cookie->{$cookieKey}); } } } if (isset($this->context->cookie->{'submitFilter' . $this->table})) { unset($this->context->cookie->{'submitFilter' . $this->table}); } if (isset($this->context->cookie->{$this->table . 'Orderby'})) { unset($this->context->cookie->{$this->table . 'Orderby'}); } if (isset($this->context->cookie->{$this->table . 'Orderway'})) { unset($this->context->cookie->{$this->table . 'Orderway'}); } unset($_POST); } elseif (Tools::getValue('submitOptions' . $this->table)) { $this->updateOptions($token); } elseif (Tools::isSubmit('submitFilter' . $this->table) || $this->context->cookie->{'submitFilter' . $this->table} !== false) { $_POST = array_merge($this->context->cookie->getFamily($this->table . 'Filter_'), isset($_POST) ? $_POST : array()); foreach ($_POST as $key => $value) { /* Extracting filters from $_POST on key filter_ */ if ($value != null && !strncmp($key, $this->table . 'Filter_', 7 + Tools::strlen($this->table))) { $key = Tools::substr($key, 7 + Tools::strlen($this->table)); /* Table alias could be specified using a ! eg. alias!field */ $tmpTab = explode('!', $key); $filter = count($tmpTab) > 1 ? $tmpTab[1] : $tmpTab[0]; if ($field = $this->filterToField($key, $filter)) { $type = array_key_exists('filter_type', $field) ? $field['filter_type'] : (array_key_exists('type', $field) ? $field['type'] : false); if (($type == 'date' || $type == 'datetime') && is_string($value)) { $value = Tools::unSerialize($value); } $key = isset($tmpTab[1]) ? $tmpTab[0] . '.`' . bqSQL($tmpTab[1]) . '`' : '`' . bqSQL($tmpTab[0]) . '`'; if (array_key_exists('tmpTableFilter', $field)) { $sqlFilter =& $this->_tmpTableFilter; } elseif (array_key_exists('havingFilter', $field)) { $sqlFilter =& $this->_filterHaving; } else { $sqlFilter =& $this->_filter; } /* Only for date filtering (from, to) */ if (is_array($value)) { if (isset($value[0]) && !empty($value[0])) { if (!Validate::isDate($value[0])) { $this->_errors[] = Tools::displayError('\'From:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . $key . ' >= \'' . pSQL(Tools::dateFrom($value[0])) . '\''; } } if (isset($value[1]) && !empty($value[1])) { if (!Validate::isDate($value[1])) { $this->_errors[] = Tools::displayError('\'To:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . $key . ' <= \'' . pSQL(Tools::dateTo($value[1])) . '\''; } } } else { $sqlFilter .= ' AND '; if ($type == 'int' || $type == 'bool') { $sqlFilter .= ($key == $this->identifier || $key == '`' . $this->identifier . '`' || $key == '`active`' ? 'a.' : '') . pSQL($key) . ' = ' . (int) $value . ' '; } elseif ($type == 'decimal') { $sqlFilter .= ($key == $this->identifier || $key == '`' . $this->identifier . '`' ? 'a.' : '') . pSQL($key) . ' = ' . (double) $value . ' '; } elseif ($type == 'select') { $sqlFilter .= ($key == $this->identifier || $key == '`' . $this->identifier . '`' ? 'a.' : '') . pSQL($key) . ' = \'' . pSQL($value) . '\' '; } else { $sqlFilter .= ($key == $this->identifier || $key == '`' . $this->identifier . '`' ? 'a.' : '') . pSQL($key) . ' LIKE \'%' . pSQL($value) . '%\' '; } } } } } } elseif (Tools::isSubmit('submitFields') && $this->requiredDatabase && $this->tabAccess['add'] === '1' && $this->tabAccess['delete'] === '1') { if (!is_array($fields = Tools::getValue('fieldsBox'))) { $fields = array(); } /** @var ObjectModel $object */ $object = new $this->className(); if (!$object->addFieldsRequiredDatabase($fields)) { $this->_errors[] = Tools::displayError('Error in updating required fields'); } else { Tools::redirectAdmin(self::$currentIndex . '&conf=4&token=' . $token); } } }