<?php

// test05.php -- HotCRP paper submission tests
// HotCRP is Copyright (c) 2006-2016 Eddie Kohler and Regents of the UC
// Distributed under an MIT-like license; see LICENSE
global $ConfSitePATH;
$ConfSitePATH = preg_replace(",/[^/]+/[^/]+\$,", "", __FILE__);
require_once "{$ConfSitePATH}/test/setup.php";
$Conf->save_setting("sub_open", 1);
$Conf->save_setting("sub_update", $Now + 100);
$Conf->save_setting("sub_sub", $Now + 100);
// load users
$user_estrin = Contact::find_by_email("*****@*****.**");
// pc
$user_nobody = new Contact();
$ps = new PaperStatus($user_estrin);
$paper1a = $ps->paper_json(1);
xassert_eqq($paper1a->title, "Scalable Timers for Soft State Protocols");
$ps->save_paper_json((object) ["id" => 1, "title" => "Scalable Timers? for Soft State Protocols"]);
xassert(!$ps->nerrors);
$paper1b = $ps->paper_json(1);
xassert_eqq($paper1b->title, "Scalable Timers? for Soft State Protocols");
$paper1b->title = $paper1a->title;
$paper1b->submitted_at = $paper1a->submitted_at;
xassert_eqq(json_encode($paper1b), json_encode($paper1a));
$doc = Filer::file_upload_json(["error" => UPLOAD_ERR_OK, "name" => "amazing-sample.pdf", "tmp_name" => "{$ConfSitePATH}/src/sample.pdf", "tmp_name_safe" => true, "type" => "application/pdf"]);
$ps->save_paper_json((object) ["id" => 1, "submission" => $doc]);
xassert(!$ps->nerrors);
$paper1c = $ps->paper_json(1);
xassert_eqq($paper1c->submission->sha1, "2f1bccbf1e0e98004c01ef5b26eb9619f363e38e");
xassert_exit();
 function parse_request($opt_pj, $qreq, Contact $user, $pj)
 {
     $attachments = $opt_pj ?: [];
     $opfx = "opt{$this->id}_";
     foreach ($qreq->_FILES ?: [] as $k => $v) {
         if (str_starts_with($k, $opfx)) {
             $attachments[] = Filer::file_upload_json($v);
         }
     }
     for ($i = 0; $i < count($attachments); ++$i) {
         if (isset($attachments[$i]->docid) && $qreq["remove_opt{$this->id}_{$attachments[$i]->docid}"]) {
             array_splice($attachments, $i, 1);
             --$i;
         }
     }
     return empty($attachments) ? null : $attachments;
 }
 public function apply(Contact $user, $pj, $opj, $qreq, $action)
 {
     global $Conf;
     // Title, abstract, collaborators
     foreach (array("title", "abstract", "collaborators") as $k) {
         if (isset($qreq[$k])) {
             $pj->{$k} = $qreq[$k];
         }
     }
     // Authors
     $bad_author = ["name" => "Name", "email" => "Email", "aff" => "Affiliation"];
     $authors = array();
     foreach ($qreq as $k => $v) {
         if (preg_match('/\\Aau(name|email|aff)(\\d+)\\z/', $k, $m) && ($v = simplify_whitespace($v)) !== "" && $v !== $bad_author[$m[1]]) {
             $au = $authors[$m[2]] = get($authors, $m[2]) ?: (object) array();
             $x = $m[1] == "aff" ? "affiliation" : $m[1];
             $au->{$x} = $v;
         }
     }
     if (!empty($authors)) {
         ksort($authors, SORT_NUMERIC);
         $pj->authors = array_values($authors);
     }
     // Contacts
     if ($qreq->setcontacts || $qreq->has_contacts) {
         PaperSaver::replace_contacts($pj, $qreq);
     } else {
         if (!$opj) {
             $pj->contacts = array($user);
         }
     }
     // Status
     if ($action === "submit") {
         $pj->submitted = true;
     } else {
         if ($action === "final") {
             $pj->final_submitted = true;
         } else {
             $pj->submitted = false;
         }
     }
     // Paper upload
     if ($qreq->_FILES->paperUpload) {
         if ($action === "final") {
             $pj->final = Filer::file_upload_json($qreq->_FILES->paperUpload);
         } else {
             if ($action === "update" || $action === "submit") {
                 $pj->submission = Filer::file_upload_json($qreq->_FILES->paperUpload);
             }
         }
     }
     // Blindness
     if ($action !== "final" && $Conf->subBlindOptional()) {
         $pj->nonblind = !$qreq->blind;
     }
     // Topics
     if ($qreq->has_topics) {
         $pj->topics = (object) array();
         foreach ($Conf->topic_map() as $tid => $tname) {
             if (+$qreq["top{$tid}"] > 0) {
                 $pj->topics->{$tname} = true;
             }
         }
     }
     // Options
     if (!isset($pj->options)) {
         $pj->options = (object) [];
     }
     foreach (PaperOption::option_list() as $o) {
         if ($qreq["has_opt{$o->id}"] && (!$o->final || $action === "final")) {
             $okey = $o->abbr;
             $pj->options->{$okey} = $o->parse_request(get($pj->options, $okey), $qreq, $user, $pj);
         }
     }
     if (!count(get_object_vars($pj->options))) {
         unset($pj->options);
     }
     // PC conflicts
     if ($Conf->setting("sub_pcconf") && ($action !== "final" || $user->privChair) && $qreq->has_pcconf) {
         $cmax = $user->privChair ? CONFLICT_CHAIRMARK : CONFLICT_MAXAUTHORMARK;
         $pj->pc_conflicts = (object) array();
         foreach (pcMembers() as $pcid => $pc) {
             $ctype = cvtint($qreq["pcc{$pcid}"], 0);
             $ctype = max(min($ctype, $cmax), 0);
             if ($ctype) {
                 $email = $pc->email;
                 $pj->pc_conflicts->{$email} = Conflict::$type_names[$ctype];
             }
         }
     }
 }