<commit_before><?php

namespace App\Actions\Album;

use AccessControl;
use App\Models\Album;
use App\Models\Photo;

class Delete
{
	/**
	 * @param string $albumID
	 *
	 * @return bool
	 */
	public function do(string $albumIDs): bool
	{
		$no_error = true;
		// root = unsorted
		if ($albumIDs == '0') {
			$photos = Photo::select_unsorted(Photo::OwnedBy(AccessControl::id()))->get();
			foreach ($photos as $photo) {
				$no_error &= $photo->predelete();
				$no_error &= $photo->delete();
			}

			return $no_error;
		}

		$albums = Album::whereIn('id', explode(',', $albumIDs))->get();

		foreach ($albums as $album) {
			$no_error &= $album->predelete();

			//! We break the tree (because delete() is broken see https://github.com/lazychaser/laravel-nestedset/issues/485)
			Album::where('_lft', '>', $album->_lft)->where('_rgt', '<=', $album->_rgt)->delete();

			$no_error &= $album->delete();
		}

		//? We fix the tree :)
		if (Album::isBroken()) {
			Album::fixTree();
		}

		return $no_error;<commit_msg>Fixes 868 - Delete sub sub sub albums bug (#872)<commit_after><?php

namespace App\Actions\Album;

use AccessControl;
use App\Models\Album;
use App\Models\Photo;
use Illuminate\Support\Facades\Schema;

class Delete
{
	/**
	 * @param string $albumID
	 *
	 * @return bool
	 */
	public function do(string $albumIDs): bool
	{
		$no_error = true;
		// root = unsorted
		if ($albumIDs == '0') {
			$photos = Photo::select_unsorted(Photo::OwnedBy(AccessControl::id()))->get();
			foreach ($photos as $photo) {
				$no_error &= $photo->predelete();
				$no_error &= $photo->delete();
			}

			return $no_error;
		}

		$albums = Album::whereIn('id', explode(',', $albumIDs))->get();

		Schema::disableForeignKeyConstraints();
		foreach ($albums as $album) {
			$no_error &= $album->predelete();

			//! We break the tree (because delete() is broken see https://github.com/lazychaser/laravel-nestedset/issues/485)
			Album::where('_lft', '>', $album->_lft)->where('_rgt', '<=', $album->_rgt)->delete();

			$no_error &= $album->delete();
		}
		Schema::enableForeignKeyConstraints();

		//? We fix the tree :)
		if (Album::isBroken()) {
			Album::fixTree();
		}

		return $no_error;
{'fraction_non_alphanumeric': 0.1977116704805492, 'fraction_numerical': 0.006407322654462242, 'mean_word_length': 3.8794642857142856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5188696', 'n_tokens_mistral': 874, 'n_tokens_neox': 758, 'n_words': 159}