<?php
namespace Notaris\AdminBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* NewsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class NewsRepository extends EntityRepository
{
public function deactivateOtherSpotlight($locale){
$update = $this->createQueryBuilder('n')
->update()
->set('n.spotlight', '0')
->where('n.locale = :locale AND n.spotlight = true')
->setParameter('locale', $locale)
->getQuery()
->execute();
}
/**
* @param $locale
* @param int $offset
* @param array $sort_arr
* @param int $limit
* @return array|bool
* @throws \Doctrine\DBAL\DBALException
*/
public function getAllNewsItems($locale, $offset = 0, $sort_arr = array(), $limit = 50){
//start orders
$orders_arr = array();
$order_type = $sort_arr['type'];
$order_columns = array(
0 => 'n.pub_date',
'n.title',
'n.is_izimi',
'n.count_clicked',
'newsletter',
'is_active',
'origin',
);
if (isset($order_columns[$sort_arr['column']]) && $order_columns[$sort_arr['column']]) {
$orders_arr[] = $order_columns[$sort_arr['column']] . ' ' . $order_type;
}
//end orders
$em = $this->getEntityManager();
$con = $em->getConnection();
$sql = "SELECT n.id, n.pub_date, n.title, n.count_clicked, n.is_izimi,
IF (n.is_active = 1,(IF(DATE(n.pub_date) <= DATE(NOW()), n.is_active, 2)),n.is_active) AS is_active,
n.spotlight, n.in_newsletter, n.newsletter_sended,
IF((n.in_newsletter and n.is_active), n.in_newsletter, IF(n.newsletter_sended, n.newsletter_sended, '')) as newsletter, n.slug as news_slug,
f.id as faq,
b.id as brochure,
v.id as video,
IF(f.id, 'FAQ', IF(b.id, 'BROCH', IF(v.id, 'VIDEO', ''))) as `origin`
FROM news n
LEFT JOIN faq f ON f.news_id = n.id
LEFT JOIN brochure b ON b.news_id = n.id
LEFT JOIN video v ON v.news_id = n.id
WHERE n.locale = :locale
ORDER BY " . implode(', ', $orders_arr) . '
LIMIT ' . $offset . ', ' . $limit;
$stmt = $con->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt = $stmt->execute();
$result = $stmt->fetchAll();
//all count
$sql = "SELECT count(*) as count FROM news n WHERE n.locale = :locale";
$stmt = $con->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt = $stmt->execute();
$count = $stmt->fetchOne();
$online = $offline = $scheduled = 0;
if ($count) {
$time = new \DateTime('now');
$now = $time->format('Y-m-d 00:00:00');
//online
$sql = "SELECT count(*) as count FROM news n WHERE n.is_active = 1 AND DATE(n.pub_date) <= :now AND n.locale = :locale";
$stmt = $con->prepare($sql);
$stmt->bindValue('now', $now);
$stmt->bindValue('locale', $locale);
$stmt = $stmt->execute();
$online = $stmt->fetchOne();
//scheduled
$sql = "SELECT count(*) as count FROM news n WHERE n.is_active = 1 AND DATE(n.pub_date) > :now AND n.locale = :locale";
$stmt = $con->prepare($sql);
$stmt->bindValue('now', $now);
$stmt->bindValue('locale', $locale);
$stmt = $stmt->execute();
$scheduled = $stmt->fetchOne();
//offline
$sql = "SELECT count(*) as count FROM news n WHERE n.is_active = 0 AND n.locale = :locale";
$stmt = $con->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt = $stmt->execute();
$offline = $stmt->fetchOne();
}
if ($result) {
return array(
'result' => $result,
'count' => $count,
'online' => $online,
'offline' => $offline,
'scheduled' => $scheduled
);
}
return false;
}
/**
* @param $locale
* @param $publishDate
* @return bool
*/
public function getAllNewsItemsForMailing($locale, $publishDate){
$entityManager = $this->getEntityManager();
$connection = $entityManager->getConnection();
$sql = "SELECT id, pub_date, title, in_newsletter, newsletter_sended, slug, in_kabinetsmail FROM news n WHERE n.locale = :locale AND DATE(n.pub_date) > :publishdate AND n.newsletter_sended IS NULL AND n.is_active = 1 ORDER BY n.pub_date DESC ";
$stmt = $connection->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt->bindValue('publishdate', $publishDate);
$stmt = $stmt->execute();
$result = $stmt->fetchAll();
if($result){
return $result;
}
return false;
}
/**
* @param $locale
* @param $publishDate
* @return bool
*/
public function getAllNewsItemsForKabinetsMailing($locale, $publishDate){
$entityManager = $this->getEntityManager();
$connection = $entityManager->getConnection();
$sql = "SELECT id, pub_date, title, in_newsletter, newsletter_sended, slug, in_kabinetsmail FROM news n WHERE n.locale = :locale AND DATE(n.pub_date) > :publishdate AND n.is_active = 1 ORDER BY n.pub_date DESC ";
$stmt = $connection->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt->bindValue('publishdate', $publishDate);
$stmt = $stmt->execute();
$result = $stmt->fetchAll();
if($result){
return $result;
}
return false;
}
/**
* @param $locale
* @param $publishDate
* @return bool
*/
public function getAllNewsItemsForNewsLetter($locale, $publishDate){
$entityManager = $this->getEntityManager();
$connection = $entityManager->getConnection();
$time = new \DateTime('now');
$now = $time->format('Y-m-d 00:00:00');
$sql = "SELECT id, pub_date, title, text, introduction, pub_date as publicationDate, in_newsletter, slug FROM news n WHERE n.locale = :locale AND DATE(n.pub_date) > :publishdate AND n.in_newsletter = TRUE AND n.newsletter_sended IS NULL AND n.is_active AND DATE(n.pub_date) <= :now ORDER BY id DESC";
$stmt = $connection->prepare($sql);
$stmt->bindValue('locale', $locale);
$stmt->bindValue('publishdate', $publishDate);
$stmt->bindValue('now', $now);
$stmt = $stmt->execute();
$result = $stmt->fetchAll();
if($result) {
return $result;
}
return false;
}
/**
* @param $locale
* @param $publishDate
* @return bool
*/
public function getAllNewsItemsForKabinet($locale){
$news = $this->createQueryBuilder('n')
->select('n')
->where('n.locale = :locale')
->andWhere('n.isActive = true')
->andWhere('n.inKabinetsMail = true')
->andWhere('n.publicationDate <= :now')
->setParameter('locale', $locale)
->setParameter('now', new \DateTime())
->orderBy('n.id','DESC')
->getQuery()
->getResult();
if($news) {
return $news;
}
return false;
}
public function updateNewsSentWithNewsLetter($locale){
$update = $this->createQueryBuilder('n')
->update()
->set('n.newsletterSended', ':now')
->set('n.inNewsletter', 0)
->where('n.locale = :locale AND n.inNewsletter = 1 AND n.isActive = 1 AND n.publicationDate <= :now')
->setParameter('locale', $locale)
->setParameter('now', new \DateTime())
->getQuery()
->execute();
}
public function getAllHomepageNews($locale)
{
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$query = $this->getEntityManager()
->createQuery(
'SELECT n FROM
NotarisAdminBundle:News n
WHERE n.locale = :locale
AND n.home = true
AND n.isActive = true
AND n.publicationDate <= :now
ORDER BY n.spotlight DESC, n.publicationDate DESC, n.id DESC
'
)
->setParameter('locale', $locale)
->setParameter('now', $now)
->setMaxResults(3);
$news = $query->getResult();
if ($news) {
return $news;
}
}
public function getAllNews($locale, $maxResults = 20)
{
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$query = $this->getEntityManager()
->createQuery(
'SELECT n FROM
NotarisAdminBundle:News n
WHERE n.locale = :locale
AND n.isActive = true
AND n.publicationDate <= :now
ORDER BY n.spotlight DESC, n.publicationDate DESC, n.id DESC
'
)
->setParameter('locale', $locale)
->setParameter('now', $now);
if($maxResults){
$query->setMaxResults(20);
}
$news = $query->getResult();
if ($news) {
return $news;
}
}
public function getNewsGeneralPublic($locale, $start, $length, $order)
{
if($order == 'asc'){
$order = 'asc';
}else{
$order = 'desc';
}
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$fiveYearsAgo = new \DateTime('-5 years');
$fiveYearsAgo->setTime(0,0,0);
$fiveYearsAgo = $fiveYearsAgo->format('Y-m-d 00:00:00');
$news = $this->createQueryBuilder('n')
->select('n')
->where('n.locale = :locale')
->andWhere('n.isActive = true')
->andWhere('n.publicationDate <= :now')
->andWhere('n.created >= :fiveYearsAgo')
->orderBy('n.publicationDate', $order)
->addOrderBy('n.id', $order)
->setParameter('locale', $locale)
->setParameter('now', $now)
->setParameter('fiveYearsAgo', $fiveYearsAgo)
->setFirstResult($start)
->setMaxResults($length)
->getQuery()
->getResult();
//$news = $query->getResult();
if ($news) {
return $news;
}
}
public function getNewsGeneralPublicCount($locale)
{
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$fiveYearsAgo = new \DateTime('-5 years');
$fiveYearsAgo->setTime(0,0,0);
$fiveYearsAgo = $fiveYearsAgo->format('Y-m-d 00:00:00');
$query = $this->getEntityManager()
->createQuery(
'SELECT n FROM
NotarisAdminBundle:News n
WHERE n.locale = :locale
AND n.isActive = true
AND n.publicationDate <= :now
AND n.created >= :fiveYearsAgo
ORDER BY n.publicationDate DESC
'
)
->setParameter('locale', $locale)
->setParameter('now', $now)
->setParameter('fiveYearsAgo', $fiveYearsAgo);
$news = $query->getResult();
if ($news) {
return count($news);
}
}
public function getNewsItem($locale, $slug)
{
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$query = $this->getEntityManager()
->createQuery(
'SELECT n FROM
NotarisAdminBundle:News n
WHERE n.locale = :locale
AND n.slug = :slug
AND n.isActive = true
AND n.publicationDate <= :now
ORDER BY n.publicationDate DESC
'
)
->setParameter('locale', $locale)
->setParameter('slug', $slug)
->setParameter('now', $now);
$news = $query->getResult();
if ($news) {
return array_shift($news);
}
}
public function getNewsSubsitePublic($locale, $start, $length, $order, $subsite)
{
if($order == 'asc'){
$order = 'asc';
}else{
$order = 'desc';
}
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$news = $this->createQueryBuilder('n')
->select('n')
->innerJoin('n.subsites','ns', 'WITH', 'ns.id = :subsite')
->where('n.locale = :locale')
->andWhere('n.isActive = true')
->andWhere('n.publicationDate <= :now')
->orderBy('n.publicationDate', $order)
->addOrderBy('n.id', $order)
->setParameter('locale', $locale)
->setParameter('now', $now)
->setParameter('subsite', $subsite->getId() )
->setFirstResult($start)
->setMaxResults($length)
->getQuery()
->getResult();
//$news = $query->getResult();
if ($news) {
return $news;
}
}
public function getNewsSubsitePublicCount($locale, $subsite)
{
$now = new \DateTime('now');
$now->setTime(0,0,0);
$now = $now->format('Y-m-d 00:00:00');
$news = $this->createQueryBuilder('n')
->select('n')
->innerJoin('n.subsites','ns', 'WITH', 'ns.id = :subsite')
->where('n.locale = :locale')
->andWhere('n.isActive = true')
->andWhere('n.publicationDate <= :now')
->setParameter('locale', $locale)
->setParameter('now', $now)
->setParameter('subsite', $subsite->getId() )
->getQuery()
->getResult();
//$news = $query->getResult();
if ($news) {
return count($news);
}
}
public function getNewsFromSubsite($subsite)
{
$news = $this->createQueryBuilder('n')
->select('n')
->innerJoin('n.subsites','ns', 'WITH', 'ns.id = :subsite')
->setParameter('subsite', $subsite->getId() )
->getQuery()
->getResult();
//$news = $query->getResult();
if ($news) {
return $news;
}
}
public function search($keyword, $locale='nl') {
$searchResults = $this->createQueryBuilder('n')
->addSelect("MATCH_AGAINST (n.title, n.subtitle, n.introduction, n.source, n.text, :address 'IN BOOLEAN MODE') as score")
->where('n.locale = :locale AND n.publicationDate <= :now AND n.isActive = 1')
->andWhere("MATCH_AGAINST(n.title, n.subtitle, n.introduction, n.source, n.text, :address 'IN BOOLEAN MODE') > 0.2")
->setParameter('address', $keyword)
->setParameter('locale', $locale)
->setParameter('now', new \DateTime('now'))
->orderBy('score', 'desc')
->getQuery()
->getResult();
if($searchResults){
return $searchResults;
}
return false;
}
public function getAllIzimiNews($page, $perPage, $locale = null, $minUpdateDate = null, $maxUpdateDate = null, $sortBy = null, $sortDirection = null, $category = null)
{
$news = $this->createQueryBuilder('n');
if (!empty($category)) {
$news = $news->leftJoin('n.categories', 'c');
}
$news = $news->where('n.isIzimi = 1 AND n.isActive = 1');
if (!empty($locale)) {
$news = $news->andWhere('n.locale = :locale');
$news = $news->setParameter('locale', $locale);
}
if (!empty($minUpdateDate)) {
$news = $news->andWhere('n.updated >= :minUpdateDate');
$news = $news->setParameter('minUpdateDate', $minUpdateDate);
}
if (!empty($maxUpdateDate)) {
$news = $news->andWhere('n.updated <= :maxUpdateDate');
$news = $news->setParameter('maxUpdateDate', $maxUpdateDate);
}
if (!empty($category)) {
$news = $news->andWhere('c.id = :category');
$news = $news->setParameter('category', $category);
}
if (!empty($sortBy)) {
$order = $sortDirection === 'desc' ? 'desc' : 'asc';
if ($sortBy === 'created_datetime') {
$news = $news->orderBy('n.created', $order);
}
if ($sortBy === 'updated_datetime') {
$news = $news->orderBy('n.updated', $order);
}
if ($sortBy === 'publication_date') {
$news = $news->orderBy('n.publicationDate', $order);
}
}
$newsTotalResults = $news->getQuery()
->getResult();
$newsTotalCount = empty($newsTotalResults) ? 0 : count($newsTotalResults);
if (empty($perPage)) {
$news = $newsTotalResults;
} else {
$news = $news->setFirstResult($perPage * ($page - 1))
->setMaxResults($perPage)
->getQuery()
->getResult();
}
if(!empty($news)){
return [
'items' => $news,
'totalCount' => $newsTotalCount,
];
}
return [
'items' => [],
'totalCount' => $newsTotalCount,
];
}
}