public function save($sample = FALSE) { if ($this->loaded === FALSE) { $this->save(); $new_item = ORM::factory('navigation_item'); $new_item->navigation_id = $this->id; $new_item->fk_site = $this->fk_site; $new_item->display_name = 'ROOT'; $new_item->type = 'none'; $new_item->data = 0; $new_item->local_parent = 0; $new_item->save(); $this->root_id = $new_item->id; if ($sample) { $new_item->clear(); $new_item->navigation_id = $this->id; $new_item->fk_site = $this->fk_site; $new_item->display_name = 'Sample list item'; $new_item->type = 'none'; $new_item->data = ''; $new_item->local_parent = $navigation->root_id; $new_item->save(); $new_item->clear(); $new_item->navigation_id = $this->id; $new_item->fk_site = $this->fk_site; $new_item->display_name = 'Link to Home'; $new_item->type = 'page'; $new_item->data = 'home'; $new_item->local_parent = $navigation->root_id; $new_item->save(); $new_item->clear(); $new_item->navigation_id = $this->id; $new_item->fk_site = $this->fk_site; $new_item->display_name = 'External Google Link'; $new_item->type = 'url'; $new_item->data = 'google.com'; $new_item->local_parent = $navigation->root_id; $new_item->save(); # Update left and right values Tree::rebuild_tree('navigation_item', $navigation->root_id, $this->fk_site, '1'); } } return parent::save(); }
public function add() { if (!$_POST) { die('Nothing Sent.'); } $navigation = $this->get_parent('navigation'); $_POST['data'] = empty($_POST['data']) ? '' : $_POST['data']; # if for any reason local_parent is null, just add to root. $_POST['local_parent'] = empty($_POST['local_parent']) ? $navigation->root_id : $_POST['local_parent']; $new_item = ORM::factory('navigation_item'); $new_item->navigation_id = $this->pid; $new_item->fk_site = $this->site_id; $new_item->display_name = $_POST['item']; $new_item->type = $_POST['type']; $new_item->data = $_POST['data']; $new_item->local_parent = $_POST['local_parent']; $new_item->save(); # Update left and right values Tree::rebuild_tree('navigation_item', $navigation->root_id, $this->site_id, '1'); die("{$new_item->id}"); # output to javascript }
public static function rebuild_tree($model, $local_parent, $site_id, $left) { # the right value of this node is the left value + 1 $right = $left + 1; $navigation_items = ORM::factory($model)->where(array('fk_site' => $site_id, 'local_parent' => $local_parent))->orderby('position', 'asc')->find_all(); foreach ($navigation_items as $item) { # recursive function for each child of this node # $right is the current right value; # incremented by the rebuild_tree function $right = Tree::rebuild_tree($model, $item->id, $site_id, $right); } # we've got the left value, and now that we've processed # the children of this node we also know the right value $item = ORM::factory($model, $local_parent); $item->lft = $left; $item->rgt = $right; $item->save(); # return the right value of this node + 1 return $right + 1; }