-
Notifications
You must be signed in to change notification settings - Fork 13
/
ETCore.class.php
367 lines (323 loc) · 8.8 KB
/
ETCore.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* ETCore
*
* Core the api wrapper that provides all the base functionality
*
* @package ExactTargetLib
* @author Henning Glatter-Gotz
*/
class ETCore
{
/**
* Hydrate results as arrays
*/
const HYDRATE_ARRAY = 1;
/**
* Hydrate results as records (objects)
*/
const HYDRATE_RECORD = 2;
/**
* @var ETCore holds an instance of ETCore (singleton)
*/
protected static $instance = null;
/**
* @var string The username for the ExactTarget account
*/
protected $userName;
/**
* @var string The password for the ExactTarget account
*/
protected $password;
/**
* @var string The wsdl url
*/
protected $wsdl = 'https://webservice.exacttarget.com/etframework.wsdl';
/**
* Create an instance of the ETCore class and store it in the static instance
* variable.
* Essentially this allows for setting the ExactTarget credentials once per
* script.
*
* @param string $userName
* @param string $password
*/
public static function initialize($userName, $password)
{
$instance = new self();
$instance->userName = $userName;
$instance->password = $password;
self::$instance = $instance;
}
/**
* Return an instance of the fully configured ExactTarget SOAP client.
*
* @return ExactTargetSoapClient
*/
public static function getClient()
{
if (self::$instance === null)
{
throw new ETException('Exact Target environment is not initialized!');
}
$client = new ExactTargetSoapClient(self::$instance->wsdl, array('trace' => 0, 'soap_version' => SOAP_1_1));
$client->username = self::$instance->userName;
$client->password = self::$instance->password;
return $client;
}
/**
* ExactTarget SOAP update that allows the update action to be specified.
*
* @param array $objects Array of SOAP objects to be updated
* @param integet $saveAction A constant integer specifying the action.
*
* @return ExactTarget SOAP response
* $throws Exception
*/
protected static function _update($objects, $saveAction)
{
try
{
$soapClient = self::getClient();
$uo = new ExactTarget_UpdateOptions();
$uo->SaveOptions = array();
$so = new ExactTarget_SaveOption();
$so->PropertyName = '*';
$so->SaveAction = $saveAction;
$uo->SaveOptions[] = $so;
$uoSo = ETCore::toSoapVar($uo, 'UpdateOptions');
$request = new ExactTarget_UpdateRequest();
$request->Options = $uoSo;
$request->Objects = $objects;
return $soapClient->Update($request);
}
catch (Exception $e)
{
throw new ETException(__METHOD__ . ':' . __LINE__ . '|' . $e->getMessage());
}
}
/**
* Generic upsert operation. Updates existing records and inserts them if they
* do not yet exist.
*
* @param type $objects
* @return type
*/
public static function upsert($objects)
{
return self::_update($objects, ExactTarget_SaveAction::UpdateAdd);
}
/**
* Explicit update operation.
*
* @param type $objects
* @return type
*/
public static function update($objects)
{
return self::_update($objects, ExactTarget_SaveAction::UpdateOnly);
}
/**
* Delete objects
*
* @param type $objects
*/
public static function delete($objects)
{
try
{
$soapClient = self::getClient();
$request = new ExactTarget_DeleteRequest();
$request->Options = null;
$request->Objects = $objects;
return $soapClient->Delete($request);
}
catch (Exception $e)
{
throw new ETException(__METHOD__ . ':' . __LINE__ . '|' . $e->getMessage());
}
}
/**
* Utility method for parsing the SOAP response and throwing an exception with
* a the appropriate error message.
*
* @param type $result
*/
public static function evaluateSoapResult($result)
{
switch ($result->OverallStatus)
{
case 'OK':
break;
case 'MoreDataAvailable':
break;
default:
$msg = '';
if (property_exists($result, 'Results'))
{
if (is_array($result->Results))
{
$errors = array();
foreach ($result->Results as $res)
{
$errors[] = 'StatusCode='.$res->StatusCode.'|ErrorMessage='.$res->ErrorMessage;
}
$msg = print_r($errors, true);
}
else if ((is_object($result->Results)) && (property_exists($result->Results, 'StatusCode')))
{
$msg = 'StatusCode='.$result->Results->StatusCode.'|StatusMessage='.$result->Results->StatusMessage;
}
else
{
$msg = $result->OverallStatus;
}
}
else
{
$msg = $result->OverallStatus;
}
throw new ETException($msg);
break;
}
}
/**
* Encode an object as a SOAP variable.
*
* @param type $object
* @param type $name
* @return SoapVar
*/
public static function toSoapVar($object, $name)
{
return new SoapVar($object, SOAP_ENC_OBJECT, $name, 'http://exacttarget.com/wsdl/partnerAPI');
}
/**
* newDateRangeFilter
*
* Create a ComplexFilter part for a date range that includes the from and to
* date in the range.
* The result is a ComplexFilterPart packaged as a SoapVar
*
* @param string $fromDate The start date of the date range
* @param string $toDate The end date of the date range
* @param string $property The name of the property to create the filter for
* @static
* @access public
* @return SoapVar Instance of a ComplexFilterPart packaged as a SoapVar
*/
public static function newDateRangeFilter($fromDate, $toDate, $property)
{
$fFromDate = new ExactTarget_SimpleFilterPart();
$fFromDate->Property = $property;
$fFromDate->DateValue = date(DATE_ATOM, strtotime($fromDate));
$fFromDate->SimpleOperator = ExactTarget_SimpleOperators::greaterThanOrEqual;
$fToDate = new ExactTarget_SimpleFilterPart();
$fToDate->Property = $property;
$fToDate->DateValue = date(DATE_ATOM, strtotime($toDate));
$fToDate->SimpleOperator = ExactTarget_SimpleOperators::lessThanOrEqual;
$cfDate = new ExactTarget_ComplexFilterPart();
$cfDate->LeftOperand = self::toSoapVar($fFromDate, 'SimpleFilterPart');
$cfDate->LogicalOperator = ExactTarget_LogicalOperators::_AND;
$cfDate->RightOperand = self::toSoapVar($fToDate, 'SimpleFilterPart');
return self::toSoapVar($cfDate, 'ComplexFilterPart');
}
/**
* Get the version history from the ET SOAP API.
*
* @return type
*/
public static function getVersionHistory()
{
try
{
$client = self::getClient();
$param = new ExactTarget_VersionInfoRequestMsg();
$param->IncludeVersionHistory = True;
return $client->VersionInfo($param);
}
catch (Exception $e)
{
throw $e;
}
}
/**
* Get the ET system status.
*
* @return type
*/
public static function getSystemStatus()
{
try
{
$client = self::getClient();
$param = new ExactTarget_SystemStatusRequestMsg();
return $client->GetSystemStatus($param);
}
catch (Exception $e)
{
throw $e;
}
}
/**
* getObjectDefinition
*
* Retrieve the definition of an object from the ExactTarget SOAP service.
* This can be used to get detailed information on each property of an
* object, such as the value of IsRetrievable.
*
* @param string $objectName
* @static
* @access public
* @return array
*/
public static function getObjectDefinition($objectName)
{
try
{
$client = self::getClient();
$request = new ExactTarget_ObjectDefinitionRequest();
$request->ObjectType= $objectName;
$defRqstMsg = new ExactTarget_DefinitionRequestMsg();
$defRqstMsg->DescribeRequests[] = self::toSoapVar($request, 'ObjectDefinitionRequest');
$status = $client->Describe($defRqstMsg);
return $status->ObjectDefinition;
}
catch (Exception $e)
{
throw $e;
}
}
/**
* Utility method that reduces code volume by constructing an APIProperty
* object.
*
* @param string $name The name of the property
* @param string $value The value of the property
*
* @return ExactTarget_APIProperty
*/
public static function newAPIProperty($name, $value)
{
$prop = new ExactTarget_APIProperty();
$prop->Name = $name;
$prop->Value = $value;
return $prop;
}
/**
* Utility method that reduces code volume by constructing an Attribute
* object.
*
* @param string $name The name of the attribute
* @param string $value The value of the attribute
*
* @return ExactTarget_Attribute
*/
public static function newAttribute($name, $value)
{
$attr = new ExactTarget_Attribute();
$attr->Name = $name;
$attr->Value = $value;
return $attr;
}
}