KTDiscussion.php
8.07 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
require_once(KT_LIB_DIR . '/discussions/DiscussionThread.inc');
require_once(KT_LIB_DIR . '/discussions/DiscussionComment.inc');
class KTDiscussionPlugin extends KTPlugin {
var $sNamespace = "ktstandard.discussion.plugin";
}
$oRegistry =& KTPluginRegistry::getSingleton();
$oRegistry->registerPlugin('KTDiscussionPlugin', 'ktstandard.discussion.plugin', __FILE__);
$oPlugin =& $oRegistry->getPlugin('ktstandard.discussion.plugin');
class KTDiscussionThreadListRenderer {
function render($context, $oThread) {
$this->oThread = $oThread;
$oTemplate = $context->oValidator->validateTemplate('ktstandard/action/discussion_thread_list_item');
$oFirstComment = DiscussionComment::get($oThread->getFirstCommentId());
if (PEAR::isError($oFirstComment)) {
return null;
}
$oLastComment = DiscussionComment::get($oThread->getLastCommentId());
if (PEAR::isError($oLastComment)) {
return null;
}
$oCreator = User::get($oThread->getCreatorId());
$oTemplate->setData(array(
'thread' => $this->oThread,
'first_comment' => $oFirstComment,
'last_comment' => $oLastComment,
'creator' => $oCreator,
'context' => $context,
));
return $oTemplate->render();
}
}
class KTCommentListRenderer {
function render($context, $oComment) {
$this->oComment = $oComment;
$oTemplate = $context->oValidator->validateTemplate('ktstandard/action/discussion_comment_list_item');
$oCreator = User::get($oComment->getUserId());
$oTemplate->setData(array(
'comment' => $oComment,
'creator' => $oCreator,
'context' => $context,
));
return $oTemplate->render();
}
}
class KTDocumentDiscussionAction extends KTDocumentAction {
var $sBuiltInAction = 'viewDiscussion';
var $sDisplayName = 'Discussion';
var $sName = 'ktcore.actions.document.discussion';
function do_main() {
$this->oPage->setBreadcrumbDetails(_("discussion"));
$oTemplate =& $this->oValidator->validateTemplate('ktstandard/action/discussion');
// Fields for new thread creation
$fields = array();
$fields[] = new KTStringWidget(_("Subject"), _("The topic of discussion in this thread"), "subject", "", $this->oPage, true);
$fields[] = new KTTextWidget(_("Body"), _("Your contribution to the discussion in this thread"), "body", "", $this->oPage, true, null, null, array("cols" => 50, "rows" => 10));
$threads = DiscussionThread::getList();
$aTemplateData = array(
'context' => &$this,
'fields' => $fields,
'threads' => $threads,
'threadrenderer' => new KTDiscussionThreadListRenderer(),
);
return $oTemplate->render($aTemplateData);
}
function do_newthread() {
$aErrorOptions = array(
'redirect_to' => array('main', sprintf('fDocumentId=%d', $this->oDocument->getId())),
);
$aErrorOptions['message'] = _("No subject provided");
$sSubject = KTUtil::arrayGet($_REQUEST, 'subject');
$sSubject = $this->oValidator->validateString($sSubject, $aErrorOptions);
$aErrorOptions['message'] = _("No body provided");
$sBody = KTUtil::arrayGet($_REQUEST, 'body');
$sBody = $this->oValidator->validateString($sBody, $aErrorOptions);
// Start the transaction around thread and comment creation
$this->startTransaction();
$oThread = DiscussionThread::createFromArray(array(
'documentid' => $this->oDocument->getId(),
'creatorid' => $this->oUser->getId(),
));
$aErrorOptions['message'] = _("There was an error creating a new thread");
$this->oValidator->notError($oThread, $aErrorOptions);
$oComment = DiscussionComment::createFromArray(array(
'threadid' => $oThread->getId(),
'userid' => $this->oUser->getId(),
'subject' => $sSubject,
'body' => $sBody,
));
$aErrorOptions['message'] = _("There was an error adding the comment to the thread");
$this->oValidator->notError($oComment, $aErrorOptions);
$oThread->setFirstCommentId($oComment->getId());
$oThread->setLastCommentId($oComment->getId());
$res = $oThread->update();
$aErrorOptions['message'] = _("There was an error updating the thread with the new comment");
$this->oValidator->notError($res, $aErrorOptions);
// Thread and comment created correctly, commit to database
$this->commitTransaction();
$this->successRedirectToMain(_("New thread created"), sprintf('fDocumentId=%d', $this->oDocument->getId()));
exit(0);
}
function do_viewthread() {
$iThreadId = KTUtil::arrayGet($_REQUEST, 'fThreadId');
$oThread = DiscussionThread::get($iThreadId);
$iCommentId = $oThread->getFirstCommentId();
$oComment = DiscussionComment::get($iCommentId);
$this->aBreadcrumbs[] = array(
'name' => _('discussion'),
'url' => $_SERVER['PHP_SELF'] . sprintf('?fDocumentId=%d', $this->oDocument->getId()),
);
$this->aBreadcrumbs[] = array(
'name' => $oComment->getSubject(),
);
$this->oPage->setBreadcrumbDetails(_("viewing comments"));
$oTemplate =& $this->oValidator->validateTemplate('ktstandard/action/discussion_thread');
// Fields for new thread creation
$fields = array();
$fields[] = new KTStringWidget(_("Subject"), _("The topic of discussion in this thread"), "subject", "", $this->oPage, true);
$fields[] = new KTTextWidget(_("Body"), _("Your contribution to the discussion in this thread"), "body", "", $this->oPage, true, null, null, array("cols" => 50, "rows" => 10));
$aTemplateData = array(
'context' => &$this,
'fields' => $fields,
'thread' => $oThread,
'commentrenderer' => new KTCommentListRenderer(),
);
return $oTemplate->render($aTemplateData);
}
function do_postreply() {
$aErrorOptions = array(
'redirect_to' => array('main', sprintf('fDocumentId=%d', $this->oDocument->getId())),
);
$iThreadId = KTUtil::arrayGet($_REQUEST, 'fThreadId');
$oThread = DiscussionThread::get($iThreadId);
$this->oValidator->notError($oThread, $aErrorOptions);
$aErrorOptions = array(
'redirect_to' => array('viewthread', sprintf('fDocumentId=%d&fThreadId=%d', $this->oDocument->getId(), $oThread->getId())),
);
$aErrorOptions['message'] = _("No subject provided");
$sSubject = KTUtil::arrayGet($_REQUEST, 'subject');
$sSubject = $this->oValidator->validateString($sSubject, $aErrorOptions);
$aErrorOptions['message'] = _("No body provided");
$sBody = KTUtil::arrayGet($_REQUEST, 'body');
$sBody = $this->oValidator->validateString($sBody, $aErrorOptions);
// Start the transaction comment creation
$this->startTransaction();
$oComment = DiscussionComment::createFromArray(array(
'threadid' => $oThread->getId(),
'userid' => $this->oUser->getId(),
'subject' => $sSubject,
'body' => $sBody,
));
$aErrorOptions['message'] = _("There was an error adding the comment to the thread");
$this->oValidator->notError($oComment, $aErrorOptions);
$oThread->setLastCommentId($oComment->getId());
$res = $oThread->update();
$aErrorOptions['message'] = _("There was an error updating the thread with the new comment");
$this->oValidator->notError($res, $aErrorOptions);
// Thread and comment created correctly, commit to database
$this->commitTransaction();
$this->successRedirectTo('viewThread', _("Reply posted"), sprintf('fDocumentId=%d&fThreadId=%d', $this->oDocument->getId(), $oThread->getId()));
exit(0);
}
}
$oPlugin->registerAction('documentaction', 'KTDocumentDiscussionAction', 'ktcore.actions.document.discussion');