Created
January 28, 2014 15:06
-
-
Save Cosmologist/8669235 to your computer and use it in GitHub Desktop.
Get subscription information for user from forum (CCDNForumForumBundle) subscription - http://pickbox.ru/e/256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
ccdn_forum_forum.twig_extension.subscriptions_stat.class: Acme\ForumBundle\Twig\SubscriptionsStatExtension | |
services: | |
ccdn_forum_forum.twig_extension.unread: | |
class: %ccdn_forum_forum.twig_extension.subscriptions_stat.class% | |
arguments: | |
- @security.context | |
- @service_container | |
tags: | |
- { name: twig.extension } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\ForumBundle\Twig; | |
use Symfony\Component\DependencyInjection\ContainerInterface as Container; | |
use Symfony\Component\Security\Core\SecurityContext as SecurityContext; | |
class SubscriptionsStatExtension extends \Twig_Extension | |
{ | |
public function __construct(SecurityContext $securityContext, Container $container) | |
{ | |
$this->securityContext = $securityContext; | |
$this->container = $container; | |
} | |
public function getFunctions() | |
{ | |
return array( | |
'forum_subscriptions_stats' => new \Twig_Function_Method($this, 'subscriptionsStat'), | |
); | |
} | |
public function subscriptionsStat() | |
{ | |
$totalForumsSubscribed = array('count_read' => 0, 'count_unread' => 0, 'count_total' => 0); | |
$user = $this->securityContext->getToken()->getUser(); | |
if ($user) { | |
$subscriptionModel = $this->container->get('ccdn_forum_forum.model.subscription'); | |
$subscriptionForums = $subscriptionModel->findAllSubscriptionsForUserById($user->getId(), true); | |
foreach ($subscriptionForums as $subscription) { | |
$forumSubscribed = $subscription->getForum(); | |
if ($forumSubscribed) { | |
$totalForumsSubscribed['count_total']++; | |
if ($subscription->isRead()) { | |
$totalForumsSubscribed['count_read']++; | |
} else { | |
$totalForumsSubscribed['count_unread']++; | |
} | |
} | |
} | |
} | |
return $totalForumsSubscribed; | |
} | |
public function getName() | |
{ | |
return 'subscriptionsStat'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment