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 Contact($db); $form = new Form($db); $anlage = new Anlage($db); $anlageType = new AnlageType($db); // Load contact 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 contact $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) $object->socid)." AND ss.fk_contact = ".((int) $id)." 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 contact 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_contact, fk_system, date_creation, fk_user_creat, active)"; $sql .= " VALUES (".$conf->entity.", ".((int) $object->socid).", ".((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 contact if ($action == 'confirm_remove_system' && $confirm == 'yes' && $permissiontodelete) { $removeSystemId = GETPOSTINT('remove_system_id'); if ($removeSystemId > 0) { // Check if system has any elements for this contact $sql = "SELECT COUNT(*) as cnt FROM ".MAIN_DB_PREFIX."kundenkarte_anlage WHERE fk_soc = ".((int) $object->socid)." AND fk_contact = ".((int) $id)." 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) $object->socid)." AND fk_contact = ".((int) $id)." 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 = $object->socid; $anlage->fk_contact = $id; $anlage->fk_anlage_type = GETPOSTINT('fk_anlage_type'); $anlage->fk_parent = GETPOSTINT('fk_parent'); $anlage->fk_system = $systemId; $anlage->manufacturer = GETPOST('manufacturer', 'alphanohtml'); $anlage->model = GETPOST('model', 'alphanohtml'); $anlage->serial_number = GETPOST('serial_number', 'alphanohtml'); $anlage->power_rating = GETPOST('power_rating', 'alphanohtml'); $anlage->location = GETPOST('location', 'alphanohtml'); $anlage->installation_date = dol_mktime(0, 0, 0, GETPOSTINT('installation_datemonth'), GETPOSTINT('installation_dateday'), GETPOSTINT('installation_dateyear')); $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; } // Dynamic fields $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { $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->manufacturer = GETPOST('manufacturer', 'alphanohtml'); $anlage->model = GETPOST('model', 'alphanohtml'); $anlage->serial_number = GETPOST('serial_number', 'alphanohtml'); $anlage->power_rating = GETPOST('power_rating', 'alphanohtml'); $anlage->location = GETPOST('location', 'alphanohtml'); $anlage->installation_date = dol_mktime(0, 0, 0, GETPOSTINT('installation_datemonth'), GETPOSTINT('installation_dateday'), GETPOSTINT('installation_dateyear')); $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; } // Dynamic fields $fieldValues = array(); $fields = $type->fetchFields(); foreach ($fields as $field) { $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->getFullName($langs); llxHeader('', $title, '', '', 0, 0, array('/kundenkarte/js/kundenkarte.js'), array('/kundenkarte/css/kundenkarte.css')); // Prepare tabs $head = contact_prepare_head($object); print dol_get_fiche_head($head, 'anlagen', $langs->trans("ContactAddress"), -1, 'contact'); // Contact card $linkback = ''.$langs->trans("BackToList").''; dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'nom'); print '
| '.$langs->trans('Type').' | '; print ''.dol_escape_htmltag($anlage->type_label).' |
| '.$langs->trans('FieldManufacturer').' | '; print ''.dol_escape_htmltag($anlage->manufacturer).' |
| '.$langs->trans('FieldModel').' | '; print ''.dol_escape_htmltag($anlage->model).' |
| '.$langs->trans('FieldSerialNumber').' | '; print ''.dol_escape_htmltag($anlage->serial_number).' |
| '.$langs->trans('FieldPowerRating').' | '; print ''.dol_escape_htmltag($anlage->power_rating).' |
| '.$langs->trans('FieldLocation').' | '; print ''.dol_escape_htmltag($anlage->location).' |
| '.$langs->trans('FieldInstallationDate').' | '; print ''.dol_print_date($anlage->installation_date, 'day').' |
| '.dol_escape_htmltag($field->field_label).' | '; $value = $fieldValues[$field->field_code]; print ''.dol_escape_htmltag($value).' |
| '.$langs->trans('FieldNotes').' | '; print ''.dol_htmlentitiesbr($anlage->note_private).' |
| '.$langs->trans('DateCreation').' | '; print ''.dol_print_date($anlage->date_creation, 'dayhour').' |
| '.$langs->trans('DateLastModification').' | '; print ''.dol_print_date($anlage->tms, 'dayhour').' |
'.$langs->trans('NoFiles').'
'; } // Action buttons print '