Created
January 22, 2013 18:38
-
-
Save rodrigodiez/4597124 to your computer and use it in GitHub Desktop.
Feedback message system to store messages on users session and show them on templates. Flash messages and persistent ones, two implementations. It works. Maybe this Gist would rise some errors because of the changes the code needed to make it public. Hope no security holes. Enjoy!
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 | |
/* | |
* This file is part of the FOSUserBundle package. | |
* | |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Acme\Bundle\FeedbackBundle\Controller; | |
use Acme\Bundle\FeedbackBundle\Form\Type\FormType as FormType; | |
use Acme\Bundle\FeedbackBundle\Model\FormModel as FormModel; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\DependencyInjection\ContainerAware; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
class DefaultController extends ContainerAware | |
{ | |
public function indexAction(Request $request) | |
{ | |
$form = $this->createForm(new FormType(),new FormModel()) | |
if('POST' === $request->getMethod() | |
{ | |
$form->bind($request); | |
if ($form->isValid()) { | |
$url = $this->container->get('router')->generate('your_redirect_url'); | |
$response = new RedirectResponse($url); | |
} | |
// If errors | |
$this->noticeFormErrors($form); | |
return $this->container->get('templating')->renderResponse('AcmeFeedbackBundle:Demo:form_error.json.twig',array(),new Response('',500)); | |
} | |
} | |
private function noticeFormErrors($form) | |
{ | |
foreach($form->getErrors() as $error) | |
{ | |
$opts = array( | |
'level' => FlashFeedbackMessage::LEVEL_ERROR, | |
'scope' => $form->getName(), | |
'text' => $error->getMessage(), | |
'html' => $error->getMessage() | |
); | |
$message = new FlashFeedbackMessage($opts); | |
$message->notify($this->get('session')); | |
} | |
foreach($this->form->getChildren() as $child) | |
{ | |
foreach($child->getErrors() as $error) | |
{ | |
$opts = array( | |
'level' => FlashFeedbackMessage::LEVEL_ERROR, | |
'scope' => $form->getName() . '_' . $child->getName(), | |
'text' => $error->getMessage(), | |
'html' => $error->getMessage() | |
); | |
$message = new FlashFeedbackMessage($opts); | |
$message->notify($this->get('session')); | |
} | |
} | |
} | |
} |
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\Bundle\FeedbackBundle\Util\FeedbackMessageInterface.php | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Symfony\Component\OptionsResolver\Options; | |
abstract class FeedbackMessageInterface | |
{ | |
protected $level; | |
protected $scope; | |
protected $text; | |
protected $html; | |
const LEVEL_INFO = 'info'; | |
const LEVEL_SUCCESS = 'success'; | |
const LEVEL_WARNING = 'warning'; | |
const LEVEL_ERROR = 'error'; | |
public function __construct(array $options = array()) | |
{ | |
$resolver = new OptionsResolver(); | |
$this->setDefaultOptions($resolver); | |
$this->setRequiredOptions($resolver); | |
$this->options = $resolver->resolve($options); | |
$this->level = $this->options['level']; | |
$this->scope = $this->options['scope']; | |
$this->text = $this->options['text']; | |
$this->html = $this->options['html']; | |
} | |
protected function setRequiredOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setRequired(array( | |
'scope', | |
'text' | |
)); | |
} | |
protected function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'level' => self::LEVEL_INFO, | |
'html' => $this->options['text'] | |
)); | |
} | |
} |
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\Bundle\FeedbackBundle\Util\Feedback; | |
use Symfony\Component\HttpFoundation\Session\Session; | |
class FlashFeedbackMessage extends FeedbackMessageInterface | |
{ | |
public $level; | |
public $scope; | |
public $text; | |
public $html; | |
public function notify(Session $session) | |
{ | |
$session->getFlashBag()->add('feedback_messages',(array)$this); | |
} | |
} |
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\Bundle\FeedbackBundle\Util\Feedback; | |
use Symfony\Component\HttpFoundation\Session\Session; | |
class PersistentFeedbackMessage extends FeedbackMessageInterface | |
{ | |
protected $id; | |
public $level; | |
public $scope; | |
public $text; | |
public $html; | |
const ID_EMAIL_NOT_VERIFIED = 'email_not_verified'; | |
const ID_PROFILE_NOT_COMPLETED = 'profile_not_completed'; | |
public function __construct(array $options = array()) | |
{ | |
parent::__construct($options); | |
$this->id = $this->options['id']; | |
} | |
public function notify(Session $session) | |
{ | |
$session->set('feedback_messages/' . $this->id,(array)$this); | |
} | |
protected function setRequiredOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setRequired(array( | |
'id', | |
'scope', | |
'text' | |
)); | |
$resolver->setAllowedValues(array( | |
'id' => array(self::ID_PROFILE_NOT_COMPLETED, self::ID_EMAIL_NOT_VERIFIED) | |
)); | |
} | |
} |
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
{ | |
messages: [ | |
{% block messages %} | |
{% for message in app.session.flashbag.get('feedback_messages') %} | |
{ | |
level: '{{ message.level }}', | |
persist: false, | |
scope: '{{ message.scope }}', | |
text: '{{ message.text }}', | |
html: '{{ message.html }}' | |
}, | |
{% endfor %} | |
{% for message in app.session.get('feedback_messages') %} | |
{ | |
level: '{{ message.level }}', | |
persist: true, | |
scope: '{{ message.scope }}', | |
text: '{{ message.text }}', | |
html: '{{ message.html }}' | |
}, | |
{% endfor %} | |
{% endblock %} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment