1
|
|
|
<?php |
2
|
|
|
namespace Ajax\bootstrap\html\content; |
3
|
|
|
|
4
|
|
|
use Ajax\bootstrap\html\base\HtmlDoubleElement; |
5
|
|
|
use Ajax\bootstrap\html\base\CssSize; |
6
|
|
|
use Ajax\JsUtils; |
7
|
|
|
use Phalcon\Mvc\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Inner element for Twitter Bootstrap Grid row |
11
|
|
|
* @see http://getbootstrap.com/css/#grid |
12
|
|
|
* @author jc |
13
|
|
|
* @version 1.001 |
14
|
|
|
*/ |
15
|
|
|
class HtmlGridRow extends HtmlDoubleElement { |
16
|
|
|
private $cols; |
17
|
|
|
public function __construct($identifier,$numCols=NULL){ |
18
|
|
|
parent::__construct($identifier,"div"); |
19
|
|
|
$this->setProperty("class", "row"); |
20
|
|
|
$this->cols=array(); |
21
|
|
|
if(isset($numCols)){ |
22
|
|
|
$numCols=min(12,$numCols); |
23
|
|
|
$numCols=max(1,$numCols); |
24
|
|
|
$width=12/$numCols; |
25
|
|
|
for ($i=0;$i<$numCols;$i++){ |
26
|
|
|
$this->addCol(CssSize::SIZE_MD,$width); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function addCol($size=CssSize::SIZE_MD,$width=1){ |
32
|
|
|
$col=new HtmlGridCol($this->identifier."-col-".(sizeof($this->cols)+1),$size,$width); |
33
|
|
|
$this->cols[]=$col; |
34
|
|
|
return $col; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function addColAt($size=CssSize::SIZE_MD,$width=1,$offset=1){ |
38
|
|
|
$col=$this->addCol($size,$width); |
39
|
|
|
return $col->setOffset($size, max($offset,sizeof($this->cols)+1)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getCol($index,$force=true){ |
43
|
|
View Code Duplication |
if($index<sizeof($this->cols)+1){ |
|
|
|
|
44
|
|
|
$result=$this->cols[$index-1]; |
45
|
|
|
}else if ($force){ |
46
|
|
|
$result=$this->addColAt(CssSize::SIZE_MD,1,$index); |
47
|
|
|
} |
48
|
|
|
return $result; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getColAt($offset,$force=true){ |
52
|
|
|
$result=null; |
53
|
|
|
foreach ($this->cols as $col){ |
54
|
|
|
$offsets=$col->getOffsets(); |
55
|
|
|
if($result=array_search($offset, $offsets)){ |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
if(!$result || isset($result)==false){ |
|
|
|
|
60
|
|
|
$result=$this->getCol($offset,$force); |
61
|
|
|
} |
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function compile(JsUtils $js=NULL, View $view=NULL) { |
66
|
|
|
|
67
|
|
|
foreach ($this->cols as $col){ |
68
|
|
|
$this->addContent($col); |
69
|
|
|
} |
70
|
|
|
return parent::compile($js,$view); |
71
|
|
|
} |
72
|
|
|
public function getCols() { |
73
|
|
|
return $this->cols; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setContentForAll($content){ |
77
|
|
|
foreach ($this->cols as $col){ |
78
|
|
|
$col->setContent($content); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
public function merge($size=CssSize::SIZE_MD,$start,$width){ |
82
|
|
|
$col=$this->getColAt($start,false); |
83
|
|
|
if(isset($col)){ |
84
|
|
|
$col->setWidth($size,$width+1); |
85
|
|
|
$this->delete($size,$start+1, $width); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
public function delete($size=CssSize::SIZE_MD,$start,$width){ |
89
|
|
|
while($start<sizeof($this->cols)+1 && $width>0){ |
90
|
|
|
$col=$this->getColAt($start,false); |
91
|
|
|
if(isset($col)){ |
92
|
|
|
$widthCol=$col->getWidth($size); |
93
|
|
|
if($widthCol<=$width){ |
94
|
|
|
unset($this->cols[$start-1]); |
95
|
|
|
$this->cols = array_values($this->cols); |
96
|
|
|
$width=$width-$widthCol; |
97
|
|
|
} |
98
|
|
|
}else{ |
99
|
|
|
$width=0; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
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.