-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCFormComment.php
103 lines (88 loc) · 2.04 KB
/
CFormComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Anax\HTMLForm;
/**
* Anax base class for forms.
*
*/
class CFormComment extends \Mos\HTMLForm\CForm
{
use \Anax\DI\TInjectionaware,
\Anax\MVC\TRedirectHelpers;
public $answers=null;
public $userId;
public $sourceId;
public $acronym;
public $type;
public $comments;
public $question;
/**
* Constructor
*
*/
public function __construct($comments, $userId, $sourceId, $acronym, $type, $question, $comment=null)
{
$this->comments = $comments;
$this->userId = $userId;
$this->sourceId = $sourceId;
$this->acronym = $acronym;
$this->type = $type;
$this->question = $question;
parent::__construct([], [
'text' => [
'type' => 'textarea',
'label' => 'Comment:',
'required' => true,
'validation' => ['not_empty'],
'value' => isset($comment) ? $comment->text : '',
],
'submit' => [
'type' => 'submit',
'callback' => [$this, 'callbackSubmit'],
],
]);
}
/**
* Customise the check() method.
*
* @param callable $callIfSuccess handler to call if function returns true.
* @param callable $callIfFail handler to call if function returns true.
*/
public function check($callIfSuccess = null, $callIfFail = null)
{
return parent::check([$this, 'callbackSuccess'], [$this, 'callbackFail']);
}
/**
* Callback for submit-button.
*
*/
public function callbackSubmit()
{
$now = gmdate('Y-m-d H:i:s');
$this->comments->save([
'commentType' => $this->type,
'questionAnswerId' => $this->sourceId,
'userId' => $this->userId,
'commentText' =>$this->Value('text'),
'created' => $now,
'userAcronym' => $this->acronym
]);
return true;
}
/**
* Callback What to do if the form was submitted?
*
*/
public function callbackSuccess()
{
$this->redirectTo('questions/id/' . $this->question->id);
}
/**
* Callback What to do when form could not be processed?
*
*/
public function callbackFail()
{
$this->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>");
$this->redirectTo();
}
}