Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ajout du tracking dans le service ThreadService et de l'evenement sur…

… le addPost
  • Loading branch information...
commit 7aa8ea21f9365a5cc6cc23caadde13fdd0e8745c 1 parent 4a603c6
@Orkin authored
View
29 Module.php
@@ -18,6 +18,7 @@
namespace ZfrForum;
+use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ControllerPluginProviderInterface;
@@ -31,6 +32,27 @@ class Module implements
ServiceProviderInterface,
ViewHelperProviderInterface
{
+ /**
+ * {@inheritDoc}
+ */
+ public function onBootstrap(EventInterface $e)
+ {
+ /* @var $app \Zend\Mvc\ApplicationInterface */
+ $app = $e->getTarget();
+ $events = $app->getEventManager();
+
+ // Attach to helper set event and load the entity manager helper.
+ $events->attach('addPost.post', function(EventInterface $e) {
+ /* @var $threadService \ZfrForum\Service\ThreadService */
+ $threadService = $e->getTarget();
+
+ $params = $e->getParams();
+ $user = $params['user'];
+ $thread = $params['thread'];
+
+ $threadService->track($user, $thread);
+ });
+ }
/**
* @return array
@@ -146,9 +168,10 @@ public function getServiceConfig()
return new Service\SettingsService($settingsMapper);
},
'ZfrForum\Service\ThreadService' => function($serviceManager) {
- $threadMapper = $serviceManager->get('ZfrForum\Mapper\ThreadMapperInterface');
- $authentication = $serviceManager->get('Zend\Authentication\AuthenticationService');
- return new Service\ThreadService($threadMapper, $authentication);
+ $threadMapper = $serviceManager->get('ZfrForum\Mapper\ThreadMapperInterface');
+ $threadTrackingMapper = $serviceManager->get('ZfrForum\Mapper\ThreadTrackingMapperInterface');
+ $authentication = $serviceManager->get('Zend\Authentication\AuthenticationService');
+ return new Service\ThreadService($threadMapper, $threadTrackingMapper, $authentication);
},
'ZfrForum\Service\UserBanService' => function($serviceManager) {
$userBanMapper = $serviceManager->get('ZfrForum\Mapper\UserBanMapperInterface');
View
1  src/ZfrForum/Entity/CategoryTracking.php
@@ -24,6 +24,7 @@
use ZfrForum\Entity\Category;
/**
+ * @ORM\Entity(repositoryClass="ZfrForum\Repository\CategoryTrackingRepository")
* @ORM\Entity
* @ORM\Table(name="CategoriesTracking")
*/
View
3  src/ZfrForum/Entity/ThreadTracking.php
@@ -16,6 +16,8 @@
* and is licensed under the MIT license.
*/
+namespace ZfrForum\Entity;
+
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use ZfrForum\Entity\UserInterface;
@@ -23,6 +25,7 @@
use ZfrForum\Entity\Category;
/**
+ * @ORM\Entity(repositoryClass="ZfrForum\Repository\ThreadTrackingRepository")
* @ORM\Entity
* @ORM\Table(name="ThreadsTracking")
*/
View
9 src/ZfrForum/Mapper/CategoryTrackingMapperInterface.php
@@ -20,8 +20,6 @@
use Doctrine\Common\Persistence\ObjectRepository;
use ZfrForum\Entity\CategoryTracking;
-use ZfrForum\Entity\Category;
-use ZfcUser\Entity\UserInterface;
interface CategoryTrackingMapperInterface extends ObjectRepository
{
@@ -42,11 +40,4 @@ public function update(CategoryTracking $categoryTracking);
* @return void
*/
public function remove(CategoryTracking $categoryTracking);
-
- /**
- * @param Category $category
- * @param UserInterface $user
- * @return CategoryTracking
- */
- public function findByCategoryAndUser(Category $category, UserInterface $user);
}
View
23 src/ZfrForum/Repository/CategoryTrackingRepository.php
@@ -27,7 +27,6 @@
class CategoryTrackingRepository extends EntityRepository implements CategoryTrackingMapperInterface
{
-
/**
* @param CategoryTracking $categoryTracking
* @return CategoryTracking
@@ -55,26 +54,4 @@ public function remove(CategoryTracking $categoryTracking)
{
// TODO: Implement remove() method.
}
-
- /**
- * @param Category $category
- * @param UserInterface $user
- * @return CategoryTracking
- */
- public function findByCategoryAndUser(Category $category, UserInterface $user)
- {
- $queryBuilder = $this->getEntityManager()->createQueryBuilder();
- $queryBuilder->select('ct')
- ->from('ZfrForum\Entity\CategoryTracking', 'ct')
- ->where('ct.category = :category')
- ->andWhere('ct.user = :user')
- ->setParameter('category', $category)
- ->setParameter('user', $user);
-
- $categoryTracking = $queryBuilder->getQuery()->getOneOrNullResult();
-
- return $categoryTracking;
- }
-
-
}
View
53 src/ZfrForum/Service/ThreadService.php
@@ -19,14 +19,18 @@
namespace ZfrForum\Service;
use DateTime;
+use ZfcBase\EventManager\EventProvider;
use Zend\Authentication\AuthenticationService;
use Zend\Paginator\Paginator;
use ZfrForum\Entity\Category;
use ZfrForum\Entity\Post;
use ZfrForum\Entity\Thread;
+use ZfrForum\Entity\ThreadTracking;
use ZfrForum\Mapper\ThreadMapperInterface;
+use ZfrForum\Mapper\ThreadTrackingMapperInterface;
+use ZfrForum\Entity\UserInterface;
-class ThreadService
+class ThreadService extends EventProvider
{
/**
* @var ThreadMapperInterface
@@ -34,18 +38,26 @@ class ThreadService
protected $threadMapper;
/**
+ * @var ThreadTrackingMapperInterface
+ */
+ protected $threadTrackingMapper;
+
+ /**
* @var AuthenticationService
*/
protected $authenticationService;
/**
- * @param ThreadMapperInterface $threadMapper
- * @param AuthenticationService $authenticationService
+ * @param ThreadMapperInterface $threadMapper
+ * @param ThreadTrackingMapperInterface $threadTrackingMapper
+ * @param AuthenticationService $authenticationService
*/
- public function __construct(ThreadMapperInterface $threadMapper, AuthenticationService $authenticationService)
+ public function __construct(ThreadMapperInterface $threadMapper, ThreadTrackingMapperInterface $threadTrackingMapper,
+ AuthenticationService $authenticationService)
{
$this->threadMapper = $threadMapper;
+ $this->threadTrackingMapper = $threadTrackingMapper;
$this->authenticationService = $authenticationService;
}
@@ -66,6 +78,13 @@ public function addPost(Thread $thread, Post $post)
$thread->addPost($post);
$this->threadMapper->update($thread);
+
+ // we raised the event
+ $this->getEventManager()->trigger('addPost.post', this, array(
+ 'user' => $post->getAuthor(),
+ 'thread' => $thread,
+ 'date' => $post->getSentAt()
+ ));
}
/**
@@ -122,4 +141,30 @@ public function getById($id)
{
return $this->threadMapper->find($id);
}
+
+ /**
+ * Add or update the track for this thread
+ *
+ * @param UserInterface $user
+ * @param Thread $thread
+ */
+ public function track(UserInterface $user, Thread $thread) {
+ $threadTracking = $this->threadTrackingMapper->findOnBy(array(
+ 'thread' => $thread,
+ 'user' => $user
+ ));
+
+ // There is not a track for this thread
+ if ($threadTracking === null) {
+ $threadTracking = new ThreadTracking($user, $thread);
+ $this->threadTrackingMapper->add($threadTracking);
+ } elseif ($threadTracking->getMarkTime() < $thread->getLastPost()->getSentAt()) {
+ $threadTracking->setMarkTime($thread->getLastPost()->getSentAt());
+ $this->threadTrackingMapper->update($threadTracking);
+ }
+
+ // TODO : Penser à tenir compte du CategoryTracking : lorsque tous les threads d'une catégorie sont lus en
+ // TODO : tenant compte du markTime il faut supprimer les threadTracking et ajouter le CategoryTracking à la
+ // TODO : date du jour. Il faut donc s'assurer
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.