1<?php 2// searchselection.php -- HotCRP helper class for paper selections 3// Copyright (c) 2006-2018 Eddie Kohler; see LICENSE. 4 5class SearchSelection { 6 private $sel = array(); 7 private $selmap = null; 8 9 function __construct($papers = null) { 10 if ($papers) { 11 $selmap = []; 12 foreach ($papers as $pid) 13 if (($pid = cvtint($pid)) > 0 && !isset($selmap[$pid])) 14 $this->sel[] = $selmap[$pid] = $pid; 15 } 16 } 17 18 static function make($qreq, Contact $user = null, $key = null) { 19 $ps = null; 20 if ($key !== null) { 21 $ps = $qreq->get_a($key); 22 } else { 23 $ps = $qreq->get_a(isset($qreq["p"]) ? "p" : "pap"); 24 } 25 if ($user && $ps === "all") { 26 $ps = (new PaperSearch($user, $qreq))->sorted_paper_ids(); 27 } else if ($ps === "all") 28 $ps = null; 29 if (is_string($ps)) 30 $ps = preg_split('/\s+/', $ps); 31 return new SearchSelection($ps); 32 } 33 34 static function clear_request(Qrequest $qreq) { 35 unset($qreq->p, $qreq->pap, $_GET["p"], $_GET["pap"], $_POST["p"], $_POST["pap"]); 36 } 37 38 function is_empty() { 39 return empty($this->sel); 40 } 41 42 function count() { 43 return count($this->sel); 44 } 45 46 function selection() { 47 return $this->sel; 48 } 49 50 function selection_at($i) { 51 return get($this->sel, $i); 52 } 53 54 function selection_map() { 55 if ($this->selmap === null) { 56 $this->selmap = array(); 57 foreach ($this->sel as $i => $pid) 58 $this->selmap[$pid] = $i + 1; 59 } 60 return $this->selmap; 61 } 62 63 function is_selected($pid) { 64 if ($this->selmap === null) 65 $this->selection_map(); 66 return isset($this->selmap[$pid]); 67 } 68 69 function selection_index($pid) { 70 return get($this->selection_map(), $pid, 0) - 1; 71 } 72 73 function sort_selection() { 74 sort($this->sel); 75 $this->selmap = null; 76 } 77 78 function equals_search($search) { 79 if ($search instanceof PaperSearch) 80 $search = $search->paper_ids(); 81 if (count($search) !== count($this->sel)) 82 return false; 83 sort($search); 84 $sel = $this->sel; 85 sort($sel); 86 return join(" ", $search) === join(" ", $sel); 87 } 88 89 function sql_predicate() { 90 return sql_in_numeric_set($this->sel); 91 } 92 93 function request_value() { 94 return join(" ", $this->sel); 95 } 96 97 function reorder($a) { 98 $ax = array(); 99 foreach ($this->sel as $pid) 100 if (array_key_exists($pid, $a)) 101 $ax[$pid] = $a[$pid]; 102 return $ax; 103 } 104} 105