stepper.php
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
class Icl_Stepper {
/**
* Added dummy element to match number of elements and to cover init action
*
* @var array
*/
protected $_steps = array( null );
/**
* Current step
*
* @var int
*/
protected $_step;
/**
* Next step can be forced with Icl_Stepper::setNextStep()
*
* @var int
*/
protected $_nextStep = null;
/**
* Provide current step here
*
* @param int $step
*/
function __construct( $step = 0 ) {
if ( empty( $step ) ) {
$step = 0;
}
$this->_step = intval( $step );
}
/**
* Register steps (function names)
*/
public function registerSteps() {
$args = func_get_args();
$this->_steps = array_merge( $this->_steps, $args );
}
/**
* Returns current step
*
* @return int
*/
public function getStep() {
return $this->_step;
}
/**
* Returns next step
*
* @return int
*/
public function getNextStep() {
return ! is_null( $this->_nextStep ) ? $this->_nextStep : $this->_step += 1;
}
/**
* Sets current step
*
* @param int $num
*/
public function setStep( $num ) {
$this->_step = intval( $num );
}
/**
* Forcing next step
*
* @param int $num
*/
public function setNextStep( $num ) {
$this->_nextStep = intval( $num );
}
/**
* Calculates bar width
*
* @return int Should be used as percentage width (%)
*/
public function barWidth() {
return round( ( $this->_step * 100 ) / count( $this->_steps ) );
}
/**
* Calls current step's function
*
* @return mixed
*/
public function init() {
if ( $this->_step !== 0 && isset( $this->_steps[ $this->_step ] )
&& is_callable( $this->_steps[ $this->_step ] ) ) {
return call_user_func_array( $this->_steps[ $this->_step ], array( $this->_step, $this ) );
}
}
/**
* Returns initial HTML formatted screen
*
* @param string $message Message to be displayed
* @return string
*/
public function render( $message = '' ) {
$output = '<div class="progress-bar" style="width: 100%; height: 15px; background-color: #FFFFFFF; border: 1px solid #e6db55;">
<div class="progress" style="height: 100%; background-color: Yellow; width: ' . $this->barWidth() . '%;"></div>
</div>
<div class="message" style="clear:both;">' . $message . '</div>';
return $output;
}
}