1
|
|
|
<?php |
2
|
|
|
namespace Datatrics\API\Modules; |
3
|
|
|
|
4
|
|
|
class Content extends Base |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* Private constructor so only the client can create this |
8
|
|
|
* @param string $apikey |
9
|
|
|
* @param string $projectid |
10
|
|
|
*/ |
11
|
|
|
public function __construct($apikey, $projectid) |
12
|
|
|
{ |
13
|
|
|
parent::__construct($apikey, "/project/" . $projectid . "/content"); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get one or multiple contents |
18
|
|
|
* @param string content id, leave null for list of boxes |
19
|
|
|
* @param object Containing query arguments |
20
|
|
|
* @return object Result of the request |
21
|
|
|
*/ |
22
|
|
|
public function Get($contentId = null, $args = array("limit" => 50, 'type' => 'item', 'itemtype' => 'product')) |
23
|
|
|
{ |
24
|
|
|
if ($contentId) { |
25
|
|
|
if (!isset($args['source'])) { |
26
|
|
|
throw new \Exception('Source is required'); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return $contentId == null ? $this->request(self::HTTP_GET, "?".http_build_query($args)) : $this->request(self::HTTP_GET, "/".$contentId."?".http_build_query($args)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create new content |
34
|
|
|
* @param object Containing all the information of a content |
35
|
|
|
* @return object Result of the request |
36
|
|
|
*/ |
37
|
|
|
public function Create($content) |
38
|
|
|
{ |
39
|
|
|
return $this->request(self::HTTP_POST, "", $content); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create new content |
44
|
|
|
* @param id of the content |
45
|
|
|
* @param object Containing all the information of a content |
46
|
|
|
* @return object Result of the request |
47
|
|
|
*/ |
48
|
|
|
public function Update($contentId, $content) |
49
|
|
|
{ |
50
|
|
|
return $this->request(self::HTTP_PUT, "/".$contentId, $content); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Delete a content object by content id |
55
|
|
|
* @param string Id of the content |
56
|
|
|
* @return object Result of the request |
57
|
|
|
*/ |
58
|
|
|
public function Delete($contentId, $args = array('type' => 'item', 'itemtype' => 'product')) |
59
|
|
|
{ |
60
|
|
|
if ($contentId) { |
61
|
|
|
if (!isset($args['source'])) { |
62
|
|
|
throw new \Exception('Source is required'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return $this->request(self::HTTP_DELETE, "/".$contentId."?".http_build_query($args)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Updates a maximum of 50 content items at a time. |
70
|
|
|
* @param array Containing content items with a maximum of 50 |
71
|
|
|
* @throws \Exception When more that 50 content items are provided |
72
|
|
|
* @return object Result of the request |
73
|
|
|
*/ |
74
|
|
|
public function UpdateBulk($items) |
75
|
|
|
{ |
76
|
|
|
if (count($items) > 50) { |
77
|
|
|
throw new \Exception("Maximum of 50 content items allowed at a time"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->request(self::HTTP_POST, "/bulk", ['items' => $items]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|