-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegaplanApi.php
453 lines (410 loc) · 16.4 KB
/
MegaplanApi.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
require_once('Request.php');
// require_once('RequestInfo.php');
/*
* @author SADESIGN
* */
class MegaplanApi {
//You must enter your own Megaplan data to enable Debug mode. After all tests have been made, EMPTY ALL FIELDS;
const DEBUG = false;
const DebugScheme = 'https';
const DebugLogin = '[email protected]'; // Megaplan Account Loggin
const DebugPass = 'b48aLLjok'; // Megaplan Account Password
const DebugHost = 'mega-test.megaplan.ru'; // Megaplan Test Host
protected static $Category = [
'common' => 'BumsCommonApiV01',
'task' => 'BumsTaskApiV01',
'project' => 'BumsProjectV01',
'staff' => 'BumsStaffApiV01',
'todo' => 'BumsTimeApiV01',
'trade' => 'BumsTradeApiV01',
'crm' => 'BumsCrmApiV01',
'invoice' => 'BumsInvoiceApiV01',
'discuss' => 'BumsDiscussApiV01',
];
protected static $ObjectType = [
'user' => 'User',
'userInfo' => 'UserInfo',
'search' => 'Search',
'task' => 'Task',
'project' => 'Project',
'comment' => 'Comment',
'contractor' => 'Contractor',
];
protected static $Action = [
'authorize' => 'authorize',
'createOneTimeKeyAuth' => 'createOneTimeKeyAuth',
'id' => 'id',
'quick' => 'quick',
'list' => 'list',
'card' => 'card',
'create' => 'create',
'save' => 'save',
];
protected static $actionType = [
'xml' => 'xml',
'json' => 'api',
];
private $requestApi;
private $requestInfoApi;
public $ch; // curl handler
public $https = false;
public $host; // host for API request
public $login; // Account login
public $pass; // Account Pass
public $accessId; // AccessId for request session
public $secretKey; // secretKey for request session
public $oneTimeKey; // temporary key instead of login/password access
public $UserId; // System user id
public $EmployeeId; // System employee id
public $Folder = 'all'; //Allowed values: "incoming","responsible","executor","owner","auditor","all"
public $TimeUpdated ; //Return objects after particular date (date/time format ISO 8601)
public $Status= 'any'; // Allowed values: "actual", "inprocess", "new", "overdue", "done", "delayed", "completed", "failed", "any"
public $FavoritesOnly = 0; // Allowed Values: 0, 1
public $Search; //Query string
public $Detailed = false; //Print all fields of a task Allowed values: true, false
public $OnlyActual = false;// Print onlu current tasks Allowed values: true, false
public $FilterId; //string Allowed value: any string
public $Count = false; //Print amount of task instead of full list Allowed values: true, false
public $ProjectId; //return list of tasks connected with current projectId
public $SuperTaskId; // Return child task list of SuperTaskId
public $SortBy; // Result sorting Allowed value: "id", "name", "activity", "deadline", "responsible", "owner", "contractor", "start","plannedFinish","plannedWork", "actualWork", "completed","bonus","fine"б "plannedTime" - длительность
public $SortOrder = 'asc'; // Sort order Allowed values: "asc", "desc"
public $ShowActions = false; // Show list of available actions Allowed values: true, false
public $Limit = 50 ; //integer Amount of tasks(LIMIT) Integer value range[1,100]
public $Offset; // task OFFSET
public $SubjectType; // Object type Allowed values: task, project, contractor, deal, discuss
public $SubjectId; // commenting object ID
public $Model = [
'Text' => '', // Comment text of a Model object
'Work' => '',// Amount of spent time for current task/project. Part of a Model object
'WorkDate' => '',// Date to which an amount of time will be spent to. Part of a Model object
'Attaches' => '', // Array of attached files . Must be sent over POST request. Part of a Model object. Array
// Data(file content) encoded with MIME base64. Part of Attaches
// file name. They will be displayed with comments
];
public $id;
public $RequestedFields;
/**
* @param null $scheme // http/https
* @param null $host // host name of saas megaplan
* @param null $login
* @param null $password
* @param bool $debug //
*/
public function __construct( $scheme = null, $host = null, $login = null, $password = null, $debug = self::DEBUG){
if($debug){
$this->https = (self::DebugScheme === 'https') ? true : false;
$this->host = self::DebugHost;
$this->login = self::DebugLogin;
$this->pass = self::DebugPass;
}else{
$this->https = ($scheme === 'https') ? true : false;
$this->host = trim(filter_var($host, FILTER_SANITIZE_URL));
$this->login = trim(filter_var($login, FILTER_SANITIZE_STRING));
$this->pass = trim(filter_var($password, FILTER_SANITIZE_STRING));
}
if(empty($this->host) || empty($this->login) || empty($this->pass))
throw new InvalidArgumentException('Missing authentication parameter for api');
}
/**
* @return resource
*/
public function init($url = null){
$this->ch = curl_init($url);
return $this->ch;
}
/**
* @param $url
*/
public function close($url){
curl_close($url);
}
/**
* @param $toggle
*/
public function setHttps($toggle){
$this->https = $toggle;
}
/**
* @return string
*/
protected function getScheme(){
return $this->https ? 'https' : 'http';
}
public function getPath($category,$objectType,$action,$actionType){
$path = '/';
if(array_key_exists($category,self::$Category)){
$path .= self::$Category[$category] . '/';
}
if(array_key_exists($objectType, self::$ObjectType)){
$path .= self::$ObjectType[$objectType] . '/';
}
if(array_key_exists($action, self::$Action)){
$path .= self::$Action[$action] . '.';
}
if(array_key_exists($actionType, self::$actionType)){
$path .= self::$actionType[$actionType];
}
return $path;
}
/**
* @param $url
* @param $args
* @return string
*/
public function formLink($uri,$args){
$url = $this->getScheme();
$url .= '://' . $this->host.$uri;
$url[strlen($url)] = '?';
foreach($args as $key => $arg){
$url .= $key . '=' . $arg . '&';
}
return substr($url,0,-1);
}
/**
* @return mixed
*/
protected function request(){
return curl_exec($this->ch);
}
/**
* @return SdfApi_Request
*/
protected function getApiRequest(){
if(empty($this->requestApi)){
$this->requestApi = new SdfApi_Request($this->accessId,$this->secretKey,$this->host,$this->https);
}
return $this->requestApi;
}
/**
* @param $method
* @param $host
* @param $Uri
* @param $headers
* @return SdfApi_RequestInfo
* @throws Exception
*/
protected function getInfoApiRequest($method,$host,$Uri,$headers){
if(empty($this->requestInfoApi)){
$this->requestInfoApi = SdfApi_RequestInfo::create($method,$host,$Uri,$headers);
}
return $this->requestInfoApi;
}
/**
* @param $options
* @param null $value
* @return bool
*/
protected function setOpt($options,$value=null){
return curl_setopt($this->ch,$options,$value);
}
/**
* @param $masterList
* @param $compareList
* @return array
*/
protected function formArg($masterList, $compareList){
return array_intersect_key($masterList,$compareList);
}
/**
* OneTimeKeyAuth
* https://help.megaplan.ru/API_onetimekey
* @return mixed if a request failed,
* @return OneTimeKey string if a request successed
* @throws ErrorException
*/
public function getOneTimeKey(){
$this->init();
$arg = [
'Login' => $this->login,
'Password' => md5($this->pass),
];
$path = $this->getPath('common','user','createOneTimeKeyAuth','json');
$link = $this->formLink($path,$arg);
$this->setOpt(CURLOPT_URL,$link);
$response = $this->request();
if($response === false)
throw new ErrorException('Invalid URL or login/password: ' . $link);
$response = json_decode($response,true);
if(array_key_exists('OneTimeKey',$response)){
$this->oneTimeKey = $response['OneTimeKey'];
return $this->oneTimeKey;
}
$this->close($this->ch);
return $response;
}
/**
*
* @param null $oneTimeKey
* @return mixed
*/
public function auth($oneTimeKey = null){
$this->init();
if(empty($oneTimeKey)){
$args = [
'Login' => $this->login,
'Password' => md5($this->pass),
];
}else{
$args = [
'OneTimeKey' => $oneTimeKey,
];
}
$path = $this->getPath('common','user','authorize','json');
$link = $this->formLink($path,$args);
$this->setOpt(CURLOPT_URL,$link);
$this->setOpt(CURLOPT_RETURNTRANSFER,1);
$response = $this->request();
$response = json_decode($response,true);
$this->close($this->ch);
if(array_key_exists('status',$response)){
if($response['status']['code'] === 'ok'){
$this->accessId = $response['data']['AccessId'];
$this->secretKey = $response['data']['SecretKey'];
$this->UserId = $response['data']['UserId'];
$this->EmployeeId = $response['data']['EmployeeId'];
return [
'accessId' => $this->accessId,
'secretKey' => $this->secretKey,
'UserId' => $this->UserId,
'EmployeeId' => $this->EmployeeId
];
}
return $response['status']['code'];
}
return $response;
}
/**
* https://help.megaplan.ru/API_task_list
*/
public function getTaskList($params){
$this->init();
$masterList = get_class_vars(get_class($this));
$arg = $this->formArg($params,$masterList);
$path = $this->getPath('task','task','list', 'json');
$request = $this->getApiRequest();
$response = $request->get($path,$arg);
$this->close($this->ch);
return json_decode($response,true);
}
/**
* https://help.megaplan.ru/API_task_card
* @param $id
* @return mixed
*/
public function getTaskCard($params){
$path = $this->getPath('task','task','card','json');
$masterList = get_class_vars($this);
$arg = $this->formArg($params,$masterList);
$request = $this->getApiRequest();
$response = $request->get($this->host.$path, $arg);
return json_decode($response,true);
}
/*
* https://help.megaplan.ru/API_project_list
* @param $params
* @return mixed
*/
public function getProjectList($params){
$path = $this->getPath('project','project','list','json');
$masterList = get_object_vars($this);
$arg = $this->formArg($params, $masterList);
$request = $this->getApiRequest();
$response = $request->get($path,$arg);
return json_decode($response,true);
}
/**
* https://help.megaplan.ru/API_project_card
* @param $params
* @return mixed
*/
public function getProjectCard($params){
$attrList = get_object_vars($this);
$params = $this->formArg($params,$attrList);
$path = $this->getPath('project','project','card','json');
$request = $this->getApiRequest();
$response = $request->get($path,$params);
return json_decode($response,true);
}
/**
* https://help.megaplan.ru/API_comment_create
* @param $params
* @return mixed
*/
public function addComment($params){
$attrList = get_object_vars($this);
$params = $this->formArg($params,$attrList);
$path = $this->getPath('common','comment','create','json');
$request = $this->getApiRequest();
$response = $request->post($path,$params);
return json_decode($response,true);
}
/**
* https://help.megaplan.ru/API_contractor_list
* @param $params
* @return mixed
*/
public function getClientList($params){
$path = $this->getPath('crm','contractor','list','json');
$masterList = get_object_vars($this);
$arg = $this->formArg($params, $masterList);
$request = $this->getApiRequest();
$response = $request->get($path,$arg);
return json_decode($response,true);
}
/**
* https://help.megaplan.ru/API_contractor_card
* @param $params
*/
public function getClientCard($params){
$attrList = get_object_vars($this);
$params = $this->formArg($params,$attrList);
$path = $this->getPath('crm','contractor','card','json');
$request = $this->getApiRequest();
$response = $request->get($path,$params);
return json_decode($response,true);
}
public function addClient($params){
$path = $this->getPath('crm','contractor','save','json');
$attrList = get_object_vars($this);
$params = $this->formArg($params,$attrList);
$request = $this->getApiRequest();
$response = $request->post($path,$params);
return json_decode($response,true);
}
public function editClient($params){
// TODO edit client card
}
public function removeClient(){
// TODO Implement remove query
}
public function addDeal($params){
$path = $this->getPath('crm','contractor','save','json');
$attrList = get_object_vars($this);
$params = $this->formArg($params,$attrList);
$request = $this->getApiRequest();
$response = $request->post($path,$params);
return json_decode($response,true);
}
/**
* @param $client_id
* @param array $filters
* @return bool| array Contractor
*/
public function isClientExists($client_id, array $filters = []){
$clientList = $this->getClientList($filters);
foreach($clientList['data'] as $client){
switch($client_id){
case $client['Id']:
return $client;
case $client['Email']:
return $client;
case $client['Name']:
return $client;
}
}
return false;
}
}
?>