| 1: | <?php |
| 2: | |
| 3: | namespace Baril\Bonsai\Concerns; |
| 4: | |
| 5: | use Baril\Bonsai\TreeException; |
| 6: | |
| 7: | trait ManagesClosures |
| 8: | { |
| 9: | |
| 10: | |
| 11: | |
| 12: | public static function bootManagesClosures() |
| 13: | { |
| 14: | static::saving(function ($item) { |
| 15: | $item->checkIfParentIdIsValid(); |
| 16: | }); |
| 17: | |
| 18: | static::created(function ($item) { |
| 19: | $item->createSelfClosure(); |
| 20: | $item->attachSubtree(); |
| 21: | }); |
| 22: | |
| 23: | static::updated(function ($item) { |
| 24: | |
| 25: | |
| 26: | if ($item->isDirty($item->getParentForeignKeyName())) { |
| 27: | $item->detachSubtree(); |
| 28: | $item->attachSubtree(); |
| 29: | } |
| 30: | }); |
| 31: | |
| 32: | static::deleting(function ($item) { |
| 33: | $item->checkIfDeletable(); |
| 34: | }); |
| 35: | |
| 36: | static::deleted(function ($item) { |
| 37: | |
| 38: | |
| 39: | if (!property_exists($item, 'forceDeleting') || $item->forceDeleting) { |
| 40: | $item->deleteClosures(); |
| 41: | } |
| 42: | }); |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | protected function checkIfDeletable() |
| 52: | { |
| 53: | if ($this->children()->exists()) { |
| 54: | throw new TreeException('Can\'t delete an item with children!'); |
| 55: | } |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | protected function checkIfParentIdIsValid() |
| 66: | { |
| 67: | if (is_null($parentKey = $this->getParentKey())) { |
| 68: | return; |
| 69: | } |
| 70: | |
| 71: | if (!$this->parent()->exists()) { |
| 72: | throw new TreeException('The item\'s parent doesn\'t exist!'); |
| 73: | } |
| 74: | |
| 75: | if ( |
| 76: | $parentKey == $this->getKey() |
| 77: | || $this->descendingClosures()->whereDescendant($parentKey)->exists() |
| 78: | ) { |
| 79: | throw new TreeException( |
| 80: | 'Redundancy error! The item\'s parent can\'t be the item itself or one of its descendants.' |
| 81: | ); |
| 82: | } |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | protected function createSelfClosure() |
| 91: | { |
| 92: | return $this->newClosureQuery()->insert([ |
| 93: | 'ancestor_id' => $this->getKey(), |
| 94: | 'descendant_id' => $this->getKey(), |
| 95: | 'depth' => 0, |
| 96: | ]); |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | protected function attachSubtree() |
| 108: | { |
| 109: | if (! $parentKey = $this->getParentKey()) { |
| 110: | return; |
| 111: | } |
| 112: | |
| 113: | $connection = $this->getConnection(); |
| 114: | $grammar = $connection->getQueryGrammar(); |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | $select = $this->newClosureQuery('descendants') |
| 129: | ->selfCrossJoin('ancestors') |
| 130: | ->where('descendants.ancestor_id', $this->getKey()) |
| 131: | ->where('ancestors.descendant_id', $parentKey) |
| 132: | ->select( |
| 133: | 'descendants.descendant_id', |
| 134: | 'ancestors.ancestor_id', |
| 135: | $connection->raw(sprintf( |
| 136: | '%s + %s + 1', |
| 137: | $grammar->wrap('descendants.depth'), |
| 138: | $grammar->wrap('ancestors.depth') |
| 139: | )) |
| 140: | ); |
| 141: | |
| 142: | return $this->newClosureQuery() |
| 143: | ->insertUsing(['descendant_id', 'ancestor_id', 'depth'], $select); |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | protected function detachSubtree() |
| 154: | { |
| 155: | return $this->deleteClosures('>'); |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | protected function deleteClosures($operator = null) |
| 168: | { |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | $query = $this->newClosureQuery('closures_to_delete') |
| 176: | ->selfJoin('descendants', 'descendant_id') |
| 177: | ->where('descendants.ancestor_id', $this->id) |
| 178: | |
| 179: | ->when($operator, function ($query, $operator) { |
| 180: | $query->whereColumn('closures_to_delete.depth', $operator, 'descendants.depth'); |
| 181: | }); |
| 182: | |
| 183: | return $query->delete(); |
| 184: | } |
| 185: | } |
| 186: | |