348 lines
12 KiB
PHP
348 lines
12 KiB
PHP
<?php
|
|
/* Copyright (C) 2026 Alles Watt lauft
|
|
*
|
|
* AJAX endpoint for equipment connections (generic for all system types)
|
|
*/
|
|
|
|
if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
|
|
if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
|
|
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
|
|
if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
|
|
|
|
$res = 0;
|
|
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
|
|
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
|
|
if (!$res) die("Include of main fails");
|
|
|
|
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentconnection.class.php';
|
|
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipment.class.php';
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
$action = GETPOST('action', 'aZ09');
|
|
$connectionId = GETPOSTINT('connection_id');
|
|
$carrierId = GETPOSTINT('carrier_id');
|
|
$equipmentId = GETPOSTINT('equipment_id');
|
|
|
|
$connection = new EquipmentConnection($db);
|
|
|
|
$response = array('success' => false, 'error' => '');
|
|
|
|
// Security check
|
|
if (!$user->hasRight('kundenkarte', 'read')) {
|
|
$response['error'] = 'Permission denied';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
switch ($action) {
|
|
case 'get':
|
|
// Get single connection data
|
|
if ($connectionId > 0 && $connection->fetch($connectionId) > 0) {
|
|
$response['success'] = true;
|
|
$response['connection'] = array(
|
|
'id' => $connection->id,
|
|
'fk_source' => $connection->fk_source,
|
|
'source_terminal' => $connection->source_terminal,
|
|
'fk_target' => $connection->fk_target,
|
|
'target_terminal' => $connection->target_terminal,
|
|
'connection_type' => $connection->connection_type,
|
|
'color' => $connection->color,
|
|
'output_label' => $connection->output_label,
|
|
'medium_type' => $connection->medium_type,
|
|
'medium_spec' => $connection->medium_spec,
|
|
'medium_length' => $connection->medium_length,
|
|
'is_rail' => $connection->is_rail,
|
|
'rail_start_te' => $connection->rail_start_te,
|
|
'rail_end_te' => $connection->rail_end_te,
|
|
'rail_phases' => $connection->rail_phases,
|
|
'excluded_te' => $connection->excluded_te,
|
|
'fk_carrier' => $connection->fk_carrier,
|
|
'position_y' => $connection->position_y,
|
|
'source_label' => $connection->source_label,
|
|
'target_label' => $connection->target_label
|
|
);
|
|
} else {
|
|
$response['error'] = 'Connection not found';
|
|
}
|
|
break;
|
|
|
|
case 'list':
|
|
// List all connections for a carrier
|
|
if ($carrierId > 0) {
|
|
$connections = $connection->fetchByCarrier($carrierId);
|
|
$result = array();
|
|
foreach ($connections as $c) {
|
|
$result[] = array(
|
|
'id' => $c->id,
|
|
'fk_source' => $c->fk_source,
|
|
'source_terminal' => $c->source_terminal,
|
|
'source_label' => $c->source_label,
|
|
'source_pos' => $c->source_pos,
|
|
'source_width' => $c->source_width,
|
|
'fk_target' => $c->fk_target,
|
|
'target_terminal' => $c->target_terminal,
|
|
'target_label' => $c->target_label,
|
|
'target_pos' => $c->target_pos,
|
|
'connection_type' => $c->connection_type,
|
|
'color' => $c->getColor(),
|
|
'output_label' => $c->output_label,
|
|
'medium_type' => $c->medium_type,
|
|
'medium_spec' => $c->medium_spec,
|
|
'medium_length' => $c->medium_length,
|
|
'is_rail' => $c->is_rail,
|
|
'rail_start_te' => $c->rail_start_te,
|
|
'rail_end_te' => $c->rail_end_te,
|
|
'rail_phases' => $c->rail_phases,
|
|
'excluded_te' => $c->excluded_te,
|
|
'position_y' => $c->position_y,
|
|
'display_label' => $c->getDisplayLabel()
|
|
);
|
|
}
|
|
$response['success'] = true;
|
|
$response['connections'] = $result;
|
|
} else {
|
|
$response['error'] = 'Missing carrier_id';
|
|
}
|
|
break;
|
|
|
|
case 'list_outputs':
|
|
// List outputs for an equipment
|
|
if ($equipmentId > 0) {
|
|
$outputs = $connection->fetchOutputs($equipmentId);
|
|
$result = array();
|
|
foreach ($outputs as $c) {
|
|
$result[] = array(
|
|
'id' => $c->id,
|
|
'connection_type' => $c->connection_type,
|
|
'color' => $c->getColor(),
|
|
'output_label' => $c->output_label,
|
|
'medium_type' => $c->medium_type,
|
|
'medium_spec' => $c->medium_spec,
|
|
'medium_length' => $c->medium_length,
|
|
'display_label' => $c->getDisplayLabel()
|
|
);
|
|
}
|
|
$response['success'] = true;
|
|
$response['outputs'] = $result;
|
|
} else {
|
|
$response['error'] = 'Missing equipment_id';
|
|
}
|
|
break;
|
|
|
|
case 'create':
|
|
if (!$user->hasRight('kundenkarte', 'write')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
|
|
$connection->fk_source = GETPOSTINT('fk_source');
|
|
$connection->source_terminal = GETPOST('source_terminal', 'alphanohtml') ?: 'output';
|
|
$connection->source_terminal_id = GETPOST('source_terminal_id', 'alphanohtml');
|
|
$connection->fk_target = GETPOSTINT('fk_target');
|
|
$connection->target_terminal = GETPOST('target_terminal', 'alphanohtml') ?: 'input';
|
|
$connection->target_terminal_id = GETPOST('target_terminal_id', 'alphanohtml');
|
|
$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
|
|
$connection->color = GETPOST('color', 'alphanohtml');
|
|
$connection->output_label = GETPOST('output_label', 'alphanohtml');
|
|
$connection->medium_type = GETPOST('medium_type', 'alphanohtml');
|
|
$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
|
|
$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
|
|
$connection->is_rail = GETPOSTINT('is_rail');
|
|
$connection->rail_start_te = GETPOSTINT('rail_start_te');
|
|
$connection->rail_end_te = GETPOSTINT('rail_end_te');
|
|
$connection->fk_carrier = $carrierId;
|
|
$connection->position_y = GETPOSTINT('position_y');
|
|
|
|
$result = $connection->create($user);
|
|
if ($result > 0) {
|
|
$response['success'] = true;
|
|
$response['connection_id'] = $result;
|
|
} else {
|
|
$response['error'] = $connection->error ?: 'Create failed';
|
|
}
|
|
break;
|
|
|
|
case 'update':
|
|
if (!$user->hasRight('kundenkarte', 'write')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
if ($connection->fetch($connectionId) > 0) {
|
|
$connection->fk_source = GETPOSTINT('fk_source');
|
|
$connection->source_terminal = GETPOST('source_terminal', 'alphanohtml') ?: 'output';
|
|
$connection->fk_target = GETPOSTINT('fk_target');
|
|
$connection->target_terminal = GETPOST('target_terminal', 'alphanohtml') ?: 'input';
|
|
$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
|
|
$connection->color = GETPOST('color', 'alphanohtml');
|
|
$connection->output_label = GETPOST('output_label', 'alphanohtml');
|
|
$connection->medium_type = GETPOST('medium_type', 'alphanohtml');
|
|
$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
|
|
$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
|
|
$connection->is_rail = GETPOSTINT('is_rail');
|
|
$connection->rail_start_te = GETPOSTINT('rail_start_te');
|
|
$connection->rail_end_te = GETPOSTINT('rail_end_te');
|
|
$connection->rail_phases = GETPOST('rail_phases', 'alphanohtml');
|
|
$connection->excluded_te = GETPOST('excluded_te', 'alphanohtml');
|
|
$connection->position_y = GETPOSTINT('position_y');
|
|
|
|
$result = $connection->update($user);
|
|
if ($result > 0) {
|
|
$response['success'] = true;
|
|
} else {
|
|
$response['error'] = $connection->error ?: 'Update failed';
|
|
}
|
|
} else {
|
|
$response['error'] = 'Connection not found';
|
|
}
|
|
break;
|
|
|
|
case 'delete':
|
|
if (!$user->hasRight('kundenkarte', 'delete')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
if ($connection->fetch($connectionId) > 0) {
|
|
$result = $connection->delete($user);
|
|
if ($result > 0) {
|
|
$response['success'] = true;
|
|
} else {
|
|
$response['error'] = $connection->error ?: 'Delete failed';
|
|
}
|
|
} else {
|
|
$response['error'] = 'Connection not found';
|
|
}
|
|
break;
|
|
|
|
case 'create_rail':
|
|
// Create a rail/bar connection spanning multiple equipment
|
|
if (!$user->hasRight('kundenkarte', 'write')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
|
|
$connection->is_rail = 1;
|
|
$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
|
|
$connection->color = GETPOST('color', 'alphanohtml');
|
|
$connection->rail_start_te = GETPOSTINT('rail_start_te');
|
|
$connection->rail_end_te = GETPOSTINT('rail_end_te');
|
|
$connection->rail_phases = GETPOST('rail_phases', 'alphanohtml');
|
|
$connection->excluded_te = GETPOST('excluded_te', 'alphanohtml');
|
|
$connection->fk_carrier = $carrierId;
|
|
$connection->position_y = GETPOSTINT('position_y');
|
|
|
|
$result = $connection->create($user);
|
|
if ($result > 0) {
|
|
$response['success'] = true;
|
|
$response['connection_id'] = $result;
|
|
} else {
|
|
$response['error'] = $connection->error ?: 'Create failed';
|
|
}
|
|
break;
|
|
|
|
case 'create_output':
|
|
// Create an output connection
|
|
if (!$user->hasRight('kundenkarte', 'write')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
|
|
$connection->fk_source = $equipmentId;
|
|
$connection->source_terminal = 'output';
|
|
$connection->fk_target = null;
|
|
$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
|
|
$connection->color = GETPOST('color', 'alphanohtml');
|
|
$connection->output_label = GETPOST('output_label', 'alphanohtml');
|
|
$connection->medium_type = GETPOST('medium_type', 'alphanohtml');
|
|
$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
|
|
$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
|
|
$connection->fk_carrier = $carrierId;
|
|
$connection->position_y = 0;
|
|
|
|
$result = $connection->create($user);
|
|
if ($result > 0) {
|
|
$response['success'] = true;
|
|
$response['connection_id'] = $result;
|
|
} else {
|
|
$response['error'] = $connection->error ?: 'Create failed';
|
|
}
|
|
break;
|
|
|
|
case 'list_all':
|
|
// List all connections for an anlage (across all carriers)
|
|
$anlageId = GETPOSTINT('anlage_id');
|
|
if ($anlageId > 0) {
|
|
// Get all carriers for this anlage
|
|
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentcarrier.class.php';
|
|
$carrierObj = new EquipmentCarrier($db);
|
|
$carriers = $carrierObj->fetchByAnlage($anlageId);
|
|
|
|
$allConnections = array();
|
|
foreach ($carriers as $carrier) {
|
|
$connections = $connection->fetchByCarrier($carrier->id);
|
|
foreach ($connections as $c) {
|
|
$allConnections[] = array(
|
|
'id' => $c->id,
|
|
'fk_source' => $c->fk_source,
|
|
'source_terminal' => $c->source_terminal,
|
|
'source_terminal_id' => $c->source_terminal_id,
|
|
'source_label' => $c->source_label,
|
|
'fk_target' => $c->fk_target,
|
|
'target_terminal' => $c->target_terminal,
|
|
'target_terminal_id' => $c->target_terminal_id,
|
|
'target_label' => $c->target_label,
|
|
'connection_type' => $c->connection_type,
|
|
'color' => $c->getColor(),
|
|
'output_label' => $c->output_label,
|
|
'medium_type' => $c->medium_type,
|
|
'medium_spec' => $c->medium_spec,
|
|
'medium_length' => $c->medium_length,
|
|
'is_rail' => $c->is_rail,
|
|
'fk_carrier' => $c->fk_carrier
|
|
);
|
|
}
|
|
}
|
|
|
|
$response['success'] = true;
|
|
$response['connections'] = $allConnections;
|
|
} else {
|
|
$response['error'] = 'Missing anlage_id';
|
|
}
|
|
break;
|
|
|
|
case 'clear_all':
|
|
// Delete all connections for an anlage
|
|
if (!$user->hasRight('kundenkarte', 'delete')) {
|
|
$response['error'] = 'Permission denied';
|
|
break;
|
|
}
|
|
|
|
$anlageId = GETPOSTINT('anlage_id');
|
|
if ($anlageId > 0) {
|
|
// Get all carriers for this anlage
|
|
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentcarrier.class.php';
|
|
$carrierObj = new EquipmentCarrier($db);
|
|
$carriers = $carrierObj->fetchByAnlage($anlageId);
|
|
|
|
$deletedCount = 0;
|
|
foreach ($carriers as $carrier) {
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_connection WHERE fk_carrier = ".((int)$carrier->id);
|
|
$resql = $db->query($sql);
|
|
if ($resql) {
|
|
$deletedCount += $db->affected_rows($resql);
|
|
}
|
|
}
|
|
|
|
$response['success'] = true;
|
|
$response['deleted_count'] = $deletedCount;
|
|
} else {
|
|
$response['error'] = 'Missing anlage_id';
|
|
}
|
|
break;
|
|
|
|
default:
|
|
$response['error'] = 'Unknown action';
|
|
}
|
|
|
|
echo json_encode($response);
|
|
$db->close();
|