mirror of
https://github.com/claradex/nativegallery.git
synced 2024-11-15 03:31:10 +03:00
add edit comments
This commit is contained in:
parent
69f69a13e7
commit
a0323f6305
5 changed files with 114 additions and 45 deletions
35
app/Controllers/Api/Images/Comments/Edit.php
Normal file
35
app/Controllers/Api/Images/Comments/Edit.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controllers\Api\Images\Comments;
|
||||
|
||||
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json};
|
||||
|
||||
class Edit
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
|
||||
$postId = explode('/', $_SERVER['REQUEST_URI'])[4];
|
||||
$postuserid = DB::query('SELECT user_id FROM photos_comments WHERE id=:id', array(':id' => $postId))[0]['user_id'];
|
||||
if ($postuserid === Auth::userid()) {
|
||||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json, true);
|
||||
DB::query('UPDATE photos_comments SET body=:body WHERE id=:id', array(':id' => $postId, ':body' => $data['value']));
|
||||
echo json_encode(
|
||||
array(
|
||||
'errorcode' => '0',
|
||||
'error' => 0,
|
||||
'postid' => $postId
|
||||
)
|
||||
);
|
||||
} else {
|
||||
echo json_encode(
|
||||
array(
|
||||
'errorcode' => '1',
|
||||
'error' => 1
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ use \App\Controllers\Api\Images\CheckAll as PhotoCheckAll;
|
|||
use \App\Controllers\Api\Images\LoadRecent as PhotoLoadRecent;
|
||||
use \App\Controllers\Api\Images\Stats as PhotoStats;
|
||||
use \App\Controllers\Api\Images\Comments\Create as PhotoComment;
|
||||
use \App\Controllers\Api\Images\Comments\Edit as PhotoCommentEdit;
|
||||
use \App\Controllers\Api\Images\Comments\Load as PhotoCommentLoad;
|
||||
use \App\Controllers\Api\Images\Comments\Rate as PhotoCommentVote;
|
||||
use \App\Controllers\Api\Profile\Update as ProfileUpdate;
|
||||
|
@ -39,6 +40,9 @@ class ApiController
|
|||
public static function photocomment() {
|
||||
return new PhotoComment();
|
||||
}
|
||||
public static function photocommentedit() {
|
||||
return new PhotoCommentEdit();
|
||||
}
|
||||
public static function photocommentvote() {
|
||||
return new PhotoCommentVote();
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ class Routes
|
|||
Router::get('/api/photo/vote', 'ApiController@photovote');
|
||||
Router::get('/api/photo/checkall', 'ApiController@checkallphotos');
|
||||
Router::get('/api/photo/comment/rate', 'ApiController@photocommentvote');
|
||||
Router::post('/api/photo/comment/$id/edit', 'ApiController@photocommentedit');
|
||||
if ($user->i('admin') > 0) {
|
||||
Router::any('/admin', 'AdminController@index');
|
||||
Router::any('/api/admin/images/setvisibility', 'ApiController@adminsetvis');
|
||||
|
|
|
@ -76,8 +76,8 @@ class Comment {
|
|||
<div class="dropdown">
|
||||
<a style="color: #000" class="compl" href="/lk/ticket.php?action=add&wid=3252565">...</a>
|
||||
<div class="dropdown-content">'; ?>
|
||||
<a href="#" onclick="createModal(100, 'EDIT_COMMENT', '<?=htmlspecialchars($this->c['body'])?>'); return false;">Редактировать</a><br>
|
||||
<a href="#" onclick="createModal(100, 'DELETE_COMMENT'); return false;">Удалить</a>
|
||||
<a href="#" onclick="createModal(<?=$this->c['id']?>, 'EDIT_COMMENT', '<?=htmlspecialchars($this->c['body'])?>'); return false;">Редактировать</a><br>
|
||||
<a href="#" onclick="createModal(<?=$this->c['id']?>, 'DELETE_COMMENT'); return false;">Удалить</a>
|
||||
<?php
|
||||
echo '
|
||||
</div>
|
||||
|
|
115
static/js/act.js
115
static/js/act.js
|
@ -1,50 +1,79 @@
|
|||
|
||||
|
||||
function createModal(id, type, value) {
|
||||
if (type === 'EDIT_COMMENT') {
|
||||
var modal = `
|
||||
<div id="modal`+id+`" class="modal" style="display: block;">
|
||||
<div class="modal-content">
|
||||
<span data-modal-id="`+id+`" class="close">×</span>
|
||||
<h3><b>Отредактировать комментарий</b></h3>
|
||||
<div style="padding:0 11px 11px">
|
||||
|
||||
|
||||
<textarea name="wtext" id="wtext">`+value+`</textarea><br>
|
||||
<p id="statusSend" style="display: none;">Ошибка</p>
|
||||
<div class="cmt-submit"><input type="submit" value="Отредактировать" id="sbmt">  Ctrl + Enter
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>`;
|
||||
}
|
||||
document.body.innerHTML += modal;
|
||||
if (type === 'EDIT_COMMENT') {
|
||||
var modal = `
|
||||
<div id="modal`+id+`" class="modal" style="display: block;">
|
||||
<div class="modal-content">
|
||||
<span data-modal-id="`+id+`" class="close">×</span>
|
||||
<h3><b>Отредактировать комментарий</b></h3>
|
||||
<div style="padding:0 11px 11px">
|
||||
<textarea name="wtext" id="bodypost__commedit`+id+`">`+value+`</textarea><br>
|
||||
<p id="statusSend" style="display: none;">Ошибка</p>
|
||||
<div class="cmt-submit">
|
||||
<button type="submit" onclick="editComment('` + id + `', document.getElementById('bodypost__commedit` + id + `').value)" id="sbmt">Отредактировать</button>  Ctrl + Enter
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
document.body.innerHTML += modal;
|
||||
}
|
||||
|
||||
|
||||
var modals = document.querySelectorAll(".modal");
|
||||
document.addEventListener("click", function(event) {
|
||||
if (event.target.classList.contains("close")) {
|
||||
var modalId = event.target.getAttribute("data-modal-id");
|
||||
document.getElementById("modal" + modalId).style.display = "none";
|
||||
}
|
||||
|
||||
// Loop through each modal
|
||||
modals.forEach(function(modal) {
|
||||
// Get the unique ID of the modal
|
||||
var modalId = modal.id;
|
||||
|
||||
// Get the close button within the modal
|
||||
var closeButton = modal.querySelector(".close[data-modal-id='" + modalId + "']");
|
||||
|
||||
// Set up event listener for the close button
|
||||
closeButton.addEventListener("click", function() {
|
||||
// Hide the modal with the matching ID
|
||||
document.getElementById(modalId).style.display = "none";
|
||||
var modals = document.querySelectorAll(".modal");
|
||||
modals.forEach(function(modal) {
|
||||
if (event.target == modal) {
|
||||
modal.style.display = "none";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Set up event listener for click outside the modal
|
||||
window.addEventListener("click", function(event) {
|
||||
// Check if the user clicked outside of the modal
|
||||
if (event.target == modal) {
|
||||
// Hide the modal with the matching ID
|
||||
document.getElementById(modalId).style.display = "none";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
const editComment = (postId, body) => {
|
||||
$(document).ready(function() {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: '/api/photo/comment/'+postId+'/edit',
|
||||
data: JSON.stringify({ "value": body }),
|
||||
success: function(response) {
|
||||
var jsonData = JSON.parse(response);
|
||||
|
||||
console.log(response);
|
||||
if (jsonData.errorcode == "1") {
|
||||
Notify.noty('danger', JSON.stringify(response));
|
||||
} else {
|
||||
document.getElementById("modal" + postId).style.display = "none";
|
||||
|
||||
Notify.noty('success', 'Успешно отредактировано!');
|
||||
const url = window.location.pathname;
|
||||
const segments = url.split('/');
|
||||
const id = segments[segments.length - 1];
|
||||
$.ajax({
|
||||
|
||||
|
||||
type: "POST",
|
||||
url: "/api/photo/getcomments/"+id,
|
||||
processData: false,
|
||||
async: true,
|
||||
success: function(r) {
|
||||
$('#posts').html(r)
|
||||
|
||||
|
||||
},
|
||||
error: function(r) {
|
||||
console.log(r)
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue