<?php
namespace App\Frontend\OpenSpaceBundle\Services;
use App\AMZ\CoreBundle\Services\AMZS;
use App\AMZ\InquiryBundle\Entity\Project;
use App\AMZ\PostBundle\Entity\Post;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use function Doctrine\ORM\QueryBuilder;
class OpenSpaceService{
private $em;
private $paginator;
public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator){
$this->em = $em;
$this->paginator = $paginator;
}
public function getSliderForHomepage(){
$qb = $this->em->getRepository('AMZPostBundle:Gallery')
->createQueryBuilder("gallery");
$qb->andWhere(
$qb->expr()->in('gallery.slug',OpenSpace::DEFAULT_HOMEPAGE_SLIDER)
);
$gallery = $qb->getQuery()->getResult();
return $gallery;
}
public function getBannerBySlug($slug){
$qb = $this->em->getRepository("AMZPostBundle:Banner")
->createQueryBuilder('banner');
$qb->andWhere(
$qb->expr()->eq('banner.slug', $qb->expr()->literal($slug))
);
$qb->setFirstResult(0);
$qb->setMaxResults(1);
return $qb->getQuery()->getSingleResult();
}
public function getPageBySlug($slug){
$qb = $this->em->getRepository("AMZPageBundle:Page")
->createQueryBuilder('page');
$qb->join("page.post",'post');
$qb->andWhere(
$qb->expr()->eq("post.slug",$qb->expr()->literal($slug))
);
$qb->setFirstResult(0);
$qb->setMaxResults(1);
return $qb->getQuery()->getSingleResult();
}
public function getCompanyService(){
$qb = $this->em->getRepository("AMZProductBundle:Service")
->createQueryBuilder("service");
return $qb->getQuery()->getResult();
}
public function getTestimonial(){
$qb = $this->em->getRepository("AMZPostBundle:Testimonial")
->createQueryBuilder("testimonial");
return $qb->getQuery()->getResult();
}
public function getProjectCategory(){
$qb = $this->em->getRepository("AMZPostBundle:Category")
->createQueryBuilder("category");
$qb->join('category.parent','parent');
$qb->andWhere(
$qb->expr()->eq('parent.slug', $qb->expr()->literal(AMZS::DEFAULT_COMPANY_PROJECT_CATEGORY_SLUG))
);
$qb->select('category.title','category.thumbnail','category.description','category.slug');
$categories = $qb->getQuery()->getArrayResult();
return $categories;
}
public function getProjectByCategorySlug($slug, $page = 1){
$qb = $this->em->getRepository("AMZProjectBundle:Project")
->createQueryBuilder("project");
$qb->join("project.post",'post');
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('category.slug',$qb->expr()->literal($slug)),
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_COMPANY_PROJECT)
);
$qb->orderBy('post.display',"ASC");
$qb->orderBy('project.id',"DESC");
return $this->paginator->paginate($qb->getQuery(),$page, OpenSpace::DEFAULT_PROJECT_PER_PAGE);
// return $qb->getQuery()->getResult();
}
public function getArticleByCategorySlug($slug, $page = 1){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('category.slug',$qb->expr()->literal($slug)),
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_ARTICLE)
);
$qb->orderBy('post.id',"DESC");
$query = $qb->getQuery();
return $this->paginator->paginate(
$query,$page, OpenSpace::DEFAULT_ARTICLE_PER_PAGE
);
}
public function findProductBySlug($slug){
$qb = $this->em->getRepository("AMZProductBundle:Product")
->createQueryBuilder('product');
$qb->join('product.post','post');
$qb->andWhere(
$qb->expr()->eq('post.slug', $qb->expr()->literal($slug)),
$qb->expr()->eq('post.deleted', AMZS::STATUS_NOT_DELETED)
);
$qb->setFirstResult(0);
$qb->setMaxResults(1);
$qb->select('post.title as title','post.slug as slug', 'post.image as image','post.content as content');
return $qb->getQuery()->getSingleResult();
}
public function getServices(){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('category.slug',$qb->expr()->literal('dich-vu')),
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_SERVICE)
);
$qb->orderBy('post.display',"ASC");
return $qb->getQuery()->getArrayResult();
}
public function getSolutions(){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('category.slug',$qb->expr()->literal(AMZS::DEFAULT_SOLUTION_SLUG)),
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_SOLUTION)
);
$qb->orderBy('post.display',"ASC");
return $qb->getQuery()->getArrayResult();
}
public function getRelativeSolution(Post $post){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('category.slug',$qb->expr()->literal(AMZS::DEFAULT_SOLUTION_SLUG)),
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_SOLUTION)
);
$qb->orderBy('post.display',"ASC");
return $qb->getQuery()->getArrayResult();
}
public function getRelativeServices(Post $post){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_SERVICE)
);
$qb->orderBy('post.display',"ASC");
return $qb->getQuery()->getArrayResult();
}
public function getFAQForHomepage(){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->join("post.category",'category');
$qb->andWhere(
$qb->expr()->eq('post.deleted',AMZS::STATUS_NOT_DELETED),
$qb->expr()->eq('post.type', AMZS::POST_TYPE_FAQ),
$qb->expr()->eq('post.isHot', AMZS::POST_IS_HOT)
);
$qb->orderBy('post.display',"ASC");
return $qb->getQuery()->getArrayResult();
}
public function getCompanyProject(){
$qb = $this->em->getRepository('AMZProjectBundle:Project')
->createQueryBuilder("project");
$qb->join('project.post','post');
$qb->join('post.category','category');
$qb->andWhere(
$qb->expr()->eq('post.type',AMZS::POST_TYPE_COMPANY_PROJECT),
$qb->expr()->eq('category.type',AMZS::POST_TYPE_COMPANY_PROJECT)
);
$qb->orderBy('post.display', 'ASC');
$qb->select('post.title','post.image','post.description','project.client','project.location','post.slug');
return $qb->getQuery()->getArrayResult();
}
public function getProjectDetailBySlug($slug){
$qb = $this->em->getRepository('AMZProjectBundle:Project')
->createQueryBuilder("project");
$qb->join('project.post','post');
$qb->andWhere(
$qb->expr()->eq('post.type',AMZS::POST_TYPE_COMPANY_PROJECT),
$qb->expr()->eq('post.slug', $qb->expr()->literal($slug))
);
$qb->orderBy('post.display', 'ASC');
$qb->setFirstResult(0);
$qb->setMaxResults(1);
// $qb->select('post.title','post.image','post.content','post.description','project.client','project.location','post.slug');
return $qb->getQuery()->getSingleResult();
}
public function getRelativeProjects($slug, $first=0, $max=AMZS::DEFAULT_RELATIVE_POST){
$qb = $this->em->getRepository('AMZProjectBundle:Project')
->createQueryBuilder("project");
$qb->join('project.post','post');
$qb->andWhere(
$qb->expr()->eq('post.type',AMZS::POST_TYPE_COMPANY_PROJECT),
$qb->expr()->neq('post.slug', $qb->expr()->literal($slug))
);
$qb->setFirstResult($first);
$qb->setMaxResults($max);
$qb->select('post.title','post.image','post.description','project.client','project.location','post.slug');
return $qb->getQuery()->getArrayResult();
}
public function validateInquiryData($data){
$name = $data['name'];
$email = $data['email'];
$phone = $data['phone'];
$title = $data['title'];
$content = $data['content'];
$isValid = array();
if(!($this->validateName($name))){
$isValid['name'] = 'check-your-name';
}
if(!($this->validateMail($email))){
$isValid['email'] = 'check-your-email';
}
if(!($this->validateMail($phone))){
$isValid['phone'] = 'check-your-phone';
}
if(!($this->validateTitle($title))){
$isValid['title'] = 'check-your-title';
}
if(!($this->validateContent($content))){
$isValid['content'] = 'check-your-content';
}
return $isValid;
}
private function validateName($txt){
if($this->isEmpty($txt)){
return false;
}
return true;
}
private function validatePhone($txt){
if($this->isEmpty($txt)){
return false;
}
return true;
}
private function validateMail($txt){
if($this->isEmpty($txt)){
return false;
}
return true;
}
private function validateTitle($txt){
if($this->isEmpty($txt)){
return false;
}
return true;
}
private function validateContent($txt){
if($this->isEmpty($txt)){
return false;
}
return true;
}
private function isEmpty($txt){
}
private function isPhone($txt){
}
private function isEmail($txt){
}
public function getArticleBySlug($slug){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder('post');
$qb->andWhere(
$qb->expr()->eq('post.type', AMZS::POST_TYPE_ARTICLE),
$qb->expr()->eq('post.slug',$qb->expr()->literal($slug))
);
$qb->setFirstResult(0);
$qb->setMaxResults(1);
return $qb->getQuery()->getSingleResult();
}
public function getOlderArticle(Post $post,$max=OpenSpace::DEFAULT_MAX_ARTICLE_NUMBER, $start = 0){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->andWhere(
$qb->expr()->eq('post.type', AMZS::POST_TYPE_ARTICLE),
$qb->expr()->lt('post.id', $post->getId())
);
$qb->setFirstResult($start);
$qb->setMaxResults($max);
$qb->orderBy("post.id", "DESC");
return $qb->getQuery()->getResult();
}
public function getLatestArticles($max=OpenSpace::DEFAULT_MAX_ARTICLE_NUMBER, $start = 0){
$qb = $this->em->getRepository("AMZPostBundle:Post")
->createQueryBuilder("post");
$qb->andWhere(
$qb->expr()->eq('post.type', AMZS::POST_TYPE_ARTICLE)
);
$qb->setFirstResult($start);
$qb->setMaxResults($max);
$qb->orderBy("post.id", "DESC");
return $qb->getQuery()->getResult();
}
}