2011-01-16 22:51:39 +01:00
|
|
|
<?php
|
|
|
|
|
2013-03-09 22:52:21 +01:00
|
|
|
abstract class AphrontView extends Phobject
|
|
|
|
implements PhutilSafeHTMLProducerInterface {
|
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(
|
2013-03-02 00:37:32 +01:00
|
|
|
pht("View '%s' does not support children.", $class));
|
2012-11-21 03:01:18 +01:00
|
|
|
}
|
2011-01-16 22:51:39 +01:00
|
|
|
$this->children[] = $child;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
final protected function renderChildren() {
|
2013-01-29 03:09:00 +01:00
|
|
|
$out = array();
|
|
|
|
foreach ($this->children as $child) {
|
2013-02-11 23:43:25 +01:00
|
|
|
$out[] = $this->renderSingleView($child);
|
2013-01-29 03:09:00 +01:00
|
|
|
}
|
|
|
|
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-13 23:50:15 +01:00
|
|
|
return phutil_implode_html('', $out);
|
2011-01-16 22:51:39 +01:00
|
|
|
} else {
|
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2013-03-09 22:52:21 +01:00
|
|
|
public function producePhutilSafeHTML() {
|
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|