Menu protocol

Is used to create a menu, set a protocol that is used later to validate pages 404 and 301 redirect.

The order in the methods bellow is called is important.

Create menu

1. Load items from database

Load database items and init builder.

$select = \query\DB::_select("id,name,permalink,parent,template", "pages", ["CON_DEFAULT", "type" => "page"], "position ASC");
$fetch = $select->fetch(function($row) {
	return [$row->parent => [$row->id => $row]];
});
$builder = new \nest\builder($fetch);
2. Build URI menu structure

Build backend URI menu structure, here you can customize the premalink for each item.

// If you want to prepend URI. This should be called before build
$builder->uriAppend(["blogg"]);

$builder->build(function($obj) {
	// Add to URI protocol. 
	// IF false then take previous URI value, can be used with section/anchor, 
	// will return page URI, then you can later add the anchor
	return $obj->permalink; 
});

Once it is build you can use the same meny as regular menu and sidebar without loading above.

2. Get protocol

Protocol will be used to validate current page. You need to set protocol before building template bellow so active menu item will work.

$this->modal()->protocol() = $builder->protocol();
2.1 Modify item in protocol

You can modify the URI for a item in protocol.

@builder->protocol()->modifyProtocol(PAGE_ID, URI_ARRAY_PART, TYPE)

TYPE
  • append: about-us/our-blogg-page => about-us/our-blogg-page/blogg
  • prepend: about-us/our-blogg-page => blogg/about-us/our-blogg-page
  • change: about-us/our-blogg-page => about-us/blogg
  • replace: about-us/our-blogg-page => blogg
$this->modal()->protocol()->modifyProtocol(1, ["blogg"], "change");
2.2 Set current page in protocol

Protocol will be used to validate current page. You need to set protocol before building template bellow so active menu item will work.

@builder->protocol()->get(URI_ROUTER_ARRAY_PART)

$uriPart = $this->url()->select("page")->get();
$this->modal()->protocol()->get($uriPart);
3. Set menu template

KEY is used so you can choose nav template when echo menu.

@builder->html(SET_TEMPLATE_NAME, UL_TAG, LI_TAG, CALLBACK)

// Default navigation template, args: template name, listparent tag, list tag, callback
$builder->html("nav", "ul", "li", function($obj, $li, $active, $level) {

	// Has children?
	$hasChild = ($obj->hasChild) ? " has-child" : NULL;

	// Add class to li tag
	$li->attr("class", "menuitem{$hasChild}{$active}");

	// Create a tag (link)
	$a = $li->create("a", $obj->name)
	->attr("href", $obj->uri)
	->attr("class", "item{$active}");
});
4. echo menu

@builder->get(GET_TEMPLATE_NAME)

echo $builder->get("nav");
5. echo sidebar menu after top menu

@builder->get(GET_TEMPLATE_NAME)

if(!is_null($this->modal()->protocol()) && ($arr = $this->modal()->protocol()->data()) && count($arr) > 1) {
	$obj = reset($arr);
	$topParent = key($arr);
	echo $builder->get("nav", $topParent);
}

Multiple menus

Modify/add the bolded text bellow into the above code.

1. Load items from database

$select = \query\DB::_select("id,name,permalink,parent,template", "pages", ["CON_DEFAULT", "type" => "page"], "position ASC");
$fetch = $select->fetch(function($row) {
	return [$row->mid => [$row->parent => [$row->id => $row]]];
});
$builder = new \nest\builder($fetch);

2. Build URI menu structure

$builder->multiple()->build(function($obj) {
	// Add to URI protocol
	return $obj->permalink;
});

4. echo menu

@builder->select(MENU_ID)->get(GET_TEMPLATE_NAME)

// Default menu
echo $builder->select(0)->get("nav");

// New menu
echo $builder->select(1)->get("nav");

Validate current page

Now the menu and protocol is set

switch($this->modal()->protocol()->status()) {
	case 301:
		$this->modal()->headers()->set(301);
		$this->modal()->headers()->location(301);
		// Instantly kill processed and redirect the users location. You can tho skip this, it will be executed later on.
		// $this->modal()->headers()->send();
	break;
	case 404:
		$this->modal()->headers()->set(404);
	break;
	default:
		$breadcrumb = $this->modal()->protocol()->data();
		$current = end($pages);
	break;
}

Complete function list

CT nest - builder
Version: 3.5
Copyright: All right reserved for Creative Army
__construct($array)
CT nest - protocol
Version: 3.5
Copyright: All right reserved for Creative Army
static _init()
Change how to handle start/root page of protocol. Will use argument array instead as param/uri array
@param array $arr uri array
@return self
startParamSet(?array $arr)
Check if current page is the start page
@return boolean
isStart()
List all protocol data
@return array
list()
Get current protocol data
@return array
data()
Get current protocol data
@return array
current()
Modify protocol item
@param int $id Item ID
@param array $uri add URI to URI
@param string $append append, prepend or replace
@return self
modifyProtocol(int $id, array $uri, string $type = "append")
Find and protocol item with key
@param key $p
@return string/bool item / false
exists($p)
Return URI if key (table column) mathes value
@param key $p
@param string $value
@return string/bool string (URI) / false
searchURI($key, $value)
Return ROW if key (table column) mathes value
@param key $p
@param string $value
@return string/bool string (URI) / false
search($key, $value)
Find and URI with key
@param key $p
@return string/bool string (URI) / false
findURI($p, ?int $byID = NULL)
Get URI By permalink
@param string $id (string becouse of addes duplicate preflix)
@return string
getURI(?string $permalink)
Get full URI as Array
@param string $id (string becouse of addes duplicate preflix)
@return string
getURIArray(?string $permalink)
Get data from URI ARRAY
@param array $arr URI items
@return Array
feedFromArray(array $arr)
Get URI By id
@param string $id (string becouse of addes duplicate preflix)
@return string
getURIByID(string $id)
Get Data By permalink
@param string $id (string becouse of addes duplicate preflix)
@return array
getData(?string $permalink)
Get Data By ID
@param string $id (string becouse of addes duplicate preflix)
@return array
getDataByID(string $id)
Find and the URI before the key
@param key $p
@return string/bool string (URI) / false
before($p, ?int $index = NULL)
Find and the URI after the key
@param key $p
@return string/false
after($p)
Find and the URI after the key
@param key $p
@return string/false
afterAll($p)
Get index/position of item in protocol list
@param string $key the prermalink
@return INT list item index
index($key)
Add to item to protocol
@param array $params URI array
@param int $parent parent ID // not used, will be removed, just set 0
@param int $id id
add($params, $parent, $id, $data)
Change paramArr part
@param array $p URI part Array
@return self
part($p)
Will start validating header status
@param array/false $p URI Part Array
@return self
get($p = false)