1 <?php
  2 
  3   4   5   6   7   8   9  10 
 11 
 12 namespace Icybee\Modules\Forms;
 13 
 14 use ICanBoogie\Debug;
 15 use ICanBoogie\Exception;
 16 use ICanBoogie\Operation;
 17 
 18 use Brickrouge\Button;
 19 use Brickrouge\Element;
 20 
 21 class Form extends \Icybee\Modules\Nodes\Node
 22 {
 23     const MODELID = 'modelid';
 24     const CONFIG = 'config';
 25     const BEFORE = 'before';
 26     const AFTER = 'after';
 27     const COMPLETE = 'complete';
 28     const PAGEID = 'pageid';
 29 
 30     const FORM_RECORD_TAG = '#form-record';
 31 
 32      33  34  35  36 
 37     public $modelid;
 38 
 39      40  41  42  43 
 44     public $before;
 45 
 46      47  48  49  50 
 51     public $after;
 52 
 53      54  55  56  57 
 58     public $complete;
 59 
 60      61  62  63  64 
 65     public $is_notify;
 66     public $notify_destination;
 67     public $notify_from;
 68     public $notify_bcc;
 69     public $notify_subject;
 70     public $notify_template;
 71     public $pageid;
 72 
 73      74  75  76  77  78  79 
 80     protected function lazy_get_form_model()
 81     {
 82         global $core;
 83 
 84         $modelid = $this->modelid;
 85         $models = $core->configs->synthesize('formmodels', 'merge');
 86 
 87         if (empty($models[$modelid]))
 88         {
 89             throw new Exception('Unknown model id: %id', array('%id' => $modelid), 404);
 90         }
 91 
 92         return $models[$modelid];
 93     }
 94 
 95     protected function get_url()
 96     {
 97         global $core;
 98 
 99         if (!$this->pageid)
100         {
101             return '#form-url-not-defined';
102         }
103 
104         try
105         {
106             return $core->models['pages'][$this->pageid]->url;
107         }
108         catch (\Exception $e)
109         {
110             return '#missing-target-page-' . $this->pageid;
111         }
112     }
113 
114     115 116 117 118 
119     protected function lazy_get_form()
120     {
121         $class = $this->form_model['class'];
122 
123         return new $class
124         (
125             array
126             (
127                 \Brickrouge\Form::ACTIONS => new Button
128                 (
129                     'Send', array
130                     (
131                         'class' => 'btn-primary',
132                         'type' => 'submit'
133                     )
134                 ),
135 
136                 \Brickrouge\Form::HIDDENS => array
137                 (
138                     Operation::DESTINATION => 'forms',
139                     Operation::NAME => Module::OPERATION_POST,
140                     Module::OPERATION_POST_ID => $this->nid
141                 ),
142 
143                 \Brickrouge\Form::VALUES => $_POST + $_GET,
144 
145                 self::FORM_RECORD_TAG => $this,
146 
147                 'id' => $this->slug
148             )
149         );
150     }
151 
152     153 154 155 156 
157     public function render()
158     {
159         global $core;
160 
161         
162         
163         
164 
165         $session = $core->session;
166 
167         if (!empty($session->modules['forms']['rc'][$this->nid]))
168         {
169             unset($session->modules['forms']['rc'][$this->nid]);
170 
171             return '<div id="' . $this->slug . '">' . $this->complete . '</div>';
172         }
173 
174         $form = $this->form;
175 
176         if (isset($form->hiddens[Operation::DESTINATION]) && isset($form->hiddens[Operation::NAME]))
177         {
178             $destination = $form->hiddens[Operation::DESTINATION];
179             $name = $access = $form->hiddens[Operation::NAME];
180 
181             if ($name == 'save')
182             {
183                 $access = Module::PERMISSION_CREATE;
184             }
185             else if ($name == 'post' && $destination == 'forms')
186             {
187                 $access = 'post form';
188             }
189 
190             if (!$core->user->has_permission($access, $destination))
191             {
192                 return (string) new \Brickrouge\Alert
193                 (
194                     <<<EOT
195 <p>You don't have permission to execute the <q>$name</q> operation on the <q>$destination</q> module,
196 <a href="{$core->site->path}/admin/users.roles">the <q>{$core->user->role->name}</q> role should be modified</a>.</p>
197 EOT
198                     , array(), 'error'
199                 );
200             }
201         }
202 
203         $core->document->css->add(DIR . 'public/page.css');
204 
205         $before = $this->before;
206         $after = $this->after;
207         $form = $this->form;
208 
209         new Form\BeforeRenderEvent
210         (
211             $this, array
212             (
213                 'before' => &$before,
214                 'after' => &$after,
215                 'form' => $form,
216             )
217         );
218 
219         $normalized = \ICanBoogie\normalize($this->slug);
220 
221         if ($before)
222         {
223             $before = '<div class="form-before form-before--' . $normalized . '">' . $before . '</div>';
224         }
225 
226         if ($after)
227         {
228             $after = '<div class="form-after form-after--' . $normalized . '">' . $after . '</div>';
229         }
230 
231         $html = $before . $form . $after;
232 
233         new Form\RenderEvent
234         (
235             $this, array
236             (
237                 'html' => &$html,
238                 'before' => $before,
239                 'after' => $after,
240                 'form' => $form,
241             )
242         );
243 
244         return $html;
245     }
246 
247     public function __toString()
248     {
249         try
250         {
251             return (string) $this->render();
252         }
253         catch (\Exception $e)
254         {
255             Debug::report($e);
256 
257             return Debug::format_alert($e);
258         }
259     }
260 }
261 
262 namespace Icybee\Modules\Forms\Form;
263 
264 /**
265  * Event class for the `Icybee\Modules\Forms\Form::render:before` event.
266  */
267 class BeforeRenderEvent extends \ICanBoogie\Event
268 {
269     /**
270      * The form to render.
271      *
272      * @var \Icybee\Modules\Forms\Form
273      */
274     public $form;
275 
276     /**
277      * The HTML content before the form.
278      *
279      * @var string
280      */
281     public $before;
282 
283     /**
284      * The HTML content after the form.
285      *
286      * @var string
287      */
288     public $after;
289 
290     /**
291      * The event is created with the type `render:before`.
292      *
293      * @param \Icybee\Modules\Forms\Form $target
294      * @param array $payload
295      */
296     public function __construct(\Icybee\Modules\Forms\Form $target, array $payload)
297     {
298         parent::__construct($target, 'render:before', $payload);
299     }
300 }
301 
302 /**
303  * Event class for the `Icybee\Modules\Forms\Form::render` event.
304  */
305 class RenderEvent extends \ICanBoogie\Event
306 {
307     /**
308      * Reference to the HTML resulting of the rendering.
309      *
310      * @var string
311      */
312     public $html;
313 
314     /**
315      * The form to render.
316      *
317      * @var \Icybee\Modules\Forms\Form
318      */
319     public $form;
320 
321     /**
322      * The HTML content before the form.
323      *
324      * @var string
325      */
326     public $before;
327 
328     /**
329      * The HTML content after the form.
330      *
331      * @var string
332      */
333     public $after;
334 
335     /**
336      * The event is created with the type `render`.
337      *
338      * @param \Icybee\Modules\Forms\Form $target
339      * @param array $payload
340      */
341     public function __construct(\Icybee\Modules\Forms\Form $target, array $payload)
342     {
343         parent::__construct($target, 'render', $payload);
344     }
345 }