Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Navigation_Core
- {
- public $navigation;
- public $headings;
- public $subs;
- public function __construct(){
- }
- /**
- * Creates a new Navigation.
- *
- * @return object
- */
- public static function factory()
- {
- return new Navigation();
- }
- public static function save(){
- // Get kohana,
- $K = Kohana::instance();
- switch( $K->uri->segment(4) ){
- case 'order' :
- $new = json_decode($K->input->post('menu_new'), true);
- $old = json_decode($K->input->post('menu_old'), true);
- // need to have js not fire ajax when old is empty
- if(count($old) == 0) return;
- foreach($new AS $k => $v){
- if($v != $old[$k]){
- $id = str_replace('nid_', '', $k);
- $o = str_replace('mo_', '', $v);
- $nav = ORM::factory('nav', $id);
- $nav->order_num = $o;
- $nav->save();
- } // end if
- } // end foreach
- break;
- default :
- header('Content-type: text/x-javascript');
- echo "alert('bad request')";
- exit();
- break;
- } // end swtich segment(4)
- }
- /**
- * Creates Site Navigation
- *
- * Separates the top and side navs of site.
- *
- * @todo Set the config var for the sites limit
- * @return object consisting of new top and side
- */
- public function siteMenu(){
- // build it
- $this->_build();
- // get ready for the nav
- $navigation = new stdClass();
- // make the top and side navs seperate
- $length = 5;
- $offset = 0;
- $top = array_slice($this->navigation, $offset, $length);
- $side = array_slice($this->navigation, $length);
- // turn the arrays into ol for nav
- $navigation->top = html::ol($top, array('id'=>'top_menu_ol'));
- $navigation->side = html::ol($side, array('id'=>'side_menu_ol'));
- return $navigation;
- } // end get()
- /**
- * Builds the complete HTML ready navigation
- * for the Class
- *
- * @return array of completed navigation
- */
- private function _build(){
- $this->headings = ORM::factory('nav')->getHeadings();
- $this->subs = ORM::factory('nav')->getSubHeadings();
- // set vars in case empty
- $n = array();
- $s = array();
- // get all the nav with no parents
- $navs = $this->headings;
- foreach($navs AS $nav){
- // used to set the att in the anchors
- $n_attr = array(
- 'id' => 'nid_' . $nav->id,
- 'class' => 'menu_heading',
- 'rel' => $nav->window_rel->name,
- 'title' => $nav->page->title
- );
- // check if heading has no link
- empty($nav->page->name) ? // no rel tag in spans
- $n[$nav->id] = html::tag($nav->name, 'span', arr::un_set('rel', $n_attr)):
- $n[$nav->id] = html::anchor($nav->page->name, $nav->name, $n_attr);
- }
- // get all sub navs
- $subs = $this->subs;
- foreach($subs AS $sub){
- // used to set the att in the anchors
- $s_attr = array(
- 'id' => 'nid_' . $sub->id,
- 'class' => 'menu_sub',
- 'rel' => $sub->window_rel->name,
- 'title' => $sub->page->title
- );
- // check if heading has no link
- empty($sub->page->name) ? // no rel tag in spans
- $s[$sub->parent_id][$sub->id] = html::tag($sub->name, 'span', arr::un_set('rel', $s_attr)):
- $s[$sub->parent_id][$sub->id] = html::anchor($sub->page->name, $sub->name, $s_attr);
- }
- // merge the headings with their subs
- foreach($n AS $k => $heading){
- if(array_key_exists($k, $s)){
- $navi[$heading] = $s[$k];
- } else {
- $navi[] = $heading;
- }
- }
- // if there is no navi we have a big problem
- if( ! isset($navi) )
- throw new Outland_Exception('System', 'Could not locate your navigation');
- $this->navigation = $navi;
- }
- }// end class Navigation
Add Comment
Please, Sign In to add comment