| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\ReportLog; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Core; |
| 26 |
|
| 27 |
class ReportLog |
| 28 |
{ |
| 29 |
public const ORDER_ASCENT = 1; |
| 30 |
public const ORDER_DESCENT = 2; |
| 31 |
|
| 32 |
private $db = null; |
| 33 |
private $filter = []; |
| 34 |
private $order = [ |
| 35 |
'direction' => 'ascent' |
| 36 |
]; |
| 37 |
private $rec_limit = 0; |
| 38 |
|
| 39 |
public function __construct($db = null) |
| 40 |
{ |
| 41 |
$this->db = $db ?? Core::instance()->database(); |
| 42 |
} |
| 43 |
|
| 44 |
public function setOrder(int $dir) |
| 45 |
{ |
| 46 |
$this->order['direction'] = ($dir === self::ORDER_DESCENT ? 'descent' : 'ascent'); |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
public function setMaxCount(int $n) |
| 51 |
{ |
| 52 |
$this->rec_limit = $n; |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function setFilter(array $filter) |
| 68 |
{ |
| 69 |
$this->filter = $filter; |
| 70 |
if (isset($filter['source'])) { |
| 71 |
$this->filter['source'] = ReportLogItem::stringToSource($filter['source']); |
| 72 |
} |
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
public function count(): int |
| 77 |
{ |
| 78 |
$limit = [ 'offset' => 0, 'count' => $this->rec_limit ]; |
| 79 |
return $this->db->getMapper('report-log')->count($this->filter, $limit); |
| 80 |
} |
| 81 |
|
| 82 |
public function getList(int $pos): array |
| 83 |
{ |
| 84 |
$max_rec = $this->rec_limit > 0 ? $this->rec_limit : 25; |
| 85 |
|
| 86 |
$limit = [ 'offset' => $pos, 'count' => $max_rec + 1 ]; |
| 87 |
|
| 88 |
$list = $this->db->getMapper('report-log')->list($this->filter, $this->order, $limit); |
| 89 |
if (count($list) > $max_rec) { |
| 90 |
$more = true; |
| 91 |
unset($list[$max_rec]); |
| 92 |
} else { |
| 93 |
$more = false; |
| 94 |
} |
| 95 |
foreach ($list as &$it) { |
| 96 |
$it['source'] = ReportLogItem::sourceToString($it['source']); |
| 97 |
} |
| 98 |
unset($it); |
| 99 |
|
| 100 |
return [ |
| 101 |
'items' => $list, |
| 102 |
'more' => $more |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
public function delete() |
| 107 |
{ |
| 108 |
$limit = [ 'offset' => 0, 'count' => $this->rec_limit ]; |
| 109 |
$this->db->getMapper('report-log')->delete($this->filter, $this->order, $limit); |
| 110 |
} |
| 111 |
} |