Ejemplo n.º 1
0
 public function editAction()
 {
     $this->view->title = 'Edit user profile';
     $this->view->messages = $this->_helper->flashMessenger->getMessages();
     $form = new UserForm();
     $this->view->form = $form;
     $userId = $this->_request->getParam('id');
     if ($this->getUser()->getid_uzivatel() !== $userId && !$this->getUser()->isAdmin()) {
         // Redirects
         $this->_helper->redirector->gotoRoute(array('controller' => 'candidate', 'action' => 'index'), 'default', true);
         return;
     }
     if (!empty($userId)) {
         $user = My_Model::get('Users')->getById($userId);
         if ($user !== NULL) {
             $form->setDefaults($user->get_data());
             $avatar = $user->getFoto();
             if ($avatar !== NULL) {
                 $base64 = base64_encode($avatar->getfoto());
                 $form->avatar->setAttrib('src', "data:image/gif;base64," . $base64);
             }
         }
     }
     // ########################### POST ###########################
     // Handles form submission
     if ($this->_request->isPost()) {
         if ($this->_request->getPost('saveButton', false)) {
             if ($form->isValid($this->_request->getPost())) {
                 $formValues = $form->getValues();
                 // Profile photo
                 $photo;
                 if ($form->profilePhoto->isUploaded()) {
                     if (!$form->profilePhoto->receive()) {
                         print "Error receiving the file";
                     }
                     // Reads location and creates blob
                     $profilePhotoLocation = $form->profilePhoto->getFileName();
                     $profilePhotoBlob = file_get_contents($profilePhotoLocation);
                     if (!empty($profilePhotoBlob)) {
                         // Creates photo object
                         $photo = My_Model::get('Photos')->createRow();
                         $photo->foto = $profilePhotoBlob;
                         $photo->nazev = array_pop(explode("/", $profilePhotoLocation));
                         $photo->save();
                     }
                     // Deletes file from directory (is already in DB)
                     unlink($profilePhotoLocation);
                 }
                 // Adds photo id
                 if (!empty($photo)) {
                     $formValues['id_fotografie'] = $photo->getid_foto();
                 }
                 if ($user === NULL) {
                     $user = My_Model::get('Users')->createRow();
                 }
                 if (!empty($formValues["heslo"])) {
                     $formValues["heslo"] = sha1("interview" . $formValues["heslo"]);
                 } else {
                     unset($formValues["heslo"]);
                 }
                 $user->updateFromArray($formValues);
                 $this->_helper->redirector->gotoRoute(array('controller' => 'user', 'action' => 'detail', 'id' => $userId), 'default', true);
             }
         } else {
             if ($this->_request->getPost('closeButton', false)) {
                 if (!empty($userId)) {
                     $this->_helper->redirector->gotoRoute(array('controller' => 'user', 'action' => 'detail', 'id' => $userId), 'default', true);
                 } else {
                     $this->_helper->redirector->gotoRoute(array('controller' => 'user', 'action' => 'index'), 'default', true);
                 }
             } else {
                 if ($this->_request->getPost('deleteButton', false)) {
                     if (!empty($userId)) {
                         My_Model::get('Users')->getById($userId)->delete();
                     }
                     $this->_helper->redirector->gotoRoute(array('controller' => 'user', 'action' => 'index'), 'default', true);
                 }
             }
         }
     }
 }