HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbAlert.php
Go to the documentation of this file.
1 <?php
9 Yii::import('booster.widgets.TbWidget');
19 class TbAlert extends TbWidget {
20 
21  const CTX_ERROR = 'error';
22  const CTX_ERROR_CLASS = 'danger';
23 
46  public $alerts;
47 
54  public $closeText = '&times;';
55 
59  public $fade = true;
60 
69  public $events = array();
70 
74  public $htmlOptions = array();
75 
82  public $userComponentId = 'user';
83 
84  protected static $_containerId = 0;
85 
91  public function init() {
92 
93  if (!isset($this->htmlOptions['id'])) {
94  $this->htmlOptions['id'] = $this->getId();
95  }
96 
97  if (is_string($this->alerts)) {
98  $this->alerts = array($this->alerts);
99  }
100 
101  // Display all alert types by default.
102  if (!isset($this->alerts)) {
103  $this->alerts = array(
104  self::CTX_SUCCESS,
105  self::CTX_INFO,
106  self::CTX_WARNING,
107  self::CTX_DANGER,
108  self::CTX_ERROR
109  );
110  }
111  }
112 
118  public function run() {
119 
120  $id = $this->htmlOptions['id'];
121 
122  echo CHtml::openTag('div', $this->htmlOptions);
123 
124  foreach ($this->alerts as $type => $alert) {
125 
126  if (is_string($alert)) {
127  $type = $alert;
128  $alert = array();
129  }
130 
131  if (isset($alert['visible']) && $alert['visible'] === false) {
132  continue;
133  }
134 
136  $userComponent = Yii::app()->getComponent($this->userComponentId);
137  if (!$userComponent->hasFlash($type))
138  continue;
139 
140  $alertText = $userComponent->getFlash($type);
141  if (empty($alertText)) { // null, ''
142  continue;
143  }
144 
145  $this->renderSingleAlert($alert, $type, $alertText);
146  }
147 
148  echo CHtml::closeTag('div');
149 
150  $id .= '_' . self::$_containerId++;
151  $selector = "#{$id} .alert";
152 
154  $cs = Yii::app()->getClientScript();
155  $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('{$selector}').alert();");
156 
157  foreach ($this->events as $name => $handler) {
158  $handler = CJavaScript::encode($handler);
159  $cs->registerScript(
160  __CLASS__ . '#' . $id . '_' . $name,
161  "jQuery('{$selector}').on('{$name}', {$handler});"
162  );
163  }
164  }
165 
171  protected function renderSingleAlert($alert, $context, $alertText) {
172 
173  $classes = array('alert in');
174 
175  if (!isset($alert['fade'])) {
176  $alert['fade'] = $this->fade;
177  }
178 
179  if ($alert['fade'] === true) {
180  $classes[] = 'fade';
181  }
182 
183  if ($this->isValidContext($context)) {
184  $classes[] = 'alert-' . $this->getContextClass($context);
185  }
186 
187  if (!isset($alert['htmlOptions'])) {
188  $alert['htmlOptions'] = array();
189  }
190 
191  $classes = implode(' ', $classes);
192  if (isset($alert['htmlOptions']['class'])) {
193  $alert['htmlOptions']['class'] .= ' ' . $classes;
194  } else {
195  $alert['htmlOptions']['class'] = $classes;
196  }
197 
198  echo CHtml::openTag('div', $alert['htmlOptions']);
199 
200  // Logic is this: if no type-specific `closeText` was defined, let's show `$this->closeText`.
201  // Else, show type-specific `closeText`. Treat 'false' differently.
202  if (!isset($alert['closeText'])) {
203  $alert['closeText'] = (isset($this->closeText) && $this->closeText !== false)
204  ? $this->closeText
205  : false;
206  }
207 
208  // If `closeText` which is in effect now is `false` then do not show button.
209  if ($alert['closeText'] !== false) {
210  echo '<a href="#" class="close" data-dismiss="alert">' . $alert['closeText'] . '</a>';
211  }
212 
213  echo $alertText;
214  echo CHtml::closeTag('div');
215  }
216 
220  protected function isValidContext($context = false) {
221  return in_array($context, [
222  self::CTX_SUCCESS,
223  self::CTX_INFO,
224  self::CTX_WARNING,
225  self::CTX_DANGER,
226  self::CTX_ERROR,
227  ]);
228  }
229 
230 }