public function addAction()
 {
     $form = new CalendarForm();
     $request = $this->getRequest();
     $calendarDAO = CalendarDAO::getInstance($this->getServiceLocator());
     $query = $request->getQuery();
     if ($request->isPost()) {
         $post = $request->getPost()->toArray();
         $form->setData($post);
         if ($form->isValid()) {
             $data = $form->getData();
             $dateTime = new \DateTime();
             $dateTime->setTimestamp(strtotime($data['date']));
             $data['date'] = $dateTime;
             $userData = new Calendar();
             $userData->setTitle($data['title']);
             $userData->setDescription($data['description']);
             $userData->setDate($data['date']);
             $calendarDAO->save($userData);
             $viewModel = new ViewModel(array('done' => true));
             $viewModel->setTerminal(true);
             return $viewModel;
         } else {
             $form->getMessages();
         }
     } elseif (!empty($query)) {
         $date = $query->date;
         $form->get('date')->setValue(date('d-M-Y', strtotime($date)));
     }
     $viewModel = new ViewModel(array('form' => $form));
     $viewModel->setTerminal(true);
     return $viewModel;
 }
 public function addAction()
 {
     $config = $this->getServiceLocator()->get('config');
     $applicationManager = ApplicationManager::getInstance($this->getServiceLocator());
     $form = new ClientsForm(array('services' => $applicationManager->prepareFormServices(), 'clinics' => $applicationManager->prepareFormClinics(), 'doctors' => $applicationManager->prepareFormDoctors(), 'countries' => $applicationManager->prepareFormCountries()));
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
         $form->setData($post);
         if ($form->isValid()) {
             $data = $form->getData();
             $dateTime = new \DateTime();
             $dateTime->setTimestamp(strtotime($data['dos']));
             $data['dos'] = $dateTime;
             $dateTime = new \DateTime();
             $dateTime->setTimestamp(strtotime($data['nextContactDate']));
             $data['nextContactDate'] = $dateTime;
             $dateTime = new \DateTime();
             $dateTime->setTimestamp(time());
             $now = $dateTime;
             if (!empty($data['newClinic'])) {
                 $newClinic = new Clinic();
                 $newClinic->setName($data['newClinic']);
                 ClinicDAO::getInstance($this->getServiceLocator())->save($newClinic);
                 $data['clinic'] = $newClinic->getId();
             }
             if (!empty($data['newDoctor'])) {
                 $newDoctor = new Doctor();
                 $newDoctor->setName($data['newDoctor']);
                 DoctorDAO::getInstance($this->getServiceLocator())->save($newDoctor);
                 $data['doctor'] = $newDoctor->getId();
             }
             if (!empty($data['newCountry'])) {
                 $newCountry = new Country();
                 $newCountry->setName($data['newCountry']);
                 CountryDAO::getInstance($this->getServiceLocator())->save($newCountry);
                 $data['country'] = $newDoctor->getId();
             }
             if (!empty($post['attachments'])) {
                 foreach ($post['attachments'] as $attach) {
                     if (!empty($attach['name'])) {
                         $attach['name'] = str_replace(' ', '_', $attach['name']);
                         move_uploaded_file($attach['tmp_name'], $config['app']['uploads_path'] . $attach['name']);
                         $attachmentNames[] = 'uploads/' . $attach['name'];
                     }
                 }
             }
             if (!empty($post['conclusions'])) {
                 foreach ($post['conclusions'] as $conclusion) {
                     if (!empty($conclusion['name'])) {
                         $conclusion['name'] = str_replace(' ', '_', $conclusion['name']);
                         move_uploaded_file($conclusion['tmp_name'], $config['app']['uploads_path'] . $conclusion['name']);
                         $conclusionNames[] = 'uploads/' . $conclusion['name'];
                     }
                 }
             } else {
                 $conclusionNames = array();
             }
             $client = new Clients();
             $client->setFio($data['fio']);
             $client->setService(ServiceDAO::getInstance($this->getServiceLocator())->findOneById($data['service']));
             $client->setDiagnosis($data['diagnosis']);
             $client->setContacts($data['contacts']);
             $client->setDOS($data['dos']);
             $client->setStatus($data['status']);
             $client->setComments($data['comments']);
             $client->setCountry(CountryDAO::getInstance($this->getServiceLocator())->findOneById($data['country']));
             $client->setNextContactDate($data['nextContactDate']);
             $client->setNextContactComment($data['nextContactComment']);
             $client->setAttachments(serialize($attachmentNames));
             $client->setClinic(ClinicDAO::getInstance($this->getServiceLocator())->findOneById($data['clinic']));
             $client->setDoctor(DoctorDAO::getInstance($this->getServiceLocator())->findOneById($data['doctor']));
             $client->setConclusion(serialize(array_unique($conclusionNames)));
             $client->setPayment($data['payment']);
             $client->setInformed((int) $data['informed']);
             $client->setDateAdded($now);
             $client->setManager($applicationManager->getCurrentUser());
             ClientsDAO::getInstance($this->getServiceLocator())->save($client);
             if (!empty($data['nextContactDate'])) {
                 $calendarDAO = CalendarDAO::getInstance($this->getServiceLocator());
                 $event = new Calendar();
                 $event->setTitle($data['fio']);
                 $event->setDescription($client->getNextContactComment());
                 $event->setDate($client->getNextContactDate());
                 $event->setClient($client);
                 $calendarDAO->save($event);
             }
             return $this->redirect()->toRoute('clients');
         }
     }
     return array('form' => $form);
 }
 /**
  * {@inheritDoc}
  */
 public function setTitle($title)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setTitle', array($title));
     return parent::setTitle($title);
 }