<commit_before><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
use Log;
use Auth;

use App\Category;
use App\Product;

class CategoriesController extends Controller
{
    public function __construct()
    {
        $this->commonInit([
            'reference_class' => 'App\\Category'
        ]);
    }

    public function index()
    {
        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            abort(503);
        }

        $categories = Category::where('id', '!=', 'non-specificato')->where('parent_id', '=', null)->get();

        return view('categories.edit', ['categories' => $categories]);
    }

    public function store(Request $request)
    {
        DB::beginTransaction();

        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            return $this->errorResponse(_i('Non autorizzato'));
        }

        $category = new Category();
        $category->name = $request->input('name');

        $parent = $request->input('parent_id');
        if ($parent != 'null') {
            $category->parent_id = $parent;
        } else {
            $category->parent_id = null;
        }

        $category->save();

        return $this->successResponse([
            'id' => $category->id,
            'parent' => $category->parent_id,
            'name' => $category->name,
        ]);
    }

    private function updateRecursive($data, $parent, &$accumulator)
    {
        foreach ($data as $category) {
            $c = null;

            if (empty($category['name'])) {
                continue;
            }

            if (isset($category['id'])) {
                if (in_array($category['id'], $accumulator)) {
                    Log::error('Itero ricorsivamente categoria già gestita, salto');
                    continue;
                }

                $c = Category::find($category['id']);
            }

            if (is_null($c)) {
                $c = new Category();
            }

            $c->name = $category['name'];
            $c->parent_id = $parent;
            $c->save();
            $accumulator[] = $c->id;

            if (array_key_exists('children', $category)) {
                $this->updateRecursive($category['children'], $c->id, $accumulator);
            }
        }
    }

    public function update(Request $request, $id)
    {
        DB::beginTransaction();

        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            return $this->errorResponse(_i('Non autorizzato'));
        }

        $data = $request->input('serialized');
        $accumulator = ['non-specificato'];

        $this->updateRecursive($data, null, $accumulator);
        Product::whereNotIn('category_id', $accumulator)->update(['category_id' => 'non-specificato']);
        Category::whereNotIn('id', $accumulator)->delete();

        return $this->successResponse();
    }
}<commit_msg>fix in rimozione categorie prodotti<commit_after><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
use Log;
use Auth;

use App\Category;
use App\Product;

class CategoriesController extends Controller
{
    public function __construct()
    {
        $this->commonInit([
            'reference_class' => 'App\\Category'
        ]);
    }

    public function index()
    {
        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            abort(503);
        }

        $categories = Category::where('id', '!=', 'non-specificato')->where('parent_id', '=', null)->get();

        return view('categories.edit', ['categories' => $categories]);
    }

    public function store(Request $request)
    {
        DB::beginTransaction();

        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            return $this->errorResponse(_i('Non autorizzato'));
        }

        $category = new Category();
        $category->name = $request->input('name');

        $parent = $request->input('parent_id');
        if ($parent != 'null') {
            $category->parent_id = $parent;
        } else {
            $category->parent_id = null;
        }

        $category->save();

        return $this->successResponse([
            'id' => $category->id,
            'parent' => $category->parent_id,
            'name' => $category->name,
        ]);
    }

    private function updateRecursive($data, $parent, &$accumulator)
    {
        foreach ($data as $category) {
            $c = null;

            if (empty($category['name'])) {
                continue;
            }

            if (isset($category['id'])) {
                if (in_array($category['id'], $accumulator)) {
                    Log::error('Itero ricorsivamente categoria già gestita, salto');
                    continue;
                }

                $c = Category::find($category['id']);
            }

            if (is_null($c)) {
                $c = new Category();
            }

            $c->name = $category['name'];
            $c->parent_id = $parent;
            $c->save();
            $accumulator[] = $c->id;

            if (array_key_exists('children', $category)) {
                $this->updateRecursive($category['children'], $c->id, $accumulator);
            }
        }
    }

    public function update(Request $request, $id)
    {
        DB::beginTransaction();

        $user = Auth::user();
        if ($user->can('categories.admin', $user->gas) == false) {
            return $this->errorResponse(_i('Non autorizzato'));
        }

        $data = $request->input('serialized');
        $accumulator = ['non-specificato'];

        $this->updateRecursive($data, null, $accumulator);

        $orphaned_products = Product::withoutGlobalScopes()->whereNotIn('category_id', $accumulator)->get();
        foreach($orphaned_products as $op) {
            $op->category_id = 'non-specificato';
            $op->save();
        }

        Category::whereNotIn('id', $accumulator)->delete();

        return $this->successResponse();
    }
}
{'fraction_non_alphanumeric': 0.1685140562248996, 'fraction_numerical': 0.0009638554216867469, 'mean_word_length': 1.983229516051749, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, 'https://': 0, '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': '2006956', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1719, 'n_words': 326}