dolibarr 25.0.0-alpha
interface.php
1<?php
2/* Copyright (C) 2024 John BOTELLA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Do not create database handler $db
19//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Do not load object $user
20//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1'); // Do not load object $mysoc
21//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Do not load object $langs
22//if (! defined('NOSCANGETFORINJECTION')) define('NOSCANGETFORINJECTION', '1'); // Do not check injection attack on GET parameters
23//if (! defined('NOSCANPOSTFORINJECTION')) define('NOSCANPOSTFORINJECTION', '1'); // Do not check injection attack on POST parameters
24//if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1'); // Do not check CSRF attack (test on referer + on token if option MAIN_SECURITY_CSRF_WITH_TOKEN is on).
25if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
26//if (! defined('NOSTYLECHECK')) define('NOSTYLECHECK', '1'); // Do not check style html tag into posted data
27if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
28if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
29//if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
30//if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session). This include the NOIPCHECK too.
31//if (! defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
32//if (! defined("MAIN_LANG_DEFAULT")) define('MAIN_LANG_DEFAULT', 'auto'); // Force lang to a particular value
33//if (! defined("MAIN_AUTHENTICATION_MODE")) define('MAIN_AUTHENTICATION_MODE', 'aloginmodule'); // Force authentication handler
34if (! defined("NOREDIRECTBYMAINTOLOGIN")) define('NOREDIRECTBYMAINTOLOGIN', 1); // The main.inc.php does not make a redirect if not logged, instead show simple error message
35//if (! defined("FORCECSP")) define('FORCECSP', 'none'); // Disable all Content Security Policies
36//if (! defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
37//if (! defined('NOBROWSERNOTIF')) define('NOBROWSERNOTIF', '1'); // Disable browser notification
38
39
40// Load Dolibarr environment
41require '../main.inc.php';
48// Load required classes
49require_once DOL_DOCUMENT_ROOT . '/core/class/jsonResponse.class.php';
50require_once __DIR__ . '/class/memo.class.php';
51require_once __DIR__ . '/lib/quickmemo.lib.php';
52if (!class_exists('Validate')) {
53 require_once DOL_DOCUMENT_ROOT . '/core/class/validate.class.php';
54}
55
56// Initialize hooks for the interface
57$hookmanager->initHooks(['quickmemoInterface']);
58
59// Load translation files required by the page
60$langs->loadLangs(["quickmemo", "other", 'main']);
61
62$action = GETPOST('action');
63
64// Security check: check if the module is enabled
65if (!isModEnabled('quickmemo')) accessforbidden('Module not enabled');
66
67$jsonResponse = new JsonResponse();
68
69// Security check: basic read permission
70if (!$user || !$user->hasRight('quickmemo', 'memo', 'read')) {
71 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
72 $jsonResponse->output();
73}
74
75// Security check: memos are for internal users only (the js interface is not loaded for external users)
76if ($user->socid > 0) {
77 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
78 $jsonResponse->output();
79}
80
81// Execute hooks before standard actions
82$reshook = $hookmanager->executeHooks('quickMemoInterface', [], $jsonResponse, $action);
83if ($reshook < 0) {
84 $errMsg = $hookmanager->error;
85 if (!empty($hookmanager->errors)) {
86 $errMsg.= (!empty($hookmanager->error) ? '<br>' : '') . implode('<br>', $hookmanager->errors);
87 }
88 $jsonResponse->setError($errMsg);
89 $jsonResponse->output();
90}
91
92// Action Dispatcher: Routes the request to the appropriate function based on the 'action' parameter
93if ($reshook > 0) {
94 // Action handled by hook
95} elseif ($action === 'update_position') {
96 quickMemoIntefaceActionUpdatePosition($jsonResponse);
97} elseif ($action === 'update_all_positions') {
98 quickMemoIntefaceActionUpdateAllPositions($jsonResponse);
99} elseif ($action === 'list') {
100 quickMemoIntefaceActionList($jsonResponse);
101} elseif ($action === 'list_models') {
102 quickMemoIntefaceActionListModels($jsonResponse);
103} elseif ($action === 'update-color') {
104 quickMemoIntefaceActionUpdateColor($jsonResponse);
105} elseif ($action === 'update-shared-on-element') {
106 quickMemoIntefaceActionUpdateSharedOnElement($jsonResponse);
107} elseif ($action === 'update-private') {
108 quickMemoIntefaceActionUpdatePrivate($jsonResponse);
109} elseif ($action === 'archive') {
110 quickMemoIntefaceActionArchiveNote($jsonResponse);
111} elseif ($action === 'delete') {
112 quickMemoIntefaceActionDeleteNote($jsonResponse);
113} elseif ($action === 'create') {
114 quickMemoIntefaceActionCreate($jsonResponse);
115} elseif ($action === 'create_model') {
116 quickMemoIntefaceActionCreateModel($jsonResponse);
117} elseif ($action === 'delete_model') {
118 quickMemoIntefaceActionDeleteModel($jsonResponse);
119} elseif ($action === 'update_model_rank') {
120 quickMemoIntefaceActionUpdateModelRank($jsonResponse);
121} elseif ($action === 'update_note') {
122 quickMemoIntefaceActionUpdateNote($jsonResponse);
123} else {
124 $jsonResponse->setError($langs->trans('ErrorActionNotFound'));
125}
126
127$jsonResponse->output();
128
134function quickMemoIntefaceActionUpdatePosition($jsonResponse)
135{
136 global $user, $langs, $db;
137
138 // Read permission is sufficient as this only affects the current user's view
139 if (!$user->hasRight('quickmemo', 'memo', 'read')) {
140 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
141 return false;
142 }
143
144 // Prevent moving a private note belonging to another user
145 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantMoveThisPrivateNote');
146 if (!$memo) {
147 return false;
148 }
149
150 $z = GETPOST("z", "int");
151 $x = GETPOST("x", "int");
152 $y = GETPOST("y", "int");
153 $w = GETPOST("w", "int");
154 $h = GETPOST("h", "int");
155
156 if (!$memo->updatePosition($user, (int) $x, (int) $y, (int) $w, (int) $h, (int) $z)) {
157 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
158 return false;
159 } else {
160 $jsonResponse->setSuccess();
161 $jsonResponse->data = null;
162 return true;
163 }
164}
165
171function quickMemoIntefaceActionUpdateAllPositions($jsonResponse)
172{
173 global $user, $langs, $db;
174
175 // In case of position, read permission is enough because changing position will affect only this user not others.
176 // So he can't modify the content but can move it for himself.
177 if (!$user->hasRight('quickmemo', 'memo', 'read')) {
178 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
179 return false;
180 }
181
182 $json = file_get_contents('php://input');
183 $data = json_decode($json, true);
184
185 if (!is_array($data) || empty($data['memos'])) {
186 $jsonResponse->setError($langs->trans('ErrorNoDataReceived'));
187 return;
188 }
189
190 $db->begin();
191
192 foreach ($data['memos'] as $item) {
193 $memo = new Memo($db);
194 if ($memo->fetch((int) $item['id']) > 0) {
195 // Security check for private notes
196 if ($user->id != $memo->fk_user_creat && $memo->private) continue;
197 if (!quickMemoInterfaceCanAccessMemo($memo)) continue;
198
199 $memo->updatePosition(
200 $user,
201 (int) $item['x'],
202 (int) $item['y'],
203 (int) $item['w'],
204 (int) $item['h'],
205 (int) $item['z']
206 );
207 }
208 }
209
210 $db->commit();
211 $jsonResponse->setSuccess();
212
213 return true;
214}
215
221function quickMemoIntefaceActionCreate($jsonResponse)
222{
223 global $user, $langs, $db, $conf;
224
225 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
226 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
227 return false;
228 }
229
230 $element_id = GETPOST("element_id", "int");
231 $element_type = GETPOST("element_type");
232
233
234 if (empty($element_id) && !is_numeric($element_id) && !empty($element_type)) {
235 $jsonResponse->setError($langs->trans('ErrorNeedElementId'));
236 return false;
237 }
238
239 if (!quickMemoInterfaceCanAccessElement((string) $element_type, (int) $element_id)) {
240 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
241 return false;
242 }
243
244 $memo = new Memo($db);
245 $context_tab = GETPOST('context');
246 if (!in_array($context_tab, $memo->getAvailableMemoContext())) {
247 $jsonResponse->setError($langs->trans('ErrorContextUnknown'));
248 return false;
249 }
250
251 // Handle color selection with preset fallback
252 $colorPresets = Memo::getColorPreset();
253 $firstColor = !empty($colorPresets) ? $colorPresets[0] : null;
254 $memo->color = GETPOST("color");
255 if (!Memo::checkColor($memo->color)) {
256 $memo->color = $firstColor;
257 }
258
259 $memo->quick_note = GETPOST("note", quickMemoInterfaceGetNoteSecurityCheck());
260 $memo->fk_element = $element_id;
261 $memo->element_type = $element_type;
262 $memo->context_tab = $context_tab;
263 $memo->shared_on_element = GETPOST("shared_on_element", "int") ? 1 : 0;
264 $memo->private = GETPOST("private", "int") ? 1 : 0;
265
266 $memo->pos_x = GETPOST("x", "int");
267 $memo->pos_y = GETPOST("y", "int");
268 $memo->pos_w = GETPOST("w", "int");
269 $memo->pos_h = GETPOST("h", "int");
270 $memo->pos_z = GETPOST("z", "int");
271 $memo->status = Memo::STATUS_VALIDATED;
272 $memo->entity = quickMemoInterfaceGetMemoEntity((string) $element_type, (int) $element_id, (bool) $memo->shared_on_element);
273
274 $resCre = $memo->create($user);
275 if ($resCre <= 0 ) {
276 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
277 return false;
278 } else {
279 $jsonResponse->setSuccess();
280 $jsonResponse->data = new stdClass();
281 $jsonResponse->data->id = (int) $resCre;
282
283 return true;
284 }
285}
286
287
293function quickMemoIntefaceActionArchiveNote($jsonResponse)
294{
295 global $user, $langs, $db;
296
297 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
298 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
299 return false;
300 }
301
302 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantArchiveThisPrivateNote');
303 if (!$memo) {
304 return false;
305 }
306
307 if ($memo->setArchived($user) < 0) {
308 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
309 return false;
310 } else {
311 $jsonResponse->setSuccess();
312 $jsonResponse->data = null;
313 return true;
314 }
315}
316
322function quickMemoIntefaceActionCreateModel($jsonResponse)
323{
324 global $user, $langs, $db, $conf;
325
326 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
327 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
328 return false;
329 }
330
331 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantChangeThisPrivateNote');
332 if (!$memo) {
333 return false;
334 }
335
336 $element_type = GETPOST("element_type");
337 if (empty($element_type)) {
338 $memo->element_type = '';
339 }
340
341 $memo->status = Memo::STATUS_TPL;
342 $memo->entity = $conf->entity; // A template is not linked to the object anymore
343 $memo->private_tpl = GETPOST("tpl_private", "int");
344 $memo->name_tpl = GETPOST("tpl_name");
345 $memo->fk_user_creat = $user->id;
346 $memo->date_creation = dol_now();
347 $memo->fk_user_modif = null;
348 $memo->date_modification = null;
349 $memo->fk_user_archived = null;
350 $memo->date_archived = null;
351
352 if ($memo->update($user) < 0) {
353 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
354 return false;
355 } else {
356 $jsonResponse->setSuccess();
357 $jsonResponse->data = null;
358 return true;
359 }
360}
361
362
368function quickMemoIntefaceActionDeleteModel($jsonResponse)
369{
370 global $user, $langs, $db;
371
372 if (!$user->hasRight('quickmemo', 'memo', 'delete')) {
373 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
374 return false;
375 }
376
377 $id = GETPOSTINT("id");
378
379 if ($id <= 0) {
380 $jsonResponse->setError($langs->trans('ErrorNeedModelId'));
381 return false;
382 }
383
384 $memo = new Memo($db);
385 if ($memo->fetch($id) <= 0 || !$memo->isInCurrentEntity()) {
386 $jsonResponse->setError($langs->trans('ErrorMemoNotFound'));
387 return false;
388 }
389
390 if ($memo->status != Memo::STATUS_TPL) {
391 $jsonResponse->setError($langs->trans('ErrorThisMemoIsNotATemplate'));
392 return false;
393 }
394
395 if ($user->id != $memo->fk_user_creat && $memo->private_tpl) {
396 $jsonResponse->setError($langs->trans('QuickMemoCantDeleteThisPrivateModel'));
397 return false;
398 }
399
400 if ($memo->delete($user) < 0) {
401 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
402 return false;
403 } else {
404 $jsonResponse->setSuccess();
405 $jsonResponse->data = null;
406 return true;
407 }
408}
409
442function quickMemoIntefaceActionUpdateModelRank($jsonResponse)
443{
444 global $user, $langs, $db;
445
446 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
447 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
448 return false;
449 }
450
451 $json = file_get_contents('php://input');
452 $data = json_decode($json, true);
453
454 if (!is_array($data) || empty($data['moved']['id']) || !isset($data['moved']['newPos'])) {
455 $jsonResponse->setError($langs->trans('ErrorInvalidMovedData'));
456 return false;
457 }
458
459
460
461 $movedId = (int) $data['moved']['id'];
462 $newPos = max(1, (int) $data['moved']['newPos']);
463
464 $tmpMemo = new Memo($db);
465
466 // 1 Retrieve the moved model
467 $sql = 'SELECT rowid, element_type, context_tab
468 FROM '.MAIN_DB_PREFIX.$tmpMemo->table_element.'
469 WHERE rowid = '. (int) $movedId.'
470 AND entity IN ('.getEntity($tmpMemo->element).')
471 AND status = '. (int) Memo::STATUS_TPL;
472
473 $res = $db->query($sql);
474 if (!$res || !$db->num_rows($res)) {
475 $jsonResponse->setError($langs->trans('ErrorModelNotFound'));
476 return false;
477 }
478
479 $movedModel = $db->fetch_object($res);
480
481 // 2 Retrieve ALL compatible models
482 $sqlAll = 'SELECT rowid
483 FROM '.MAIN_DB_PREFIX.$tmpMemo->table_element.'
484 WHERE status = '. (int) Memo::STATUS_TPL.'
485 AND entity IN ('.getEntity($tmpMemo->element).')
486 AND element_type IN (\''.$db->escape($movedModel->element_type).'\', \'\')
487 ORDER BY rank_tpl DESC';
488
489 // AND context_tab = \''.$db->escape($movedModel->context_tab).'\'
490
491 $resAll = $db->query($sqlAll);
492 if (!$resAll) {
493 $jsonResponse->setError($db->error(), JsonResponse::HTTP_INTERNAL_ERROR);
494 return false;
495 }
496
497 $orderedIds = [];
498 while ($obj = $db->fetch_object($resAll)) {
499 $orderedIds[] = (int) $obj->rowid;
500 }
501
502 // 3 Remove the moved ID
503 $orderedIds = array_values(array_diff($orderedIds, [$movedId]));
504
505 // 4 Calculate actual index (0 based)
506 $newIndex = min(count($orderedIds), $newPos - 1);
507
508 // 5 Insert at new position
509 array_splice($orderedIds, $newIndex, 0, [$movedId]);
510
511 // 6 Clean re-indexing
512 $total = count($orderedIds);
513 $currentRank = $total;
514
515 $db->begin();
516
517 foreach ($orderedIds as $id) {
518 $sqlUp = 'UPDATE '.MAIN_DB_PREFIX.$tmpMemo->table_element.'
519 SET rank_tpl = '.(int) $currentRank.'
520 WHERE rowid = '.(int) $id.'
521 AND status = '. (int) Memo::STATUS_TPL.'
522 AND entity IN ('.getEntity($tmpMemo->element).')
523 AND (private_tpl = 0 OR fk_user_creat = '.(int) $user->id.')';
524
525 if (!$db->query($sqlUp)) {
526 $db->rollback();
527 $jsonResponse->setError($db->error(), JsonResponse::HTTP_INTERNAL_ERROR);
528 return false;
529 }
530
531 $currentRank--;
532 }
533
534 $db->commit();
535
536 $jsonResponse->setSuccess();
537 $jsonResponse->data = null;
538 return true;
539}
545function quickMemoIntefaceActionDeleteNote($jsonResponse)
546{
547 global $user, $langs, $db;
548
549 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
550 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
551 return false;
552 }
553
554 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantDeleteThisPrivateNote');
555 if (!$memo) {
556 return false;
557 }
558
559 if ($memo->status == Memo::STATUS_TPL) { // In case of models need check other stuff
560 $jsonResponse->setError($langs->trans('ErrorCantDeleteModel'));
561 return false;
562 }
563
564 if ($memo->delete($user) < 0) {
565 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
566 return false;
567 } else {
568 $jsonResponse->setSuccess();
569 $jsonResponse->data = null;
570 return true;
571 }
572}
573
574
580function quickMemoIntefaceActionUpdateNote($jsonResponse)
581{
582 global $user, $langs, $db;
583
584 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
585 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
586 return false;
587 }
588
589 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantChangeThisPrivateNote');
590 if (!$memo) {
591 return false;
592 }
593
594 $memo->quick_note = GETPOST("note", quickMemoInterfaceGetNoteSecurityCheck());
595 $memo->tms = dol_now();
596 $memo->fk_user_modif = $user->id;
597
598 if ($memo->update($user) < 0) {
599 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
600 return false;
601 } else {
602 $jsonResponse->setSuccess();
603 $jsonResponse->data = new stdClass();
604
605 // return new memo
606 $jsonResponse->data->memo = $memo->getJsMemo($user);
607 return true;
608 }
609}
610
616function quickMemoIntefaceActionUpdateColor($jsonResponse)
617{
618 global $user, $langs, $db;
619
620 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
621 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
622 return false;
623 }
624
625 $color = GETPOST("color");
626 if (!Memo::checkColor($color)) {
627 $jsonResponse->setError($langs->trans('ErrorNeedValidColor'));
628 return false;
629 }
630
631 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantChangeThisPrivateNote');
632 if (!$memo) {
633 return false;
634 }
635
636 $memo->color = $color;
637
638 if ($memo->update($user) < 0) {
639 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
640 return false;
641 } else {
642 $jsonResponse->setSuccess();
643 $jsonResponse->data = new stdClass();
644
645 // return new memo
646 $jsonResponse->data->memo = $memo->getJsMemo($user);
647 return true;
648 }
649}
650
656function quickMemoIntefaceActionUpdateSharedOnElement($jsonResponse)
657{
658 global $user, $langs, $db;
659
660 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
661 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
662 return false;
663 }
664
665 $shared_on_element = GETPOST("shared_on_element", "int");
666
667 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantChangeThisPrivateNote');
668 if (!$memo) {
669 return false;
670 }
671
672 $memo->shared_on_element = $shared_on_element ? 1 : 0;
673 $memo->entity = quickMemoInterfaceGetMemoEntity((string) $memo->element_type, (int) $memo->fk_element, (bool) $memo->shared_on_element);
674
675 if ($memo->update($user) < 0) {
676 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
677 return false;
678 } else {
679 $jsonResponse->setSuccess();
680 $jsonResponse->data = null;
681 $jsonResponse->data = new stdClass();
682
683 // return new memo
684 $jsonResponse->data->memo = $memo->getJsMemo($user);
685 return true;
686 }
687}
688
689
695function quickMemoIntefaceActionUpdatePrivate($jsonResponse)
696{
697 global $user, $langs, $db;
698
699 if (!$user->hasRight('quickmemo', 'memo', 'write')) {
700 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
701 return false;
702 }
703
704 $private = GETPOST("private", "int");
705
706 $memo = quickMemoInterfaceFetchMemo($jsonResponse, 'QuickMemoCantChangeThisPrivateNote');
707 if (!$memo) {
708 return false;
709 }
710
711 $memo->private = $private ? 1 : 0;
712
713 if ($memo->update($user) < 0) {
714 $jsonResponse->setError($langs->trans('UpdateError') . ' : ' . $memo->errorsToString(), JsonResponse::HTTP_INTERNAL_ERROR);
715 return false;
716 } else {
717 $jsonResponse->setSuccess();
718 $jsonResponse->data = new stdClass();
719
720 // return new memo
721 $jsonResponse->data->memo = $memo->getJsMemo($user);
722 return true;
723 }
724}
725
731function quickMemoIntefaceActionListModels($jsonResponse)
732{
733 global $user, $langs, $db;
734
735 if (!$user->hasRight('quickmemo', 'memo', 'read')) {
736 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
737 return false;
738 }
739
740 $element_type = GETPOST('element_type', 'alpha');
741 $context = GETPOST('context', 'alpha');
742
743 $jsonResponse->data = new stdClass();
744 $jsonResponse->data->modelTemplate = [];
745 $jsonResponse->data->presetTemplate = [];
746
747 // Get standard color list presets
748 $colorList = Memo::getColorPreset();
749
750 foreach ($colorList as $color) {
751 $default = new stdClass();
752 $default->name = '';
753 $default->color = $color;
754 $default->note = '';
755 $default->pos_w = 0;
756 $default->pos_h = 0;
757 $default->pos_x = 0;
758 $default->pos_y = 0;
759 $default->shared_on_element = 0;
760 $default->private = 0;
761 $default->user_name = $user->getFullName($langs);
762 $default->date_creation = dol_print_date(dol_now(), '', 'tzuserrel');
763 $default->date_change = '';
764 $default->user_change_name = '';
765
766
767 $jsonResponse->data->presetTemplate[] = $default;
768 }
769
770 // GET all memo templates (tpl)
771 $memoStatic = new Memo($db);
772 $sql = $memoStatic->getTemplateMemosQuery($element_type, $context);
773 $resql = $db->query($sql);
774 if (!$resql) {
775 $jsonResponse->setError($db->lasterror(), JsonResponse::HTTP_INTERNAL_ERROR);
776 return;
777 }
778
779 while ($obj = $db->fetch_object($resql)) {
780 // fallback user-specific position
781 $jsonResponse->data->modelTemplate[] = quickMemoInterfacePopulateMemoTplFromQueryObj($obj);
782 }
783
784 $jsonResponse->setSuccess();
785}
786
792function quickMemoIntefaceActionList($jsonResponse)
793{
794 global $user, $langs, $db;
795
796 if (!$user->hasRight('quickmemo', 'memo', 'read')) {
797 $jsonResponse->setError($langs->trans('NotEnoughPermissions'), JsonResponse::HTTP_FORBIDDEN);
798 return;
799 }
800
801 $element_id = GETPOSTINT('element_id');
802 $element_type = GETPOST('element_type', 'alpha');
803 $context = GETPOST('context', 'alpha');
804 $jsonResponse->data = new stdClass();
805 $jsonResponse->data->memos = [];
806 $jsonResponse->data->nbArchives = 0;
807
808 if (empty($element_id) && !empty($element_type)) {
809 $jsonResponse->setError($langs->trans('ErrorNeedElementId'));
810 return;
811 }
812
813 if (!quickMemoInterfaceCanAccessElement((string) $element_type, $element_id)) {
814 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
815 return;
816 }
817
818 $staticMemo = new Memo($db);
819
820 // GET all active memos
821 $sql = $staticMemo->getMemosQuery($element_type, $element_id, $context);
822 $resql = $db->query($sql);
823 if (!$resql) {
824 $jsonResponse->setError($db->lasterror(), JsonResponse::HTTP_INTERNAL_ERROR);
825 return;
826 }
827
828
829 $memos = [];
830 while ($obj = $db->fetch_object($resql)) {
831 // fallback user-specific position
832 $memos[] = quickMemoInterfacePopulateMemoFromQueryObj($obj);
833 }
834
835 $jsonResponse->data->memos = $memos;
836
837 // Count archived notes for display
838 $nbArchives = $staticMemo->countArchivedMemoQuery($element_type, $element_id, $context);
839 if ($nbArchives === false) {
840 $jsonResponse->setError($langs->trans('ErrorCountArchive'), JsonResponse::HTTP_INTERNAL_ERROR);
841 return;
842 }
843
844 $jsonResponse->data->nbArchives = $nbArchives;
845
846 $jsonResponse->setSuccess();
847}
848
849
855function quickMemoInterfacePopulateMemoFromQueryObj($obj)
856{
857 global $db, $langs;
858 $obj->date_creation = $db->jdate($obj->date_creation);
859 $obj->tms = $db->jdate($obj->tms);
860
861 $memo = Memo::getJsMemoDefault();
862 $memo->id = $obj->rowid;
863 $memo->color = $obj->color;
864 $memo->note = $obj->quick_note;
865 // Use user-specific position if available, otherwise fallback to default
866 $memo->pos_x = $obj->user_pos_x !== null ? (int) $obj->user_pos_x : (int) $obj->pos_x;
867 $memo->pos_y = $obj->user_pos_y !== null ? (int) $obj->user_pos_y : (int) $obj->pos_y;
868 $memo->pos_w = $obj->user_pos_w !== null ? (int) $obj->user_pos_w : (int) $obj->pos_w;
869 $memo->pos_h = $obj->user_pos_h !== null ? (int) $obj->user_pos_h : (int) $obj->pos_h;
870 $memo->pos_z = $obj->user_pos_z !== null ? (int) $obj->user_pos_z : (int) $obj->pos_z;
871
872 $memo->shared_on_element = $obj->shared_on_element;
873 $memo->private = (int) $obj->private;
874 $memo->date_creation = dol_print_date($obj->date_creation, '%d/%m/%Y %H:%M', 'tzuserrel');
875 $memo->date_change = '';
876 if (!empty($obj->tms) && ((int) $obj->date_creation !== (int) $obj->tms || (int) $obj->fk_user_modif > 0)) {
877 $memo->date_change = dol_print_date($obj->tms, '%d/%m/%Y %H:%M', 'tzuserrel');
878 }
879
880 $memo->fk_user_creat = $obj->fk_user_creat;
881 $memo->user_name = '';
882 if ((int) $obj->fk_user_creat > 0) {
883 $userCreate = new User($db);
884 if ($userCreate->fetch((int) $obj->fk_user_creat) > 0) {
885 $memo->user_name = $userCreate->getFullName($langs);
886 }
887 }
888
889 $memo->fk_user_modif = $obj->fk_user_modif;
890 $memo->user_change_name = '';
891 if ((int) $obj->fk_user_modif > 0) {
892 $userMod = new User($db);
893 if ($userMod->fetch((int) $obj->fk_user_modif) > 0) {
894 $memo->user_change_name = $userMod->getFullName($langs);
895 }
896 }
897
898 return $memo;
899}
900
906function quickMemoInterfacePopulateMemoTplFromQueryObj($obj)
907{
908 global $db, $langs, $user;
909 $obj->date_creation = $db->jdate($obj->date_creation);
910 $obj->tms = $db->jdate($obj->tms);
911
912
913 $memo = new stdClass();
914 $memo->name = $obj->name_tpl;
915 $memo->id = $obj->rowid;
916 $memo->color = $obj->color;
917 $memo->note = $obj->quick_note;
918 $memo->pos_x = $obj->user_pos_x !== null ? (int) $obj->user_pos_x : (int) $obj->pos_x;
919 $memo->pos_y = $obj->user_pos_y !== null ? (int) $obj->user_pos_y : (int) $obj->pos_y;
920 $memo->pos_w = $obj->user_pos_w !== null ? (int) $obj->user_pos_w : (int) $obj->pos_w;
921 $memo->pos_h = $obj->user_pos_h !== null ? (int) $obj->user_pos_h : (int) $obj->pos_h;
922 $memo->pos_z = $obj->user_pos_z !== null ? (int) $obj->user_pos_z : (int) $obj->pos_z;
923 $memo->rank_tpl = (int) $obj->rank_tpl;
924
925 $memo->shared_on_element = $obj->shared_on_element;
926 $memo->private = (int) $obj->private;
927 $memo->date_creation = dol_print_date($obj->date_creation, '%d/%m/%Y %H:%M', 'tzuserrel');
928 $memo->fk_user_creat = $user->id;
929 $memo->user_name = $user->getFullName($langs);
930 $memo->date_change = '';
931 $memo->user_change_name = '';
932
933 return $memo;
934}
935
945function quickMemoInterfaceGetMemoEntity($element_type, $element_id, $shared_on_element)
946{
947 global $conf;
948
949 if (empty($element_type) || $shared_on_element || $element_id <= 0) {
950 return (int) $conf->entity;
951 }
952
953 // Object already loaded in cache by quickMemoInterfaceCanAccessElement()
954 $object = fetchObjectByElement($element_id, quickMemoInterfaceResolveElementType($element_type), '', 1);
955
956 return is_object($object) ? (int) setEntity($object) : (int) $conf->entity;
957}
958
965function quickMemoInterfaceCanAccessMemo($memo)
966{
967 if (!$memo->isInCurrentEntity()) {
968 return false;
969 }
970
971 // Templates are not attached to an object
972 if ($memo->status == Memo::STATUS_TPL) {
973 return true;
974 }
975
976 // A memo shared on element is displayed on all objects of the same type
977 return quickMemoInterfaceCanAccessElement((string) $memo->element_type, (int) $memo->fk_element, !empty($memo->shared_on_element));
978}
979
987function quickMemoInterfaceFetchMemo($jsonResponse, $privateErrorKey = '')
988{
989 global $user, $langs, $db;
990
991 $id = GETPOSTINT("id");
992 if ($id <= 0) {
993 $jsonResponse->setError($langs->trans('ErrorNeedMemoId'));
994 return null;
995 }
996
997 $memo = new Memo($db);
998 if ($memo->fetch($id) <= 0 || !$memo->isInCurrentEntity()) {
999 $jsonResponse->setError($langs->trans('ErrorMemoNotFound'));
1000 return null;
1001 }
1002
1003 if (!empty($privateErrorKey) && $user->id != $memo->fk_user_creat && $memo->private) {
1004 $jsonResponse->setError($langs->trans($privateErrorKey));
1005 return null;
1006 }
1007
1008 if (!quickMemoInterfaceCanAccessMemo($memo)) {
1009 $jsonResponse->setError($langs->trans('NotEnoughRights'), JsonResponse::HTTP_FORBIDDEN);
1010 return null;
1011 }
1012
1013 return $memo;
1014}
1015
1021function quickMemoInterfaceGetNoteSecurityCheck()
1022{
1023 if (getDolGlobalString('MAIN_SECURITY_DISALLOW_UNSECURED_TEXT')) {
1024 return 'alphanohtml';
1025 }
1026 return 'nohtml';
1027}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class JsonResponse used for ajax responses in Dolibarr.
Class for Memo.
static checkColor($color)
Checks if a color is a valid hex code.
static getJsMemoDefault()
Get JS memo default.
static getColorPreset()
Get color preset.
Class to manage Dolibarr users.
$context
@method int call_trigger(string $triggerName, ?User $user)
Definition command.php:209
dol_now($mode='gmt')
Return date for now.
setEntity($currentobject)
Set entity id to use when to create an object.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
quickMemoInterfaceCanAccessElement($element_type, $element_id, $moduleLevelOnly=false)
Check that current user can read the object a memo is attached to.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.