Initial commit
This commit is contained in:
133
playarea/scripts/pages/posts.php
Normal file
133
playarea/scripts/pages/posts.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
// Post Fields
|
||||
|
||||
$author_field = new Text();
|
||||
$create_date_field = new Date();
|
||||
$post_photo_field = new Image();
|
||||
|
||||
$b->field_register("author", $author_field);
|
||||
$b->field_register("create_date", $create_date_field);
|
||||
$b->field_register("post_photo", $post_photo_field);
|
||||
|
||||
$b->router_add("/post/%", function () { preload_post(); }, array("get"));
|
||||
|
||||
function preload_post()
|
||||
{
|
||||
global $b;
|
||||
$slug = $b->router_get_segment(1);
|
||||
|
||||
$search = new Search(array(
|
||||
"class" => "Post",
|
||||
"terms" => array("item_slug" => $slug)
|
||||
));
|
||||
|
||||
$pages = $search->collect();
|
||||
|
||||
if(!count($pages)) {
|
||||
http_response_code(404);
|
||||
$b->template_load("404.php");
|
||||
return;
|
||||
}
|
||||
|
||||
$page = $pages[0];
|
||||
$b->ls_set_key("page", $page);
|
||||
|
||||
$title = $page->get_prop("seo_title") ?? $page->get_item_name();
|
||||
$b->title_set($title);
|
||||
$b->meta_add("description", $page->get_prop("seo_description"));
|
||||
$b->meta_add("keywords", $page->get_prop("seo_keywords"));
|
||||
$b->meta_add("viewport", "width=device-width, user-scalable=no");
|
||||
$b->link_add(array("rel" => "canonical", "href" => $b->router_get_canonical_uri()));
|
||||
|
||||
$parent = $page->get_parent();
|
||||
$breadcrumbs = array();
|
||||
|
||||
if($parent) {
|
||||
$parent_title = $parent->get_item_name();
|
||||
$parent_slug = $parent->get_item_slug();
|
||||
$breadcrumbs[] = array($parent_title, "/posts/" . $parent_slug . "/");
|
||||
}
|
||||
|
||||
$breadcrumbs[] = array($page->get_item_name(), $b->router_get_canonical_uri());
|
||||
$b->ls_set_key("breadcrumbs", $breadcrumbs);
|
||||
$b->template_load("pages/post.php");
|
||||
}
|
||||
|
||||
$b->router_add("/posts/%", function () { preload_posts(); }, array("get"));
|
||||
|
||||
function preload_posts()
|
||||
{
|
||||
global $b;
|
||||
$slug = $b->router_get_segment(1);
|
||||
|
||||
$search = new Search(array(
|
||||
"class" => "Category",
|
||||
"terms" => array("item_slug" => $slug)
|
||||
));
|
||||
|
||||
$pages = $search->collect();
|
||||
|
||||
if(!count($pages)) {
|
||||
http_response_code(404);
|
||||
$b->template_load("404.php");
|
||||
return;
|
||||
}
|
||||
|
||||
$page = $pages[0];
|
||||
$b->ls_set_key("page", $page);
|
||||
|
||||
$title = $page->get_prop("seo_title") ?? $page->get_item_name();
|
||||
$b->title_set($title);
|
||||
$b->meta_add("description", $page->get_prop("seo_description"));
|
||||
$b->meta_add("keywords", $page->get_prop("seo_keywords"));
|
||||
|
||||
$b->meta_add("viewport", "width=device-width, user-scalable=no");
|
||||
$b->link_add(array("rel" => "canonical", "href" => $b->router_get_canonical_uri()));
|
||||
|
||||
$breadcrumbs = array();
|
||||
$breadcrumbs[] = array($page->get_item_name(), $b->router_get_canonical_uri());
|
||||
$b->ls_set_key("breadcrumbs", $breadcrumbs);
|
||||
$b->template_load("pages/posts.php");
|
||||
}
|
||||
|
||||
function posts_search($params): array
|
||||
{
|
||||
global $b;
|
||||
$query = posts_query($params);
|
||||
$result = $b->db_query($query, array(), true);
|
||||
$items = array();
|
||||
foreach ($result as $key => $item_db) {
|
||||
$item = new $item_db["item_class"]($item_db["item_id"]);
|
||||
$item->set_parent($item_db["item_parent"], true);
|
||||
$items[] = $item;
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
function posts_search_count($params)
|
||||
{
|
||||
global $b;
|
||||
$count_query = posts_query($params, true);
|
||||
$result = $b->db_query($count_query, array(), true);
|
||||
return $result[0]["count"];
|
||||
}
|
||||
|
||||
function posts_query($params, $count = false): string
|
||||
{
|
||||
$category_id = $params["category_id"];
|
||||
|
||||
$page = intval($params["page"]) ?? 1;
|
||||
$products_per_page = intval($params["products_per_page"]) ?? 30;
|
||||
$offset = intval(($page - 1) * $products_per_page);
|
||||
$limit = " LIMIT $offset, $products_per_page";
|
||||
$select = "*";
|
||||
|
||||
if($count) {
|
||||
$limit = "";
|
||||
$select = "COUNT(*) as `count`";
|
||||
}
|
||||
|
||||
$parent_where = " AND (`item_parent` = " . $category_id. " OR EXISTS(SELECT 1 FROM `bive_items_links` WHERE `bive_items_links`.`parent_item_id` = " . $category_id . " AND `bive_items`.`item_id` = `bive_items_links`.`child_item_id`))";
|
||||
return "SELECT " . $select . " FROM `bive_items` WHERE `item_class` = 'Post' " . $parent_where . $limit;
|
||||
}
|
||||
Reference in New Issue
Block a user