firediy/private-message-bundle

Symfony FDPrivateMessageBundle

dev-master 2017-10-05 16:45 UTC

This package is not auto-updated.

Last update: 2024-04-25 02:32:16 UTC


README

Build Status Coverage Status Total Downloads SensioLabsInsight

This bundle provides a conversation system to your users.

knpbundles.com

Requirements

  • Symfony >= 2.8
  • A user class implementing Symfony\Component\Security\Core\User\UserInterface

Translations

If you wish to use default texts provided in this bundle, you have to make sure you have translator enabled in your config.

# app/config/config.yml
framework:
    translator: ~

Installation

Step 1: Download FDPrivateMessageBundle using composer

Require the bundle with composer:

composer require firediy/private-message-bundle dev-master

Step 2: Enable the bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FD\PrivateMessageBundle\FDPrivateMessageBundle(),
        // ...
    );
}

Step 3: Create your own User class

The goal of this bundle is to provide a private message system to allow users communicating between them. FDPrivateMessageBundle does not provide a ready-to-use User entity but uses Symfony's UserInterface.

So, you just have to create your own User class implementing Symfony\Component\Security\Core\User\UserInterface. You can even use FOSUserBundle.

Step 4: Configure your application's config.yml

Now you have your User entity, you just have to tell FDPrivateMessageBundle to use it :

# app/config/config.yml
doctrine:
    orm:
        resolve_target_entities:
            Symfony\Component\Security\Core\User\UserInterface: AcmeBundle\Entity\YourUserEntiy

Step 5: Import FDPrivateMessageBundle routing files

Now that you have activated and configured the bundle, all that is left to do is import the FDPrivateMessageBundle routing files.

# app/config/routing.yml
fd_private_message:
    resource: "@FDPrivateMessageBundle/Resources/config/routing.yml"

Step 6: Update your database schema

Finally, just update your database schema :

php bin/console doctrine:schema:update --force

Form

You are able to override FDPrivateMessageBundle's forms.

In example, you're using FOSUserBundle and want to load only recipients being NOT locked :

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use FD\PrivateMessageBundle\Form\ConversationType as BaseType;


// Make your form extends FDPrivateMessageBunde::ConversationType.
class ConversationType extends BaseType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Call parent's builder.
        parent::buildForm($builder, $options);


        // Load only users being enabled.
        $builder->add('recipients', EntityType::class, array(
            'class'         => 'AcmeBundle:YourUserEntity',
            'multiple'      => true,
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->where('u.locked', false);
            },
        ));

    }
}

Override default FDPrivateMessageBundle templates

Example: Override the conversation's show.html.twig template

Just create a new file in app/Resources/FDPrivateMessageBundle/views/Conversation/show.html.twig

<h1>Overridden template</h1>

<h2>{{ conversation.subject }}</h2>

<ul>
    {% for message in conversation.messages %}
        <li>
            <p>{{ message.author }}, {{ message.created | date('Y-m-d H:i:s') }}</p>
            <div>{{ message.body | raw }}</div>
        </li>
    {% endfor %}
</ul>