<commit_before><?php

namespace YesWiki\Bazar\Controller;

use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\Guard;
use YesWiki\Core\YesWikiController;

class FormController extends YesWikiController
{
    protected $formManager;

    public function __construct(FormManager $formManager)
    {
        $this->formManager = $formManager;
    }

    public function displayAll($message)
    {
        $forms = $this->formManager->getAll();

        // If there are forms to import
        if (isset($_POST['imported-form'])) {
            foreach ($_POST['imported-form'] as $id => $value) {
                $value = json_decode($value, true);
                $existingForms = multiArraySearch($forms, 'bn_label_nature', $value['bn_label_nature']);
                // If a form with the same name exist, replace it
                if (count($existingForms) > 0) {
                    // Replace with ID of existing formulaire
                    $value['bn_id_nature'] = $existingForms[0]['bn_id_nature'];
                    $this->formManager->update($value);
                } else {
                    $value['bn_id_nature'] = $id;
                    $this->formManager->create($value);
                }
            }

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORM_IMPORT_SUCCESSFULL'], false));
        }

        $values = [];
        if (is_array($forms)) {
            foreach ($forms as $form) {
                $values[$form['bn_id_nature']]['title'] = $form['bn_label_nature'];
                $values[$form['bn_id_nature']]['description'] = $form['bn_description'];
                $values[$form['bn_id_nature']]['canEdit'] = $this->getService(Guard::class)->isAllowed('saisie_formulaire');
                $values[$form['bn_id_nature']]['canDelete'] = $this->wiki->UserIsAdmin();
                $values[$form['bn_id_nature']]['isSemantic'] = isset($form['bn_sem_type']) && $form['bn_sem_type'] !== "";
            }
        }

        return $this->render("@bazar/forms/forms_table.twig", [
            'message' => $message,
            'forms' => $values,
            'loggedUser' => $this->wiki->GetUser()
        ]);
    }

    public function create()
    {
        if( isset($_POST['valider']) ) {
            $this->formManager->create($_POST);

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_NOUVEAU_FORMULAIRE_ENREGISTRE'], false));
        }

        return $this->render("@bazar/forms/forms_form.twig", [
            'formAndListIds' => baz_forms_and_lists_ids()
        ]);
    }

    public function update($id)
    {
        if( isset($_POST['valider']) ) {
            $this->formManager->update($_POST);

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_MODIFIE'], false));
        }

        return $this->render("@bazar/forms/forms_form.twig", [
            'form' => $this->formManager->getOne($id),
            'formAndListIds' => baz_forms_and_lists_ids()
        ]);
    }

    public function delete($id)
    {
        $this->formManager->delete($id);

        return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_ET_FICHES_SUPPRIMES'], false));
    }

    public function empty($id)
    {
        $this->formManager->clear($id);

        return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_VIDE'], false));
    }
}<commit_msg>fix(FormController): checkACL before update, delete, empty<commit_after><?php

namespace YesWiki\Bazar\Controller;

use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\Guard;
use YesWiki\Core\YesWikiController;

class FormController extends YesWikiController
{
    protected $formManager;

    public function __construct(FormManager $formManager)
    {
        $this->formManager = $formManager;
    }

    public function displayAll($message)
    {
        $forms = $this->formManager->getAll();

        // If there are forms to import
        if (isset($_POST['imported-form'])) {
            foreach ($_POST['imported-form'] as $id => $value) {
                $value = json_decode($value, true);
                $existingForms = multiArraySearch($forms, 'bn_label_nature', $value['bn_label_nature']);
                // If a form with the same name exist, replace it
                if (count($existingForms) > 0) {
                    // Replace with ID of existing formulaire
                    $value['bn_id_nature'] = $existingForms[0]['bn_id_nature'];
                    $this->formManager->update($value);
                } else {
                    $value['bn_id_nature'] = $id;
                    $this->formManager->create($value);
                }
            }

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORM_IMPORT_SUCCESSFULL'], false));
        }

        $values = [];
        if (is_array($forms)) {
            foreach ($forms as $form) {
                $values[$form['bn_id_nature']]['title'] = $form['bn_label_nature'];
                $values[$form['bn_id_nature']]['description'] = $form['bn_description'];
                $values[$form['bn_id_nature']]['canEdit'] = $this->getService(Guard::class)->isAllowed('saisie_formulaire');
                $values[$form['bn_id_nature']]['canDelete'] = $this->wiki->UserIsAdmin();
                $values[$form['bn_id_nature']]['isSemantic'] = isset($form['bn_sem_type']) && $form['bn_sem_type'] !== "";
            }
        }

        return $this->render("@bazar/forms/forms_table.twig", [
            'message' => $message,
            'forms' => $values,
            'loggedUser' => $this->wiki->GetUser()
        ]);
    }

    public function create()
    {
        if (isset($_POST['valider'])) {
            $this->formManager->create($_POST);

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_NOUVEAU_FORMULAIRE_ENREGISTRE'], false));
        }

        return $this->render("@bazar/forms/forms_form.twig", [
            'formAndListIds' => baz_forms_and_lists_ids()
        ]);
    }

    public function update($id)
    {
        if ($this->getService(Guard::class)->isAllowed('saisie_formulaire')) {
            if (isset($_POST['valider'])) {
                $this->formManager->update($_POST);

                return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_MODIFIE'], false));
            }

            return $this->render("@bazar/forms/forms_form.twig", [
                'form' => $this->formManager->getOne($id),
                'formAndListIds' => baz_forms_and_lists_ids()
            ]);
        } else {
            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_NEED_ADMIN_RIGHTS'], false));
        }
    }

    public function delete($id)
    {
        if ($this->wiki->UserIsAdmin()) {
            $this->formManager->delete($id);

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_ET_FICHES_SUPPRIMES'], false));
        } else {
            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_NEED_ADMIN_RIGHTS'], false));
        }
    }

    public function empty($id)
    {
        if ($this->wiki->UserIsAdmin()) {
            $this->formManager->clear($id);

            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_FORMULAIRE_VIDE'], false));
        } else {
            return $this->wiki->redirect($this->wiki->href('', '', ['vue' => 'formulaire', 'msg' => 'BAZ_NEED_ADMIN_RIGHTS'], false));
        }
    }
}
{'fraction_non_alphanumeric': 0.18425057063149886, 'fraction_numerical': 0.000507227998985544, 'mean_word_length': 2.4098573281452658, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 155, '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': '2482795', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2349, 'n_words': 361}