nextcloud /
notes
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace OCA\Notes\Controller; |
||||
| 4 | |||||
| 5 | use OCP\AppFramework\Controller; |
||||
| 6 | use OCP\IRequest; |
||||
| 7 | use OCP\IConfig; |
||||
| 8 | use OCP\IL10N; |
||||
| 9 | use OCP\AppFramework\Http; |
||||
| 10 | use OCP\AppFramework\Http\DataResponse; |
||||
| 11 | |||||
| 12 | use OCA\Notes\Service\NotesService; |
||||
| 13 | use OCA\Notes\Service\SettingsService; |
||||
| 14 | use OCA\Notes\Service\InsufficientStorageException; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * Class NotesController |
||||
| 18 | * |
||||
| 19 | * @package OCA\Notes\Controller |
||||
| 20 | */ |
||||
| 21 | class NotesController extends Controller { |
||||
| 22 | |||||
| 23 | use Errors; |
||||
| 24 | |||||
| 25 | /** @var NotesService */ |
||||
| 26 | private $notesService; |
||||
| 27 | /** @var SettingsService */ |
||||
| 28 | private $settingsService; |
||||
| 29 | /** @var IConfig */ |
||||
| 30 | private $settings; |
||||
| 31 | /** @var string */ |
||||
| 32 | private $userId; |
||||
| 33 | /** @var IL10N */ |
||||
| 34 | private $l10n; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * @param string $AppName |
||||
| 38 | * @param IRequest $request |
||||
| 39 | * @param NotesService $notesService |
||||
| 40 | * @param SettingsService $settingsService |
||||
| 41 | * @param IConfig $settings |
||||
| 42 | * @param IL10N $l10n |
||||
| 43 | * @param string $UserId |
||||
| 44 | */ |
||||
| 45 | public function __construct( |
||||
| 46 | $AppName, |
||||
| 47 | IRequest $request, |
||||
| 48 | NotesService $notesService, |
||||
| 49 | SettingsService $settingsService, |
||||
| 50 | IConfig $settings, |
||||
| 51 | IL10N $l10n, |
||||
| 52 | $UserId |
||||
| 53 | ) { |
||||
| 54 | parent::__construct($AppName, $request); |
||||
| 55 | $this->notesService = $notesService; |
||||
| 56 | $this->settingsService = $settingsService; |
||||
| 57 | $this->settings = $settings; |
||||
| 58 | $this->userId = $UserId; |
||||
| 59 | $this->l10n = $l10n; |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @NoAdminRequired |
||||
| 65 | */ |
||||
| 66 | public function index() { |
||||
| 67 | $settings = $this->settingsService->getAll($this->userId); |
||||
| 68 | |||||
| 69 | $errorMessage = null; |
||||
| 70 | $lastViewedNote = (int) $this->settings->getUserValue( |
||||
| 71 | $this->userId, |
||||
| 72 | $this->appName, |
||||
| 73 | 'notesLastViewedNote' |
||||
| 74 | ); |
||||
| 75 | // check if notes folder is accessible |
||||
| 76 | $notes = null; |
||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||
| 77 | try { |
||||
| 78 | $notes = $this->notesService->getAll($this->userId, true); |
||||
| 79 | if ($lastViewedNote) { |
||||
| 80 | // check if note exists |
||||
| 81 | try { |
||||
| 82 | $this->notesService->get($lastViewedNote, $this->userId); |
||||
| 83 | } catch (\Exception $ex) { |
||||
| 84 | $this->settings->deleteUserValue($this->userId, $this->appName, 'notesLastViewedNote'); |
||||
| 85 | $lastViewedNote = 0; |
||||
| 86 | $errorMessage = $this->l10n->t('The last viewed note cannot be accessed. ').$ex->getMessage(); |
||||
| 87 | } |
||||
| 88 | } |
||||
| 89 | } catch (\Exception $e) { |
||||
| 90 | $errorMessage = $this->l10n->t('The notes folder is not accessible: %s', $e->getMessage()); |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | return new DataResponse([ |
||||
| 94 | 'notes' => $notes, |
||||
| 95 | 'settings' => $settings, |
||||
| 96 | 'lastViewedNote' => $lastViewedNote, |
||||
| 97 | 'errorMessage' => $errorMessage, |
||||
| 98 | ]); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | |||||
| 102 | /** |
||||
| 103 | * @NoAdminRequired |
||||
| 104 | * |
||||
| 105 | * @param int $id |
||||
| 106 | * @return DataResponse |
||||
| 107 | */ |
||||
| 108 | public function get($id) { |
||||
| 109 | // save the last viewed note |
||||
| 110 | $this->settings->setUserValue( |
||||
| 111 | $this->userId, |
||||
| 112 | $this->appName, |
||||
| 113 | 'notesLastViewedNote', |
||||
| 114 | strval($id) |
||||
| 115 | ); |
||||
| 116 | |||||
| 117 | $note = $this->notesService->get($id, $this->userId); |
||||
| 118 | return new DataResponse($note); |
||||
| 119 | } |
||||
| 120 | |||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * @NoAdminRequired |
||||
| 124 | * |
||||
| 125 | * @param string $content |
||||
| 126 | */ |
||||
| 127 | public function create($content = '', $category = null) { |
||||
| 128 | try { |
||||
| 129 | $note = $this->notesService->create($this->userId); |
||||
| 130 | $note = $this->notesService->update( |
||||
| 131 | $note->getId(), |
||||
| 132 | $content, |
||||
| 133 | $this->userId, |
||||
| 134 | $category |
||||
| 135 | ); |
||||
| 136 | return new DataResponse($note); |
||||
| 137 | } catch (InsufficientStorageException $e) { |
||||
| 138 | return new DataResponse([], Http::STATUS_INSUFFICIENT_STORAGE); |
||||
| 139 | } |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * @NoAdminRequired |
||||
| 145 | * |
||||
| 146 | * @param int $id |
||||
| 147 | * @param string $content |
||||
| 148 | * @return DataResponse |
||||
| 149 | */ |
||||
| 150 | public function update($id, $content) { |
||||
| 151 | try { |
||||
| 152 | $note = $this->notesService->update($id, $content, $this->userId); |
||||
| 153 | return new DataResponse($note); |
||||
| 154 | } catch (InsufficientStorageException $e) { |
||||
| 155 | return new DataResponse([], Http::STATUS_INSUFFICIENT_STORAGE); |
||||
| 156 | } |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | |||||
| 160 | |||||
| 161 | /** |
||||
| 162 | * @NoAdminRequired |
||||
| 163 | * |
||||
| 164 | * @param int $id |
||||
| 165 | * @param string $category |
||||
| 166 | * @return DataResponse |
||||
| 167 | */ |
||||
| 168 | public function category($id, $category) { |
||||
| 169 | $note = $this->notesService->update($id, null, $this->userId, $category); |
||||
| 170 | return new DataResponse($note->category); |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | |||||
| 174 | /** |
||||
| 175 | * @NoAdminRequired |
||||
| 176 | * |
||||
| 177 | * @param int $id |
||||
| 178 | * @param boolean $favorite |
||||
| 179 | * @return DataResponse |
||||
| 180 | */ |
||||
| 181 | public function favorite($id, $favorite) { |
||||
| 182 | $result = $this->notesService->favorite($id, $favorite, $this->userId); |
||||
| 183 | return new DataResponse($result); // @phan-suppress-current-line PhanTypeMismatchArgument |
||||
|
0 ignored issues
–
show
$result of type boolean is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 184 | } |
||||
| 185 | |||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * @NoAdminRequired |
||||
| 189 | * |
||||
| 190 | * @param int $id |
||||
| 191 | * @return DataResponse |
||||
| 192 | */ |
||||
| 193 | public function destroy($id) { |
||||
| 194 | $this->notesService->delete($id, $this->userId); |
||||
| 195 | return new DataResponse([]); |
||||
| 196 | } |
||||
| 197 | } |
||||
| 198 |