3
3
* Group controller
4
4
*/
5
5
6
+ use Garden \Schema \Validation ;
7
+ use Garden \Schema \ValidationException ;
8
+ use Garden \Web \Exception \ClientException ;
6
9
use Vanilla \Message ;
7
10
use Cocur \Slugify \Slugify ;
8
11
12
+
9
13
/**
10
14
* Handles accessing & displaying a single group via /group endpoint.
11
15
*/
@@ -108,6 +112,7 @@ public function index($GroupID = '') {
108
112
}
109
113
}
110
114
115
+ $ this ->setData ('DefaultAnnouncementUrl ' , $ defaultDiscussionUrl .'?announce=1 ' );
111
116
$ this ->setData ('DefaultDiscussionUrl ' , $ defaultDiscussionUrl );
112
117
113
118
// Find all discussions with content from after DateMarkedRead.
@@ -405,9 +410,27 @@ public function invite($GroupID) {
405
410
if (GroupModel::isMemberOfGroup ($ user ->UserID , $ GroupID )) {
406
411
$ this ->Form ->addError ('User is a member of " ' .$ Group ->Name .'". ' );
407
412
} else {
408
- $ this ->GroupModel ->invite ($ GroupID , $ user ->UserID );
409
- $ this ->informMessage ('Invitation was sent. ' );
410
- $ this ->render ('invitation_sent ' );
413
+ $ groupInvitation ['GroupID ' ] = $ GroupID ;
414
+ $ groupInvitation ['InviteeUserID ' ] = $ user ->UserID ;
415
+ $ groupInvitationModel = new GroupInvitationModel ();
416
+ $ result = $ groupInvitationModel ->save ($ groupInvitation );
417
+ if ($ result ) {
418
+ $ this ->informMessage ('Invitation was sent. ' );
419
+ } else {
420
+ $ validationErrors = $ groupInvitationModel ->validationResults ();
421
+ $ validation = new Validation ();
422
+ foreach ($ validationErrors as $ field => $ errors ) {
423
+ foreach ($ errors as $ error ) {
424
+ $ validation ->addError (
425
+ $ field ,
426
+ $ error
427
+ );
428
+ }
429
+ }
430
+ $ this ->Form ->addError ($ validation ->getMessage ());
431
+ $ this ->render ();
432
+ }
433
+
411
434
}
412
435
} catch (\Exception $ e ) {
413
436
$ this ->Form ->addError ('Error ' . $ e ->getMessage ());
@@ -493,7 +516,13 @@ public function watch($GroupID) {
493
516
$ this ->setData ('Group ' , $ Group );
494
517
if ($ this ->Form ->authenticatedPostBack (true )) {
495
518
$ this ->GroupModel ->watchGroup ($ Group , Gdn::session ()->UserID );
496
- $ this ->setRedirectTo ('group/ ' . $ GroupID );
519
+ // Stay in the previous page
520
+ if (isset ($ _SERVER ['HTTP_REFERER ' ])) {
521
+ $ previous = $ _SERVER ['HTTP_REFERER ' ];
522
+ $ this ->setRedirectTo ($ previous );
523
+ } else {
524
+ $ this ->setRedirectTo ('group/ ' .$ GroupID );
525
+ }
497
526
}
498
527
$ this ->render ();
499
528
}
@@ -512,7 +541,13 @@ public function unwatch($GroupID) {
512
541
$ this ->setData ('Group ' , $ Group );
513
542
if ($ this ->Form ->authenticatedPostBack (true )) {
514
543
$ this ->GroupModel ->unwatchGroup ($ Group , Gdn::session ()->UserID );
515
- $ this ->setRedirectTo ('group/ ' .$ GroupID );
544
+ // Stay in the previous page
545
+ if (isset ($ _SERVER ['HTTP_REFERER ' ])) {
546
+ $ previous = $ _SERVER ['HTTP_REFERER ' ];
547
+ $ this ->setRedirectTo ($ previous );
548
+ } else {
549
+ $ this ->setRedirectTo ('group/ ' .$ GroupID );
550
+ }
516
551
}
517
552
$ this ->render ();
518
553
}
@@ -523,11 +558,41 @@ public function unwatch($GroupID) {
523
558
* @param $UserID
524
559
* @throws Gdn_UserException
525
560
*/
526
- public function accept ($ GroupID , $ UserID ) {
527
- if (!GroupModel::isMemberOfGroup ($ UserID , $ GroupID ) ) {
528
- $ this ->GroupModel ->accept ($ GroupID , $ UserID );
561
+ public function accept ($ token ='' ) {
562
+
563
+ if (!Gdn::session ()->isValid ()) {
564
+ redirectTo (signInUrl ());
565
+ }
566
+
567
+ $ groupInvitationModel = new GroupInvitationModel ();
568
+
569
+ $ result = $ groupInvitationModel ->validateToken ($ token );
570
+ $ validationErrors = $ groupInvitationModel ->Validation ->results ();
571
+ if (count ($ validationErrors ) > 0 ) {
572
+ $ validation = new Validation ();
573
+ foreach ($ validationErrors as $ field => $ errors ) {
574
+ foreach ($ errors as $ error ) {
575
+ $ validation ->addError (
576
+ $ field ,
577
+ $ error
578
+ );
579
+ }
580
+ }
581
+ if ($ validation ->getErrorCount () > 0 ) {
582
+ $ this ->setData ('ErrorMessage ' , $ validation ->getMessage ());
583
+ $ this ->render ();
584
+ }
585
+ } else {
586
+ if (!GroupModel::isMemberOfGroup ($ result ['InviteeUserID ' ], $ result ['GroupID ' ]) ) {
587
+ $ GroupModel = new GroupModel ();
588
+ $ GroupModel ->join ($ result ['GroupID ' ],$ result ['InviteeUserID ' ]);
589
+ }
590
+ $ result ['Status ' ] = 'accepted ' ;
591
+ $ result ['DateAccepted ' ] = Gdn_Format::toDateTime ();
592
+ $ groupInvitationModel ->save ($ result );
593
+ redirectTo (GroupsPlugin::GROUP_ROUTE .$ result ['GroupID ' ]);
529
594
}
530
- redirectTo (GroupsPlugin:: GROUP_ROUTE . $ GroupID );
595
+
531
596
}
532
597
533
598
@@ -668,134 +733,6 @@ public function discussions($GroupID='',$Page = false) {
668
733
}
669
734
670
735
671
- /**
672
- * Create a new announcement
673
- * @param string $GroupID
674
- * @throws Gdn_UserException
675
- */
676
- public function announcement ($ GroupID ='' ){
677
-
678
- $ Group = $ this ->findGroup ($ GroupID );
679
-
680
- if (!$ this ->GroupModel ->canAddAnnouncement ($ Group )) {
681
- throw permissionException ();
682
- }
683
-
684
- $ this ->setData ('Breadcrumbs ' ,
685
- [['Name ' => t ('Challenge Discussions ' ), 'Url ' => GroupsPlugin::GROUPS_ROUTE ],
686
- ['Name ' => $ Group ->Name , 'Url ' => GroupsPlugin::GROUP_ROUTE .$ Group ->GroupID ], ['Name ' => t ('New Announcement ' )]]);
687
- $ this ->title ('New Announcement ' );
688
- $ this ->setDiscussionData ($ Group , 2 );
689
- $ this ->View = 'discussion ' ;
690
- $ this ->render ();
691
- }
692
-
693
- /**
694
- * Create a new discussion
695
- * @param string $GroupID
696
- * @throws Gdn_UserException
697
- */
698
- public function discussion ($ GroupID ='' ){
699
- $ Group = $ this ->findGroup ($ GroupID );
700
-
701
- if (!$ this ->GroupModel ->canAddDiscussion ($ Group )) {
702
- throw permissionException ();
703
- }
704
-
705
- $ this ->setData ('Breadcrumbs ' , [['Name ' => t ('Challenge Discussions ' ), 'Url ' => GroupsPlugin::GROUPS_ROUTE ],
706
- ['Name ' => $ Group ->Name , 'Url ' => GroupsPlugin::GROUP_ROUTE .$ Group ->GroupID ], ['Name ' => t ('New Discussion ' )]]);
707
- $ this ->title ('New Discussion ' );
708
- $ this ->setDiscussionData ($ Group , 1 );
709
- $ this ->View = 'discussion ' ;
710
- $ this ->render ();
711
-
712
- }
713
-
714
- /**
715
- * Create a discussion.
716
- * @param int $categoryID Unique ID of the category to add the discussion to.
717
- */
718
- public function setDiscussionData ($ Group ,$ Announce = '0 ' ) {
719
- $ categoryUrlCode =$ Group ->ChallengeID ;//.'-questions';
720
- $ useCategories = true ;
721
-
722
- // Setup head
723
- $ this ->addJsFile ('jquery.autosize.min.js ' );
724
- $ this ->addJsFile ('autosave.js ' );
725
- $ this ->addJsFile ('post.js ' );
726
-
727
- $ session = Gdn::session ();
728
-
729
- Gdn_Theme::section ('PostDiscussion ' );
730
-
731
- // Set discussion, draft, and category data
732
- $ discussionID = isset ($ this ->Discussion ) ? $ this ->Discussion ->DiscussionID : '' ;
733
- $ draftID = isset ($ this ->Draft ) ? $ this ->Draft ->DraftID : 0 ;
734
- $ category = false ;
735
- $ categoryModel = new CategoryModel ();
736
- $ category = CategoryModel::categories ($ categoryUrlCode );
737
- if ($ category ) {
738
- $ this ->CategoryID = val ('CategoryID ' , $ category );
739
- }
740
-
741
- if ($ category ) {
742
- $ this ->Category = (object )$ category ;
743
- $ this ->setData ('Category ' , $ category );
744
- $ this ->Form ->addHidden ('CategoryID ' , $ this ->Category ->CategoryID );
745
- }
746
-
747
- $ categoryData = $ this ->ShowCategorySelector ? CategoryModel::categories () : false ;
748
- if (!$ useCategories || $ this ->ShowCategorySelector ) {
749
- // See if we should fill the CategoryID value.
750
- $ allowedCategories = CategoryModel::getByPermission (
751
- 'Discussions.Add ' ,
752
- $ this ->Form ->getValue ('CategoryID ' , $ this ->CategoryID ),
753
- ['Archived ' => 0 , 'AllowDiscussions ' => 1 ],
754
- ['AllowedDiscussionTypes ' => $ this ->Data ['Type ' ]]
755
- );
756
- $ allowedCategoriesCount = count ($ allowedCategories );
757
-
758
- if ($ this ->ShowCategorySelector && $ allowedCategoriesCount === 1 ) {
759
- $ this ->ShowCategorySelector = false ;
760
- }
761
-
762
- if (!$ this ->ShowCategorySelector && $ allowedCategoriesCount ) {
763
- $ allowedCategory = array_pop ($ allowedCategories );
764
- $ this ->Form ->addHidden ('CategoryID ' , $ allowedCategory ['CategoryID ' ]);
765
-
766
- if ($ this ->Form ->isPostBack () && !$ this ->Form ->getFormValue ('CategoryID ' )) {
767
- $ this ->Form ->setFormValue ('CategoryID ' , $ allowedCategory ['CategoryID ' ]);
768
- }
769
- }
770
- }
771
-
772
- // Set the model on the form
773
- $ DiscussionModel = new DiscussionModel ();
774
- $ this ->Form ->setModel ($ DiscussionModel );
775
- $ this ->Form ->addHidden ('GroupID ' , $ Group ->GroupID );
776
- $ this ->Form ->Action = '/post/discussion ' ;
777
- $ this ->Form ->setFormValue ('Announce ' , $ Announce );
778
- $ this ->setData ('Group ' , $ Group );
779
- $ this ->setData ('Announce ' , $ Announce );
780
- $ this ->setData ('_AnnounceOptions ' , $ this ->announceOptions ());
781
-
782
- $ this ->fireEvent ('BeforeDiscussionRender ' );
783
-
784
- if ($ this ->CategoryID ) {
785
- $ breadcrumbs = CategoryModel::getAncestors ($ this ->CategoryID );
786
- } else {
787
- $ breadcrumbs = [];
788
- }
789
-
790
- $ breadcrumbs [] = [
791
- 'Name ' => $ this ->data ('Title ' ),
792
- 'Url ' => val ('AddUrl ' , val ($ this ->data ('Type ' ), DiscussionModel::discussionTypes ()), '/post/discussion ' )
793
- ];
794
-
795
- $ this ->setData ('Breadcrumbs ' , $ breadcrumbs );
796
-
797
- }
798
-
799
736
/**
800
737
* Join a group
801
738
* @param $GroupID
0 commit comments