PluginProbe
Contact Forms by Cimatti / 2.3.6
Contact Forms by Cimatti v2.3.6
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / PFBC / View / Grid.php

Grid.php in Contact Forms by Cimatti 2.3.6, at PFBC/View/Grid.php

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