<?php
namespace App\Utils;
use Doctrine\ORM\EntityManagerInterface;
use App\Exception\ResourceNotFoundException;
use App\Entity\Customer;
use App\Entity\Site;
use Symfony\Component\HttpFoundation\RequestStack;
class CustomerHelper
{
protected $entityManager;
protected $requestStack;
public function __construct (
EntityManagerInterface $entityManager,
RequestStack $requestStack
) {
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
}
public function getSiteCode() {
$site_code = null;
$request = $this->requestStack->getCurrentRequest();
if($request) {
$session = $request->getSession();
if($session) {
$site_code = $session->get("site");
}
}
return $site_code;
}
public function getCustomerById ($id = 0)
{
$customer = $this->entityManager
->getRepository(Customer::class)
->findOneBy([
"id" => $id
]);
if ($customer) {
return $customer;
}
throw new ResourceNotFoundException ("Customer with Id {$id} - Not Found");
}
public function getCustomersByCategory (
$categoryid = "",
$site = ""
) {
return $this->entityManager
->getRepository(Customer::class)
->getCustomersByCategory($categoryid, $site);
}
public function getActiveCustomers () {
return $this->entityManager
->getRepository(Customer::class)
->getActiveCustomers();
}
public function getCustomersByContentType (
$content_type = ""
) {
$site_code = $this->getSiteCode();
$query = implode(" ", array (
"SELECT customer",
"FROM",
"App\Entity\Customer customer",
"JOIN",
"customer.contents content",
(($site_code) ? implode(" ", array (
"JOIN",
"customer.site cusite",
"JOIN",
"content.site csite",
)) : ""),
"WHERE",
"content.type = {$content_type}",
"AND content.status = 1",
(($site_code) ? implode(" ", array (
"AND",
"cusite.id = {$site_code}",
"AND",
"csite.id = {$site_code}",
)) : ""),
"ORDER BY",
"customer.title ASC",
));
$results = $this->entityManager
->createQuery($query)
->getResult();
return $results;
}
public function getCustomersWithProducts () {
$query = implode(" ", array (
"SELECT customer",
"FROM",
"App\Entity\Customer customer",
"JOIN",
"customer.products product",
"WHERE",
"product.type = 4",
"AND product.status = 1",
"ORDER BY",
"customer.title ASC",
));
$results = $this->entityManager
->createQuery($query)
->getResult();
return $results;
}
public function customerHasEbooks(){
$site_code = $this->getSiteCode();
$results = $this->entityManager
->getRepository(Customer::class)
->customerHasEbooks($site_code);
return $results;
}
public function getDirectoryBySlug($slug) {
$qb = $this->entityManager->createQueryBuilder();
$qb->select('c')
->from('App\Entity\Customer', 'c')
->where('c.slug = :slug')
->setParameter('slug', $slug);
$results = $qb->getQuery()->getResult();
return $results;
}
public function getFacebookHandles() {
$stmt = $this->entityManager->getConnection()->query("SELECT facebook FROM customer WHERE facebook != ''");
$res = $stmt->fetchAll();
$handles = [];
foreach($res as $r) {
$handle = preg_replace(["/\s+/","/@/"], "", $r["facebook"]);
if (filter_var($handle, FILTER_VALIDATE_URL) !== FALSE) {
$handle = parse_url($handle, PHP_URL_PATH);
$handle = explode("/", $handle);
$handle = $handle[1];
}
$handle = strstr($handle, '?', true) ?: $handle;
$handles[] = $handle;
}
sort($handles);
return array_values(array_unique($handles));
}
public function getLinkedinHandles() {
$stmt = $this->entityManager->getConnection()->query("SELECT linkedin FROM customer WHERE linkedin != ''");
$res = $stmt->fetchAll();
$handles = [];
foreach($res as $r) {
$handle = preg_replace(["/\s+/","/@/"], "", $r["linkedin"]);
if (filter_var($handle, FILTER_VALIDATE_URL) !== FALSE) {
$handle = parse_url($handle, PHP_URL_PATH);
if(substr($handle, 0, strlen("/company-beta/")) == "/company-beta/") {
$handle = substr($handle, strlen("/company-beta/") - 1);
}
if(substr($handle, 0, strlen("/company/")) == "/company/") {
$handle = substr($handle, strlen("/company/") - 1);
}
if(substr($handle, 0, strlen("/in/")) == "/in/") {
$handle = substr($handle, strlen("/in/") - 1);
}
$handle = explode("/", $handle);
$handle = $handle[1];
}
$handle = strstr($handle, '?', true) ?: $handle;
$handles[] = $handle;
}
sort($handles);
return array_values(array_unique($handles));
}
public function getTwitterHandles() {
$stmt = $this->entityManager->getConnection()->query("SELECT twitter FROM customer WHERE twitter != ''");
$res = $stmt->fetchAll();
$handles = [];
foreach($res as $r) {
$handle = preg_replace(["/\s+/","/@/"], "", $r["twitter"]);
if (filter_var($handle, FILTER_VALIDATE_URL) !== FALSE) {
$handle = parse_url($handle, PHP_URL_PATH);
$handle = explode("/", $handle);
$handle = $handle[1];
}
$handle = strstr($handle, '?', true) ?: $handle;
$handles[] = $handle;
}
sort($handles);
return array_values(array_unique($handles));
}
public function getCustomerTags() {
$stmt = $this->entityManager->getConnection()->query("SELECT hash_tag FROM customer WHERE hash_tag != ''");
$res = $stmt->fetchAll();
$tags = [];
foreach($res as $r) {
$tags = array_merge($tags, explode(",", preg_replace(["/\s+/","/#/"], "", $r["hash_tag"])));
}
sort($tags);
return array_values(array_unique($tags));
}
}