Skip to content

Commit d5bf889

Browse files
3.0.0.22
API - Adding get item details
1 parent 69fa1a9 commit d5bf889

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

api/Controller/Api/AuthController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function authorizeAction()
3434
$superGlobal = new protect\SuperGlobal\SuperGlobal();
3535
$strErrorDesc = '';
3636
$responseData = '';
37+
$strErrorHeader = '';
3738
$requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
3839
$arrQueryStringParams = $this->getQueryStringParams();
3940

api/Controller/Api/ItemController.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ItemController extends BaseController
2727

2828

2929
/**
30-
* Manage case inFolder
30+
* Manage case inFolder - get items inside an array of folders
3131
*
3232
* @param array $userData
3333
*/
@@ -102,4 +102,62 @@ public function inFoldersAction(array $userData): void
102102
}
103103
}
104104
//end InFoldersAction()
105-
}
105+
106+
107+
/**
108+
* Manage case get - get an item
109+
*
110+
* @param array $userData
111+
*/
112+
public function getAction(array $userData): void
113+
{
114+
$superGlobal = new protect\SuperGlobal\SuperGlobal();
115+
$strErrorDesc = '';
116+
$requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
117+
118+
// get parameters
119+
$arrQueryStringParams = $this->getQueryStringParams();
120+
121+
if (strtoupper($requestMethod) === 'GET') {
122+
// SQL where clause with item id
123+
if (isset($arrQueryStringParams['id']) === true) {
124+
// build sql where clause
125+
$sqlExtra = ' WHERE i.id = '.$arrQueryStringParams['id'];
126+
} else {
127+
// Send error
128+
$this->sendOutput(
129+
json_encode(['error' => 'Item id is mandatory']),
130+
['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided']
131+
);
132+
}
133+
134+
// send query
135+
try {
136+
$itemModel = new ItemModel();
137+
138+
$arrItems = $itemModel->getItems($sqlExtra, 0, $userData['private_key'], $userData['id']);
139+
$responseData = json_encode($arrItems);
140+
} catch (Error $e) {
141+
$strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
142+
$strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
143+
}
144+
} else {
145+
$strErrorDesc = 'Method not supported';
146+
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
147+
}
148+
149+
// send output
150+
if (empty($strErrorDesc) === true) {
151+
$this->sendOutput(
152+
$responseData,
153+
['Content-Type: application/json', 'HTTP/1.1 200 OK']
154+
);
155+
} else {
156+
$this->sendOutput(
157+
json_encode(['error' => $strErrorDesc]),
158+
['Content-Type: application/json', $strErrorHeader]
159+
);
160+
}
161+
}
162+
//end getAction()
163+
}

api/Model/ItemModel.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ class ItemModel extends Database
4141
public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array
4242
{
4343
$rows = $this->select(
44-
"SELECT id, label, description, pw, url, id_tree, login, email, viewed_no, fa_icon, inactif, perso
45-
FROM ".prefixTable('items')."".
44+
"SELECT i.id, label, description, i.pw, i.url, i.id_tree, i.login, i.email, i.viewed_no, i.fa_icon, i.inactif, i.perso, t.title as folder_label
45+
FROM ".prefixTable('items')." as i
46+
LEFT JOIN ".prefixTable('nested_tree')." as t ON (t.id = i.id_tree) ".
4647
$sqlExtra .
47-
" ORDER BY id ASC" .
48+
" ORDER BY i.id ASC" .
4849
($limit > 0 ? " LIMIT ?". ["i", $limit] : '')
4950
);
5051
$ret = [];
@@ -66,7 +67,20 @@ public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, i
6667
)
6768
));
6869
}
69-
70+
71+
// get path to item
72+
require_once API_ROOT_PATH. '/../includes/libraries/Tree/NestedTree/NestedTree.php';
73+
$tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
74+
$arbo = $tree->getPath($row['id_tree'], false);
75+
$path = '';
76+
foreach ($arbo as $elem) {
77+
if (empty($path) === true) {
78+
$path = htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
79+
} else {
80+
$path .= '>' . htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
81+
}
82+
}
83+
7084
array_push(
7185
$ret,
7286
[
@@ -81,15 +95,14 @@ public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, i
8195
'fa_icon' => $row['fa_icon'],
8296
'inactif' => (int) $row['inactif'],
8397
'perso' => (int) $row['perso'],
98+
'id_tree' => (int) $row['id_tree'],
99+
'folder_label' => $row['folder_label'],
100+
'path' => empty($path) === true ? '' : $path,
84101
]
85102
);
86103
}
87104

88105
return $ret;
89-
/*[
90-
'number' => count($ret),
91-
'values' => $ret
92-
];*/
93106
}
94107
//end getItems()
95108
}

docs/api-basic.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
- [x] Global API structure
88
- [x] Authentication
9-
- [ ] Items - list with criteria
10-
- [ ] Items - get item info
9+
- [x] Items - list with criteria
10+
- [x] Items - get item info
1111
- [ ] Items - edit an item
1212

1313

@@ -65,6 +65,21 @@ The base API url is: `<Teampass url>/api/index.php/<action criteria>`
6565
| HEADER | {<br>&nbsp;&nbsp;&nbsp;&nbsp;"Authorization": "Bearer _token received from authorize step_"<br>} |
6666
| Return | An array of items in json format.<br>Example:<br>[<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;"id": 1027,<br>&nbsp;&nbsp;&nbsp;&nbsp;"label": "Teampass production",<br>&nbsp;&nbsp;&nbsp;&nbsp;"description": "Use for administration",<br>&nbsp;&nbsp;&nbsp;&nbsp;"pwd": "Ajdh-652Syw-625sWW-Ca18",<br>&nbsp;&nbsp;&nbsp;&nbsp;"url": "https://teampass.net",<br>&nbsp;&nbsp;&nbsp;&nbsp;"login": "tpAdmin",<br>&nbsp;&nbsp;&nbsp;&nbsp;"email": "[email protected]",<br>&nbsp;&nbsp;&nbsp;&nbsp;"viewed_no": 54,<br>&nbsp;&nbsp;&nbsp;&nbsp;"fa_icon": null,<br>&nbsp;&nbsp;&nbsp;&nbsp;"inactif": 0,<br>&nbsp;&nbsp;&nbsp;&nbsp;"perso": 0<br>&nbsp;&nbsp;}<br>] |
6767

68+
### Get item data
69+
70+
> :memo: **Note:** Returns the item definition based upon its ID (taking into account the user access rights)
71+
72+
| Info | Description |
73+
| ---- | ----------- |
74+
| Criteria | item/get |
75+
| Type | GET |
76+
| URL | `<Teampass url>/api/index.php/item/get?id=2052` |
77+
| PARAMETERS | id=<item_id> |
78+
| HEADER | {<br>&nbsp;&nbsp;&nbsp;&nbsp;"Authorization": "Bearer _token received from authorize step_"<br>} |
79+
| Return | An array of item attributes in json format.<br>Example:<br>[{<br>&nbsp;&nbsp;&nbsp;&nbsp;"id":2053,<br>&nbsp;&nbsp;&nbsp;&nbsp;"label":"new object for #3500 v3",<br>&nbsp;&nbsp;&nbsp;&nbsp;"description":"<p>bla bla</p>",<br>&nbsp;&nbsp;&nbsp;&nbsp;"pwd":"SK^6A}]V$t^]",<br>&nbsp;&nbsp;&nbsp;&nbsp;"url":"",<br>&nbsp;&nbsp;&nbsp;&nbsp;"login":"Me",<br>&nbsp;&nbsp;&nbsp;&nbsp;"email":"",<br>&nbsp;&nbsp;&nbsp;&nbsp;"viewed_no":2,<br>&nbsp;&nbsp;&nbsp;&nbsp;"fa_icon":"",<br>&nbsp;&nbsp;&nbsp;&nbsp;"inactif":0,<br>&nbsp;&nbsp;&nbsp;&nbsp;"perso":0<br>&nbsp;&nbsp;&nbsp;&nbsp;"id_tree": 670,<br>&nbsp;&nbsp;&nbsp;&nbsp;"folder_label": "MACHINES",<br>&nbsp;&nbsp;&nbsp;&nbsp;"path": "issue3317>issue 3325>ITI - Speedcall 2>PROD - CLARANET"<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;] |
80+
81+
82+
6883

6984

7085

0 commit comments

Comments
 (0)