354 lines
9.5 KiB
PHP
354 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\NewsletterBundle\Model;
|
|
|
|
use Trinity\Bundle\NewsletterBundle\Configuration\ModelConfiguration;
|
|
use Trinity\Bundle\NewsletterBundle\Model\om\BaseNewsletter;
|
|
use \FOS\UserBundle\Propel\UserQuery;
|
|
use \FOS\UserBundle\Propel\GroupQuery;
|
|
use \FOS\UserBundle\Propel\UserPeer;
|
|
use Trinity\Component\Utils\Propel;
|
|
use WD\Component\Csv\CsvParser;
|
|
|
|
class Newsletter extends BaseNewsletter
|
|
{
|
|
public function __toString()
|
|
{
|
|
if ($this->sent_at) {
|
|
return sprintf('%s envoyé le : %s', $this->name, $this->getSentAt('d/m/Y H:i:s'));
|
|
}
|
|
|
|
return sprintf('%s modifier le : %s', $this->name, $this->getUpdatedAt('d/m/Y H:i:s'));
|
|
}
|
|
|
|
//************** Système d'envoi **************************
|
|
|
|
public function getRecipients()
|
|
{
|
|
$email_list = $this->splitEmailList($this->getEmailListing());
|
|
$users_mail = $this->getUserByGroups();
|
|
|
|
$recipients_array = array_unique(array_merge($email_list, $users_mail));
|
|
$this->setRecipientsNumber(count($recipients_array));
|
|
|
|
$emails = array_diff($recipients_array, $this->getUnsubscribed());
|
|
$this->setBlacklistNumber($this->getRecipientsNumber() - count($emails));
|
|
$recipients = filter_var_array($emails, FILTER_VALIDATE_EMAIL);
|
|
|
|
foreach ($recipients as $key => $value) {
|
|
if (empty($value) || $value == false) {
|
|
unset($recipients[$key]);
|
|
}
|
|
}
|
|
|
|
return $recipients;
|
|
}
|
|
|
|
public function getUnsubscribed()
|
|
{
|
|
$blck_group = GroupQuery::create()->filterByCode('BLACKLIST')->findOne();
|
|
|
|
if (!$blck_group) {
|
|
throw new \Exception('You must define a group with "BLACKLIST" code in order to allow user\'s unsubscribe');
|
|
}
|
|
|
|
return UserQuery::create()->select(UserPeer::EMAIL)->filterByGroup($blck_group)->find()->toArray();
|
|
}
|
|
|
|
private function getUserByGroups()
|
|
{
|
|
if (!$this->getGroups() || $this->countGroups() == 0) {
|
|
return array();
|
|
}
|
|
|
|
$user_query = UserQuery::create()->select(UserPeer::EMAIL);
|
|
|
|
$group_iterator = new \ArrayIterator($this->getGroups());
|
|
|
|
while ($group = $group_iterator->current()) {
|
|
$user_query->filterByGroup($group);
|
|
$group_iterator->next();
|
|
|
|
if ($group_iterator->current()) {
|
|
$user_query->_or();
|
|
}
|
|
}
|
|
|
|
return $user_query->find()->toArray();
|
|
}
|
|
|
|
private function splitEmailList($listing)
|
|
{
|
|
preg_match_all('`([\w.-]+@[\w.-]+\.[a-z]{2,6})`i', $listing, $matchesarray, PREG_SET_ORDER);
|
|
|
|
$lists = array();
|
|
|
|
if (!empty($matchesarray)) {
|
|
foreach ($matchesarray as $matches) {
|
|
$lists[] = $matches[1];
|
|
}
|
|
}
|
|
|
|
return $lists;
|
|
}
|
|
|
|
// *********** Variable dynamique dans CSV *************
|
|
|
|
public function uploadEmailFileVar()
|
|
{
|
|
if (null === $this->email_file_var && !$this->delete_email_file_var && null !== $this->rollback_email_file_var) {
|
|
$this->email_file_var = $this->rollback_email_file_var; // keep the file
|
|
return true;
|
|
}
|
|
|
|
if (null === $this->email_file_var || !is_object($this->email_file_var)) {
|
|
return true; // no file to upload
|
|
}
|
|
|
|
//Delete old file on overload
|
|
$this->removeEmailFileVar();
|
|
|
|
$filename = $this->getNewFilename($this->email_file_var->guessExtension());
|
|
|
|
$this->email_file_var_file = $this->email_file_var;
|
|
|
|
$this->has_uploaded_email_file_var = true;
|
|
|
|
$this->email_file_var = $filename;
|
|
|
|
if ($this->has_uploaded_email_file_var) {
|
|
|
|
$filename = $this->email_file_var_file->getRealPath();
|
|
$parser = new CsvParser($filename, ';', '"', '\\', true);
|
|
$parser->parse();
|
|
$legend = $parser->getLegend();
|
|
|
|
$text = "Vous avez chargez un fichier CSV avec des variables. Ces variables sont accessibles dans les blocks de contenus. <br>Voici la liste exhaustive des variables disponibles : <ul>";
|
|
foreach ($legend as $term) {
|
|
$text .= "<li>" . $this->tokenize($term) . "</li>";
|
|
}
|
|
$text .= "</ul>";
|
|
|
|
$unsubscribed = $this->getUnsubscribed();
|
|
|
|
foreach ($parser->getDatas() as $k => $line) {
|
|
$email = trim($line[0]);
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$no_complient[$k] = $email;
|
|
}
|
|
if (in_array($email, $unsubscribed)) {
|
|
$blacklisted[$k] = $email;
|
|
}
|
|
}
|
|
|
|
if (isset($no_complient)) {
|
|
$text .= "<br>Votre fichier contient des adresses emails invalide, voici la liste: <ul>";
|
|
foreach ($no_complient as $line => $email) {
|
|
$text .= "<li>Ligne " . ($line + 2) . ' : ' . $email . "</li>";
|
|
}
|
|
$text .= "</ul>";
|
|
}
|
|
|
|
if (isset($blacklisted)) {
|
|
$text .= "<br>Votre fichier contient des adresses emails présent dans les désinscrit, voici la liste: <ul>";
|
|
foreach ($blacklisted as $line => $email) {
|
|
$text .= "<li>Ligne " . ($line + 2) . ' : ' . $email . "</li>";
|
|
}
|
|
$text .= "</ul>";
|
|
}
|
|
|
|
$this->setEmailVarDesc($text);
|
|
}
|
|
}
|
|
|
|
public function tokenize($term)
|
|
{
|
|
setlocale(LC_CTYPE, 'fr_FR.utf8');
|
|
|
|
$var = $term;
|
|
|
|
if (function_exists('iconv')) {
|
|
$var = iconv('utf-8', 'us-ascii//TRANSLIT', $var);
|
|
}
|
|
|
|
// lowercase
|
|
if (function_exists('mb_strtolower')) {
|
|
$var = mb_strtolower($var);
|
|
} else {
|
|
$var = strtolower($var);
|
|
}
|
|
|
|
// remove accents resulting from OSX's iconv
|
|
$var = str_replace(array('\'', '`', '^'), '', $var);
|
|
|
|
// replace non letter or digits with separator
|
|
$var = preg_replace('/\W+/', '_', $var);
|
|
|
|
// trim
|
|
$var = trim($var, '_');
|
|
|
|
return '%%' . $var . '%%';
|
|
}
|
|
|
|
//************ Templating et modèle ********************
|
|
|
|
protected $blocks = array();
|
|
|
|
protected $configuration = null;
|
|
|
|
protected $templating = null;
|
|
|
|
protected $object;
|
|
|
|
public function __construct($template = null)
|
|
{
|
|
if ($template) {
|
|
$this->setTemplate($template);
|
|
}
|
|
|
|
parent::__construct();
|
|
|
|
$this->configuration = new ModelConfiguration();
|
|
}
|
|
|
|
public function getConfiguration()
|
|
{
|
|
return $this->configuration;
|
|
}
|
|
|
|
protected function hasBlock($d)
|
|
{
|
|
return isset($this->blocks[is_object($d) ? $d->getName() : $d]);
|
|
}
|
|
|
|
public function setBlock(Block $block)
|
|
{
|
|
$block->setNewsletterId($this->getId());
|
|
|
|
$this->blocks[$block->getName()] = $block;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getTemplating()
|
|
{
|
|
return $this->templating;
|
|
}
|
|
|
|
public function setTemplating($templating)
|
|
{
|
|
$this->templating = $templating;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function save(\PropelPDO $con = null)
|
|
{
|
|
parent::save($con);
|
|
|
|
if (!empty($this->blocks)) {
|
|
foreach ($this->blocks as $block) {
|
|
$block->save($con);
|
|
}
|
|
} else {
|
|
foreach ($this->getConfiguration()->getBlocks() as $block_configuration) {
|
|
$this->getBlock($block_configuration->getName())->save($con);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getBlock($name)
|
|
{
|
|
if (!$this->getId()) {
|
|
return $this->getNewBlock($name);
|
|
}
|
|
|
|
$qBlock = BlockQuery::create()->filterByName($name)->filterByNewsletterId($this->getId())->findOne();
|
|
|
|
$block = $qBlock ? $qBlock : $this->getNewBlock($name, $this->getId());
|
|
|
|
if (!$this->hasBlock($block->getName())) {
|
|
$this->setBlock($block);
|
|
}
|
|
|
|
return $block;
|
|
}
|
|
|
|
public function getBlockTitle()
|
|
{
|
|
return $this->getBlock('title');
|
|
}
|
|
|
|
public function getBlockSubtitle()
|
|
{
|
|
return $this->getBlock('subtitle');
|
|
}
|
|
|
|
public function getBlockContent()
|
|
{
|
|
return $this->getBlock('content');
|
|
}
|
|
|
|
public function getBlockSubject()
|
|
{
|
|
return $this->getBlock('subject');
|
|
}
|
|
|
|
public function setBlockTitle(Block $block)
|
|
{
|
|
return $this->setBlock($block);
|
|
}
|
|
|
|
public function setBlockSubtitle(Block $block)
|
|
{
|
|
return $this->setBlock($block);
|
|
}
|
|
|
|
public function setBlockContent(Block $block)
|
|
{
|
|
return $this->setBlock($block);
|
|
}
|
|
|
|
public function setBlockSubject(Block $block)
|
|
{
|
|
return $this->setBlock($block);
|
|
}
|
|
|
|
protected function getNewBlock($name, $newsletterId = null)
|
|
{
|
|
$type = $this->getConfiguration()->getBlock($name)->getType();
|
|
$form = new $type();
|
|
$form_model = $form->getOption('data_class') ? $form->getOption('data_class') : 'Block';
|
|
|
|
$block = new $form_model();
|
|
$block->setname($name);
|
|
|
|
if (null !== $newsletterId) {
|
|
$block->setNewsletterId($newsletterId);
|
|
}
|
|
|
|
return $block;
|
|
}
|
|
|
|
//********** Affichage BO ***********
|
|
|
|
public function getRecipientsGroups()
|
|
{
|
|
return $this->getGroups();
|
|
}
|
|
|
|
public function getStats()
|
|
{
|
|
return $this;
|
|
}
|
|
|
|
public function getSubject()
|
|
{
|
|
$subject = $this->getBlock('subject');
|
|
|
|
return ($subject) ? $subject : $this->getName();
|
|
}
|
|
}
|