|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace okapi\services\lists\create; |
| 4 | + |
| 5 | +use okapi\core\Db; |
| 6 | +use okapi\core\Okapi; |
| 7 | +use okapi\core\Request\OkapiRequest; |
| 8 | +use okapi\Settings; |
| 9 | +use okapi\core\Exception\InvalidParam; |
| 10 | + |
| 11 | +class WebService |
| 12 | +{ |
| 13 | + |
| 14 | + public static function options() |
| 15 | + { |
| 16 | + return array( |
| 17 | + 'min_auth_level' => 3 |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + public static function call(OkapiRequest $request) |
| 22 | + { |
| 23 | + $result = array( |
| 24 | + 'success' => false |
| 25 | + ); |
| 26 | + |
| 27 | + if (Settings::get('OC_BRANCH') == 'oc.de') |
| 28 | + { |
| 29 | + $user_id = $request->token->user_id; |
| 30 | + |
| 31 | + $listName = $request->get_parameter('list_name'); |
| 32 | + $listDescription = $request->get_parameter('list_description'); |
| 33 | + $listStatus = $request->get_parameter('list_status'); |
| 34 | + $isWatched = $request->get_parameter('is_watched'); |
| 35 | + $listPassword = $request->get_parameter('list_password'); |
| 36 | + |
| 37 | + if (empty($listName)) { |
| 38 | + throw new InvalidParam('list_name', 'list_name is mandatory and must not be empty.'); |
| 39 | + } |
| 40 | + |
| 41 | + $insertFields = array( |
| 42 | + 'name' => Db::escape_string($listName), |
| 43 | + 'user_id' => Db::escape_string($user_id) |
| 44 | + ); |
| 45 | + |
| 46 | + if (!empty($listDescription)) { |
| 47 | + $insertFields['description'] = Db::escape_string($listDescription); |
| 48 | + } |
| 49 | + |
| 50 | + if ($listStatus !== null && $listStatus !== '') { |
| 51 | + $listStatus = (int)$listStatus; |
| 52 | + if (!in_array($listStatus, [0, 2, 3])) { |
| 53 | + throw new InvalidParam('list_status', 'list_status must be a valid value (0, 2, 3).'); |
| 54 | + } |
| 55 | + $insertFields['is_public'] = $listStatus; |
| 56 | + |
| 57 | + // Handle list_password only if list_status is 0 (private) |
| 58 | + if ($listStatus == 0) { |
| 59 | + if (isset($listPassword) && $listPassword !== '') { |
| 60 | + $insertFields['password'] = substr(Db::escape_string($listPassword), 0, 16); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $columns = implode(', ', array_keys($insertFields)); |
| 66 | + $values = "'" . implode("', '", $insertFields) . "'"; |
| 67 | + |
| 68 | + $insertQuery = "INSERT INTO cache_lists ($columns) VALUES ($values)"; |
| 69 | + Db::query($insertQuery); |
| 70 | + |
| 71 | + $listId = Db::last_insert_id(); |
| 72 | + |
| 73 | + // Handle is_watched |
| 74 | + if ($isWatched !== null && $isWatched !== '') { |
| 75 | + $isWatched = (int)$isWatched; |
| 76 | + if (!in_array($isWatched, [0, 1])) { |
| 77 | + throw new InvalidParam('is_watched', 'is_watched must be a valid value (0, 1).'); |
| 78 | + } |
| 79 | + |
| 80 | + // Insert a new record |
| 81 | + Db::query("INSERT INTO cache_list_watches (cache_list_id, user_id, is_watched) VALUES (LAST_INSERT_ID(), '$user_id', $isWatched)"); |
| 82 | + } |
| 83 | + |
| 84 | + $result = array( |
| 85 | + 'success' => true, |
| 86 | + 'message' => 'Cache list created successfully.', |
| 87 | + 'list_id' => $listId |
| 88 | + ); |
| 89 | + } |
| 90 | + return Okapi::formatted_response($request, $result); |
| 91 | + } |
| 92 | +} |
| 93 | + |
0 commit comments