HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbUiLayout.php
Go to the documentation of this file.
1 <?php
2 
14 class TbUiLayout extends CWidget
15 {
16  public $jsVarName;
17 
21  public $htmlOptions = array();
22 
24  public $options = array();
25 
30  public $layouts = array();
31 
35  public function init()
36  {
37  Booster::getBooster()->registerPackage('ui-layout');
38 
39  if (!is_array($this->options)) {
40  $this->options = array();
41  }
42 
43  if (!is_array($this->htmlOptions)) {
44  $this->htmlOptions = array();
45  }
46  }
47 
51  public function run()
52  {
54  $cs = Yii::app()->getClientScript();
55 
56  // Javascript var
57  if (empty($this->jsVarName)) {
58  $this->jsVarName = $this->getId() . 'Layout';
59  }
60 
61  // Container ID
62  if (!isset($this->htmlOptions['id'])) {
63  $this->htmlOptions['id'] = $this->getId();
64  }
65 
66  $id = $this->htmlOptions['id'];
67 
68  echo CHtml::openTag('div', $this->htmlOptions);
69  $layoutsOptions = $this->renderLayouts();
70  echo '</div>';
71 
72  // Prepare options
73  $options = CMap::mergeArray($this->options, $layoutsOptions);
74  $options = empty($options) ? '' : CJavaScript::encode($options);
75 
76  // Register global JS var
77  $cs->registerScript(__CLASS__ . '#jsVar#' . $this->getId(), 'var '.$this->jsVarName.';', CClientScript::POS_HEAD);
78  // Register Layouts init script
79  $cs->registerScript(__CLASS__ . '#init#' . $this->getId(), $this->jsVarName.' = $("#'.$id.'").layout('.$options.');', CClientScript::POS_READY);
80  }
81 
85  protected function renderLayouts()
86  {
87  $layoutsOptions = array();
88  $availableLayouts = array('center', 'north', 'south', 'east', 'west');
89 
90  foreach ($availableLayouts as $layoutName) {
91  if (!isset($this->layouts[$layoutName])) {
92  continue;
93  }
94  $layout = $this->layouts[$layoutName];
95 
96  // Empty
97  if (!is_array($layout) && !isset($layout['content'])) {
98  continue;
99  }
100 
101  // Build content
102  $content = $layout['content'];
103  if (is_array($content) && !empty($content['class'])) {
104  $class = $content['class'];
105  unset($content['class']);
106  $content = $this->widget($class, $content, true);
107  }
108 
109  // Options
110  if (isset($layout['options']) && is_array($layout['options'])) {
111  foreach ($layout['options'] as $key => $value) {
112  if (strpos($key, $layoutName . '__') === false) {
113  $key = $layoutName . '__' . $key;
114  }
115  $layoutsOptions[$key] = $value;
116  }
117  }
118 
119  $htmlOptions = !empty($layout['htmlOptions']) ? $layout['htmlOptions'] : array();
120 
121  // Css class
122  if (!isset($htmlOptions['class'])) {
123  $htmlOptions['class'] = 'ui-layout-' . $layoutName;
124  } else {
125  $htmlOptions['class'] .= 'ui-layout-' . $layoutName;
126  }
127 
128  // Print out
129  echo CHtml::openTag('div', $htmlOptions);
130  echo $content.'</div>';
131  }
132 
133  return $layoutsOptions;
134  }
135 }