Completed
Push — master ( b9a8b8...3e8704 )
by Jean-Christophe
03:43
created

HtmlSelect   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 93
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A addOption() 0 11 4
A setValue() 0 8 3
A compile() 0 11 3
A fromArray() 0 12 4
A run() 0 6 1
A onChange() 0 3 1
A onKeypress() 0 3 1
A fromDatabaseObject() 0 3 1
A setSize() 0 3 1
1
<?php
2
namespace Ajax\common\html\html5;
3
4
use Ajax\common\html\HtmlDoubleElement;
5
/**
6
 * HTML Select
7
 * @author jc
8
 * @version 1.002
9
 */
10
11
class HtmlSelect extends HtmlDoubleElement {
12
	private $default;
13
	private $options;
14
15
	public function __construct($identifier, $caption,$default=NULL) {
16
		parent::__construct($identifier, "select");
17
		$this->setProperty("name", $identifier);
18
		$this->setProperty("class", "form-control");
19
		$this->setProperty("value", "");
20
		$this->default=$default;
21
		$this->options=array();
22
	}
23
24
	public function addOption($element,$value="",$selected=false){
25
		if($element instanceof HtmlOption){
26
			$option=$element;
27
		}else{
28
			$option=new HtmlOption($this->identifier."-".count($this->options), $element,$value);
29
		}
30
		if($selected || $option->getValue()==$this->getProperty("value")){
31
			$option->select();
32
		}
33
		$this->options[]=$option;
34
	}
35
36
	public function setValue($value) {
37
		foreach ( $this->options as $option ) {
38
			if (strcasecmp($option->getValue(),$value)===0) {
39
				$option->setProperty("selected", "");
40
			}
41
		}
42
		$this->setProperty("value", $value);
43
	}
44
45
	/*
46
	 * (non-PHPdoc)
47
	 * @see \Ajax\bootstrap\html\base\BaseHtml::compile()
48
	 */
49
	public function compile(JsUtils $js=NULL, View $view=NULL) {
50
		$this->content=array();
51
		if(isset($this->default)){
52
			$default=new HtmlOption("", $this->default);
53
			$this->content[]=$default;
54
		}
55
		foreach ($this->options as $option){
56
			$this->content[]=$option;
57
		}
58
		return parent::compile($js, $view);
59
	}
60
61
	public function fromArray($array){
62
		if(JArray::isAssociative($array)){
63
			foreach ($array as $k=>$v){
64
				$this->addOption($v, $k);
65
			}
66
		}else{
67
			foreach ($array as $v){
68
				$this->addOption($v, $v);
69
			}
70
		}
71
		return $this;
72
	}
73
74
	/*
75
	 * (non-PHPdoc)
76
	 * @see \Ajax\bootstrap\html\base\HtmlSingleElement::run()
77
	 */
78
	public function run(JsUtils $js) {
79
		parent::run($js);
80
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
81
		$this->addEventsOnRun($js);
82
		return $this->_bsComponent;
83
	}
84
85
	public function onChange($jsCode) {
86
		return $this->on("change", $jsCode);
87
	}
88
89
	public function onKeypress($jsCode) {
90
		return $this->on("keypress", $jsCode);
91
	}
92
93
	/* (non-PHPdoc)
94
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
95
	 */
96
	public function fromDatabaseObject($object, $function) {
97
		$this->addOption($function($object));
98
	}
99
100
	public function setSize($size){
101
		return $this->setProperty("size", $size);
102
	}
103
}
104