Last active
August 29, 2015 14:23
-
-
Save haet/30d74f8120a9d83e27ed to your computer and use it in GitHub Desktop.
CleverReach individual salutation http://etzelstorfer.com/cleverreach-adressdaten-per-script-bearbeiten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php session_start(); ?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>CleverReach Salutation</title> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> | |
<!-- Optional theme --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row marketing"> | |
<div class="col-lg-3"></div> | |
<div class="col-lg-6"> | |
<br> | |
<h2>CleverReach individual Salutation</h2> | |
<p><a href="http://etzelstorfer.com/cleverreach-adressdaten-per-script-bearbeiten" target="_blank">http://etzelstorfer.com/cleverreach-adressdaten-per-script-bearbeiten</a> | |
<? | |
if( !isset($_GET['api_key']) ){ | |
form_api_key(); | |
} else { // $api_key exists | |
$api_key = trim($_GET['api_key']); | |
metabox('Account',account_info($api_key)); | |
// #### LIST ######### | |
if( !isset($_GET['list']) ){ | |
form_list($api_key); | |
} else{ // $list exists | |
$list = $_GET['list']; | |
metabox('Group',list_name($api_key,$list)); | |
// #### condition-FIELD ######### | |
if( !isset($_GET['condition_field']) || !isset($_GET['condition_value']) ){ | |
form_condition( $api_key, $list ); | |
} else { | |
$condition_field = $_GET['condition_field']; | |
$condition = $_GET['condition']; | |
$condition_value = $_GET['condition_value']; | |
$target_field = $_GET['target_field']; | |
$target_value = $_GET['target_value']; | |
$page = 0; | |
if( isset($_GET['page']) ) | |
$page = $_GET['page']; | |
process_receivers($_GET['api_key'], $_GET['list'],$condition_field,$condition,$condition_value,$target_field,$target_value,$page); | |
metabox('IF','<strong>['.$condition_field.']</strong> '.strtoupper( $condition ).' <strong>['.$condition_value.']</strong>'); | |
metabox('THEN','<strong>['.$target_field.']</strong> = <strong>['.$target_value.']</strong>'); | |
metabox('Result','Processed: '.$_SESSION['processed'].' / '.$_SESSION['list_count'].'<br>'. | |
'Affected: '.$_SESSION['modified']); | |
$redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; | |
$redirect_url = preg_replace("/(.*)(&page=\d*)(.*)/", "$1$3", $redirect_url); | |
if( $_SESSION['processed'] < $_SESSION['list_count'] ){ | |
$redirect_url .= '&page='.($page+1); | |
?> | |
<script> | |
window.location.href="<?php echo $redirect_url; ?>"; | |
</script> | |
<?php | |
}else{ | |
metabox('Finished','If you want to run this task later again, please use this direct URL:<br><code>'.$redirect_url.'</code>'); | |
} | |
} | |
} | |
} // $api_key exists | |
/************************************** | |
* Display API Key form | |
**************************************/ | |
function form_api_key(){ | |
?> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h3 class="panel-title">CleverReach API Key</h3> | |
</div> | |
<div class="panel-body"> | |
<form method="GET" class="form-inline"> | |
<div class="form-group"> | |
<label>CleverReach API Key</label> | |
<input type="text" name="api_key"> | |
</div> | |
<div class="form-group"> | |
<button type="submit" class="btn btn-primary">next »</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
<?php | |
} | |
/************************************** | |
* Display general account infos | |
**************************************/ | |
function account_info($api_key){ | |
$api = new SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl'); | |
try{ | |
$result = $api->clientGetDetails($api_key); | |
//print_r($result); | |
if($result->status=="SUCCESS"){ | |
if( isset($result->data) ){ | |
return $result->data->firstname.' '.$result->data->name.' - '.$result->data->company; | |
} | |
} | |
} catch(Exception $e){ | |
echo 'Could not connect to the Cleverreach API.'; | |
} | |
return false; | |
} | |
/************************************** | |
* Display form to select the CleverReach subscriber List | |
**************************************/ | |
function form_list($api_key){ | |
$lists = get_lists($api_key); | |
if( is_array($lists) ){ ?> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h3 class="panel-title">CleverReach Group</h3> | |
</div> | |
<div class="panel-body"> | |
<form method="GET" class="form-inline"> | |
<div class="form-group"> | |
<?php foreach ($_GET as $key => $value) { //forward parameters?> | |
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>"> | |
<?php } ?> | |
<label>Group</label> | |
<select name="list"> | |
<?php foreach ($lists as $list): ?> | |
<option value="<?php echo $list->id; ?>"><?php echo $list->name.' ('.$list->count.' Receivers)'; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
<div class="form-group"> | |
<button type="submit" class="btn btn-primary">next »</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
<?php } // is_array_lists | |
} | |
/************************************** | |
* Display name of the selectes subscriber list | |
**************************************/ | |
function list_name($api_key,$list_id){ | |
$lists = get_lists($api_key); | |
if( is_array($lists) ){ | |
foreach ($lists as $list){ | |
if( $list_id==$list->id){ | |
$_SESSION['list_count'] = $list->count; | |
return $list->name.' ('.$list->count.' Receivers)'; | |
} | |
} | |
} // is_array_lists | |
} | |
/************************************** | |
* get all available lists from API | |
**************************************/ | |
function get_lists($api_key){ | |
$api = new SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl'); | |
try{ | |
$result = $api->groupGetList($api_key); | |
//print_r($result); | |
if($result->status=="SUCCESS"){ | |
if( !is_array($result->data) ){ | |
echo 'You don\'t have any Cleverreach forms.'; | |
return false; | |
}else{ | |
return $result->data; | |
} | |
} | |
} catch(Exception $e){ | |
echo 'Could not connect to the Cleverreach API.'; | |
} | |
return false; | |
} | |
/************************************** | |
* Display form to select enter the processing condition | |
**************************************/ | |
function form_condition($api_key,$list){ | |
$fields = get_fields($api_key,$list); | |
if( is_array($fields) ){ ?> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h3 class="panel-title">Condition</h3> | |
</div> | |
<div class="panel-body"> | |
<form method="GET" class="form-inline"> | |
<div class="form-group"> | |
<?php foreach ($_GET as $key => $value) { //forward parameters?> | |
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>"> | |
<?php } ?> | |
<label>IF</label> | |
<select name="condition_field"> | |
<?php foreach ($fields as $field): ?> | |
<option value="<?php echo $field->key; ?>"><?php echo $field->key; ?></option> | |
<?php endforeach; ?> | |
</select> | |
<select name="condition"> | |
<option value="in">in</option> | |
<option value="not_in">not in</option> | |
</select> | |
<input type="text" name="condition_value"> | |
<span id="helpBlock" class="help-block"> | |
separate entries with semikolon (;) | |
</span> | |
</div> | |
<br> | |
<div class="form-group"> | |
<label>THEN</label> | |
<select name="target_field"> | |
<?php foreach ($fields as $field): ?> | |
<option value="<?php echo $field->key; ?>"><?php echo $field->key; ?></option> | |
<?php endforeach; ?> | |
</select> | |
<label>=</label> | |
<input type="text" name="target_value"> | |
</div> | |
<div class="form-group"> | |
<button type="submit" class="btn btn-primary">GO »</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
<?php } // is_array_lists | |
} | |
/************************************** | |
* get global and local fields for list | |
**************************************/ | |
function get_fields($api_key,$list){ | |
$api = new SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl'); | |
try{ | |
$result = $api->groupGetDetails($api_key, $list); | |
if($result->status=="SUCCESS"){ | |
if( !is_array($result->data->attributes) ){ | |
echo 'No fields found.'; | |
return false; | |
}else{ | |
return array_merge($result->data->globalAttributes, $result->data->attributes); | |
} | |
} | |
} catch(Exception $e){ | |
echo 'Could not connect to the Cleverreach API.'; | |
} | |
return false; | |
} | |
/************************************** | |
* get chunks of 100 receivers and process | |
**************************************/ | |
function process_receivers($api_key, $list, $condition_field,$condition,$condition_value,$target_field,$target_value,$page=0){ | |
$api = new SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl'); | |
if( !isset($_SESSION['processed']) || $page == 0 ){ | |
$_SESSION['processed'] = 0; | |
$_SESSION['modified'] = 0; | |
} | |
try{ | |
$filter = array( "page"=>$page, "filter"=>"all" ); | |
$result = $api->receiverGetPage($api_key, $list, $filter); | |
if($result->status=="SUCCESS"){ | |
if( !is_array($result->data) ){ | |
var_dump($result->message); | |
return false; | |
}else{ | |
// echo '<pre>'; | |
// print_r($result->data); | |
// echo '</pre>'; | |
$condition_value_array = explode(';', $condition_value); | |
for ($i = 0; $i<count($condition_value_array); $i++) | |
$condition_value_array[$i] = strtolower( trim( $condition_value_array[$i] ) ); | |
//echo 'test: '.in_array('herr', $condition_value_array); | |
// echo '<pre>'; | |
// print_r($result->data); | |
// echo '</pre>'; | |
foreach ($result->data as $receiver) { | |
if( isset($receiver->globalAttributes) && isset($receiver->attributes) ) | |
$attributes = array_merge($receiver->globalAttributes, $receiver->attributes); | |
else if(isset($receiver->globalAttributes)) | |
$attributes = $receiver->globalAttributes; | |
else if(isset($receiver->attributes)) | |
$attributes = $receiver->attributes; | |
$_SESSION['processed'] ++; | |
foreach ($attributes as $attribute) { | |
if( $attribute->key == $condition_field && isset($attribute->value) && strlen($attribute->value)>0 ){ | |
$val = strtolower( trim( $attribute->value) ); | |
if( | |
($condition == 'in' && in_array( $val, $condition_value_array) ) | |
|| ($condition == 'not_in' && !in_array( $val, $condition_value_array) ) ){ | |
$needs_update = false; | |
foreach ($attributes as $target_attribute) { | |
if( $target_attribute->key == $target_field && $target_attribute->value != $target_value ) | |
$needs_update = true; | |
} | |
if( $needs_update ){ | |
update_receiver($api_key,$list,$receiver->email,$target_field,$target_value); | |
$_SESSION['modified'] ++; | |
} | |
} | |
} | |
} | |
} | |
return; | |
} | |
} | |
} catch(Exception $e){ | |
echo 'Could not connect to the Cleverreach API.'; | |
} | |
return false; | |
} | |
/************************************** | |
* update a single receiver | |
**************************************/ | |
function update_receiver($api_key,$list,$email,$target_field,$target_value){ | |
$api = new SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl'); | |
try{ | |
$user = array( | |
'email' => $email, | |
'attributes' => array( | |
0 => array('key' => $target_field, 'value' => $target_value) | |
) | |
); | |
$result = $api->receiverUpdate($api_key, $list, $user); | |
} catch(Exception $e){ | |
echo 'Could not connect to the Cleverreach API.'; | |
} | |
return false; | |
} | |
/************************************** | |
* Display a bootstrap metabox | |
**************************************/ | |
function metabox($title, $content){ | |
?> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h3 class="panel-title"><?php echo $title; ?></h3> | |
</div> | |
<div class="panel-body"> | |
<?php echo $content; ?> | |
</div> | |
</div> | |
<?php | |
} | |
?> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment