loadLangs(array('admin', 'kundenkarte@kundenkarte'));
// Security check
if (!$user->admin && !$user->hasRight('kundenkarte', 'admin')) {
accessforbidden();
}
$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
$typeId = GETPOSTINT('typeid');
$systemFilter = GETPOSTINT('system');
$form = new Form($db);
$anlageType = new AnlageType($db);
// Load systems
$systems = array();
$sql = "SELECT rowid, code, label FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system WHERE active = 1 ORDER BY position ASC";
$resql = $db->query($sql);
if ($resql) {
while ($obj = $db->fetch_object($resql)) {
$systems[$obj->rowid] = $obj;
}
}
/*
* Actions
*/
if ($action == 'add') {
$anlageType->ref = GETPOST('ref', 'aZ09');
$anlageType->label = GETPOST('label', 'alphanohtml');
$anlageType->label_short = GETPOST('label_short', 'alphanohtml');
$anlageType->description = GETPOST('description', 'restricthtml');
$anlageType->fk_system = GETPOSTINT('fk_system');
$anlageType->can_have_children = GETPOSTINT('can_have_children');
$anlageType->can_be_nested = GETPOSTINT('can_be_nested');
$anlageType->allowed_parent_types = GETPOST('allowed_parent_types', 'alphanohtml');
$anlageType->picto = GETPOST('picto', 'alphanohtml');
$anlageType->color = GETPOST('color', 'alphanohtml');
$anlageType->position = GETPOSTINT('position');
$anlageType->active = 1;
if (empty($anlageType->ref) || empty($anlageType->label) || empty($anlageType->fk_system)) {
setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
$action = 'create';
} else {
$result = $anlageType->create($user);
if ($result > 0) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.$_SERVER['PHP_SELF'].'?system='.$anlageType->fk_system);
exit;
} else {
setEventMessages($anlageType->error, $anlageType->errors, 'errors');
$action = 'create';
}
}
}
if ($action == 'update') {
$anlageType->fetch($typeId);
$anlageType->ref = GETPOST('ref', 'aZ09');
$anlageType->label = GETPOST('label', 'alphanohtml');
$anlageType->label_short = GETPOST('label_short', 'alphanohtml');
$anlageType->description = GETPOST('description', 'restricthtml');
$anlageType->fk_system = GETPOSTINT('fk_system');
$anlageType->can_have_children = GETPOSTINT('can_have_children');
$anlageType->can_be_nested = GETPOSTINT('can_be_nested');
$anlageType->allowed_parent_types = GETPOST('allowed_parent_types', 'alphanohtml');
$anlageType->picto = GETPOST('picto', 'alphanohtml');
$anlageType->color = GETPOST('color', 'alphanohtml');
$anlageType->position = GETPOSTINT('position');
$result = $anlageType->update($user);
if ($result > 0) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.$_SERVER['PHP_SELF'].'?system='.$anlageType->fk_system);
exit;
} else {
setEventMessages($anlageType->error, $anlageType->errors, 'errors');
$action = 'edit';
}
}
if ($action == 'confirm_delete' && $confirm == 'yes') {
$anlageType->fetch($typeId);
$result = $anlageType->delete($user);
if ($result > 0) {
setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
} else {
setEventMessages($anlageType->error, $anlageType->errors, 'errors');
}
$action = '';
}
if ($action == 'activate') {
$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_anlage_type SET active = 1 WHERE rowid = ".((int) $typeId);
$db->query($sql);
$action = '';
}
if ($action == 'deactivate') {
$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_anlage_type SET active = 0 WHERE rowid = ".((int) $typeId);
$db->query($sql);
$action = '';
}
// Field actions
$fieldId = GETPOSTINT('fieldid');
if ($action == 'add_field') {
$fieldCode = GETPOST('field_code', 'aZ09');
$fieldLabel = GETPOST('field_label', 'alphanohtml');
$fieldType = GETPOST('field_type', 'aZ09');
$fieldOptions = GETPOST('field_options', 'restricthtml');
$showInTree = GETPOSTINT('show_in_tree');
$showInHover = GETPOSTINT('show_in_hover');
$isRequired = GETPOSTINT('is_required');
$fieldPosition = GETPOSTINT('field_position');
if (empty($fieldCode) || empty($fieldLabel) || empty($fieldType)) {
setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
} else {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field";
$sql .= " (fk_anlage_type, field_code, field_label, field_type, field_options, show_in_tree, show_in_hover, required, position, active)";
$sql .= " VALUES (".((int) $typeId).", '".$db->escape($fieldCode)."', '".$db->escape($fieldLabel)."',";
$sql .= " '".$db->escape($fieldType)."', '".$db->escape($fieldOptions)."',";
$sql .= " ".((int) $showInTree).", ".((int) $showInHover).", ".((int) $isRequired).", ".((int) $fieldPosition).", 1)";
if ($db->query($sql)) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
} else {
setEventMessages($db->lasterror(), null, 'errors');
}
}
$action = 'edit';
}
if ($action == 'update_field') {
$fieldCode = GETPOST('field_code', 'aZ09');
$fieldLabel = GETPOST('field_label', 'alphanohtml');
$fieldType = GETPOST('field_type', 'aZ09');
$fieldOptions = GETPOST('field_options', 'restricthtml');
$showInTree = GETPOSTINT('show_in_tree');
$showInHover = GETPOSTINT('show_in_hover');
$isRequired = GETPOSTINT('is_required');
$fieldPosition = GETPOSTINT('field_position');
$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field SET";
$sql .= " field_code = '".$db->escape($fieldCode)."',";
$sql .= " field_label = '".$db->escape($fieldLabel)."',";
$sql .= " field_type = '".$db->escape($fieldType)."',";
$sql .= " field_options = '".$db->escape($fieldOptions)."',";
$sql .= " show_in_tree = ".((int) $showInTree).",";
$sql .= " show_in_hover = ".((int) $showInHover).",";
$sql .= " required = ".((int) $isRequired).",";
$sql .= " position = ".((int) $fieldPosition);
$sql .= " WHERE rowid = ".((int) $fieldId);
if ($db->query($sql)) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
} else {
setEventMessages($db->lasterror(), null, 'errors');
}
$action = 'edit';
}
if ($action == 'confirm_delete_field' && $confirm == 'yes') {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field WHERE rowid = ".((int) $fieldId);
if ($db->query($sql)) {
setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
} else {
setEventMessages($db->lasterror(), null, 'errors');
}
$action = 'edit';
}
if ($action == 'activate_field') {
$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field SET active = 1 WHERE rowid = ".((int) $fieldId);
$db->query($sql);
$action = 'edit';
}
if ($action == 'deactivate_field') {
$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field SET active = 0 WHERE rowid = ".((int) $fieldId);
$db->query($sql);
$action = 'edit';
}
/*
* View
*/
$title = $langs->trans('AnlagenTypes');
// Include CSS and JS
$morejs = array('/kundenkarte/js/kundenkarte.js');
$morecss = array('/kundenkarte/css/kundenkarte.css');
llxHeader('', $title, '', '', 0, 0, $morejs, $morecss);
$head = kundenkarteAdminPrepareHead();
print dol_get_fiche_head($head, 'types', $langs->trans('ModuleKundenKarteName'), -1, 'fa-file');
// Confirmation
if ($action == 'delete') {
print $form->formconfirm(
$_SERVER['PHP_SELF'].'?typeid='.$typeId.'&system='.$systemFilter,
$langs->trans('Delete'),
$langs->trans('ConfirmDeleteType'),
'confirm_delete',
'',
'yes',
1
);
}
// Add/Edit form
if (in_array($action, array('create', 'edit'))) {
if ($action == 'edit' && $typeId > 0) {
$anlageType->fetch($typeId);
}
// Get all types for parent selection
$allTypes = $anlageType->fetchAllBySystem(0, 0);
print '
';
// Fields management for existing type
if ($action == 'edit' && $typeId > 0) {
$editFieldId = GETPOSTINT('editfield');
// Confirmation for field deletion
if ($action == 'delete_field') {
print $form->formconfirm(
$_SERVER['PHP_SELF'].'?action=edit&typeid='.$typeId.'&fieldid='.$fieldId.'&system='.$systemFilter,
$langs->trans('Delete'),
$langs->trans('ConfirmDeleteField'),
'confirm_delete_field',
'',
'yes',
1
);
}
print '
';
print ''.$langs->trans('AnlagenTypeFields').'
';
$fields = $anlageType->fetchFields(0);
// Field types available
$fieldTypes = array(
'text' => 'Textfeld (einzeilig)',
'textarea' => 'Textfeld (mehrzeilig)',
'number' => 'Zahlenfeld',
'select' => 'Dropdown-Auswahl',
'date' => 'Datumsfeld',
'checkbox' => 'Checkbox (Ja/Nein)',
);
print '';
// Help box for field options
print '';
print '
Hilfe: Feld-Optionen nach Feldtyp
';
print '
';
print '| Textfeld (einzeilig) | Keine Optionen nötig |
';
print '| Textfeld (mehrzeilig) | Keine Optionen nötig |
';
print '| Zahlenfeld | Optional: min:0|max:100|step:0.1 |
';
print '| Dropdown-Auswahl | Pflicht! Optionen mit | trennen, z.B.: Option A|Option B|Option C |
';
print '| Datumsfeld | Keine Optionen nötig |
';
print '| Checkbox (Ja/Nein) | Keine Optionen nötig |
';
print '
';
print '
';
}
} else {
// System filter
print '';
// Add button
print '';
// List
$types = $anlageType->fetchAllBySystem($systemFilter, 0);
print '';
print '';
print '| '.$langs->trans('TypeRef').' | ';
print ''.$langs->trans('TypeLabel').' | ';
print ''.$langs->trans('System').' | ';
print ''.$langs->trans('CanHaveChildren').' | ';
print ''.$langs->trans('Position').' | ';
print ''.$langs->trans('Status').' | ';
print ''.$langs->trans('Actions').' | ';
print '
';
foreach ($types as $type) {
print '';
print '| ';
if ($type->picto) {
print kundenkarte_render_icon($type->picto).' ';
}
print dol_escape_htmltag($type->ref).' | ';
print ''.dol_escape_htmltag($type->label);
if ($type->label_short) {
print ' ('.$type->label_short.')';
}
print ' | ';
print ''.dol_escape_htmltag($type->system_label).' | ';
print ''.($type->can_have_children ? img_picto('', 'tick') : '').' | ';
print ''.$type->position.' | ';
print '';
if ($type->active) {
print ''.img_picto($langs->trans('Enabled'), 'switch_on').'';
} else {
print ''.img_picto($langs->trans('Disabled'), 'switch_off').'';
}
print ' | ';
print '';
print ''.img_edit().'';
if (!$type->is_system) {
print ' '.img_delete().'';
}
print ' | ';
print '
';
}
if (empty($types)) {
print '| '.$langs->trans('NoRecords').' |
';
}
print '
';
}
print dol_get_fiche_end();
// JavaScript for parent type selector
print '';
llxFooter();
$db->close();