| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class apiwrapper{ |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * @var string api username |
|---|
| 7 | */ |
|---|
| 8 | var $username; |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * @var string api password |
|---|
| 12 | */ |
|---|
| 13 | var $password; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * @var string the hostname of the target script |
|---|
| 17 | */ |
|---|
| 18 | var $apiHostname; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * @var string the url path from $apiHostname to the target script |
|---|
| 22 | */ |
|---|
| 23 | var $apiUrl; |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * @var integer Custom port |
|---|
| 27 | */ |
|---|
| 28 | var $port = NULL; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * @var bool Whether or not to use an ssl connection |
|---|
| 32 | */ |
|---|
| 33 | var $secure = false; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * @var integer size (in bytes) of each fread |
|---|
| 37 | */ |
|---|
| 38 | var $chunkSize = 8192; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * @var integer time (in seconds) of connection timeout |
|---|
| 42 | */ |
|---|
| 43 | var $timeout = 300; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /* |
|---|
| 47 | * function __construct |
|---|
| 48 | * |
|---|
| 49 | * @param string $hostname The hostname of the target script |
|---|
| 50 | * @param string $urlPath The location of the target script in relation to $hostname (usually something like 'modules/api/api_json.php'). |
|---|
| 51 | * @param string $username The username of an user with portal access |
|---|
| 52 | * @param string $password The password of the same user with portal access |
|---|
| 53 | * @param bool $secure Whether or not to transfer data over ssl. |
|---|
| 54 | * @param integer $port If there is a non-standard port (i.e. different than 80 if not using an ssl connection, and different than 443 if using an ssl connection). |
|---|
| 55 | */ |
|---|
| 56 | |
|---|
| 57 | public function __construct($hostname, $urlPath, $username, $password, $secure = false, $port = NULL) { |
|---|
| 58 | |
|---|
| 59 | $this->apiHostname = $hostname; |
|---|
| 60 | $this->apiUrl = $urlPath; |
|---|
| 61 | $this->username = $username; |
|---|
| 62 | $this->password = $password; |
|---|
| 63 | $this->secure = (bool) $secure; |
|---|
| 64 | if($port === 0 || (int)$port > 0) |
|---|
| 65 | $this->port = $port; |
|---|
| 66 | |
|---|
| 67 | }//end function |
|---|
| 68 | |
|---|
| 69 | /* |
|---|
| 70 | * function ping |
|---|
| 71 | * @param string $tabledefuuid A tabledef uuid. |
|---|
| 72 | * |
|---|
| 73 | * @return array|bool An associative array containing the response, or false if a connection error has occured (in which case see $this->errorMessage). |
|---|
| 74 | * @returnf string type The type of response. This will either be 'error' or 'message'. |
|---|
| 75 | * @returnf string message The related message. This will be 'Everything is phpBMSy!' if no errors have occurred and the tabledefuuid is api accessible. |
|---|
| 76 | */ |
|---|
| 77 | |
|---|
| 78 | function ping($tabledefuuid) { |
|---|
| 79 | |
|---|
| 80 | $params["request"][0]["tabledefid"] = $tabledefuuid; |
|---|
| 81 | $params["request"][0]["command"] = "ping"; |
|---|
| 82 | $params["request"][0]["data"] = array(); |
|---|
| 83 | |
|---|
| 84 | $response = $this->_callServer($params); |
|---|
| 85 | |
|---|
| 86 | if($response !== false) |
|---|
| 87 | return $response[0]; |
|---|
| 88 | else |
|---|
| 89 | return false; |
|---|
| 90 | |
|---|
| 91 | }//end funciton |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /* |
|---|
| 95 | * function insertRecords |
|---|
| 96 | * |
|---|
| 97 | * @param string $tabledefuuid The uuid of the tabledefinition that you wish to insert records. |
|---|
| 98 | * @param array $data An array of associative arrays consising of fieldname => value pairs. |
|---|
| 99 | * @param bool $generateUuid Whether to generate a new uuid for the inserted record (and ignore any passed uuid field). |
|---|
| 100 | * @param bool $keepDestId This option dictates whether or not to keep the destination's id field if "replacing" (via the mysql replace) when there is no id field set. |
|---|
| 101 | * @param string $dateFormat The format of the dates (if any) in $data. Possible choices are : 'SQL', 'English, US', 'English, UK', or 'Dutch, NL'. If none of these are chosen, it will default to 'SQL'. |
|---|
| 102 | * @param string $timeFormat The format of the times (if any) in $data. Possible choices are : '24 Hour' or '12 Hour'. If none of these are chosen, it will default to '24 Hour'. |
|---|
| 103 | * |
|---|
| 104 | * |
|---|
| 105 | * @return array An array of associative arrays of responses for each insert |
|---|
| 106 | * @returnf array Associative array (integer key) |
|---|
| 107 | * @returnff string type The result of the individual insert (either 'added' if successful, or 'error' if not). |
|---|
| 108 | * @returnff string message The detailed message describing the result |
|---|
| 109 | * @returnff string extras The uuid of the inserted record (or the integer id if the 'useUuid' option is false). <em>Note:</em> his field only exists if type is not 'error'. |
|---|
| 110 | */ |
|---|
| 111 | |
|---|
| 112 | public function insertRecords($tabledefuuid, $data, $generateUuid = true, $keepDestId = true, $dateFormat = NULL, $timeFormat = NULL) { |
|---|
| 113 | |
|---|
| 114 | if($generateUuid !== true) |
|---|
| 115 | $generateUuid = false; |
|---|
| 116 | |
|---|
| 117 | if($keepDestId !== true) |
|---|
| 118 | $keepDestId = false; |
|---|
| 119 | |
|---|
| 120 | switch($dateFormat){ |
|---|
| 121 | |
|---|
| 122 | case "SQL": |
|---|
| 123 | case "English, US": |
|---|
| 124 | case "English, UK": |
|---|
| 125 | case "Dutch, NL": |
|---|
| 126 | break; |
|---|
| 127 | |
|---|
| 128 | default: |
|---|
| 129 | $dateFormat = "SQL"; |
|---|
| 130 | break; |
|---|
| 131 | |
|---|
| 132 | }//end switch |
|---|
| 133 | |
|---|
| 134 | switch($timeFormat){ |
|---|
| 135 | |
|---|
| 136 | case "24 Hour": |
|---|
| 137 | case "12 Hour": |
|---|
| 138 | break; |
|---|
| 139 | |
|---|
| 140 | default: |
|---|
| 141 | $timeFormat = "24 Hour"; |
|---|
| 142 | break; |
|---|
| 143 | |
|---|
| 144 | }//end switch |
|---|
| 145 | |
|---|
| 146 | $options = array( |
|---|
| 147 | "useUuid" => $generateUuid, |
|---|
| 148 | "dateFormat" => $dateFormat, |
|---|
| 149 | "timeFormat" => $timeFormat, |
|---|
| 150 | "keepDestId" => $keepDestId |
|---|
| 151 | ); |
|---|
| 152 | |
|---|
| 153 | return $this->_runTableCommnad("insert", $tabledefuuid, $data, $options); |
|---|
| 154 | |
|---|
| 155 | }//end function |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | /* |
|---|
| 159 | * function updateRecords |
|---|
| 160 | * |
|---|
| 161 | * @param string $tabledefuuid The uuid of the tabledefinition that you wish to update records. |
|---|
| 162 | * @param array $data An array of associative arrays consising of fieldname => value pairs. |
|---|
| 163 | * @param bool $useUuid Whether to use the id (false) or the uuid (true) in the update whereclause. |
|---|
| 164 | * @param string $dateFormat The format of the dates (if any) in $data. Possible choices are : 'SQL', 'English, US', 'English, UK', or 'Dutch, NL'. If none of these are chosen, it will default to 'SQL'. |
|---|
| 165 | * @param string $timeFormat The format of the times (if any) in $data. Possible choices are : '24 Hour' or '12 Hour'. If none of these are chosen, it will default to '24 Hour'. |
|---|
| 166 | * |
|---|
| 167 | * @return array An array of associative arrays of responses for each update |
|---|
| 168 | * @returnf array Associative array (integer key) |
|---|
| 169 | * @returnff string type The result of the individual update (either 'updated' if successful, or 'error' if not). |
|---|
| 170 | * @returnff string message The detailed message describing the result |
|---|
| 171 | */ |
|---|
| 172 | |
|---|
| 173 | public function updateRecords($tabledefuuid, $data, $useUuid = true, $dateFormat = NULL, $timeFormat = NULL) { |
|---|
| 174 | |
|---|
| 175 | if($useUuid !== true) |
|---|
| 176 | $useUuid = false; |
|---|
| 177 | |
|---|
| 178 | switch($dateFormat){ |
|---|
| 179 | |
|---|
| 180 | case "SQL": |
|---|
| 181 | case "English, US": |
|---|
| 182 | case "English, UK": |
|---|
| 183 | case "Dutch, NL": |
|---|
| 184 | break; |
|---|
| 185 | |
|---|
| 186 | default: |
|---|
| 187 | $dateFormat = "SQL"; |
|---|
| 188 | break; |
|---|
| 189 | |
|---|
| 190 | }//end switch |
|---|
| 191 | |
|---|
| 192 | switch($timeFormat){ |
|---|
| 193 | |
|---|
| 194 | case "24 Hour": |
|---|
| 195 | case "12 Hour": |
|---|
| 196 | break; |
|---|
| 197 | |
|---|
| 198 | default: |
|---|
| 199 | $timeFormat = "24 Hour"; |
|---|
| 200 | break; |
|---|
| 201 | |
|---|
| 202 | }//end switch |
|---|
| 203 | |
|---|
| 204 | $options = array( |
|---|
| 205 | "useUuid" => $useUuid, |
|---|
| 206 | "dateFormat" => $dateFormat, |
|---|
| 207 | "timeFormat" => $timeFormat |
|---|
| 208 | ); |
|---|
| 209 | |
|---|
| 210 | return $this->_runTableCommnad("update", $tabledefuuid, $data, $options); |
|---|
| 211 | |
|---|
| 212 | }//end function |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | /* |
|---|
| 216 | * function getRecords |
|---|
| 217 | * |
|---|
| 218 | * @param string $tabledefuuid The uuid of the tabledefinition that you wish to get records. |
|---|
| 219 | * @param mixed $ids Either an array uuids (or ids if $useUuid = false), or an individual uuid (or id). |
|---|
| 220 | * @param bool $useUuid Whether the data is an array of uuids or ids |
|---|
| 221 | * |
|---|
| 222 | * @return array An array of associative arrays of responses for each get |
|---|
| 223 | * @returnf array Associative array (integer key) |
|---|
| 224 | * @returnff string type The result of the individual get (either 'retrieved' if successful, or 'error' if not). |
|---|
| 225 | * @returnff string message The detailed message describing the result |
|---|
| 226 | * @returnff array extras The associative array containing the record retrieved. <em>Note:</em> This field only exists if type is not 'error' AND there is a record that corresponds to the uuid/id searched for. |
|---|
| 227 | */ |
|---|
| 228 | |
|---|
| 229 | public function getRecords($tabledefuuid, $ids, $useUuid = true) { |
|---|
| 230 | |
|---|
| 231 | if($useUuid !== true) |
|---|
| 232 | $useUuid = false; |
|---|
| 233 | |
|---|
| 234 | if($useUuid) |
|---|
| 235 | $keyName = "uuid"; |
|---|
| 236 | else |
|---|
| 237 | $keyName = "id"; |
|---|
| 238 | |
|---|
| 239 | if(!is_array($ids)) |
|---|
| 240 | $ids = array($ids); |
|---|
| 241 | |
|---|
| 242 | $data = array(); |
|---|
| 243 | foreach($ids as $id){ |
|---|
| 244 | |
|---|
| 245 | if($useUuid) |
|---|
| 246 | $id = (string)$id; |
|---|
| 247 | else |
|---|
| 248 | $id = (int)$id; |
|---|
| 249 | $data[][$keyName] = $id; |
|---|
| 250 | |
|---|
| 251 | }//end foreach |
|---|
| 252 | |
|---|
| 253 | $options = array( |
|---|
| 254 | "useUuid" => $useUuid |
|---|
| 255 | ); |
|---|
| 256 | |
|---|
| 257 | return $this->_runTableCommnad("get", $tabledefuuid, $data, $options); |
|---|
| 258 | |
|---|
| 259 | }//end function |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | /** |
|---|
| 263 | * function getDefaults |
|---|
| 264 | * |
|---|
| 265 | * @param string $tabledefuuid The uuid of the tabledefinition that you wish to get defaults. |
|---|
| 266 | * |
|---|
| 267 | * @return array An associative array response for the get |
|---|
| 268 | * @returnf string type The result of the procedure (either 'retrieved' if successful, or 'error' if not). |
|---|
| 269 | * @returnf string message The detailed message describing the result |
|---|
| 270 | * @returnf array extras The associative array containing the default record. <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 271 | */ |
|---|
| 272 | |
|---|
| 273 | public function getDefaults($tabledefuuid){ |
|---|
| 274 | |
|---|
| 275 | $params["request"][0]["command"] = "getDefaults"; |
|---|
| 276 | $params["request"][0]["data"] = ""; |
|---|
| 277 | $params["request"][0]["tabledefid"] = $tabledefuuid; |
|---|
| 278 | |
|---|
| 279 | $response = $this->_callServer($params); |
|---|
| 280 | |
|---|
| 281 | if($response !== false) |
|---|
| 282 | return $response[0]; |
|---|
| 283 | else |
|---|
| 284 | return false; |
|---|
| 285 | |
|---|
| 286 | }//end function getDefaults |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | /* |
|---|
| 290 | * function deleteRecords |
|---|
| 291 | * |
|---|
| 292 | * @param string $tabledefuuid The uuid of the tabledefinition that you wish to delete (or inactivate, depending upon the tabledef) records. |
|---|
| 293 | * @param mixed $ids Either an array uuids (or ids if $useUuid is false), or an individual uuid (or id). |
|---|
| 294 | * @param bool $useUuid Whether the data is an array of uuids or ids. |
|---|
| 295 | * |
|---|
| 296 | * @return array An array of associative arrays of responses for each delete |
|---|
| 297 | * @returnf array Associative array (integer key) |
|---|
| 298 | * @returnff string type The result of the individual delete (either 'delete' or 'inactivate' (depending upon the tabledef) if successful, or 'error' if not). <em>Note:</em> The successful type (i.e. the non-error type) can possibly be values other than 'delete' or 'inactivate' depending upon the table definition. |
|---|
| 299 | * @returnff string message The detailed message describing the result |
|---|
| 300 | */ |
|---|
| 301 | |
|---|
| 302 | public function deleteRecords($tabledefuuid, $ids, $useUuid = true) { |
|---|
| 303 | |
|---|
| 304 | if($useUuid !== true) |
|---|
| 305 | $useUuid = false; |
|---|
| 306 | |
|---|
| 307 | if($useUuid) |
|---|
| 308 | $keyName = "uuid"; |
|---|
| 309 | else |
|---|
| 310 | $keyName = "id"; |
|---|
| 311 | |
|---|
| 312 | if(!is_array($ids)) |
|---|
| 313 | $ids = array($ids); |
|---|
| 314 | |
|---|
| 315 | $data = array(); |
|---|
| 316 | foreach($ids as $id){ |
|---|
| 317 | |
|---|
| 318 | if($useUuid) |
|---|
| 319 | $id = (string)$id; |
|---|
| 320 | else |
|---|
| 321 | $id = (int)$id; |
|---|
| 322 | $data[][$keyName] = $id; |
|---|
| 323 | |
|---|
| 324 | }//end foreach |
|---|
| 325 | |
|---|
| 326 | $options = array( |
|---|
| 327 | "useUuid" => $useUuid |
|---|
| 328 | ); |
|---|
| 329 | |
|---|
| 330 | return $this->_runTableCommnad("delete", $tabledefuuid, $data, $options); |
|---|
| 331 | |
|---|
| 332 | }//end function |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | /* |
|---|
| 336 | * function runStoredProcedure |
|---|
| 337 | * |
|---|
| 338 | * @param string $tabledefuuid The uuid of an api accessible tabledefinition. |
|---|
| 339 | * @param string $procedureName The stored procedure to be called. |
|---|
| 340 | * |
|---|
| 341 | * @return array An associative array response for the procedure |
|---|
| 342 | * @returnf string type The result of the procedure (either 'result' if successful, or 'error' if not). |
|---|
| 343 | * @returnf string message The detailed message describing the result |
|---|
| 344 | */ |
|---|
| 345 | |
|---|
| 346 | public function runStoredProcedure($tabledefuuid, $procedureName) { |
|---|
| 347 | |
|---|
| 348 | $params["request"][0]["command"] = "procedure"; |
|---|
| 349 | $params["request"][0]["data"]["name"] = $procedurename; |
|---|
| 350 | $params["request"][0]["tabledefid"] = $tabledefuuid; |
|---|
| 351 | |
|---|
| 352 | $response = $this->_callServer($params); |
|---|
| 353 | |
|---|
| 354 | if($response !== false) |
|---|
| 355 | return $response[0]; |
|---|
| 356 | else |
|---|
| 357 | return false; |
|---|
| 358 | |
|---|
| 359 | }//end function |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | /* |
|---|
| 363 | * function getSetting |
|---|
| 364 | * |
|---|
| 365 | * @param array $settings Array of settings names |
|---|
| 366 | * |
|---|
| 367 | * @return array An associative array response for the get |
|---|
| 368 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 369 | * @returnf string message The detailed message describing the result |
|---|
| 370 | * @returnf array extras The associative array containing the settings retrieved. <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 371 | */ |
|---|
| 372 | |
|---|
| 373 | public function getSettings($settings) { |
|---|
| 374 | |
|---|
| 375 | $params["request"][0]["command"] = "getsettings"; |
|---|
| 376 | $params["request"][0]["data"] = $settings; |
|---|
| 377 | |
|---|
| 378 | $response = $this->_callServer($params); |
|---|
| 379 | |
|---|
| 380 | if($response !== false) |
|---|
| 381 | return $response[0]; |
|---|
| 382 | else |
|---|
| 383 | return false; |
|---|
| 384 | |
|---|
| 385 | }//end function |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | /* |
|---|
| 389 | * function searchClientByEmail |
|---|
| 390 | * @param string $email The email to be searched for |
|---|
| 391 | * @param bool $returnUuid Whether to return uuids or ids. |
|---|
| 392 | * |
|---|
| 393 | * @return array An associative array response for the get |
|---|
| 394 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 395 | * @returnf string message The detailed message describing the result |
|---|
| 396 | * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'returnUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 397 | */ |
|---|
| 398 | |
|---|
| 399 | public function searchClientByEmail($email, $returnUuid = true) { |
|---|
| 400 | |
|---|
| 401 | $method = "api_searchByEmail"; |
|---|
| 402 | $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; |
|---|
| 403 | $data["email"] = (string)$email; |
|---|
| 404 | |
|---|
| 405 | if($returnUuid !== true) |
|---|
| 406 | $returnUuid = false; |
|---|
| 407 | |
|---|
| 408 | $options = array( |
|---|
| 409 | "useUuid" => $returnUuid |
|---|
| 410 | ); |
|---|
| 411 | |
|---|
| 412 | $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); |
|---|
| 413 | |
|---|
| 414 | if($response !== false) |
|---|
| 415 | return $response[0]; |
|---|
| 416 | else |
|---|
| 417 | return false; |
|---|
| 418 | |
|---|
| 419 | }//end function |
|---|
| 420 | |
|---|
| 421 | /* |
|---|
| 422 | * function searchClientByNameAndPostalcode |
|---|
| 423 | * @param name $firstname The first name to search for in the client's table |
|---|
| 424 | * @param name $lastname The last name to search for in the client's table |
|---|
| 425 | * @param name $postalcode The postal code to search for in the client's table |
|---|
| 426 | * @param bool $returnUuid Whether to return uuids or ids. |
|---|
| 427 | * |
|---|
| 428 | * @return array An associative array response for the get |
|---|
| 429 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 430 | * @returnf string message The detailed message describing the result |
|---|
| 431 | * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'returnUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 432 | */ |
|---|
| 433 | |
|---|
| 434 | public function searchClientByNameAndPostalcode($firstname, $lastname, $postalcode, $returnUuid = true) { |
|---|
| 435 | |
|---|
| 436 | $method = "api_searchByNameAndPostalcode"; |
|---|
| 437 | $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; |
|---|
| 438 | $data["firstname"] = (string)$firstname; |
|---|
| 439 | $data["lastname"] = (string)$lastname; |
|---|
| 440 | $data["postalcode"] = (string)$postalcode; |
|---|
| 441 | |
|---|
| 442 | if($returnUuid !== true) |
|---|
| 443 | $returnUuid = false; |
|---|
| 444 | |
|---|
| 445 | $options = array( |
|---|
| 446 | "useUuid" => $returnUuid |
|---|
| 447 | ); |
|---|
| 448 | |
|---|
| 449 | $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); |
|---|
| 450 | |
|---|
| 451 | if($response !== false) |
|---|
| 452 | return $response[0]; |
|---|
| 453 | else |
|---|
| 454 | return false; |
|---|
| 455 | |
|---|
| 456 | }//end function |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | /* |
|---|
| 460 | * function searchClientByUsernameAndPassword |
|---|
| 461 | * @param string $username The username to search for in the client's table |
|---|
| 462 | * @param string $password The password to search for in the client's table |
|---|
| 463 | * @param bool $returnUuid Whether to return uuids or ids. |
|---|
| 464 | * |
|---|
| 465 | * @return array An associative array response for the get |
|---|
| 466 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 467 | * @returnf string message The detailed message describing the result |
|---|
| 468 | * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'returnUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 469 | */ |
|---|
| 470 | |
|---|
| 471 | public function searchClientByUsernameAndPassword($username, $password, $returnUuid = true) { |
|---|
| 472 | |
|---|
| 473 | $method = "api_searchByUsernameAndPassword"; |
|---|
| 474 | $tabledefuuid = "tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"; |
|---|
| 475 | $data["username"] = (string)$username; |
|---|
| 476 | $data["password"] = (string)$password; |
|---|
| 477 | |
|---|
| 478 | if($returnUuid !== true) |
|---|
| 479 | $returnUuid = false; |
|---|
| 480 | |
|---|
| 481 | $options = array( |
|---|
| 482 | "useUuid" => $returnUuid |
|---|
| 483 | ); |
|---|
| 484 | |
|---|
| 485 | $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); |
|---|
| 486 | |
|---|
| 487 | if($response !== false) |
|---|
| 488 | return $response[0]; |
|---|
| 489 | else |
|---|
| 490 | return false; |
|---|
| 491 | |
|---|
| 492 | }//end function |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | /* |
|---|
| 496 | * function searchSalesOrdersByClientUuid |
|---|
| 497 | * @param string $clientuuid The uuid of a client record |
|---|
| 498 | * @param string $ordertype The type of the sales order. Possible types are :'Quote','Order','Invoice','VOID' |
|---|
| 499 | * @param string $startdate The sql encoded DATETIME lower range of creation dates. |
|---|
| 500 | * @param string $enddate The sql encoded DATETIME upper range of creation dates. |
|---|
| 501 | * @param bool $returnUuid Whether to return uuids or ids. |
|---|
| 502 | * |
|---|
| 503 | * @return array An associative array response for the get |
|---|
| 504 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 505 | * @returnf string message The detailed message describing the result |
|---|
| 506 | * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'returnUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 507 | */ |
|---|
| 508 | |
|---|
| 509 | function searchSalesOrdersByClientUuid($clientuuid, $ordertype = NULL, $startdate = NULL, $enddate = NULL, $returnUuid = true) { |
|---|
| 510 | |
|---|
| 511 | $method = "api_searchByClientUuid"; |
|---|
| 512 | $tabledefuuid = "tbld:62fe599d-c18f-3674-9e54-b62c2d6b1883"; |
|---|
| 513 | $data["clientid"] = $clientuuid; |
|---|
| 514 | if($ordertype !== NULL) |
|---|
| 515 | $data["type"] = $ordertype; |
|---|
| 516 | if($startdate !== NULL) |
|---|
| 517 | $data["startdate"] = $startdate; |
|---|
| 518 | if($enddate !== NULL) |
|---|
| 519 | $data["enddate"] = $enddate; |
|---|
| 520 | |
|---|
| 521 | if($returnUuid !== true) |
|---|
| 522 | $returnUuid = false; |
|---|
| 523 | |
|---|
| 524 | $options = array( |
|---|
| 525 | "useUuid" => $returnUuid |
|---|
| 526 | ); |
|---|
| 527 | |
|---|
| 528 | $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); |
|---|
| 529 | |
|---|
| 530 | if($response !== false) |
|---|
| 531 | return $response[0]; |
|---|
| 532 | else |
|---|
| 533 | return false; |
|---|
| 534 | |
|---|
| 535 | }//end function |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | /* |
|---|
| 539 | * function searchProductsByPartnumber |
|---|
| 540 | * @param $partnumber |
|---|
| 541 | * @param $webenabled |
|---|
| 542 | * @param $inactive |
|---|
| 543 | * @param $returnUuid |
|---|
| 544 | * |
|---|
| 545 | * @return array An associative array response for the get |
|---|
| 546 | * @returnf string type The result of the get (either 'result' if successful, or 'error' if not). |
|---|
| 547 | * @returnf string message The detailed message describing the result |
|---|
| 548 | * @returnf array extras If the type is 'result', this will be a (possibly empty) array of uuids (or ids if the 'returnUuid' option is false). <em>Note:</em> This field only exists if type is not 'error'. |
|---|
| 549 | */ |
|---|
| 550 | |
|---|
| 551 | public function searchProductsByPartnumber($partnumber, $webenabled = true, $inactive = false, $returnUuid = true) { |
|---|
| 552 | |
|---|
| 553 | $method = "api_searchByPartNumber"; |
|---|
| 554 | $tabledefuuid = "tbld:7a9e87ed-d165-c4a4-d9b9-0a4adc3c5a34"; |
|---|
| 555 | $data["partnumber"] = (string)$partnumber; |
|---|
| 556 | $data["webenabled"] = (bool)$webenabled; |
|---|
| 557 | $data["inactive"] = (bool)$inactive; |
|---|
| 558 | |
|---|
| 559 | $returnUuid = (bool)$returnUuid; |
|---|
| 560 | |
|---|
| 561 | $options = array( |
|---|
| 562 | "useUuid" => $returnUuid |
|---|
| 563 | ); |
|---|
| 564 | |
|---|
| 565 | $response = $this->runApiMethod($method, $tabledefuuid, $data, $options); |
|---|
| 566 | |
|---|
| 567 | if($response !== false) |
|---|
| 568 | return $response[0]; |
|---|
| 569 | else |
|---|
| 570 | return false; |
|---|
| 571 | |
|---|
| 572 | }//end function |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | /* |
|---|
| 576 | * function getFile |
|---|
| 577 | * @param string $url The full url to the api_servfile.php url. This can be retrieved from the getRecords method on a `files` table get (the 'apifileurl' key in the record). |
|---|
| 578 | * |
|---|
| 579 | * @return mixed The file if successful, false, if not. |
|---|
| 580 | */ |
|---|
| 581 | |
|---|
| 582 | public function getFile($url) { |
|---|
| 583 | |
|---|
| 584 | $urlInfo = parse_url($url); |
|---|
| 585 | |
|---|
| 586 | $oldHost = $this->apiHostname; |
|---|
| 587 | $oldPort = $this->port; |
|---|
| 588 | $oldUrl = $this->apiUrl; |
|---|
| 589 | |
|---|
| 590 | $this->apiHostname = $urlInfo["host"]; |
|---|
| 591 | if($port === 0 || (int)$port > 0) |
|---|
| 592 | $this->port = $urlInfo["port"]; |
|---|
| 593 | else |
|---|
| 594 | $this->port = NULL; |
|---|
| 595 | $this->apiUrl = $urlInfo["path"]."?".$urlInfo["query"]; |
|---|
| 596 | |
|---|
| 597 | $response = $this->_callServer(array()); |
|---|
| 598 | |
|---|
| 599 | $this->apiUrl = $oldUrl; |
|---|
| 600 | $this->apiHostname = $oldHost; |
|---|
| 601 | $this->port = $oldPort; |
|---|
| 602 | |
|---|
| 603 | if($response !== false) |
|---|
| 604 | return $response; |
|---|
| 605 | else{ |
|---|
| 606 | $this->errorMessage = ""; |
|---|
| 607 | return false; |
|---|
| 608 | }//end if |
|---|
| 609 | |
|---|
| 610 | }//end function |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | /* |
|---|
| 614 | * function runApiMethod |
|---|
| 615 | * |
|---|
| 616 | * @param string $method The name of the api method to be called. This must start with 'api_'. |
|---|
| 617 | * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. |
|---|
| 618 | * @param mixed $data Method specific data |
|---|
| 619 | * @param array $options An associative array of options. Possible options are : 'useUuid', 'dateFormat', 'timeFormat' |
|---|
| 620 | * |
|---|
| 621 | * @return mixed Response dependent upon the api command |
|---|
| 622 | */ |
|---|
| 623 | |
|---|
| 624 | public function runApiMethod($method, $tabledefuuid, $data, $options = NULL) { |
|---|
| 625 | |
|---|
| 626 | if(substr($method, 0, 4) != "api_"){ |
|---|
| 627 | |
|---|
| 628 | $this->errorMessage = "The command is not a valid api method"; |
|---|
| 629 | return false; |
|---|
| 630 | |
|---|
| 631 | }//end if |
|---|
| 632 | |
|---|
| 633 | $params["request"][0]["tabledefid"] = $tabledefuuid; |
|---|
| 634 | $params["request"][0]["options"] = $options; |
|---|
| 635 | $params["request"][0]["command"] = $method; |
|---|
| 636 | $params["request"][0]["data"] = $data; |
|---|
| 637 | |
|---|
| 638 | return $this->_callServer($params); |
|---|
| 639 | |
|---|
| 640 | }//end function |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | /* |
|---|
| 644 | * function runSearchMethod |
|---|
| 645 | * |
|---|
| 646 | * @param string $method The name of the api method to be called. This must start with 'api_'. |
|---|
| 647 | * @param string $tabledefuuid The uuid of the tabledefinition with the relevant api method. |
|---|
| 648 | * @param mixed $data Method specific data |
|---|
| 649 | * @param array $options An associative array of options. Possible options are : 'useUuid', 'dateFormat', 'timeFormat' |
|---|
| 650 | * |
|---|
| 651 | * @return array An array of associative arrays of responses for each search method |
|---|
| 652 | * @returnf array Associative array (integer key) |
|---|
| 653 | * @returnff string type The result of the individual search method (either '$method' if successful, or 'error' if not). |
|---|
| 654 | * @returnff string message The detailed message describing the result |
|---|
| 655 | * @returnff array extras The associative array containing the record retrieved. <em>Note:</em> This field only exists if type is 'error'. |
|---|
| 656 | */ |
|---|
| 657 | |
|---|
| 658 | public function runSearchMethod($method, $tabledefuuid, $data, $options = NULL) { |
|---|
| 659 | |
|---|
| 660 | if(substr($method, 0, 4) == "api_"){ |
|---|
| 661 | $this->errorMessage = "The command is not a valid search method"; |
|---|
| 662 | return false; |
|---|
| 663 | }//end if |
|---|
| 664 | |
|---|
| 665 | switch($method){ |
|---|
| 666 | |
|---|
| 667 | case "insert": |
|---|
| 668 | case "update": |
|---|
| 669 | case "delete": |
|---|
| 670 | case "procedure": |
|---|
| 671 | case "get": |
|---|
| 672 | case "getsettings": |
|---|
| 673 | $this->errorMessage = "The command is not a valid search method"; |
|---|
| 674 | return false; |
|---|
| 675 | break; |
|---|
| 676 | |
|---|
| 677 | }//end switch |
|---|
| 678 | |
|---|
| 679 | return $this->_runTableCommnad($method, $tabledefuuid, $data, $options); |
|---|
| 680 | |
|---|
| 681 | }//end function |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | /* |
|---|
| 685 | * function _runTableCommnad |
|---|
| 686 | * |
|---|
| 687 | * @param string $command The api command |
|---|
| 688 | * @param string $tabledefuuid The uuid of the tabledefinition with the relevant method. |
|---|
| 689 | * @param array $records Array of associative arrays |
|---|
| 690 | * @param array $options An associative array of options. Possible options are : 'useUuid', 'dateFormat', 'timeFormat', 'keepDestId' |
|---|
| 691 | * |
|---|
| 692 | * @return array The response from _callServer |
|---|
| 693 | */ |
|---|
| 694 | |
|---|
| 695 | private function _runTableCommnad($command, $tabledefuuid, $records, $options) { |
|---|
| 696 | |
|---|
| 697 | $params = array(); |
|---|
| 698 | $i = 0; |
|---|
| 699 | foreach($records as $record){ |
|---|
| 700 | $params["request"][$i]["tabledefid"] = $tabledefuuid; |
|---|
| 701 | $params["request"][$i]["options"] = $options; |
|---|
| 702 | $params["request"][$i]["command"] = $command; |
|---|
| 703 | $params["request"][$i]["data"] = $record; |
|---|
| 704 | $i++; |
|---|
| 705 | }//end foreach |
|---|
| 706 | |
|---|
| 707 | return $this->_callServer($params); |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | }//end function |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | /* |
|---|
| 714 | * function _encode |
|---|
| 715 | * |
|---|
| 716 | * @param array $message Message to be encoded |
|---|
| 717 | * |
|---|
| 718 | * @return string Encoded message |
|---|
| 719 | */ |
|---|
| 720 | |
|---|
| 721 | private function _encode($message) { |
|---|
| 722 | |
|---|
| 723 | return json_encode($message); |
|---|
| 724 | |
|---|
| 725 | }//end function |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | /* |
|---|
| 729 | * function _decode |
|---|
| 730 | * |
|---|
| 731 | * @param string $response Encoded api response |
|---|
| 732 | * |
|---|
| 733 | * @return array Decoded api response |
|---|
| 734 | */ |
|---|
| 735 | |
|---|
| 736 | private function _decode($response) { |
|---|
| 737 | |
|---|
| 738 | return json_decode($response, true); |
|---|
| 739 | |
|---|
| 740 | }//end function |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | /* |
|---|
| 744 | * function _callServer |
|---|
| 745 | * |
|---|
| 746 | * @param array $params |
|---|
| 747 | * |
|---|
| 748 | * @return array False if major (i.e. connection) error has occurred. Otherwise, returns an array of associative arrays. |
|---|
| 749 | */ |
|---|
| 750 | |
|---|
| 751 | private function _callServer($params) { |
|---|
| 752 | |
|---|
| 753 | $params["phpbmsusername"] = $this->username; |
|---|
| 754 | $params["phpbmspassword"] = $this->password; |
|---|
| 755 | |
|---|
| 756 | if(!isset($params["request"])) |
|---|
| 757 | $params["request"] = array(); |
|---|
| 758 | |
|---|
| 759 | $params["request"] = $this->_encode($params["request"]); |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | $this->errorMessage = ""; |
|---|
| 763 | $this->errorCode = ""; |
|---|
| 764 | |
|---|
| 765 | $post_vars = http_build_query($params); |
|---|
| 766 | |
|---|
| 767 | $payload = "POST " .$this->apiUrl. " HTTP/1.0\r\n"; |
|---|
| 768 | $payload .= "Host: " . $this->apiHostname . "\r\n"; |
|---|
| 769 | $payload .= "Content-type: application/x-www-form-urlencoded\r\n"; |
|---|
| 770 | $payload .= "Content-length: " . strlen($post_vars) . "\r\n"; |
|---|
| 771 | $payload .= "Connection: close \r\n\r\n"; |
|---|
| 772 | $payload .= $post_vars; |
|---|
| 773 | |
|---|
| 774 | ob_start(); |
|---|
| 775 | if ($this->secure){ |
|---|
| 776 | |
|---|
| 777 | if($this->port !== NULL) |
|---|
| 778 | $port = $this->port; |
|---|
| 779 | else |
|---|
| 780 | $port = 443; |
|---|
| 781 | |
|---|
| 782 | $sock = fsockopen("ssl://".$this->apiHostname, $port, $errno, $errstr, 30); |
|---|
| 783 | } else { |
|---|
| 784 | |
|---|
| 785 | if($this->port !== NULL) |
|---|
| 786 | $port = $this->port; |
|---|
| 787 | else |
|---|
| 788 | $port = 80; |
|---|
| 789 | |
|---|
| 790 | $sock = fsockopen($this->apiHostname, $port, $errno, $errstr, 30); |
|---|
| 791 | } |
|---|
| 792 | if(!$sock) { |
|---|
| 793 | $this->errorMessage = "Could not connect (ERR $errno: $errstr)"; |
|---|
| 794 | $this->errorCode = "-99"; |
|---|
| 795 | ob_end_clean(); |
|---|
| 796 | return false; |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | $response = ""; |
|---|
| 800 | fwrite($sock, $payload); |
|---|
| 801 | stream_set_timeout($sock, $this->timeout); |
|---|
| 802 | $info = stream_get_meta_data($sock); |
|---|
| 803 | while ((!feof($sock)) && (!$info["timed_out"])) { |
|---|
| 804 | $response .= fread($sock, $this->chunkSize); |
|---|
| 805 | $info = stream_get_meta_data($sock); |
|---|
| 806 | } |
|---|
| 807 | if ($info["timed_out"]) { |
|---|
| 808 | $this->errorMessage = "Could not read response (timed out)"; |
|---|
| 809 | $this->errorCode = -98; |
|---|
| 810 | } |
|---|
| 811 | fclose($sock); |
|---|
| 812 | ob_end_clean(); |
|---|
| 813 | list($throw, $response) = explode("\r\n\r\n", $response, 2); |
|---|
| 814 | |
|---|
| 815 | if ($info["timed_out"]) return false; |
|---|
| 816 | |
|---|
| 817 | if(ini_get("magic_quotes_runtime")) $response = stripslashes($response); |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | $decodedResponse = $this->_decode($response); |
|---|
| 821 | |
|---|
| 822 | if($response && $decodedResponse === false) { |
|---|
| 823 | $this->errorMessage = "Bad Response. Got This:".$response; |
|---|
| 824 | return false; |
|---|
| 825 | } else { |
|---|
| 826 | $response = $decodedResponse; |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | return $response; |
|---|
| 830 | |
|---|
| 831 | }//end function --_callServer-- |
|---|
| 832 | |
|---|
| 833 | }//end class |
|---|
| 834 | |
|---|
| 835 | ?> |
|---|