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;
13
14 /**
15 * Save the configuration of the module.
16 *
17 * There are two spaces for the configuration to be saved in: a local space and a global
18 * space.
19 *
20 * Configuration in the local space is saved in the `metas` of the working site object, whereas
21 * the configuration in the global space is saved in the registry.
22 *
23 *
24 * Event: properties:before
25 * ------------------------
26 *
27 * The `property:before` event of class `Icybee\ConfigOperation\BeforePropertiesEvent` is fired by
28 * the `Icybee\ConfigOperation` and its subclasses before the config properties are collected.
29 *
30 * One can attach a hook to this event to modify the operation request params before they are used
31 * to collect the config properties.
32 *
33 *
34 * Event: properties
35 * -----------------
36 *
37 * The `properties` event of class `Icybee\ConfigOperation\PropertiesEvent` is fired by the
38 * `Icybee\ConfigOperation` and its subclasses after the config properties were collected.
39 *
40 * One can attach a hook to this event to modify the properties before they are stored.
41 */
42 class ConfigOperation extends \ICanBoogie\Operation
43 {
44 protected function get_controls()
45 {
46 return array
47 (
48 self::CONTROL_PERMISSION => Module::PERMISSION_ADMINISTER,
49 self::CONTROL_FORM => true
50 )
51
52 + parent::get_controls();
53 }
54
55 /**
56 * Parse the operation parameters to create the key/value pairs to save in the "global" and
57 * "local" config spaces.
58 */
59 protected function lazy_get_properties()
60 {
61 return array_intersect_key($this->request->params, array('global' => true, 'local' => true));
62 }
63
64 protected function validate(\ICanboogie\Errors $errors)
65 {
66 return !count($errors);
67 }
68
69 protected function process()
70 {
71 global $core;
72
73 new ConfigOperation\BeforePropertiesEvent($this, array('request' => $this->request));
74
75 $properties = $this->properties;
76
77 new ConfigOperation\PropertiesEvent($this, array('request' => $this->request, 'properties' => &$properties));
78
79 if (isset($properties['global']))
80 {
81 $registry = $core->registry;
82
83 foreach ($properties['global'] as $name => $value)
84 {
85 $registry[$name] = $value;
86 }
87 }
88
89 if (isset($properties['local']))
90 {
91 $site = $core->site;
92
93 foreach ($properties['local'] as $name => $value)
94 {
95 if (is_array($value))
96 {
97 foreach ($value as $subname => $subvalue)
98 {
99 $site->metas[$name . '.' . $subname] = $subvalue;
100 }
101
102 continue;
103 }
104
105 $site->metas[$name] = $value;
106 }
107 }
108
109 $this->response->message = "The configuration has been saved.";
110 $this->response->location = $this->request->path;
111
112 return true;
113 }
114 }
115
116 namespace Icybee\ConfigOperation;
117
118 /**
119 * Event class for the `Icybee\ConfigOperation::properties:before` event.
120 */
121 class BeforePropertiesEvent extends \ICanBoogie\Event
122 {
123 /**
124 * The HTTP request.
125 *
126 * @var \ICanBoogie\HTTP\Request
127 */
128 public $request;
129
130 /**
131 * The event is constructed with the type `properties:before`.
132 *
133 * @param \Icybee\ConfigOperation $target
134 * @param array $payload
135 */
136 public function __construct(\Icybee\ConfigOperation $target, array $payload)
137 {
138 parent::__construct($target, 'properties:before', $payload);
139 }
140 }
141
142 /**
143 * Event class for the `Icybee\ConfigOperation::properties` event.
144 */
145 class PropertiesEvent extends \ICanBoogie\Event
146 {
147 /**
148 * The HTTP request.
149 *
150 * @var \ICanBoogie\HTTP\Request
151 */
152 public $request;
153
154 /**
155 * Reference to the config properties.
156 *
157 * @var array
158 */
159 public $payload;
160
161 /**
162 * The event is constructed with the type `properties`.
163 *
164 * @param \Icybee\ConfigOperation $target
165 * @param array $payload
166 */
167 public function __construct(\Icybee\ConfigOperation $target, array $payload)
168 {
169 parent::__construct($target, 'properties', $payload);
170 }
171 }