CommunityID/modules/users/controllers/PersonalinfoController.php

118 lines
3.4 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/*
* @copyright Copyright (C) 2005-2010 Keyboard Monkeys Ltd. http://www.kb-m.com
2019-07-17 20:08:50 +00:00
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
2019-07-17 20:16:19 +00:00
class Users_PersonalinfoController extends CommunityID_Controller_Action
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:19:00 +00:00
public function preDispatch()
{
if ($this->user->role == Users_Model_User::ROLE_ADMIN) {
throw new Monkeys_AccessDeniedException();
}
}
2019-07-17 20:08:50 +00:00
public function indexAction()
{
$profiles = new Users_Model_Profiles();
$this->view->profiles = $profiles->getForUser($this->user);
2019-07-17 20:08:50 +00:00
$this->_helper->actionStack('index', 'login', 'users');
2019-07-17 20:08:50 +00:00
}
public function editAction()
{
$this->view->profile = $this->_getProfile();
2019-07-17 20:08:50 +00:00
$appSession = Zend_Registry::get('appSession');
if (isset($appSession->personalInfoForm)) {
$this->view->fields = $appSession->personalInfoForm->getElements();
unset($appSession->personalInfoForm);
} else {
$personalInfoForm = new Users_Form_PersonalInfo(null, $this->view->profile);
2019-07-17 20:08:50 +00:00
$this->view->fields = $personalInfoForm->getElements();
}
$this->_helper->actionStack('index', 'login', 'users');
2019-07-17 20:08:50 +00:00
}
public function saveAction()
{
$profile = $this->_getProfile();
$form = new Users_Form_PersonalInfo(null, $profile);
2019-07-17 20:08:50 +00:00
$formData = $this->_request->getPost();
$form->populate($formData);
if (!$form->isValid($formData)) {
$appSession = Zend_Registry::get('appSession');
$appSession->personalInfoForm = $form;
$this->_forward('edit');
return;
}
2019-07-17 20:16:19 +00:00
$fieldsValues = new Model_FieldsValues();
if ($this->_getParam('profile')) {
$fieldsValues->deleteForProfile($profile);
} else {
$profile->user_id = $this->user->id;
$profile->name = $form->getValue('profileName');
$profile->save();
}
2019-07-17 20:08:50 +00:00
foreach ($form->getValues() as $fieldName => $fieldValue) {
if ($fieldName == 'profileName' || !$fieldValue) {
2019-07-17 20:08:50 +00:00
continue;
}
$fieldsValue = $fieldsValues->createRow();
$fieldsValue->user_id = $this->user->id;
$fieldsValue->profile_id = $profile->id;
2019-07-17 20:08:50 +00:00
list(, $fieldId) = explode('_', $fieldName);
$fieldsValue->field_id = $fieldId;
$fieldsValue->value = $fieldValue;
$fieldsValue->save();
}
$this->_helper->FlashMessenger->addMessage($this->view->translate('Profile has been saved'));
$this->_redirect('/users/personalinfo');
}
public function deleteAction()
{
$profile = $this->_getProfile();
if ($profile->id) {
$profile->delete();
}
$this->_helper->FlashMessenger->addMessage($this->view->translate('Profile has been deleted'));
$this->_redirect('/users/personalinfo');
}
private function _getProfile()
{
$profiles = new Users_Model_Profiles();
if (!$this->_getParam('profile')) {
return $profiles->createRow();
}
$profile = $profiles->getRowInstance($this->_getParam('profile'));
if (!$profile || $profile->user_id != $this->user->id) {
throw new Monkeys_AccessDeniedException();
}
2019-07-17 20:08:50 +00:00
return $profile;
2019-07-17 20:08:50 +00:00
}
}