MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mstepbystepform.class
Ir para a documentação deste ficheiro.
1<?php
2
34{
35 public $steps;
36 public $step;
37 public $nextStep;
38 public $stepName;
39 public $prevStep;
40 public $data;
41 private static $defaultButtons = true;
42 private static $showImageOnButtons = false;
43
44 const BUTTONS_DIV_ID = 'stepButtonsDiv';
45 const BUTTONS_DIV_STYLE = 'm-stepbystep-buttons';
46 const BUTTONS_DIV_IMG_STYLE = 'm-stepbystep-buttons-img';
47
56 public function __construct($title, $steps = NULL, $step = NULL, $nextStep = NULL)
57 {
59
60 $this->steps = $steps;
61 $this->stepName = MIOLO::getCurrentAction();
62
63 parent::__construct($title);
64 $this->eventHandler();
65
66 $this->buttons = array();
67 $this->step = $step;
68 $this->nextStep = $nextStep;
69 }
70
71 public function createFields()
72 {
74 $module = MIOLO::getCurrentModule();
75
76 parent::createFields();
77
78 if ( !self::getCurrentStep() )
79 {
80 $this->cleanStepData();
81 }
82
83 $this->step = $this->step ? $this->step : self::getCurrentStep() ? self::getCurrentStep() : 1;
84 $this->nextStep = $this->nextStep ? $this->nextStep : $this->step+1;
85
86 foreach ( $this->steps as $k => $stp )
87 {
88 if ( $this->step == $k )
89 {
90 $type = MStep::TYPE_CURRENT;
91 }
92 elseif ( $this->step < $k )
93 {
94 $type = MStep::TYPE_NEXT;
95 }
96 elseif ( $this->step > $k )
97 {
99 }
100
101 $stepsDiv[$k] = new MStep($k, $stp, $type);
102 }
103
104 /*
105 * If it has more than five steps shows the first, the previous,
106 * the current, the next and the last one.
107 */
108 if ( count($this->steps) > 5 )
109 {
110 $asteps[] = $stepsDiv[1];
111 if ( $this->step <= 3 )
112 {
113 $asteps[] = $stepsDiv[2];
114 $asteps[] = $stepsDiv[3];
115 $asteps[] = $stepsDiv[4];
116 $asteps[] = new MDiv(NULL, '...', MStep::HIDE_STYLE);
117 $asteps[] = $stepsDiv[count($this->steps)];
118 }
119 elseif ( $this->step >= (count($this->steps) - 2) )
120 {
121 $step = count($this->steps) - 2;
122 $asteps[] = new MDiv(NULL, '...', MStep::HIDE_STYLE);
123 $asteps[] = $stepsDiv[$step - 1];
124 $asteps[] = $stepsDiv[$step];
125 $asteps[] = $stepsDiv[$step + 1];
126 $asteps[] = $stepsDiv[count($this->steps)];
127 }
128 else
129 {
130 $asteps[] = new MDiv(NULL, '...', MStep::HIDE_STYLE);
131 $asteps[] = $stepsDiv[$this->step - 1];
132 $asteps[] = $stepsDiv[$this->step];
133 $asteps[] = $stepsDiv[$this->step + 1];
134 $asteps[] = new MDiv('', '...', MStep::HIDE_STYLE);
135 $asteps[] = $stepsDiv[count($this->steps)];
136 }
137 }
138 else
139 {
140 $asteps = $stepsDiv;
141 }
142
143 $fields[] = new MBaseGroup(NULL, NULL, $asteps);
144
145 $this->addFields($fields);
146 }
147
152 public function nextStepButton_click()
153 {
154 if ( !$this->nextStep )
155 {
156 $this->nextStep = $this->step ? $this->step + 1 : 2;
157 }
158
159 $this->prepareData($this->getData());
160 self::setCurrentStep($this->nextStep);
161
163 }
164
169 public function previousStepButton_click()
170 {
171 if ( !$this->prevStep )
172 {
173 $this->prevStep = $this->step - 1;
174 }
175
176 if ( $this->prevStep < 1 )
177 {
178 $this->prevStep = 1;
179 }
180
181 $this->prepareData($this->getData());
182 self::setCurrentStep($this->prevStep);
183
185 }
186
192 public function gotoStep($step)
193 {
196 }
197
204 public function objectToArray($object)
205 {
206 if ( count($object) > 1 )
207 {
208 $arr = array( );
209 for ( $i = 0; $i < count($object); $i++ )
210 {
211 $arr[] = get_object_vars($object[$i]);
212 }
213 return $arr;
214 }
215 else
216 {
217 return get_object_vars($object);
218 }
219 }
220
224 public function getStepName()
225 {
226 return $this->stepName;
227 }
228
232 public function setStepName($stepName)
233 {
234 $this->stepName = $stepName;
235 }
236
243 public function setStepData($data, $step = null)
244 {
245 $data = (object) array_merge($this->getStepData($step, false), (array) $data);
246 $this->prepareData($data, $step);
247 }
248
256 public function getStepData($step = null, $returnAsObject = true)
257 {
258 $stepsData = $this->getAllStepData();
259
260 // gets the last step data because it's not on session yet
261 if ( $this->isLastStep() )
262 {
264 $lastStep = array_pop(array_keys($this->steps));
265 $stepsData[$lastStep] = $data;
266 }
267
268 // if a step is given, returns only the specific step data
269 if ( $step )
270 {
271 $data = $returnAsObject ? (object) $stepsData[$step] : $stepsData[$step];
272 }
273 else
274 {
275 if ( $returnAsObject )
276 {
277 $returnData = array();
278 foreach ( $stepsData as $key => $data )
279 {
280 $returnData = array_merge($returnData, $data);
281 }
282
283 $data = (object) $returnData;
284 }
285 else
286 {
287 $data = $stepsData;
288 }
289 }
290
291 return $data;
292 }
293
299 public function cleanStepData($step=null)
300 {
301 if ( $step )
302 {
303 $stepsData = $this->getAllStepData();
304 $stepsData[$step] = NULL;
305
306 $this->setAllStepData($stepsData);
307 }
308 else
309 {
310 $this->setAllStepData(NULL);
311 }
312 }
313
320 public function prepareData($data = null, $step = null)
321 {
322 if ( !$step )
323 {
324 // the first time on the first step, the step is still undefined
325 $step = $this->step ? $this->step : 1;
326 }
327
328 if ( is_object($data) )
329 {
331 }
332
333 // verifies if there is already data on session of this step by step
334 $stepData = $this->getStepData(null, false);
335
336 // adds the step data array to be saved on session
337 $stepData[$step] = $data;
338
339 $this->setAllStepData($stepData);
340 }
341
347 public function setAllStepData($stepData)
348 {
349 // serializes the data and saves it
351 $MIOLO->getSession()->setValue($this->getStepName(), serialize($stepData));
352 }
353
359 public function getAllStepData()
360 {
362 $data = unserialize($MIOLO->getSession()->getValue($this->getStepName()));
363 return $data;
364 }
365
369 public static function getCurrentStep()
370 {
371 return MIOLO::_REQUEST('step');
372 }
373
377 public static function setCurrentStep($step)
378 {
379 $_GET['step'] = $step;
380 }
381
385 public static function getShowImageOnButtons()
386 {
387 return self::$showImageOnButtons;
388 }
389
393 public static function setShowImageOnButtons($showImageOnButtons)
394 {
395 self::$showImageOnButtons = $showImageOnButtons;
396 }
397
403 public function getButtons()
404 {
405 if ( $this->isFirstStep() )
406 {
407 $buttons = $this->firstStepButtons();
408 }
409 elseif ( $this->isLastStep() )
410 {
411 $buttons = $this->lastStepButtons();
412 }
413 else
414 {
415 $buttons = $this->innerStepButtons();
416 }
417
418 return $this->buttonsDiv($buttons);
419 }
420
427 public function finalizeStepByStep($buttons = NULL)
428 {
429 $label = _M('Finished step');
430
431 $jsCode = "
432 document.getElementById('".self::BUTTONS_DIV_ID."').innerHTML = '';
433 document.getElementById('stepImage_{$this->step}').className = '".MStep::PREVIOUS_ICON_STYLE."';
434 document.getElementById('stepDescription_{$this->step}').innerHTML = '$label'; ";
435
436 foreach ( $this->steps as $step => $stepName )
437 {
438 $jsCode .= "
439 var element = document.getElementById('divStep_$step');
440 if ( element )
441 {
442 element.className = '".MStep::DISABLED_STYLE."';
443 element.onclick = '';
444 }";
445 }
446
447 $this->page->onLoad($jsCode);
448
449 if ( $buttons )
450 {
451 $this->addField($this->buttonsDiv($buttons));
452 }
453 else
454 {
455 $div = $this->buttonsDiv(array( new MButton('closeButton', _M('Close')) ));
456 $this->addField($div);
457 }
458 }
459
463 public static function setDefaultButtons($defaultButtons)
464 {
465 self::$defaultButtons = $defaultButtons;
466 }
467
471 public static function getDefaultButtons()
472 {
473 return self::$defaultButtons;
474 }
475
480 public function onLoad()
481 {
482 if ( self::$defaultButtons )
483 {
484 $this->addFields(array( $this->getButtons() ));
485 }
486
487 if ( !$this->isFirstAccess($this->step) )
488 {
489 parent::setData($this->getStepData($this->step));
490 }
491 }
492
496 public function isFirstStep()
497 {
498 if ( self::getCurrentStep() == 1 || !self::getCurrentStep() )
499 {
500 return true;
501 }
502 else
503 {
504 return false;
505 }
506 }
507
511 public function isLastStep()
512 {
513 $lastStep = array_pop(array_keys($this->steps));
514 return $lastStep == $this->step;
515 }
516
523 public function isFirstAccess($step = NULL)
524 {
526
527 if ( !$step )
528 {
530 }
531
532 $stepsData = $this->getAllStepData();
533
534 $data = $stepsData[$step];
535
536 // if is set, returns true
537 return !$data;
538 }
539
544 public function cancelButton_click($args)
545 {
547 }
548
554 public function finalizeButton_click($args)
555 {
557 $module = MIOLO::getCurrentModule();
558 $url = $MIOLO->getActionUrl($module, 'main');
559 $buttons[] = new MButton(NULL, _M('Close'), $url);
561 }
562
571 public function redirect($module, $action, $args = NULL)
572 {
574 $url = $MIOLO->getActionURL($module, $action, '', $args);
575 $MIOLO->page->redirect($url);
576 }
577
581 public function cancelButton()
582 {
583 if ( self::$showImageOnButtons )
584 {
585 $image = MIOLO::getInstance()->getUI()->getImageTheme(MIOLO::getInstance()->getTheme()->id, 'button_cancel.png');
586 }
587 return new MButton('cancelButton', '<span>'._M('Cancel').'</span>', NULL, $image);
588 }
589
593 public function nextStepButton()
594 {
595 if ( self::$showImageOnButtons )
596 {
597 $image = MIOLO::getInstance()->getUI()->getImageTheme(MIOLO::getInstance()->getTheme()->id, 'button_next.png');
598 }
599 return new MButton('nextStepButton', '<span>'._M('Next step').'</span>', NULL, $image);
600 }
601
605 public function previousStepButton()
606 {
607 if ( self::$showImageOnButtons )
608 {
609 $image = MIOLO::getInstance()->getUI()->getImageTheme(MIOLO::getInstance()->getTheme()->id, 'button_previous.png');
610 }
611 return new MButton('previousStepButton', '<span>'._M('Previous step').'</span>', NULL, $image);
612 }
613
617 public function finalizeButton()
618 {
619 if ( self::$showImageOnButtons )
620 {
621 $image = MIOLO::getInstance()->getUI()->getImageTheme(MIOLO::getInstance()->getTheme()->id, 'button_finalize.png');
622 }
623 return new MButton('finalizeButton', '<span>'._M('Finalize').'</span>', NULL, $image);
624 }
625
629 public function firstStepButtons()
630 {
631 return $this->buttonsDiv(array( $this->nextStepButton(), $this->cancelButton() ));
632 }
633
637 public function innerStepButtons()
638 {
639 return $this->buttonsDiv(array( $this->nextStepButton(), $this->previousStepButton(), $this->cancelButton() ));
640 }
641
645 public function lastStepButtons()
646 {
647 return $this->buttonsDiv(array( $this->finalizeButton(), $this->previousStepButton(), $this->cancelButton() ));
648 }
649
654 public function buttonsDiv($buttons)
655 {
656 $class = self::$showImageOnButtons ? self::BUTTONS_DIV_IMG_STYLE : self::BUTTONS_DIV_STYLE;
657 $div = new MDiv(self::BUTTONS_DIV_ID, $buttons, $class);
658 $div->addBoxStyle('direction', 'rtl');
659 $div->addBoxStyle('width', '100%');
660 return $div;
661 }
662}
663?>
eventHandler()
Definição mcontrol.class:723
Definição mform.class:9
$buttons
Definição mform.class:28
getData()
Definição mform.class:1474
$action
Definição mform.class:18
$fields
Definição mform.class:33
$title
Definição mform.class:13
static getCurrentModule()
Definição miolo.class:1066
static _REQUEST( $vars, $from='ALL')
Definição miolo.class:1109
static getCurrentAction()
Definição miolo.class:1086
static getInstance()
Definição miolo.class:134
setAllStepData($stepData)
setStepData($data, $step=null)
static setCurrentStep($step)
redirect($module, $action, $args=NULL)
static setDefaultButtons($defaultButtons)
cleanStepData($step=null)
isFirstAccess($step=NULL)
static setShowImageOnButtons($showImageOnButtons)
prepareData($data=null, $step=null)
__construct($title, $steps=NULL, $step=NULL, $nextStep=NULL)
static getShowImageOnButtons()
finalizeStepByStep($buttons=NULL)
getStepData($step=null, $returnAsObject=true)
const TYPE_NEXT
Definição mstep.class:38
const PREVIOUS_ICON_STYLE
Definição mstep.class:43
const TYPE_CURRENT
Definição mstep.class:37
const TYPE_PREVIOUS
Definição mstep.class:36
const HIDE_STYLE
Definição mstep.class:52
$url
Definição base.php:2