From 8b5a43c0941756875c8353a84944f5f29584817b Mon Sep 17 00:00:00 2001 From: Bogdanova Olga Date: Sun, 29 Nov 2020 21:30:08 +0300 Subject: [PATCH 1/2] Issues-218 --- controllers/class.groupcontroller.php | 60 ++++++++++----- controllers/class.groupscontroller.php | 2 + models/class.groupmodel.php | 102 ++++++++++++++++++++++++- views/group/edit.php | 2 +- views/group/index.php | 18 ++++- views/groups/index.php | 2 +- 6 files changed, 159 insertions(+), 27 deletions(-) diff --git a/controllers/class.groupcontroller.php b/controllers/class.groupcontroller.php index 38bc4bb..a49c76b 100644 --- a/controllers/class.groupcontroller.php +++ b/controllers/class.groupcontroller.php @@ -165,20 +165,25 @@ public function edit($groupID = false) { $this->setData('Breadcrumbs', $this->buildBreadcrumb($Group)); } else { - $Group->Type = Gdn::request()->get('type'); + $Group->Type = GroupModel::TYPE_REGULAR; $Group->OwnerID = Gdn::session()->UserID; $Group->LeaderID = Gdn::session()->UserID; } - $typesData = [GroupModel::TYPE_REGULAR => GroupModel::TYPE_REGULAR, GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE]; - $this->setData('Types', $typesData); - + // Set Privacy Types $type = GroupsPlugin::UI[$Group->Type]['TypeName']; $privacyTypes = [GroupModel::PRIVACY_PUBLIC => sprintf('Public. Anyone can see the %s and its content. Anyone can join.', $type), GroupModel::PRIVACY_PRIVATE => sprintf('Private. Anyone can see the %s, but only members can see its content. People must apply or be invited to join.', $type), GroupModel::PRIVACY_SECRET => sprintf('Secret. Only members can see the %s and view its content. People must be invited to join.', $type)]; $this->setData('PrivacyTypes', $privacyTypes); + // Set Type dropbox + if($Group->Type == GroupModel::TYPE_REGULAR || $groupID === false) { // Regular Groups can be created from UI only + $typesData = [GroupModel::TYPE_REGULAR => GroupModel::TYPE_REGULAR, GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE]; + } else if ($Group->Type == GroupModel::TYPE_CHALLENGE){ + $typesData = [GroupModel::TYPE_CHALLENGE => GroupModel::TYPE_CHALLENGE]; + } + $this->setData('Types', $typesData); // Set the model on the form. $this->Form->setModel($this->GroupModel); @@ -187,23 +192,32 @@ public function edit($groupID = false) { $this->Form->addHidden('GroupID', $groupID); $this->Form->addHidden('OwnerID', $Group->OwnerID); - // If seeing the form for the first time... - if ($this->Form->authenticatedPostBack() === false) { - // Get the group data for the requested $GroupID and put it into the form. - $this->Form->setData($Group); - } else { + if ($this->Form->authenticatedPostBack(true)) { //The form was submitted + if (!Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_ADD_PERMISSION)) { + $this->Form->addError('You do not have permission to add a group'); + } - // If the form has been posted back... - $this->Form->formValues(); - $this->Form->saveImage('Icon'); - $this->Form->saveImage('Banner'); - if ($groupID = $this->Form->save()) { - if ($this->deliveryType() === DELIVERY_TYPE_DATA) { - $this->index($groupID); - return; + if ($this->Form->errorCount() == 0) { + // If the form has been posted back... + $isIconUploaded = $this->Form->saveImage('Icon'); + $isBannerUploaded = $this->Form->saveImage('Banner'); + $data = $this->Form->formValues(); + if ($groupID = $this->GroupModel->save($data)) { + $this->Form->setValidationResults($this->GroupModel->validationResults()); + if($groupID) { + $this->setRedirectTo('group/' . $groupID); + } + $this->View = 'Edit'; + } else { + $this->Form->setValidationResults($this->GroupModel->validationResults()); } - $this->setRedirectTo('group/'.$groupID ); + + } else { + $this->errorMessage($this->Form->errors()); } + } else { + // Get the group data for the requested $GroupID and put it into the form. + $this->Form->setData($Group); } $this->render(); @@ -775,14 +789,20 @@ public function category($GroupID) { $this->setData('Group', $Group); $this->Form->setModel($this->CategoryModel); $slugify = new Slugify(); - $parentCategory = $Group->ChallengeID ? $this->CategoryModel->getByCode($Group->ChallengeID): -1; + + $parentCategory = $this->GroupModel->getRootGroupCategory($Group); $this->Form->addHidden('ParentCategoryID', $parentCategory->CategoryID); $this->Form->addHidden('DisplayAs', 'Discussions'); $this->Form->addHidden('AllowFileUploads',1); $this->Form->addHidden('UrlCode',''); $this->Form->addHidden('GroupID',$GroupID); if ($this->Form->authenticatedPostBack(true)) { - $this->Form->setFormValue('UrlCode', $Group->ChallengeID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-')); + if($Group->Type === GroupModel::TYPE_CHALLENGE) { + $this->Form->setFormValue('UrlCode', $Group->ChallengeID . '-' . $slugify->slugify($this->Form->getValue('Name'), '-')); + } + //else { + // $this->Form->setFormValue('UrlCode', 'group-'.$Group->GroupID.'-'.$slugify->slugify($this->Form->getValue('Name'), '-')); + // } $newCategoryID = $this->Form->save(); if(!$newCategoryID) { $this->errorMessage($this->Form->errors()); diff --git a/controllers/class.groupscontroller.php b/controllers/class.groupscontroller.php index 66b39b9..ebed24f 100644 --- a/controllers/class.groupscontroller.php +++ b/controllers/class.groupscontroller.php @@ -49,6 +49,7 @@ public function setFilterPageData($filter) { $this->View = 'index'; $this->title('Challenges'); $this->setData('Title', 'My Challenges'); + $this->setData('ShowAddButton', false); $this->setData('AddButtonTitle', 'Challenge'); $this->setData('AddButtonLink', '/group/add?type=challenge'); $this->setData('AvailableGroupTitle', 'Available Challenges'); @@ -63,6 +64,7 @@ public function setFilterPageData($filter) { $this->View = 'index'; $this->title('Groups'); $this->setData('Title', 'My Groups'); + $this->setData('ShowAddButton', true); $this->setData('AddButtonTitle', 'Group'); $this->setData('AddButtonLink', '/group/add?type=regular'); $this->setData('MyGroupButtonTitle', 'All My Groups'); diff --git a/models/class.groupmodel.php b/models/class.groupmodel.php index c3c2703..381ea95 100644 --- a/models/class.groupmodel.php +++ b/models/class.groupmodel.php @@ -991,19 +991,22 @@ public function save($formPostValues, $settings = false) { $groupID = val('GroupID', $formPostValues); $ownerID = val('OwnerID', $formPostValues); + $type = val('Type', $formPostValues); + $name = val('Name', $formPostValues); + $archived = val('Archived', $formPostValues); $insert = $groupID > 0 ? false : true; if ($insert) { // Figure out the next group ID. $maxGroupID = $this->SQL->select('g.GroupID', 'MAX')->from('Group g')->get()->value('GroupID', 0); $groupID = $maxGroupID + 1; - $this->addInsertFields($formPostValues); $formPostValues['GroupID'] = strval($groupID); // string for validation } else { $this->addUpdateFields($formPostValues); } + // TODO: Move creating Challenge group categories from challenge processor // Validate the form posted values if ($this->validate($formPostValues, $insert)) { $fields = $this->Validation->schemaValidationFields(); @@ -1013,6 +1016,23 @@ public function save($formPostValues, $settings = false) { $this->update($fields, ['GroupID' => $groupID]); } else { $this->insert($fields); + //Create a category for a regular group + if($type == GroupModel::TYPE_REGULAR) { + $categoryModel = new CategoryModel(); + $parentCategory = $categoryModel->getByCode('groups'); + $categoryData = [ 'Name' => $name, + 'ParentCategoryID' => $parentCategory->CategoryID, + 'DisplayAs' => 'Discussions', + 'AllowFileUploads' => 1, + 'UrlCode' => 'group-'.$groupID, + 'GroupID' => $groupID, + 'Archived' => $archived]; + $categoryID = $categoryModel->save($categoryData); + // TODO + if(!$categoryID) { + // Error + } + } $this->SQL->insert( 'UserGroup', [ @@ -1053,8 +1073,28 @@ public function deleteID($groupID, $options = []) { * @inheritdoc */ public function validate($values, $insert = false) { + $result = true; + // TODO: Move Challenge group validation from challenge processor + $type = val('Type', $values); + $urlCode = val('UrlCode', $values); + if($insert === true) { + $categoryModel = new CategoryModel(); + if($type === GroupModel::TYPE_REGULAR) { + $category = $categoryModel->getByCode($urlCode); + if($category) { + $result = false; + $this->Validation->addValidationResult('UrlCode', 'Group UrlCode has existed.'); + } + $parentCategory = $categoryModel->getByCode('groups'); + if (!$parentCategory) { + $result = false; + $this->Validation->addValidationResult('ParentCategoryID', 'Groups category was not found.'); + } - return parent::validate($values, $insert); + } + } + $result = $result && parent::validate($values, $insert); + return $result; } /** @@ -1304,6 +1344,14 @@ public function canAddGroup() { * */ public function canManageCategories($group) { + if((int)$group->Archived === 1) { + return false; + } + + if($group->Type === GroupModel::TYPE_REGULAR) { + return false; + } + return $this->isProjectCopilot() || $this->isProjectManager() || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_CATEGORY_MANAGE_PERMISSION); } @@ -1329,6 +1377,9 @@ private function checkTopcoderProjectRole($topcoderProjectRole){ * */ public function canEdit($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole == GroupModel::ROLE_LEADER || @@ -1347,6 +1398,9 @@ public function canEdit($group) { * */ public function canDelete($group){ + if((int)$group->Archived === 1) { + return false; + } return Gdn::session()->UserID == $group->OwnerID || Gdn::session()->checkPermission(GroupsPlugin::GROUPS_GROUP_DELETE_PERMISSION); } @@ -1355,6 +1409,9 @@ public function canDelete($group){ * */ public function canJoin($group) { + if((int)$group->Archived === 1) { + return false; + } return $group->Privacy == GroupModel::PRIVACY_PUBLIC; } @@ -1363,6 +1420,9 @@ public function canJoin($group) { * */ public function canRemoveMember($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole == GroupModel::ROLE_LEADER || @@ -1377,6 +1437,9 @@ public function canRemoveMember($group) { * */ public function canChangeGroupRole($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole == GroupModel::ROLE_LEADER || @@ -1391,6 +1454,9 @@ public function canChangeGroupRole($group) { * */ public function canLeave($group) { + if((int)$group->Archived === 1) { + return false; + } if(isset($group->ChallengeID)) { return false; } @@ -1409,6 +1475,9 @@ public function canLeave($group) { * */ public function canManageMembers($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole == GroupModel::ROLE_LEADER || @@ -1423,6 +1492,9 @@ public function canManageMembers($group) { * */ public function canInviteNewMember($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole === GroupModel::ROLE_LEADER || @@ -1438,6 +1510,9 @@ public function canInviteNewMember($group) { * */ public function canAddDiscussion($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole || Gdn::session()->UserID == $group->OwnerID) { @@ -1451,6 +1526,9 @@ public function canAddDiscussion($group) { * */ public function canAddAnnouncement($group) { + if((int)$group->Archived === 1) { + return false; + } $result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID); $groupRole = val('Role', $result, null); if($groupRole === GroupModel::ROLE_LEADER || Gdn::session()->UserID == $group->OwnerID @@ -1764,4 +1842,24 @@ public function archiveGroup($group){ $group->Archived = 1; $this->save($group); } + + public function getGroupDiscussionCategories($group){ + if(is_numeric($group) && $group > 0) { + $group = $this->getByGroupID($group); + } + $categoryModel = new CategoryModel(); + return $categoryModel->getWhere(['GroupID' => $group->GroupID, 'DisplayAs' => 'Discussions'])->resultArray(); + } + + public function getRootGroupCategory($group){ + if(is_numeric($group) && $group > 0) { + $group = $this->getByGroupID($group); + } + $categoryModel = new CategoryModel(); + if($group->ChallengeID) { + return $categoryModel->getByCode($group->ChallengeID); + } + + return -1; //return Vanilla root + } } \ No newline at end of file diff --git a/views/group/edit.php b/views/group/edit.php index dfbf178..932b60e 100644 --- a/views/group/edit.php +++ b/views/group/edit.php @@ -27,7 +27,7 @@ echo '
'; echo $this->Form->label('Type', 'Type'); - echo $this->Form->dropDown('Type', $this->data('Types'), ['IncludeNull' => t('All Types')]); + echo $this->Form->dropDown('Type', $this->data('Types')); echo '
'; echo '
'; diff --git a/views/group/index.php b/views/group/index.php index d8efcc9..eac6da6 100644 --- a/views/group/index.php +++ b/views/group/index.php @@ -18,7 +18,7 @@ $groupModel = new GroupModel(); $currentTopcoderProjectRoles = Gdn::controller()->data('ChallengeCurrentUserProjectRoles'); $groupModel->setCurrentUserTopcoderProjectRoles($currentTopcoderProjectRoles); - +$discussionCategories = $groupModel->getGroupDiscussionCategories($Group); ?> @@ -30,7 +30,13 @@ canAddAnnouncement($Group)) { - echo anchor('New Announcement', '/group/announcement/' . $Group->GroupID, 'Button Primary', ''); + if(count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) { + // The group category is selected automatically + $firstCategory = $discussionCategories[0]; + echo anchor('New Announcement', '/post/discussion/'.$firstCategory['UrlCode'], 'Button Primary', ''); + } else { + echo anchor('New Announcement', '/post/discussion/', 'Button Primary', ''); + } } ?>
@@ -53,7 +59,13 @@
canAddDiscussion($Group)) { - echo anchor('New Discussion', '/group/discussion/' . $Group->GroupID, 'Button Primary', ''); + // The group category is selected automatically + if (count($discussionCategories) > 0 && $Group->Type == GroupModel::TYPE_REGULAR) { + $firstCategory = $discussionCategories[0]; + echo anchor('New Discussion', '/post/discussion/' . $firstCategory['UrlCode'], 'Button Primary', ''); + } else { + echo anchor('New Discussion', '/post/discussion/', 'Button Primary', ''); + } } ?>
diff --git a/views/groups/index.php b/views/groups/index.php index 362cf43..558a004 100644 --- a/views/groups/index.php +++ b/views/groups/index.php @@ -8,7 +8,7 @@ include_once $this->fetchViewLocation('helper_functions'); -if($canAddGroup === true) { +if($canAddGroup === true && $this->data(ShowAddButton) === true) { echo '
'. $this->data('AddButtonTitle').'
'; } From 70da02067df62e1863e575c030eb16a8a4397cd1 Mon Sep 17 00:00:00 2001 From: Bogdanova Olga Date: Sun, 29 Nov 2020 21:31:44 +0300 Subject: [PATCH 2/2] Issues-218 --- views/groups/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/groups/index.php b/views/groups/index.php index 558a004..31ed225 100644 --- a/views/groups/index.php +++ b/views/groups/index.php @@ -8,7 +8,7 @@ include_once $this->fetchViewLocation('helper_functions'); -if($canAddGroup === true && $this->data(ShowAddButton) === true) { +if($canAddGroup === true && $this->data('ShowAddButton') === true) { echo ''; }