2011-01-16 22:51:39 +01:00
|
|
|
<?php
|
|
|
|
|
2012-12-19 01:11:07 +01:00
|
|
|
abstract class AphrontView extends Phobject {
|
2011-01-16 22:51:39 +01:00
|
|
|
|
2012-12-20 23:27:12 +01:00
|
|
|
protected $user;
|
2011-01-16 22:51:39 +01:00
|
|
|
protected $children = array();
|
|
|
|
|
2012-12-20 23:27:12 +01:00
|
|
|
public function setUser(PhabricatorUser $user) {
|
|
|
|
$this->user = $user;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2012-11-21 03:01:18 +01:00
|
|
|
protected function canAppendChild() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
final public function appendChild($child) {
|
2012-11-21 03:01:18 +01:00
|
|
|
if (!$this->canAppendChild()) {
|
|
|
|
$class = get_class($this);
|
|
|
|
throw new Exception(
|
|
|
|
"View '{$class}' does not support children.");
|
|
|
|
}
|
2011-01-16 22:51:39 +01:00
|
|
|
$this->children[] = $child;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function renderChildren() {
|
|
|
|
$out = array();
|
|
|
|
foreach ($this->children as $child) {
|
2011-03-13 01:17:34 +01:00
|
|
|
$out[] = $this->renderSingleView($child);
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|
|
|
|
return implode('', $out);
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:09:00 +01:00
|
|
|
final protected function renderHTMLChildren() {
|
|
|
|
$out = array();
|
|
|
|
foreach ($this->children as $child) {
|
|
|
|
$out[] = $this->renderHTMLView($child);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2011-03-13 01:17:34 +01:00
|
|
|
final protected function renderSingleView($child) {
|
2011-01-16 22:51:39 +01:00
|
|
|
if ($child instanceof AphrontView) {
|
|
|
|
return $child->render();
|
|
|
|
} else if (is_array($child)) {
|
|
|
|
$out = array();
|
|
|
|
foreach ($child as $element) {
|
2011-03-13 01:17:34 +01:00
|
|
|
$out[] = $this->renderSingleView($element);
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|
2013-02-11 22:16:57 +01:00
|
|
|
return phutil_implode_html('', $out);
|
2011-01-16 22:51:39 +01:00
|
|
|
} else {
|
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 21:57:47 +01:00
|
|
|
final protected function renderHTMLView($child) {
|
|
|
|
if ($child instanceof AphrontView) {
|
2013-02-11 22:16:57 +01:00
|
|
|
return $child->render();
|
2013-01-25 21:57:47 +01:00
|
|
|
} else if ($child instanceof PhutilSafeHTML) {
|
|
|
|
return $child;
|
|
|
|
} else if (is_array($child)) {
|
|
|
|
$out = array();
|
|
|
|
foreach ($child as $element) {
|
|
|
|
$out[] = $this->renderHTMLView($element);
|
|
|
|
}
|
2013-02-11 22:16:57 +01:00
|
|
|
return phutil_implode_html('', $out);
|
2013-01-25 21:57:47 +01:00
|
|
|
} else {
|
2013-02-11 22:16:57 +01:00
|
|
|
return hsprintf('%s', $child);
|
2013-01-25 21:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:09:33 +01:00
|
|
|
final protected function isEmptyContent($content) {
|
|
|
|
if (is_array($content)) {
|
|
|
|
foreach ($content as $element) {
|
|
|
|
if (!$this->isEmptyContent($element)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return !strlen((string)$content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
abstract public function render();
|
|
|
|
|
|
|
|
}
|