feat: Dodaj funkcjonalność usuwania komentarzy oraz wyświetlanie ich w historii produktów

This commit is contained in:
2026-01-26 23:40:20 +01:00
parent e165fd3ef3
commit cd5ab2b8b6
3 changed files with 104 additions and 5 deletions

View File

@@ -51,6 +51,19 @@ class Products
exit;
}
static public function comment_delete()
{
$comment_id = \S::get( 'comment_id' );
if ( \factory\Products::delete_product_comment( $comment_id ) )
{
echo json_encode( [ 'status' => 'ok' ] );
}
else
echo json_encode( [ 'status' => 'error' ] );
exit;
}
static public function get_product_data() {
$product_id = \S::get( 'product_id' );
@@ -255,6 +268,15 @@ class Products
$roas_value = ( $row['cost'] > 0) ? ( $row['conversions_value'] / $row['cost'] ) * 100 : 0;
$roas = number_format( $roas_value, 0, '.', '' ) . '%';
$comment_data = \factory\Products::get_product_comment_by_date( $product_id, $row['date_add'] );
$comment_html = '';
if ( $comment_data )
{
$comment_html = '<div class="comment-cell">
<span class="comment-text">' . htmlspecialchars( $comment_data['comment'] ) . '</span>
<a href="#" class="text-danger delete-comment" data-comment-id="' . $comment_data['id'] . '" style="margin-left: 10px;">Usuń</a>
</div>';
}
$data['data'][] = [
$row['id'],
@@ -266,6 +288,7 @@ class Products
\S::number_display( $row['conversions_value'] ),
$roas,
$row['date_add'],
$comment_html,
];
}

View File

@@ -11,7 +11,19 @@ class Products
static public function get_product_comments( $product_id )
{
global $mdb;
return $mdb -> query( 'SELECT comment, date_add FROM products_comments WHERE product_id = \'' . $product_id . '\' ORDER BY date_add DESC' ) -> fetchAll( \PDO::FETCH_ASSOC );
return $mdb -> query( 'SELECT id, comment, date_add FROM products_comments WHERE product_id = \'' . $product_id . '\' ORDER BY date_add DESC' ) -> fetchAll( \PDO::FETCH_ASSOC );
}
static public function delete_product_comment( $comment_id )
{
global $mdb;
return $mdb -> delete( 'products_comments', [ 'id' => $comment_id ] );
}
static public function get_product_comment_by_date( $product_id, $date )
{
global $mdb;
return $mdb -> get( 'products_comments', [ 'id', 'comment' ], [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] );
}
static public function get_min_roas( $product_id )

View File

@@ -42,6 +42,7 @@
<th scope="col">Wartość konwersji</th>
<th scope="col">ROAS</th>
<th scope="col">Data</th>
<th scope="col">Komentarz</th>
</tr>
</thead>
<tbody></tbody>
@@ -90,6 +91,7 @@
{ width: '175px', name: 'conversions_value', className: "dt-type-numeric" },
{ width: '100px', name: 'roas', className: "dt-type-numeric", orderable: false },
{ width: '100px', name: 'date_add' },
{ width: '250px', name: 'comment', orderable: false },
],
order: [[0, false],[8, 'desc']]
});
@@ -157,10 +159,10 @@
})
}, false);
// 2) Dodaj „niewidzialne markery do hovera
// 2) Dodaj „niewidzialne" markery do hovera
var points = (parsedData.comments || []).map(function(c) {
var idx = parsedData.dates.indexOf((c.date_add || '').split(' ')[0]);
return { x: idx, y: 0, comment: c.comment };
return { x: idx, y: 0, comment: c.comment, comment_id: c.id };
});
chart.addSeries({
@@ -177,12 +179,43 @@
enabled: true,
radius: 4,
lineWidth: 0,
fillOpacity: 0 // marker „niewidoczny
fillOpacity: 0 // marker „niewidoczny"
},
states: {
hover: { enabled: true, halo: { size: 5 } }
},
enableMouseTracking: true
enableMouseTracking: true,
cursor: 'pointer',
point: {
events: {
click: function () {
var commentId = this.comment_id;
var commentText = this.comment;
if (confirm('Czy na pewno chcesz usunąć komentarz: "' + commentText + '"?')) {
$.ajax({
url: '/products/comment_delete/',
method: 'POST',
data: {
comment_id: commentId
},
success: function(res) {
res = JSON.parse(res);
if (res.status === 'ok') {
location.reload();
} else {
alert('Nie udało się usunąć komentarza. Spróbuj ponownie.');
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Błąd usuwania komentarza:', textStatus, errorThrown);
alert('Nie udało się usunąć komentarza. Spróbuj ponownie.');
}
});
}
}
}
}
}, false);
chart.redraw();
@@ -282,5 +315,36 @@
}
});
});
// OBSŁUGA USUWANIA KOMENTARZA Z TABELI
$(document).on('click', '.delete-comment', function(e) {
e.preventDefault();
var comment_id = $(this).data('comment-id');
if (!confirm('Czy na pewno chcesz usunąć ten komentarz?')) {
return;
}
$.ajax({
url: '/products/comment_delete/',
method: 'POST',
data: {
comment_id: comment_id
},
success: function(res) {
res = JSON.parse(res);
if (res.status === 'ok') {
location.reload();
} else {
alert('Nie udało się usunąć komentarza. Spróbuj ponownie.');
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Błąd usuwania komentarza:', textStatus, errorThrown);
alert('Nie udało się usunąć komentarza. Spróbuj ponownie.');
}
});
});
});
</script>