src/Controller/Seo/SEOTasksController.php line 247

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Seo;
  3. use App\Entity\Seo\SEOTasks;
  4. use App\Entity\Seo\GPlaces;
  5. use App\Entity\Seo\IntelQueue;
  6. use App\Entity\Seo\CompetitorPivot;
  7. use App\Entity\Seo\Locations;
  8. use App\Entity\Fp\FPTasks;
  9. use App\Form\SEOTasksType;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. use Psr\Log\LoggerInterface;
  17. use App\Service\D4SeoService;
  18. use App\Service\D4SeoReturns;
  19. use App\Service\CommonService;
  20. use App\Repository\Seo\FPTasksRepository;
  21. class SEOTasksController extends AbstractController
  22. {
  23.     public $logger;
  24.     public $d4seo;
  25.     public $d4return;
  26.     public $seoinfoLogger;
  27.     protected $d4SeoService;
  28.     protected $d4SeoReturns;
  29.     function __construct(CommonService $commonServiceD4SeoService $d4SeoServiceD4SeoReturns $d4SeoReturnsLoggerInterface $logger,  LoggerInterface $seoinfoLoggerEntityManagerInterface $em) {
  30.         if(!strstr($_SERVER['REQUEST_URI'], '/d4return/') && !strstr($_SERVER['REQUEST_URI'], '/d4sheet/')) {
  31.             $custInfo $commonService->custInfo();
  32.             $this->counts $custInfo['counts'];
  33.             $this->userinfo $custInfo['info'];
  34.         } 
  35.         $this->seologger $seoinfoLogger;
  36.         // FOR ERROR and CRITICAL 
  37.         // $this->seologger->error("COME INFORMATION HERE - ".__FUNCTION__." ".__LINE__);
  38.         // FOR FLOW INFORMAITON
  39.         // $this->seologger->info("WHAT WERE DOING - ".__FUNCTION__."->businessDataStoreG"); 
  40.         $this->logger $logger;
  41.         $this->d4seo $d4SeoService;
  42.         $this->d4return $d4SeoReturns;
  43.         $this->entityManager $em;
  44.     }
  45.     /**
  46.      * @Route("/seotasks", name="seotasks")
  47.      */
  48.     public function index(): Response
  49.     {
  50.         $content json_decode($this->d4seo->getUserInfo());
  51.         $apinfo $content->tasks[0]->result[0];
  52.         return $this->render('seo_tasks/seotasksmain.html.twig', [
  53.             'page_name' => 'SEO Tasks',
  54.             'breadcrumbs' => array('SEO Tasks' => '/seotasks''API Info' => '' ),
  55.             'user' => $this->userinfo,
  56.             'counts' => $this->counts,
  57.             'results' => $apinfo,
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/seotasks/businessdata/new", name="businessdata_new", methods={"GET","POST"})
  62.      */
  63.     public function bizdatanew(Request $request): Response
  64.     {
  65.         $seoTask = new SEOTasks();
  66.         $form $this->createForm(SEOTasksType::class, $seoTask);
  67.         $form->handleRequest($request);
  68.         if ($form->isSubmitted() && $form->isValid()) {
  69.             $request Request::createFromGlobals();
  70.             $content $request->request->all();
  71.             $params = (object) $content['seo_tasks'];
  72.             $taskinfo = (object) array(
  73.                 'language_code' => $params->language_code,
  74.                 'location_code' => $params->location_code,
  75.                 'keyword' => trim($params->keyword),
  76.                 'purpose' => $params->purpose,
  77.                 'device' => $params->device,
  78.                 'se_type' => 'gmbusiness',
  79.                 'priority' => 2,
  80.             );
  81.             $result $this->d4seo->businessData($taskinfo);
  82.             $mess $result->status;
  83.             return $this->redirectToRoute('business_data', array('message' => $mess));
  84.         }
  85.         return $this->render('seo_tasks/newgmb.html.twig', [
  86.             'seo_tasks' => $seoTask,
  87.             'form' => $form->createView(),
  88.             'page_name' => 'New Task GMB Info',
  89.             'breadcrumbs' => array('SEO Tasks' => '/seotasks''GMB Info' => '/seotasks/businessdata/''New Task' => '' ),
  90.             'user' => $this->userinfo,
  91.             'counts' => $this->counts,
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/seotasks/businessmaps/new", name="businessmaps_new", methods={"GET","POST"})
  96.      */
  97.     public function bizmapsnew(Request $request): Response  
  98.     {
  99.         $seoTask = new SEOTasks();
  100.         $form $this->createForm(SEOTasksType::class, $seoTask);
  101.         $form->handleRequest($request);
  102.         if ($form->isSubmitted() && $form->isValid()) {
  103.             $request Request::createFromGlobals();
  104.             $content $request->request->all();
  105.             $params = (object) $content['seo_tasks'];
  106.             $taskinfo = (object) array(
  107.                 'language_code' => $params->language_code,
  108.                 'location_code' => $params->location_code,
  109.                 'keyword' => trim($params->keyword),
  110.                 'purpose' => $params->purpose,
  111.                 'device' => $params->device,
  112.                 'se_type' => 'maps',
  113.                 'priority' => 2,
  114.             );
  115.             $result $this->d4seo->businessMaps($taskinfo);
  116.             $mess $result->status;
  117.             return $this->redirectToRoute('business_maps', array('message' => $mess));
  118.         }
  119.         return $this->render('seo_tasks/newmaps.html.twig', [
  120.             'seo_tasks' => $seoTask,
  121.             'form' => $form->createView(),
  122.             'page_name' => 'New Task GMB Maps',
  123.             'breadcrumbs' => array('SEO Tasks' => '/seotasks''GMB Info' => '/seotasks/businessmaps/''New Task' => '' ),
  124.             'user' => $this->userinfo,
  125.             'counts' => $this->counts,
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/seotasks/businessdata", name="business_data")
  130.      * This is simply how to create a direct GMB task request. Returns business and several competitors
  131.      */
  132.     public function businessData(Request $request): Response
  133.     {
  134.         //$request = Request::createFromGlobals();
  135.         if($request->get('message')) {
  136.             $mess $request->get('message');
  137.         } else { 
  138.             $mess ''
  139.         }
  140.         return $this->render('seo_tasks/bizdata.html.twig', [
  141.             'page_name' => 'GMB Info Tasks',
  142.             'breadcrumbs' => array('SEO Tasks' => '/seotasks''GMB Data' => '' ),
  143.             'user' => $this->userinfo,
  144.             'counts' => $this->counts,
  145.             'message' => $mess,
  146.         ]);
  147.     }
  148.     /**
  149.      * @Route("/seotasks/businessmaps", name="business_maps")
  150.      * This is simply how to create a direct GMB task request. Returns business and several competitors
  151.      */
  152.     public function businessMaps(Request $request): Response
  153.     {
  154.         /*$postinfo = (object) array(
  155.             'language_code' => 'en',
  156.             'location_code' => '1025622',
  157.             'keyword' => 'plumbers',
  158.             'purpose' => 'discovery',
  159.             'device' => 'desktop',
  160.             'priority' => 2,
  161.         );
  162.         if(!$this->checkThirty($postinfo->keyword, $postinfo->location_code, 'maps', 'sent')) {
  163.             $taskinfo = $this->d4seo->businessMaps($postinfo);
  164.         } else {
  165.             $postinfo->status = "too soon to send";
  166.             $taskinfo = $postinfo;
  167.         }*/
  168.         if($request->get('message')) {
  169.             $mess $request->get('message');
  170.         } else { 
  171.             $mess ''
  172.         }
  173.         return $this->render('seo_tasks/bizmaps.html.twig', [
  174.             'page_name' => 'Google Maps Tasks',
  175.             'breadcrumbs' => array('SEO Tasks' => '/seotasks''Maps Data' => '' ),
  176.             'user' => $this->userinfo,
  177.             'counts' => $this->counts,
  178.             'message' => $mess,
  179.         ]);
  180.     }
  181.     /**
  182.      * @Route("/d4sheet/{api}/{se}/{function}/{report}/{d4taskid}/", name="seotasks_sheet", methods={"GET","POST"})
  183.      * Temporarily not in use, ignore errors.
  184.      */
  185.     public function task4sheet(string $apistring $sestring $functionstring $report,  string $d4taskid): Response
  186.     {
  187.         $thispost file_get_contents('php://input');
  188.         $this->seologger->info("Sheet Returned Result ID: ".$d4taskid." - ".$api." / ".$se." / ".$function." - ".$thispost['status_code']." ".$thispost['status_message']." ");
  189.         //dd($thispost);
  190.         
  191.         $newtaskid $this->d4seo->getUUID();
  192.         $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid'status' => 'returned'));
  193.         if (!$task) {
  194.             $taskinfo->purpose "discovery";
  195.             $taskinfo->task_id $newtaskid;
  196.             $taskinfo->d4id $d4taskid;
  197.             
  198.         }
  199.            
  200.         if($postinfo->status == 'sent') {
  201.             $this->d4seo->storeTask($taskinfo);
  202.         }
  203.         $postinfo->task_id $task_id;
  204.         $taskback = (object) json_decode($content);
  205.         $taskback $taskback->tasks[0];
  206.         $postinfo->d4id $taskback->id;
  207.         $postinfo->api $taskback->data->api;
  208.         $postinfo->function $taskback->data->function;
  209.         $postinfo->se $taskback->data->se;
  210.         $postinfo->se_type 'gmbusiness';
  211.         $postinfo->device $taskback->data->device;
  212.         $postinfo->os $taskback->data->os;
  213.         if ($taskback->status_message == 'Task Created.') {
  214.             $postinfo->status 'sent';
  215.         } else {
  216.             $postinfo->status 'failed';
  217.         }
  218.         $postinfo->created date('Y-m-d H:i:s');
  219.     }
  220.     /**
  221.      * @Route("/d4return/{purpose}/{api}/{se}/{function}/{task_id}/", name="seotasks_return", methods={"GET","POST"})
  222.      */
  223.     public function taskreturn(string $purposestring $apistring $sestring $functionstring $task_id): Response
  224.     {
  225.         $thispost file_get_contents('php://input');
  226.         $em $this->getDoctrine()->getManager();
  227.         $seotask $em->getRepository(SEOTasks::class)->findOneBy(array('task_id' => $task_id));
  228.         if (!$seotask) {
  229.             throw $this->createNotFoundException("Task Not Found With ID: {$task_id} - ".__FUNCTION__." ".__LINE__);
  230.         } else {
  231.             $d4taskid $seotask->getD4id();
  232.             $purpose $seotask->getPurpose();
  233.         }
  234.         
  235.         if (!empty($thispost)) {
  236.             $postback json_decode(gzdecode($thispost), true);
  237.             if (isset($postback['status_code'])) {
  238.                 $this->seologger->info("D4API Returned Result ID: ".$purpose."/".$d4taskid."/".$task_id." - ".$api." / ".$se." / ".$function." - ".$postback['status_code']." ".$postback['status_message']." ");
  239.             }
  240.             $d4result $postback;  
  241.         } else {
  242.             // This is a MANUAL PULL. In other words this is check for D4Seo errors logged. It will fix lost returns.
  243.             if($function == 'my_business_info') {
  244.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/'.$function.'/task_get/'.$d4taskid);
  245.             } elseif($function == 'local_pack') {
  246.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/'.$function.'/task_get/regular/'.$d4taskid);
  247.             } elseif($function == 'maps') {
  248.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/'.$function.'/task_get/advanced/'.$d4taskid);
  249.             } elseif($function == 'reviews') {
  250.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/'.$function.'/task_get/'.$d4taskid);
  251.             } elseif($function == 'backlinkchk') {
  252.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/organic/task_get/regular/'.$d4taskid);
  253.             } else {
  254.                 $thispost $this->d4seo->getFetchTask('/'.$api.'/'.$se.'/'.$function.'/task_get/regular/'.$d4taskid);
  255.             }
  256.             
  257.             if (empty($postback)) {
  258.                 $this->seologger->info("Manual Fetch : ".$d4taskid."/".$task_id." - ".__FUNCTION__." ".__LINE__);
  259.             }
  260.             $d4result json_decode($thisposttrue);
  261.         }
  262.         if(isset($d4result['tasks'][0]['result'])) {
  263.             $d4result $d4result['tasks'][0]['result'];
  264.         } else {
  265.             $em $this->getDoctrine()->getManager();
  266.             $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid));
  267.             $task->setStatus('not found');
  268.             $em->persist($task);
  269.             $em->flush();
  270.             if($purpose == 'footprint') {
  271.                 $fptask $em->getRepository(FPTasks::class)->findOneBy(array('uuid' => $task_id));
  272.                 $fptask->setStatus('not found');
  273.                 $em->persist($fptask);
  274.                 $em->flush();
  275.                 unset($fptask);
  276.             }
  277.             unset($task);
  278.             $this->seologger->error("Returning Task : ".$d4taskid."/".$task_id." - Returned no results- ".__FUNCTION__." ".__LINE__);
  279.             return new Response();
  280.         }
  281.         
  282.         if (isset($d4result[0]['items_count']) && ($function != 'backlinkchk') ) {
  283.             if (== $d4result[0]['items_count']) {
  284.                 $em $this->getDoctrine()->getManager();
  285.                 $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid));
  286.                 $task->setStatus('not found');
  287.                 $em->persist($task);
  288.                 $em->flush();
  289.                 if($purpose == 'footprint') {
  290.                     $fptask $em->getRepository(FPTasks::class)->findOneBy(array('uuid' => $task_id));
  291.                     $fptask->setStatus('not found');
  292.                     $em->persist($fptask);
  293.                     $em->flush();
  294.                     unset($fptask);
  295.                 }                
  296.                 unset($task);
  297.                 $this->seologger->error("Returning Task : ".$d4taskid."/".$task_id." - Returned no results - ".__FUNCTION__." ".__LINE__);
  298.                 return new Response();
  299.             }
  300.         }
  301.         
  302.         if('business_data' == $api) {
  303.             if('google' == $se) {
  304.                 if('my_business_info' == $function
  305.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to businessDataStoreG - ".__FUNCTION__." ".__LINE__);
  306.                   $this->businessDataStoreG($d4result$d4taskid$purpose); }
  307.                 elseif('my_business_updates' == $function
  308.                 { return new Response(); }
  309.                 elseif('reviews' == $function
  310.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningReviews - ".__FUNCTION__." ".__LINE__);
  311.                 $this->d4return->returningReviews($d4result$d4taskid$purpose'google'); }
  312.             } elseif('yelp' == $se) {
  313.                 if('reviews' == $function)
  314.                 { return new Response(); }
  315.             }
  316.         } elseif('serp' == $api) {
  317.             if('google' == $se) {
  318.                 if('organic' == $function)
  319.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningOrganics - ".__FUNCTION__." ".__LINE__);
  320.                   $this->d4return->returningOrganics($d4result$d4taskid$purpose'google'); }
  321.                 elseif('maps' == $function
  322.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningMapSerp - ".__FUNCTION__." ".__LINE__);
  323.                 $this->d4return->returningMapSerp($d4result$d4taskid$purpose'google'); }
  324.                 elseif('backlinkchk' == $function
  325.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningBacklinkChk - ".__FUNCTION__." ".__LINE__);
  326.                 $this->d4return->returningBacklinkChk($d4result$d4taskid$purpose'google'); }
  327.                 elseif('news' == $function
  328.                 { return ''; }
  329.                 elseif('images' == $function
  330.                 { return ''; } 
  331.             } elseif('bing' == $se) {
  332.                 if('organic' == $function)
  333.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningOrganics - ".__FUNCTION__." ".__LINE__);
  334.                 $this->d4return->returningOrganics($d4result$d4taskid$purpose'bing'); }
  335.                 elseif('local_pack' == $function
  336.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningMapSerp - ".__FUNCTION__." ".__LINE__);
  337.                 $this->d4return->returningMapSerp($d4result$d4taskid$purpose'bing'); }
  338.                 elseif('backlinkchk' == $function
  339.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningBacklinkChk - ".__FUNCTION__." ".__LINE__);
  340.                 $this->d4return->returningBacklinkChk($d4result$d4taskid$purpose'bing'); }
  341.             } elseif('yahoo' == $se) {
  342.                 if('organic' == $function)
  343.                 { $this->seologger->info("Transferring {$task_id} for {$purpose} to returningOrganics - ".__FUNCTION__." ".__LINE__);
  344.                 $this->d4return->returningOrganics($d4result$d4taskid$purpose'yahoo');  }
  345.             }  
  346.         } elseif('reviews' == $api) {
  347.             if('google' == $se
  348.                 { return ''; }
  349.             elseif('yelp' == $se
  350.                 { return ''; }
  351.         } elseif('keywords_data' == $api) {
  352.             if('google' == $se) {
  353.                 if('search_volume' == $function)
  354.                 { $this->d4return->returningVolumes($d4result$d4taskid$purpose'google'); }
  355.                 elseif('keywords_for_site' == $function)
  356.                 { return ''; }
  357.                 elseif('keywords_for_keywords' == $function)
  358.                 { return ''; }
  359.                 elseif('keywords_for_category' == $function)
  360.                 { return ''; }
  361.                 elseif('ad_traffic_by_keywords' == $function)
  362.                 { return ''; }
  363.                 elseif('ad_traffic_by_platforms' == $function)
  364.                 { return ''; }
  365.             } elseif('google_trends' == $se) {
  366.                 if('explore' == $function)
  367.                 { return ''; }
  368.             } elseif('bing' == $se) {
  369.                 if('search_volume' == $function)
  370.                 { return ''; }
  371.                 elseif('keywords_for_site' == $function)
  372.                 { return ''; }
  373.                 elseif('keywords_for_keywords' == $function)
  374.                 { return ''; }
  375.                 elseif('keywords_for_category' == $function)
  376.                 { return ''; }
  377.                 elseif('keyword_performance' == $function)
  378.                 { return ''; }
  379.             } 
  380.         
  381.         } elseif('merchant' == $api) {
  382.             if('google' == $se) {
  383.                 if('products' == $function)
  384.                 { return ''; }
  385.                 elseif('sellers' == $function)
  386.                 { return ''; }
  387.                 elseif('product_spec' == $function)
  388.                 { return ''; }
  389.             } elseif('amazon' == $se) {
  390.                 if('products' == $function)
  391.                 { return ''; }
  392.                 elseif('asin' == $function)
  393.                 { return ''; }
  394.             }
  395.         } elseif('on_page' == $api) {
  396.             if('ping' == $se) {
  397.                 if('back' == $function)
  398.                 { return ''; }
  399.             }
  400.         }
  401.         return new Response('Success'200, array('Content-Type' => 'text/html'));
  402.     }
  403.     public function businessDataStoreG($d4result$d4taskid$purpose) {
  404.         // This guy will process and store GMB Business and GMB Map Serps
  405.         $taskready explode('-'$d4taskid);
  406.         $itemcount $d4result[0]['items_count'];
  407.         $keyword $d4result[0]['keyword'];
  408.         if($itemcount >= && isset($d4result[0]['items'][0]['cid']) && !empty($d4result[0]['items'][0]['title'])) 
  409.         {
  410.             $folder 'data/gmb/'.substr($taskready[4], 02);
  411.             $filename $folder.'/'.$taskready[4].'-gmbadvanced.json';
  412.             if(!is_dir($_ENV["HOMEDIR"].'/'.$folder)) {
  413.                 mkdir($_ENV["HOMEDIR"].'/'.$folder0766true );
  414.             }
  415.             if(file_exists($_ENV["HOMEDIR"].'/'.$filename)) {
  416.                 $this->seologger->info("Updating {$filename} - ".__FUNCTION__." ".__LINE__); 
  417.             } else {
  418.                 $this->seologger->info("Saving {$filename} - ".__FUNCTION__." ".__LINE__); 
  419.             }
  420.             file_put_contents($_ENV["HOMEDIR"].'/'.$filenamejson_encode($d4result));
  421.             
  422.         } else {
  423.             $this->seologger->error("Task {$d4taskid} had no resulting items - ".__FUNCTION__." ".__LINE__); 
  424.             $em $this->getDoctrine()->getManager();
  425.             $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid));
  426.             if(isset($task)) {
  427.                 $em->remove($task);
  428.                 $this->seologger->info("Removing task {$d4taskid} - ".__FUNCTION__." ".__LINE__); 
  429.                 $em->flush();
  430.             }
  431.             $intel $em->getRepository(IntelQueue::class)->json_like($keyword);
  432.             if(isset($intel)) {
  433.                 $em->remove($intel);
  434.                 $this->seologger->info("Removing intel queue {$keyword} - ".__FUNCTION__." ".__LINE__);
  435.                 $em->flush();
  436.             }
  437.             return;
  438.         }
  439.         $check_url $d4result[0]['check_url'];
  440.         $location_code $d4result[0]['location_code'];
  441.         $this->seologger->info("Keyword '{$keyword}' - ".__FUNCTION__." ".__LINE__);
  442.         foreach($d4result[0]['items'] as $item) {
  443.             $em $this->getDoctrine()->getManager();
  444.             if(isset($item['cid'])  && $item['cid'] >= 1) {
  445.                 $place $em->getRepository(Gplaces::class)->findOneBy(array('cid' => $item['cid']));
  446.             } else {
  447.                 $this->seologger->error("Keyword '{$keyword}' for task {$d4taskid} had no CID - ".__FUNCTION__." ".__LINE__); 
  448.                 $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid));
  449.                 if(isset($task)) {
  450.                     $em->remove($task);
  451.                     $this->seologger->info("Removing task {$d4taskid} - ".__FUNCTION__." ".__LINE__); 
  452.                     $em->flush();
  453.                 }
  454.                 $intel $em->getRepository(IntelQueue::class)->json_like($keyword);
  455.                 if(isset($intel)) {
  456.                     $em->remove($intel);
  457.                     $this->seologger->info("Removing intel queue {$keyword} - ".__FUNCTION__." ".__LINE__); 
  458.                     $em->flush();
  459.                 }
  460.                 break;
  461.             }
  462.             
  463.             if (!$place && isset($item['cid'])) {
  464.                 $place = new Gplaces();
  465.                 $recheck 0;
  466.                 
  467.                 $this->seologger->info("Adding NEW Place '{$item['title']}{$item['cid']} - ".__FUNCTION__." ".__LINE__);
  468.                 if(isset($item['latitude']) && isset($item['latitude'])) {
  469.                     $place->setLat($item['latitude']);
  470.                     $place->setLng($item['longitude']);
  471.                 } else {
  472.                     $recheck 1;
  473.                 }
  474.                 $place->setCid($item['cid']);
  475.                 $place->setFeatureID($item['feature_id']);
  476.                 $place->setPlaceId($item['place_id']);
  477.                 if(isset($item['address'])) {
  478.                     $address_all file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAbTq8g-XSRuqwjMltfCKf1BI-PziP_ym0&address='.urlencode($item['address']));
  479.                     if(isset($address_all)) {
  480.                         $rawaddy json_decode($address_allTRUE);
  481.                         if($rawaddy['status'] != 'OVER_QUERY_LIMIT' && $rawaddy['status'] != 'ZERO_RESULTS') {
  482.                             $rawaddy $rawaddy['results'][0];
  483.                         }
  484.                     }
  485.                 }
  486.                 if($recheck == && isset($item['address']) && isset($rawaddy['results'][0])) {
  487.                     if(isset($rawaddy['geometry']['location']['lat']) && isset($rawaddy['geometry']['location']['lng'])) {
  488.                         $place->setLat($rawaddy['geometry']['location']['lat']);
  489.                         $place->setLng($rawaddy['geometry']['location']['lng']);
  490.                     }
  491.                 }
  492.             } 
  493.             if(isset($rawaddy['results'][0])) {
  494.                 $place->setAddressAll($rawaddy);
  495.                 $bestlocation $this->getLocCode($rawaddy,$location_code);
  496.             } else {
  497.                 $rawaddy $place->getAddressAll();
  498.                 $bestlocation $location_code;
  499.             }
  500.             $place->setKeyword($keyword);
  501.             if(!empty($item['address'])) {
  502.                 $place->setAddress($item['address']);
  503.             }
  504.             $place->setLocation($bestlocation);
  505.             if(isset($item['additional_categories'])) { 
  506.                 $place->setAddcats($item['additional_categories']);
  507.             } 
  508.             
  509.             $place->setTitle($item['title']);
  510.             $place->setPrimarycat($item['category']);
  511.             if(!empty($item['phone'])) {
  512.                 $place->setPhone($item['phone']); 
  513.             }
  514.             $place->setWorktime($item['work_time']);
  515.             if($item['url']) {
  516.                 $place->setUrl($item['url']);
  517.                 $place->setDomain($item['domain']);
  518.             }
  519.             $place->setMainImage($item['main_image']);
  520.             $place->setSnippet($item['snippet']);
  521.             if(isset($item['rating'])) {
  522.                 $place->setRatingType($item['rating']['rating_type']);
  523.                 $place->setRatingValue($item['rating']['value']);
  524.                 $place->setVotesCount($item['rating']['votes_count']);
  525.                 if(isset($item['rating']['rating_max'])) {
  526.                     $place->setRatingMax($item['rating']['rating_max']);
  527.                 }
  528.             }
  529.             $check_url "https://www.google.com/maps?cid=".$item['cid']."&hl=en&gl=US";
  530.             $place->setCheckUrl($check_url);
  531.             if(isset($item['description'])) {
  532.                 $place->setDescription($item['description']);
  533.             }
  534.             if(isset($item['logo'])) {
  535.                 $place->setLogo($item['logo']);
  536.             }
  537.             if(isset($item['attributes'])) {
  538.                 $place->setAttributes($item['attributes']);
  539.             }
  540.             if(isset($item['people_also_search'])) {
  541.                 $place->setCompetitors($item['people_also_search']);
  542.             }
  543.             $place->setModified(\DateTime::createFromFormat('Y-m-d H:i:s'date('Y-m-d H:i:s')));
  544.             $em->persist($place);
  545.             $em->flush();
  546.             $task $em->getRepository(SEOTasks::class)->findOneBy(array('d4id' => $d4taskid));
  547.             
  548.             $task->setStatus('returned');
  549.             $em->persist($task);
  550.             $em->flush();
  551.             if(isset($item['people_also_search'])) {
  552.                 $complist = array();
  553.                 foreach($item['people_also_search'] as $competitor)  {
  554.                     // First check if we already know about the listing via CID. If not we add the listing to the IntelQueue 
  555.                     // so tasks can be created over time. Not right away for sure. Reminder: $130 reasons why not, lol.
  556.                     $em $this->getDoctrine()->getManager();
  557.                     if(!empty($competitor['title']) && !empty($competitor['cid'])) {
  558.                         $cid = (string) $competitor['cid'];
  559.                         $cidcheck $em->getRepository(GPlaces::class)->doesGPlaceExist($cid);
  560.                         if (!$cidcheck) {
  561.                             $more = array(
  562.                                 'language_code' => 'en',
  563.                                 'se' => 'google',
  564.                                 'location_code' => $location_code,
  565.                                 'keyword' => $competitor['title'],
  566.                                 'cid' => $competitor['cid'],
  567.                                 'purpose' => $purpose,
  568.                             );
  569.                             $qinfo = (object) array(
  570.                                 'purpose' => $purpose,
  571.                                 'location_code' => $location_code,
  572.                                 'actionapi' => 'business_data',
  573.                                 'function' => 'my_business_info',
  574.                                 'content' =>  $competitor['title'],
  575.                                 'more' => json_encode($more),
  576.                             );
  577.                             $this->seologger->info("Storing intel queue for '{$competitor['title']}{$competitor['cid']} - ".__FUNCTION__." ".__LINE__); 
  578.                             $this->d4seo->storeQueue($qinfo);
  579.                         }
  580.                         array_push($complist$competitor['cid']);
  581.                     }
  582.                 }
  583.                 $cpivot $em->getRepository(CompetitorPivot::class)->findOneBy(array('cid' => $item['cid']));
  584.                 if (!$cpivot) {
  585.                     $cpivot = new CompetitorPivot();
  586.                     $cpivot->setCid($item['cid']);
  587.                     $cpivot->setEngine('google');
  588.                 } else {
  589.                     if(is_array($cpivot->getCompCids())) {
  590.                         $complist array_unique(array_merge($complistjson_decode($cpivot->getCompCids())), SORT_REGULAR);
  591.                     } else {
  592.                         $complist = array();
  593.                     }
  594.                 }
  595.                 $cpivot->setCompCids(json_encode($complist));
  596.                 $em->persist($cpivot);
  597.                 $em->flush();
  598.             }
  599.         }
  600.     }
  601.     private function getLocCode($addyin$current) {
  602.         $key 'United States';
  603.         foreach($addyin['address_components'] as $component) {
  604.             if (in_array('locality'$component['types'])) { 
  605.                 $city $component['short_name'];
  606.             } elseif (in_array('administrative_area_level_1'$component['types'])) { 
  607.                 $state $component['long_name'];
  608.             } elseif (in_array('postal_code'$component['types'])) { 
  609.                 $zipcode $component['short_name'];
  610.             }
  611.         }
  612.         if (isset($city)) {
  613.             $key$city.','; }
  614.         if (isset($state)) {
  615.             $key.= $state.','; }
  616.         $key.='United States';
  617.         $em $this->getDoctrine()->getManager();
  618.         $location $em->getRepository(Locations::class)->findOneBy(array('location_name' => $key));
  619.         if (!$location && $zipcode) {
  620.             $location $em->getRepository(Locations::class)->findOneBy(array('location_name' => $zipcode.','.$state.',United States')); }
  621.         if($location) {
  622.             return $location->getLocationCode();
  623.         } else {
  624.             return $current;
  625.         }
  626.     }
  627. }