<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpClient\HttpClient;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Driver\Connection;
use App\Service\CommonService;
use Psr\Log\LoggerInterface;
use App\Controller\SEOTasksController;
use App\Repository\System\UserRepository;
use App\Repository\Fp\FPTasksRepository;
use App\Repository\Fp\FPRankingsRepository;
use App\Repository\Fp\FPSerpsRepository;
use App\Repository\Seo\LocationsRepository;
use App\Repository\Seo\SEOTasksRepository;
use App\Entity\Fp\FPReports;
use App\Entity\Fp\FPTests;
use App\Entity\Fp\FPTasks;
use App\Entity\Fp\FPRankings;
use App\Entity\Fp\FPSerps;
use App\Entity\Seo\SEOTasks;
use App\Entity\Seo\GPlaces;
use App\Entity\Seo\IntelQueue;
class D4SeoService
{
protected $client;
public $logger;
public $seoinfoLogger;
private $userRepository;
public function __construct(CommonService $commonService, HttpClientInterface $client, LoggerInterface $logger, LoggerInterface $seoinfoLogger,
FPTasksRepository $fpTasksRepository, FPRankingsRepository $fpRankingsRepository, SEOTasksRepository $seoTasksRepository,
FPSerpsRepository $fpSerpsRepository, LocationsRepository $locationsRepository, UserRepository $userRepository, EntityManagerInterface $em)
{
$this->settings = (object) $commonService->getSettings('api');
$this->client = $client;
$this->userRepository = $userRepository;
$this->fpTasksRepository = $fpTasksRepository;
$this->fpRankingsRepository = $fpRankingsRepository;
$this->fpSerpsRepository = $fpSerpsRepository;
$this->locationsRepository = $locationsRepository;
$this->seoTasksRepository = $seoTasksRepository;
$this->seologger = $seoinfoLogger;
// FOR ERROR and CRITICAL
// $this->seologger->error("COME INFORMATION HERE - ".__FUNCTION__." ".__LINE__);
// FOR FLOW INFORMAITON
// $this->seologger->info("WHAT WERE DOING - ".__FUNCTION__."->businessDataStoreG");
$this->logger = $logger;
$this->entityManager = $em;
}
private function connect($method, $url, $postdata = null) {
$conn = $this->client->request($method, "https://api.dataforseo.com/v3".$url, [
'auth_basic' => [$this->settings->MAGICEMAIL, $this->settings->MAGICSTRING],
'headers' => [
'User-Agent' => 'Surfside Web SEO',
'Accept' => 'application/json',
],
'json' => $postdata,
]);
return $conn;
}
public function getFetchTask($path) {
$conn = $this->connect('GET', $path);
$content = $conn->getContent();
return $content;
}
/**
* Gets our user info for testing
*/
public function getUserInfo() {
$conn = $this->connect('GET', '/appendix/user_data');
$content = $conn->getContent();
return $content;
}
/**
* Sets the task to collect GMB Maps information. AKA the list of maps results by topic.
*/
public function businessMaps($postinfo) {
/*
// OUT /serp/google/maps/task_post
// IN /d4return/purpose/serp/google/maps/{task_id}
*/
$em = $this->entityManager;
$task = $em->getRepository(SEOTasks::class)->findOneBy(array('keyword' => $postinfo->keyword, 'status' => 'sent'));
if (!$task) {
$task_id = $this->getUUID();
$postbackurl = "https://seopanel.surfsideweb.com/d4return/serp/google/maps/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keyword)) {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => mb_convert_encoding($postinfo->keyword, "UTF-8"),
"priority" => $postinfo->priority,
"tag" => $task_id,
"postback_data" => 'advanced',
"postback_url" => $postbackurl );
$conn = $this->connect('POST', '/serp/google/maps/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = $taskback->data->function;
$postinfo->se = $taskback->data->se;
$postinfo->se_type = 'maps';
$postinfo->device = $taskback->data->device;
$postinfo->os = $taskback->data->os;
$postinfo->created = date('Y-m-d H:i:s');
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
} else {
$postinfo->status = 'failed to send';
}
}
} else {
$postinfo->status = 'task already sent';
}
return $postinfo;
}
/**
* Sets the task to collect GMB panel information. AKA the main GMB listing info.
*/
public function businessData($postinfo, $force = FALSE) {
/*
// OUT /business_data/google/my_business_info/task_post
// IN /d4return/purpose/business_data/my_business_info/{task_id}/
*/
$em = $this->entityManager;
$keywordsafe = mb_convert_encoding($postinfo->keyword, "UTF-8");
if($force != TRUE) {
$thirty = date('Y-m-d G:i:s', strtotime('-30 days'));
$task = $em->getRepository(SEOTasks::class)->chkRecent($thirty, $postinfo->purpose, $postinfo->se, $postinfo->location_code, 'gmbusiness', $keywordsafe, 'desktop');
}
if (!$task) {
$task_id = $this->getUUID();
$purpose = $postinfo->purpose;
$postbackurl = "https://seopanel.surfsideweb.com/d4return/".$purpose."/business_data/google/my_business_info/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keyword)) {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => $keywordsafe,
"priority" => $postinfo->priority,
"tag" => $task_id,
"postback_url" => $postbackurl );
$postinfo->created = date('Y-m-d H:i:s');
$conn = $this->connect('POST', '/business_data/google/my_business_info/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = $taskback->data->function;
$postinfo->se = $taskback->data->se;
$postinfo->se_type = 'gmbusiness';
$postinfo->device = $taskback->data->device;
$postinfo->os = $taskback->data->os;
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
} else {
$postinfo->status = 'Too soon or failed to send';
}
}
} else {
$postinfo->status = 'Task already sent';
}
return $postinfo;
}
/**
* Sets the task to collect GMB reviews segment plus other gplaces info.
*/
public function businessReviews($postinfo, $force = FALSE) {
/*
// OUT /business_data/google/reviews/task_post
// IN /d4return/purpose/business_data/reviews/{task_id}/
*/
$em = $this->entityManager;
if($force != TRUE) {
$thirty = date('Y-m-d H:i:s', strtotime('-30 days'));
$task = $em->getRepository(SEOTasks::class)->chkRecent($thirty, $postinfo->purpose, $postinfo->se, $postinfo->location_code, 'gmbtest', $postinfo->keyword, $postinfo->device);
}
if (!$task) {
$task_id = $this->getUUID();
$postbackurl = "https://seopanel.surfsideweb.com/d4return/".$postinfo->purpose."/business_data/google/reviews/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keyword)) {
//echo $postinfo->keyword.' - '.$postinfo->se.' - '.$postinfo->se_type.' - '.$postinfo->type.' - '.$postinfo->device.' - '.$postinfo->location_code.' - No Task<br>';
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => $postinfo->keyword,
"priority" => $postinfo->priority,
"tag" => $task_id,
"depth" => '20',
"sort_by" => 'newest',
"postback_url" => $postbackurl );
//dd($postdata);
$postinfo->created = date('Y-m-d H:i:s');
$conn = $this->connect('POST', '/business_data/google/reviews/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->result = json_encode($taskback);
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = $taskback->data->function;
$postinfo->se = $taskback->data->se;
$postinfo->device = $taskback->data->device;
$postinfo->os = $taskback->data->os;
$postinfo->se_type = 'gmbtest';
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
if(isset($postinfo->purpose) && 'footprint' == $postinfo->purpose) {
$postinfo->se_type = 'reviews';
$this->storeFpTask($postinfo);
}
} else {
$postinfo->status = 'Too soon or failed to send';
}
}
} else {
//echo $postinfo->keyword.' - '.$postinfo->se.' - '.$postinfo->se_type.' - '.$postinfo->device.' - '.$postinfo->location_code.' - <b>Task Exists</b><br>';
$postinfo->status = 'Task already sent';
}
return $postinfo;
}
public function createFpReportTests($userinfo, $prevreport = null) {
//Lets build the current keywords set
$keywords = $this->getUserKeywords($userinfo); // WHY IS THIS HERE
$em = $this->entityManager;
$now = date('Y-m-d G:i:s');
// Next lets create the report itself giving it a unique UUID
$report = $em->getRepository(FPReports::class)->findOneBy(array('lead_id' => $userinfo['id'], 'year' => date('Y'), 'month' => date('m'), 'status' => 'pending'));
if(!$report) {
$report = new FPReports();
$report->setLeadID($userinfo['id']);
$reportid = $this->getUUID();
$report->setUuid($reportid);
$report->setYear(date('Y'));
$report->setMonth(date('m'));
$report->setStatus('pending');
$report->setEmailed('unsent');
$report->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $now));
$em->persist($report);
$em->flush();
if ($userinfo['local'] == 'yes') {
$testtypes = array('kwranking', 'kwvolumes', 'mapsranking', 'pagespeed', 'backlinks', 'analytics', 'legaldocs', 'socialcheck',
'gscertified', 'gmbtest');
} else {
$testtypes = array('kwranking', 'kwvolumes', 'pagespeed', 'backlinks', 'analytics', 'legaldocs', 'socialcheck', 'gscertified');
}
foreach($testtypes as $testtype) {
$test = $em->getRepository(FPTests::class)->findOneBy(array('type' => $testtype, 'lead_id' => $userinfo['id'], 'status' => 'pending' ));
if(!$test) {
$test = new FPTests();
$test->setType($testtype);
$test->setTestGroup($this->getUUID());
$test->setLeadId($userinfo['id']);
$test->setReportId($reportid);
$test->setYear(date('Y'));
$test->setMonth(date('m'));
$test->setStatus('pending');
$test->setResult(null);
$test->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $now));
$em->persist($test);
$em->flush();
}
}
} else {
$reportid = $report->getUuid();
}
return $reportid;
}
public function retrieveOrphanTasks() {
// Reclaim lost tasts still in SENT status
$em = $this->entityManager;
$tasks = $em->getRepository(SEOTasks::class)->chkOrphanTasks('sent');
//dd($tasks);
if($tasks) {
foreach($tasks as $task) {
if($task->getSeType() == 'backlinkchk') {
$function = 'backlinkchk';
} else {
$function = $task->getFunction();
}
$path = 'https://seopanel.surfsideweb.com/d4return/'.$task->getPurpose().'/'.$task->getApi().'/'.$task->getSe().'/'.$function.'/'.$task->getTaskId().'/';
//echo '<a href="'.$path.'">'.$path.'</a><br>';
$result = @file_get_contents($path);
//dd($result,$task);
if(!isset($result) || $result != 'Success') {
$task->setStatus('not found');
$em->persist($task);
$em->flush();
if('footprint' == $task->getPurpose() && $function != 'backlinkchk') {
$fptask = $em->getRepository(FPTasks::class)->findOneBy(array('uuid' => $task->getTaskId()));
if($fptask) {
$fptask->setStatus('not found');
$em->persist($fptask);
$em->flush();
}
}
}
sleep(1);
}
return $tasks;
}
}
/**
* Sets the task to collect keyword volume information.
*/
public function keywordVolumes($postinfo, $force = FALSE) {
/*
// OUT /keywords_data/google/search_volume/task_post
// IN /d4return/purpose/keywords_data/google/search_volume/{task_id}/
*/
$em = $this->entityManager;
$keywords = json_encode($postinfo->keywords);
if($force != TRUE) {
$thirty = date('Y-m-d G:i:s', strtotime('-30 days'));
$task = $em->getRepository(SEOTasks::class)->chkRecent($thirty, $postinfo->purpose, $postinfo->se, $postinfo->location_code, $postinfo->se_type, $keywords, $postinfo->device);
}
if (!$task) {
$task_id = $this->getUUID();
$postbackurl = "https://seopanel.surfsideweb.com/d4return/".$postinfo->purpose."/keywords_data/google/search_volume/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keywords)) {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keywords" => $postinfo->keywords,
"priority" => $postinfo->priority,
"search_partners" => TRUE,
"tag" => $task_id,
"postback_url" => $postbackurl );
$postinfo->created = date('Y-m-d H:i:s');
$conn = $this->connect('POST', '/keywords_data/google/search_volume/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->result = json_encode($taskback);
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = $postinfo->se_type;
$postinfo->se = $taskback->data->se;
$postinfo->type = $postinfo->se_type;
$postinfo->os = 'windows';
$postinfo->keyword = $keywords;
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
if(isset($postinfo->purpose) && 'footprint' == $postinfo->purpose) {
$this->storeFpTask($postinfo);
}
} else {
$postinfo->status = 'Failed to send';
}
}
} else {
//echo $keywords.' - '.$postinfo->se.' - '.$postinfo->se_type.' - '.$postinfo->device.' - '.$postinfo->location_code.' - <b>Task Exists</b><br>';
$postinfo->status = 'Task already sent';
}
return $postinfo;
}
public function keywordSerps($postinfo, $postbackdata = 'regular', $force = FALSE) {
/*
// OUT /serp/google/organic/task_post
// IN /d4return/purpose/serp/google/organic/{task_id}/
*/
$em = $this->entityManager;
$task = FALSE;
if($force != TRUE) {
$thirty = date('Y-m-d G:i:s', strtotime('-30 days'));
$task = $em->getRepository(SEOTasks::class)->chkRecent($thirty, $postinfo->purpose, $postinfo->se, $postinfo->location_code, $postinfo->se_type, $postinfo->keyword, $postinfo->device);
}
if (!$task) {
//echo $postinfo->keyword.' - '.$postinfo->se.' - '.$postinfo->device.' - '.$postinfo->location_code.' - No Task<br>';
$task_id = $this->getUUID();
if($postinfo->type == 'backlinkchk') {
$postinfo->se_type = 'backlinkchk';
}
$postbackurl = "https://seopanel.surfsideweb.com/d4return/".$postinfo->purpose."/serp/".$postinfo->se."/".$postinfo->se_type."/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keyword)) {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => $postinfo->keyword,
"device" => $postinfo->device,
"priority" => $postinfo->priority,
"tag" => $task_id,
"postback_url" => $postbackurl,
"postback_data" => $postbackdata );
$postinfo->created = date('Y-m-d H:i:s');
$conn = $this->connect('POST', '/serp/'.$postinfo->se.'/organic/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->result = json_encode($taskback);
if($taskback->status_code == 20100) {
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = 'organic';
$postinfo->se = $taskback->data->se;
$postinfo->device = $taskback->data->device;
$postinfo->os = $taskback->data->os;
}
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
if(isset($postinfo->purpose) && 'footprint' == $postinfo->purpose) {
$this->storeFpTask($postinfo);
}
} else {
$postinfo->status = 'Failed to send';
}
}
} else {
$postinfo->status = 'Task already sent';
}
return $postinfo;
}
public function keywordMapPacks($postinfo, $force = FALSE) {
/*
// OUT /serp/google/maps/task_post
// IN /d4return/purpose/serp/google/maps/{task_id}/
*/
$em = $this->entityManager;
if($postinfo->se == 'google') {
$postbackdata = 'advanced';
$device = $postinfo->device;
} else {
$postbackdata = 'regular';
$device = 'none';
}
if($force != TRUE) {
$thirty = date('Y-m-d G:i:s', strtotime('-30 days'));
$task = $em->getRepository(SEOTasks::class)->chkRecent($thirty, $postinfo->purpose, $postinfo->se, $postinfo->location_code, $postinfo->se_type, $postinfo->keyword, $device);
}
if (!$task) {
//echo $postinfo->keyword.' - '.$postinfo->se.' - '.$postinfo->se_type.' - '.$device.' - '.$postinfo->location_code.' - No Task<br>';
$task_id = $this->getUUID();
$postbackurl = "https://seopanel.surfsideweb.com/d4return/".$postinfo->purpose."/serp/".$postinfo->se."/".$postinfo->se_type."/".$task_id."/";
if(!empty($postinfo->language_code) && !empty($postinfo->location_code) && !empty($postinfo->keyword)) {
if($postinfo->se == 'google') {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => $postinfo->keyword,
"device" => $device,
"priority" => $postinfo->priority,
"tag" => $task_id,
"postback_url" => $postbackurl,
"postback_data" => $postbackdata );
} else {
$postdata[] = array(
"language_code" => $postinfo->language_code,
"location_code" => $postinfo->location_code,
"keyword" => $postinfo->keyword,
"priority" => $postinfo->priority,
"tag" => $task_id,
"postback_url" => $postbackurl,
"postback_data" => $postbackdata );
}
$postinfo->created = date('Y-m-d H:i:s');
$conn = $this->connect('POST', '/serp/'.$postinfo->se.'/'.$postinfo->se_type.'/task_post', $postdata);
$content = $conn->getContent();
$postinfo->task_id = $task_id;
$taskback = (object) json_decode($content);
$taskback = $taskback->tasks[0];
$postinfo->result = json_encode($taskback);
if($taskback->status_code == 20100) {
$postinfo->d4id = $taskback->id;
$postinfo->api = $taskback->data->api;
$postinfo->function = $taskback->data->se_type;
$postinfo->se = $taskback->data->se;
$postinfo->se_type = $taskback->data->se_type;
if($postinfo->se == 'google') {
$postinfo->device = $taskback->data->device;
} else {
$postinfo->device = 'none';
}
$postinfo->os = $taskback->data->os;
}
if ($taskback->status_message == 'Task Created.') {
$postinfo->status = 'sent';
$this->storeTask($postinfo);
if(isset($postinfo->purpose) && 'footprint' == $postinfo->purpose) {
$this->storeFpTask($postinfo);
}
} else {
$postinfo->status = 'Failed to send';
}
}
} else {
echo $postinfo->keyword.' - '.$postinfo->se.' - '.$postinfo->se_type.' - '.$postinfo->device.' - '.$postinfo->location_code.' - <b>Task Exists</b><br>';
$postinfo->status = 'Task Exists';
}
return $postinfo;
}
public function storeTask($taskinfo) {
$em = $this->entityManager;
$task = $em->getRepository(SEOTasks::class)->findOneBy(array('task_id' => $taskinfo->task_id));
if(!$task) {
$task = new SEOTasks();
$task->setPurpose($taskinfo->purpose);
$task->setTaskId($taskinfo->task_id);
$task->setD4id($taskinfo->d4id);
$task->setApi($taskinfo->api);
$task->setFunction($taskinfo->function);
$task->setSe($taskinfo->se);
$task->setLanguageCode($taskinfo->language_code);
$task->setLocationCode($taskinfo->location_code);
$task->setKeyword($taskinfo->keyword);
$task->setSeType($taskinfo->se_type);
$task->setDevice($taskinfo->device);
$task->setOs($taskinfo->os);
$task->setStatus($taskinfo->status);
$task->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $taskinfo->created));
$em->persist($task);
$em->flush();
}
return $task;
}
public function storeFpTask($taskinfo) {
$em = $this->entityManager;
$task = $em->getRepository(FPTasks::class)->findOneBy(array('uuid' => $taskinfo->task_id));
if(!$task) {
$task = new FPTasks();
$task->setTestId($taskinfo->test_id);
$task->setUuid($taskinfo->task_id);
$task->setName($taskinfo->type);
$task->setLeadId($taskinfo->lead_id);
$task->setKeyword($taskinfo->keyword);
$task->setSearchEngine($taskinfo->se);
$task->setSeType($taskinfo->se_type);
$task->setDevice($taskinfo->device);
$task->setStatus($taskinfo->status);
$task->setResult($taskinfo->result);
$task->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $taskinfo->created));
$em->persist($task);
$em->flush();
}
return $task;
}
public function storeFpRanking($ranking) {
return; // This has been discontinued in favor of the processSerpKwRankings function below
$em = $this->entityManager;
$rank = $em->getRepository(FPRankings::class)->findOneBy(array('uuid' => $ranking->uuid, 'test_group' => $ranking->test_group));
if(!$rank) {
$rank = new FPRankings();
$rank->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $ranking->created));
}
$rank->setLeadId($ranking->lead_id);
$rank->setUuid($ranking->uuid);
$rank->setTestGroup($ranking->test_group);
$rank->setKeyword($ranking->keyword);
$rank->setTitle($ranking->title);
$rank->setDescription($ranking->description);
$rank->setBreadcrumb($ranking->breadcrumb);
$rank->setDomain($ranking->domain);
$rank->setString($ranking->string);
$rank->setYear($ranking->year);
$rank->setMonth($ranking->month);
$rank->setGoogleDeskNat($ranking->google_desk_nat);
$rank->setGoogleDeskLoc($ranking->google_desk_loc);
$rank->setGoogleMobiNat($ranking->google_mobi_nat);
$rank->setGoogleMobiLoc($ranking->google_mobi_loc);
$rank->setGoogleAdsNat($ranking->google_ads_nat);
$rank->setGoogleAdsLoc($ranking->google_ads_loc);
$rank->setGoogleMapNat($ranking->google_map_nat);
$rank->setGoogleMapLoc($ranking->google_map_loc);
$rank->setBingDeskNat($ranking->bing_desk_nat);
$rank->setBingDeskLoc($ranking->bing_desk_loc);
$rank->setBingMobiNat($ranking->bing_mobi_nat);
$rank->setBingMobiLoc($ranking->bing_mobi_loc);
$rank->setYahooDeskNat($ranking->yahoo_desk_nat);
$rank->setYahooDeskLoc($ranking->yahoo_desk_loc );
$em->persist($rank);
$em->flush();
return $rank;
}
public function storeQueue($qinfo) {
$em = $this->entityManager;
$qitem = $em->getRepository(IntelQueue::class)->findOneBy(array('content' => $qinfo->content, 'actionapi' => $qinfo->actionapi, 'function' => $qinfo->function));
//dd($qinfo->content);
if(!$qitem) {
$qitem = new IntelQueue();
$qitem->setPurpose($qinfo->purpose);
$qitem->setLocationCode($qinfo->location_code);
$qitem->setActionApi($qinfo->actionapi);
$qitem->setFunction($qinfo->function);
$qitem->setContent($qinfo->content);
$qitem->setMore($qinfo->more);
$qitem->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s')));
$em->persist($qitem);
$em->flush();
}
return $qitem;
}
public function getUUID() {
$randomString = openssl_random_pseudo_bytes(16);
$time_low = bin2hex(substr($randomString, 0, 4));
$time_mid = bin2hex(substr($randomString, 4, 2));
$time_hi_and_version = bin2hex(substr($randomString, 6, 2));
$clock_seq_hi_and_reserved = bin2hex(substr($randomString, 8, 2));
$node = bin2hex(substr($randomString, 10, 6));
$time_hi_and_version = hexdec($time_hi_and_version);
$time_hi_and_version = $time_hi_and_version >> 4;
$time_hi_and_version = $time_hi_and_version | 0x4000;
$clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;
return sprintf('%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node);
}
private function getTargetLocation($userinfo) {
if(!empty($userinfo['localtarget'])) {
$location = $this->locationsRepository->findLocationCity($userinfo['localtarget'], $userinfo['state']);
} else {
$location = $this->locationsRepository->findLocationCity($userinfo['city'], $userinfo['state']);
}
return $location->getLocationCode();
}
public function processSerpKwRankings($testgroup,$campinfo,$purpose = 'footprint') {
// Lets process the rankings for keywords found in the Serps
$em = $this->entityManager;
if($purpose == 'footprint') {
//For now we will do the footprint purpose. Will multipurpose this function later.
$serpsRepo = $em->getRepository(FPSerps::class);
$rankingsRepo = $em->getRepository(FPRankings::class);
} else {
//Assume purpose of Campaign. Will do this later.
//$serpRepo = $this->kwSerpsRepository;
//$rankRepo = $this->kwRankingsRepository;
}
//dd($testgroup,$campinfo);
$thistestgroup = $testgroup->getTestGroup();
$lead_id = $testgroup->getLeadId();
$checkserps = $serpsRepo->findBy(['test_group' => $thistestgroup], ['task_id' => 'ASC']);
//echo count($checkserps).'<br>';
$month = $testgroup->getCreated()->format('m');
$year = $testgroup->getCreated()->format('Y');
$created = $testgroup->getCreated()->format('Y-m-d H:i:s');
foreach($checkserps as $serp) {
//echo $serp->getKeyword().' - '.$serp->getSearchEngine().' - '.$serp->getDevice().' - '.$serp->getLocation().' - ';
if($serp->getSearchEngine() == 'google') {
$serpfile = $serp->getAdvanced();
} else {
$serpfile = $serp->getRegular();
}
if(!isset($campinfo['domain'])) {
$parse = parse_url($campinfo['url']);
$campinfo['domain'] = $parse['host'];
}
$stripdomain = str_replace('www.', '', $campinfo['domain']);
$keyword = $serp->getKeyword();
//echo ' trying for '.$serpfile.'<br>';
if(file_exists($_ENV["HOMEDIR"].'/'.$serpfile)) {
//echo '.... found '.$serpfile.'<br>';
$serpfile = json_decode(file_get_contents($_ENV["HOMEDIR"].'/'.$serpfile));
$serpfile = (object) $serpfile[0];
$ranking = $rankingsRepo->findOneBy(['lead_id' => $lead_id, 'keyword' => $keyword, 'year' => $year, 'month' => $month]);
if(!$ranking) {
$ranking = new FPRankings;
$ranking->setCreated(\DateTime::createFromFormat('Y-m-d H:i:s', $created));
$ranking->setLeadId($lead_id);
$ranking->setUuid($this->getUUID());
$ranking->setTestGroup($thistestgroup);
$ranking->setKeyword($keyword);
$ranking->setYear($year);
$ranking->setMonth($month);
}
$thisrank = (object) array();
$device = $serp->getDevice();
$searchengine = $serp->getSearchEngine();
$locationcode = $serp->getLocation();
if($locationcode == 2840) {
$locationtype = 'Country';
} else {
$locationinfo = $this->locationsRepository->findLocationUS($serp->getLocation());
$locationtype = $locationinfo->getLocationType();
}
if($serpfile->se_domain == 'google.com') {
// Leaving it as is cause may add bing.com later.
foreach($serpfile->items as $slisting) {
if(!empty($slisting->domain)) {
// First we check for Paid adds in the Serp and add the rankings to the
// google_ads_nat and google_ads_loc columns. Maybe bing ads later.
if((stristr($slisting->domain, $stripdomain) || stristr($slisting->domain, $stripdomain)) && $slisting->type == 'paid') {
$column = $serp->getSearchEngine().'_ads';
if (($locationtype == 'City' || $locationtype == 'State' || $locationtype == 'County')) {
$thisrank->{$column.'_loc'} = $slisting->rank_group;
} elseif (($locationtype == 'Region' || $locationtype == 'Country')) {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
} else {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
}
// We include these in case they are never found in rankings later. If they are we
// just overwrite them when the organic rankings are found.
$thisrank->setTitle = $slisting->title;
$thisrank->setString = $slisting->url;
$thisrank->setDescription = $slisting->description;
break;
}
}
}
}
if($serpfile->se_domain != 'yahoo.com') {
// This will check for the Google and Bing Maps rankings. If maps are found in the Serp they are
// added to google_map_nat google_map_loc bing_map_nat bing_map_loc columns
foreach($serpfile->items as $slisting) {
if(!empty($slisting->domain)) {
if((stristr($slisting->domain, $stripdomain) || stristr($slisting->domain, $stripdomain)) && ($slisting->type == 'maps' || $slisting->type == 'local_pack')) {
$column = $searchengine.'_map';
if (($locationtype == 'City' || $locationtype == 'State' || $locationtype == 'County')) {
$thisrank->{$column.'_loc'} = $slisting->rank_group;
} elseif (($locationtype == 'Region' || $locationtype == 'Country')) {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
} else {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
}
break;
}
}
}
}
// Now we chew on the truly Organic listings.
foreach($serpfile->items as $slisting) {
if(!empty($slisting->domain)) {
if((stristr($slisting->domain, $stripdomain) || stristr($slisting->domain, $stripdomain)) && $slisting->type == 'organic') {
if($device == 'desktop') {
$column = $searchengine.'_desk';
} else {
$column = $searchengine.'_mobi';
}
if (($locationtype == 'City' || $locationtype == 'State' || $locationtype == 'County')) {
$thisrank->{$column.'_loc'} = $slisting->rank_group;
$usecolumn = $column.'_loc';
} elseif (($locationtype == 'Region' || $locationtype == 'Country')) {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
$usecolumn = $column.'_nat';
} else {
$thisrank->{$column.'_nat'} = $slisting->rank_group;
$usecolumn = $column.'_nat';
}
//echo '<b>'.$slisting->domain.' - '.$serpfile->keyword.' - organic - column: '.$usecolumn.' location: '.$locationcode.' loc_type: '.$locationtype.' rank: '.$slisting->rank_group.'</b><br>';
if(!isset($thisrank->domain) || 'google' == $searchengine) {
$thisrank->domain = $slisting->domain;
}
if(!isset($thisrank->title) || 'google' == $searchengine) {
$thisrank->title = $slisting->title;
}
if(!isset($thisrank->string) || 'google' == $searchengine) {
$thisrank->string = $slisting->url;
}
if(!isset($thisrank->description) || 'google' == $searchengine) {
$thisrank->description = $slisting->description;
}
if(!isset($thisrank->breadcrumb) || 'google' == $searchengine) {
$thisrank->breadcrumb = $slisting->breadcrumb;
}
break;
}
}
}
}
//$this->storeFpRanking($ranking);
if(isset($thisrank->google_desk_nat)) { $ranking->setGoogleDeskNat($thisrank->google_desk_nat); }
if(isset($thisrank->google_desk_loc)) { $ranking->setGoogleDeskLoc($thisrank->google_desk_loc); }
if(isset($thisrank->google_mobi_nat)) { $ranking->setGoogleMobiNat($thisrank->google_mobi_nat); }
if(isset($thisrank->google_mobi_loc)) { $ranking->setGoogleMobiLoc($thisrank->google_mobi_loc); }
if(isset($thisrank->google_ads_nat)) { $ranking->setGoogleAdsNat($thisrank->google_ads_nat); }
if(isset($thisrank->google_ads_loc)) { $ranking->setGoogleAdsLoc($thisrank->google_ads_loc); }
if(isset($thisrank->google_map_nat)) { $ranking->setGoogleMapNat($thisrank->google_map_nat); }
if(isset($thisrank->google_map_loc)) { $ranking->setGoogleMapLoc($thisrank->google_map_loc); }
if(isset($thisrank->bing_desk_nat)) { $ranking->setBingDeskNat($thisrank->bing_desk_nat); }
if(isset($thisrank->bing_desk_loc)) { $ranking->setBingDeskLoc($thisrank->bing_desk_loc); }
if(isset($thisrank->bing_mobi_nat)) { $ranking->setBingMobiNat($thisrank->bing_mobi_nat); }
if(isset($thisrank->bing_mobi_loc)) { $ranking->setBingMobiLoc($thisrank->bing_mobi_loc); }
if(isset($thisrank->yahoo_desk_nat)) { $ranking->setYahooDeskNat($thisrank->yahoo_desk_nat); }
if(isset($thisrank->yahoo_desk_loc)) { $ranking->setYahooDeskLoc($thisrank->yahoo_desk_loc); }
if(isset($thisrank->domain) && empty($ranking->getDomain())) {
$ranking->setDomain($thisrank->domain);
}
if(isset($thisrank->title) && empty($ranking->getTitle())) {
$ranking->setTitle($thisrank->title);
}
if(isset($thisrank->string) && empty($ranking->getString())) {
$ranking->setString($thisrank->string);
}
if(isset($thisrank->description) && empty($ranking->getDescription())) {
$ranking->setDescription($thisrank->description);
}
if(isset($thisrank->breadcrumb) && empty($ranking->getBreadcrumb())) {
$ranking->setBreadcrumb($thisrank->breadcrumb);
}
$em->persist($ranking);
$em->flush();
}
//$testgroup->setStatus('ready');
//$em->persist($testgroup);
//$em->flush();
return;
}
public function getUserKeywords($userinfo) {
//Lets build the current keywords set from the user info
foreach($userinfo as $key => $val) {
if($userinfo['local'] == 'yes') {
if (strstr($key, 'keyword') && !strstr($key, 'keywordval') ) {
if(!empty($val) && !empty($userinfo['localtarget'])) {
$keywords['local'][] = $val.' '.strtolower($userinfo['localtarget']);
}
}
if (strstr($key, 'keyword') && !strstr($key, 'keywordval') ) {
if(!empty($val) && !empty($userinfo['city'])) {
$keywords['local'][] = $val.' '.strtolower($userinfo['city']);
}
}
}
if($userinfo['national'] == 'yes') {
if (strstr($key, 'keyword') && !strstr($key, 'keywordval') ) {
if(!empty($val)) {
$keywords['national'][] = $val;
}
}
}
}
if(!empty($userinfo['brand'])) {
$keywords['local'][] = mb_convert_encoding(strtolower($userinfo['brand']), "UTF-8");
$keywords['national'][] = mb_convert_encoding(strtolower($userinfo['brand']), "UTF-8");
}
return $keywords;
}
/*
// Here we keep some of the Footprint functions used in other classes
// Tests kwranking, kwvolumes, mapsranking, pagespeed, gmbtest are D4SEO Service
*/
/**
* Create and run kwvolumes and complete the test
*/
public function testD4kwvolumes($test,$userinfo) {
//return;
echo "Creating KWVolumes Tasks<br>";
$userinfo = $this->userRepository->findSafeLeadInfo($test->getLeadId());
$keywords = $this->getUserKeywords($userinfo);
if($userinfo['national'] == 'yes') {
$location = 2840;
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keywords" => $keywords['national'],
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'se_type' => 'kwvolumes',
'se' => 'google',
'device' => 'desktop',
'lead_id' => $test->getLeadId()
);
$result = $this->keywordVolumes($postinfo);
}
if($userinfo['local'] = 'yes') {
$location = $this->getTargetLocation($userinfo);
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keywords" => $keywords['local'],
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'se_type' => 'kwvolumes',
'se' => 'google',
'device' => 'desktop',
'lead_id' => $test->getLeadId()
);
$this->keywordVolumes($postinfo);
}
/*
// Get keyword volumes for each of the keywords. Send for each location national and local if applicable.
// We may want to check if test is still pending on the second run. If so we can force pull from the
// retrieval endpoint. Return possibly failed. If we can get it the second time mark test as failed.
// DO THIS LATER
*/
}
/**
* Create and run kwranking by calling necessary serps and complete the test
*/
public function testD4kwranking($test,$userinfo) {
//return;
echo "Creating KWRankings Tasks<br>";
$keywords = $this->getUserKeywords($userinfo);
//dd($keywords);
$engines = array('google', 'bing', 'yahoo');
$devices = array('desktop', 'mobile');
foreach ($engines as $engine) {
foreach($devices as $device) {
if(isset($keywords['national']) && $userinfo['national'] == 'yes') {
$locallocation = $this->getTargetLocation($userinfo);
foreach($keywords['national'] as $keyword) {
$location = 2840;
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'kwranking',
'se_type' => 'organic',
'lead_id' => $test->getLeadId(),
'se' => $engine,
'device' => $device
);
$this->keywordSerps($postinfo);
}
//echo " - National ".$engine." ".$device." ".$location."<br>";
foreach($keywords['national'] as $keyword) {
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $locallocation,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'kwranking',
'se_type' => 'organic',
'lead_id' => $test->getLeadId(),
'se' => $engine,
'device' => $device
);
$this->keywordSerps($postinfo);
}
//echo " - National ".$engine." ".$device." ".$locallocation."<br>";
}
if(isset($keywords['local']) && $userinfo['local'] == 'yes') {
if('yahoo' != $engine) {
foreach($keywords['local'] as $keyword) {
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $locallocation,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'kwranking',
'se_type' => 'organic',
'lead_id' => $test->getLeadId(),
'se' => $engine,
'device' => $device
);
$this->keywordSerps($postinfo);
}
}
//echo " - Local ".$engine." ".$device." ".$location."<br>";
}
}
sleep(1);
}
sleep(1);
}
/**
* Create and run mapsranking and complete the test
*/
public function testD4mapsranking($test,$userinfo) {
//return;
echo "Creating MapsRanking Tasks<br>";
$keywords = $this->getUserKeywords($userinfo);
$engines = array('google', 'bing');
$devices = array('desktop');
foreach ($engines as $engine) {
if($engine == 'google') {
$setype = 'maps';
} else {
$setype = 'local_pack';
}
foreach($devices as $device) {
if($engine == 'bing' && $device == 'mobile') {
break;
}
if(isset($keywords['national']) && $userinfo['national'] == 'yes') {
foreach($keywords['national'] as $keyword) {
$location = 2840;
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'mapsranking',
'se_type' => $setype,
'se' => $engine,
'lead_id' => $test->getLeadId(),
'engine' => $engine,
'device' => $device,
);
$this->keywordMapPacks($postinfo);
}
//echo " - National ".$engine." ".$device." ".$location."<br>";
}
if(isset($keywords['local']) && $userinfo['local'] == 'yes') {
$location = $this->getTargetLocation($userinfo);
foreach($keywords['local'] as $keyword) {
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'mapsranking',
'se_type' => $setype,
'se' => $engine,
'lead_id' => $test->getLeadId(),
'engine' => $engine,
'device' => $device,
);
$this->keywordMapPacks($postinfo);
}
//echo " - Local ".$engine." ".$setype." ".$device." ".$location."<br>";
}
}
}
sleep(1);
}
/**
* Create and run gmbtest including GMB and Reviews and complete the test
*/
public function testD4gmbtest($test, $userinfo) {
//return;
echo "Creating GMB Test Tasks<br>";
if(!empty($userinfo['brand'])) {
$keyword = mb_convert_encoding($userinfo['brand'], "UTF-8");
} else {
$keyword = mb_convert_encoding($userinfo['companyname'], "UTF-8");
}
$userinfo['localtarget'] = $userinfo['city'];
$location = $this->getTargetLocation($userinfo);
$em = $this->entityManager;
$place = $em->getRepository(Gplaces::class)->findBy(array('title' => $keyword, 'location' => $location), ['modified' => 'DESC'], 1);
if(!$place) {
$address = trim($userinfo['address'], '.').', '.$userinfo['city'].', '.$userinfo['iso'].' '.$userinfo['zip'];
$place = $em->getRepository(Gplaces::class)->findOneBy(array('title' => $keyword, 'address' => $address), ['modified' => 'DESC'], 1);
}
if(isset($place)) {
$keyword = "cid:".$place->getCid();
//dd($place);
}
if($userinfo['local'] == 'yes') {
$postinfo = (object) array(
"language_code" => "en",
"location_code" => $location,
"keyword" => $keyword,
"purpose" => 'footprint',
"priority" => 1,
'test_id' => $test->getId(),
'type' => 'gmbtest',
'se_type' => 'reviews',
'se' => 'google',
'lead_id' => $test->getLeadId(),
'device' => 'desktop',
);
//dd($postinfo);
$this->businessReviews($postinfo);
}
}
}