Каталог с товарами
Создаем файлы моделей models/catalogs.model.php и models/products.model.php
<? //Файл models/catalogs.model.php class Catalogs extends Model { protected $name = "Разделы каталога"; public $parents = array(); protected $model_elements = array( array("Активировать", "bool", "active", array("on_create" => true)), array("Название", "char", "name", array("required" => true)), array("Ссылка", "url", "url", array("unique" => true, "translit_from" => "name")), array("Родительский раздел", "parent", "parent", array("parent_for" => "Products")), array("Изображение", "image", "image"), array("Позиция", "order", "order"), array("Описание", "text", "content", array("rich_text" => true)) ); } //Файл models/products.model.php class Products extends Model { protected $name = "Товары каталога"; protected $model_elements = array( array("Активировать", "bool", "active", array("on_create" => true)), array("Название", "char", "name", array("required" => true)), array("Раздел каталога", "enum", "catalog", array("foreign_key" => "Catalogs", "required" => true, "is_parent" => true, "show_parent" => true)), array("Ссылка", "url", "url", array("unique" => true, "translit_from" => "name")), array("Цена", "int", "price", array("required" => true, "positive" => true)), array("Позиция", "order", "order"), array("Изображения", "multi_images", "images"), array("Рекомендуемые товары", "group", "additional", array("long_list" => true)), array("Описание", "text", "content", array("rich_text" => true)) ); protected $model_display_params = array( "fields_groups" => array( "Основные параметры" => array("active", "name", "catalog", "url", "price", "order", "additional", "images"), "Описание" => array("content") )); }
Вносим названия моделей в файл config/models.php
$mvActiveModels = array('pages', 'blocks', ... , 'catalogs', 'products');
Создаем SQL таблицы в базе данных, после чего модели смогут работать в административной панели
CREATE TABLE `catalogs` ( `id` int(11) NOT NULL, `active` tinyint(1) NOT NULL, `name` varchar(200) NOT NULL, `url` varchar(200) NOT NULL, `order` int(11) NOT NULL, `parent` int(11) NOT NULL, `content` text NOT NULL, `image` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `catalogs` ADD PRIMARY KEY (`id`), ADD KEY `parent_active` (`parent`,`active`), ADD KEY `url_active` (`url`,`active`), ADD KEY `parent` (`parent`); ALTER TABLE `catalogs` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; CREATE TABLE `products` ( `id` int(11) NOT NULL, `name` varchar(250) NOT NULL, `active` tinyint(1) NOT NULL, `order` int(11) NOT NULL, `catalog` int(11) NOT NULL, `url` varchar(200) NOT NULL, `price` int(11) NOT NULL, `content` text NOT NULL, `images` text NOT NULL, `additional` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `products` ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `id_active` (`id`,`active`), ADD KEY `url_active` (`url`,`active`), ADD KEY `catalog_active` (`catalog`,`active`); ALTER TABLE `products` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Создаем файлы шаблонов views/catalog/view-catalog.php и views/catalog/view-product.php, а также вносим маршрут к шаблонам в файл config/routes.php
$mvFrontendRoutes = array( ... , "catalog/*/" => "catalog/view-catalog.php", "product/*/" => "catalog/view-product.php" );
Добавляем методы в файл модели Catalogs
public function defineCatalogPage(Router $router) { $url_parts = $router -> getUrlParts(); $record = false; if(count($url_parts) == 2 && $url_parts[0] == "catalog") if(is_numeric($url_parts[1])) $record = $this -> findRecord(array("id" => $url_parts[1], "active" => 1)); else $record = $this -> findRecord(array("url" => $url_parts[1], "active" => 1)); if($record) { $this -> id = $record -> id; $this -> parents = array_keys($this -> getParents($record -> id)); } return $record; } public function displayCatalogTreeMenu($parent) { $rows = $this -> select(array("parent" => $parent, "active" => 1, "order->asc" => "order")); $html = ""; foreach($rows as $row) { $children = 0; $css_class = ""; $open = in_array($row['id'], $this -> parents); $children = $this -> countRecords(array("parent" => $row['id'], "active" => 1)); $css_class = $children ? "has-children" : ""; $css_class .= ($row['id'] == $this -> id) ? " active" : ""; $css_class .= ($children && in_array($row['id'], $this -> parents)) ? " open" : ""; $css_class = $css_class ? ' class="'.trim($css_class).'"' : ""; $url = $this -> root_path."catalog/".($row['url'] ? $row['url'] : $row['id'])."/"; $html .= "<li".$css_class.">\n<a".$css_class." href=\"".$url."\">".$row['name']."</a>\n"; if($children && $open) $html .= "<ul>\n".$this -> displayCatalogTreeMenu($row['id'])."</ul>\n"; $html .= "</li>\n"; } return $html; } public function display($parent) { $rows = $this -> select(array("parent" => $parent, "active" => 1, "order->asc" => "order")); $html = ""; foreach($rows as $row) { $url = $this -> root_path."catalog/".($row['url'] ? $row['url'] : $row['id'])."/"; $html .= "<div>\n"; $html .= "<a class=\"image\" href=\"".$url."\">\n"; $html .= $this -> cropImage($this -> getFirstImage($row['image']), 230, 200)."</a>\n"; $html .= "<a class=\"name\" href=\"".$url."\">".$row['name']."</a>"; $html .= "</div>\n"; } return $html; }
Добавляем методы в файл модели Products
public function defineProductPage(Router $router) { $url_parts = $router -> getUrlParts(); $record = false; if(count($url_parts) == 2) if(is_numeric($url_parts[1])) $record = $this -> findRecord(array("id" => $url_parts[1], "active" => 1)); else $record = $this -> findRecord(array("url" => $url_parts[1], "active" => 1)); return $record; } public function display($params) { $params["active"] = 1; if(isset($this -> pager)) $params["limit->"] = $this -> pager -> getParamsForSelect(); $rows = $this -> select($params); $html = ""; foreach($rows as $row) { $url = $this -> root_path."product/".($row["url"] ? $row["url"] : $row["id"])."/"; $image = $this -> resizeImage($this -> getFirstImage($row["images"]), 250, 250); $html .= "<div class=\"product\">\n"; $html .= "<div class=\"image\">\n<a href=\"".$url."\">\n"; $html .= $image."</a>\n</div>\n"; $html .= "<div class=\"name\">\n<a href=\"".$url."\">".$row["name"]."</a></div>\n"; $html .= "<div class=\"price\">".$row["price"]."</div>\n"; $html .= "</div>"; } return $html; } public function displayAdditionalProducts($product) { if($product -> additional) $params = array("id->in" => $product -> additional, "limit->" => 4); else $params = array("id!=" => $product -> id, "catalog" => $product -> catalog, "limit->" => 4); $count = $this -> countRecords($params); if($count) { $html = "<div id=\"recommended-header\">Рекомендуем посмотреть</div>\n"; $html .= "<div id=\"recommended-products\">\n"; $html .= $this -> display($params); $html .= "</div>\n"; return $html; } }
Содержимое файла view-catalog.php
<? $catalog = $mv -> catalogs -> defineCatalogPage($mv -> router); $mv -> display404($catalog); $count_products = $mv -> products -> countRecords(array("catalog" => $catalog -> id, "active" => 1)); //Если в каталоге есть товары, то включаем постраничную разбивку if($count_products) { $current_page = $mv -> router -> defineCurrentPage("page"); $mv -> products -> runPager($count_products, 20, $current_page); $path = $mv -> root_path."catalog/".($catalog -> url ? $catalog -> url : $catalog -> id)."/"; } $mv -> seo -> mergeParams($catalog, "name"); include $mv -> views_path."main-header.php"; ?> <div id="content"> <div class="breadcrumbs"> <a href="<? echo $mv -> root_path; ?>">На главную</a> <? echo $mv -> catalogs -> displayBreadcrumbs($catalog -> id, "catalog", "url"); ?> </div> <h1><? echo $catalog -> name; ?></h1> <? if(!$count_products) //Вывод подразделов { echo "<div id=\"catalog-categories\">\n"; echo $mv -> catalogs -> display($catalog -> id)."</div>\n"; } else //Вывод товаров если они есть { echo "<div id=\"catalog-products\">\n"; echo $mv -> products -> display(array("catalog" => $catalog -> id))."</div>\n"; if($count_products && $mv -> products -> pager -> hasPages()) { echo "<div class=\"pager\">\n<span>Страницы: </span>\n"; echo $mv -> products -> pager -> display($path, false); echo "</div>\n"; } } ?> </div> <? include $mv -> views_path."main-footer.php"; ?>
Содержимое файла view-product.php
<? $product = $mv -> products -> defineProductPage($mv -> router); $mv -> display404($product); $mv -> seo -> mergeParams($product, "name"); include $mv -> views_path."main-header.php"; ?> <div id="content"> <div class="breadcrumbs"> <a href="<? echo $mv -> root_path; ?>">На главную</a> <? echo $mv -> products -> displayBreadcrumbs($product -> id, "catalog", "url"); ?> </div> <div class="product-image"> <? $image = $mv -> products -> getFirstImage($product -> images); echo $mv -> products -> resizeImage($image, 450, 450, array("title" => $product -> name)); ?> </div> <h1><? echo $product -> name; ?></h1> <div class="price"><? echo $product -> price; ?></div> <div class="content"><? echo $product -> content; ?></div> <? echo $mv -> products -> displayAdditionalProducts($product); ?> </div> <? include $mv -> views_path."main-footer.php"; ?>