engelsystem/includes/pages/user_questions.php

80 lines
2.3 KiB
PHP
Raw Normal View History

2011-06-02 23:45:54 +02:00
<?php
2019-12-03 20:09:45 +01:00
use Engelsystem\Models\Question;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function questions_title()
{
return __('Ask the Heaven');
2013-11-25 21:04:58 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function user_questions()
{
2018-10-31 12:48:22 +01:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
if (!$request->has('action')) {
2020-11-29 20:00:15 +01:00
$open_questions = $user->questionsAsked()
->whereNull('answerer_id')
->orderByDesc('created_at')
->get();
$answered_questions = $user->questionsAsked()
->whereNotNull('answerer_id')
->orderByDesc('answered_at')
->get();
2017-01-02 15:43:36 +01:00
2017-08-28 16:21:10 +02:00
return Questions_view(
2019-12-03 20:09:45 +01:00
$open_questions->all(),
$answered_questions->all(),
2017-08-28 16:21:10 +02:00
page_link_to('user_questions', ['action' => 'ask'])
);
2017-01-02 03:57:23 +01:00
} else {
switch ($request->input('action')) {
2017-01-02 15:43:36 +01:00
case 'ask':
$question = request()->get('question');
if (!empty($question) && $request->hasPostData('submit')) {
2019-12-03 20:09:45 +01:00
Question::create([
'user_id' => $user->id,
'text' => $question,
2019-12-03 20:09:45 +01:00
]);
2017-07-23 11:46:54 +02:00
success(__('You question was saved.'));
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_questions'));
2017-01-02 15:43:36 +01:00
} else {
return page_with_title(questions_title(), [
error(__('Please enter a question!'), true)
2017-01-02 15:43:36 +01:00
]);
}
break;
case 'delete':
if (
$request->has('id')
&& preg_match('/^\d{1,11}$/', $request->input('id'))
&& $request->hasPostData('submit')
) {
$question_id = $request->input('id');
2017-01-02 15:43:36 +01:00
} else {
return error(__('Incomplete call, missing Question ID.'), true);
2017-01-02 15:43:36 +01:00
}
2019-12-03 20:09:45 +01:00
$question = Question::find($question_id);
if (!empty($question) && $question->user_id == $user->id) {
$question->delete();
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_questions'));
2017-01-02 15:43:36 +01:00
} else {
return page_with_title(questions_title(), [
error(__('No question found.'), true)
2017-01-02 15:43:36 +01:00
]);
}
break;
}
2017-01-02 03:57:23 +01:00
}
2017-01-03 03:22:48 +01:00
return '';
2011-06-02 23:45:54 +02:00
}