lekoala /
silverstripe-blocks
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Blocks\Fields; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | use SilverStripe\Forms\FieldGroup; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Display fields in column |
||
| 10 | * |
||
| 11 | * @link https://getbootstrap.com/docs/4.4/layout/grid/ |
||
| 12 | * @link http://sassflexboxgrid.com/ |
||
| 13 | */ |
||
| 14 | class ColumnsField extends FieldGroup |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @config |
||
| 18 | * @var boolean |
||
| 19 | */ |
||
| 20 | private static $autosize = true; |
||
| 21 | /** |
||
| 22 | * @config |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private static $column_class = 'col'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $breakpoint = 'md'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $columnSizes = []; |
||
| 36 | |||
| 37 | |||
| 38 | public function __construct($children = null) |
||
| 39 | { |
||
| 40 | parent::__construct($children); |
||
| 41 | $this->setColumnCount(count($this->children)); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Called in template |
||
| 46 | * <div class="$Up.ColumnClass($Pos) $FirstLast $EvenOdd"> |
||
| 47 | * $FieldHolder |
||
| 48 | * </div> |
||
| 49 | * |
||
| 50 | * @param int $pos |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function ColumnClass($pos) |
||
| 54 | { |
||
| 55 | $col_class = self::config()->column_class; |
||
| 56 | $class = $col_class; |
||
| 57 | if ($this->breakpoint) { |
||
| 58 | $class .= '-' . $this->breakpoint; |
||
| 59 | if (isset($this->columnSizes[$pos])) { |
||
| 60 | $class .= '-' . $this->columnSizes[$pos]; |
||
| 61 | } elseif (self::config()->autosize) { |
||
| 62 | $autoSize = round(12 / count($this->children)); |
||
| 63 | $class .= '-' . $autoSize . ' ' . $col_class . '-xs-12'; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | return $class; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the value of breakpoint |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getBreakpoint() |
||
| 74 | { |
||
| 75 | return $this->breakpoint; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set the value of breakpoint |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function setBreakpoint($breakpoint) |
||
| 83 | { |
||
| 84 | $this->breakpoint = $breakpoint; |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the value of columnSizes |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getColumnSizes() |
||
| 94 | { |
||
| 95 | return $this->columnSizes; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set size of columns based on their position |
||
| 100 | * |
||
| 101 | * Eg: [1 => 4, 2 => 8] |
||
| 102 | * |
||
| 103 | * @param array $columnSizes An position based array of sizes to assign to your columns |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setColumnSizes($columnSizes) |
||
| 107 | { |
||
| 108 | if (!is_array($columnSizes)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 109 | throw new InvalidArgumentException("columnSizes should be an array, Eg: [1 => 4, 2 => 8]"); |
||
| 110 | } |
||
| 111 | $this->columnSizes = $columnSizes; |
||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param int $col |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getColumnSize($col) |
||
| 120 | { |
||
| 121 | if (isset($this->columnSizes[$col])) { |
||
| 122 | return $this->columnSizes[$col]; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param int $col |
||
| 128 | * @param int $size |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setColumnSize($col, $size) |
||
| 132 | { |
||
| 133 | $this->columnSizes[$col] = $size; |
||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function Field($properties = array()) |
||
| 138 | { |
||
| 139 | $result = parent::Field($properties); |
||
| 140 | return $result; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function FieldHolder($properties = array()) |
||
| 144 | { |
||
| 145 | $result = parent::FieldHolder($properties); |
||
| 146 | return $result; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |