This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
CategoryRelationships.php
101 lines (73 loc) · 3.02 KB
/
CategoryRelationships.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
<?php
require_once('./init.php');
try {
$categoryIds = [];
$unique = sha1(rand(0,5000) . microtime());
$cleanup = true;
// create a category
$categoryCreate = $moltin->categories->create([
'type' => 'category',
'name' => 'My First Category',
'slug' => 'my-first-category-' . $unique
]);
if ($categoryCreate->getStatusCode() === 201) {
$categoryId = $categoryCreate->data()->id;
$categoryIds[] = $categoryId;
echo "Category ($categoryId) created (" . $categoryCreate->getExecutionTime() . "s)\n";
}
// create a second category
$categoryCreate = $moltin->categories->create([
'type' => 'category',
'name' => 'My Second Category',
'slug' => 'my-second-category-' . $unique
]);
if ($categoryCreate->getStatusCode() === 201) {
$categoryId = $categoryCreate->data()->id;
$categoryIds[] = $categoryId;
echo "Category ($categoryId) created (" . $categoryCreate->getExecutionTime() . "s)\n";
}
// create a third category
$categoryCreate = $moltin->categories->create([
'type' => 'category',
'name' => 'My Third Category',
'slug' => 'my-third-category-' . $unique
]);
if ($categoryCreate->getStatusCode() === 201) {
$categoryId = $categoryCreate->data()->id;
$categoryIds[] = $categoryId;
echo "Category ($categoryId) created (" . $categoryCreate->getExecutionTime() . "s)\n";
}
function printTree($data) {
echo "Tree: \t" . print_r($data, true);
}
printTree($moltin->categories->get('tree')->data());
echo "make second a child of first:\n";
$moltin->categories->createRelationships($categoryIds[0], 'children', [$categoryIds[1]]);
printTree($moltin->categories->tree()->data());
echo "move second to be a child of third:\n";
$moltin->categories->createRelationships($categoryIds[1], 'parent', $categoryIds[2]);
printTree($moltin->categories->tree()->data());
echo "make second to be a child of first again:\n";
$moltin->categories->createRelationships($categoryIds[1], 'parent', $categoryIds[0]);
printTree($moltin->categories->tree()->data());
echo "make third the parent of first:\n";
$moltin->categories->createRelationships($categoryIds[2], 'children', [$categoryIds[0]]);
printTree($moltin->categories->tree()->data());
echo "remove children of third:\n";
$moltin->categories->updateRelationships($categoryIds[2], 'children', []);
printTree($moltin->categories->tree()->data());
// clean up
if ($cleanup) {
// remove the categories
foreach($categoryIds as $categoryId) {
$deleteCategory = $moltin->categories->delete($categoryId);
if ($deleteCategory->getStatusCode() === 200) {
echo "Category ($categoryId) deleted (" . $deleteCategory->getExecutionTime() . "s)\n";
}
}
}
} catch(Exception $e) {
echo 'An exception occurred calling the moltin API:';
var_dump($e);
exit;
}