Skip to content

Commit

Permalink
Adding method to delete a Nesty object (and optionally all it's child…
Browse files Browse the repository at this point in the history
…ren).

Signed-off-by: Ben Corlett <bencorlett@me.com>
  • Loading branch information
bencorlett committed Jun 15, 2012
1 parent ce343e3 commit 99c233d
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions nesty.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Closure;
use Crud;
use DB;
use Event;
use Exception;
use HTML;
use Str;
Expand Down Expand Up @@ -751,6 +752,89 @@ protected function fill_children(array $children_array = array())
return $this;
}

/*
|--------------------------------------------------------------------------
| Deleting
|--------------------------------------------------------------------------
*/

/**
* Delete a Nesty object from the database. Children items
* will be orphaned; they'll be pushed up to the same level
* as the parent object.
*
* @return bool
*/
public function delete()
{
// Call parent method
$result = parent::delete();

if ($result)
{
// Shift every result 1 to the left
$this->query()
->where(static::$_nesty_cols['left'], 'BETWEEN', DB::raw($this->{static::$_nesty_cols['left']}.' AND '.$this->{static::$_nesty_cols['right']}))
->where(static::$_nesty_cols['tree'], '=', $this->{static::$_nesty_cols['tree']})
->update(array(
static::$_nesty_cols['left'] => DB::raw('`'.static::$_nesty_cols['left'].'` - 1'),
static::$_nesty_cols['right'] => DB::raw('`'.static::$_nesty_cols['right'].'` - 1'),
));

// Move everything outside our right
// limit 2 to the left
$this->query()
->where(static::$_nesty_cols['right'], '>', $this->{static::$_nesty_cols['right']})
->where(static::$_nesty_cols['tree'], '=', $this->{static::$_nesty_cols['tree']})
->update(array(
static::$_nesty_cols['right'] => DB::raw('`'.static::$_nesty_cols['right'].'` - 2'),
));

$this->query()
->where(static::$_nesty_cols['left'], '>', $this->{static::$_nesty_cols['right']})
->where(static::$_nesty_cols['tree'], '=', $this->{static::$_nesty_cols['tree']})
->update(array(
static::$_nesty_cols['right'] => DB::raw('`'.static::$_nesty_cols['right'].'` - 2'),
));
}

return $result;
}

/**
* Delete a Nesty object from the database along with
* all of it's children. Use with care!
*
* @return bool
*/
public function delete_with_children()
{
// Our delete methodology is different
// to normal here... So we are putting
// a bunch of code and callbacks so our API
// is similar to the delete() one.
$query = $this->query()
->where(static::$_nesty_cols['left'], 'BETWEEN', DB::raw($this->{static::$_nesty_cols['left']}.' AND '.$this->{static::$_nesty_cols['right']}))
->where(static::$_nesty_cols['tree'], '=', $this->{static::$_nesty_cols['tree']});

// Callbacks
$query = $this->before_delete($query);
$result = $query->delete();
$result = $this->after_delete($result);

if (static::$_events)
{
// Fire delete event
Event::fire(static::event().'.delete', array($this));
}

// Remove our gap we created
if ($result)
{
$this->gap($this->{static::$_nesty_cols['left']}, - ($this->size() + 1));
}
}

/*
|--------------------------------------------------------------------------
| Debugging
Expand Down

0 comments on commit 99c233d

Please sign in to comment.