/** * Manage page processing * * @global string $currentIndex Current URL in order to keep current Tab */ public function postProcess() { global $currentIndex, $cookie; if (!isset($this->table)) { return false; } // set token $token = Tools14::getValue('token') ? Tools14::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())) { if ($object->deleteImage()) { Tools14::redirectAdmin($currentIndex . '&add' . $this->table . '&' . $this->identifier . '=' . Tools14::getValue($this->identifier) . '&conf=7&token=' . $token); } } $this->_errors[] = Tools14::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()) and isset($this->fieldImageSettings)) { // check if request at least one object with noZeroObject if (isset($object->noZeroObject) and sizeof(call_user_func(array($this->className, $object->noZeroObject))) <= 1) { $this->_errors[] = Tools14::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools14::displayError('You cannot delete all of the items.'); } else { if ($this->deleted) { $object->deleteImage(); $object->deleted = 1; if ($object->update()) { Tools14::redirectAdmin($currentIndex . '&conf=1&token=' . $token); } } elseif ($object->delete()) { Tools14::redirectAdmin($currentIndex . '&conf=1&token=' . $token); } $this->_errors[] = Tools14::displayError('An error occurred during deletion.'); } } else { $this->_errors[] = Tools14::displayError('An error occurred while deleting object.') . ' <b>' . $this->table . '</b> ' . Tools14::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools14::displayError('You do not have permission to delete here.'); } } elseif ((isset($_GET['status' . $this->table]) or isset($_GET['status'])) and Tools14::getValue($this->identifier)) { if ($this->tabAccess['edit'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject())) { if ($object->toggleStatus()) { Tools14::redirectAdmin($currentIndex . '&conf=5' . (($id_category = (int) Tools14::getValue('id_category') and Tools14::getValue('id_product')) ? '&id_category=' . $id_category : '') . '&token=' . $token); } else { $this->_errors[] = Tools14::displayError('An error occurred while updating status.'); } } else { $this->_errors[] = Tools14::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools14::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools14::displayError('You do not have permission to edit here.'); } } elseif (isset($_GET['position'])) { if ($this->tabAccess['edit'] !== '1') { $this->_errors[] = Tools14::displayError('You do not have permission to edit here.'); } elseif (!Validate::isLoadedObject($object = $this->loadObject())) { $this->_errors[] = Tools14::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools14::displayError('(cannot load object)'); } elseif (!$object->updatePosition((int) Tools14::getValue('way'), (int) Tools14::getValue('position'))) { $this->_errors[] = Tools14::displayError('Failed to update the position.'); } else { Tools14::redirectAdmin($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = (int) Tools14::getValue($this->identifier)) ? '&' . $this->identifier . '=' . $id_category : '') . '&token=' . $token); } Tools14::redirectAdmin($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = (int) Tools14::getValue('id_category') and Tools14::getValue('id_product')) ? '&id_category=' . $id_category : '') . '&token=' . $token); } elseif (Tools14::getValue('submitDel' . $this->table)) { if ($this->tabAccess['delete'] === '1') { if (isset($_POST[$this->table . 'Box'])) { $object = new $this->className(); if (isset($object->noZeroObject) and (sizeof(call_user_func(array($this->className, $object->noZeroObject))) <= 1 or sizeof($_POST[$this->table . 'Box']) == sizeof(call_user_func(array($this->className, $object->noZeroObject))))) { $this->_errors[] = Tools14::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools14::displayError('You cannot delete all of the items.'); } else { $result = true; if ($this->deleted) { foreach (Tools14::getValue($this->table . 'Box') as $id) { $toDelete = new $this->className($id); $toDelete->deleted = 1; $result = $result and $toDelete->update(); } } else { $result = $object->deleteSelection(Tools14::getValue($this->table . 'Box')); } if ($result) { Tools14::redirectAdmin($currentIndex . '&conf=2&token=' . $token); } $this->_errors[] = Tools14::displayError('An error occurred while deleting selection.'); } } else { $this->_errors[] = Tools14::displayError('You must select at least one element to delete.'); } } else { $this->_errors[] = Tools14::displayError('You do not have permission to delete here.'); } } elseif (Tools14::getValue('submitAdd' . $this->table)) { /* Checking fields validity */ $this->validateRules(); if (!sizeof($this->_errors)) { $id = (int) Tools14::getValue($this->identifier); /* Object update */ if (isset($id) and !empty($id)) { if ($this->tabAccess['edit'] === '1' or $this->table == 'employee' and $cookie->id_employee == Tools14::getValue('id_employee') and Tools14::isSubmit('updateemployee')) { $object = new $this->className($id); if (Validate::isLoadedObject($object)) { /* Specific to objects which must not be deleted */ if ($this->deleted and $this->beforeDelete($object)) { // 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 (!$result) { $this->_errors[] = Tools14::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')'; } elseif ($this->postImage($object->id) and !sizeof($this->_errors)) { $parent_id = (int) Tools14::getValue('id_parent', 1); // Specific back redirect if ($back = Tools14::getValue('back')) { Tools14::redirectAdmin(urldecode($back) . '&conf=4'); } // Specific scene feature if (Tools14::getValue('stay_here') == 'on' || Tools14::getValue('stay_here') == 'true' || Tools14::getValue('stay_here') == '1') { Tools14::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&updatescene&token=' . $token); } // Save and stay on same form if (Tools14::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools14::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&update' . $this->table . '&token=' . $token); } // Save and back to parent if (Tools14::isSubmit('submitAdd' . $this->table . 'AndBackToParent')) { Tools14::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=4&token=' . $token); } // Default behavior (save and back) Tools14::redirectAdmin($currentIndex . ($parent_id ? '&' . $this->identifier . '=' . $object->id : '') . '&conf=4&token=' . $token); } } else { $this->_errors[] = Tools14::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> ' . Tools14::displayError('(cannot load object)'); } } else { $this->_errors[] = Tools14::displayError('You do not have permission to edit here.'); } } else { if ($this->tabAccess['add'] === '1') { $object = new $this->className(); $this->copyFromPost($object, $this->table); if (!$object->add()) { $this->_errors[] = Tools14::displayError('An error occurred while creating object.') . ' <b>' . $this->table . ' (' . mysql_error() . ')</b>'; } elseif ($_POST[$this->identifier] = $object->id and $this->postImage($object->id) and !sizeof($this->_errors) and $this->_redirect) { $parent_id = (int) Tools14::getValue('id_parent', 1); $this->afterAdd($object); // Save and stay on same form if (Tools14::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools14::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=3&update' . $this->table . '&token=' . $token); } // Save and back to parent if (Tools14::isSubmit('submitAdd' . $this->table . 'AndBackToParent')) { Tools14::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=3&token=' . $token); } // Default behavior (save and back) Tools14::redirectAdmin($currentIndex . ($parent_id ? '&' . $this->identifier . '=' . $object->id : '') . '&conf=3&token=' . $token); } } else { $this->_errors[] = Tools14::displayError('You do not have permission to add here.'); } } } $this->_errors = array_unique($this->_errors); } elseif (isset($_POST['submitReset' . $this->table])) { $filters = $cookie->getFamily($this->table . 'Filter_'); foreach ($filters as $cookieKey => $filter) { if (strncmp($cookieKey, $this->table . 'Filter_', 7 + Tools14::strlen($this->table)) == 0) { $key = substr($cookieKey, 7 + Tools14::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($cookie->{$cookieKey}); } } } if (isset($cookie->{'submitFilter' . $this->table})) { unset($cookie->{'submitFilter' . $this->table}); } if (isset($cookie->{$this->table . 'Orderby'})) { unset($cookie->{$this->table . 'Orderby'}); } if (isset($cookie->{$this->table . 'Orderway'})) { unset($cookie->{$this->table . 'Orderway'}); } unset($_POST); } elseif (Tools14::getValue('submitOptions' . $this->table)) { $this->updateOptions($token); } elseif (Tools14::isSubmit('submitFilter' . $this->table) or $cookie->{'submitFilter' . $this->table} !== false) { $_POST = array_merge($cookie->getFamily($this->table . 'Filter_'), isset($_POST) ? $_POST : array()); foreach ($_POST as $key => $value) { /* Extracting filters from $_POST on key filter_ */ if ($value != NULL and !strncmp($key, $this->table . 'Filter_', 7 + Tools14::strlen($this->table))) { $key = Tools14::substr($key, 7 + Tools14::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' or $type == 'datetime') and is_string($value)) { $value = unserialize($value); } $key = isset($tmpTab[1]) ? $tmpTab[0] . '.`' . $tmpTab[1] . '`' : '`' . $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]) and !empty($value[0])) { if (!Validate::isDate($value[0])) { $this->_errors[] = Tools14::displayError('\'from:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . pSQL($key) . ' >= \'' . pSQL(Tools14::dateFrom($value[0])) . '\''; } } if (isset($value[1]) and !empty($value[1])) { if (!Validate::isDate($value[1])) { $this->_errors[] = Tools14::displayError('\'to:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . pSQL($key) . ' <= \'' . pSQL(Tools14::dateTo($value[1])) . '\''; } } } else { $sqlFilter .= ' AND '; if ($type == 'int' or $type == 'bool') { $sqlFilter .= (($key == $this->identifier or $key == '`' . $this->identifier . '`' or $key == '`active`') ? 'a.' : '') . pSQL($key) . ' = ' . (int) $value . ' '; } elseif ($type == 'decimal') { $sqlFilter .= (($key == $this->identifier or $key == '`' . $this->identifier . '`') ? 'a.' : '') . pSQL($key) . ' = ' . (double) $value . ' '; } elseif ($type == 'select') { $sqlFilter .= (($key == $this->identifier or $key == '`' . $this->identifier . '`') ? 'a.' : '') . pSQL($key) . ' = \'' . pSQL($value) . '\' '; } else { $sqlFilter .= (($key == $this->identifier or $key == '`' . $this->identifier . '`') ? 'a.' : '') . pSQL($key) . ' LIKE \'%' . pSQL($value) . '%\' '; } } } } } } elseif (Tools14::isSubmit('submitFields') and $this->requiredDatabase and $this->tabAccess['add'] === '1' and $this->tabAccess['delete'] === '1') { if (!is_array($fields = Tools14::getValue('fieldsBox'))) { $fields = array(); } $object = new $this->className(); if (!$object->addFieldsRequiredDatabase($fields)) { $this->_errors[] = Tools14::displayError('Error in updating required fields'); } else { Tools14::redirectAdmin($currentIndex . '&conf=4&token=' . $token); } } }