cancel
Showing results for 
Search instead for 
Did you mean: 

deleteContactFromList

Occasional Participant

Re: deleteContactFromList

Herm....

 

echo 'CHECKING ACTIVE MEMBERSHIPS...<p>';
foreach($active as $a_email){
  //find the contact...
  echo 'searching for cc email '.$a_email.'<br>';
  $tmp_cntct = $cc->getContactByEmail(ACCESS_TOKEN, $a_email);
  if(sizeof($tmp_cntct->results) === 0){
  echo 'user not found.  Adding '.$a_email.'<br>';
    //add this contact to the active list...
    $Contact = new Contact();
    $Contact->addEmail($a_email);
    $Contact->addList($ref_active);
    $NewContact = $cc->addContact(ACCESS_TOKEN, $Contact);
    echo 'added to active list successfully';
  }
  else{
    echo 'user '. $a_email .' found, remove from inactive list and add to active list<br>';
    $ctct = $tmp_cntct->results[0];
   if(checkInList($ctct, $ref_nonactive->id)){
     echo 'Found user '. $a_email .' in nonactive list....removing...<p>';
     $ret = $cc->deleteContactFromList(ACCESS_TOKEN, $ctct->id, $ref_nonactive);
   }
    $ctct->addList($ref_active);
    $cc->updateContact(ACCESS_TOKEN, $ctct, false);
  }
}


$chk = $cc->getContactByEmail(ACCESS_TOKEN, "blah@blah.com");
$usrchk = $chk->results[0];
echo 'Check users lists: <p>';
var_dump($usrchk->lists);

 

 

Outputs:

 

searching for cc email blah@blah.com


user blah@blah.com found, remove from inactive list, and add to active list


checking 3 == 3

 

Found user blah@blah.com in nonactive list....removing...(id: 6)

 

Check users lists (active list id: 3, non_active list id: 4):

 

array(2) { [0]=> object(Ctct\Components\Contacts\ContactList)#35 (4) { ["id"]=> string(1) "3" ["name"]=> NULL ["status"]=> string(6) "ACTIVE" ["contact_count"]=> NULL } [1]=> object(Ctct\Components\Contacts\ContactList)#40 (4) { ["id"]=> string(1) "4" ["name"]=> NULL ["status"]=> string(6) "ACTIVE" ["contact_count"]=> NULL } }

 

 

  *For good measure I also tried passing the list as an id as well.....same results :(   I feel like everything is in place, heck I even wrote a quick check to prevent it from trying to remove a user form a list they may not belong to in the first place...

Occasional Participant

Re: deleteContactFromList

This seems to do the trick for me...not using that function at all...

 

echo 'CHECKING NON-ACTIVE MEMBERSHIPS...<p>';

foreach($all_others as $na_email){
  //find the contact...
  echo "searching for cc email ".$na_email."<br>";
  $tmp_cntct      = $cc->getContactByEmail(ACCESS_TOKEN, $na_email);
  if(sizeof($tmp_cntct->results) === 0){
  echo "user not found.  Adding ".$na_email."<br>";
    //add this contact to the non active list...
    $Contact      = new Contact();
    $Contact->addEmail($na_email);
    $Contact->addList($ref_nonactive);
    $NewContact   = $cc->addContact(ACCESS_TOKEN, $Contact);
    echo 'added to nonactive list successfully';
  }
  else{
    echo "user ". $na_email ." found, remove from active list, add to non active list<br>";
    $ctct         = $tmp_cntct->results[0];
    if(checkInList($ctct, $ref_active->id)){
     // echo 'Found user '. $na_email .' in active list....removing...(id: '. $ctct->id .')<p>';
      //echo 'params: '. ACCESS_TOKEN .' '.$ctct->id .' '.$ref_active->id.'<p>';
      //$ret        = $cc->deleteContactFromList(ACCESS_TOKEN, (int)$ctct->id, $ref_active);
      removeFromList($cc, $ctct, $ref_active);
    }
    $ctct->addList($ref_nonactive);
    $cc->updateContact(ACCESS_TOKEN, $ctct, false);
  }
}

## =========================
## Function check in List
## =========================
function checkInList($cntct, $listid){
  $ret            = false;
  $lists          = $cntct->lists;
  foreach($lists as $contactlist){
    echo 'checking '.$contactlist->id .' == '. $listid .'<p>';
    if($contactlist->id === $listid){
      $ret        = true;
    }
  }
  return $ret;
}

#==============================
#Function removeFromList
#============================== function removeFromList($cc, $contact, $list){ for($i=0; $i<sizeof($contact->lists); $i++){ $tmplist = $contact->lists[$i]; if($tmplist->id == $list->id){ array_splice($contact->lists, $i, 1); } } $cc->updateContact(ACCESS_TOKEN, $contact, false); }

 

...Though I feel a bit dirty doing it.   I'd feel much better if I was using that API function properly :/  

Highlighted
CTCT Employee

Re: deleteContactFromList

Thanks for sharing this!  I'm going to dig into this more and see if I can figure out what is happening...

-------------------------------------------
Check out our developer portal: http://developer.constantcontact.com