asdlkif
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
/**
|
||||
* The note controller: Just an example of simple create, read, update and delete (CRUD) actions.
|
||||
*/
|
||||
require_once __DIR__ . '/../libs/SimpleMarkdown.php';
|
||||
|
||||
class NoteController extends Controller
|
||||
{
|
||||
/**
|
||||
@@ -29,6 +31,23 @@ class NoteController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get note as JSON (AJAX endpoint)
|
||||
*/
|
||||
public function getNote($note_id)
|
||||
{
|
||||
$note = NoteModel::getNote($note_id);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
if ($note) {
|
||||
// Add markdown version
|
||||
$note->note_html = SimpleMarkdown::parse($note->note_text);
|
||||
echo json_encode(['success' => true, 'note' => $note]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Note not found']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method controls what happens when you move to /dashboard/create in your app.
|
||||
* Creates a new note. This is usually the target of form submit actions.
|
||||
@@ -36,7 +55,18 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
NoteModel::createNote(Request::post('note_text'));
|
||||
$success = NoteModel::createNote(Request::post('note_text'));
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note created successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to create note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
@@ -59,7 +89,18 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function editSave()
|
||||
{
|
||||
NoteModel::updateNote(Request::post('note_id'), Request::post('note_text'));
|
||||
$success = NoteModel::updateNote(Request::post('note_id'), Request::post('note_text'));
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note updated successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to update note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
@@ -71,7 +112,27 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function delete($note_id)
|
||||
{
|
||||
NoteModel::deleteNote($note_id);
|
||||
$success = NoteModel::deleteNote($note_id);
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note deleted successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to delete note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is an AJAX request
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user