68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\NewsletterBundle\Model;
|
|
|
|
use Trinity\Bundle\NewsletterBundle\Model\om\BaseNewsletter;
|
|
use \FOS\UserBundle\Propel\UserQuery;
|
|
use \FOS\UserBundle\Propel\GroupQuery;
|
|
use \FOS\UserBundle\Propel\UserPeer;
|
|
|
|
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'));
|
|
}
|
|
|
|
public function getRecipients()
|
|
{
|
|
$email_list = explode(',', $this->getEmailListing());
|
|
$users_mail = $this->getUserByGroups();
|
|
|
|
return array_diff(
|
|
array_unique(
|
|
array_merge(
|
|
$email_list,
|
|
$users_mail)
|
|
),
|
|
$this->getUnsubscribed()
|
|
);
|
|
}
|
|
|
|
private 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()) {
|
|
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();
|
|
}
|
|
}
|