Paperyard
Archive.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
17 class Archive extends BasicController
18 {
20  private $rootPath = '/data/sort';
21 
23  private $archiveRelPath;
24 
27 
33  public function __construct(Twig $view, LoggerInterface $logger, Messages $flash)
34  {
35  $this->view = $view;
36  $this->logger = $logger;
37  $this->flash = $flash;
38 
39  $this->registerPlugin('clickable-row');
40  $this->registerPlugin('searchable-table');
41  $this->registerPlugin('datatables.min');
42  $this->registerPlugin('bootstrap-notify.min');
43  }
44 
51  public function __invoke(Request $request, Response $response, $args)
52  {
53  $this->archiveRelPath = $request->getAttribute('path');
54  $this->archiveFullPath = $this->rootPath . $this->archiveRelPath;
55 
56  if (!is_dir($this->archiveFullPath)) {
57  $this->flash->addMessage('error', _('Archive not found.'));
58  return $response->withRedirect('/archive');
59  }
60 
61  $this->view->render($response, 'archive/archive.twig', $this->render());
62  return $response;
63  }
64 
68  public function render()
69  {
70  return array(
71  'plugins' => parent::getPlugins(),
72  'languageFlag' => parent::getLanguageFlag(),
73  'archives' => $this->getArchives(),
74  'newestFiles' => $this -> getNewestFiles(),
75  'files' => $this->getFiles()
76  );
77  }
78 
84  private function getArchives()
85  {
86  // combine to current path
87  $archive_path = $this->rootPath . $this->archiveRelPath;
88 
89  // iterate over current folder and get every deeper folder
90  $archives = [];
91  $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($archive_path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
92  foreach ($iterator as $file) {
93  if ($file->isDir()) {
94  $archives[] = str_replace($this->rootPath, "", $file);
95  }
96  }
97  return $archives;
98  }
99 
105  private function getFiles()
106  {
107  // searchpattern for current archive
108  $archive_search_pattern = $this->archiveFullPath . "/*.pdf";
109 
110  // get files as strings from filesystem
111  $pdfs = glob($archive_search_pattern, GLOB_NOSORT);
112 
113  // convert string elements into ArchiveDocuments objects
114  array_walk($pdfs, function (&$pdf) {
115  $pdf = (new Document($pdf))->toArray();
116  });
117  return $pdfs;
118  }
119 
125  private function getNewestFiles()
126  {
127  // get archive list
128  $pdfs = $this -> getFiles();
129 
130  // sort archives by date
131  usort($pdfs, function($a, $b){
132  return $a['date'] < $b['date'];
133  });
134  return $pdfs;
135  }
136 }
registerPlugin($name, $type=PluginType::NORMAL)
__invoke(Request $request, Response $response, $args)
Definition: Archive.php:51
$pdfs
__construct(Twig $view, LoggerInterface $logger, Messages $flash)
Definition: Archive.php:33