Skip to content

Commit

Permalink
Heaps of changes...
Browse files Browse the repository at this point in the history
  • Loading branch information
bencorlett committed May 1, 2012
1 parent 5a0efba commit 3ae37de
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion nesty.php
Expand Up @@ -13,6 +13,7 @@

use DB;
use Crud;
use Log;

/**
* Nesty model class.
Expand All @@ -30,7 +31,7 @@ class Nesty extends Crud
*
* @var array
*/
protected static $nesty_cols = array(
public static $nesty_cols = array(
'left' => 'lft',
'right' => 'rgt',
'name' => 'name',
Expand Down Expand Up @@ -189,6 +190,9 @@ public function child_of(Nesty &$parent, $position)
// nesty)
$this->gap($this->{static::$nesty_cols['left']});

// Reload parent
$parent->reload();

$this->save();
}

Expand Down Expand Up @@ -502,6 +506,55 @@ protected function fill_children(array $children_array = array())
return $this;
}

/*
|--------------------------------------------------------------------------
| Regenerating from array
|--------------------------------------------------------------------------
*/

public static function create_from_hierarchy(array $items)
{
DB::query('TRUNCATE menus');

try
{
// Firstly, create a root model
$root = new static(array(
'name' => 'Root Item',
));
$root->root();

// Log::nesty(print_r($items, true));

// Loop through items
foreach ($items as $item)
{
$root = static::find(1);
static::insert_recursive($item, $root);
}
}
catch (\Exception $e)
{
Log::error($e->getMessage());
}
}

protected static function insert_recursive(array $item = array(), Nesty &$parent)
{
$item_m = new static(array(
'name' => $item['id']
));
$item_m->last_child_of($parent);

if (isset($item['children']) and is_array($item['children']) and count($item['children']) > 0)
{
foreach ($item['children'] as $child)
{
static::insert_recursive($child, $item_m);
}
}
}

/*
|-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
| Nesty helper methods
Expand Down

0 comments on commit 3ae37de

Please sign in to comment.