/**
  * Handles a given form for the add and edit action
  * @return array Array for the view, containing the form and maybe id and errors
  */
 private function handleForm(Request &$request, NewsCategoryForm &$form, NewsCategory &$nc, $id = 0)
 {
     $form->setInputFilter($nc->getInputFilter());
     $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
     $form->setData($post);
     if ($form->isValid()) {
         $nc->exchangeArray($post);
         $old = $this->getNewsCategoryTable()->getNewsCategoryBy(['id' => $id]);
         if ($this->getNewsCategoryTable()->getNewsCategoryBy(['name' => $nc->getName()]) && ($id === 0 || $nc->getName() !== $old->getName())) {
             $errors['name'] = ['exists' => 'A category with this name already exists'];
             $form->get('name')->setMessages($errors);
             if (!$id) {
                 return ['form' => $form, 'errors' => $errors];
             }
             return ['form' => $form, 'errors' => $errors, 'id' => $id];
         }
         $size = new Size(['min' => 20, 'max' => 20000]);
         $adapter = new Http();
         $adapter->setValidators([$size], $post['path']);
         //Only throw error if a new category is created. New Categories need an image
         if (!$adapter->isValid() && $id === 0) {
             $errors = $adapter->getMessages();
             return ['form' => $form, 'errors' => $errors];
         }
         $dir = getcwd() . '/public/news_cat/';
         //A file was given, so it will be saved on the server
         if ($adapter->isValid()) {
             if (!file_exists($dir)) {
                 mkdir($dir);
             }
             $pic = $post['path'];
             $file = file_get_contents($pic['tmp_name']);
             file_put_contents($dir . $nc->getName() . '.png', $file);
         } else {
             //No new file was given, so update the filename to the new name
             rename($dir . $old->getName() . '.png', $dir . $nc->getName() . '.png');
         }
         $this->getNewsCategoryTable()->saveNewsCategory($nc);
         return $this->redirect()->toRoute('newscategory');
     }
     $errors = $form->getMessages();
     if ($id) {
         return ['form' => $form, 'errors' => $errors, 'id' => $id];
     }
     return ['form' => $form, 'errors' => $errors];
 }