/** * Set the filters used for the list display */ public function processFilter() { if (!isset($this->list_id)) { $this->list_id = $this->table; } $prefix = str_replace(array('admin', 'controller'), '', Tools::strtolower(get_class($this))); if (isset($this->list_id)) { foreach ($_POST as $key => $value) { if ($value === '') { unset($this->context->cookie->{$prefix . $key}); } elseif (stripos($key, $this->list_id . 'Filter_') === 0) { $this->context->cookie->{$prefix . $key} = !is_array($value) ? $value : serialize($value); } elseif (stripos($key, 'submitFilter') === 0) { $this->context->cookie->{$key} = !is_array($value) ? $value : serialize($value); } } foreach ($_GET as $key => $value) { if (stripos($key, $this->list_id . 'Filter_') === 0) { $this->context->cookie->{$prefix . $key} = !is_array($value) ? $value : serialize($value); } elseif (stripos($key, 'submitFilter') === 0) { $this->context->cookie->{$key} = !is_array($value) ? $value : serialize($value); } if (stripos($key, $this->list_id . 'Orderby') === 0 && Validate::isOrderBy($value)) { if ($value === '' || $value == $this->_defaultOrderBy) { unset($this->context->cookie->{$prefix . $key}); } else { $this->context->cookie->{$prefix . $key} = $value; } } elseif (stripos($key, $this->list_id . 'Orderway') === 0 && Validate::isOrderWay($value)) { if ($value === '' || $value == $this->_defaultOrderWay) { unset($this->context->cookie->{$prefix . $key}); } else { $this->context->cookie->{$prefix . $key} = $value; } } } } $filters = $this->context->cookie->getFamily($prefix . $this->list_id . 'Filter_'); foreach ($filters as $key => $value) { /* Extracting filters from $_POST on key filter_ */ if ($value != null && !strncmp($key, $prefix . $this->list_id . 'Filter_', 7 + Tools::strlen($prefix . $this->list_id))) { $key = Tools::substr($key, 7 + Tools::strlen($prefix . $this->list_id)); /* Table alias could be specified using a ! eg. alias!field */ $tmp_tab = explode('!', $key); $filter = count($tmp_tab) > 1 ? $tmp_tab[1] : $tmp_tab[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($tmp_tab[1]) ? $tmp_tab[0] . '.`' . $tmp_tab[1] . '`' : '`' . $tmp_tab[0] . '`'; // Assignement by reference if (array_key_exists('tmpTableFilter', $field)) { $sql_filter =& $this->_tmpTableFilter; } elseif (array_key_exists('havingFilter', $field)) { $sql_filter =& $this->_filterHaving; } else { $sql_filter =& $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('The \'From\' date format is invalid (YYYY-MM-DD)'); } else { $sql_filter .= ' AND ' . pSQL($key) . ' >= \'' . pSQL(Tools::dateFrom($value[0])) . '\''; } } if (isset($value[1]) && !empty($value[1])) { if (!Validate::isDate($value[1])) { $this->errors[] = Tools::displayError('The \'To\' date format is invalid (YYYY-MM-DD)'); } else { $sql_filter .= ' AND ' . pSQL($key) . ' <= \'' . pSQL(Tools::dateTo($value[1])) . '\''; } } } else { $sql_filter .= ' AND '; $check_key = $key == $this->identifier || $key == '`' . $this->identifier . '`'; if ($type == 'int' || $type == 'bool') { $sql_filter .= ($check_key || $key == '`active`' ? 'a.' : '') . pSQL($key) . ' = ' . (int) $value . ' '; } elseif ($type == 'decimal') { $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' = ' . (double) $value . ' '; } elseif ($type == 'select') { $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' = \'' . pSQL($value) . '\' '; } else { if ($type == 'price') { $value = (double) str_replace(',', '.', $value); } $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' LIKE \'%' . pSQL($value) . '%\' '; } } } } } }
/** * 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 = 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())) { if ($object->deleteImage()) { Tools::redirectAdmin($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()) 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[] = 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 ($object->update()) { Tools::redirectAdmin($currentIndex . '&conf=1&token=' . $token); } } elseif ($object->delete()) { Tools::redirectAdmin($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]) or isset($_GET['status'])) and Tools::getValue($this->identifier)) { if ($this->tabAccess['edit'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject())) { if ($object->toggleStatus()) { Tools::redirectAdmin($currentIndex . '&conf=5' . (($id_category = (int) Tools::getValue('id_category') and 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'])) { 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($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = (int) Tools::getValue($this->identifier)) ? '&' . $this->identifier . '=' . $id_category : '') . '&token=' . $token); } Tools::redirectAdmin($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = (int) Tools::getValue('id_category') and Tools::getValue('id_product')) ? '&id_category=' . $id_category : '') . '&token=' . $token); } elseif (Tools::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[] = 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) { $toDelete = new $this->className($id); $toDelete->deleted = 1; $result = $result and $toDelete->update(); } } else { $result = $object->deleteSelection(Tools::getValue($this->table . 'Box')); } if ($result) { Tools::redirectAdmin($currentIndex . '&conf=2&token=' . $token); } $this->_errors[] = Tools::displayError('An error occurred while deleting selection.'); } } 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 (!sizeof($this->_errors)) { $id = (int) Tools::getValue($this->identifier); /* Object update */ if (isset($id) and !empty($id)) { if ($this->tabAccess['edit'] === '1' or $this->table == 'employee' and $cookie->id_employee == Tools::getValue('id_employee') and Tools::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[] = Tools::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) 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($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($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($currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=4&token=' . $token); } // Default behavior (save and back) Tools::redirectAdmin($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') { $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 . ' (' . mysql_error() . ')</b>'; } elseif ($_POST[$this->identifier] = $object->id and $this->postImage($object->id) and !sizeof($this->_errors) and $this->_redirect) { $parent_id = (int) Tools::getValue('id_parent', 1); $this->afterAdd($object); // Save and stay on same form if (Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) { Tools::redirectAdmin($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($currentIndex . '&' . $this->identifier . '=' . $parent_id . '&conf=3&token=' . $token); } // Default behavior (save and back) Tools::redirectAdmin($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 = $cookie->getFamily($this->table . 'Filter_'); foreach ($filters as $cookieKey => $filter) { if (strncmp($cookieKey, $this->table . 'Filter_', 7 + Tools::strlen($this->table)) == 0) { $key = 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($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 (Tools::getValue('submitOptions' . $this->table)) { $this->updateOptions($token); } elseif (Tools::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 + 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' 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[] = Tools::displayError('\'from:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . pSQL($key) . ' >= \'' . pSQL(Tools::dateFrom($value[0])) . '\''; } } if (isset($value[1]) and !empty($value[1])) { if (!Validate::isDate($value[1])) { $this->_errors[] = Tools::displayError('\'to:\' date format is invalid (YYYY-MM-DD)'); } else { $sqlFilter .= ' AND ' . pSQL($key) . ' <= \'' . pSQL(Tools::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 (Tools::isSubmit('submitFields') and $this->requiredDatabase and $this->tabAccess['add'] === '1' and $this->tabAccess['delete'] === '1') { if (!is_array($fields = Tools::getValue('fieldsBox'))) { $fields = array(); } $object = new $this->className(); if (!$object->addFieldsRequiredDatabase($fields)) { $this->_errors[] = Tools::displayError('Error in updating required fields'); } else { Tools::redirectAdmin($currentIndex . '&conf=4&token=' . $token); } } }
/** * Set the filters used for the list display */ public function processFilter() { $filters = $this->context->cookie->getFamily($this->table . 'Filter_'); foreach ($filters 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 */ $tmp_tab = explode('!', $key); $filter = count($tmp_tab) > 1 ? $tmp_tab[1] : $tmp_tab[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($tmp_tab[1]) ? $tmp_tab[0] . '.`' . $tmp_tab[1] . '`' : '`' . $tmp_tab[0] . '`'; // Assignement by reference if (array_key_exists('tmpTableFilter', $field)) { $sql_filter =& $this->_tmpTableFilter; } elseif (array_key_exists('havingFilter', $field)) { $sql_filter =& $this->_filterHaving; } else { $sql_filter =& $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 { $sql_filter .= ' AND ' . pSQL($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 { $sql_filter .= ' AND ' . pSQL($key) . ' <= \'' . pSQL(Tools::dateTo($value[1])) . '\''; } } } else { $sql_filter .= ' AND '; $check_key = $key == $this->identifier || $key == '`' . $this->identifier . '`'; if ($type == 'int' || $type == 'bool') { $sql_filter .= ($check_key || $key == '`active`' ? 'a.' : '') . pSQL($key) . ' = ' . (int) $value . ' '; } elseif ($type == 'decimal') { $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' = ' . (double) $value . ' '; } elseif ($type == 'select') { $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' = \'' . pSQL($value) . '\' '; } else { $sql_filter .= ($check_key ? 'a.' : '') . pSQL($key) . ' LIKE \'%' . pSQL($value) . '%\' '; } } } } } }