@@ -363,11 +363,22 @@ public function categoriesController_afterDiscussionFilters_handler($sender){
363
363
$ this ->addGroupLinkToMenu ();
364
364
}
365
365
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
+ }
371
382
}
372
383
373
384
/**
@@ -403,6 +414,154 @@ public function postController_afterDiscussionSave_handler($sender, $args) {
403
414
}
404
415
}
405
416
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
+ }
406
565
/**
407
566
* Add Topcoder Roles
408
567
* @param $sender
0 commit comments