From 6349f998a9246f1a6d59ce657d0a7b2e9a369965 Mon Sep 17 00:00:00 2001 From: Panos Date: Wed, 26 Mar 2014 16:26:29 +0100 Subject: [PATCH] Integrated user-picker notification functionnalities --- Controller/SectionController.php | 2 + Entity/Contribution.php | 46 ++++++++++-- Listener/ContributionListener.php | 70 +++++++++++++++++++ Resources/config/services/listeners.yml | 3 +- Resources/translations/notification.en.yml | 1 + Resources/translations/notification.fr.yml | 3 +- .../Notification/notification_item.html.twig | 2 + .../notification_user_tagged.html.twig | 1 + 8 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 Listener/ContributionListener.php create mode 100644 Resources/views/Notification/notification_user_tagged.html.twig diff --git a/Controller/SectionController.php b/Controller/SectionController.php index 8918802..084e8e3 100755 --- a/Controller/SectionController.php +++ b/Controller/SectionController.php @@ -321,6 +321,8 @@ private function persistCreateSection (Request $request, Wiki $wiki, Section $se $flashBag->add('success', $translator->trans('icap_wiki_section_add_success', array(), 'icap_wiki')); } catch (\Exception $exception) { + var_dump($exception->getMessage()); + die(); $flashBag->add('error', $translator->trans('icap_wiki_section_add_error', array(), 'icap_wiki')); } diff --git a/Entity/Contribution.php b/Entity/Contribution.php index 118ed80..6f04953 100755 --- a/Entity/Contribution.php +++ b/Entity/Contribution.php @@ -5,10 +5,16 @@ use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Claroline\CoreBundle\Entity\User; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Icap\NotificationBundle\Entity\UserPickerContent; +use JMS\DiExtraBundle\Annotation as DI; + /** -* @ORM\Entity(repositoryClass="Icap\WikiBundle\Repository\ContributionRepository") -* @ORM\Table(name="icap__wiki_contribution") + * @ORM\Entity(repositoryClass="Icap\WikiBundle\Repository\ContributionRepository") + * @ORM\EntityListeners({"Icap\WikiBundle\Listener\ContributionListener"}) + * @ORM\Table(name="icap__wiki_contribution") + * @ORM\HasLifecycleCallbacks() */ class Contribution { @@ -49,6 +55,8 @@ class Contribution */ protected $section; + protected $userPicker = null; + /** * Get id * @@ -115,7 +123,7 @@ public function setCreationDate($creationDate) * Set contributor * * @param User $contributor - * @return Post + * @return Contribution */ public function setContributor(User $contributor = null) { @@ -138,7 +146,7 @@ public function getContributor() * Set section * * @param \Icap\WikiBundle\Entity\Section $section - * @return section + * @return contribution */ public function setSection(\Icap\WikiBundle\Entity\Section $section) { @@ -156,4 +164,34 @@ public function getSection() { return $this->section; } + + /** + * @param UserPickerContent $userPicker + * @return $this + */ + public function setUserPicker(UserPickerContent $userPicker) + { + $this->userPicker = $userPicker; + + return $this; + } + + /** + * @return \Icap\NotificationBundle\Entity\UserPickerContent + */ + public function getUserPicker() + { + return $this->userPicker; + } + + /** + * @ORM\PrePersist + */ + public function createUserPicker(LifecycleEventArgs $event){ + if ($this->getText() != null) { + $userPicker = new UserPickerContent($this->getText()); + $this->setUserPicker($userPicker); + $this->setText($userPicker->getFinalText()); + } + } } diff --git a/Listener/ContributionListener.php b/Listener/ContributionListener.php new file mode 100644 index 0000000..cc44316 --- /dev/null +++ b/Listener/ContributionListener.php @@ -0,0 +1,70 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Icap\WikiBundle\Listener; + +use Doctrine\ORM\Event\LifecycleEventArgs; +use Icap\WikiBundle\Entity\Contribution; +use JMS\DiExtraBundle\Annotation as DI; +use Icap\NotificationBundle\Manager\NotificationManager as NotificationManager; + +/** + * @DI\Service("icap.wiki_bundle.entity_listener.contribution") + * @DI\Tag("doctrine.entity_listener") + */ +class ContributionListener +{ + /** @var \Icap\NotificationBundle\Manager\NotificationManager */ + private $notificationManager; + + /** + * @DI\InjectParams({ + * "notificationManager" = @DI\Inject("icap.notification.manager"), + * }) + */ + public function __construct(NotificationManager $notificationManager) + { + $this->notificationManager = $notificationManager; + } + + public function postPersist(Contribution $contribution, LifecycleEventArgs $event) + { + if ($contribution->getUserPicker() != null) { + $section = $contribution->getSection(); + $wiki = $section->getWiki(); + $details = array( + 'contribution' => array( + 'wiki' => $wiki->getId(), + 'section' => $section->getId(), + 'id' => $contribution->getId(), + 'title' => $contribution->getTitle(), + 'text' => $contribution->getText(), + 'contributor' => $contribution->getContributor()->getFirstName() . + ' ' . + $contribution->getContributor()->getLastName() + ), + 'resource' => array( + 'id' => $wiki->getId(), + 'name' => $wiki->getResourceNode()->getName(), + 'type' => $wiki->getResourceNode()->getResourceType()->getName() + ) + ); + $notification = $this->notificationManager->createNotification( + 'resource-icap_wiki-user_tagged', + 'wiki', + $wiki->getResourceNode()->getId(), + $details, + $contribution->getContributor() + ); + $this->notificationManager->notifyUsers($notification, $contribution->getUserPicker()->getUserIds()); + } + } +} \ No newline at end of file diff --git a/Resources/config/services/listeners.yml b/Resources/config/services/listeners.yml index 5a823a7..0b01462 100755 --- a/Resources/config/services/listeners.yml +++ b/Resources/config/services/listeners.yml @@ -38,4 +38,5 @@ services: - [setContainer, ["@service_container"]] tags: - { name: kernel.event_listener, event: create_notification_item_resource-icap_wiki-section_create, method: onCreateNotificationItem } - - { name: kernel.event_listener, event: create_notification_item_resource-icap_wiki-contribution_create, method: onCreateNotificationItem } \ No newline at end of file + - { name: kernel.event_listener, event: create_notification_item_resource-icap_wiki-contribution_create, method: onCreateNotificationItem } + - { name: kernel.event_listener, event: create_notification_item_resource-icap_wiki-user_tagged, method: onCreateNotificationItem } \ No newline at end of file diff --git a/Resources/translations/notification.en.yml b/Resources/translations/notification.en.yml index b7ac656..6314d5b 100644 --- a/Resources/translations/notification.en.yml +++ b/Resources/translations/notification.en.yml @@ -1,2 +1,3 @@ resource-icap_wiki-contribution_create: contributed to '%section%' section of '%wiki%' wiki resource-icap_wiki-section_create: created a '%section%' to '%wiki%' wiki +resource-icap_wiki-user_tagged: tagged you in '%section%' section of '%wiki%' wiki diff --git a/Resources/translations/notification.fr.yml b/Resources/translations/notification.fr.yml index 012af30..0baedc1 100644 --- a/Resources/translations/notification.fr.yml +++ b/Resources/translations/notification.fr.yml @@ -1,2 +1,3 @@ resource-icap_wiki-contribution_create: a contribué a la section '%section%' du '%wiki%' wiki -resource-icap_wiki-section_create: a creé la section '%section%' dans '%wiki%' wiki \ No newline at end of file +resource-icap_wiki-section_create: a creé la section '%section%' dans '%wiki%' wiki +resource-icap_wiki-user_tagged: vous a cité dans la section '%section%' du '%wiki%' wiki \ No newline at end of file diff --git a/Resources/views/Notification/notification_item.html.twig b/Resources/views/Notification/notification_item.html.twig index 5f1f2de..5adb14d 100644 --- a/Resources/views/Notification/notification_item.html.twig +++ b/Resources/views/Notification/notification_item.html.twig @@ -11,5 +11,7 @@ {% include 'IcapWikiBundle:Notification:notification_section_create.html.twig' %} {% elseif constant('Icap\\WikiBundle\\Event\\Log\\LogContributionCreateEvent::ACTION') == notification.actionKey %} {% include 'IcapWikiBundle:Notification:notification_contribution_create.html.twig' %} + {% elseif 'resource-icap_wiki-user_tagged' == notification.actionKey %} + {% include 'IcapWikiBundle:Notification:notification_user_tagged.html.twig' %} {% endif %} {% endblock %} \ No newline at end of file diff --git a/Resources/views/Notification/notification_user_tagged.html.twig b/Resources/views/Notification/notification_user_tagged.html.twig new file mode 100644 index 0000000..b0f81b2 --- /dev/null +++ b/Resources/views/Notification/notification_user_tagged.html.twig @@ -0,0 +1 @@ +{{ notification.actionKey|trans({'%wiki%' : notification.details.resource.name, '%section%' : notification.details.contribution.title}, 'notification') }} \ No newline at end of file