1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Nodes;
13
14 use ICanBoogie\DateTime;
15 use ICanBoogie\I18n\FormattedString;
16
17 /**
18 * Saves a node.
19 */
20 class SaveOperation extends \ICanBoogie\SaveOperation
21 {
22 /**
23 * Overrides the method to handle the following properties:
24 *
25 * `uid`: Only users with the PERMISSION_ADMINISTER permission can choose the user of records.
26 * If the user saving a record has no such permission, the Node::UID property is removed from
27 * the properties created by the parent method.
28 *
29 * `siteid`: If the user is creating a new record or the user has no permission to choose the
30 * record's site, the property is set to the value of the working site's id.
31 *
32 * Also, the following default values are used:
33 *
34 * - `uid`: 0
35 * - `nativeid`: 0
36 * - `language`: an empty string
37 */
38 protected function lazy_get_properties()
39 {
40 global $core;
41
42 $properties = parent::lazy_get_properties() + [
43
44 Node::UID => 0,
45 Node::NATIVEID => 0,
46 Node::LANGUAGE => ''
47
48 ];
49
50 $user = $core->user;
51
52 if (!$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
53 {
54 unset($properties[Node::UID]);
55 }
56
57 if (!$this->key || !$user->has_permission(Module::PERMISSION_MODIFY_BELONGING_SITE))
58 {
59 $properties[Node::SITEID] = $core->site_id;
60 }
61
62 $properties[Node::UPDATED_AT] = DateTime::now();
63
64 return $properties;
65 }
66
67 /**
68 * Returns the form from the `edit` block if the getter wasn't able to retrieve the form. This
69 * is currently used to create records using XHR.
70 */
71 protected function lazy_get_form()
72 {
73 $form = parent::lazy_get_form();
74
75 if ($form)
76 {
77 return $form;
78 }
79
80 $block = $this->module->getBlock('edit', $this->key);
81
82 return $block->element;
83 }
84
85 /**
86 * Overrides the method to provide a nicer log message.
87 */
88 protected function process()
89 {
90 $rc = parent::process();
91
92 $this->response->message = new FormattedString
93 (
94 $rc['mode'] == 'update' ? '%title has been updated in :module.' : '%title has been created in :module.', array
95 (
96 'title' => \ICanBoogie\shorten($this->record->title),
97 'module' => $this->module->title
98 )
99 );
100
101 return $rc;
102 }
103 }