-
Notifications
You must be signed in to change notification settings - Fork 767
/
class.discussioncontroller.php
919 lines (761 loc) · 34.6 KB
/
class.discussioncontroller.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
<?php if (!defined('APPLICATION')) exit();
/**
* Handles accessing & displaying a single discussion.
*
* @copyright Copyright 2008, 2009 Vanilla Forums Inc.
* @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
* @since 2.0.0
* @package Vanilla
*/
class DiscussionController extends VanillaController {
/**
* Models to include.
*
* @since 2.0.0
* @access public
* @var array
*/
public $Uses = array('DiscussionModel', 'CommentModel', 'Form');
/**
* Unique identifier.
*
* @since 2.0.0
* @access public
* @var array
*/
public $CategoryID;
/**
* @var DiscussionModel
*/
public $DiscussionModel;
public function __get($Name) {
switch ($Name) {
case 'CommentData':
Deprecated('DiscussionController->CommentData', "DiscussionController->Data('Comments')");
return $this->Data('Comments');
break;
}
throw new Exception("DiscussionController->$Name not found.", 400);
}
/**
* Default single discussion display.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID
* @param string $DiscussionStub URL-safe title slug
* @param int $Page The current page of comments
*/
public function Index($DiscussionID = '', $DiscussionStub = '', $Page = '') {
// Setup head
$Session = Gdn::Session();
$this->AddJsFile('jquery.autogrow.js');
$this->AddJsFile('discussion.js');
$this->AddJsFile('autosave.js');
Gdn_Theme::Section('Discussion');
// Load the discussion record
$DiscussionID = (is_numeric($DiscussionID) && $DiscussionID > 0) ? $DiscussionID : 0;
if (!array_key_exists('Discussion', $this->Data))
$this->SetData('Discussion', $this->DiscussionModel->GetID($DiscussionID), TRUE);
if(!is_object($this->Discussion)) {
throw new Exception(sprintf(T('%s Not Found'), T('Discussion')), 404);
}
// Define the query offset & limit.
$Limit = C('Vanilla.Comments.PerPage', 30);
$OffsetProvided = $Page != '';
list($Offset, $Limit) = OffsetLimit($Page, $Limit);
// Check permissions
$this->Permission('Vanilla.Discussions.View', TRUE, 'Category', $this->Discussion->PermissionCategoryID);
$this->SetData('CategoryID', $this->CategoryID = $this->Discussion->CategoryID, TRUE);
if (strcasecmp(GetValue('Type', $this->Discussion), 'redirect') === 0) {
$this->RedirectDiscussion($this->Discussion);
}
$Category = CategoryModel::Categories($this->Discussion->CategoryID);
$this->SetData('Category', $Category);
if ($CategoryCssClass = GetValue('CssClass', $Category))
Gdn_Theme::Section($CategoryCssClass);
$this->SetData('Breadcrumbs', CategoryModel::GetAncestors($this->CategoryID));
// Setup
$this->Title($this->Discussion->Name);
// Actual number of comments, excluding the discussion itself.
$ActualResponses = $this->Discussion->CountComments;
$this->Offset = $Offset;
if (C('Vanilla.Comments.AutoOffset')) {
// if ($this->Discussion->CountCommentWatch > 1 && $OffsetProvided == '')
// $this->AddDefinition('ScrollTo', 'a[name=Item_'.$this->Discussion->CountCommentWatch.']');
if (!is_numeric($this->Offset) || $this->Offset < 0 || !$OffsetProvided) {
// Round down to the appropriate offset based on the user's read comments & comments per page
$CountCommentWatch = $this->Discussion->CountCommentWatch > 0 ? $this->Discussion->CountCommentWatch : 0;
if ($CountCommentWatch > $ActualResponses)
$CountCommentWatch = $ActualResponses;
// (((67 comments / 10 perpage) = 6.7) rounded down = 6) * 10 perpage = offset 60;
$this->Offset = floor($CountCommentWatch / $Limit) * $Limit;
}
if ($ActualResponses <= $Limit)
$this->Offset = 0;
if ($this->Offset == $ActualResponses)
$this->Offset -= $Limit;
} else {
if ($this->Offset == '')
$this->Offset = 0;
}
if ($this->Offset < 0)
$this->Offset = 0;
$LatestItem = $this->Discussion->CountCommentWatch;
if ($LatestItem === NULL) {
$LatestItem = 0;
} elseif ($LatestItem < $this->Discussion->CountComments) {
$LatestItem += 1;
}
$this->SetData('_LatestItem', $LatestItem);
// Set the canonical url to have the proper page title.
$this->CanonicalUrl(DiscussionUrl($this->Discussion, PageNumber($this->Offset, $Limit, 0, FALSE)));
// Url(ConcatSep('/', 'discussion/'.$this->Discussion->DiscussionID.'/'. Gdn_Format::Url($this->Discussion->Name), PageNumber($this->Offset, $Limit, TRUE, Gdn::Session()->UserID != 0)), TRUE), Gdn::Session()->UserID == 0);
// Load the comments
$this->SetData('Comments', $this->CommentModel->Get($DiscussionID, $Limit, $this->Offset));
$PageNumber = PageNumber($this->Offset, $Limit);
$this->SetData('Page', $PageNumber);
$this->_SetOpenGraph();
include_once(PATH_LIBRARY.'/vendors/simplehtmldom/simple_html_dom.php');
if ($PageNumber == 1) {
$this->Description(SliceParagraph(Gdn_Format::PlainText($this->Discussion->Body, $this->Discussion->Format), 160));
// Add images to head for open graph
$Dom = str_get_html(Gdn_Format::To($this->Discussion->Body, $this->Discussion->Format));
} else {
$this->Data['Title'] .= sprintf(T(' - Page %s'), PageNumber($this->Offset, $Limit));
$FirstComment = $this->Data('Comments')->FirstRow();
$FirstBody = GetValue('Body', $FirstComment);
$FirstFormat = GetValue('Format', $FirstComment);
$this->Description(SliceParagraph(Gdn_Format::PlainText($FirstBody, $FirstFormat), 160));
// Add images to head for open graph
$Dom = str_get_html(Gdn_Format::To($FirstBody, $FirstFormat));
}
if ($Dom) {
foreach($Dom->find('img') as $img) {
if (isset($img->src))
$this->Image($img->src);
}
}
// Queue notification.
if ($this->Request->Get('new') && C('Vanilla.QueueNotifications')) {
$this->AddDefinition('NotifyNewDiscussion', 1);
}
// Make sure to set the user's discussion watch records
$this->CommentModel->SetWatch($this->Discussion, $Limit, $this->Offset, $this->Discussion->CountComments);
// Build a pager
$PagerFactory = new Gdn_PagerFactory();
$this->EventArguments['PagerType'] = 'Pager';
$this->FireEvent('BeforeBuildPager');
$this->Pager = $PagerFactory->GetPager($this->EventArguments['PagerType'], $this);
$this->Pager->ClientID = 'Pager';
$this->Pager->Configure(
$this->Offset,
$Limit,
$ActualResponses,
array('DiscussionUrl')
);
$this->Pager->Record = $this->Discussion;
PagerModule::Current($this->Pager);
$this->FireEvent('AfterBuildPager');
// Define the form for the comment input
$this->Form = Gdn::Factory('Form', 'Comment');
$this->Form->Action = Url('/vanilla/post/comment/');
$this->DiscussionID = $this->Discussion->DiscussionID;
$this->Form->AddHidden('DiscussionID', $this->DiscussionID);
$this->Form->AddHidden('CommentID', '');
// Look in the session stash for a comment
$StashComment = $Session->Stash('CommentForDiscussionID_'.$this->Discussion->DiscussionID, '', FALSE);
if ($StashComment)
$this->Form->SetFormValue('Body', $StashComment);
// Retrieve & apply the draft if there is one:
if (Gdn::Session()->UserID) {
$DraftModel = new DraftModel();
$Draft = $DraftModel->Get($Session->UserID, 0, 1, $this->Discussion->DiscussionID)->FirstRow();
$this->Form->AddHidden('DraftID', $Draft ? $Draft->DraftID : '');
if ($Draft && !$this->Form->IsPostBack()) {
$this->Form->SetValue('Body', $Draft->Body);
$this->Form->SetValue('Format', $Draft->Format);
}
}
// Deliver JSON data if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
$this->SetJson('LessRow', $this->Pager->ToString('less'));
$this->SetJson('MoreRow', $this->Pager->ToString('more'));
$this->View = 'comments';
}
// Inform moderator of checked comments in this discussion
$CheckedComments = $Session->GetAttribute('CheckedComments', array());
if (count($CheckedComments) > 0)
ModerationController::InformCheckedComments($this);
// Add modules
$this->AddModule('DiscussionFilterModule');
$this->AddModule('NewDiscussionModule');
$this->AddModule('CategoriesModule');
$this->AddModule('BookmarkedModule');
$this->CanEditComments = Gdn::Session()->CheckPermission('Vanilla.Comments.Edit', TRUE, 'Category', 'any') && C('Vanilla.AdminCheckboxes.Use');
// Report the discussion id so js can use it.
$this->AddDefinition('DiscussionID', $DiscussionID);
$this->FireEvent('BeforeDiscussionRender');
$this->Render();
}
/**
* Display comments in a discussion since a particular CommentID.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID
* @param int $LastCommentID Only shows comments posted after this one
*/
public function GetNew($DiscussionID, $LastCommentID = 0) {
$this->SetData('Discussion', $this->DiscussionModel->GetID($DiscussionID), TRUE);
// Check permissions.
$this->Permission('Vanilla.Discussions.View', TRUE, 'Category', $this->Discussion->PermissionCategoryID);
$this->SetData('CategoryID', $this->CategoryID = $this->Discussion->CategoryID, TRUE);
// Get the comments.
$Comments = $this->CommentModel->GetNew($DiscussionID, $LastCommentID)->Result();
$this->SetData('Comments', $Comments, TRUE);
// Set the data.
if(count($Comments) > 0) {
$LastComment = $Comments[count($Comments) - 1];
// Mark the comment read.
$this->SetData('Offset', $this->Discussion->CountComments, TRUE);
$this->CommentModel->SetWatch($this->Discussion, $this->Discussion->CountComments, $this->Discussion->CountComments, $this->Discussion->CountComments);
$LastCommentID = $this->Json('LastCommentID');
if(is_null($LastCommentID) || $LastComment->CommentID > $LastCommentID) {
$this->Json('LastCommentID', $LastComment->CommentID);
}
} else {
$this->SetData('Offset', $this->CommentModel->GetOffset($LastCommentID), TRUE);
}
$this->View = 'comments';
$this->Render();
}
/**
* Highlight route.
*
* Always called by dispatcher before controller's requested method.
*
* @since 2.0.0
* @access public
*/
public function Initialize() {
parent::Initialize();
$this->AddDefinition('ImageResized', T('This image has been resized to fit in the page. Click to enlarge.'));
$this->Menu->HighlightRoute('/discussions');
}
/**
* Display discussion page starting with a particular comment.
*
* @since 2.0.0
* @access public
*
* @param int $CommentID Unique comment ID
*/
public function Comment($CommentID) {
// Get the discussionID
$Comment = $this->CommentModel->GetID($CommentID);
if (!$Comment)
throw NotFoundException('Comment');
$DiscussionID = $Comment->DiscussionID;
// Figure out how many comments are before this one
$Offset = $this->CommentModel->GetOffset($Comment);
$Limit = Gdn::Config('Vanilla.Comments.PerPage', 30);
$PageNumber = PageNumber($Offset, $Limit, TRUE);
$this->SetData('Page', $PageNumber);
$this->View = 'index';
$this->Index($DiscussionID, 'x', $PageNumber);
}
/**
* Allows user to remove announcement.
*
* Users may remove announcements from being displayed for themselves only.
* Does not affect what announcements are shown for other users.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
* @param string $TransientKey Single-use hash to prove intent.
*/
public function DismissAnnouncement($DiscussionID = '') {
// Confirm announcements may be dismissed
if (!C('Vanilla.Discussions.Dismiss', 1)) {
throw PermissionException('Vanilla.Discussions.Dismiss');
}
// Make sure we are posting back.
if (!$this->Request->IsPostBack())
throw PermissionException('Javascript');
$Session = Gdn::Session();
if (
is_numeric($DiscussionID)
&& $DiscussionID > 0
&& $Session->UserID > 0
) $this->DiscussionModel->DismissAnnouncement($DiscussionID, $Session->UserID);
// Redirect back where the user came from if necessary
if ($this->_DeliveryType === DELIVERY_TYPE_ALL)
Redirect('discussions');
$this->JsonTarget("#Discussion_$DiscussionID", NULL, 'SlideUp');
$this->Render('Blank', 'Utility', 'Dashboard');
}
/**
* Allows user to bookmark or unbookmark a discussion.
*
* If the discussion isn't bookmarked by the user, this bookmarks it.
* If it is already bookmarked, this unbookmarks it.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
*/
public function Bookmark($DiscussionID) {
// Make sure we are posting back.
if (!$this->Request->IsPostBack())
throw PermissionException('Javascript');
$Session = Gdn::Session();
if ($Session->UserID == 0)
throw PermissionException('SignedIn');
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
$State = $this->DiscussionModel->BookmarkDiscussion($DiscussionID, $Session->UserID, $Discussion);
// Update the user's bookmark count
$CountBookmarks = $this->DiscussionModel->SetUserBookmarkCount($Session->UserID);
$this->JsonTarget('.User-CountBookmarks', (string)$CountBookmarks);
// Return the appropriate bookmark.
require_once $this->FetchViewLocation('helper_functions', 'Discussions');
$Html = BookmarkButton($Discussion);
// $this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID .Bookmark,.Section-Discussion .PageTitle .Bookmark", $Html, 'ReplaceWith');
$this->JsonTarget("!element", $Html, 'ReplaceWith');
// Add the bookmark to the bookmarks module.
if($State) {
// Grab the individual bookmark and send it to the client.
$Bookmarks = new BookmarkedModule($this);
if($CountBookmarks == 1) {
// When there is only one bookmark we have to get the whole module.
$Target = '#Panel';
$Type = 'Append';
$Bookmarks->GetData();
$Data = $Bookmarks->ToString();
} else {
$Target = '#Bookmark_List';
$Type = 'Prepend';
$Loc = $Bookmarks->FetchViewLocation('discussion');
ob_start();
include($Loc);
$Data = ob_get_clean();
}
$this->JsonTarget($Target, $Data, $Type);
} else {
// Send command to remove bookmark html.
if($CountBookmarks == 0) {
$this->JsonTarget('#Bookmarks', NULL, 'Remove');
} else {
$this->JsonTarget('#Bookmark_'.$DiscussionID, NULL, 'Remove');
}
}
$this->Render('Blank', 'Utility', 'Dashboard');
}
/**
* Allows user to announce or unannounce a discussion.
*
* If the discussion isn't announced, this announces it.
* If it is already announced, this unannounces it.
* Announced discussions stay at the top of the discussions
* list regardless of how long ago the last comment was.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
* @param string $TransientKey Single-use hash to prove intent.
*/
public function Announce($DiscussionID = '', $Target = '') {
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
$this->Permission('Vanilla.Discussions.Announce', TRUE, 'Category', $Discussion->PermissionCategoryID);
if ($this->Form->IsPostBack()) {
// Save the property.
$CacheKeys = array('Announcements', 'Announcements_'.GetValue('CategoryID', $Discussion));
$this->DiscussionModel->SQL->Cache($CacheKeys);
$this->DiscussionModel->SetProperty($DiscussionID, 'Announce', (int)$this->Form->GetFormValue('Announce', 0));
if ($Target)
$this->RedirectUrl = Url($Target);
} else {
if (!$Discussion->Announce)
$Discussion->Announce = 2;
$this->Form->SetData($Discussion);
}
$Discussion = (array)$Discussion;
$Category = CategoryModel::Categories($Discussion['CategoryID']);
$this->SetData('Discussion', $Discussion);
$this->SetData('Category', $Category);
$this->Title(T('Announce'));
$this->Render();
}
public function SendOptions($Discussion) {
require_once $this->FetchViewLocation('helper_functions', 'Discussion');
ob_start();
WriteDiscussionOptions($Discussion);
$Options = ob_get_clean();
$this->JsonTarget("#Discussion_{$Discussion->DiscussionID} .OptionsMenu,.Section-Discussion .Discussion .OptionsMenu", $Options, 'ReplaceWith');
}
/**
* Allows user to sink or unsink a discussion.
*
* If the discussion isn't sunk, this sinks it. If it is already sunk,
* this unsinks it. Sunk discussions do not move to the top of the
* discussion list when a new comment is added.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
* @param bool $Sink Whether or not to unsink the discussion.
*/
public function Sink($DiscussionID = '', $Sink = TRUE, $From = 'list') {
// Make sure we are posting back.
if (!$this->Request->IsPostBack())
throw PermissionException('Javascript');
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
$this->Permission('Vanilla.Discussions.Sink', TRUE, 'Category', $Discussion->PermissionCategoryID);
// Sink the discussion.
$this->DiscussionModel->SetField($DiscussionID, 'Sink', $Sink);
$Discussion->Sink = $Sink;
// Redirect to the front page
if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
$Target = GetIncomingValue('Target', 'discussions');
Redirect($Target);
}
$this->SendOptions($Discussion);
$this->JsonTarget("#Discussion_$DiscussionID", NULL, 'Highlight');
$this->JsonTarget(".Discussion #Item_0", NULL, 'Highlight');
$this->Render('Blank', 'Utility', 'Dashboard');
}
/**
* Allows user to close or re-open a discussion.
*
* If the discussion isn't closed, this closes it. If it is already
* closed, this re-opens it. Closed discussions may not have new
* comments added to them.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
* @param bool $Close Whether or not to close the discussion.
*/
public function Close($DiscussionID = '', $Close = TRUE, $From = 'list') {
// Make sure we are posting back.
if (!$this->Request->IsPostBack())
throw PermissionException('Javascript');
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
$this->Permission('Vanilla.Discussions.Close', TRUE, 'Category', $Discussion->PermissionCategoryID);
// Close the discussion.
$this->DiscussionModel->SetField($DiscussionID, 'Closed', $Close);
$Discussion->Closed = $Close;
// Redirect to the front page
if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
$Target = GetIncomingValue('Target', 'discussions');
Redirect($Target);
}
$this->SendOptions($Discussion);
if ($Close) {
require_once $this->FetchViewLocation('helper_functions', 'Discussions');
$this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID .Meta-Discussion", Tag($Discussion, 'Closed', 'Closed'), 'Prepend');
$this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID", 'Closed', 'AddClass');
} else {
$this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID .Tag-Closed", NULL, 'Remove');
$this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID", 'Closed', 'RemoveClass');
}
$this->JsonTarget("#Discussion_$DiscussionID", NULL, 'Highlight');
$this->JsonTarget(".Discussion #Item_0", NULL, 'Highlight');
$this->Render('Blank', 'Utility', 'Dashboard');
}
/**
* Allows user to delete a discussion.
*
* This is a "hard" delete - it is removed from the database.
*
* @since 2.0.0
* @access public
*
* @param int $DiscussionID Unique discussion ID.
*/
public function Delete($DiscussionID, $Target = '') {
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
$this->Permission('Vanilla.Discussions.Delete', TRUE, 'Category', $Discussion->PermissionCategoryID);
if ($this->Form->IsPostBack()) {
if (!$this->DiscussionModel->Delete($DiscussionID))
$this->Form->AddError('Failed to delete discussion');
if ($this->Form->ErrorCount() == 0) {
if ($this->_DeliveryType === DELIVERY_TYPE_ALL)
Redirect($Target);
if ($Target)
$this->RedirectUrl = Url($Target);
$this->JsonTarget(".Section-DiscussionList #Discussion_$DiscussionID", NULL, 'SlideUp');
}
}
$this->SetData('Title', T('Delete Discussion'));
$this->Render();
}
/**
* Allows user to delete a comment.
*
* If the comment is the only one in the discussion, the discussion will
* be deleted as well. Users without administrative delete abilities
* should not be able to delete a comment unless it is a draft. This is
* a "hard" delete - it is removed from the database.
*
* @since 2.0.0
* @access public
*
* @param int $CommentID Unique comment ID.
* @param string $TransientKey Single-use hash to prove intent.
*/
public function DeleteComment($CommentID = '', $TransientKey = '') {
$Session = Gdn::Session();
$DefaultTarget = '/discussions/';
$ValidCommentID = is_numeric($CommentID) && $CommentID > 0;
$ValidUser = $Session->UserID > 0 && $Session->ValidateTransientKey($TransientKey);
if ($ValidCommentID && $ValidUser) {
// Get comment and discussion data
$Comment = $this->CommentModel->GetID($CommentID);
$DiscussionID = GetValue('DiscussionID', $Comment);
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if ($Comment && $Discussion) {
$DefaultTarget = DiscussionUrl($Discussion);
// Make sure comment is this user's or they have Delete permission
if ($Comment->InsertUserID != $Session->UserID || !C('Vanilla.Comments.AllowSelfDelete'))
$this->Permission('Vanilla.Comments.Delete', TRUE, 'Category', $Discussion->PermissionCategoryID);
// Make sure that content can (still) be edited
$EditContentTimeout = C('Garden.EditContentTimeout', -1);
$CanEdit = $EditContentTimeout == -1 || strtotime($Comment->DateInserted) + $EditContentTimeout > time();
if (!$CanEdit)
$this->Permission('Vanilla.Comments.Delete', TRUE, 'Category', $Discussion->PermissionCategoryID);
// Delete the comment
if (!$this->CommentModel->Delete($CommentID))
$this->Form->AddError('Failed to delete comment');
}
else {
$this->Form->AddError('Invalid comment');
}
} else {
$this->Form->AddError('ErrPermission');
}
// Redirect
if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
$Target = GetIncomingValue('Target', $DefaultTarget);
Redirect($Target);
}
if ($this->Form->ErrorCount() > 0) {
$this->SetJson('ErrorMessage', $this->Form->Errors());
} else {
$this->JsonTarget("#Comment_$CommentID", '', 'SlideUp');
}
$this->Render();
}
/**
* Alternate version of Index that uses the embed master view.
*/
public function Embed($DiscussionID = '', $DiscussionStub = '', $Offset = '', $Limit = '') {
$this->Title(T('Comments'));
$this->AddDefinition('DoInform', '0'); // Suppress inform messages on embedded page.
$this->AddDefinition('SelfUrl', Gdn::Request()->PathAndQuery());
$this->AddDefinition('Embedded', TRUE);
$this->CanEditComments = FALSE; // Don't show the comment checkboxes on the embed comments page
$this->Theme = C('Garden.CommentsTheme', $this->Theme);
Gdn_Theme::Section('Comments');
// Add some css to help with the transparent bg on embedded comments
if ($this->Head)
$this->Head->AddString('<style type="text/css">
body { background: transparent !important; }
</style>');
$Session = Gdn::Session();
$this->AddJsFile('jquery.gardenmorepager.js');
$this->AddJsFile('jquery.autogrow.js');
$this->AddJsFile('discussion.js');
$this->RemoveJsFile('autosave.js');
$this->MasterView = 'empty';
// Define incoming variables (prefer querystring parameters over method parameters)
$DiscussionID = (is_numeric($DiscussionID) && $DiscussionID > 0) ? $DiscussionID : 0;
$DiscussionID = GetIncomingValue('vanilla_discussion_id', $DiscussionID);
$Offset = GetIncomingValue('Offset', $Offset);
$Limit = GetIncomingValue('Limit', $Limit);
$vanilla_identifier = GetIncomingValue('vanilla_identifier', '');
// Only allow vanilla identifiers of 32 chars or less - md5 if larger
if (strlen($vanilla_identifier) > 32) {
$vanilla_identifier = md5($vanilla_identifier);
}
$vanilla_type = GetIncomingValue('vanilla_type', 'page');
$vanilla_url = GetIncomingValue('vanilla_url', '');
$vanilla_category_id = GetIncomingValue('vanilla_category_id', '');
$ForeignSource = array(
'vanilla_identifier' => $vanilla_identifier,
'vanilla_type' => $vanilla_type,
'vanilla_url' => $vanilla_url,
'vanilla_category_id' => $vanilla_category_id
);
$this->SetData('ForeignSource', $ForeignSource);
$SortComments = C('Garden.Embed.SortComments') == 'desc' ? 'desc' : 'asc';
$this->SetData('SortComments', $SortComments);
// Retrieve the discussion record.
$Discussion = FALSE;
if ($DiscussionID > 0) {
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
} else if ($vanilla_identifier != '' && $vanilla_type != '') {
$Discussion = $this->DiscussionModel->GetForeignID($vanilla_identifier, $vanilla_type);
}
if ($Discussion) {
$this->SetData('Discussion', $Discussion, TRUE);
$this->SetData('DiscussionID', $Discussion->DiscussionID, TRUE);
$this->Title($this->Discussion->Name);
// Actual number of comments, excluding the discussion itself
$ActualResponses = $this->Discussion->CountComments;
// Define the query offset & limit
if (!is_numeric($Limit) || $Limit < 0)
$Limit = C('Garden.Embed.CommentsPerPage', 30);
$OffsetProvided = $Offset != '';
list($Offset, $Limit) = OffsetLimit($Offset, $Limit);
$this->Offset = $Offset;
if (C('Vanilla.Comments.AutoOffset')) {
if ($ActualResponses <= $Limit)
$this->Offset = 0;
if ($this->Offset == $ActualResponses)
$this->Offset -= $Limit;
} else if ($this->Offset == '')
$this->Offset = 0;
if ($this->Offset < 0)
$this->Offset = 0;
// Set the canonical url to have the proper page title.
$this->CanonicalUrl(DiscussionUrl($Discussion, PageNumber($this->Offset, $Limit)));
// Load the comments.
$CurrentOrderBy = $this->CommentModel->OrderBy();
if (StringBeginsWith(GetValueR('0.0', $CurrentOrderBy), 'c.DateInserted'))
$this->CommentModel->OrderBy('c.DateInserted '.$SortComments); // allow custom sort
$this->SetData('Comments', $this->CommentModel->Get($this->Discussion->DiscussionID, $Limit, $this->Offset), TRUE);
if (count($this->CommentModel->Where()) > 0)
$ActualResponses = FALSE;
$this->SetData('_Count', $ActualResponses);
// Build a pager
$PagerFactory = new Gdn_PagerFactory();
$this->EventArguments['PagerType'] = 'MorePager';
$this->FireEvent('BeforeBuildPager');
$this->Pager = $PagerFactory->GetPager($this->EventArguments['PagerType'], $this);
$this->Pager->ClientID = 'Pager';
$this->Pager->MoreCode = 'More Comments';
$this->Pager->Configure(
$this->Offset,
$Limit,
$ActualResponses,
'discussion/embed/'.$this->Discussion->DiscussionID.'/'.Gdn_Format::Url($this->Discussion->Name).'/%1$s'
);
$this->Pager->CurrentRecords = $this->Comments->NumRows();
$this->FireEvent('AfterBuildPager');
}
// Define the form for the comment input
$this->Form = Gdn::Factory('Form', 'Comment');
$this->Form->Action = Url('/vanilla/post/comment/');
$this->Form->AddHidden('CommentID', '');
$this->Form->AddHidden('Embedded', 'true'); // Tell the post controller that this is an embedded page (in case there are custom views it needs to pick up from a theme).
$this->Form->AddHidden('DisplayNewCommentOnly', 'true'); // Only load/display the new comment after posting (don't load all new comments since the page last loaded).
if ($this->Request->Get('title')) {
$this->Form->SetValue('Name', $this->Request->Get('title'));
}
if ($Discussion) {
$this->Form->AddHidden('DiscussionID', $Discussion->DiscussionID);
}
foreach ($ForeignSource as $Key => $Val) {
// Drop the foreign source information into the form so it can be used if creating a discussion
$this->Form->AddHidden($Key, $Val);
// Also drop it into the definitions so it can be picked up for stashing comments
$this->AddDefinition($Key, $Val);
}
// Retrieve & apply the draft if there is one:
$Draft = FALSE;
if (Gdn::Session()->UserID && $Discussion) {
$DraftModel = new DraftModel();
$Draft = $DraftModel->Get($Session->UserID, 0, 1, $Discussion->DiscussionID)->FirstRow();
$this->Form->AddHidden('DraftID', $Draft ? $Draft->DraftID : '');
}
if ($Draft)
$this->Form->SetFormValue('Body', $Draft->Body);
else {
// Look in the session stash for a comment
$StashComment = $Session->Stash('CommentForForeignID_'.$ForeignSource['vanilla_identifier'], '', FALSE);
if ($StashComment)
$this->Form->SetFormValue('Body', $StashComment);
}
// Deliver JSON data if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
if ($this->Discussion) {
$this->SetJson('LessRow', $this->Pager->ToString('less'));
$this->SetJson('MoreRow', $this->Pager->ToString('more'));
}
$this->View = 'comments';
}
if ($SortComments == 'desc')
$this->AddDefinition('PrependNewComments', '1');
// Report the discussion id so js can use it.
if ($Discussion)
$this->AddDefinition('DiscussionID', $Discussion->DiscussionID);
$this->FireEvent('BeforeDiscussionRender');
$this->Render();
}
/**
* Redirect to the url specified by the discussion.
* @param array|object $Discussion
*/
protected function RedirectDiscussion($Discussion) {
$Body = Gdn_Format::To(GetValue('Body', $Discussion), GetValue('Format', $Discussion));
if (preg_match('`href="([^"]+)"`i', $Body, $Matches)) {
$Url = $Matches[1];
Redirect($Url, 301);
}
}
/**
* Re-fetch a discussion's content based on its foreign url.
* @param type $DiscussionID
*/
public function RefetchPageInfo($DiscussionID) {
// Make sure we are posting back.
if (!$this->Request->IsPostBack())
throw PermissionException('Javascript');
// Grab the discussion.
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if (!$Discussion)
throw NotFoundException('Discussion');
// Make sure the user has permission to edit this discussion.
$this->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $Discussion->PermissionCategoryID);
$ForeignUrl = GetValueR('Attributes.ForeignUrl', $Discussion);
if (!$ForeignUrl) {
throw new Gdn_UserException(T("This discussion isn't associated with a url."));
}
$Stub = $this->DiscussionModel->FetchPageInfo($ForeignUrl, TRUE);
// decho($Stub);
// die();
// Save the stub.
$this->DiscussionModel->SetField($DiscussionID, (array)$Stub);
// Send some of the stuff back.
if (isset($Stub['Name']))
$this->JsonTarget('.PageTitle h1', Gdn_Format::Text($Stub['Name']));
if (isset($Stub['Body']))
$this->JsonTarget("#Discussion_$DiscussionID .Message", Gdn_Format::To($Stub['Body'], $Stub['Format']));
$this->InformMessage('The page was successfully fetched.');
$this->Render('Blank', 'Utility', 'Dashboard');
}
protected function _SetOpenGraph() {
if (!$this->Head)
return;
$this->Head->AddTag('meta', array('property' => 'og:type', 'content' => 'article'));
}
}