loadLangs(array('companies', 'kundenkarte@kundenkarte')); // Get parameters $id = GETPOSTINT('id'); $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); $systemId = GETPOSTINT('system'); $anlageId = GETPOSTINT('anlage_id'); $parentId = GETPOSTINT('parent_id'); // Security check if (!$user->hasRight('kundenkarte', 'read')) { accessforbidden(); } // Initialize objects $object = new Societe($db); $form = new Form($db); $anlage = new Anlage($db); $anlageType = new AnlageType($db); // Load thirdparty if ($id > 0) { $result = $object->fetch($id); if ($result <= 0) { dol_print_error($db, $object->error); exit; } } $permissiontoread = $user->hasRight('kundenkarte', 'read'); $permissiontoadd = $user->hasRight('kundenkarte', 'write'); $permissiontodelete = $user->hasRight('kundenkarte', 'delete'); // Load ALL available systems (from dictionary) $allSystems = array(); $sql = "SELECT rowid, code, label, picto, color 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)) { $allSystems[$obj->rowid] = $obj; } } // Load systems ENABLED for this customer (only thirdparty-level, not contact-specific) $customerSystems = array(); $sql = "SELECT ss.rowid, ss.fk_system, s.code, s.label, s.picto, s.color FROM ".MAIN_DB_PREFIX."kundenkarte_societe_system ss JOIN ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system s ON s.rowid = ss.fk_system WHERE ss.fk_soc = ".((int) $id)." AND (ss.fk_contact IS NULL OR ss.fk_contact = 0) AND ss.active = 1 AND s.active = 1 ORDER BY s.position ASC"; $resql = $db->query($sql); if ($resql) { while ($obj = $db->fetch_object($resql)) { $customerSystems[$obj->fk_system] = $obj; } } // Default to first enabled system if not specified if (empty($systemId) && !empty($customerSystems)) { $systemId = array_key_first($customerSystems); } /* * Actions */ // Add system to customer if ($action == 'add_system' && $permissiontoadd) { $newSystemId = GETPOSTINT('new_system_id'); if ($newSystemId > 0 && !isset($customerSystems[$newSystemId])) { $sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_societe_system"; $sql .= " (entity, fk_soc, fk_system, date_creation, fk_user_creat, active)"; $sql .= " VALUES (".$conf->entity.", ".((int) $id).", ".((int) $newSystemId).", NOW(), ".((int) $user->id).", 1)"; $result = $db->query($sql); if ($result) { setEventMessages($langs->trans('SystemAdded'), null, 'mesgs'); header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id.'&system='.$newSystemId); exit; } else { setEventMessages($db->lasterror(), null, 'errors'); } } $action = ''; } // Remove system from customer if ($action == 'confirm_remove_system' && $confirm == 'yes' && $permissiontodelete) { $removeSystemId = GETPOSTINT('remove_system_id'); if ($removeSystemId > 0) { // Check if system has any elements (only thirdparty-level, not contact-specific) $sql = "SELECT COUNT(*) as cnt FROM ".MAIN_DB_PREFIX."kundenkarte_anlage WHERE fk_soc = ".((int) $id)." AND (fk_contact IS NULL OR fk_contact = 0) AND fk_system = ".((int) $removeSystemId); $resql = $db->query($sql); $obj = $db->fetch_object($resql); if ($obj->cnt > 0) { setEventMessages($langs->trans('ErrorSystemHasElements'), null, 'errors'); } else { $sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_societe_system WHERE fk_soc = ".((int) $id)." AND (fk_contact IS NULL OR fk_contact = 0) AND fk_system = ".((int) $removeSystemId); $db->query($sql); setEventMessages($langs->trans('SystemRemoved'), null, 'mesgs'); // Switch to another system or none unset($customerSystems[$removeSystemId]); if (!empty($customerSystems)) { $systemId = array_key_first($customerSystems); } else { $systemId = 0; } } } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id.($systemId ? '&system='.$systemId : '')); exit; } if ($action == 'add' && $permissiontoadd) { $anlage->label = GETPOST('label', 'alphanohtml'); $anlage->fk_soc = $id; $anlage->fk_anlage_type = GETPOSTINT('fk_anlage_type'); $anlage->fk_parent = GETPOSTINT('fk_parent'); $anlage->fk_system = $systemId; $anlage->note_private = isset($_POST['note_private']) ? $_POST['note_private'] : ''; $anlage->status = 1; // Get type for system ID $type = new AnlageType($db); if ($type->fetch($anlage->fk_anlage_type) > 0) { $anlage->fk_system = $type->fk_system; } // All fields come from dynamic fields now $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { if ($field->field_type === 'header') continue; // Skip headers $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'].'?id='.$id.'&system='.$anlage->fk_system); 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->note_private = isset($_POST['note_private']) ? $_POST['note_private'] : ''; // Get type for system ID $type = new AnlageType($db); if ($type->fetch($anlage->fk_anlage_type) > 0) { $anlage->fk_system = $type->fk_system; } // All fields come from dynamic fields now $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { if ($field->field_type === 'header') continue; // Skip headers $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'].'?id='.$id.'&system='.$anlage->fk_system); exit; } else { setEventMessages($anlage->error, $anlage->errors, 'errors'); $action = 'edit'; } } if ($action == 'confirm_delete' && $confirm == 'yes' && $permissiontodelete) { $anlage->fetch($anlageId); $systemIdBefore = $anlage->fk_system; $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'].'?id='.$id.'&system='.$systemIdBefore); exit; } // File upload if ($action == 'uploadfile' && $permissiontoadd) { $anlage->fetch($anlageId); $upload_dir = $anlage->getFileDirectory(); // Create directory if not exists if (!is_dir($upload_dir)) { dol_mkdir($upload_dir); } if (!empty($_FILES['userfile']['name'])) { $result = dol_add_file_process($upload_dir, 0, 1, 'userfile', '', null, '', 1); if ($result > 0) { // Add to database $anlagefile = new AnlageFile($db); $anlagefile->fk_anlage = $anlageId; $anlagefile->filename = dol_sanitizeFileName($_FILES['userfile']['name']); // IMPORTANT: Store ONLY relative path (anlagen/socid/anlageid/filename) - never full path! $anlagefile->filepath = 'anlagen/'.$anlage->fk_soc.'/'.$anlage->id.'/'.$anlagefile->filename; $anlagefile->filesize = $_FILES['userfile']['size']; $anlagefile->mimetype = dol_mimetype($anlagefile->filename); $anlagefile->create($user); // Generate thumbnail $anlagefile->generateThumbnail(); setEventMessages($langs->trans('FileUploaded'), null, 'mesgs'); } } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id.'&system='.$systemId.'&action=view&anlage_id='.$anlageId); exit; } // File delete (after confirmation) if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontodelete) { $fileId = GETPOSTINT('fileid'); if ($fileId > 0) { $anlagefile = new AnlageFile($db); if ($anlagefile->fetch($fileId) > 0) { // Delete method handles physical file and database if ($anlagefile->delete($user) > 0) { setEventMessages($langs->trans('FileDeleted'), null, 'mesgs'); } else { setEventMessages($langs->trans('Error'), null, 'errors'); } } } header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id.'&system='.$systemId.'&action=view&anlage_id='.$anlageId); exit; } /* * View */ $title = $langs->trans('TechnicalInstallations').' - '.$object->name; llxHeader('', $title, '', '', 0, 0, array('/kundenkarte/js/kundenkarte.js'), array('/kundenkarte/css/kundenkarte.css')); // Prepare tabs $head = societe_prepare_head($object); print dol_get_fiche_head($head, 'anlagen', $langs->trans("ThirdParty"), -1, 'company'); // Thirdparty card $linkback = ''.$langs->trans("BackToList").''; dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom'); print '
'; // Confirmation dialogs if ($action == 'delete') { print $form->formconfirm( $_SERVER['PHP_SELF'].'?id='.$id.'&anlage_id='.$anlageId.'&system='.$systemId, $langs->trans('DeleteElement'), $langs->trans('ConfirmDeleteElement'), 'confirm_delete', '', 'yes', 1 ); } if ($action == 'remove_system') { $removeSystemId = GETPOSTINT('remove_system_id'); $sysLabel = isset($customerSystems[$removeSystemId]) ? $customerSystems[$removeSystemId]->label : ''; print $form->formconfirm( $_SERVER['PHP_SELF'].'?id='.$id.'&remove_system_id='.$removeSystemId, $langs->trans('RemoveSystem'), $langs->trans('ConfirmRemoveSystem', $sysLabel), 'confirm_remove_system', '', 'yes', 1 ); } if ($action == 'askdeletefile') { $fileId = GETPOSTINT('fileid'); print $form->formconfirm( $_SERVER['PHP_SELF'].'?id='.$id.'&system='.$systemId.'&anlage_id='.$anlageId.'&fileid='.$fileId, $langs->trans('Delete'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 'yes', 1 ); } // System tabs (only show enabled systems for this customer) print '
'; print '
'; foreach ($customerSystems as $sysId => $sys) { $activeClass = ($sysId == $systemId) ? ' active' : ''; print '
'; print ''; if ($sys->picto) { print ''.kundenkarte_render_icon($sys->picto).''; } print ''.dol_escape_htmltag($sys->label).''; print ''; // Remove button (only if no elements) if ($permissiontodelete && $sysId == $systemId) { print ' '; } print '
'; } // Add system button (always on the right) if ($permissiontoadd) { // Get systems not yet enabled for this customer $availableSystems = array_diff_key($allSystems, $customerSystems); if (!empty($availableSystems)) { print ''; } } print '
'; // Expand/Collapse buttons (only in tree view, not in create/edit/view/copy) $isTreeView = !in_array($action, array('create', 'edit', 'view', 'copy')); if ($isTreeView) { print '
'; print ''; print ''; if ($systemId > 0) { $exportUrl = dol_buildpath('/kundenkarte/ajax/export_tree_pdf.php', 1).'?socid='.$id.'&system='.$systemId; print ''; print ' PDF Export'; print ''; } print '
'; } print '
'; // End kundenkarte-system-tabs-wrapper // Add system form (hidden by default) if ($permissiontoadd && !empty($availableSystems)) { print ''; } // Check if customer has any systems if (empty($customerSystems)) { print '
'; print '
'; print $langs->trans('NoSystemsConfigured').'

'; if ($permissiontoadd && !empty($allSystems)) { print $langs->trans('ClickAddSystemToStart'); } else { print $langs->trans('ContactAdminToAddSystems'); } print '
'; } elseif ($systemId > 0) { // Show form or tree for selected system if (in_array($action, array('create', 'edit', 'view', 'copy'))) { // Load element for edit/view/copy if ($action != 'create' && $anlageId > 0) { $anlage->fetch($anlageId); $type = new AnlageType($db); $type->fetch($anlage->fk_anlage_type); $type->fetchFields(); } // Load types for select $types = $anlageType->fetchAllBySystem($systemId); print '
'; if ($action == 'view') { // View mode print '

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

'; print ''; print ''; print ''; // Dynamic fields - all fields come from type definition $fieldValues = $anlage->getFieldValues(); $typeFieldsList = $type->fetchFields(); foreach ($typeFieldsList as $field) { if ($field->field_type === 'header') { // Section header print ''; } else { $value = isset($fieldValues[$field->field_code]) ? $fieldValues[$field->field_code] : ''; if ($value !== '') { print ''; // Format date fields if ($field->field_type === 'date' && $value) { print ''; } elseif ($field->field_type === 'checkbox') { print ''; } else { print ''; } } } } if ($anlage->note_private) { print ''; print ''; } // Creation date if ($anlage->date_creation) { print ''; print ''; } // Last modification date if ($anlage->tms && $anlage->tms != $anlage->date_creation) { print ''; print ''; } print '
'.$langs->trans('Type').''.dol_escape_htmltag($anlage->type_label).'
'.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('DateCreation').''.dol_print_date($anlage->date_creation, 'dayhour').'
'.$langs->trans('DateLastModification').''.dol_print_date($anlage->tms, 'dayhour').'
'; // Files section $anlagefile = new AnlageFile($db); $files = $anlagefile->fetchAllByAnlage($anlageId); print '

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

'; if ($permissiontoadd) { print '
'; print ''; print ''; print ' '; print '

'; } if (!empty($files)) { print '
'; foreach ($files as $file) { print '
'; print '
'; if ($file->file_type == 'image') { $thumbUrl = $file->getThumbUrl(); if ($thumbUrl) { print ''; } else { print ''; } } elseif ($file->file_type == 'pdf') { // PDF preview using iframe - 50% smaller, no toolbar print '
'; print ''; print '
'; } else { print ''; } print '
'; print '
'; print '
'.dol_escape_htmltag(dol_trunc($file->filename, 20)).'
'; print '
'.dol_print_size($file->filesize).'
'; print '
'; print ''; if ($permissiontodelete) { print ''; } print '
'; print '
'; print '
'; } print '
'; } else { print '

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

'; } // Action buttons print '
'; if ($permissiontoadd) { print ''.$langs->trans('Modify').''; print ''.$langs->trans('Copy').''; } if ($permissiontodelete) { print ''.$langs->trans('Delete').''; } print ''.$langs->trans('Back').''; print '
'; } else { // Create/Edit/Copy form $isEdit = ($action == 'edit'); $isCopy = ($action == 'copy'); $formAction = $isEdit ? 'update' : 'add'; print '
'; print ''; print ''; if ($isEdit) { print ''; } if ($isCopy) { print ''; } print ''; // Label $labelValue = ''; if ($isEdit) { $labelValue = $anlage->label; } elseif ($isCopy) { $labelValue = $anlage->label.' (Kopie)'; } else { $labelValue = GETPOST('label'); } print ''; print ''; // Type print ''; print ''; // Parent - for copy, use same parent as original $tree = $anlage->fetchTree($id, $systemId); $selectedParent = $isEdit || $isCopy ? $anlage->fk_parent : $parentId; $excludeId = $isEdit ? $anlageId : 0; print ''; print ''; // Dynamic fields will be inserted here via JavaScript print ''; // Notes (always at the end) print ''; $noteValue = ($isEdit || $isCopy) ? $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').''; } print '
'.$langs->trans('SelectParent').'
'.$langs->trans('FieldNotes').'
'; print '
'; print ''; print ' '.$langs->trans('Cancel').''; print '
'; print '
'; } print '
'; } else { // Tree view if ($permissiontoadd) { print '
'; print ''; print ' '.$langs->trans('AddElement'); print ''; print '
'; } // Load tree $tree = $anlage->fetchTree($id, $systemId); // Pre-load all type fields for tooltip and tree display $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 '
'; printTree($tree, $id, $systemId, $permissiontoadd, $permissiontodelete, $langs, 0, $typeFieldsMap); print '
'; } else { print '
'.$langs->trans('NoInstallations').'
'; } } } print '
'; print dol_get_fiche_end(); // Tooltip container print '
'; llxFooter(); $db->close(); /** * Print tree recursively */ function printTree($nodes, $socid, $systemId, $canEdit, $canDelete, $langs, $level = 0, $typeFieldsMap = array()) { foreach ($nodes as $node) { $hasChildren = !empty($node->children); $fieldValues = $node->getFieldValues(); // Build tooltip data - only label, type and dynamic fields $tooltipData = array( 'label' => $node->label, 'type' => $node->type_label, 'note_html' => $node->note_private ? nl2br(htmlspecialchars($node->note_private, ENT_QUOTES, 'UTF-8')) : '', 'fields' => array() ); // Collect fields for tooltip (show_in_hover) and tree label (show_in_tree) $treeInfo = array(); if (!empty($typeFieldsMap[$node->fk_anlage_type])) { foreach ($typeFieldsMap[$node->fk_anlage_type] as $fieldDef) { // Handle header fields if ($fieldDef->field_type === 'header') { if ($fieldDef->show_in_hover) { $tooltipData['fields'][$fieldDef->field_code] = array( 'label' => $fieldDef->field_label, 'value' => '', 'type' => 'header' ); } continue; } $value = isset($fieldValues[$fieldDef->field_code]) ? $fieldValues[$fieldDef->field_code] : ''; // Add to tooltip if show_in_hover if ($fieldDef->show_in_hover && $value !== '') { // Format date values $displayValue = $value; if ($fieldDef->field_type === 'date' && $value) { $displayValue = dol_print_date(strtotime($value), 'day'); } $tooltipData['fields'][$fieldDef->field_code] = array( 'label' => $fieldDef->field_label, 'value' => $displayValue, 'type' => $fieldDef->field_type ); } // Add to tree label info if show_in_tree if ($fieldDef->show_in_tree && $value !== '') { // Format date for tree info too if ($fieldDef->field_type === 'date' && $value) { $treeInfo[] = dol_print_date(strtotime($value), 'day'); } else { $treeInfo[] = $value; } } } } print '
'; print '
'; // Toggle if ($hasChildren) { print ''; } else { print ''; } // Icon with tooltip data $picto = $node->type_picto ? $node->type_picto : 'fa-cube'; print ''.kundenkarte_render_icon($picto).''; // Label with tree info in parentheses + file indicators $viewUrl = $_SERVER['PHP_SELF'].'?id='.$socid.'&system='.$systemId.'&action=view&anlage_id='.$node->id; print ''.dol_escape_htmltag($node->label); if (!empty($treeInfo)) { print ' ('.dol_escape_htmltag(implode(', ', $treeInfo)).')'; } // File indicators - directly after parentheses if ($node->image_count > 0 || $node->doc_count > 0) { print ' '; if ($node->image_count > 0) { print ''; print ''; if ($node->image_count > 1) { print ' '.$node->image_count; } print ''; } if ($node->doc_count > 0) { print ''; print ''; if ($node->doc_count > 1) { print ' '.$node->doc_count; } print ''; } print ''; } print ''; // Type badge if ($node->type_short || $node->type_label) { $typeDisplay = $node->type_short ? $node->type_short : $node->type_label; print ''.dol_escape_htmltag($typeDisplay).''; } // Actions print ''; print ''; if ($canEdit) { print ''; print ''; print ''; } if ($canDelete) { print ''; } print ''; print '
'; // Children if ($hasChildren) { print '
'; printTree($node->children, $socid, $systemId, $canEdit, $canDelete, $langs, $level + 1, $typeFieldsMap); print '
'; } print '
'; } } /** * Print tree options for select */ function printTreeOptions($nodes, $selected = 0, $excludeId = 0, $prefix = '') { foreach ($nodes as $node) { if ($node->id == $excludeId) continue; $sel = ($node->id == $selected) ? ' selected' : ''; print ''; if (!empty($node->children)) { printTreeOptions($node->children, $selected, $excludeId, $prefix.'   '); } } }