daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Navigation_Core
  4. {
  5.     public $navigation;
  6.     public $headings;
  7.     public $subs;
  8.  
  9.     public function __construct(){
  10.  
  11.     }
  12.  
  13.     /**
  14.      * Creates a new Navigation.
  15.      *
  16.      * @return  object
  17.      */
  18.     public static function factory()
  19.     {
  20.         return new Navigation();
  21.     }
  22.  
  23.     public static function save(){
  24.  
  25.         // Get kohana,
  26.         $K = Kohana::instance();
  27.  
  28.         switch( $K->uri->segment(4) ){
  29.  
  30.             case 'order' :
  31.  
  32.                 $new = json_decode($K->input->post('menu_new'), true);
  33.                 $old = json_decode($K->input->post('menu_old'), true);
  34.  
  35.                 // need to have js not fire ajax when old is empty
  36.                 if(count($old) == 0) return;
  37.  
  38.                 foreach($new AS $k => $v){
  39.                     if($v != $old[$k]){
  40.                         $id = str_replace('nid_', '', $k);
  41.                         $o  = str_replace('mo_', '', $v);
  42.                         $nav = ORM::factory('nav', $id);
  43.                         $nav->order_num = $o;
  44.                         $nav->save();
  45.                     } // end if
  46.  
  47.                 } // end foreach
  48.             break;
  49.  
  50.             default :
  51.                 header('Content-type: text/x-javascript');
  52.                 echo "alert('bad request')";
  53.                 exit();
  54.             break;
  55.         } // end swtich segment(4)
  56.        
  57.     }
  58.  
  59.     /**
  60.      * Creates Site Navigation
  61.      *
  62.      *  Separates the top and side navs of site.
  63.      *
  64.      * @todo Set the config var for the sites limit
  65.      * @return object consisting of new top and side
  66.      */
  67.  
  68.     public function siteMenu(){
  69.        
  70.         // build it
  71.         $this->_build();
  72.  
  73.         // get ready for the nav
  74.         $navigation = new stdClass();
  75.  
  76.         // make the top and side navs seperate
  77.         $length = 5;
  78.         $offset = 0;
  79.         $top  = array_slice($this->navigation, $offset, $length);
  80.         $side = array_slice($this->navigation, $length);
  81.  
  82.         // turn the arrays into ol for nav
  83.         $navigation->top  = html::ol($top, array('id'=>'top_menu_ol'));
  84.         $navigation->side = html::ol($side, array('id'=>'side_menu_ol'));
  85.  
  86.         return $navigation;
  87.        
  88.     } // end get()
  89.  
  90.     /**
  91.      * Builds the complete HTML ready navigation
  92.      * for the Class
  93.      *
  94.      * @return array of completed navigation
  95.      */
  96.  
  97.     private function _build(){
  98.  
  99.         $this->headings = ORM::factory('nav')->getHeadings();
  100.         $this->subs     = ORM::factory('nav')->getSubHeadings();
  101.        
  102.         // set vars in case empty
  103.         $n = array();
  104.         $s = array();
  105.  
  106.         // get all the nav with no parents
  107.         $navs = $this->headings;
  108.         foreach($navs AS $nav){
  109.  
  110.             // used to set the att in the anchors
  111.             $n_attr = array(
  112.                 'id'    => 'nid_' . $nav->id,
  113.                 'class' => 'menu_heading',
  114.                 'rel'   => $nav->window_rel->name,
  115.                 'title' => $nav->page->title
  116.             );
  117.  
  118.             // check if heading has no link
  119.             empty($nav->page->name) ?                    // no rel tag in spans
  120.             $n[$nav->id] = html::tag($nav->name, 'span', arr::un_set('rel', $n_attr)):
  121.                 $n[$nav->id] = html::anchor($nav->page->name, $nav->name, $n_attr);
  122.  
  123.         }
  124.  
  125.         // get all sub navs
  126.         $subs = $this->subs;
  127.         foreach($subs AS $sub){
  128.  
  129.             // used to set the att in the anchors
  130.             $s_attr = array(
  131.                 'id'    => 'nid_' . $sub->id,
  132.                 'class' => 'menu_sub',
  133.                 'rel'   => $sub->window_rel->name,
  134.                 'title' => $sub->page->title
  135.             );
  136.  
  137.             // check if heading has no link
  138.             empty($sub->page->name) ?                                     // no rel tag in spans
  139.             $s[$sub->parent_id][$sub->id] = html::tag($sub->name, 'span', arr::un_set('rel', $s_attr)):
  140.             $s[$sub->parent_id][$sub->id] = html::anchor($sub->page->name, $sub->name, $s_attr);
  141.  
  142.         }
  143.  
  144.         // merge the headings with their subs
  145.         foreach($n AS $k => $heading){
  146.  
  147.             if(array_key_exists($k, $s)){
  148.                 $navi[$heading] = $s[$k];
  149.             } else {
  150.                 $navi[] = $heading;
  151.             }
  152.  
  153.         }
  154.  
  155.         // if there is no navi we have a big problem
  156.         if( ! isset($navi) )
  157.             throw new Outland_Exception('System', 'Could not locate your navigation');
  158.  
  159.             $this->navigation = $navi;
  160.     }
  161.  
  162. }// end class Navigation
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top