Caldera_Form_Grid

The Caldera Forms Caldera Form Grid class.

Defined (1)

The class is defined in the following location(s).

/classes/caldera-grid.php  
  1. class Caldera_Form_Grid { 
  2.  
  3. private $layoutString = array(); 
  4. private $debug = false; 
  5. private $layoutType = false; 
  6. private $config = array(); 
  7. private $nests = array(); 
  8. private $output = ''; 
  9. private $paged = false; 
  10. public $grid = array(); 
  11.  
  12.  
  13. function __construct($config) { 
  14.  
  15. $this->config = $config;//json_decode(file_get_contents(plugin_dir_path(__FILE__) . '/engine-config.json'), true); 
  16. if(empty($this->config)) { 
  17. echo 'Error loading engine config'; 
  18. die; 
  19. public function debug() { 
  20. $this->debug = true; 
  21. public function setLayout($str) { 
  22. // find pages 
  23. if( false !== strpos($str, '#') ) { 
  24. $this->paged = true; 
  25. // find nests 
  26. preg_match_all("/\[[0-9:\|]+\]/", $str, $matches); 
  27. if(!empty($matches[0])) { 
  28. foreach($matches[0] as $key=>$nest) { 
  29. $port = uniqid('__'); 
  30. $this->nests[$port] = substr($nest, 1, strlen($nest)-2); 
  31. $str = str_replace($nest, $port, $str); 
  32. $this->grid = $this->splitString($str); 
  33. private function splitString($str) { 
  34.  
  35. $rows = explode('|', $str); 
  36. $grid = array(); 
  37. foreach($rows as $row=>$cols) { 
  38. $cols = explode(':', $cols); 
  39. foreach($cols as $col=>$span) {  
  40. $nest = strpos($span, '__'); 
  41. if($nest !== false) { 
  42. $grid[$row+1][$col+1] = $this->splitString($this->nests[substr($span, $nest)]); 
  43. $grid[$row+1][$col+1]['span'] = $span; 
  44. $grid[$row+1][$col+1]['html'] = ''; 
  45.  
  46. return $grid; 
  47. static function mergeArray($first, $second, $type = 'replace') {  
  48. foreach($second as $key => $value) { 
  49. if(is_array($value)) { 
  50. if(!isset($first[$key])) { 
  51. $first[$key] = array(); 
  52. $first[$key] = self::mergeArray($first[$key], $value, $type); 
  53. }else{ 
  54. switch ($type) { 
  55. case 'replace': 
  56. $first[$key] = $value; 
  57. break; 
  58. case 'append': 
  59. if(empty($first[$key])) { 
  60. $first[$key] = $value; 
  61. }else{ 
  62. $first[$key] .= $value; 
  63. break; 
  64. case 'prepend': 
  65. if(empty($first[$key])) { 
  66. $first[$key] = $value; 
  67. }else{ 
  68. $first[$key] = $value.$first[$key]; 
  69. $first[$key] = $value.$first[$key]; 
  70. break; 
  71. return $first; 
  72. static function mapValue($type, $value, &$map) { 
  73. $out = '';$end = ''; 
  74. $map = explode(':', $map); 
  75. foreach($map as $key=>$val) { 
  76. $out .= '{"'.$val.'":'; 
  77. $end .= "}"; 
  78. $map = json_decode($out.json_encode(array($type=>$value)).$end, true);  
  79. public function html($html, $map, $type = 'replace') { 
  80. $this->mapValue('html', $html, $map); 
  81. $this->grid = self::mergeArray($this->grid, $map, $type); 
  82. public function before($html, $map, $type = 'replace') { 
  83. $this->mapValue('before', $html, $map); 
  84. $this->grid = self::mergeArray($this->grid, $map, $type); 
  85. public function after($html, $map, $type = 'replace') { 
  86. $this->mapValue('after', $html, $map); 
  87. $this->grid = self::mergeArray($this->grid, $map, $type); 
  88. public function append($html, $map) { 
  89. self::html($html, $map, 'append'); 
  90. public function prepend($html, $map) { 
  91. self::html($html, $map, 'prepend'); 
  92. public function setClass($class, $map) { 
  93. $this->mapValue('class', $class, $map); 
  94. $this->grid = self::mergeArray($this->grid, $map); 
  95. public function appendClass($class, $map) { 
  96. $this->mapValue('class', $class, $map); 
  97. $this->grid = self::mergeArray($this->grid, $map, 'append');  
  98. public function prependClass($class, $map) { 
  99. $this->mapValue('class', $class, $map); 
  100. $this->grid = self::mergeArray($this->grid, $map, 'prepend'); 
  101. }  
  102. public function setRowID($ID, $row) { 
  103. if(!isset($this->grid[$row])) {return;} 
  104. $this->grid[$row]['id'] = $ID; 
  105. public function setID($ID, $map) { 
  106. $this->mapValue('id', $ID, $map); 
  107. $this->grid = self::mergeArray($this->grid, $map); 
  108. public function renderLayout($grid = false) { 
  109. $inner = true; 
  110. if(empty($this->grid)) { 
  111. return 'ERROR: Layout string not set.'; 
  112. if(empty($grid)) { 
  113. $inner = false; 
  114. $grid = $this->grid; 
  115.  
  116. foreach($grid as $row=>$cols) { 
  117.  
  118. $rowID = ''; 
  119. $rowClass = ''; 
  120. $rowBefore = ''; 
  121. $rowAfter = ''; 
  122.  
  123. if(isset($cols['id'])) { 
  124. $rowID = $cols['id']; 
  125. unset($cols['id']); 
  126.  
  127. if( empty( $rowID ) ) { 
  128. $rowID = $row; 
  129.  
  130. if( ! empty( $this->config[ 'form_id_attr' ] ) ) { 
  131. $rowID = $this->config[ 'form_id_attr' ] . '-row-' . $rowID; 
  132.  
  133. /** 
  134. * Alter row ID attribute in Caldera Grid 
  135. * @since 1.4.9 
  136. * @param string $rowID The row's ID attribute 
  137. * @param int $row Row number 
  138. * @param array $config Grid config. Contains form ID in form_id key. 
  139. */ 
  140. $rowID = apply_filters( 'caldera_forms_grid_row_id', $rowID, $row, $this->config ); 
  141.  
  142. $rowID = 'id="'.$rowID.'" '; 
  143.  
  144. if(isset($cols['class'])) { 
  145. $rowClass = $cols['class']; 
  146. unset($cols['class']); 
  147.  
  148. if(isset($grid['*']['class'])) { 
  149. $rowClass .= $grid['*']['class']; 
  150.  
  151. if($row === 1 && $row !== count($grid)) { 
  152. $rowClass .= " ".$this->config['first']; 
  153. }elseif ($row === count($grid) && $row !== 1) { 
  154. $rowClass .= " ".$this->config['last']; 
  155. }elseif ($row === count($grid) && $row === 1) { 
  156. $rowClass .= " ".$this->config['single']; 
  157.  
  158. if(isset($cols['before'])) { 
  159. $this->output .= $cols['before']; 
  160. unset($cols['before']); 
  161.  
  162. /** 
  163. * Alter row class attribute in Caldera Grid 
  164. * STRONGLY recommended you use this to add, but not subtract classes. 
  165. * @since 1.4.9 
  166. * @param string $rowClass The row's classes 
  167. * @param int $row Row number 
  168. * @param array $config Grid config. Contains form ID in form_id key. 
  169. */ 
  170. $rowClass = apply_filters( 'caldera_forms_grid_row_class', $rowClass, $row, $this->config ); 
  171. $this->output .= sprintf($this->config['before'], $rowID, $rowClass); 
  172.  
  173. if(!is_array($cols)) { 
  174. echo $cols; 
  175. }else{ 
  176. foreach($cols as $col=>$content) { 
  177. if(!is_array($content) || empty($content)) { 
  178. continue; 
  179. if(!isset($content['span'])) {continue;} 
  180. $colClass = ''; 
  181. if(isset($content['class'])) { 
  182. $colClass = $content['class']; 
  183. unset($content['class']); 
  184. if(isset($cols['*']['class'])) { 
  185. $colClass .= $cols['*']['class']; 
  186.  
  187. if($col === 1 && $col !== count($cols)) { 
  188. $colClass .= " ".$this->config['column_first']; 
  189. }elseif($col === count($cols) && $col !== 1) { 
  190. $colClass .= " ".$this->config['column_last']; 
  191. }elseif($col === count($cols) && $col === 1) { 
  192. $colClass .= " ".$this->config['column_single']; 
  193. $colID = ''; 
  194. if(isset($content['id'])) { 
  195. $colID = 'id="'.$content['id'].'"'; 
  196. unset($content['id']); 
  197. if(isset($content['before'])) { 
  198. $this->output .= $content['before']; 
  199. unset($content['before']); 
  200. $afterBuffer = ''; 
  201. if(isset($content['after'])) { 
  202. $afterBuffer = $content['after']; 
  203. unset($content['after']); 
  204. $span = (!empty($this->config['column_spans'][$content['span']]) ? $this->config['column_spans'][$content['span']] : $content['span']); 
  205. $this->output .= sprintf($this->config['column_before'], $colID, $span, $colClass);//" <div class=\"span".$content['span']." ".$colClass."\">\n"; 
  206. $this->output .= $content['html']; 
  207. unset($content['html']); 
  208. unset($content['span']); 
  209. if(!empty($content)) { 
  210. $this->output = $this->renderLayout($content);  
  211. $this->output .= $this->config['column_after']; 
  212. $this->output .= $afterBuffer;  
  213. $this->output .= $this->config['after'];//"</div>\n";  
  214. if(isset($cols['after'])) { 
  215. $this->output .= $cols['after']; 
  216.  
  217.  
  218. return $this->output; 
  219.