Skip to content

Commit c729da5

Browse files
authored
Merge pull request #65 from topcoder-platform/develop
V1.3
2 parents 3531db8 + e458a94 commit c729da5

8 files changed

+395
-352
lines changed

class.groups.plugin.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,6 @@ public function discussionController_render_before($sender, $args) {
272272

273273
}
274274

275-
public function base_BeforeCommentForm_handler($sender, $args) {
276-
if($sender instanceof DiscussionController && $sender->Form->Action === '/post/comment/') {
277-
$categoryID = $sender->data('Discussion.CategoryID');
278-
$groupID = $sender->data('Discussion.GroupID');
279-
$groupModel = new GroupModel();
280-
if(!$groupModel->canAddComment($categoryID, $groupID)) {
281-
$cssClass = &$args['FormCssClass'];
282-
$cssClass = 'hidden';
283-
}
284-
}
285-
}
286-
287275
/**
288276
* The '...' discussion dropdown options
289277
*/
@@ -295,8 +283,6 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
295283
$data = $sender->Data;
296284
// $currentTopcoderProjectRoles = val('ChallengeCurrentUserProjectRoles', $data, []);
297285
$groupModel = new GroupModel();
298-
// $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
299-
$canView = $groupModel->canViewDiscussion($Discussion);
300286
$canEdit = $groupModel->canEditDiscussion($Discussion);
301287
$canDelete = $groupModel->canDeleteDiscussion($Discussion);
302288
$canDismiss = $groupModel->canDismissDiscussion($Discussion);
@@ -343,7 +329,6 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
343329
}
344330

345331
self::log('discussionController_discussionOptionsDropdown_handler', ['Discussion' => $Discussion->DiscussionID,
346-
'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles, 'canView' => $canView,
347332
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDismiss' => $canDismiss,
348333
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canReFetch' => $canRefetch ]);
349334
}

controllers/class.groupcontroller.php

Lines changed: 74 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
* Group controller
44
*/
55

6+
use Garden\Schema\Validation;
7+
use Garden\Schema\ValidationException;
8+
use Garden\Web\Exception\ClientException;
69
use Vanilla\Message;
710
use Cocur\Slugify\Slugify;
811

12+
913
/**
1014
* Handles accessing & displaying a single group via /group endpoint.
1115
*/
@@ -108,6 +112,7 @@ public function index($GroupID = '') {
108112
}
109113
}
110114

115+
$this->setData('DefaultAnnouncementUrl', $defaultDiscussionUrl.'?announce=1');
111116
$this->setData('DefaultDiscussionUrl', $defaultDiscussionUrl);
112117

113118
// Find all discussions with content from after DateMarkedRead.
@@ -405,9 +410,27 @@ public function invite($GroupID) {
405410
if(GroupModel::isMemberOfGroup($user->UserID, $GroupID)) {
406411
$this->Form->addError('User is a member of "'.$Group->Name.'".');
407412
} 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+
411434
}
412435
} catch (\Exception $e) {
413436
$this->Form->addError('Error' . $e->getMessage());
@@ -493,7 +516,13 @@ public function watch($GroupID) {
493516
$this->setData('Group', $Group);
494517
if ($this->Form->authenticatedPostBack(true)) {
495518
$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+
}
497526
}
498527
$this->render();
499528
}
@@ -512,7 +541,13 @@ public function unwatch($GroupID) {
512541
$this->setData('Group', $Group);
513542
if ($this->Form->authenticatedPostBack(true)) {
514543
$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+
}
516551
}
517552
$this->render();
518553
}
@@ -523,11 +558,41 @@ public function unwatch($GroupID) {
523558
* @param $UserID
524559
* @throws Gdn_UserException
525560
*/
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']);
529594
}
530-
redirectTo(GroupsPlugin::GROUP_ROUTE.$GroupID);
595+
531596
}
532597

533598

@@ -668,134 +733,6 @@ public function discussions($GroupID='',$Page = false) {
668733
}
669734

670735

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-
799736
/**
800737
* Join a group
801738
* @param $GroupID

0 commit comments

Comments
 (0)