How To Check If a Tag Exists And If Not Add It

Written by

in

[php]<?php

// Connect to Infusionsoft

require_once("isdk.php");
$app = new iSDK;
if ($app->cfgCon("connectionName")) {

// Current date in Infusionsoft-friendly format

$currentDate = date_format(date_create(), ‘YmdTH:i:s’);
echo "You connected at $currentDate <br/><br/>";

// $fname = ‘Dritte’;
// $lname = ‘Dawg’;
// $email = ‘dritte@kimsnider.com’;

// Assign POST variables using ternary operators and IF statement
if ($_POST[‘Id’]) {
$conID = $_POST[‘Id’];
} else {
$conID = ($_POST[‘contactId’]) ? $_POST[‘contactId’] : ”;
}

$eventDate = ($_POST[‘eventDate’]) ? $_POST[‘eventDate’] : ”;

$tagDate = date_format($eventDate, ‘Ymd’); // 20131220 Event Registered

// Dump to file
$file=fopen("kim_log.txt","a+");
fwrite($file, "n $currentDate n");
fwrite($file, "Id: $conID n");
fwrite($file, "Event Date: $eventDate n");
fwrite($file, print_r($_POST,true));
$file=fclose($file);

// Find the ID of the Event Registered tag for this date

$returnFields = array(‘Id’);
$query = array(‘GroupCategoryId’ => 31,
‘GroupName’ => $tagDate.’%’);
$return = $app->dsQuery("ContactGroup",1,0,$query,$returnFields);

// If a matching tag doesn’t exist already …
if(!isset($return[0][‘Id’])) {

//Create the tag

$tagText = ‘ API Mastermind’;
$tag = array(‘GroupName’ => $tagDate.$tagText,
‘GroupCategoryId’ => 31);
$theTag = $app->dsAdd(‘ContactGroup’, $tag);

// If it does exist …
} else {
// Grab that tag’s ID so we can use it
$theTag = $return[0][‘Id’];
}

// Whether we found it or created it, now we set it

$set = $app->grpAssign($contactId, $theTag);

} else {
echo "Not Connected…";
}
[/php]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *