Commit d33f986e authored by Theo Lecolley's avatar Theo Lecolley
Browse files

Passage de la page discipline en vue.js et modification de la discipline directement dans la table.

parent 837571f5
Loading
Loading
Loading
Loading
Loading
+407 −0
Changes for front/Discipline/Discipline.vue: 407 added lines, 0 removed lines.
Original line number Diff line number Diff line
<script setup>
import { ref, computed, onMounted } from 'vue';
import DisciplineForm from './DisciplineForm/DisciplineForm.vue';

const disciplines = ref([]);
const entetes = ref([]);

const pageCourante = ref(1);
const lignesParPage = ref(10);
const recherche = ref('');
const totalRows = ref(0);

const disciplineEnEdition = ref(null);
const disciplineEnModal = ref(null);
const showModal = ref(false);

const { canEdit } = defineProps({
    canEdit: Boolean
});

const fields = computed(() => [
    { key: 'sourceCode', label: 'Code' },
    { key: 'libelleCourt', label: 'Libellé court' },
    { key: 'libelleLong', label: 'Libellé long' },
    { key: 'codesCorresp1', label: entetes.value[1] },
    { key: 'codesCorresp2', label: entetes.value[2] },
    { key: 'codesCorresp3', label: entetes.value[3] },
    { key: 'codesCorresp4', label: entetes.value[4] },
    { key: 'actions', label: 'Src/Actions' }
]);

const lignesParPageOptions = [10, 25, 50, 100];

async function chargerDisciplines(pageId = null) {
    try {
        const response = await unicaenVue.axios.get(
            unicaenVue.url('discipline/index-json')
        );

        entetes.value = response.data[0][0];
        disciplines.value = response.data[0][1];
        totalRows.value = disciplines.value.length;

        if (pageId) {
            const index = disciplines.value.findIndex(
                discipline => discipline.id === pageId
            );

            if (index !== -1) {
                pageCourante.value =
                    Math.floor(index / lignesParPage.value) + 1;
            }
        }
    } catch (error) {
        console.error(error);
    }
}

function filtrerDisciplines(items) {
    totalRows.value = items.length;
    pageCourante.value = 1;
}

async function sauvegarderDiscipline(discipline) {
    if (!discipline) {
        return false;
    }

    const data = new URLSearchParams();

    if (discipline.id) {
        data.append('id', discipline.id);
    }

    data.append('source-code', discipline.sourceCode ?? '');
    data.append('libelle-court', discipline.libelleCourt ?? '');
    data.append('libelle-long', discipline.libelleLong ?? '');
    data.append('codes-corresp-1', discipline.codesCorresp1 ?? '');
    data.append('codes-corresp-2', discipline.codesCorresp2 ?? '');
    data.append('codes-corresp-3', discipline.codesCorresp3 ?? '');
    data.append('codes-corresp-4', discipline.codesCorresp4 ?? '');

    try {
        const response = await unicaenVue.axios.post(
            unicaenVue.url('discipline/modifier-json'),
            data
        );
        console.log(response);
        if (!response.data.success) {
            console.error(response.data.errors);
            return false;
        }else{
            await chargerDisciplines(discipline.id);
            return true;
        }

    } catch (error) {
        console.error(error);
        return false;
    }
}

async function supprimerDiscipline(discipline) {
    if (!discipline?.id) return;

    if (
        !confirm(
            `Voulez-vous vraiment supprimer la discipline "${discipline.libelleCourt}" ?`
        )
    ) {
        return;
    }

    try {
        const data = new URLSearchParams();
        data.append('id', discipline.id);

        const response = await unicaenVue.axios.post(
            unicaenVue.url('discipline/supprimer-json'),
            data
        );

        if (response.data.success) {
            await chargerDisciplines();
        }
    } catch (error) {
        console.error(error);
    }
}

function ouvrirModalAjout() {
    disciplineEnModal.value = {
        sourceCode: '',
        libelleCourt: '',
        libelleLong: '',
        codesCorresp1: '',
        codesCorresp2: '',
        codesCorresp3: '',
        codesCorresp4: ''
    };

    showModal.value = true;
}

function fermerModal() {
    showModal.value = false;
    disciplineEnModal.value = null;
}

function truncCodesCorresp(value) {
    if (!value) return '';

    const texte = value.replaceAll(',', ', ');

    return texte.length > 23
        ? `${texte.substring(0, 22)}...`
        : texte;
}

async function enregistrer() {
    if (await sauvegarderDiscipline(disciplineEnEdition.value)) {
        disciplineEnEdition.value = null;
    }
}

async function enregistrerAjout(discipline) {
    if (await sauvegarderDiscipline(discipline)) {
        fermerModal();
    }
}
onMounted(async()=> {
    await chargerDisciplines()
})
</script>

<template>
    <h1 class="page-header">
        Gestion des disciplines
    </h1>

    <div class="d-flex justify-content-between align-items-center my-2">
        <div class="d-flex align-items-center">
            <span class="me-2">Afficher</span>

            <b-form-select
                v-model="lignesParPage"
                :options="lignesParPageOptions"
                size="sm"
                class="w-auto"
            />

            <span class="ms-2">éléments</span>
        </div>

        <b-form-input
            v-model="recherche"
            type="search"
            placeholder="Rechercher..."
            size="sm"
            class="w-auto"
        />
    </div>

    <b-table
        :items="disciplines"
        :fields="fields"
        :filter="recherche"
        :per-page="lignesParPage"
        :current-page="pageCourante"
        @filtered="filtrerDisciplines"
        bordered
        class="table b-table mb-0"
        responsive
    >
        <template #cell(sourceCode)="data">
            <div style="text-align: center">
                <b-form-textarea
                    v-if="disciplineEnEdition?.id === data.item.id"
                    v-model="disciplineEnEdition.sourceCode"
                    size="sm"
                    rows="1"
                    max-rows="10"
                    no-resize
                />

                <span v-else>
                    {{ data.value }}
                </span>
            </div>
        </template>

        <template #cell(libelleCourt)="data">
            <b-form-textarea
                v-if="disciplineEnEdition?.id === data.item.id"
                v-model="disciplineEnEdition.libelleCourt"
                rows="1"
                max-rows="10"
                no-resize
            />

            <span v-else>
                {{ data.value }}
            </span>
        </template>

        <template #cell(libelleLong)="data">
            <b-form-textarea
                v-if="disciplineEnEdition?.id === data.item.id"
                v-model="disciplineEnEdition.libelleLong"
                rows="1"
                max-rows="10"
                no-resize
            />

            <span v-else>
                {{ data.value }}
            </span>
        </template>

        <template
            v-for="champ in [
                'codesCorresp1',
                'codesCorresp2',
                'codesCorresp3'
            ]"
            #[`cell(${champ})`]="data"
        >
            <b-form-textarea
                v-if="disciplineEnEdition?.id === data.item.id"
                v-model="disciplineEnEdition[champ]"
                size="sm"
                rows="1"
                max-rows="10"
                no-resize
            />

            <span
                v-else
                :title="data.value"
            >
                {{ truncCodesCorresp(data.value) }}
            </span>
        </template>

        <template #cell(codesCorresp4)="data">
            <b-form-textarea
                v-if="disciplineEnEdition?.id === data.item.id"
                v-model="disciplineEnEdition.codesCorresp4"
                rows="1"
                max-rows="10"
                no-resize
            />

            <span
                v-else
                :title="data.value"
            >
                {{ truncCodesCorresp(data.value) }}
            </span>
        </template>

        <template #cell(actions)="data">
            <template v-if="disciplineEnEdition?.id === data.item.id">
                <div style="text-align: center">
                    <b-button
                        variant="link"
                        style="padding: 2px"
                        @click="enregistrer"
                    >
                        <i
                            class="fas fa-check"
                            style="font-size: 1.2rem;"
                        />
                    </b-button>

                    <b-button
                        variant="link"
                        style="padding: 2px"
                        @click="disciplineEnEdition = null"
                    >
                        <i
                            class="fas fa-xmark"
                            style="font-size: 1.2rem;"
                        />
                    </b-button>
                </div>
            </template>

            <template
                v-else-if="!data.item.sourceImportable && canEdit"
            >
                <div style="text-align: center">
                    <b-button
                        variant="link"
                        style="padding: 2px"
                        @click="disciplineEnEdition = { ...data.item }"
                    >
                        <i
                            class="fas fa-pen-to-square"
                            style="font-size: 0.9rem;"
                        />
                    </b-button>

                    <b-button
                        variant="link"
                        style="padding: 2px"
                        @click="supprimerDiscipline(data.item)"
                    >
                        <i
                            class="fas fa-trash-can"
                            style="font-size: 0.9rem;"
                        />
                    </b-button>
                </div>
            </template>

            <span v-else>
                {{ data.item.source }}
            </span>
        </template>
    </b-table>

    <div class="d-flex justify-content-between align-items-center mb-1">
        <span>
            Affichage de l'élément
            {{ totalRows ? (pageCourante - 1) * lignesParPage + 1 : 0 }}
            à
            {{ Math.min(pageCourante * lignesParPage, totalRows) }}
            sur
            {{ totalRows }}
        </span>

        <b-pagination
            v-model="pageCourante"
            :total-rows="totalRows"
            :per-page="lignesParPage"
            first-text="Premier"
            prev-text="Précédent"
            next-text="Suivant"
            last-text="Dernier"
            class="mb-0"
        />
    </div>

    <b-button
        v-if="canEdit"
        variant="primary"
        size="sm"
        @click="ouvrirModalAjout"
    >
        <i class="fas fa-plus" />
        Ajout d'une nouvelle discipline
    </b-button>

    <DisciplineForm
        :show="showModal"
        :discipline="disciplineEnModal"
        @close="fermerModal"
        @save="enregistrerAjout"
    />
</template>

<style scoped>
.table th {
    font-weight: bold;
}
</style>
 No newline at end of file
+197 −0
Changes for front/Discipline/DisciplineForm/DisciplineForm.vue: 197 added lines, 0 removed lines.
Original line number Diff line number Diff line
<script setup>
import { ref, watch } from 'vue';

const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },

    discipline: {
        type: Object,
        default: null
    }
});

const emit = defineEmits(['close', 'save']);

const formulaire = ref({
    sourceCode: '',
    libelleCourt: '',
    libelleLong: '',
    codesCorresp1: '',
    codesCorresp2: '',
    codesCorresp3: '',
    codesCorresp4: ''
});

watch(
    () => props.discipline,
    (discipline) => {
        formulaire.value = discipline
            ? { ...discipline }
            : {
                sourceCode: '',
                libelleCourt: '',
                libelleLong: '',
                codesCorresp1: '',
                codesCorresp2: '',
                codesCorresp3: '',
                codesCorresp4: ''
            };
    },
    { immediate: true }
);

function enregistrer() {

    emit('save', formulaire.value);
    emit('close');
}
</script>

<template>
    <div
        v-if="show"
        class="modal-overlay"
        @click.self="emit('close')"
    >
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <h2 class="modal-title">
                        Ajout d'une discipline
                    </h2>

                    <button
                        type="button"
                        class="btn-close"
                        @click="emit('close')"
                    ></button>
                </div>

                <div class="modal-body">

                    <div class="mb-2">
                        <label class="form-label">
                            Code
                        </label>

                        <input
                            v-model="formulaire.sourceCode"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Libellé court
                        </label>

                        <input
                            v-model="formulaire.libelleCourt"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Libellé long
                        </label>

                        <input
                            v-model="formulaire.libelleLong"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Section(s) CNU Apogée
                        </label>

                        <input
                            v-model="formulaire.codesCorresp1"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Section(s) CNU Harpège
                        </label>

                        <input
                            v-model="formulaire.codesCorresp2"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Spécialité Harpège
                        </label>

                        <input
                            v-model="formulaire.codesCorresp3"
                            type="text"
                            class="form-control"
                        >
                    </div>

                    <div class="mb-2">
                        <label class="form-label">
                            Discipline du 2nd degré
                        </label>

                        <input
                            v-model="formulaire.codesCorresp4"
                            type="text"
                            class="form-control"
                        >
                    </div>

                </div>

                <div class="modal-footer">
                    <button
                        type="button"
                        class="btn btn-primary"
                        @click="enregistrer"
                    >
                        Enregistrer
                    </button>

                    <button
                        type="button"
                        class="btn btn-secondary"
                        @click="emit('close')"
                    >
                        Fermer
                    </button>
                </div>

            </div>
        </div>
    </div>
</template>

<style scoped>
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);

    display: flex;
    align-items: center;
    justify-content: center;

    z-index: 99999;
}
</style>
 No newline at end of file
+14 −2
Changes for module/OffreFormation/config/module.config.php: 14 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -230,6 +230,18 @@ return [
                    ],
                    'action'      => 'supprimer',
                ],
                'index-json' => [
                    'route'  => '/index-json',
                    'action' => 'index-json',
                ],
                'modifier-json' => [
                    'route'  => '/modifier-json',
                    'action' => 'modifier-json',
                ],
                'supprimer-json' => [
                    'route'  => '/supprimer-json',
                    'action' => 'supprimer-json',
                ],
            ],
        ],
        'type-intervention' => [
@@ -516,7 +528,7 @@ return [
        /* discipline */
        [
            'controller' => DisciplineController::class,
            'action'     => ['index'],
            'action'     => ['index', 'index-json'],
            'privileges' => [
                Privileges::DISCIPLINE_GESTION,
            ],
@@ -530,7 +542,7 @@ return [
        ],
        [
            'controller' => DisciplineController::class,
            'action'     => ['saisir', 'supprimer'],
            'action'     => ['saisir', 'supprimer', 'modifier-json', 'supprimer-json'],
            'privileges' => [
                Privileges::DISCIPLINE_EDITION,
            ],
+155 −2
Changes for module/OffreFormation/src/Controller/DisciplineController.php: 155 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Application\Service\Traits\ParametresServiceAwareTrait;
use Application\Service\Traits\SourceServiceAwareTrait;
use OffreFormation\Entity\Db\Discipline;
use OffreFormation\Service\Traits\DisciplineServiceAwareTrait;
use UnicaenVue\View\Model\AxiosModel;


/**
@@ -23,7 +24,6 @@ class DisciplineController extends AbstractController
    use ParametresServiceAwareTrait;



    public function indexAction()
    {
        $this->em()->getFilters()->enable('historique')->init([
@@ -45,6 +45,61 @@ class DisciplineController extends AbstractController
        return compact('disciplines', 'libellesCodesCorresp');
    }

    public function indexJsonAction()
    {
        $this->em()->getFilters()->enable('historique')->init([
            Discipline::class,
        ]);

        $qb = $this->getServiceDiscipline()->initQuery()[0];

        $this->getServiceDiscipline()->join(
            $this->getServiceSource(),
            $qb,
            'source',
            true
        );

        $disciplines = $this->getServiceDiscipline()->getList($qb);
        $disciplinesListe = [];
        $result = [];

        //Libelle colonnes
        $libellesCodesCorresp = [];
        for ($i = 1; $i <= 4; $i++) {
            $lcc = $this->getServiceParametres()->get('discipline_codes_corresp_' . $i . '_libelle');
            if ($lcc) {
                $libellesCodesCorresp[$i] = $lcc;
            }
        }


        //Données tableau disciplines
        foreach ($disciplines as $discipline) {
            $disciplinesListe[] = [
                'id' => $discipline->getId(),
                'sourceCode' => $discipline->getSourceCode(),
                'libelleCourt' => $discipline->getLibelleCourt(),
                'libelleLong' => $discipline->getLibelleLong(),
                'codesCorresp1' => $discipline->getCodesCorresp1(),
                'codesCorresp2' => $discipline->getCodesCorresp2(),
                'codesCorresp3' => $discipline->getCodesCorresp3(),
                'codesCorresp4' => $discipline->getCodesCorresp4(),
                'source' => (string)$discipline->getSource(),
                'sourceImportable' => $discipline->getSource()->getImportable(),
            ];
        }

        $sourceImportable = $discipline->getSource()->getImportable();

        $result[] = [
            $libellesCodesCorresp,
            $disciplinesListe,
            $sourceImportable
        ];

        return new AxiosModel($result);
    }


    public function voirAction()
@@ -53,7 +108,6 @@ class DisciplineController extends AbstractController
    }



    public function saisirAction()
    {
        $discipline = $this->getEvent()->getParam('discipline');
@@ -85,7 +139,60 @@ class DisciplineController extends AbstractController
        return compact('form', 'title', 'errors');
    }

    public function modifierJsonAction()
    {
        $request = $this->getRequest();

        if (!$request->isPost()) {
            return new AxiosModel([
                'success' => false,
                'errors' => ['Méthode HTTP non autorisée.'],
            ]);
        }

        $id = $request->getPost('id');

        try {
            if ($id) {
                // Modification
                $discipline = $this->getServiceDiscipline()->get($id);

                if (!$discipline) {
                    return new AxiosModel([
                        'success' => false,
                        'errors' => ['Discipline introuvable.'],
                    ]);
                }
            } else {
                // Création
                $discipline = $this->getServiceDiscipline()->newEntity();
            }

            $form = $this->getFormDiscipline();

            $form->bind($discipline);
            $form->setData($request->getPost());

            if (!$form->isValid()) {
                return new AxiosModel([
                    'success' => false,
                    'errors' => $form->getMessages(),
                ]);
            }

            $this->getServiceDiscipline()->save($discipline);

            return new AxiosModel([
                'success' => true,
                'id' => $discipline->getId(),
            ]);
        } catch (\Exception $e) {
            return new AxiosModel([
                'success' => false,
                'errors' => [$this->translate($e)],
            ]);
        }
    }

    public function supprimerAction()
    {
@@ -99,4 +206,50 @@ class DisciplineController extends AbstractController

        return compact('discipline', 'title', 'form');
    }


    public function supprimerJsonAction()
    {
        $request = $this->getRequest();

        if (!$request->isPost()) {
            return new AxiosModel([
                'success' => false,
                'errors' => ['Méthode HTTP non autorisée.'],
            ]);
        }

        $id = $request->getPost('id');

        if (!$id) {
            return new AxiosModel([
                'success' => false,
                'errors' => ['Identifiant de discipline manquant.'],
            ]);
        }

        try {
            $discipline = $this->getServiceDiscipline()->get($id);

            if (!$discipline) {
                return new AxiosModel([
                    'success' => false,
                    'errors' => ['Discipline introuvable.'],
                ]);
            }

            $this->getServiceDiscipline()->delete($discipline);

            return new AxiosModel([
                'success' => true,
            ]);


        } catch (\Exception $e) {
            return new AxiosModel([
                'success' => false,
                'errors' => [$this->translate($e)],
            ]);
        }
    }
}
 No newline at end of file
+57 −54

File changed.

Preview size limit exceeded, changes collapsed.