00001 <?php
00002
00003
00004
00005 class WidgetYAMLSubtemplates implements IWidget {
00006
00007
00008
00009 const SLOTS_2_50 = '2';
00010
00011
00012
00013 const SLOTS_2_25_75 = '25_75';
00014
00015
00016
00017 const SLOTS_2_75_25 = '75_25';
00018
00019
00020
00021 const SLOTS_2_33_66 = '33_66';
00022
00023
00024
00025 const SLOTS_2_66_33 = '66_33';
00026
00027
00028
00029 const SLOTS_2_GOLDEN_RATIO_LEFT = 'golden_ratio_left';
00030
00031
00032
00033 const SLOTS_2_GOLDEN_RATIO_RIGHT = 'golden_ratio_right';
00034
00035
00036
00037 const SLOTS_3_33 = '3';
00038
00039
00040
00041 const SLOTS_4_25 = '4';
00042
00043
00044
00045 const SLOTS_5_20 = '5';
00046
00047
00048
00049 const SLOTS_AUTO = 'AUTO';
00050
00051 protected $slotwidths;
00052 protected $data;
00053 protected $cls;
00054
00055
00056
00057
00058
00059
00060
00061 public static function output($arr_data, $arr_slotswidths, $cls = '') {
00062 $w = new WidgetYAMLSubtemplates($arr_data, $arr_slotswidths, $cls);
00063 return $w->render();
00064 }
00065
00066 public function __construct($arr_data, $arr_slotswidths, $cls) {
00067 $this->data = $arr_data;
00068 if (is_array($arr_slotswidths)) {
00069 $this->slotwidths = $arr_slotswidths;
00070 }
00071 else {
00072 $this->slotwidths = $this->translate_slot($arr_slotswidths);
00073 }
00074 $this->cls = $cls;
00075 }
00076
00077 protected function translate_slot($slot) {
00078 switch ($slot) {
00079 case self::SLOTS_AUTO:
00080 $c = count($this->data);
00081 if ($c > 0) {
00082 return array_fill(0, $c, floor(100/$c));
00083 } else {
00084 return array(50, 50);
00085 }
00086 case self::SLOTS_2_33_66:
00087 return array(33, 66);
00088 case self::SLOTS_2_66_33:
00089 return array(66, 33);
00090 case self::SLOTS_2_25_75:
00091 return array(25, 75);
00092 case self::SLOTS_2_75_25:
00093 return array(75, 25);
00094 case self::SLOTS_2_GOLDEN_RATIO_LEFT:
00095 return array(38, 62);
00096 case self::SLOTS_2_GOLDEN_RATIO_RIGHT:
00097 return array(62, 38);
00098 case self::SLOTS_3_33:
00099 return array(33, 33, 33);
00100 case self::SLOTS_4_25:
00101 return array(25, 25, 25, 25);
00102 case self::SLOTS_5_20:
00103 return array(20, 20, 20, 20, 20);
00104 case self::SLOTS_2_50:
00105 default:
00106 return array(50, 50);
00107 }
00108 }
00109
00110 public function render($policy = self::NONE) {
00111 $slots = $this->create_slots($this->slotwidths);
00112 $ret = '';
00113 while(count($this->data)) {
00114 $row = '';
00115 foreach($slots as $slot) {
00116 $row .= html::div(html::div(array_shift($this->data), $slot['subc']), $slot['cls']);
00117 $row .= "\n";
00118 }
00119 $ret .= html::div($row, 'subcolumns ' . $this->cls) . "\n";
00120 }
00121 return $ret;
00122 }
00123
00124 protected function create_slots($slotwidths) {
00125 $ret = array();
00126 $c = count($slotwidths);
00127 for($i = 0; $i < $c; $i++) {
00128 $appendix = 'l';
00129 $appendix_sub = '';
00130 if ($i == ($c - 1)) {
00131 $appendix = 'r';
00132 $appendix_sub = 'r';
00133 } else if ($i == 0) {
00134 $appendix_sub = 'l';
00135 }
00136 $ret[] = array('cls' => 'c' . $slotwidths[$i] . $appendix, 'subc' => 'subc'. $appendix_sub);
00137 }
00138 return $ret;
00139 }
00140 }