diff --git a/app/AdManager/KeyManager.php b/app/AdManager/KeyManager.php index ea46477..1e6925e 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; @@ -15,6 +16,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']; @@ -34,6 +37,7 @@ public function createCustomTargetingKey($keyName) $foo = [ 'keyId' => $key->getId(), 'keyName' => $key->getName(), + 'keyStatus' => $key->getStatus(), 'keyDisplayNameId' => $key->getDisplayName(), ]; array_push($output, $foo); @@ -55,6 +59,7 @@ public function getAllCustomTargetingKeys() $foo = [ 'keyId' => $key->getId(), 'keyName' => $key->getName(), + 'keyStatus' => $key->getStatus(), 'keyDisplayNameId' => $key->getDisplayName(), ]; array_push($output, $foo); @@ -63,6 +68,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 = []; @@ -77,6 +94,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); diff --git a/script/tests/ConnexionTest.php b/script/tests/ConnexionTest.php index 4858c10..d964c87 100644 --- a/script/tests/ConnexionTest.php +++ b/script/tests/ConnexionTest.php @@ -2,7 +2,7 @@ require __DIR__.'/../scriptLoader.php'; -$traffickerId = (new \App\Dfp\UserManager())->getUserId(); +$traffickerId = (new \App\AdManager\UserManager())->getUserId(); if (is_numeric($traffickerId)) { echo "\n====Connexion OK====\n\n";