loadLangs(array('kundenkarte@kundenkarte')); $action = GETPOST('action', 'aZ09'); $systemId = GETPOSTINT('system_id'); $response = array('success' => false, 'error' => ''); // Security check if (!$user->hasRight('kundenkarte', 'read')) { $response['error'] = $langs->trans('ErrorPermissionDenied'); echo json_encode($response); exit; } $mediumType = new MediumType($db); switch ($action) { case 'list': // Get all medium types for a system (or all) $types = $mediumType->fetchAllBySystem($systemId, 1); $result = array(); foreach ($types as $t) { $specs = $t->getAvailableSpecsArray(); $result[] = array( 'id' => $t->id, 'ref' => $t->ref, 'label' => $t->label, 'label_short' => $t->label_short, 'category' => $t->category, 'category_label' => $t->getCategoryLabel(), 'fk_system' => $t->fk_system, 'system_label' => $t->system_label, 'default_spec' => $t->default_spec, 'available_specs' => $specs, 'color' => $t->color ); } $response['success'] = true; $response['types'] = $result; break; case 'list_grouped': // Get types grouped by category $grouped = $mediumType->fetchGroupedByCategory($systemId); $result = array(); foreach ($grouped as $category => $types) { $catTypes = array(); foreach ($types as $t) { $catTypes[] = array( 'id' => $t->id, 'ref' => $t->ref, 'label' => $t->label, 'label_short' => $t->label_short, 'default_spec' => $t->default_spec, 'available_specs' => $t->getAvailableSpecsArray(), 'color' => $t->color ); } $result[] = array( 'category' => $category, 'category_label' => $types[0]->getCategoryLabel(), 'types' => $catTypes ); } $response['success'] = true; $response['groups'] = $result; break; case 'get': // Get single type details $typeId = GETPOSTINT('type_id'); if ($typeId > 0 && $mediumType->fetch($typeId) > 0) { $response['success'] = true; $response['type'] = array( 'id' => $mediumType->id, 'ref' => $mediumType->ref, 'label' => $mediumType->label, 'label_short' => $mediumType->label_short, 'category' => $mediumType->category, 'category_label' => $mediumType->getCategoryLabel(), 'default_spec' => $mediumType->default_spec, 'available_specs' => $mediumType->getAvailableSpecsArray(), 'color' => $mediumType->color, 'description' => $mediumType->description ); } else { $response['error'] = $langs->trans('ErrorRecordNotFound'); } break; default: $response['error'] = 'Unknown action'; } echo json_encode($response);