subtotaltitle/ajax/create_textline.php

36 lines
1.3 KiB
PHP
Executable file

<?php
define('NOTOKENRENEWAL', 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");
header('Content-Type: application/json');
$facture_id = GETPOST('facture_id', 'int');
$text = GETPOST('text', 'restricthtml');
if (!$facture_id || !$text) {
echo json_encode(array('success' => false, 'error' => 'Missing parameters'));
exit;
}
// Hole nächste line_order
$sql = "SELECT MAX(line_order) as max_order FROM ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " WHERE fk_facture = ".(int)$facture_id;
$resql = $db->query($sql);
$obj = $db->fetch_object($resql);
$next_order = ($obj->max_order ? $obj->max_order + 1 : 1);
// Füge Textzeile ein
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_lines_manager";
$sql .= " (fk_facture, line_type, title, line_order, date_creation)";
$sql .= " VALUES (".(int)$facture_id.", 'text', '".$db->escape($text)."', ".$next_order.", NOW())";
if ($db->query($sql)) {
$new_id = $db->last_insert_id(MAIN_DB_PREFIX."facture_lines_manager");
echo json_encode(array('success' => true, 'id' => $new_id));
} else {
echo json_encode(array('success' => false, 'error' => $db->lasterror()));
}