Skip to content

V1.3 #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 16, 2021
15 changes: 0 additions & 15 deletions class.groups.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,6 @@ public function discussionController_render_before($sender, $args) {

}

public function base_BeforeCommentForm_handler($sender, $args) {
if($sender instanceof DiscussionController && $sender->Form->Action === '/post/comment/') {
$categoryID = $sender->data('Discussion.CategoryID');
$groupID = $sender->data('Discussion.GroupID');
$groupModel = new GroupModel();
if(!$groupModel->canAddComment($categoryID, $groupID)) {
$cssClass = &$args['FormCssClass'];
$cssClass = 'hidden';
}
}
}

/**
* The '...' discussion dropdown options
*/
Expand All @@ -295,8 +283,6 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
$data = $sender->Data;
// $currentTopcoderProjectRoles = val('ChallengeCurrentUserProjectRoles', $data, []);
$groupModel = new GroupModel();
// $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles);
$canView = $groupModel->canViewDiscussion($Discussion);
$canEdit = $groupModel->canEditDiscussion($Discussion);
$canDelete = $groupModel->canDeleteDiscussion($Discussion);
$canDismiss = $groupModel->canDismissDiscussion($Discussion);
Expand Down Expand Up @@ -343,7 +329,6 @@ public function base_discussionOptionsDropdown_handler($sender, $args){
}

self::log('discussionController_discussionOptionsDropdown_handler', ['Discussion' => $Discussion->DiscussionID,
'currentUserTopcoderProjectRoles' =>$currentTopcoderProjectRoles, 'canView' => $canView,
'canDelete' => $canDelete, 'canEdit' => $canEdit, 'canDismiss' => $canDismiss,
'canAnnounce' =>$canAnnounce, 'canSink' => $canSink, 'canMove' => $canMove, 'canReFetch' => $canRefetch ]);
}
Expand Down
211 changes: 74 additions & 137 deletions controllers/class.groupcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* Group controller
*/

use Garden\Schema\Validation;
use Garden\Schema\ValidationException;
use Garden\Web\Exception\ClientException;
use Vanilla\Message;
use Cocur\Slugify\Slugify;


/**
* Handles accessing & displaying a single group via /group endpoint.
*/
Expand Down Expand Up @@ -108,6 +112,7 @@ public function index($GroupID = '') {
}
}

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

// Find all discussions with content from after DateMarkedRead.
Expand Down Expand Up @@ -405,9 +410,27 @@ public function invite($GroupID) {
if(GroupModel::isMemberOfGroup($user->UserID, $GroupID)) {
$this->Form->addError('User is a member of "'.$Group->Name.'".');
} else {
$this->GroupModel->invite($GroupID, $user->UserID);
$this->informMessage('Invitation was sent.');
$this->render('invitation_sent');
$groupInvitation['GroupID'] = $GroupID;
$groupInvitation['InviteeUserID'] = $user->UserID;
$groupInvitationModel = new GroupInvitationModel();
$result = $groupInvitationModel->save($groupInvitation);
if($result) {
$this->informMessage('Invitation was sent.');
} else {
$validationErrors = $groupInvitationModel->validationResults();
$validation = new Validation();
foreach ($validationErrors as $field => $errors) {
foreach ($errors as $error) {
$validation->addError(
$field,
$error
);
}
}
$this->Form->addError($validation->getMessage());
$this->render();
}

}
} catch (\Exception $e) {
$this->Form->addError('Error' . $e->getMessage());
Expand Down Expand Up @@ -493,7 +516,13 @@ public function watch($GroupID) {
$this->setData('Group', $Group);
if ($this->Form->authenticatedPostBack(true)) {
$this->GroupModel->watchGroup($Group, Gdn::session()->UserID);
$this->setRedirectTo('group/' . $GroupID);
// Stay in the previous page
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
$this->setRedirectTo($previous);
} else {
$this->setRedirectTo('group/'.$GroupID);
}
}
$this->render();
}
Expand All @@ -512,7 +541,13 @@ public function unwatch($GroupID) {
$this->setData('Group', $Group);
if ($this->Form->authenticatedPostBack(true)) {
$this->GroupModel->unwatchGroup($Group, Gdn::session()->UserID);
$this->setRedirectTo('group/'.$GroupID);
// Stay in the previous page
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
$this->setRedirectTo($previous);
} else {
$this->setRedirectTo('group/'.$GroupID);
}
}
$this->render();
}
Expand All @@ -523,11 +558,41 @@ public function unwatch($GroupID) {
* @param $UserID
* @throws Gdn_UserException
*/
public function accept($GroupID, $UserID) {
if(!GroupModel::isMemberOfGroup($UserID, $GroupID) ) {
$this->GroupModel->accept($GroupID, $UserID);
public function accept($token ='') {

if (!Gdn::session()->isValid()) {
redirectTo(signInUrl());
}

$groupInvitationModel = new GroupInvitationModel();

$result = $groupInvitationModel->validateToken($token);
$validationErrors = $groupInvitationModel->Validation->results();
if (count($validationErrors) > 0) {
$validation = new Validation();
foreach ($validationErrors as $field => $errors) {
foreach ($errors as $error) {
$validation->addError(
$field,
$error
);
}
}
if ($validation->getErrorCount() > 0) {
$this->setData('ErrorMessage', $validation->getMessage());
$this->render();
}
} else {
if(!GroupModel::isMemberOfGroup($result['InviteeUserID'], $result['GroupID']) ) {
$GroupModel = new GroupModel();
$GroupModel->join($result['GroupID'],$result['InviteeUserID']);
}
$result['Status'] = 'accepted';
$result['DateAccepted'] = Gdn_Format::toDateTime();
$groupInvitationModel->save($result);
redirectTo(GroupsPlugin::GROUP_ROUTE.$result['GroupID']);
}
redirectTo(GroupsPlugin::GROUP_ROUTE.$GroupID);

}


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


/**
* Create a new announcement
* @param string $GroupID
* @throws Gdn_UserException
*/
public function announcement($GroupID=''){

$Group = $this->findGroup($GroupID);

if(!$this->GroupModel->canAddAnnouncement($Group)) {
throw permissionException();
}

$this->setData('Breadcrumbs',
[['Name' => t('Challenge Discussions'), 'Url' => GroupsPlugin::GROUPS_ROUTE],
['Name' => $Group->Name, 'Url' => GroupsPlugin::GROUP_ROUTE.$Group->GroupID], ['Name' => t('New Announcement')]]);
$this->title('New Announcement');
$this->setDiscussionData($Group, 2);
$this->View = 'discussion';
$this->render();
}

/**
* Create a new discussion
* @param string $GroupID
* @throws Gdn_UserException
*/
public function discussion($GroupID=''){
$Group = $this->findGroup($GroupID);

if(!$this->GroupModel->canAddDiscussion($Group)) {
throw permissionException();
}

$this->setData('Breadcrumbs', [['Name' => t('Challenge Discussions'), 'Url' => GroupsPlugin::GROUPS_ROUTE],
['Name' => $Group->Name, 'Url' => GroupsPlugin::GROUP_ROUTE.$Group->GroupID], ['Name' => t('New Discussion')]]);
$this->title('New Discussion');
$this->setDiscussionData($Group, 1);
$this->View = 'discussion';
$this->render();

}

/**
* Create a discussion.
* @param int $categoryID Unique ID of the category to add the discussion to.
*/
public function setDiscussionData($Group,$Announce = '0') {
$categoryUrlCode =$Group->ChallengeID;//.'-questions';
$useCategories = true;

// Setup head
$this->addJsFile('jquery.autosize.min.js');
$this->addJsFile('autosave.js');
$this->addJsFile('post.js');

$session = Gdn::session();

Gdn_Theme::section('PostDiscussion');

// Set discussion, draft, and category data
$discussionID = isset($this->Discussion) ? $this->Discussion->DiscussionID : '';
$draftID = isset($this->Draft) ? $this->Draft->DraftID : 0;
$category = false;
$categoryModel = new CategoryModel();
$category = CategoryModel::categories($categoryUrlCode);
if ($category) {
$this->CategoryID = val('CategoryID', $category);
}

if ($category) {
$this->Category = (object)$category;
$this->setData('Category', $category);
$this->Form->addHidden('CategoryID', $this->Category->CategoryID);
}

$categoryData = $this->ShowCategorySelector ? CategoryModel::categories() : false;
if (!$useCategories || $this->ShowCategorySelector) {
// See if we should fill the CategoryID value.
$allowedCategories = CategoryModel::getByPermission(
'Discussions.Add',
$this->Form->getValue('CategoryID', $this->CategoryID),
['Archived' => 0, 'AllowDiscussions' => 1],
['AllowedDiscussionTypes' => $this->Data['Type']]
);
$allowedCategoriesCount = count($allowedCategories);

if ($this->ShowCategorySelector && $allowedCategoriesCount === 1) {
$this->ShowCategorySelector = false;
}

if (!$this->ShowCategorySelector && $allowedCategoriesCount) {
$allowedCategory = array_pop($allowedCategories);
$this->Form->addHidden('CategoryID', $allowedCategory['CategoryID']);

if ($this->Form->isPostBack() && !$this->Form->getFormValue('CategoryID')) {
$this->Form->setFormValue('CategoryID', $allowedCategory['CategoryID']);
}
}
}

// Set the model on the form
$DiscussionModel = new DiscussionModel();
$this->Form->setModel($DiscussionModel);
$this->Form->addHidden('GroupID', $Group->GroupID);
$this->Form->Action = '/post/discussion';
$this->Form->setFormValue('Announce', $Announce);
$this->setData('Group', $Group);
$this->setData('Announce', $Announce);
$this->setData('_AnnounceOptions', $this->announceOptions());

$this->fireEvent('BeforeDiscussionRender');

if ($this->CategoryID) {
$breadcrumbs = CategoryModel::getAncestors($this->CategoryID);
} else {
$breadcrumbs = [];
}

$breadcrumbs[] = [
'Name' => $this->data('Title'),
'Url' => val('AddUrl', val($this->data('Type'), DiscussionModel::discussionTypes()), '/post/discussion')
];

$this->setData('Breadcrumbs', $breadcrumbs);

}

/**
* Join a group
* @param $GroupID
Expand Down
Loading