Skip to content

Commit

Permalink
Activate existing inactive key-values
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyceBabu committed Apr 25, 2019
1 parent 1af915d commit 18e96b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/AdManager/KeyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'];
Expand All @@ -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);
Expand All @@ -54,6 +58,7 @@ public function getAllCustomTargetingKeys()
$foo = [
'keyId' => $key->getId(),
'keyName' => $key->getName(),
'keyStatus' => $key->getStatus(),
'keyDisplayNameId' => $key->getDisplayName(),
];
array_push($output, $foo);
Expand All @@ -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 = [];
Expand All @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions app/AdManager/ValueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,29 +35,41 @@ 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) {
array_push($output, $bar);
}
}

if (!empty($valuesToBeActivated)) {
$this->activateCustomTargetingValues($valuesToBeActivated);
}

return $output;
}

Expand Down Expand Up @@ -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 = [];
Expand All @@ -119,6 +147,7 @@ public function getExistingValuesFromAdManager()
$foo = [
'valueId' => $value->getId(),
'valueName' => $value->getName(),
'valueStatus' => $value->getStatus(),
'valueDisplayName' => $value->getDisplayName(),
];
array_push($output, $foo);
Expand Down

0 comments on commit 18e96b4

Please sign in to comment.