forked from Rocketeer007/php-dynamics-crm-2011
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicsCRM2011.php
225 lines (205 loc) · 8.41 KB
/
DynamicsCRM2011.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
<?php
/**
* Dynamics CRM 2011 Connector for PHP
*
* @author Nicholas Price
*/
interface DynamicsCRM2011_Interface {
/** Default GUID for "not known" or new Entities */
const EmptyGUID = '00000000-0000-0000-0000-000000000000';
/** Maximum number of records in a single RetrieveMultiple */
const MAX_CRM_RECORDS = 5000;
}
abstract class DynamicsCRM2011 implements DynamicsCRM2011_Interface {
/** List of recognised SOAP Faults that can be returned by MS Dynamics CRM */
public static $SOAPFaultActions = Array(
'http://www.w3.org/2005/08/addressing/soap/fault',
'http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/ExecuteOrganizationServiceFaultFault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/CreateOrganizationServiceFaultFault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveOrganizationServiceFaultFault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/UpdateOrganizationServiceFaultFault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/DeleteOrganizationServiceFaultFault',
'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultipleOrganizationServiceFaultFault',
);
/* Internal details */
protected static $debugMode = FALSE;
/**
* Implementation of Class Autoloader
* See http://www.php.net/manual/en/function.spl-autoload-register.php
*
* @param String $className the name of the Class to load
*/
public static function loadClass($className){
/* Only load classes that don't exist, and are part of DynamicsCRM2011 */
if ((class_exists($className)) || (strpos($className, 'DynamicsCRM2011') === false)) {
return false;
}
/* Work out the filename of the Class to be loaded. */
$classFilePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $className . '.class.php';
/* Only try to load files that actually exist and can be read */
if ((file_exists($classFilePath) === false) || (is_readable($classFilePath) === false)) {
return false;
}
/* Don't load it if it's already been loaded */
require_once $classFilePath;
}
/**
* Utility function to get the appropriate Class name for a particular Entity.
* Note that the class may not actually exist - this function just returns
* the name of the class, which can then be used in a class_exists test.
*
* The class name is normally DynamicsCRM2011_Entity_Name_Capitalised,
* e.g. DyanmicsCRM2011_Incident, or DynamicsCRM2011_Account
*
* @param String $entityLogicalName
* @return String the name of the class
*/
public static function getClassName($entityLogicalName) {
/* Since EntityLogicalNames are usually in lowercase, we captialise each word */
$capitalisedEntityName = self::capitaliseEntityName($entityLogicalName);
$className = 'DynamicsCRM2011_'.$capitalisedEntityName;
/* Return the generated class name */
return $className;
}
/**
* Utility function to captialise the Entity Name according to the following rules:
* 1. The first letter of each word in the Entity Name is capitalised
* 2. Words are separated by underscores only
*
* @param String $entityLogicalName as it is stored in the CRM
* @return String the Entity Name as it would be in a PHP Class name
*/
private static function capitaliseEntityName($entityLogicalName) {
/* User-defined Entities generally have underscore separated names
* e.g. mycompany_special_item
* We capitalise this as Mycompany_Special_Item
*/
$words = explode('_', $entityLogicalName);
foreach($words as $key => $word) $words[$key] = ucwords(strtolower($word));
$capitalisedEntityName = implode('_', $words);
/* Return the capitalised name */
return $capitalisedEntityName;
}
/**
* Utility function to strip any Namespace from an XML attribute value
* @param String $attributeValue
* @return String Attribute Value without the Namespace
*/
protected static function stripNS($attributeValue) {
return preg_replace('/[a-zA-Z]+:([a-zA-Z]+)/', '$1', $attributeValue);
}
/**
* Get the current time, as required in XML format
* @ignore
*/
protected static function getCurrentTime() {
return substr(gmdate('c'),0,-6) . ".00";
}
/**
* Get an appropriate expiry time for the XML requests, as required in XML format
* @ignore
*/
protected static function getExpiryTime() {
return substr(gmdate('c', strtotime('+1 minute')),0,-6) . ".00";
}
/**
* Enable or Disable DEBUG for the Class
* @ignore
*/
public static function setDebug($_debugMode) {
self::$debugMode = $_debugMode;
}
/**
* Utility function to parse time from XML - includes handling Windows systems with no strptime
* @param String $timestamp
* @param String $formatString
* @return integer PHP Timestamp
* @ignore
*/
protected static function parseTime($timestamp, $formatString) {
/* Quick solution: use strptime */
if(function_exists("strptime") == true) {
$time_array = strptime($timestamp, $formatString);
} else {
$masks = Array(
'%d' => '(?P<d>[0-9]{2})',
'%m' => '(?P<m>[0-9]{2})',
'%Y' => '(?P<Y>[0-9]{4})',
'%H' => '(?P<H>[0-9]{2})',
'%M' => '(?P<M>[0-9]{2})',
'%S' => '(?P<S>[0-9]{2})',
// usw..
);
$rexep = "#".strtr(preg_quote($formatString), $masks)."#";
if(!preg_match($rexep, $timestamp, $out)) return false;
$time_array = Array(
"tm_sec" => (int) $out['S'],
"tm_min" => (int) $out['M'],
"tm_hour" => (int) $out['H'],
"tm_mday" => (int) $out['d'],
"tm_mon" => $out['m']?$out['m']-1:0,
"tm_year" => $out['Y'] > 1900 ? $out['Y'] - 1900 : 0,
);
}
$phpTimestamp = gmmktime($time_array['tm_hour'], $time_array['tm_min'], $time_array['tm_sec'],
$time_array['tm_mon']+1, $time_array['tm_mday'], 1900+$time_array['tm_year']);
return $phpTimestamp;
}
/**
* Add a list of Formatted Values to an Array of Attributes, using appropriate handling
* avoiding over-writing existing attributes already in the array
*
* Optionally specify an Array of sub-keys, and a particular sub-key
* - If provided, each sub-key in the Array will be created as an Object attribute,
* and the value will be set on the specified sub-key only (e.g. (New, Old) / New)
*
* @ignore
*/
protected static function addFormattedValues(Array &$targetArray, DOMNodeList $keyValueNodes, Array $keys = NULL, $key1 = NULL) {
foreach ($keyValueNodes as $keyValueNode) {
/* Get the Attribute name (key) */
$attributeKey = $keyValueNode->getElementsByTagName('key')->item(0)->textContent;
$attributeValue = $keyValueNode->getElementsByTagName('value')->item(0)->textContent;
/* If we are working normally, just store the data in the array */
if ($keys == NULL) {
/* Assume that if there is a duplicate, it's an un-formatted version of this */
if (array_key_exists($attributeKey, $targetArray)) {
$targetArray[$attributeKey] = (Object)Array(
'Value' => $targetArray[$attributeKey],
'FormattedValue' => $attributeValue
);
} else {
$targetArray[$attributeKey] = $attributeValue;
}
} else {
/* Store the data in the array for this AuditRecord's properties */
if (array_key_exists($attributeKey, $targetArray)) {
/* We assume it's already a "good" Object, and just set this key */
if (isset($targetArray[$attributeKey]->$key1)) {
/* It's already set, so add the Formatted version */
$targetArray[$attributeKey]->$key1 = (Object)Array(
'Value' => $targetArray[$attributeKey]->$key1,
'FormattedValue' => $attributeValue);
} else {
/* It's not already set, so just set this as a value */
$targetArray[$attributeKey]->$key1 = $attributeValue;
}
} else {
/* We need to create the Object */
$obj = (Object)Array();
foreach ($keys as $k) {
$obj->$k = NULL;
}
/* And set the particular property */
$obj->$key1 = $attributeValue;
/* And store the Object in the target Array */
$targetArray[$attributeKey] = $obj;
}
}
}
}
}
/* Register the Class Loader */
spl_autoload_register(Array('DynamicsCRM2011', 'loadClass'));
?>