diff --git a/batch/reviewcsv.php b/batch/reviewcsv.php index 2a0967df0..c4e037f9e 100644 --- a/batch/reviewcsv.php +++ b/batch/reviewcsv.php @@ -51,7 +51,7 @@ class ReviewCSV_Batch { function __construct($conf) { $this->conf = $conf; $this->user = $conf->root_user(); - $this->fr = new FieldRender(FieldRender::CFLIST | FieldRender::CFCSV | FieldRender::CFHTML, $this->user); + $this->fr = new FieldRender(FieldRender::CFHTML | FieldRender::CFVERBOSE, $this->user); $this->csv = new CsvGenerator; $this->rfseen = $conf->review_form()->order_array(false); } diff --git a/src/fieldrender.php b/src/fieldrender.php index 3b263b2a5..735c81e39 100644 --- a/src/fieldrender.php +++ b/src/fieldrender.php @@ -3,11 +3,14 @@ // Copyright (c) 2006-2022 Eddie Kohler; see LICENSE. class FieldRender { - /** @var ?Contact */ + /** @var ?Contact + * @readonly */ public $user; - /** @var ?PaperTable */ + /** @var ?PaperTable + * @readonly */ public $table; - /** @var int */ + /** @var int + * @readonly */ public $context; /** @var null|false|string */ public $title; @@ -19,28 +22,39 @@ class FieldRender { public $value_long; const CFHTML = 0x1; - const CFPAGE = 0x2; - const CFLIST = 0x4; - const CFCOLUMN = 0x8; - const CFSUGGEST = 0x10; - const CFCSV = 0x20; - const CFMAIL = 0x40; - const CFFORM = 0x80; - const CFVERBOSE = 0x100; + const CFTEXT = 0x2; - const CTEXT = 0; - const CPAGE = 3; + const CFPAGE = 0x10; + const CFFORM = 0x20; + const CFLIST = 0x40; + const CFMAIL = 0x80; + const CFSUGGEST = 0x100; + + const CFCSV = 0x1000; + const CFROW = 0x2000; + const CFCOLUMN = 0x4000; + const CFVERBOSE = 0x8000; /** @param int $context */ function __construct($context, Contact $user = null) { + assert(($context & 3) !== 0 && ($context & 3) !== 3); + assert((($context & 0xFF0) & (($context & 0xFF0) - 1)) === 0); $this->context = $context; $this->user = $user; } - /** @param ?int $context */ - function clear($context = null) { - if ($context !== null) { - $this->context = $context; - } + /** @param PaperTable $table + * @return $this + * @suppress PhanAccessReadOnlyProperty */ + function make_table($table) { + assert(($this->context & self::CFHTML) === self::CFHTML); + assert(($this->context & (self::CFPAGE | self::CFFORM)) !== 0); + assert(!$this->table && (!$this->user || $this->user === $table->user)); + $this->user = $table->user; + $this->table = $table; + return $this; + } + + function clear() { $this->title = null; $this->value = $this->value_format = $this->value_long = null; } @@ -48,31 +62,43 @@ function clear($context = null) { function is_empty() { return (string) $this->title === "" && (string) $this->value === ""; } - /** @return bool */ + /** @param int $context + * @return bool */ + function want($context) { + return ($this->context & $context) === $context; + } + /** @return bool + * @deprecated */ function for_page() { return ($this->context & self::CFPAGE) !== 0; } - /** @return bool */ + /** @return bool + * @deprecated */ function for_form() { return ($this->context & self::CFFORM) !== 0; } - /** @return bool */ + /** @return bool + * @deprecated */ function want_text() { return ($this->context & self::CFHTML) === 0; } - /** @return bool */ + /** @return bool + * @deprecated */ function want_html() { return ($this->context & self::CFHTML) !== 0; } - /** @return bool */ + /** @return bool + * @deprecated */ function want_list() { return ($this->context & self::CFLIST) !== 0; } - /** @return bool */ + /** @return bool + * @deprecated */ function want_list_row() { return ($this->context & (self::CFLIST | self::CFCOLUMN)) === self::CFLIST; } - /** @return bool */ + /** @return bool + * @deprecated */ function want_list_column() { return ($this->context & (self::CFLIST | self::CFCOLUMN)) === (self::CFLIST | self::CFCOLUMN); @@ -98,13 +124,12 @@ function set_html($t) { /** @param bool $b * @return $this */ function set_bool($b) { - $v = $this->verbose(); - if ($this->context & self::CFHTML) { - $this->set_text($b ? "✓" : ($v ? "✗" : "")); - } else if ($this->context & self::CFCSV) { - $this->set_text($b ? "Y" : ($v ? "N" : "")); + if (($this->context & self::CFHTML) !== 0) { + $this->set_text($b ? "✓" : "✗"); + } else if (($this->context & self::CFCSV) !== 0) { + $this->set_text($b ? "Y" : "N"); } else { - $this->set_text($b ? "Yes" : ($v ? "No" : "")); + $this->set_text($b ? "Yes" : "No"); } return $this; } diff --git a/src/formula.php b/src/formula.php index 5b29bc134..852b632cf 100644 --- a/src/formula.php +++ b/src/formula.php @@ -2608,7 +2608,7 @@ function unparse_text($x, $real_format) { return $this->_format_detail->unparse_computed($rx, $real_format); } else if ($this->_format === Fexpr::FSUBFIELD) { $prow = $this->placeholder_prow(); - $fr = new FieldRender(FieldRender::CFCSV); + $fr = new FieldRender(FieldRender::CFTEXT | FieldRender::CFCSV | FieldRender::CFVERBOSE); $this->_format_detail->render($fr, new PaperValue($prow, $x)); return $fr->value; // XXX } else if ($this->_format === Fexpr::FPREFEXPERTISE) { diff --git a/src/hotcrpmailer.php b/src/hotcrpmailer.php index fb9147a37..46ba41f7d 100644 --- a/src/hotcrpmailer.php +++ b/src/hotcrpmailer.php @@ -469,7 +469,7 @@ function kw_paperfield($args, $isbool, $uf) { || !($ov = $this->row->option($uf->option))) { return $isbool ? false : ""; } else { - $fr = new FieldRender(FieldRender::CFMAIL, $this->permuser); + $fr = new FieldRender(FieldRender::CFTEXT | FieldRender::CFMAIL, $this->permuser); $uf->option->render($fr, $ov); if ($isbool) { return ($fr->value ?? "") !== ""; diff --git a/src/listactions/la_getabstracts.php b/src/listactions/la_getabstracts.php index 279e6f461..d1d6f829a 100644 --- a/src/listactions/la_getabstracts.php +++ b/src/listactions/la_getabstracts.php @@ -44,7 +44,7 @@ static function render(PaperInfo $prow, Contact $user) { $n = prefix_word_wrap("", "Submission #{$prow->paperId}: {$prow->title}", 0, self::WIDTH); $text = $n . str_repeat("=", min(self::WIDTH, strlen($n) - 1)) . "\n\n"; - $fr = new FieldRender(FieldRender::CTEXT, $user); + $fr = new FieldRender(FieldRender::CFTEXT, $user); foreach ($user->conf->options()->page_fields($prow) as $o) { if (($o->id <= 0 || $user->allow_view_option($prow, $o)) && $o->on_page()) { diff --git a/src/options/o_abstract.php b/src/options/o_abstract.php index 00af142c1..2187e53b6 100644 --- a/src/options/o_abstract.php +++ b/src/options/o_abstract.php @@ -41,7 +41,7 @@ function print_web_edit(PaperTable $pt, $ov, $reqov) { $this->print_web_edit_text($pt, $ov, $reqov, ["rows" => 5]); } function render(FieldRender $fr, PaperValue $ov) { - if ($fr->for_page()) { + if ($fr->want(FieldRender::CFPAGE)) { $fr->table->render_abstract($fr, $this); } else { $text = $ov->prow->abstract(); diff --git a/src/options/o_attachments.php b/src/options/o_attachments.php index 0e845ba6a..565151158 100644 --- a/src/options/o_attachments.php +++ b/src/options/o_attachments.php @@ -173,13 +173,14 @@ function print_web_edit(PaperTable $pt, $ov, $reqov) { function render(FieldRender $fr, PaperValue $ov) { $ts = []; foreach ($ov->document_set() as $d) { - if ($fr->want_text()) { + if ($fr->want(FieldRender::CFTEXT)) { $ts[] = $d->member_filename(); } else { $linkname = htmlspecialchars($d->member_filename()); - if ($fr->want_list()) { + $dif = 0; + if ($fr->want(FieldRender::CFLIST)) { $dif = DocumentInfo::L_SMALL | DocumentInfo::L_NOSIZE; - } else if ($fr->for_form()) { + } else if ($fr->want(FieldRender::CFFORM)) { $dif = 0; } else if ($this->display() === PaperOption::DISP_TOP) { $dif = 0; @@ -194,24 +195,26 @@ function render(FieldRender $fr, PaperValue $ov) { $ts[] = $t; } } - if (!empty($ts)) { - if ($fr->want_text()) { - $fr->set_text(join("; ", $ts)); - } else if ($fr->want_list_row()) { - $fr->set_html(''); - } else { - $fr->set_html(''); + if (empty($ts)) { + if ($fr->verbose()) { + $fr->set_text("None"); } - if ($fr->for_page() && $this->display() === PaperOption::DISP_TOP) { - $fr->title = false; - $v = ''; - if ($fr->table && $fr->user->view_option_state($ov->prow, $this) === 1) { - $v = ' fx8'; - } - $fr->value = "
{$fr->value}
"; + return; + } + if ($fr->want(FieldRender::CFTEXT)) { + $fr->set_text(join("; ", $ts)); + } else if ($fr->want(FieldRender::CFLIST | FieldRender::CFROW)) { + $fr->set_html(''); + } else { + $fr->set_html(''); + } + if ($fr->want(FieldRender::CFPAGE) && $this->display() === PaperOption::DISP_TOP) { + $fr->title = false; + $v = ''; + if ($fr->table && $fr->user->view_option_state($ov->prow, $this) === 1) { + $v = ' fx8'; } - } else if ($fr->verbose()) { - $fr->set_text("None"); + $fr->value = "
{$fr->value}
"; } } diff --git a/src/options/o_authors.php b/src/options/o_authors.php index 171ce2981..e7b2aaeb6 100644 --- a/src/options/o_authors.php +++ b/src/options/o_authors.php @@ -284,7 +284,7 @@ function print_web_edit(PaperTable $pt, $ov, $reqov) { } function render(FieldRender $fr, PaperValue $ov) { - if ($fr->for_page()) { + if ($fr->want(FieldRender::CFPAGE)) { $fr->table->render_authors($fr, $this); } else { $names = ["