| 1 |
<?php |
| 2 |
class View_Grid extends View { |
| 3 |
private $gridIncludedElements = 0; |
| 4 |
|
| 5 |
protected $form; |
| 6 |
protected $grid; |
| 7 |
protected $gridMargin = 2; |
| 8 |
|
| 9 |
public function __construct(array $grid, array $properties = null) { |
| 10 |
if(!empty($properties)) |
| 11 |
$properties["grid"] = $grid; |
| 12 |
else |
| 13 |
$properties = array("grid" => $grid); |
| 14 |
|
| 15 |
parent::__construct($properties); |
| 16 |
} |
| 17 |
|
| 18 |
public function jQueryDocumentReady() { |
| 19 |
$id = $this->form->getId(); |
| 20 |
/*jQuery is used to remove margin from the elements on the far left/right of each row and apply css |
| 21 |
entries to the last row.*/ |
| 22 |
echo <<<JS |
| 23 |
jQuery("#$id .pfbc-grid").each(function() { |
| 24 |
jQuery(".pfbc-element:first", this).css({ "margin-left": "0" }); |
| 25 |
jQuery(".pfbc-element:last", this).css({ "margin-right": "0" }); |
| 26 |
}); |
| 27 |
jQuery("#$id .pfbc-grid:last").css({ |
| 28 |
"margin-bottom": "0", |
| 29 |
"padding-bottom": "0", |
| 30 |
"border-bottom": "none" |
| 31 |
}); |
| 32 |
JS; |
| 33 |
} |
| 34 |
|
| 35 |
public function render() { |
| 36 |
echo '<form', $this->form->getAttributes(), '>'; |
| 37 |
$this->form->getError()->render(); |
| 38 |
|
| 39 |
$elements = $this->form->getElements(); |
| 40 |
|
| 41 |
$gridElementCount = 0; |
| 42 |
$gridIndex = 0; |
| 43 |
$gridCount = 0; |
| 44 |
|
| 45 |
$elementSize = sizeof($elements); |
| 46 |
for($e = 0; $e < $elementSize; ++$e) { |
| 47 |
$element = $elements[$e]; |
| 48 |
|
| 49 |
if($element instanceof Element_Hidden || $element instanceof Element_HTMLExternal) |
| 50 |
$element->render(); |
| 51 |
elseif($element instanceof Element_Button) { |
| 52 |
/*Consecutive Button elements are rendered horizontally in the same row.*/ |
| 53 |
if($e == 0 || !$elements[($e - 1)] instanceof Element_Button) |
| 54 |
echo '<div class="pfbc-grid pfbc-grid-1"><div class="pfbc-element pfbc-buttons">'; |
| 55 |
$element->render(); |
| 56 |
if(($e + 1) == $elementSize || !$elements[($e + 1)] instanceof Element_Button) |
| 57 |
echo '</div></div>'; |
| 58 |
} |
| 59 |
else { |
| 60 |
echo $element->getPreHTML(); |
| 61 |
if(!empty($this->grid[$gridIndex])) { |
| 62 |
if($gridCount == 0) |
| 63 |
echo '<div class="pfbc-grid pfbc-grid-' . $this->grid[$gridIndex] . '">'; |
| 64 |
} |
| 65 |
else |
| 66 |
echo '<div class="pfbc-grid pfbc-grid-1">'; |
| 67 |
|
| 68 |
echo '<div id="pfbc-element-', $gridElementCount, '" class="pfbc-element">'; |
| 69 |
$this->renderLabel($element); |
| 70 |
$element->render(); |
| 71 |
echo '</div>'; |
| 72 |
|
| 73 |
if(!empty($this->grid[$gridIndex]) && ($gridElementCount + 1) == $this->gridIncludedElements) |
| 74 |
echo '</div>'; |
| 75 |
elseif(!empty($this->grid[$gridIndex])) { |
| 76 |
if(($gridCount + 1) == $this->grid[$gridIndex]) { |
| 77 |
echo '</div>'; |
| 78 |
$gridCount = 0; |
| 79 |
++$gridIndex; |
| 80 |
} |
| 81 |
else |
| 82 |
++$gridCount; |
| 83 |
} |
| 84 |
else |
| 85 |
echo '</div>'; |
| 86 |
echo $element->getPostHTML(); |
| 87 |
++$gridElementCount; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
echo '</form>'; |
| 92 |
} |
| 93 |
|
| 94 |
public function renderCSS() { |
| 95 |
$id = $this->form->getId(); |
| 96 |
$width = $this->form->getWidth(); |
| 97 |
$widthSuffix = $this->form->getWidthSuffix(); |
| 98 |
|
| 99 |
parent::renderCSS(); |
| 100 |
echo <<<CSS |
| 101 |
#$id { width: $width{$widthSuffix}; } |
| 102 |
#$id .pfbc-label { margin-bottom: .25em; } |
| 103 |
#$id .pfbc-label label { display: block; } |
| 104 |
#$id .pfbc-grid { width: 100%; margin-bottom: 1em; padding-bottom: 1em; border-bottom: 1px solid #f4f4f4; } |
| 105 |
#$id .pfbc-grid:after { clear: both; display: block; margin: 0; padding: 0; visibility: hidden; height: 0; content: ":)"; } |
| 106 |
#$id .pfbc-buttons { text-align: right; } |
| 107 |
#$id .pfbc-textbox, #$id .pfbc-textarea, #$id .pfbc-select { width: $width{$widthSuffix}; } |
| 108 |
#$id .pfbc-grid .pfbc-element { margin-right: {$this->gridMargin}$widthSuffix; margin-left: {$this->gridMargin}$widthSuffix; } |
| 109 |
CSS; |
| 110 |
|
| 111 |
$elements = $this->form->getElements(); |
| 112 |
$gridElements = array(); |
| 113 |
foreach($elements as $element) { |
| 114 |
/*Hidden, HTMLExternal, and Button element classes aren't included in the grid.*/ |
| 115 |
if(!$element instanceof Element_Hidden && !$element instanceof Element_HTMLExternal && !$element instanceof Element_Button) { |
| 116 |
++$this->gridIncludedElements; |
| 117 |
$gridElements[] = $element; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/*If the grid array contains more elements than the form has available, it is revised.*/ |
| 122 |
if(array_sum($this->grid) > $this->gridIncludedElements) { |
| 123 |
$gridRevised = array(); |
| 124 |
foreach($this->grid as $grid) { |
| 125 |
$gridRemaining = $this->gridIncludedElements - array_sum($gridRevised); |
| 126 |
if(!empty($gridRemaining)) |
| 127 |
if($gridRemaining >= $grid) |
| 128 |
$gridRevised[] = $grid; |
| 129 |
else |
| 130 |
$gridRevised[] = $gridRemaining; |
| 131 |
else |
| 132 |
break; |
| 133 |
} |
| 134 |
$this->grid = $gridRevised; |
| 135 |
} |
| 136 |
|
| 137 |
$gridSize = sizeof($this->grid); |
| 138 |
$elementWidths = array(); |
| 139 |
for($g = 0; $g < $gridSize; ++$g) { |
| 140 |
$gridSum = array_sum(array_slice($this->grid, 0, $g)); |
| 141 |
if($widthSuffix == "px") |
| 142 |
$gridRemainingWidth = $width; |
| 143 |
else |
| 144 |
$gridRemainingWidth = 100; |
| 145 |
$gridRemainingElements = $this->grid[$g]; |
| 146 |
for($e = $gridSum; $e < ($gridSum + $this->grid[$g]); ++$e) { |
| 147 |
$elementWidths[$e] = $gridElements[$e]->getWidth(); |
| 148 |
if(!empty($elementWidths[$e])) { |
| 149 |
$gridRemainingWidth -= $elementWidths[$e]; |
| 150 |
--$gridRemainingElements; |
| 151 |
echo '#', $id, ' #pfbc-element-', $e, ' { float: left; width: ', $elementWidths[$e], $widthSuffix, '; }'; |
| 152 |
echo '#', $id, ' #pfbc-element-', $e, ' .pfbc-textbox, #', $id, ' #pfbc-element-', $e, ' .pfbc-textarea, #', $id, ' #pfbc-element-', $e, ' .pfbc-select { width: ', $elementWidths[$e], $widthSuffix, '; }'; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if(!empty($gridRemainingElements)) { |
| 157 |
$elementWidth = floor((($gridRemainingWidth - ($this->gridMargin * 2 * ($this->grid[$g] - 1))) / $gridRemainingElements)); |
| 158 |
for($e = $gridSum; $e < ($gridSum + $this->grid[$g]); ++$e) { |
| 159 |
if(empty($elementWidths[$e])) { |
| 160 |
echo '#', $id, ' #pfbc-element-', $e, ' { float: left; width: ', $elementWidth, $widthSuffix, '; }'; |
| 161 |
echo '#', $id, ' #pfbc-element-', $e, ' .pfbc-textbox, #', $id, ' #pfbc-element-', $e, ' .pfbc-textarea, #', $id, ' #pfbc-element-', $e, ' .pfbc-select { width: ', $elementWidth, $widthSuffix, '; }'; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|