|
1
|
|
|
<?php |
|
2
|
|
|
namespace Datatrics\API\Modules; |
|
3
|
|
|
|
|
4
|
|
|
use Datatrics\API\Client; |
|
5
|
|
|
|
|
6
|
|
|
class Content extends Base |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Private constructor so only the client can create this |
|
10
|
|
|
* @param Client $client |
|
11
|
|
|
*/ |
|
12
|
16 |
|
public function __construct(Client $client) |
|
13
|
|
|
{ |
|
14
|
16 |
|
parent::__construct($client); |
|
15
|
16 |
|
$this->SetUrl("/project/" . $this->GetClient()->GetProjectId() . "/content"); |
|
16
|
16 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get one or multiple contents |
|
20
|
|
|
* @param string content id, leave null for list of boxes |
|
21
|
|
|
* @param object Containing query arguments |
|
22
|
|
|
* @return object Result of the request |
|
23
|
|
|
*/ |
|
24
|
|
|
public function Get($contentId = null, $args = array("limit" => 50, 'type' => 'item')) |
|
25
|
|
|
{ |
|
26
|
|
|
if (is_null($contentId)) { |
|
27
|
|
|
return $this->GetClient()->Get($this->GetUrl(), $args); |
|
28
|
|
|
} |
|
29
|
|
|
return $this->GetClient()->Get($this->GetUrl()."/".$contentId, $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->GetClient()->Post($this->GetUrl(), $content); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create new content |
|
44
|
|
|
* @param object Containing all the information of a content |
|
45
|
|
|
* @throws \Exception When contentid, source, type or itemtype is not present |
|
46
|
|
|
* @return object Result of the request |
|
47
|
|
|
*/ |
|
48
|
|
|
public function Update($content) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($content['contentid'])) { |
|
51
|
|
|
throw new \Exception("content must contain a contentid"); |
|
52
|
|
|
} |
|
53
|
|
|
if (!isset($content['source'])) { |
|
54
|
|
|
throw new \Exception("content must contain a source"); |
|
55
|
|
|
} |
|
56
|
|
|
if (!isset($content['type'])) { |
|
57
|
|
|
throw new \Exception("content must contain a type"); |
|
58
|
|
|
} |
|
59
|
|
|
if ($content['type'] == 'item') { |
|
60
|
|
|
if (!isset($content['itemtype'])) { |
|
61
|
|
|
throw new \Exception("content must contain a itemtype"); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
return $this->GetClient()->Put($this->GetUrl()."/".$content['contentid'], $content); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Delete a content object by content id |
|
69
|
|
|
* @param string Id of the content |
|
70
|
|
|
* @return object Result of the request |
|
71
|
|
|
*/ |
|
72
|
|
|
public function Delete($contentId, $args = array('type' => 'item', 'itemtype' => 'product')) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($contentId) { |
|
75
|
|
|
if (!isset($args['source'])) { |
|
76
|
|
|
throw new \Exception('Source is required'); |
|
77
|
|
|
} |
|
78
|
|
|
if (!isset($args['type'])) { |
|
79
|
|
|
throw new \Exception("Type is required"); |
|
80
|
|
|
} |
|
81
|
|
|
if ($args['type'] == 'item') { |
|
82
|
|
|
if (!isset($args['itemtype'])) { |
|
83
|
|
|
throw new \Exception("Itemtype is required"); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
return $this->GetClient()->Delete($this->GetUrl()."/".$contentId, $args); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Updates a maximum of 50 content items at a time. |
|
92
|
|
|
* @param array Containing content items with a maximum of 50 |
|
93
|
|
|
* @param string Containg type of content items, items or categories |
|
94
|
|
|
* @throws \Exception When more that 50 content items are provided |
|
95
|
|
|
* @return object Result of the request |
|
96
|
|
|
*/ |
|
97
|
|
|
public function Bulk($items, $type = 'items') |
|
98
|
|
|
{ |
|
99
|
|
|
if (count($items) > 50) { |
|
100
|
|
|
throw new \Exception("Maximum of 50 content items allowed at a time"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->GetClient()->Post($this->GetUrl()."/bulk", ['items' => $items, 'type' => $type]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|