loadLangs(array('companies', 'kundenkarte@kundenkarte')); // Berechtigungen if (!$user->hasRight('kundenkarte', 'read')) { accessforbidden(); } $permissiontoread = $user->hasRight('kundenkarte', 'read'); $permissiontoadd = $user->hasRight('kundenkarte', 'write'); $permissiontodelete = $user->hasRight('kundenkarte', 'delete'); $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); $anlageId = GETPOSTINT('anlage_id'); $parentId = GETPOSTINT('parent_id'); // Virtuelle Firma-ID für Werkzeuge - braucht keinen echten Societe-Eintrag // Die Anlage-Tabelle verwendet diese ID nur als Gruppierung $socId = 99999999; // WERKZEUG-System ermitteln $werkzeugSystemId = 0; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system WHERE code = 'WERKZEUG' AND active = 1"; $resql = $db->query($sql); if ($resql && $db->num_rows($resql) > 0) { $obj = $db->fetch_object($resql); $werkzeugSystemId = $obj->rowid; } if ($werkzeugSystemId <= 0) { setEventMessages('System WERKZEUG nicht gefunden. Bitte Modul deaktivieren und wieder aktivieren.', null, 'errors'); llxHeader('', 'Firmen-Werkzeuge'); llxFooter(); exit; } $systemId = $werkzeugSystemId; // Sicherstellen dass WERKZEUG für eigene Firma aktiviert ist $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."kundenkarte_societe_system"; $sql .= " WHERE fk_soc = ".((int) $socId)." AND fk_system = ".((int) $systemId); $sql .= " AND (fk_contact IS NULL OR fk_contact = 0) AND active = 1"; $resql = $db->query($sql); if (!$resql || $db->num_rows($resql) == 0) { // Automatisch aktivieren $sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_societe_system"; $sql .= " (entity, fk_soc, fk_contact, fk_system, date_creation, fk_user_creat, active)"; $sql .= " VALUES (".$conf->entity.", ".((int) $socId).", 0, ".((int) $systemId).", NOW(), ".((int) $user->id).", 1)"; $db->query($sql); } // Objekte initialisieren $form = new Form($db); $anlage = new Anlage($db); $anlageType = new AnlageType($db); /* * Actions */ if ($action == 'add' && $permissiontoadd) { $anlage->label = GETPOST('label', 'alphanohtml'); $anlage->fk_soc = $socId; $anlage->fk_anlage_type = GETPOSTINT('fk_anlage_type'); $anlage->fk_parent = GETPOSTINT('fk_parent'); $anlage->fk_system = $systemId; $anlage->fk_product = GETPOSTINT('fk_product') > 0 ? GETPOSTINT('fk_product') : null; $anlage->note_private = isset($_POST['note_private']) ? $_POST['note_private'] : ''; $anlage->status = 1; // Dynamische Felder $type = new AnlageType($db); if ($type->fetch($anlage->fk_anlage_type) > 0) { $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { if ($field->field_type === 'header') continue; $value = GETPOST('field_'.$field->field_code, 'alphanohtml'); if ($value !== '') { $fieldValues[$field->field_code] = $value; } } $anlage->setFieldValues($fieldValues); } $result = $anlage->create($user); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); header('Location: '.$_SERVER['PHP_SELF']); exit; } else { setEventMessages($anlage->error, $anlage->errors, 'errors'); $action = 'create'; } } if ($action == 'update' && $permissiontoadd) { $anlage->fetch($anlageId); $anlage->label = GETPOST('label', 'alphanohtml'); $anlage->fk_anlage_type = GETPOSTINT('fk_anlage_type'); $anlage->fk_parent = GETPOSTINT('fk_parent'); $anlage->fk_product = GETPOSTINT('fk_product') > 0 ? GETPOSTINT('fk_product') : null; $anlage->note_private = isset($_POST['note_private']) ? $_POST['note_private'] : ''; // Dynamische Felder $type = new AnlageType($db); if ($type->fetch($anlage->fk_anlage_type) > 0) { $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { if ($field->field_type === 'header') continue; $value = GETPOST('field_'.$field->field_code, 'alphanohtml'); if ($value !== '') { $fieldValues[$field->field_code] = $value; } } $anlage->setFieldValues($fieldValues); } $result = $anlage->update($user); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); header('Location: '.$_SERVER['PHP_SELF']); exit; } else { setEventMessages($anlage->error, $anlage->errors, 'errors'); $action = 'edit'; } } if ($action == 'confirm_delete' && $confirm == 'yes' && $permissiontodelete) { $anlage->fetch($anlageId); $result = $anlage->delete($user); if ($result > 0) { setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs'); } else { setEventMessages($anlage->error, $anlage->errors, 'errors'); } header('Location: '.$_SERVER['PHP_SELF']); exit; } /* * View */ $title = $langs->trans('CompanyTools'); $jsFiles = array('/kundenkarte/js/kundenkarte.js?v='.time()); $cssFiles = array('/kundenkarte/css/kundenkarte.css?v='.time()); llxHeader('', $title, '', '', 0, 0, $jsFiles, $cssFiles); print load_fiche_titre($title, '', 'fa-wrench'); print '
'; // Bestätigungsdialog if ($action == 'delete') { print $form->formconfirm( $_SERVER['PHP_SELF'].'?anlage_id='.$anlageId, $langs->trans('DeleteElement'), $langs->trans('ConfirmDeleteElement'), 'confirm_delete', '', 'yes', 1 ); } // Typen für WERKZEUG-System laden $types = $anlageType->fetchAllBySystem($systemId, 1, 1); // excludeGlobal=1, nur WERKZEUG-Typen if (in_array($action, array('create', 'edit', 'view'))) { // Formular oder Detail-Ansicht if ($action != 'create' && $anlageId > 0) { $anlage->fetch($anlageId); $type = new AnlageType($db); $type->fetch($anlage->fk_anlage_type); $type->fetchFields(); } print '
'; if ($action == 'view') { // Detail-Ansicht print '

'.dol_escape_htmltag($anlage->label).'

'; print ''; print ''; print ''; // Zugeordnetes Produkt if ($anlage->fk_product > 0) { $product = new Product($db); if ($product->fetch($anlage->fk_product) > 0) { print ''; print ''; } } // Dynamische Felder $fieldValues = $anlage->getFieldValues(); $typeFieldsList = $type->fetchFields(); foreach ($typeFieldsList as $field) { if ($field->field_type === 'header') { print ''; } else { $value = isset($fieldValues[$field->field_code]) ? $fieldValues[$field->field_code] : ''; if ($value !== '') { print ''; if ($field->field_type === 'date' && $value) { print ''; } elseif ($field->field_type === 'checkbox') { print ''; } else { print ''; } } } } if ($anlage->note_private) { print ''; print ''; } // Ausgebaut-Status if (!empty($anlage->decommissioned)) { print ''; print ''; } print '
'.$langs->trans('Type').''.dol_escape_htmltag($anlage->type_label).'
'.$langs->trans('Product').''.dol_escape_htmltag($product->ref).''; print ' - '.dol_escape_htmltag($product->label); if ($product->price > 0) { print ' ('.price($product->price).' €)'; } print '
'.dol_escape_htmltag($field->field_label).'
'.dol_escape_htmltag($field->field_label).''.dol_print_date(strtotime($value), 'day').'
'.($value ? $langs->trans('Yes') : $langs->trans('No')).'
'.dol_escape_htmltag($value).'
'.$langs->trans('FieldNotes').''.dol_htmlentitiesbr($anlage->note_private).'
'.$langs->trans('Decommissioned').' '.$langs->trans('Decommissioned').''; if (!empty($anlage->date_decommissioned)) { print ' '.dol_print_date(strtotime($anlage->date_decommissioned), 'day'); } print '
'; // Zubehör-Bereich (nur wenn Typ has_accessories hat) if ($type->has_accessories) { $accessoryObj = new AnlageAccessory($db); $accessories = $accessoryObj->fetchAllByAnlage($anlageId); print '

'.$langs->trans('Accessories').'

'; if (!empty($accessories)) { print '
'; print ''; print ''; print ''; print ''; print ''; print ''; if ($permissiontodelete) { print ''; } print ''; foreach ($accessories as $acc) { print ''; print ''; print ''; print ''; print ''; if ($permissiontodelete) { print ''; } print ''; } print '
'.$langs->trans('ProductRef').''.$langs->trans('Label').''.$langs->trans('Qty').''.$langs->trans('Note').''.$langs->trans('Action').'
'.dol_escape_htmltag($acc->product_ref).''.dol_escape_htmltag($acc->product_label).''.$acc->qty.''.dol_escape_htmltag($acc->note).'
'; print '
'; } else { print '

'.$langs->trans('NoAccessories').'

'; } // Zubehör hinzufügen if ($permissiontoadd) { print '
'; print '
'; print ''; print ''; print ''; print ''; print '
'; print '
'; } // Bestellfunktion if ($permissiontoadd && !empty($accessories)) { print '
'; print '
'.$langs->trans('OrderAccessories').'
'; print '
'; print ''; print ''; print '
'; print '
'; } } // Aktions-Buttons print '
'; if ($permissiontoadd) { print ''.$langs->trans('Modify').''; } if ($permissiontodelete) { print ''.$langs->trans('Delete').''; } print ''.$langs->trans('Back').''; print '
'; } else { // Erstellen/Bearbeiten-Formular $isEdit = ($action == 'edit'); $formAction = $isEdit ? 'update' : 'add'; print '
'; print ''; print ''; if ($isEdit) { print ''; } print ''; // Label $labelValue = $isEdit ? $anlage->label : GETPOST('label'); print ''; print ''; // Typ print ''; print ''; // Übergeordnetes Element $tree = $anlage->fetchTree($socId, $systemId); $selectedParent = $isEdit ? $anlage->fk_parent : $parentId; $excludeId = $isEdit ? $anlageId : 0; print ''; print ''; // Produkt-Zuordnung $productValue = ''; $productId = 0; if ($isEdit && $anlage->fk_product > 0) { require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; $product = new Product($db); if ($product->fetch($anlage->fk_product) > 0) { $productValue = $product->ref.' - '.$product->label; $productId = $product->id; } } print ''; print ''; // Dynamische Felder werden per JS geladen print ''; // Notizen print ''; $noteValue = $isEdit ? $anlage->note_private : (isset($_POST['note_private']) ? $_POST['note_private'] : ''); print ''; print '
'.$langs->trans('Label').'
'.$langs->trans('Type').''; if (empty($types)) { print '
'.$langs->trans('NoTypesDefinedForSystem').' '.$langs->trans('GoToTypeAdmin').''; } print '
'.$langs->trans('SelectParent').'
'.$langs->trans('Product').''; print ''; print ''; print '
'.$langs->trans('FieldNotes').'
'; print '
'; print ''; print ' '.$langs->trans('Cancel').''; print '
'; print '
'; } print '
'; } else { // Baumansicht if ($permissiontoadd) { print '
'; print ''; print ' '.$langs->trans('AddElement'); print ''; print '
'; } // Steuerungs-Buttons print '
'; print ''; print ''; print ''; print ''; print '
'; // Baum laden $tree = $anlage->fetchTree($socId, $systemId); // Feld-Metadaten laden $typeFieldsMap = array(); $sql = "SELECT f.*, f.fk_anlage_type FROM ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field f WHERE f.active = 1 ORDER BY f.position ASC"; $resql = $db->query($sql); if ($resql) { while ($obj = $db->fetch_object($resql)) { if (!isset($typeFieldsMap[$obj->fk_anlage_type])) { $typeFieldsMap[$obj->fk_anlage_type] = array(); } $typeFieldsMap[$obj->fk_anlage_type][] = $obj; } $db->free($resql); } if (!empty($tree)) { print '
'; werkzeuge_printTree($tree, $socId, $systemId, $permissiontoadd, $permissiontodelete, $langs, 0, $typeFieldsMap); print '
'; } else { print '
'; print '
'; print $langs->trans('NoToolsYet').'

'; if ($permissiontoadd) { print ' '.$langs->trans('AddFirstTool').''; } print '
'; } } print '
'; // fichecenter // Tooltip Container print '
'; // JavaScript: Produkt-Autocomplete + Zubehör-AJAX print ''; // CSS für Autocomplete print ''; llxFooter(); $db->close(); /** * Baum rekursiv ausgeben (vereinfachte Version für Werkzeuge-Seite) */ function werkzeuge_printTree($nodes, $socid, $systemId, $canEdit, $canDelete, $langs, $level = 0, $typeFieldsMap = array()) { foreach ($nodes as $node) { $hasChildren = !empty($node->children); $fieldValues = $node->getFieldValues(); // Badges sammeln $treeInfoBadges = array(); $treeInfoParentheses = array(); if (!empty($typeFieldsMap[$node->fk_anlage_type])) { foreach ($typeFieldsMap[$node->fk_anlage_type] as $fieldDef) { if ($fieldDef->field_type === 'header') continue; $value = isset($fieldValues[$fieldDef->field_code]) ? $fieldValues[$fieldDef->field_code] : ''; if ($fieldDef->show_in_tree && $value !== '') { $displayVal = $value; if ($fieldDef->field_type === 'date' && $value) { $displayVal = dol_print_date(strtotime($value), 'day'); } $fieldInfo = array( 'label' => $fieldDef->field_label, 'value' => $displayVal, 'code' => $fieldDef->field_code, 'type' => $fieldDef->field_type, 'color' => $fieldDef->badge_color ?? '' ); $displayMode = $fieldDef->tree_display_mode ?? 'badge'; if ($displayMode === 'parentheses') { $treeInfoParentheses[] = $fieldInfo; } else { $treeInfoBadges[] = $fieldInfo; } } } } $nodeClass = 'kundenkarte-tree-node'; if (!empty($node->decommissioned)) { $nodeClass .= ' decommissioned'; } if ($node->type_can_have_children) { $nodeClass .= ' node-structure'; } else { $nodeClass .= ' node-leaf'; } print '
'; print '
'; // Toggle if ($hasChildren) { print ''; } else { print ''; } // Icon $picto = $node->type_picto ? $node->type_picto : 'fa-wrench'; print ''.kundenkarte_render_icon($picto).''; // Label $viewUrl = $_SERVER['PHP_SELF'].'?action=view&anlage_id='.$node->id; print ''.dol_escape_htmltag($node->label); if (!empty($treeInfoParentheses)) { $infoValues = array(); foreach ($treeInfoParentheses as $info) { $infoValues[] = dol_escape_htmltag($info['value']); } print ' ('.implode(', ', $infoValues).')'; } print ''; // Ausgebaut-Badge if (!empty($node->decommissioned)) { $decommDate = !empty($node->date_decommissioned) ? dol_print_date(strtotime($node->date_decommissioned), 'day') : ''; $decommText = $langs->trans('Decommissioned'); if ($decommDate) $decommText .= ' '.$decommDate; print ' '.$decommText.''; } // Produkt-Badge if (!empty($node->fk_product) && !empty($node->product_ref)) { print ' '.dol_escape_htmltag($node->product_ref).''; } // Spacer print ''; // Badges $defaultBadgeColor = getDolGlobalString('KUNDENKARTE_TREE_BADGE_COLOR', '#2a4a5e'); if (!empty($treeInfoBadges)) { print ''; foreach ($treeInfoBadges as $info) { $badgeIcon = kundenkarte_get_field_icon($info['code'], $info['type']); $fieldBadgeColor = !empty($info['color']) ? $info['color'] : $defaultBadgeColor; print ''; print ' '.dol_escape_htmltag($info['value']); print ''; } print ''; } // Typ-Badge if ($node->type_short || $node->type_label) { $typeDisplay = $node->type_short ? $node->type_short : $node->type_label; print ''.dol_escape_htmltag($typeDisplay).''; } // Aktionen print ''; print ''; if ($canEdit) { print ''; print ''; $decommLabel = $node->decommissioned ? $langs->trans('Recommission') : $langs->trans('Decommission'); $decommIcon = $node->decommissioned ? 'fa-plug' : 'fa-power-off'; print ''; } if ($canDelete) { print ''; } print ''; print '
'; // Kinder if ($hasChildren) { print '
'; werkzeuge_printTree($node->children, $socid, $systemId, $canEdit, $canDelete, $langs, $level + 1, $typeFieldsMap); print '
'; } print '
'; } } /** * Baum-Options für Select (vereinfacht) */ function werkzeuge_printTreeOptions($nodes, $selected = 0, $excludeId = 0, $prefix = '', $level = 0) { foreach ($nodes as $node) { if ($node->id == $excludeId) continue; $sel = ($node->id == $selected) ? ' selected' : ''; print ''; if (!empty($node->children)) { werkzeuge_printTreeOptions($node->children, $selected, $excludeId, $prefix.'── ', $level + 1); } } }