public function postAction() { $user_id = $this->zfcUserAuthentication()->getIdentity()->getId(); $form = new NewsForm(); $request = $this->getRequest(); if ($request->isPost()) { $imagine = $this->getServiceLocator()->get('image_service'); $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); $news = new News(); $form->setInputFilter($news->getInputFilter()); $form->setData($data); if ($form->isValid()) { $file_name = rand(999999, 9999999) . '.jpg'; foreach ($this->image_sizes as $sizes) { $mode = $sizes[2]; $folder = $this->image_folder . '/' . $sizes[0] . 'x' . $sizes[1]; @mkdir($this->image_folder . '/' . $sizes[0] . 'x' . $sizes[1], 0777, true); $size = new Box($sizes[0], $sizes[1]); $imagine->open($data['photo']['tmp_name'])->thumbnail($size, $mode)->save($folder . '/' . $file_name); } $news->exchangeArray($form->getData()); $news->user_id = $user_id; $news->photo = $file_name; $this->getNewsTable()->saveNews($news); return $this->redirect()->toRoute('account'); } } return array('form' => $form, 'user_id' => (int) $user_id); }
/** * Adds a new News to the db or opens up the form for adding, if it isn't opened yet. * * @return array|\Zend\Http\Response * @throws \Exception */ public function addAction() { $session = $session = new \Zend\Session\Container('user'); if (!PermissionChecker::check(Role::ELDER)) { return $this->redirect()->toRoute('account', ['action' => 'noright']); } $form = new NewsForm(); $form->get('category_id')->setValueOptions($this->createCategorySelect()); $request = $this->getRequest(); if ($request->isPost()) { $news = new News(); $form->setInputFilter($news->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $news->exchangeArray($form->getData()); if ($this->checkWordLengths($news->getContent())) { $this->getNewsTable()->saveNews($news); return $this->redirect()->toRoute('news'); } else { return ['form' => $form, 'accountId' => $session->id, 'error' => 'tooLong']; } } else { return ['form' => $form, 'accountId' => $session->id, 'error' => 'tooLong']; } } return ['form' => $form, 'accountId' => $session->id]; }