From 18e96b4bc003bb33ac1ed27b55048fb1b890dba8 Mon Sep 17 00:00:00 2001 From: Joyce Babu Date: Thu, 25 Apr 2019 21:13:43 +0530 Subject: [PATCH] Activate existing inactive key-values --- app/AdManager/KeyManager.php | 18 ++++++++++++++++++ app/AdManager/ValueManager.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/app/AdManager/KeyManager.php b/app/AdManager/KeyManager.php index 08d0d94..0b90b97 100644 --- a/app/AdManager/KeyManager.php +++ b/app/AdManager/KeyManager.php @@ -4,6 +4,7 @@ require __DIR__.'/../../vendor/autoload.php'; +use Google\AdsApi\AdManager\v201811\ActivateCustomTargetingKeys; use Google\AdsApi\AdManager\v201811\CustomTargetingKey; use Google\AdsApi\AdManager\v201811\CustomTargetingKeyType; use Google\AdsApi\AdManager\Util\v201811\StatementBuilder; @@ -14,6 +15,8 @@ public function setUpCustomTargetingKey($keyName) { if (empty(($foo = $this->getCustomTargetingKey($keyName)))) { $foo = $this->createCustomTargetingKey($keyName); + } elseif ($foo[0]['keyStatus'] === 'INACTIVE') { + $this->activateCustomTargetingKey($keyName); } return $foo[0]['keyId']; @@ -33,6 +36,7 @@ public function createCustomTargetingKey($keyName) $foo = [ 'keyId' => $key->getId(), 'keyName' => $key->getName(), + 'keyStatus' => $key->getStatus(), 'keyDisplayNameId' => $key->getDisplayName(), ]; array_push($output, $foo); @@ -54,6 +58,7 @@ public function getAllCustomTargetingKeys() $foo = [ 'keyId' => $key->getId(), 'keyName' => $key->getName(), + 'keyStatus' => $key->getStatus(), 'keyDisplayNameId' => $key->getDisplayName(), ]; array_push($output, $foo); @@ -62,6 +67,18 @@ public function getAllCustomTargetingKeys() return $output; } + public function activateCustomTargetingKey($keyName) + { + $action = new ActivateCustomTargetingKeys(); + + $statementBuilder = (new StatementBuilder()) + ->where('name = :name') + ->WithBindVariableValue('name', $keyName); + + $customTargetingService = $this->serviceFactory->createCustomTargetingService($this->session); + $result = $customTargetingService->performCustomTargetingKeyAction($action, $statementBuilder->toStatement()); + } + public function getCustomTargetingKey($keyName) { $output = []; @@ -76,6 +93,7 @@ public function getCustomTargetingKey($keyName) $foo = [ 'keyId' => $key->getId(), 'keyName' => $key->getName(), + 'keyStatus' => $key->getStatus(), 'keyDisplayNameId' => $key->getDisplayName(), ]; array_push($output, $foo); diff --git a/app/AdManager/ValueManager.php b/app/AdManager/ValueManager.php index 12654cf..a887f3e 100644 --- a/app/AdManager/ValueManager.php +++ b/app/AdManager/ValueManager.php @@ -5,8 +5,10 @@ require __DIR__.'/../../vendor/autoload.php'; +use Google\AdsApi\AdManager\v201811\ActivateCustomTargetingValues; use Google\AdsApi\AdManager\v201811\CustomTargetingValue; use Google\AdsApi\AdManager\v201811\CustomTargetingValueMatchType; +use Google\AdsApi\AdManager\v201811\Statement; use Google\AdsApi\AdManager\Util\v201811\StatementBuilder; class ValueManager extends Manager @@ -33,22 +35,30 @@ public function convertValuesListToDFPValuesList($valuesList) //We create a table with only existing keys $existingValuesList = []; + $inactiveValuesList = []; $output = []; foreach ($existing as $foo) { array_push($existingValuesList, $foo['valueName']); if (in_array($foo['valueName'], $valuesList)) { array_push($output, $foo); + if ($foo['valueStatus'] === 'INACTIVE') { + $inactiveValuesList[$foo['valueName']] = $foo['valueId']; + } } } //We create a list with values to be created $valuesToBeCreated = []; + $valuesToBeActivated = []; foreach ($valuesList as $element) { if (!in_array($element, $existingValuesList)) { array_push($valuesToBeCreated, $element); + } else if (isset($inactiveValuesList[$element])) { + array_push($valuesToBeActivated, $inactiveValuesList[$element]); } } + if (!empty($valuesToBeCreated)) { $foo = $this->createCustomTargetingValues($valuesToBeCreated); foreach ($foo as $bar) { @@ -56,6 +66,10 @@ public function convertValuesListToDFPValuesList($valuesList) } } + if (!empty($valuesToBeActivated)) { + $this->activateCustomTargetingValues($valuesToBeActivated); + } + return $output; } @@ -98,6 +112,20 @@ public function createCustomTargetingValues($valuesToBeCreated) return $output; } + public function activateCustomTargetingValues($valuesToBeActivated) + { + $action = new ActivateCustomTargetingValues(); + + $statementText = sprintf("WHERE id IN (%s)", implode(',', $valuesToBeActivated)); + $statement = new Statement($statementText); + + $customTargetingService = $this->serviceFactory->createCustomTargetingService($this->session); + $result = $customTargetingService->performCustomTargetingValueAction($action, $statement); + if ($result->getNumChanges() !== count($valuesToBeActivated)) { + throw new \Exception("Failed to activate key values"); + } + } + public function getExistingValuesFromAdManager() { $output = []; @@ -119,6 +147,7 @@ public function getExistingValuesFromAdManager() $foo = [ 'valueId' => $value->getId(), 'valueName' => $value->getName(), + 'valueStatus' => $value->getStatus(), 'valueDisplayName' => $value->getDisplayName(), ]; array_push($output, $foo);