662 lines
20 KiB
PHP
662 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\DynamicForms\Controllers;
|
|
|
|
use CodeIgniter\Controller;
|
|
|
|
use Config\Database;
|
|
|
|
class DynamicForms extends Controller
|
|
{
|
|
public function save($code, $id)
|
|
{
|
|
$db = Database::connect();
|
|
|
|
$form = $db->table('forms')
|
|
->where('code', $code)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$form) {
|
|
return "Formulaire introuvable";
|
|
}
|
|
|
|
$data = $this->request->getPost();
|
|
|
|
// ==================================================
|
|
// POINT 1 — LECTURE DU PARENT DANS L'URL (PAS DE GET)
|
|
// /dynamic/edit/{code}/{id}/parent/{parent_code}/{parent_id}
|
|
// ==================================================
|
|
$segments = $this->request->getUri()->getSegments();
|
|
|
|
$parentId = null;
|
|
$parentFormCode = null;
|
|
|
|
$segments = $this->request->getUri()->getSegments();
|
|
$parentIndex = array_search('parent', $segments);
|
|
if ($parentIndex !== false && isset($segments[$parentIndex + 1], $segments[$parentIndex + 2])) {
|
|
$parentFormCode = $segments[$parentIndex + 1];
|
|
$parentId = (int) $segments[$parentIndex + 2];
|
|
}
|
|
$parentIndex = array_search('parent', $segments);
|
|
if ($parentIndex !== false && isset($segments[$parentIndex + 2])) {
|
|
$parentId = (int) $segments[$parentIndex + 2];
|
|
}
|
|
// ==================================================
|
|
|
|
// ==================================================
|
|
// POINT 2 — INJECTION VIA forms_relations (GÉNÉRIQUE)
|
|
// ==================================================
|
|
if ((int)$id === 0 && $parentId) {
|
|
|
|
$relation = $db->table('forms_relations')
|
|
->where('child_form_id', $form['id'])
|
|
->where('active', 1)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($relation) {
|
|
$data[$relation['foreign_key']] = $parentId;
|
|
}
|
|
}
|
|
// ==================================================
|
|
|
|
if ((int)$id === 0) {
|
|
|
|
$db->table($form['target_table'])
|
|
->insert($data);
|
|
|
|
} else {
|
|
|
|
$db->table($form['target_table'])
|
|
->where($form['target_primary_key'], $id)
|
|
->update($data);
|
|
}
|
|
|
|
if ($parentId && $parentFormCode) {
|
|
return redirect()->to(site_url('v4/dynamic/edit/'.$parentFormCode.'/'.$parentId));
|
|
}
|
|
return redirect()->to(site_url("v4/dynamic/list/{$code}"));
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $code
|
|
* @param $id
|
|
* @return string
|
|
*/
|
|
public function edit($code, $id)
|
|
{
|
|
$db = Database::connect();
|
|
|
|
$form = $db->table('forms')
|
|
->where('code', $code)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$form || !$form['active']) {
|
|
return "Formulaire introuvable";
|
|
}
|
|
|
|
$fields = $db->table('form_fields')
|
|
->where('form_id', $form['id'])
|
|
->where('show_in_form', 1)
|
|
->orderBy('sort_order_form', 'ASC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
if ((int)$id === 0) {
|
|
|
|
$record = [];
|
|
|
|
// pré-population parent si présent dans l'URL
|
|
$segments = $this->request->getUri()->getSegments();
|
|
$parentIndex = array_search('parent', $segments);
|
|
|
|
if ($parentIndex !== false && isset($segments[$parentIndex + 1], $segments[$parentIndex + 2])) {
|
|
|
|
$parentFormCode = $segments[$parentIndex + 1];
|
|
$parentId = (int)$segments[$parentIndex + 2];
|
|
|
|
$parentForm = $db->table('forms')
|
|
->where('code', $parentFormCode)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($parentForm) {
|
|
|
|
$relation = $db->table('forms_relations')
|
|
->where('parent_form_id', $parentForm['id'])
|
|
->where('child_form_id', $form['id'])
|
|
->where('active', 1)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($relation && !empty($relation['foreign_key'])) {
|
|
$record[$relation['foreign_key']] = $parentId;
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
|
|
$record = $db->table($form['target_table'])
|
|
->where($form['target_primary_key'], $id)
|
|
->get()
|
|
->getRowArray();
|
|
}
|
|
|
|
$genresModel = new \Modules\Inventaire\Models\InvGenresModel();
|
|
foreach ($fields as &$field) {
|
|
if ($field['type'] === 'select' && $field['source_table'] === 'inv_genres') {
|
|
$field['options'] = $genresModel->getOptionsByType($field['source_filter']);
|
|
}
|
|
}
|
|
unset($field);
|
|
// =================================================
|
|
// CONTEXTE PARENT (lecture URL, robuste)
|
|
// =================================================
|
|
$segments = $this->request->getUri()->getSegments();
|
|
|
|
$parentFormCode = null;
|
|
$parentId = null;
|
|
|
|
$parentIndex = array_search('parent', $segments);
|
|
if ($parentIndex !== false && isset($segments[$parentIndex + 1], $segments[$parentIndex + 2])) {
|
|
$parentFormCode = $segments[$parentIndex + 1];
|
|
$parentId = (int)$segments[$parentIndex + 2];
|
|
}
|
|
// =================================================
|
|
|
|
// =================================================
|
|
// LISTES ENFANTS (quand on est sur le parent)
|
|
// =================================================
|
|
$childLists = [];
|
|
|
|
if ((int)$id !== 0) {
|
|
|
|
$relations = $db->table('forms_relations')
|
|
->where('parent_form_id', $form['id'])
|
|
->where('active', 1)
|
|
->orderBy('sort_order', 'ASC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($relations as $relation) {
|
|
|
|
$childForm = $db->table('forms')
|
|
->where('id', $relation['child_form_id'])
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$childForm) {
|
|
continue;
|
|
}
|
|
|
|
$fieldsChild = $db->table('form_fields')
|
|
->where('form_id', $childForm['id'])
|
|
->where('show_in_list', 1)
|
|
->orderBy('sort_order_list', 'ASC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$columns = array_column($fieldsChild, 'db_column');
|
|
$columns[] = $childForm['target_primary_key'];
|
|
|
|
$dataChild = $db->table($childForm['target_table'])
|
|
->select(implode(',', $columns))
|
|
->where($relation['foreign_key'], $id)
|
|
->get()
|
|
->getResultArray();
|
|
// ==========================================
|
|
// VERIFIER SI DELETE POSSIBLE (ENFANTS)
|
|
// ==========================================
|
|
|
|
$relationsChild = $db->table('forms_relations')
|
|
->where('parent_form_id', $childForm['id'])
|
|
->where('active', 1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($dataChild as &$rowChild) {
|
|
|
|
$canDelete = true;
|
|
|
|
foreach ($relationsChild as $relChild) {
|
|
|
|
$childChildForm = $db->table('forms')
|
|
->where('id', $relChild['child_form_id'])
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$childChildForm) {
|
|
continue;
|
|
}
|
|
|
|
$exists = $db->table($childChildForm['target_table'])
|
|
->where($relChild['foreign_key'], $rowChild[$childForm['target_primary_key']])
|
|
->countAllResults();
|
|
|
|
if ($exists > 0) {
|
|
$canDelete = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$rowChild['can_delete'] = $canDelete;
|
|
}
|
|
|
|
unset($rowChild);
|
|
|
|
foreach ($fieldsChild as $field) {
|
|
|
|
if ($field['type'] !== 'lookup') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
empty($field['lookup_table']) ||
|
|
empty($field['lookup_key_field']) ||
|
|
empty($field['lookup_label_field'])
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$ids = [];
|
|
|
|
foreach ($dataChild as $row) {
|
|
if (!empty($row[$field['db_column']])) {
|
|
$ids[] = $row[$field['db_column']];
|
|
}
|
|
}
|
|
|
|
$ids = array_unique($ids);
|
|
|
|
if (empty($ids)) {
|
|
continue;
|
|
}
|
|
|
|
$lookupRows = $db->table($field['lookup_table'])
|
|
->select($field['lookup_key_field'] . ', ' . $field['lookup_label_field'])
|
|
->whereIn($field['lookup_key_field'], $ids)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$map = [];
|
|
foreach ($lookupRows as $lr) {
|
|
$map[$lr[$field['lookup_key_field']]] = $lr[$field['lookup_label_field']];
|
|
}
|
|
|
|
foreach ($dataChild as &$row) {
|
|
$row[$field['db_column']] = $map[$row[$field['db_column']]] ?? '';
|
|
}
|
|
unset($row);
|
|
}
|
|
$childLists[] = view('Views/dynamicforms/list_embed', [
|
|
'form' => $childForm,
|
|
'fields' => $fieldsChild,
|
|
'data' => $dataChild,
|
|
'genresModel' => $genresModel,
|
|
'parentFormCode' => $form['code'],
|
|
'parentId' => $id,
|
|
'page_title' => ''
|
|
]);
|
|
}
|
|
}
|
|
// =================================================
|
|
|
|
/*
|
|
*
|
|
|
|
|
|
* ===============================
|
|
* RESOLUTION LOOKUP (FORM - READONLY)
|
|
* ===============================
|
|
*/
|
|
|
|
|
|
if (!empty($record)) {
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if ($field['type'] !== 'lookup') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
empty($field['lookup_table']) ||
|
|
empty($field['lookup_key_field']) ||
|
|
empty($field['lookup_label_field'])
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$value = $record[$field['db_column']] ?? null;
|
|
|
|
if (empty($value)) {
|
|
$record[$field['db_column']] = '';
|
|
continue;
|
|
}
|
|
|
|
$row = $db->table($field['lookup_table'])
|
|
->select($field['lookup_label_field'])
|
|
->where($field['lookup_key_field'], $value)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$record[$field['db_column']] = $row[$field['lookup_label_field']] ?? '';
|
|
}
|
|
}
|
|
/*
|
|
* ===============================
|
|
*/
|
|
|
|
|
|
|
|
return view('Views/dynamicforms/form', [
|
|
'form' => $form,
|
|
'fields' => $fields,
|
|
'record' => $record,
|
|
'code' => $code,
|
|
'id' => $id,
|
|
'childLists' => $childLists,
|
|
'parentFormCode' => $parentFormCode,
|
|
'parentId' => $parentId
|
|
]);
|
|
}
|
|
public function list($code)
|
|
{
|
|
$db = Database::connect();
|
|
|
|
$form = $db->table('forms')
|
|
->where('code', $code)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$form || !$form['active']) {
|
|
return "Formulaire introuvable ou inactif";
|
|
}
|
|
|
|
$fields = $db->table('form_fields')
|
|
->where('form_id', $form['id'])
|
|
->where('show_in_list', 1)
|
|
->orderBy('sort_order_list', 'ASC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$columns = array_column($fields, 'db_column');
|
|
|
|
// clé primaire toujours requise
|
|
$columns[] = $form['target_primary_key'];
|
|
|
|
$data = $db->table($form['target_table'])
|
|
->select(implode(',', $columns))
|
|
->orderBy($form['list_order_by'] ?? $form['target_primary_key'], 'ASC')
|
|
|
|
->get()
|
|
->getResultArray();
|
|
|
|
/*
|
|
* ===============================
|
|
* RESOLUTION LOOKUP (LISTE)
|
|
* ===============================
|
|
*/
|
|
foreach ($fields as $field) {
|
|
|
|
if ($field['type'] !== 'lookup') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
empty($field['lookup_table']) ||
|
|
empty($field['lookup_key_field']) ||
|
|
empty($field['lookup_label_field'])
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$ids = [];
|
|
|
|
foreach ($data as $row) {
|
|
if (!empty($row[$field['db_column']])) {
|
|
$ids[] = $row[$field['db_column']];
|
|
}
|
|
}
|
|
|
|
$ids = array_unique($ids);
|
|
|
|
if (empty($ids)) {
|
|
continue;
|
|
}
|
|
|
|
$lookupRows = $db->table($field['lookup_table'])
|
|
->select($field['lookup_key_field'] . ', ' . $field['lookup_label_field'])
|
|
->whereIn($field['lookup_key_field'], $ids)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$map = [];
|
|
foreach ($lookupRows as $lr) {
|
|
$map[$lr[$field['lookup_key_field']]] = $lr[$field['lookup_label_field']];
|
|
}
|
|
|
|
foreach ($data as &$row) {
|
|
$row[$field['db_column']] = $map[$row[$field['db_column']]] ?? '';
|
|
}
|
|
unset($row);
|
|
}
|
|
/*
|
|
* ===============================
|
|
*/
|
|
|
|
$genresModel = new \Modules\Inventaire\Models\InvGenresModel();
|
|
// =================================================
|
|
// VERIFIER SI DELETE POSSIBLE
|
|
// =================================================
|
|
|
|
$relations = $db->table('forms_relations')
|
|
->where('parent_form_id', $form['id'])
|
|
->where('active', 1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($data as &$row) {
|
|
|
|
// PRIORITÉ — FORM
|
|
if ((int)$form['active_delete'] === 0) {
|
|
$row['can_delete'] = false;
|
|
continue;
|
|
}
|
|
|
|
$canDelete = true;
|
|
|
|
foreach ($relations as $relation) {
|
|
|
|
$childForm = $db->table('forms')
|
|
->where('id', $relation['child_form_id'])
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$childForm) {
|
|
continue;
|
|
}
|
|
|
|
$exists = $db->table($childForm['target_table'])
|
|
->where($relation['foreign_key'], $row[$form['target_primary_key']])
|
|
->countAllResults();
|
|
|
|
if ($exists > 0) {
|
|
$canDelete = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$row['can_delete'] = $canDelete;
|
|
}
|
|
|
|
unset($row);
|
|
\App\Libraries\Nav::set(current_url());
|
|
return view('Views/dynamicforms/list', [
|
|
'form' => $form,
|
|
'fields' => $fields,
|
|
'data' => $data,
|
|
'genresModel' => $genresModel,
|
|
'page_title' => 'Inventaire'
|
|
]);
|
|
}
|
|
public function getRecordsForIds($code, $ids)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
$form = $db->table('forms')
|
|
->where('code', $code)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$form) {
|
|
return [];
|
|
}
|
|
|
|
$fields = $db->table('form_fields')
|
|
->where('form_id', $form['id'])
|
|
->where('show_in_list', 1)
|
|
->orderBy('sort_order_list', 'ASC')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$columns = array_column($fields, 'db_column');
|
|
$columns[] = $form['target_primary_key'];
|
|
|
|
$data = $db->table($form['target_table'])
|
|
->select(implode(',', $columns))
|
|
->whereIn($form['target_primary_key'], $ids)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if ($field['type'] !== 'lookup') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
empty($field['lookup_table']) ||
|
|
empty($field['lookup_key_field']) ||
|
|
empty($field['lookup_label_field'])
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$lookupIds = [];
|
|
|
|
foreach ($data as $row) {
|
|
if (!empty($row[$field['db_column']])) {
|
|
$lookupIds[] = $row[$field['db_column']];
|
|
}
|
|
}
|
|
|
|
$lookupIds = array_unique($lookupIds);
|
|
|
|
if (empty($lookupIds)) {
|
|
continue;
|
|
}
|
|
|
|
$lookupRows = $db->table($field['lookup_table'])
|
|
->select($field['lookup_key_field'] . ', ' . $field['lookup_label_field'])
|
|
->whereIn($field['lookup_key_field'], $lookupIds)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$map = [];
|
|
|
|
foreach ($lookupRows as $lr) {
|
|
$map[$lr[$field['lookup_key_field']]] = $lr[$field['lookup_label_field']];
|
|
}
|
|
|
|
foreach ($data as &$row) {
|
|
$row[$field['db_column']] = $map[$row[$field['db_column']]] ?? '';
|
|
}
|
|
unset($row);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
public function delete($code, $id)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
$form = $db->table('forms')
|
|
->where('code', $code)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$form) {
|
|
return "Formulaire introuvable";
|
|
}
|
|
|
|
// ===============================
|
|
// Lecture parent dans l'URL
|
|
// ===============================
|
|
|
|
$segments = $this->request->getUri()->getSegments();
|
|
|
|
$parentFormCode = null;
|
|
$parentId = null;
|
|
|
|
$parentIndex = array_search('parent', $segments);
|
|
|
|
if ($parentIndex !== false && isset($segments[$parentIndex + 1], $segments[$parentIndex + 2])) {
|
|
$parentFormCode = $segments[$parentIndex + 1];
|
|
$parentId = (int)$segments[$parentIndex + 2];
|
|
}
|
|
|
|
// ===============================
|
|
// Vérifier enfants
|
|
// ===============================
|
|
|
|
$relations = $db->table('forms_relations')
|
|
->where('parent_form_id', $form['id'])
|
|
->where('active', 1)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($relations as $relation) {
|
|
|
|
$childForm = $db->table('forms')
|
|
->where('id', $relation['child_form_id'])
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$childForm) {
|
|
continue;
|
|
}
|
|
|
|
$exists = $db->table($childForm['target_table'])
|
|
->where($relation['foreign_key'], $id)
|
|
->countAllResults();
|
|
|
|
if ($exists > 0) {
|
|
return "Impossible de supprimer : cet élément possède des enfants.";
|
|
}
|
|
}
|
|
|
|
// ===============================
|
|
// DELETE
|
|
// ===============================
|
|
|
|
$db->table($form['target_table'])
|
|
->where($form['target_primary_key'], $id)
|
|
->delete();
|
|
|
|
// ===============================
|
|
// Redirection
|
|
// ===============================
|
|
|
|
if ($parentFormCode && $parentId) {
|
|
return redirect()->to(site_url('v4/dynamic/edit/'.$parentFormCode.'/'.$parentId));
|
|
}
|
|
|
|
return redirect()->to(site_url('v4/dynamic/list/'.$code));
|
|
}
|
|
}
|