-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathOdoo.php
More file actions
381 lines (330 loc) · 7.08 KB
/
Copy pathOdoo.php
File metadata and controls
381 lines (330 loc) · 7.08 KB
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
<?php
/**
* (c) Jacob Steringa <jacobsteringa@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Jsg\Odoo;
use Zend\Http\Client as HttpClient;
use Zend\XmlRpc\Client as XmlRpcClient;
/**
* Odoo is an PHP client for the xmlrpc api of Odoo, formerly known as OpenERP.
* This client should be compatible with version 6 and up of Odoo/OpenERP.
*
* This client is inspired on the OpenERP api from simbigo and uses a more or
* less similar API. Instead of an own XmlRpc class, it relies on the XmlRpc
* and Xml libraries from ZF.
*
* @author Jacob Steringa <jacobsteringa@gmail.com>
*/
class Odoo
{
/**
* Host to connect to
*
* @var string
*/
protected $host;
/**
* Unique identifier for current user
*
* @var integer
*/
protected $uid;
/**
* Current users username
*
* @var string
*/
protected $user;
/**
* Current database
*
* @var string
*/
protected $database;
/**
* Password for current user
*
* @var string
*/
protected $password;
/**
* XmlRpc Client
*
* @var XmlRpcClient
*/
protected $client;
/**
* XmlRpc endpoint
*
* @var string
*/
protected $path;
/**
* Optional custom http client to initialize the XmlRpcClient with
*
* @var HttpClient
*/
protected $httpClient;
/**
* Odoo constructor
*
* @param string $host The url
* @param string $database The database to log into
* @param string $user The username
* @param string $password Password of the user
* @param HttpClient $httpClient An optional custom http client to initialize the XmlRpcClient with
*/
public function __construct($host, $database, $user, $password, HttpClient $httpClient = null)
{
$this->host = $host;
$this->database = $database;
$this->user = $user;
$this->password = $password;
$this->httpClient = $httpClient;
}
/**
* Get version
*
* @return array Odoo version
*/
public function version()
{
$response = $this->getClient('common')->call('version');
return $response;
}
/**
* Get timezone
*
* @return string Current timezone
*/
public function timezone()
{
$params = array(
$this->database,
$this->user,
$this->password
);
return $this->getClient('common')->call('timezone_get', $params);
}
/**
* Search models
*
* @param string $model Model
* @param array $data Array of criteria
* @param integer $offset Offset
* @param integer $limit Max results
*
* @return array Array of model id's
*/
public function search($model, $data, $offset = 0, $limit = 100)
{
$params = $this->buildParams(array(
$model,
'search',
$data,
$offset,
$limit
));
$response = $this->getClient('object')->call('execute', $params);
return $response;
}
/**
* Create model
*
* @param string $model Model
* @param array $data Array of fields with data (format: ['field' => 'value'])
*
* @return integer Created model id
*/
public function create($model, $data)
{
$params = $this->buildParams(array(
$model,
'create',
$data
));
$response = $this->getClient('object')->call('execute', $params);
return $response;
}
/**
* Read model(s)
*
* @param string $model Model
* @param array $ids Array of model id's
* @param array $fields Index array of fields to fetch, an empty array fetches all fields
*
* @return array An array of models
*/
public function read($model, $ids, $fields = array())
{
$params = $this->buildParams(array(
$model,
'read',
$ids,
$fields
));
$response = $this->getClient('object')->call('execute', $params);
return $response;
}
/**
* Update model(s)
*
* @param string $model Model
* @param array $ids Array of model id's
* @param array $fields A associative array (format: ['field' => 'value'])
*
* @return array
*/
public function write($model, $ids, $fields)
{
$params = $this->buildParams(array(
$model,
'write',
$ids,
$fields
));
$response = $this->getClient('object')->call('execute', $params);
return $response;
}
/**
* Unlink model(s)
*
* @param string $model Model
* @param array $ids Array of model id's
*
* @return boolean True is successful
*/
public function unlink($model, $ids)
{
$params = $this->buildParams(array(
$model,
'unlink',
$ids
));
return $this->getClient('object')->call('execute', $params);
}
/**
* Get report for model
*
* @param string $model Model
* @param array $ids Array of id's, for this method it should typically be an array with one id
* @param string $type Report type
*
* @return mixed A report file
*/
public function getReport($model, $ids, $type = 'qweb-pdf')
{
$params = $this->buildParams(array(
$model,
$ids,
array(
'model' => $model,
'id' => $ids[0],
'report_type' => $type
)
));
$client = $this->getClient('report');
$reportId = $client->call('report', $params);
$state = false;
while (!$state) {
$report = $client->call(
'report_get',
$this->buildParams(array($reportId))
);
$state = $report['state'];
if (!$state) {
sleep(1);
}
}
return base64_decode($report['result']);
}
/**
* Return last request
*
* @return string
*/
public function getLastRequest()
{
return $this->getClient()->getLastRequest();
}
/**
* Return last response
*
* @return string
*/
public function getLastResponse()
{
return $this->getClient()->getLastResponse();
}
/**
* Set custom http client
*
* @param HttpClient $httpClient
*/
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* Build parameters
*
* @param array $params Array of params to append to the basic params
*
* @return array
*/
protected function buildParams(array $params)
{
return array_merge(array(
$this->database,
$this->uid(),
$this->password
), $params);
}
/**
* Get XmlRpc Client
*
* This method returns an XmlRpc Client for the requested endpoint.
* If no endpoint is specified or if a client for the requested endpoint is
* already initialized, the last used client will be returned.
*
* @param null|string $path The api endpoint
*
* @return XmlRpcClient
*/
protected function getClient($path = null)
{
if ($path === null) {
return $this->client;
}
if ($this->path === $path) {
return $this->client;
}
$this->path = $path;
$this->client = new XmlRpcClient($this->host . '/' . $path, $this->httpClient);
// The introspection done by the Zend XmlRpc client is probably specific
// to Zend XmlRpc servers. To prevent polution of the Odoo logs with errors
// resulting from this introspection calls we disable it.
$this->client->setSkipSystemLookup(true);
return $this->client;
}
/**
* Get uid
*
* @return int $uid
*/
protected function uid()
{
if ($this->uid === null) {
$client = $this->getClient('common');
$this->uid = $client->call('login', array(
$this->database,
$this->user,
$this->password
));
}
return $this->uid;
}
}