/**
  * Allows users to edit another users' data
  * (should be reserved for administrators)
  *
  * @access public
  * @return void
  */
 public function editAction()
 {
     $this->title = 'Edit this user';
     $form = new UserForm();
     $userModel = new BackofficeUser();
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($this->getRequest()->getPost())) {
             $userModel->save($form->getValues());
             $this->_helper->FlashMessenger(array('msg-success' => 'The user was successfully updated'));
             App_FlagFlippers_Manager::save();
             $this->_redirect('/users/');
         }
     } else {
         $id = $this->_getParam('id');
         if (!is_numeric($id)) {
             $this->_helper->FlashMessenger(array('msg-error' => 'The user id you provided is invalid'));
             $this->_redirect('/users/');
         }
         if ($id == 1) {
             $this->_helper->FlashMessenger(array('msg-error' => 'It is forbidden to mess with the admin account in this release.'));
             $this->_redirect('/users/');
         }
         $row = $userModel->findById($id);
         if (empty($row)) {
             $this->_helper->FlashMessenger(array('msg-error' => 'The requested user could not be found'));
             $this->_redirect('/users/');
         }
         $data = $row->toArray();
         $data['groups'] = $row->groupIds;
         $form->populate($data);
         $this->view->item = $row;
     }
     $this->view->form = $form;
 }
 public function editAction()
 {
     $record = null;
     $photoFilename = null;
     $userId = $this->_request->getParam('id');
     if (!empty($userId)) {
         $record = My_Model::get('Users')->getById($userId);
         if (!$record) {
             throw new Zend_Controller_Action_Exception('The requested page does not exist', 404);
         }
         $this->view->userId = $userId;
     }
     $form = new UserForm();
     $form->setAction($this->_helper->url->url());
     if ($record === null) {
         $this->view->title = 'Add User';
     } else {
         $this->view->title = 'Edit User';
         $form->setModifyMode();
     }
     $this->view->form = $form;
     if ($this->_request->isPost()) {
         if ($form->isValid($this->_request->getPost())) {
             $formValues = $form->getValues();
             $foundUser = My_Model::get('Users')->fetchRow(array("username = ?" => $formValues["username"]));
             if ($foundUser != null && $foundUser->getId() != $userId) {
                 $form->getElement('username')->addError('This username is taken');
                 $form->markAsError();
                 return;
             }
             //XXX: Je to dobytčárna
             if ($form->photo->receive()) {
                 $photo = $form->photo;
                 $oldFullPath = $photo->getFileName();
                 $path_parts = pathinfo($oldFullPath);
                 if ($path_parts) {
                     $photoFilename = $photo->getHash('md5') . '.' . $path_parts['extension'];
                     $newFullPath = $path_parts['dirname'] . '/' . $photoFilename;
                     rename($oldFullPath, $newFullPath);
                 }
             }
             if ($record === null) {
                 $record = My_Model::get('Users')->createRow();
                 if ($photoFilename) {
                     $record->setPhotoFilename($photoFilename);
                 }
                 $record->updateFromArray($formValues, true);
             } else {
                 if ($photoFilename) {
                     $record->setPhotoFilename($photoFilename);
                 }
                 $record->updateFromArray($formValues, false);
                 //do not update created on value
             }
             //Zend_Debug::dump($formValues);
             //echo '================================================================<br />';
             //Zend_Debug::dump($formValues);
             //echo '========================PHOTO=========================<br />';
             //$var = file_get_contents($form->photo);
             //Zend_Debug::dump($var);
             $this->_helper->flashMessenger->setNamespace("success")->addMessage("Your changes have been saved!");
             $this->_helper->redirector->gotoRoute(array('controller' => 'user'), 'default', true);
         }
     } else {
         if ($record !== null) {
             $form->populate($record->toArray());
         }
     }
 }
 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);
                 }
             }
         }
     }
 }
 public function executeUpdate($request)
 {
     $object = $this->getRequestParameter('object');
     $user = User::getByApiKey($request->getParameter('login_id'), $request->getParameter('api_key'));
     if (!$user) {
         $output = '<rsp stat="fail"><err code="2" msg="login_id and api_key do not match" /></rsp>';
     } elseif ($object == 'application') {
         $form = new ApplicationForm();
         $form->bind(array('id' => $request->getParameter('id'), 'name' => $request->getParameter('name'), 'description' => $request->getParameter('description'), 'source_url' => $request->getParameter('source_url')));
         if ($form->isValid()) {
             $application = Application::update($form->getValues(), $user);
             if ($application) {
                 $output = '<rsp stat="ok">' . $application->getXML() . '</rsp>';
             } else {
                 $output = '<rsp stat="fail"><err code="4" msg="Unable to update application." /></rsp>';
             }
         } else {
             $output = '<rsp stat="fail"><err code="4" msg="' . $form->getErrorSchema() . '" /></rsp>';
         }
     } elseif ($object == 'comment') {
         $form = new CommentForm();
         $application_id = $module_id = $theme_id = null;
         if ($request->getParameter('application_id')) {
             $application_id = $request->getParameter('application_id');
         }
         if ($request->getParameter('module_id')) {
             $module_id = $request->getParameter('module_id');
         }
         if ($request->getParameter('theme_id')) {
             $theme_id = $request->getParameter('theme_id');
         }
         $form->bind(array('comment' => $request->getParameter('comment'), 'application_id' => $application_id, 'module_id' => $module_id, 'theme_id' => $theme_id));
         if ($form->isValid()) {
             $comment = Comment::update($form->getValues(), $user);
             $output = '<rsp stat="ok">' . $comment->getXML() . '</rsp>';
         } else {
             $output = '<rsp stat="fail"><err code="3" msg="' . $form->getErrorSchema() . '" /></rsp>';
         }
     } elseif ($object == 'module') {
         $form = new ModuleForm();
         $form->bind(array('id' => $request->getParameter('id'), 'name' => $request->getParameter('name'), 'description' => $request->getParameter('description'), 'source_url' => $request->getParameter('source_url'), 'application_id' => $request->getParameter('application_id')));
         if ($form->isValid()) {
             $module = Madule::update($form->getValues(), $user);
             if ($module) {
                 $output = '<rsp stat="ok">' . $module->getXML() . '</rsp>';
             } else {
                 $output = '<rsp stat="fail"><err code="4" msg="Unable to update module." /></rsp>';
             }
         } else {
             $output = '<rsp stat="fail"><err code="4" msg="' . $form->getErrorSchema() . '" /></rsp>';
         }
     } elseif ($object == 'theme') {
         $form = new ThemeForm();
         $form->bind(array('id' => $request->getParameter('id'), 'name' => $request->getParameter('name'), 'description' => $request->getParameter('description')), $request->getFiles());
         if ($form->isValid()) {
             $theme = Theme::update($form->getValues(), $user);
             if ($theme) {
                 $output = '<rsp stat="ok">' . $theme->getXML() . '</rsp>';
             } else {
                 $output = '<rsp stat="fail"><err code="5" msg="Unable to update theme." /></rsp>';
             }
         } else {
             $output = '<rsp stat="fail"><err code="5" msg="' . $form->getErrorSchema() . '" /></rsp>';
         }
     } elseif ($object == 'theme_group') {
         $output = '<rsp stat="fail"><err code="6" msg="This object is not supported for update" /></rsp>';
     } elseif ($object == 'user') {
         $form = new UserForm();
         $form->bind(array('id' => $request->getParameter('id'), 'name' => $request->getParameter('name'), 'password' => $request->getParameter('password'), 'password2' => $request->getParameter('password'), 'email' => $request->getParameter('email'), 'role' => null));
         if ($form->isValid()) {
             $update_user = User::update($form->getValues(), $user);
             if ($update_user) {
                 $output = '<rsp stat="ok">' . $update_user->getXML() . '</rsp>';
             } else {
                 $output = '<rsp stat="fail"><err code="7" msg="Unable to update user." /></rsp>';
             }
         } else {
             $output = '<rsp stat="fail"><err code="7" msg="' . $form->getErrorSchema() . '" /></rsp>';
         }
     }
     $this->output = $output;
     $this->setTemplate('index');
 }