452 lines
16 KiB
PHP
452 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\NewsletterBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
|
use Symfony\Component\Form\Form;
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
use Trinity\Bundle\NewsletterBundle\Configuration\NewsletterCrudConfiguration as CrudConfiguration;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Trinity\Bundle\AdminBundle\Controller\BaseAdminController;
|
|
use Trinity\Bundle\NewsletterBundle\Model\Newsletter;
|
|
use Trinity\Bundle\NewsletterBundle\Model\NewsletterQuery;
|
|
use Trinity\Bundle\UserBundle\Flash\FlashError;
|
|
use Trinity\Bundle\UserBundle\Flash\FlashSuccess;
|
|
|
|
use \FOS\UserBundle\Propel\UserQuery;
|
|
use \FOS\UserBundle\Propel\GroupQuery;
|
|
use Trinity\Bundle\UserBundle\Flash\FlashWarning;
|
|
use WD\Component\Csv\CsvParser;
|
|
|
|
/**
|
|
* @Route("/admin/newsletter")
|
|
*/
|
|
class NewsletterAdminController extends BaseAdminController
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->configuration = new CrudConfiguration();
|
|
}
|
|
|
|
protected function getFormFilter($new = false)
|
|
{
|
|
$form = $this->getConfiguration()->getFormFilter() ? $this->getConfiguration()->getFormFilter() : $this->getConfiguration()->getFormNew();
|
|
|
|
$options = $this->container->getParameter('trinity_newsletter.newsletter');
|
|
$options = array_merge(
|
|
$this->getConfiguration()->getFormFilterOptions(),
|
|
array('models' => $options['models'])
|
|
);
|
|
|
|
$form = $this->createForm(new $form($options));
|
|
|
|
if (!$new) {
|
|
$datas = $this->cleanFilterArray($this->getSessionUser()->getAttribute('filter', array(),
|
|
$this->getConfiguration()->getStorageNamespace()));
|
|
$form->bind($datas);
|
|
}
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* @Route("/list/{page}", name="TrinityNewsletterBundleNewsletterAdmin_index", defaults={"page" = "1"}, requirements={"page" = "\d+"})
|
|
* @Template()
|
|
*/
|
|
public function indexAction($page, Request $request)
|
|
{
|
|
$options = $this->container->getParameter('trinity_newsletter.newsletter');
|
|
|
|
$templates = array();
|
|
|
|
foreach ($options['models'] as $key => $model) {
|
|
$templates[$key] = array();
|
|
foreach ($model['templates'] as $template) {
|
|
$templates[$key][$template['template']] = $template['title'];
|
|
}
|
|
}
|
|
|
|
return array_merge(
|
|
parent::indexAction($page, $request),
|
|
array(
|
|
'models' => $options['models'],
|
|
'templates' => $templates
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @Route("/new", name="TrinityNewsletterBundleNewsletterAdmin_new")
|
|
* @Template()
|
|
*/
|
|
public function newAction(Request $request)
|
|
{
|
|
$options = $this->getConfiguration()->getFormNewOptions();
|
|
if ($this->has('global_vars')) {
|
|
$sender_name = $this->get('global_vars')->get('trinity_newsletter_sender_name');
|
|
$sender_mail = $this->get('global_vars')->get('trinity_newsletter_sender_mail');
|
|
|
|
$this->getConfiguration()->setFormNewOptions(
|
|
array_merge(
|
|
$options,
|
|
array(
|
|
'sender_name' => $sender_name,
|
|
'sender_mail' => $sender_mail
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
$options = $this->getConfiguration()->getFormNewOptions();
|
|
$options_nws = $this->container->getParameter('trinity_newsletter.newsletter');
|
|
$options = array_merge($options, $options_nws);
|
|
|
|
$model = $this->getConfiguration()->getModel();
|
|
$object = new $model();
|
|
$form = $this->getConfiguration()->getFormNew();
|
|
if ($this->has($form)) {
|
|
$form = $this->createForm($this->get($form)->getName(), $object, $options);
|
|
} else {
|
|
$form = $this->createForm(new $form($options), $object);
|
|
}
|
|
|
|
if ('POST' === $request->getMethod()) {
|
|
if (false !== $processForm = $this->processForm($form, $object, $request)) {
|
|
$this->getSessionUser()->setFlash('message', new FlashSuccess('crud.flash.updated.singular'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $processForm;
|
|
}
|
|
|
|
$this->getSessionUser()->setFlash('message', new FlashError('crud.flash.fail.new'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
}
|
|
|
|
return array(
|
|
'form' => $form->createView(),
|
|
'fieldsets' => $this->getConfiguration()->getFieldsets(),
|
|
'fieldsetsView' => $this->getConfiguration()->getFieldsetsView(),
|
|
'fieldsnames' => $this->getConfiguration()->getFieldsnames(),
|
|
'fields_helpers' => $this->getConfiguration()->getFieldsHelpers(),
|
|
'title' => $this->getConfiguration()->getNewTitle(),
|
|
'i18n' => $this->getConfiguration()->getI18n(),
|
|
'i18n_cultures' => $this->getConfiguration()->getI18nCultures(),
|
|
'collections' => $this->getConfiguration()->getCollections(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @Route("/edit/{id}", name="TrinityNewsletterBundleNewsletterAdmin_edit")
|
|
* @Template()
|
|
* @ParamConverter("object", class="Trinity\Bundle\NewsletterBundle\Model\Newsletter")
|
|
*/
|
|
public function editAction($object, Request $request)
|
|
{
|
|
if (!$object) {
|
|
$this->getSessionUser()->setFlash('message', new FlashWarning('crud.flash.unknown'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $this->redirect($this->generateUrl(sprintf('%s_index',
|
|
$this->getConfiguration()->getRoutePrefix())));
|
|
}
|
|
|
|
$options = $this->container->getParameter('trinity_newsletter.newsletter');
|
|
|
|
$form = $this->getConfiguration()->getFormEdit();
|
|
if ($this->has($form)) {
|
|
$form = $this->createForm($this->get($form)->getName(), $object, $options);
|
|
} else {
|
|
$form = $this->createForm(new $form($options), $object);
|
|
}
|
|
|
|
if ('POST' === $request->getMethod()) {
|
|
if (false !== $processForm = $this->processForm($form, $object, $request)) {
|
|
$this->getSessionUser()->setFlash('message', new FlashSuccess('crud.flash.updated.singular'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $processForm;
|
|
}
|
|
|
|
$this->getSessionUser()->setFlash('message', new FlashError('crud.flash.fail.update'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
}
|
|
|
|
return array(
|
|
'object' => $object,
|
|
'created_at' => (method_exists($object, 'getCreatedAt')) ? $object->getCreatedAt('d/m/Y') : null,
|
|
'updated_at' => (method_exists($object, 'getUpdatedAt')) ? $object->getUpdatedAt('d/m/Y') : null,
|
|
'form' => $form->createView(),
|
|
'fieldsets' => $this->getConfiguration()->getFieldsets(),
|
|
'fieldsetsView' => $this->getConfiguration()->getFieldsetsView(),
|
|
'fieldsnames' => $this->getConfiguration()->getFieldsnames(),
|
|
'fields_helpers' => $this->getConfiguration()->getFieldsHelpers(),
|
|
'remove_token' => $this->getRemoveToken(true),
|
|
'title' => $this->getConfiguration()->getEditTitle($object),
|
|
'i18n' => $this->getConfiguration()->getI18n(),
|
|
'i18n_cultures' => $this->getConfiguration()->getI18nCultures(),
|
|
'collections' => $this->getConfiguration()->getCollections(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @Route("/remove/{id}/{token}", name="TrinityNewsletterBundleNewsletterAdmin_remove")
|
|
* @Template()
|
|
* @ParamConverter("object", class="Trinity\Bundle\NewsletterBundle\Model\Newsletter")
|
|
*/
|
|
public function removeAction($object, $token, Request $request)
|
|
{
|
|
return parent::removeAction($object, $token, $request);
|
|
}
|
|
|
|
/**
|
|
* @Route("/batch", name="TrinityNewsletterBundleNewsletterAdmin_batch")
|
|
* @Template()
|
|
* @Method({"POST"})
|
|
*/
|
|
public function batchAction(Request $request)
|
|
{
|
|
return parent::batchAction($request);
|
|
}
|
|
|
|
/**
|
|
* @Route("/filter/clear", name="TrinityNewsletterBundleNewsletterAdmin_filter_clear")
|
|
* @Template()
|
|
*/
|
|
public function clearFilterAction(Request $request)
|
|
{
|
|
return parent::clearFilterAction($request);
|
|
}
|
|
|
|
/**
|
|
* @Route("/send/{id}", name="TrinityNewsletterBundleNewsletterAdmin_send")
|
|
* @Template()
|
|
* @ParamConverter("object", class="Trinity\Bundle\NewsletterBundle\Model\Newsletter")
|
|
*/
|
|
public function sendAction($object, Request $request)
|
|
{
|
|
/** @var $object Newsletter */
|
|
if ($object->getSentAt()) {
|
|
$this->getSessionUser()->setFlash('message', new FlashError('crud.flash.fail.send'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $this->redirect($this->generateUrl('TrinityNewsletterBundleNewsletterAdmin_index'));
|
|
}
|
|
|
|
if (!$object->getClassKey()) {
|
|
$this->getSessionUser()->setFlash('message', new FlashError('crud.flash.fail.no_model'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $this->redirect($this->generateUrl('TrinityNewsletterBundleNewsletterAdmin_index'));
|
|
}
|
|
|
|
$object->setSentAt(new \DateTime('now'));
|
|
|
|
$blocks = array();
|
|
|
|
foreach ($object->getBlocks() as $block) {
|
|
$blocks[$block->getName()] = $block;
|
|
}
|
|
|
|
$message = \Swift_Message::newInstance()
|
|
->setSubject($object->getSubject())
|
|
->setFrom(array($object->getSenderEmail() => $object->getSenderName()));
|
|
|
|
if ($object->getEmailFileVar() && $object->getEmailVarDesc()) {
|
|
|
|
$filename = $object->getEmailFileVarAsFile()->getRealPath();
|
|
$parser = new CsvParser($filename, ';', '"', '\\', true);
|
|
$parser->parse();
|
|
$legend = $parser->getLegend();
|
|
$datas = $parser->getDatas();
|
|
|
|
$unsubscribed = $object->getUnsubscribed();
|
|
$sended = 0;
|
|
$blacklisted = 0;
|
|
|
|
foreach ($datas as $line) {
|
|
$email = trim($line[0]);
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
|
|
continue;
|
|
}
|
|
|
|
if(in_array($email, $unsubscribed)){
|
|
$blacklisted++;
|
|
continue;
|
|
}
|
|
|
|
$token = $this->generateUnsubscribeToken($email);
|
|
|
|
$user = UserQuery::create()->filterByEmail($email)->findOne();
|
|
|
|
if ($user) {
|
|
$user->setUnsubscribeToken($token);
|
|
$user->save();
|
|
}
|
|
|
|
$body = $this->renderView(
|
|
$object->getTemplate(),
|
|
array(
|
|
'model' => $object,
|
|
'blocks' => $blocks,
|
|
'email' => $email,
|
|
'unsubscribe_token' => $token
|
|
)
|
|
);
|
|
|
|
foreach ($legend as $term) {
|
|
$token = $object->tokenize($term);
|
|
$value = $line[$term];
|
|
$body = str_replace($token, trim($value), $body);
|
|
}
|
|
|
|
$message
|
|
->setTo($email)
|
|
->setBody($body, 'text/html');
|
|
|
|
$this->get('swiftmailer.mailer.spool_mailer')->send($message);
|
|
|
|
$sended++;
|
|
}
|
|
|
|
$object->setBlacklistNumber($blacklisted);
|
|
$object->setRecipientsNumber($sended);
|
|
|
|
} else {
|
|
foreach ($object->getRecipients() as $email) {
|
|
|
|
$user = UserQuery::create()->filterByEmail($email)->findOne();
|
|
|
|
$token = $this->generateUnsubscribeToken($email);
|
|
|
|
if ($user) {
|
|
$user->setUnsubscribeToken($token);
|
|
$user->save();
|
|
}
|
|
|
|
$body = $this->renderView(
|
|
$object->getTemplate(),
|
|
array(
|
|
'model' => $object,
|
|
'blocks' => $blocks,
|
|
'email' => $email,
|
|
'unsubscribe_token' => $token
|
|
)
|
|
);
|
|
|
|
$message
|
|
->setTo($email)
|
|
->setBody($body, 'text/html');
|
|
|
|
$this->get('swiftmailer.mailer.spool_mailer')->send($message);
|
|
}
|
|
}
|
|
|
|
$object->save();
|
|
|
|
$this->getSessionUser()->setFlash('message', new FlashSuccess('crud.flash.success.send'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $this->redirect($this->generateUrl('TrinityNewsletterBundleNewsletterAdmin_index'));
|
|
}
|
|
|
|
/**
|
|
* @Route("/copy/{id}", name="TrinityNewsletterBundleNewsletterAdmin_copy")
|
|
* @Template()
|
|
* @ParamConverter("object", class="Trinity\Bundle\NewsletterBundle\Model\Newsletter")
|
|
*/
|
|
public function copyAction($object, Request $request)
|
|
{
|
|
NewsletterQuery::create()
|
|
->findPK($object->getId())
|
|
->copy(true)
|
|
->setSentAt(null)
|
|
->setCreatedAt(new \DateTime('now'))
|
|
->save();
|
|
|
|
$this->getSessionUser()->setFlash('message', new FlashSuccess('crud.flash.success.copy'), false,
|
|
$this->getConfiguration()->getStorageNamespace());
|
|
|
|
return $this->redirect($this->generateUrl('TrinityNewsletterBundleNewsletterAdmin_index'));
|
|
}
|
|
|
|
protected function copyObjects($query, array $pks)
|
|
{
|
|
foreach ($pks as $pk) {
|
|
$query->findPK($pk)
|
|
->copy(true)
|
|
->setSentAt(null)
|
|
->setCreatedAt(new \DateTime('now'))
|
|
->save();
|
|
}
|
|
}
|
|
|
|
private function generateUnsubscribeToken($email)
|
|
{
|
|
$key = '1ag4jf96znv07m459kf29kfZl5I9fnvT8dfg0pza114bM5fg6Kl';
|
|
$iv = '3452562488791564';
|
|
$token = openssl_encrypt($email, 'aes128', $key, false, $iv);
|
|
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* @param $form Form
|
|
* @param $object Newsletter
|
|
* @param Request $request
|
|
* @return bool|\Symfony\Component\HttpFoundation\RedirectResponse
|
|
* @throws \Trinity\Bundle\AdminBundle\Exception\RequestAdminException
|
|
*/
|
|
protected function processForm(&$form, &$object, Request $request)
|
|
{
|
|
$form->submit($request);
|
|
|
|
if ($form->isValid()) {
|
|
|
|
$this->preSave($object);
|
|
|
|
$uploaded_file = $object->getEmailFile();
|
|
if ($uploaded_file instanceof UploadedFile) {
|
|
|
|
$lines = file($uploaded_file->getPathname());
|
|
$emails = "";
|
|
$regex2 = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-'
|
|
. '\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-'
|
|
. '\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-'
|
|
. '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80'
|
|
. '-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29'
|
|
. '\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^'
|
|
. '\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-'
|
|
. '\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-'
|
|
. '\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*'
|
|
. '\\x5d))*$/';
|
|
|
|
foreach ($lines as $line_num => $line) {
|
|
$words = preg_split("/[\s,\;]+/", $line);
|
|
foreach ($words as $word) {
|
|
if (preg_match($regex2, $word, $matches)) {
|
|
$emails .= "\n" . $matches[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
$object->setEmailListing($object->getEmailListing() . $emails);
|
|
}
|
|
|
|
$object->save();
|
|
|
|
$this->postSave($object);
|
|
|
|
return $this->redirect($this->redirectByRequest($request, $object));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|