Beispiel #1
0
 public function post()
 {
     $xmlService = new XmlService();
     $xmlFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR;
     switch (true) {
         case $this->getRequest()->getPost('xmlData'):
             $xmlFile .= 'xml_data_' . date('YdmHis') . '.xml';
             file_put_contents($xmlFile, trim($this->getRequest()->getPost('xmlData')));
             break;
         case $this->getRequest()->getFile('xmlFile'):
             $file = $this->getRequest()->getFile('xmlFile');
             $xmlFile .= $file['name'];
             try {
                 $uploadService = new FileUploadService($this->getRequest()->getFile('xmlFile'));
                 $uploadService->upload($xmlFile);
             } catch (\Exception $e) {
                 $this->addError($e->getMessage());
             }
             break;
         default:
             $this->addError('Invalid XML data');
             return;
     }
     try {
         if (!$this->hasErrors()) {
             $xmlService->saveDocument($xmlFile);
         }
     } catch (\Exception $e) {
         $this->addError($e->getMessage());
     }
 }
Beispiel #2
0
 public function get()
 {
     $service = new XmlService();
     if ($this->getRequest()->getParam('id')) {
         try {
             $xml = $service->getDocumentById($this->getRequest()->getParam('id'));
             /**
              *  a bit ugly but it's just for the purpose of displaying the
              *  xml data 
              */
             header('Content-type: text/xml; charset=utf8;');
             echo $xml->toXml();
             die;
         } catch (\Exception $e) {
             $this->addError($e->getMessage());
         }
     } else {
         $documents = $service->getDocumentsList();
         if (!empty($documents)) {
             echo "<ul>";
             foreach ($documents as $document) {
                 echo "<li><a href='/documents/?id={$document['documents_id']}' target='_new'>{$document['name']}</a></li>";
             }
             echo "</ul>";
         } else {
             echo "No documents found";
         }
     }
 }