<?php
namespace App\Controller;
use App\Entity\SiteSettings;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\CrmSmsTemplates;
use App\Utils\Paginator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Form\Filters\MemcacheDataFilter;
#[Route('/settings/cache')]
class CacheController extends AbstractController
{
protected $container;
protected $tokenStorage;
protected $memcache;
public function __construct(ContainerInterface $container, TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->container = $container;
$this->memcache = new \Memcached();
$this->memcache->addServer($this->container->getParameter('memcache_host'),$this->container->getParameter('memcache_port'));
}
#[Route('/', defaults: ['page' => '1'], methods: ['GET'], name: 'manage_cache')]
#[Route('/page/{page<[1-9]\d*>}', methods: ['GET'], name: 'manage_cache_index_paginated')]
#[Cache(smaxage: 10)]
public function index(Request $request,int $page,EntityManagerInterface $entityManager): Response
{
$params = $request->query->all();
$filter = [];
$filterForm = $this->createForm(MemcacheDataFilter::class);
if(isset($params['key'])) {
$filterForm['key']->setData($params['key']);
$filter['key'] = $params['key'];
}
if(isset($params['crm']) && !empty($params['crm'])) {
$userCrm = $entityManager->getRepository(\App\Entity\UserCrms::class)->findOneBy(['crm'=>$params['crm']]);
$filterForm['crm']->setData($params['crm']);
if($userCrm){
$filter['crm'] = '_'.$userCrm->getCrmId();
}
}
if(isset($params['user_name']) && !empty($params['user_name'])) {
$user = $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['name'=>$params['user_name']]);
$filterForm['user_name']->setData($params['user_name']);
if($user){
$filter['user_name'] = '_'.$user->getUserId();
}
}
if(isset($params['user_email']) && !empty($params['user_email'])) {
$user = $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['email'=>$params['user_email']]);
$filterForm['user_email']->setData($params['user_email']);
if($user){
$filter['user_email'] = '_'.$user->getUserId();
}
}
if(isset($params['phone_number']) && !empty($params['phone_number'])) {
$userProfiles = $entityManager->getRepository(\App\Entity\UserProfiles::class)->findOneBy(['phone'=>$params['phone_number']]);
$filterForm['phone_number']->setData($params['phone_number']);
if($userProfiles){
$filter['phone_number'] = '_'.$userProfiles->getUserId();
}
}
$cacheArray = $this->memcache->getAllKeys();
$this->memcache->getDelayed($cacheArray, true);
/* echo '<pre>';
print_r($memcache->fetchAll());
exit; */
$page = ($request->query->get('page'))?$request->query->get('page'):1;
$limit = 10;
$offset = $limit * ($page - 1);
$dbcacheArray = array();
foreach($cacheArray as $arrayKey => $arrayVal){
if($arrayVal == 'stored_keys'){
continue;
}
$key_arr = explode(".",$arrayVal);
//$data = array_slice($key_arr, 0, 3);
$data = array_slice($key_arr, 0, 3);
$dbcacheArray[implode("_",$data)] = implode(".",$data);
}
$filteredArray = $dbcacheArray;
$totalCache = array_filter($dbcacheArray);
if(isset($filter['key']) && !empty($filter['key'])){
// Filter the array by a specific string
$filterString = $filter['key']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['crm']) && !empty($filter['crm'])){
// Filter the array by a specific string
$filterString = $filter['crm']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['user_name']) && !empty($filter['user_name'])){
// Filter the array by a specific string
$filterString = $filter['user_name']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['user_email']) && !empty($filter['user_email'])){
// Filter the array by a specific string
$filterString = $filter['user_email']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['phone_number']) && !empty($filter['phone_number'])){
// Filter the array by a specific string
$filterString = $filter['phone_number']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
/* echo '<pre>';
print_r($dbcacheArray);
exit; */
$totalFilterCache = array_filter($filteredArray);
return $this->render('cache/index.html.twig', [
'paginator' => $totalFilterCache,
'filter' => $filter,
'totalCache' => count($totalCache),
'totalFilterCache' => count($totalFilterCache),
'form' => $filterForm->createView(),
]);
}
#[Route('/delete/{id}', name: 'cache_delete', methods: ['POST','GET'])]
public function delete($id, Request $request, EntityManagerInterface $entityManager): Response
{
$params = $request->query->all();
$filter = [];
if(isset($params['key'])) {
$filter['key'] = $params['key'];
}
if(isset($params['crm']) && !empty($params['crm'])) {
$userCrm = $entityManager->getRepository(\App\Entity\UserCrms::class)->findOneBy(['crm'=>$params['crm']]);
if($userCrm){
$filter['crm'] = '_'.$userCrm->getCrmId();
}
}
if(isset($params['user_name']) && !empty($params['user_name'])) {
$user = $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['name'=>$params['user_name']]);
if($user){
$filter['user_name'] = '_'.$user->getUserId();
}
}
if(isset($params['user_email']) && !empty($params['user_email'])) {
$user = $entityManager->getRepository(\App\Entity\User::class)->findOneBy(['email'=>$params['user_email']]);
if($user){
$filter['user_email'] = '_'.$user->getUserId();
}
}
if(isset($params['phone_number']) && !empty($params['phone_number'])) {
$userProfiles = $entityManager->getRepository(\App\Entity\UserProfiles::class)->findOneBy(['phone'=>$params['phone_number']]);
if($userProfiles){
$filter['phone_number'] = '_'.$userProfiles->getUserId();
}
}
if ($request->getMethod() == 'POST') {
$cacheArray = $this->memcache->getAllKeys();
if($id == 'all'){
foreach($cacheArray as $arrayKey => $arrayVal){
$this->memcache->delete($arrayVal);
}
return $this->redirectToRoute('manage_cache', [], Response::HTTP_SEE_OTHER);
}else{
if(count($filter) > 0){
$dbcacheArray = array();
foreach($cacheArray as $arrayKey => $arrayVal){
$key_arr = explode(".",$arrayVal);
//$data = array_slice($key_arr, 0, 3);
$data = array_slice($key_arr, 0, 3);
$dbcacheArray[implode("_",$data)] = implode(".",$data);
}
$filteredArray = $dbcacheArray;
if(isset($filter['key']) && !empty($filter['key'])){
// Filter the array by a specific string
$filterString = $filter['key']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['crm']) && !empty($filter['crm'])){
// Filter the array by a specific string
$filterString = $filter['crm']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['user_name']) && !empty($filter['user_name'])){
// Filter the array by a specific string
$filterString = $filter['user_name']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['user_email']) && !empty($filter['user_email'])){
// Filter the array by a specific string
$filterString = $filter['user_email']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
if(isset($filter['phone_number']) && !empty($filter['phone_number'])){
// Filter the array by a specific string
$filterString = $filter['phone_number']; // Replace this with the string you want to filter by
$filteredArray = array_filter($dbcacheArray, function($value) use ($filterString) {
return strpos($value, $filterString) !== false;
});
}
foreach($filteredArray as $arrayKey => $arrayVal){
$this->memcache->delete($arrayVal);
}
return $this->redirectToRoute('manage_cache', [], Response::HTTP_SEE_OTHER);
}
}
$this->addFlash('error', 'Delete successfully.');
if($request->request->get('redirect_page')){
return $this->redirectToRoute('manage_cache_index_paginated', ['page'=> $request->request->get('redirect_page')], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('cache/_delete_form.html.twig', [
'redirect_page' => $request->query->get('redirect_page'),
'id' => $id,
]);
}
}