|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Answers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A controller for "Answers". |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class AnswersController implements \Anax\DI\IInjectionAware |
|
10
|
|
|
{ |
|
11
|
|
|
use \Anax\DI\TInjectable, |
|
12
|
|
|
\Anax\MVC\TRedirectHelpers; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Initialize the controller. |
|
16
|
|
|
* |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function initialize() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->answers = new \Anax\Answers\CAnswers(); |
|
|
|
|
|
|
22
|
|
|
$this->answers->setDI($this->di); |
|
|
|
|
|
|
23
|
|
|
$this->users = new \Anax\Users\User(); |
|
|
|
|
|
|
24
|
|
|
$this->users->setDI($this->di); |
|
|
|
|
|
|
25
|
|
|
$this->questions = new \Anax\Questions\CQuestions(); |
|
|
|
|
|
|
26
|
|
|
$this->questions->setDI($this->di); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Setup initial table for users. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setupAction() |
|
35
|
|
|
{ |
|
36
|
|
|
// echo "<br>" . __FILE__ . " : " . __LINE__ . "<br>";var_dump("Setup Answers!"); |
|
37
|
|
|
$this->answers->init(); |
|
38
|
|
|
// $this->redirectTo('answers/'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Accept answer to question |
|
43
|
|
|
* |
|
44
|
|
|
* @param integer $id to answer. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function acceptAction($id) |
|
49
|
|
|
{ |
|
50
|
|
|
$answer = $this->answers->find($id); |
|
51
|
|
|
$user = $this->users->loggedInUser(); |
|
52
|
|
|
$question = $this->questions->find($answer->q_id); |
|
53
|
|
|
if ($user->id == $question->user_id && |
|
54
|
|
|
!$question->accepted_answer) { |
|
55
|
|
|
// Update answer entry in db. |
|
56
|
|
|
$this->answers->update([ |
|
57
|
|
|
'id' => $answer->id, |
|
58
|
|
|
'accepted' => true, |
|
59
|
|
|
]); |
|
60
|
|
|
$this->questions->update([ |
|
61
|
|
|
'id' => $question->id, |
|
62
|
|
|
'accepted_answer' => true, |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
$this->redirectTo($_SERVER['HTTP_REFERER']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add new question |
|
70
|
|
|
* |
|
71
|
|
|
* @param integer $question_id for question being answered. |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function addAction($question_id = null) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->users->loggedIn()) { |
|
78
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
|
79
|
|
|
$form = $this->createAddCommentForm($question_id); |
|
80
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
|
81
|
|
|
// $this->di->theme->setTitle("Add user"); |
|
82
|
|
|
$this->di->views->add('default/page', [ |
|
83
|
|
|
'title' => "Svara på fråga $question_id", |
|
84
|
|
|
'content' => $form->getHTML() |
|
85
|
|
|
]); |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->redirectTo($this->url->create('users/login')); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
private function createAddCommentForm($question_id) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->di->form->create([], [ |
|
93
|
|
|
'content' => [ |
|
94
|
|
|
'type' => 'textarea', |
|
95
|
|
|
'label' => 'Svar:', |
|
96
|
|
|
'required' => true, |
|
97
|
|
|
'validation' => ['not_empty'], |
|
98
|
|
|
], |
|
99
|
|
|
'question_id' => [ |
|
100
|
|
|
'type' => 'hidden', |
|
101
|
|
|
'value' => $question_id, |
|
102
|
|
|
], |
|
103
|
|
|
'submit' => [ |
|
104
|
|
|
'type' => 'submit', |
|
105
|
|
|
'callback' => [$this, 'callbackSubmitAddAnswer'], |
|
106
|
|
|
], |
|
107
|
|
|
// 'submit-fail' => [ |
|
108
|
|
|
// 'type' => 'submit', |
|
109
|
|
|
// 'callback' => [$this, 'callbackSubmitFailAddComment'], |
|
110
|
|
|
// ], |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
/** |
|
114
|
|
|
* Callback for submit-button. |
|
115
|
|
|
* |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function callbackSubmitAddAnswer($form) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
|
120
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
|
121
|
|
|
|
|
122
|
|
|
// Save comment to database |
|
123
|
|
|
$now = time(); |
|
124
|
|
|
$this->answers->save([ |
|
125
|
|
|
'content' => $form->Value('content'), |
|
126
|
|
|
'user_id' => $this->users->loggedInUser()->id, |
|
127
|
|
|
'q_id' => $form->Value('question_id'), |
|
128
|
|
|
'vote' => 0, |
|
129
|
|
|
'created' => $now, |
|
130
|
|
|
'accepted' => false, |
|
131
|
|
|
]); |
|
132
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
|
133
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('mail') . "</b></p>"); |
|
134
|
|
|
$form->saveInSession = false; |
|
135
|
|
|
$this->session->set('ShowFormCorA', ''); |
|
|
|
|
|
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
/** |
|
139
|
|
|
* Callback for submit-button. |
|
140
|
|
|
* |
|
141
|
|
|
*/ |
|
142
|
|
|
public function callbackSubmitFailAddComment($form) |
|
143
|
|
|
{ |
|
144
|
|
|
// TODO: Remove this? |
|
145
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
/** |
|
149
|
|
|
* Callback What to do if the form was submitted? |
|
150
|
|
|
* |
|
151
|
|
|
*/ |
|
152
|
|
|
public function callbackSuccess($form) |
|
153
|
|
|
{ |
|
154
|
|
|
$form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
|
155
|
|
|
// Redirect to page displaying the new question. |
|
156
|
|
|
// $this->redirectTo($this->url->create('questions/single/' . $this->questions->id)); |
|
157
|
|
|
// $this->redirectTo($this->url->create('questions/single/' . $this->questions->id)); |
|
158
|
|
|
$this->redirectTo(); |
|
159
|
|
|
} |
|
160
|
|
|
/** |
|
161
|
|
|
* Callback What to do when form could not be processed? |
|
162
|
|
|
* |
|
163
|
|
|
*/ |
|
164
|
|
|
public function callbackFail($form) |
|
165
|
|
|
{ |
|
166
|
|
|
$form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
|
167
|
|
|
// Redirect to comment form. |
|
168
|
|
|
$this->redirectTo(); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: