Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save midnai/e0f8bf5381f6cce6bbb7 to your computer and use it in GitHub Desktop.
Save midnai/e0f8bf5381f6cce6bbb7 to your computer and use it in GitHub Desktop.
<?php
// src/Acme/FormsTutorialBundle/AcmeFormsTutorialBundle.php
namespace Acme\FormsTutorialBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeFormsTutorialBundle extends Bundle
{
}
<?php
// src/Acme/FormsTutorialBundle/DependencyInjection/AcmeFormsTutorialExtension.php
namespace Acme\FormsTutorialBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Resource\FileResource;
class AcmeFormsTutorialExtension extends Extension implements PrependExtensionInterface
{
public function load( array $config, ContainerBuilder $container )
{
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
}
public function prepend( ContainerBuilder $container )
{
$configFile = __DIR__ . '/../Resources/config/ezpublish.yml';
$config = Yaml::parse( file_get_contents( $configFile ) );
$container->prependExtensionConfig( 'ezpublish', $config );
$container->addResource( new FileResource( $configFile ) );
}
}
<?php
// src/Acme/FormsTutorialBundle/DependencyInjection/AcmeFormsTutorialExtension.php
namespace Acme\FormsTutorialBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;
class AcmeFormsTutorialExtension extends Extension
{
public function load( array $config, ContainerBuilder $container )
{
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
$loader->load( 'services.yml' );
}
}
<?php
// src/Acme/FormsTutorialBundle/FormHandler/AdminEmailHandler.php
namespace Acme\FormsTutorialBundle\FormHandler;
use Heliopsis\eZFormsBundle\FormHandler\SwiftMailer\AbstractHandler;
use Acme\FormsTutorialBundle\Model\Feedback;
class AdminEmailHandler extends AbstractHandler
{
/**
* @var string
*/
private $recipientEmail;
/**
* @var string
*/
private $recipientName;
/**
* @param $email
* @param string $name
*/
public function setRecipient( $email, $name = '' )
{
$this->recipientEmail = $email;
$this->recipientName = $name;
}
/**
* @return void
*/
protected function addRecipients( \Swift_Message $message )
{
if ( !$this->recipientEmail )
{
throw new \RuntimeException( "Recipient unknown" );
}
$message->addTo( $this->recipientEmail, $this->recipientName );
}
/**
* @return mixed
*/
protected function addSender( \Swift_Message $message )
{
$data = $this->getData();
if ( !$data instanceof Feedback )
{
throw new \RuntimeException( "Data should be of the feedback type" );
}
$message->addFrom( $data->email, $data->fullName );
}
/**
* @return array
*/
protected function getTemplateParameters()
{
return array(
'subject' => $this->getSubject(),
);
}
/**
* @param \Swift_Message $message
*/
protected function addAttachments( \Swift_Message $message )
{
}
}
# ezpublish/config/config.yml
...
heliopsis_ezforms:
providers:
form: acme_forms_tutorial.form_provider
handler: acme_forms_tutorial.handler_provider
# ezpublish/config/config.yml
...
swiftmailer:
transport: gmail
username: your_gmail_username
password: your_gmail_password
# ezpublish/config/config.yml
...
swiftmailer:
transport: sendmail
# ezpublish/config/config.yml
...
heliopsis_ezforms:
providers:
form: acme_forms_tutorial.form_provider
# ezpublish/config/config_dev.yml
...
swiftmailer:
delivery_address: your-email@company.com
{# src/Acme/FormsTutorialBundle/Resources/views/confirm/form.html.twig #}
{% extends "eZDemoBundle::pagelayout.html.twig" %}
{% block content %}
<section class="content-view-full">
<div class="row">
<div class="span8">
<div class="attribute-header">
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
</div>
<div class="attribute-short">
{{ ez_render_field( content, 'confirm' ) }}
</div>
</div>
</div>
</section>
{% endblock content %}
# src/Acme/FormsTutorialBundle/Resources/config/ezpublish.yml
system:
ezdemo_frontend_group:
location_view:
full:
form:
controller: "heliopsis_ezforms.controller:formAction"
template: "AcmeFormsTutorialBundle:full:form.html.twig"
match:
Identifier\ContentType: [form]
confirm:
form:
template: "AcmeFormsTutorialBundle:confirm:form.html.twig"
match:
Identifier\ContentType: [form]
<?php
//ezpublish/EzPublishKernel.php
...
use Heliopsis\eZFormsBundle\HeliopsiseZFormsBundle;
...
class EzPublishKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
...
new HeliopsiseZFormsBundle(),
);
...
}
}
<?php
//ezpublish/EzPublishKernel.php
...
use Acme\FormsTutorialBundle\AcmeFormsTutorialBundle;
...
class EzPublishKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
...
new AcmeFormsTutorialBundle(),
);
...
}
}
{# src/Acme/FormsTutorialBundle/Resources/views/email/feedback.html.twig #}
<!DOCTYPE html>
<html>
<head>
<title>{{ subject }}</title>
</head>
<body>
<h1>{{ subject }}</h1>
<p>{{ data.fullName }} ({{ data.email }} ) submitted this message:</p>
<p>{{ data.message|nl2br }}</p>
</body>
</html>
<?php
// src/Acme/FormsTutorialBundle/Model/Feedback.php
namespace Acme\FormsTutorialBundle\Model;
class Feedback
{
/**
* @var string
*/
public $fullName;
/**
* @var string
*/
public $email;
/**
* @var string
*/
public $message;
}
<?php
// src/Acme/FormsTutorialBundle/Form/FeedbackType.php
namespace Acme\FormsTutorialBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FeedbackType extends AbstractType
{
public function getName()
{
return 'acme_feedback';
}
public function setDefaultOptions( OptionsResolverInterface $resolver )
{
$resolver->setDefaults( array( 'data_class' => 'Acme\\FormsTutorialBundle\\Model\\Feedback' ) );
}
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder->add(
'fullName',
'text',
array(
'label' => 'Your full name',
)
);
$builder->add(
'email',
'email',
array(
'label' => 'Your email',
)
);
$builder->add(
'message',
'textarea',
array(
'label' => 'Your message',
)
);
$builder->add(
'send',
'submit',
array(
'label' => 'Send!',
)
);
}
}
{# src/Acme/FormsTutorialBundle/Resources/views/full/form.html.twig #}
{% extends "eZDemoBundle::pagelayout.html.twig" %}
{% block content %}
<section class="content-view-full">
<div class="row">
<div class="span8">
<div class="attribute-header">
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
</div>
<div class="attribute-short">
{{ ez_render_field( content, 'intro' ) }}
</div>
{{ form( form ) }}
</div>
</div>
</section>
{% endblock content %}
# ezpublish/config/parameters.yml
parameters:
admin.email: 'admin@your-site.com'
admin.name: 'Website Administrator'
# src/Acme/FormsTutorialBundle/Resources/config/services.yml
parameters:
services:
# src/Acme/FormsTutorialBundle/Resources/config/services.yml
parameters:
acme_forms_tutorial.form.type.feedback.class: Acme\FormsTutorialBundle\Form\FeedbackType
services:
acme_forms_tutorial.form.type.feedback:
class: %acme_forms_tutorial.form.type.feedback.class%
tags:
- { name: form.type, alias: 'acme_feedback' }
# src/Acme/FormsTutorialBundle/Resources/config/services.yml
...
services:
...
acme_forms_tutorial.form_provider:
class: %heliopsis_ezforms.form_provider.content_remoteid_map.class%
arguments: [ @form.factory ]
calls:
- [ addFormType, [ '09fc43d390f64e35a3f51fdbbe980d66', 'acme_feedback' ] ]
# src/Acme/FormsTutorialBundle/Resources/config/services.yml
parameters:
...
acme_forms_tutorial.form_handler.admin_email.class: Acme\FormsTutorialBundle\FormHandler\AdminEmailHandler
services:
...
acme_forms_tutorial.form_handler.admin_email:
class: %acme_forms_tutorial.form_handler.admin_email.class%
arguments: [ @swiftmailer.mailer, @templating, @translator ]
calls:
- [ setContentType, [ 'text/html' ] ]
- [ setTemplate, [ 'AcmeFormsTutorialBundle:email:feedback.html.twig' ] ]
- [ setSubject, [ 'New feedback from the website' ] ]
- [ setRecipient, [ %admin.email%, %admin.name% ] ]
# src/Acme/FormsTutorialBundle/Resources/config/services.yml
...
services:
...
acme_forms_tutorial.handler_provider:
class: %heliopsis_ezforms.handler_provider.content_remoteid_map.class%
calls:
- [ addFormHandler, [ '09fc43d390f64e35a3f51fdbbe980d66', @acme_forms_tutorial.form_handler.admin_email ] ]
# src/Acme/FormsTutorialBundle/Resourtces/config/validation.yml
Acme\FormsTutorialBundle\Model\Feedback:
properties:
fullName:
- NotBlank: { message: "Please fill in your full name" }
email:
- NotBlank: { message: "Please fill in your email address" }
- Email: ~
message:
- NotBlank: { message: "Are you sure you've got nothing to say?" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment