Completed
Push — master ( b0560a...f1cdc4 )
by Jean-Christophe
03:54
created

HtmlProgress::onChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\JsUtils;
7
use Ajax\service\JArray;
8
use Phalcon\Mvc\View;
9
use Ajax\semantic\html\base\constants\State;
10
11
class HtmlProgress extends HtmlSemDoubleElement {
12
	private $_params=array ();
13
14
	public function __construct($identifier, $value=NULL, $label=NULL, $attributes=array()) {
15
		parent::__construct($identifier, "div", "ui progress");
16
		if (isset($value) === true)
17
			$this->setProperty("data-percent", $value);
18
		$this->createBar();
19
		if (isset($label) === true)
20
			$this->setLabel($label);
21
		self::$_states=[ State::SUCCESS,State::WARNING,State::ERROR,State::ACTIVE,State::DISABLED ];
22
		$this->addToProperty("class", $attributes);
23
	}
24
25
	public function setLabel($label) {
26
		$this->content["label"]=new HtmlSemDoubleElement("lbl-" . $this->identifier, "div", "label", $label);
27
		return $this;
28
	}
29
30
	private function createBar() {
31
		$bar=new HtmlSemDoubleElement("bar-" . $this->identifier, "div", "bar", new HtmlSemDoubleElement("progress-" . $this->identifier, "div", "progress"));
32
		$this->content["bar"]=$bar;
33
		return $this;
34
	}
35
36
	public function setTotal($value) {
37
		return $this->setProperty("data-total", $value);
38
	}
39
40
	public function setValue($value) {
41
		return $this->setProperty("data-value", $value);
42
	}
43
44
	public function setPercent($value) {
45
		return $this->setProperty("data-percent", $value);
46
	}
47
48
	public function setIndicating() {
49
		return $this->addToProperty("class", "indicating");
50
	}
51
52
	public function setActive() {
53
		return $this->addToProperty("class", "active");
54
	}
55
56
	public function setWarning() {
57
		return $this->addToProperty("class", "warning");
58
	}
59
60
	public function setError() {
61
		return $this->addToProperty("class", "error");
62
	}
63
64
	/**
65
	 *
66
	 * {@inheritDoc}
67
	 *
68
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
69
	 */
70
	public function compile(JsUtils $js=NULL, View $view=NULL) {
71
		$this->content=JArray::sortAssociative($this->content, [ "bar","label" ]);
72
		return parent::compile($js, $view);
73
	}
74
75
	public function jsSetValue($value) {
76
		return '$("#' . $this->identifier . '").progress({value:' . $value . '});';
77
	}
78
79
	public function jsIncValue() {
80
		return '$("#' . $this->identifier . '").progress("increment");';
81
	}
82
83
	public function jsDecValue() {
84
		return '$("#' . $this->identifier . '").progress("decrement");';
85
	}
86
87
	/**
88
	 *
89
	 * @param mixed $active
90
	 * @param mixed $error
91
	 * @param mixed $success
92
	 * @param mixed $warning
93
	 * @param mixed $percent
94
	 * @param mixed $ratio
95
	 * @return HtmlProgress
96
	 */
97
	public function setTextValues($active=false, $error=false, $success=false, $warning=false, $percent="{percent}%", $ratio="{value} of {total}") {
98
		if (\is_array($active) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
99
			$array=$active;
100
			$active=JArray::getDefaultValue($array, "active", false);
101
			$success=JArray::getDefaultValue($array, "success", $success);
102
			$warning=JArray::getDefaultValue($array, "warning", $warning);
103
			$percent=JArray::getDefaultValue($array, "percent", $percent);
104
			$ratio=JArray::getDefaultValue($array, "ratio", $ratio);
105
		}
106
		$this->_params["text"]="%{active  : " . \var_export($active, true) . ",error: " . \var_export($error, true) . ",success : " . \var_export($success, true) . ",warning : " . \var_export($warning, true) . ",percent : " . \var_export($percent, true) . ",ratio   : " . \var_export($ratio, true) . "}%";
107
		return $this;
108
	}
109
110
	public function onChange($jsCode) {
111
		return $this->_params["onChange"]="%function(percent, value, total){" . $jsCode . "}%";
112
	}
113
114
	/*
115
	 * (non-PHPdoc)
116
	 * @see BaseHtml::run()
117
	 */
118
	public function run(JsUtils $js) {
119 View Code Duplication
		if (isset($this->_bsComponent) === false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
			$this->_bsComponent=$js->semantic()->progress("#" . $this->identifier, $this->_params);
121
		$this->addEventsOnRun($js);
122
		return $this->_bsComponent;
123
	}
124
125
	public static function create($identifier, $value=NULL, $label=NULL, $attributes=array()) {
126
		return new HtmlProgress($identifier, $value, $label, $attributes);
127
	}
128
}