function allAction() { $fc = FrontController::getInstance(); $model = new FrontModel(); $productModel = new ProductTableModel(); $productModel->setTable('product'); $params = $fc->getParams(); $condition = ''; $paramsMap = ['brand', 'color', 'category_id', 'subcategory_id']; if (!empty($params)) { $condition .= 'WHERE'; foreach ($params as $name => $value) { if (in_array(strtolower($name), $paramsMap)) { $condition .= " product.{$name} = '{$value}' AND"; } } $condition .= ' main = 1'; if (strpos(strtolower($condition), 'join') || strpos(strtolower($condition), 'union')) { $condition = ''; } } //for pagination $num = (new AdminWidgets())->getNum('product', substr($condition, 0, -13)); $page = $fc->getParams()['page'] ? filter_var($fc->getParams()['page'], FILTER_SANITIZE_NUMBER_INT) : 1; $limit = $fc->getParams()['limit'] ? filter_var($fc->getParams()['limit'], FILTER_SANITIZE_NUMBER_INT) : 20; $orderBy = $fc->getParams()['orderBy'] ? filter_var($fc->getParams()['orderBy'], FILTER_SANITIZE_STRING) : 'id'; $direction = $fc->getParams()['direction'] ? filter_var($fc->getParams()['direction'], FILTER_SANITIZE_STRING) : 'asc'; $offset = $limit * $page - $limit; $popProducts = (new IndexWidgets())->recAndPopProductsWidget('popular', 3); $recProducts = (new IndexWidgets())->recAndPopProductsWidget('recommended'); // \app\helpers\Helper::g($condition); exit; $model->setData(['products' => $productModel->getAllProducts('*', $condition . " ORDER BY product.{$orderBy} " . strtoupper($direction) . " LIMIT {$limit} OFFSET {$offset}"), 'popularProducts' => Generator::popularProducts($popProducts, 3), 'recommendedProducts' => Generator::recommendedProducts($recProducts), 'page' => $page, 'offset' => $offset, 'start' => $offset + 1, 'end' => $limit * $page < $num ? $limit * $page : $num, 'paginationOptions' => ['brand' => $fc->getParams()['brand'] ? filter_var($fc->getParams()['brand'], FILTER_SANITIZE_STRING) : '', 'color' => $fc->getParams()['color'] ? filter_var($fc->getParams()['color'], FILTER_SANITIZE_STRING) : '', 'category_id' => $fc->getParams()['category_id'] ? filter_var($fc->getParams()['category_id'], FILTER_SANITIZE_STRING) : '', 'subcategory_id' => $fc->getParams()['subcategory_id'] ? filter_var($fc->getParams()['subcategory_id'], FILTER_SANITIZE_STRING) : '', 'limit' => $limit, 'orderBy' => $orderBy, 'direction' => $direction, 'table' => 'product', 'num' => $num]]); $output = $model->render('../views/product/all.php', 'withoutSlider'); $fc->setPage($output); }
public function getAllProductsWidget($fields = '*', $condition = '') { $model = new ProductTableModel(); return $model->getAllProducts($fields, $condition); }
public function editProductAction() { $fc = FrontController::getInstance(); $model = new AdminModel('Редактирование товара'); $id = filter_var($fc->getParams()['product'], FILTER_SANITIZE_STRING); $productModel = new ProductTableModel(); $productModel->setId($id); $productModel->setTable('product'); $imageModel = new ImageTableModel(); $imageModel->setTable('image'); $imageModel->setId($id); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $productModel->setData(); $productModel->updateProduct(); $imageModel = new ImageTableModel($productModel->getId()); $imageModel->setTable('image'); $imageModel->setId($productModel->getId()); $imageModel->setData(); $imageModel->addAllImages(); Session::setMsg('Товар успешно обновлен', 'success'); header('Location: /admin/view/product/' . $productModel->getId()); exit; } else { if (!$id) { header('Location: /admin/notFound'); exit; } $product = $productModel->getAllProducts('*', "WHERE product.id = {$id} GROUP BY product.id"); $imageModel->readRecordsById('product_id'); if (empty($product)) { header('Location: /admin/NotFound'); exit; } else { $model->setData(['products' => $productModel->getAllProducts('*', "WHERE product.id = {$id} GROUP BY product.id"), 'images' => $imageModel->getRecordsById()]); } $catsAndSub = IndexWidgets::getCatsAndSubCats(); $model->categoryList = $catsAndSub['cats']; //used magic __set $model->subCategoryList = $catsAndSub['subcats']; //used magic __set $output = $model->render('../views/admin/product/edit.php', 'admin'); $fc->setPage($output); } }