Skip to content

Commit d064429

Browse files
committed
Issues-149:implemented
1 parent 37055ab commit d064429

File tree

5 files changed

+206
-66
lines changed

5 files changed

+206
-66
lines changed

class.groups.plugin.php

Lines changed: 164 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,22 @@ public function categoriesController_afterDiscussionFilters_handler($sender){
363363
$this->addGroupLinkToMenu();
364364
}
365365

366-
// TODO: Add 'Watch/Unwatch' option in a dropdown
367-
public function categoriesController_categoryOptionsDropdown_handler($sender, $args) {
368-
// $dropdown = &$args['CategoryOptionsDropdown'];
369-
// $category = &$args['Category'];
370-
// self::log('categoriesController_categoryOptionsDropdown_handler', ['category' => $category]);
366+
public function base_categoryOptionsDropdown_handler($sender, $args) {
367+
if (!Gdn::session()->isValid()) {
368+
return ;
369+
}
370+
$dropdown = &$args['CategoryOptionsDropdown'];
371+
$category = &$args['Category'];
372+
if(val('DisplayAs', $category) == 'Discussions') {
373+
$categoryModel = new CategoryModel();
374+
$categoryID = val('CategoryID', $category);
375+
$hasWatched = $categoryModel->hasWatched($categoryID, Gdn::session()->UserID);;
376+
$dropdown->addLink(
377+
t($hasWatched ? 'Unwatch' : 'Watch'),
378+
$hasWatched ? '/category/watched?categoryid='.$categoryID.'&tkey='.Gdn::session()->transientKey() : '/category/watch?categoryid='.$categoryID.'&tkey=' . Gdn::session()->transientKey(),
379+
'watch'
380+
);
381+
}
371382
}
372383

373384
/**
@@ -403,6 +414,154 @@ public function postController_afterDiscussionSave_handler($sender, $args) {
403414
}
404415
}
405416

417+
/**
418+
* Allows user to unwatch a category.
419+
* Add the Vanilla method to stay in the same page
420+
*
421+
* @param null $categoryID
422+
* @param null $tKey
423+
* @throws Gdn_UserException
424+
*/
425+
public function categoryController_watched_create($sender,$categoryID = null, $tKey = null) {
426+
$this->watchCategory($sender, $categoryID, null, $tKey);
427+
}
428+
429+
/**
430+
* Allows user to watch a category.
431+
* Add the Vanilla method to stay in the same page
432+
*
433+
* @param null $categoryID
434+
* @param null $tKey
435+
* @throws Gdn_UserException
436+
*/
437+
public function categoryController_watch_create($sender,$categoryID = null, $tKey = null) {
438+
$this->watchCategory($sender, $categoryID, 1, $tKey);
439+
}
440+
441+
private function watchCategory($sender, $categoryID = null, $watched = null, $tKey = null) {
442+
// Make sure we are posting back.
443+
if (!$sender->Request->isAuthenticatedPostBack() && !Gdn::session()->validateTransientKey($tKey)) {
444+
throw permissionException('Javascript');
445+
}
446+
447+
if (!Gdn::session()->isValid()) {
448+
throw permissionException('SignedIn');
449+
}
450+
451+
$userID = Gdn::session()->UserID;
452+
453+
$categoryModel = new CategoryModel();
454+
$category = CategoryModel::categories($categoryID);
455+
if (!$category) {
456+
throw notFoundException('Category');
457+
}
458+
459+
$hasPermission = $categoryModel::checkPermission($categoryID, 'Vanilla.Discussions.View');
460+
if (!$hasPermission) {
461+
throw permissionException('Vanilla.Discussion.View');
462+
}
463+
464+
$result = $categoryModel->watch($categoryID, $watched);
465+
// Set the new value for api calls and json targets.
466+
$sender->setData([
467+
'UserID' => $userID,
468+
'CategoryID' => $categoryID,
469+
'Watched' => $result
470+
]);
471+
472+
switch ($sender->deliveryType()) {
473+
case DELIVERY_TYPE_DATA:
474+
$sender->render('Blank', 'Utility', 'Dashboard');
475+
return;
476+
case DELIVERY_TYPE_ALL:
477+
// Stay in the previous page
478+
if(isset($_SERVER['HTTP_REFERER'])) {
479+
$previous = $_SERVER['HTTP_REFERER'];
480+
redirectTo($previous);
481+
} else {
482+
redirectTo('/categories');
483+
}
484+
}
485+
486+
// Return the appropriate bookmark.
487+
/// require_once $sender->fetchViewLocation('helper_functions', 'Categories');
488+
$markup = watchButton($categoryID);
489+
$sender->jsonTarget("!element", $markup, 'ReplaceWith');
490+
$sender->render('Blank', 'Utility', 'Dashboard');
491+
}
492+
493+
/**
494+
* Watch a category
495+
* @param CategoryModel $sender
496+
*/
497+
public function categoryModel_watch_create(CategoryModel $sender){
498+
$categoryIDs = val(0, $sender->EventArguments);
499+
$watched = val(1, $sender->EventArguments);
500+
$sender->setCategoryMetaData($categoryIDs, Gdn::session()->UserID, $watched);
501+
}
502+
503+
/**
504+
* Set category meta data for user
505+
* @param $categoryIDs array of CategoryID
506+
* @param $userID
507+
* @param $watched 1 - to watch, null - unwatched
508+
*/
509+
public function categoryModel_setCategoryMetaData_create(CategoryModel $sender) {
510+
$categoryIDs = val(0, $sender->EventArguments);
511+
$userID = val(1, $sender->EventArguments);
512+
$watched = val(2, $sender->EventArguments);
513+
$userMetaModel = new UserMetaModel();
514+
if(is_numeric($categoryIDs) ) {
515+
$categoryIDs = [$categoryIDs];
516+
}
517+
foreach($categoryIDs as $categoryID) {
518+
$newEmailCommentKey = 'Preferences.Email.NewComment.'.$categoryID;
519+
$newEmailDiscussionKey = 'Preferences.Email.NewDiscussion.'.$categoryID;
520+
$newPopupCommentKey = 'Preferences.Popup.NewComment.'.$categoryID;
521+
$newPopupDiscussionKey = 'Preferences.Popup.NewDiscussion.'.$categoryID;
522+
$userMetaModel->setUserMeta($userID, $newEmailCommentKey , $watched);
523+
$userMetaModel->setUserMeta($userID, $newEmailDiscussionKey, $watched);
524+
$userMetaModel->setUserMeta($userID, $newPopupCommentKey , $watched);
525+
$userMetaModel->setUserMeta($userID, $newPopupDiscussionKey , $watched);
526+
}
527+
return $sender->hasWatched($categoryIDs,$userID);
528+
}
529+
530+
/**
531+
* Check if the current user has watched a category or at least one category from the list
532+
*
533+
* @param $userID
534+
* @param $categoryIDs array|int
535+
* @return bool
536+
*/
537+
public function categoryModel_hasWatched_create(CategoryModel $sender) {
538+
$categoryIDs = val(0, $sender->EventArguments);
539+
$userID = val(1, $sender->EventArguments);
540+
541+
if(is_numeric($categoryIDs) ) {
542+
$categoryIDs = [$categoryIDs];
543+
}
544+
545+
$userMetaModel = new UserMetaModel();
546+
foreach ($categoryIDs as $categoryID) {
547+
$newDiscussionKey = 'Preferences.%.NewDiscussion.' . $categoryID;
548+
$newCommentKey = 'Preferences.%.NewComment.' . $categoryID;
549+
$metaData= $userMetaModel->getUserMeta($userID, $newDiscussionKey);
550+
foreach ($metaData as $key => $value) {
551+
if($value != null) {
552+
return true;
553+
}
554+
}
555+
556+
$metaData= $userMetaModel->getUserMeta(Gdn::session()->UserID, $newCommentKey);
557+
foreach ($metaData as $key => $value) {
558+
if($value != null) {
559+
return true;
560+
}
561+
}
562+
}
563+
return false;
564+
}
406565
/**
407566
* Add Topcoder Roles
408567
* @param $sender

controllers/class.groupcontroller.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function follow($GroupID) {
394394
}
395395
$this->setData('Group', $Group);
396396
if ($this->Form->authenticatedPostBack(true)) {
397-
$this->GroupModel->follow($Group);
397+
$this->GroupModel->followGroup($Group);
398398
$this->setRedirectTo('group/' . $GroupID);
399399
}
400400
$this->render();
@@ -412,7 +412,7 @@ public function unfollow($GroupID) {
412412
}
413413
$this->setData('Group', $Group);
414414
if ($this->Form->authenticatedPostBack(true)) {
415-
$this->GroupModel->unfollow($Group);
415+
$this->GroupModel->unfollowGroup($Group);
416416
$this->setRedirectTo('group/'.$GroupID);
417417
}
418418
$this->render();
@@ -430,7 +430,7 @@ public function watch($GroupID) {
430430
}
431431
$this->setData('Group', $Group);
432432
if ($this->Form->authenticatedPostBack(true)) {
433-
$this->GroupModel->watch($Group);
433+
$this->GroupModel->watchGroup($Group);
434434
$this->setRedirectTo('group/' . $GroupID);
435435
}
436436
$this->render();
@@ -449,7 +449,7 @@ public function unwatch($GroupID) {
449449
}
450450
$this->setData('Group', $Group);
451451
if ($this->Form->authenticatedPostBack(true)) {
452-
$this->GroupModel->unwatch($Group);
452+
$this->GroupModel->unwatchGroup($Group);
453453
$this->setRedirectTo('group/'.$GroupID);
454454
}
455455
$this->render();

design/groups.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,24 @@ span.file-upload-choose, span.file-upload-browse {
779779

780780
.Section-GroupList .Frame-body .Frame-content .Frame-details .Frame-row-main aside.Panel.Panel-main {
781781
display: block;
782+
}
783+
784+
.watchButton {
785+
white-space: nowrap;
786+
font-size: 10px;
787+
text-transform: uppercase;
788+
vertical-align: middle;
789+
font-weight: 400;
790+
padding: 0 5px;
791+
text-decoration: none;
792+
}
793+
.watchButton.isWatching:not(:hover) {
794+
opacity: 0.7;
795+
}
796+
797+
.watchButton-icon {
798+
width: 14px;
799+
height: 14px;
800+
margin-right: 5px;
801+
vertical-align: sub;
782802
}

models/class.groupmodel.php

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ public function canView($group) {
621621
* Can follow group categories
622622
*
623623
*/
624-
public function canFollow($group) {
624+
public function canFollowGroup($group) {
625625
if($group->ChallengeID) {
626626
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
627627
return val('Role', $result, false);
@@ -634,7 +634,7 @@ public function canFollow($group) {
634634
* Check if the current user has followed at least one group category
635635
*
636636
*/
637-
public function hasFollowed($group) {
637+
public function hasFollowedGroup($group) {
638638
if($group->ChallengeID) {
639639
$categoryModel = new CategoryModel();
640640
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -663,7 +663,7 @@ public function hasFollowed($group) {
663663
* Can watch group categories
664664
*
665665
*/
666-
public function canWatch($group) {
666+
public function canWatchGroup($group) {
667667
if($group->ChallengeID) {
668668
$result = $this->getGroupRoleFor(Gdn::session()->UserID, $group->GroupID);
669669
return val('Role', $result, false);
@@ -677,7 +677,7 @@ public function canWatch($group) {
677677
* @param $group
678678
* @return bool User has watched at least one group category
679679
*/
680-
public function hasWatched($group) {
680+
public function hasWatchedGroup($group) {
681681
if($group->ChallengeID) {
682682
$categoryModel = new CategoryModel();
683683
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -689,37 +689,17 @@ public function hasWatched($group) {
689689
$categoryIDs = [$groupCategory->CategoryID];
690690
}
691691

692-
$userMetaModel = new UserMetaModel();
693-
foreach ($categoryIDs as $categoryID) {
694-
$newDiscussionKey = 'Preferences.%.NewDiscussion.' . $categoryID;
695-
$newCommentKey = 'Preferences.%.NewComment.' . $categoryID;
696-
GroupsPlugin::log('hasWatched', ['Group' => $group->GroupID, '$newDiscussionKey' => $userMetaModel->getUserMeta(Gdn::session()->UserID, $newDiscussionKey)]);
697-
GroupsPlugin::log('hasWatched', ['Group' => $group->GroupID, '$newCommentKey' => $userMetaModel->getUserMeta(Gdn::session()->UserID, $newCommentKey)]);
698-
$metaData= $userMetaModel->getUserMeta(Gdn::session()->UserID, $newDiscussionKey);
699-
foreach ($metaData as $key => $value) {
700-
if($value != null) {
701-
return true;
702-
}
703-
}
704-
705-
$metaData= $userMetaModel->getUserMeta(Gdn::session()->UserID, $newCommentKey);
706-
foreach ($metaData as $key => $value) {
707-
if($value != null) {
708-
return true;
709-
}
710-
}
711-
}
692+
return $categoryModel->hasWatched($categoryIDs,Gdn::session()->UserID);
712693
}
713694

714695
return false;
715696
}
716697

717-
718698
/**
719699
* Follow all group's categories
720700
*
721701
*/
722-
public function follow($group) {
702+
public function followGroup($group) {
723703
if($group->ChallengeID) {
724704
$categoryModel = new CategoryModel();
725705
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -740,7 +720,7 @@ public function follow($group) {
740720
* Unfollow all group's categories
741721
*
742722
*/
743-
public function unfollow($group) {
723+
public function unfollowGroup($group) {
744724
if($group->ChallengeID) {
745725
$categoryModel = new CategoryModel();
746726
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -761,7 +741,7 @@ public function unfollow($group) {
761741
* Watch all group's categories
762742
* @param $group
763743
*/
764-
public function watch($group) {
744+
public function watchGroup($group) {
765745
if($group->ChallengeID) {
766746
$categoryModel = new CategoryModel();
767747
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -771,15 +751,15 @@ public function watch($group) {
771751
} else {
772752
$categoryIDs = [$groupCategory->CategoryID];
773753
}
774-
$this->setCategoryMetaData($categoryIDs, Gdn::session()->UserID, 1);
754+
$categoryModel->setCategoryMetaData($categoryIDs, Gdn::session()->UserID, 1);
775755
}
776756
}
777757

778758
/**
779759
* Unwatch all group's categories
780760
* @param $group
781761
*/
782-
public function unwatch($group) {
762+
public function unwatchGroup($group) {
783763
if($group->ChallengeID) {
784764
$categoryModel = new CategoryModel();
785765
$groupCategory = $categoryModel->getByCode($group->ChallengeID);
@@ -789,29 +769,10 @@ public function unwatch($group) {
789769
} else {
790770
$categoryIDs = [$groupCategory->CategoryID];
791771
}
792-
$this->setCategoryMetaData($categoryIDs, Gdn::session()->UserID, null);
772+
$categoryModel->setCategoryMetaData($categoryIDs, Gdn::session()->UserID, null);
793773
}
794774
}
795775

796-
/**
797-
* Set category meta data for user
798-
* @param $categoryIDs array of CategoryID
799-
* @param $userID
800-
* @param $watched 1 - to watch, null - unwatched
801-
*/
802-
private function setCategoryMetaData($categoryIDs, $userID, $watched) {
803-
$userMetaModel = new UserMetaModel();
804-
foreach($categoryIDs as $categoryID) {
805-
$newEmailCommentKey = 'Preferences.Email.NewComment.'.$categoryID;
806-
$newEmailDiscussionKey = 'Preferences.Email.NewDiscussion.'.$categoryID;
807-
$newPopupCommentKey = 'Preferences.Popup.NewComment.'.$categoryID;
808-
$newPopupDiscussionKey = 'Preferences.Popup.NewDiscussion.'.$categoryID;
809-
$userMetaModel->setUserMeta($userID, $newEmailCommentKey , $watched);
810-
$userMetaModel->setUserMeta($userID, $newEmailDiscussionKey, $watched);
811-
$userMetaModel->setUserMeta($userID, $newPopupCommentKey , $watched);
812-
$userMetaModel->setUserMeta($userID, $newPopupDiscussionKey , $watched);
813-
}
814-
}
815776

816777
/**
817778
* Check add group permission

0 commit comments

Comments
 (0)