<?php
class actionUsersProfileEditGroup extends cmsAction {
public function run($profile, $do=false){
if (!cmsUser::isLogged()) { cmsCore::error404(); }
$user = cmsUser::getInstance();
// если нужно, передаем управление другому экшену
if ($do){
$this->runAction('profile_edit_'.$do, array($profile) + array_slice($this->params, 2)); return;
}
// проверяем наличие доступа
if ($profile['id'] != $user->id && !$user->is_admin) { cmsCore::error404(); }
// Получаем поля
$content_model = cmsCore::getModel('content');
$content_model->setTablePrefix('');
$content_model->orderBy('ordering');
$fields = $content_model->getContentFields('users');
// Строим форму
$form = new cmsForm();
// Разбиваем поля по группам
$fieldsets = cmsForm::mapFieldsToFieldsets($fields, function($field, $user){
// проверяем что группа пользователя имеет доступ к редактированию этого поля
if ($field['groups_edit'] && !$user->isInGroups($field['groups_edit'])) { return false; }
return true;
});
// Добавляем поле выбора группы,
// при наличии публичных групп
//
$users_model = cmsCore::getModel('users');
$public_groups = $users_model->getPublicGroups();
if ($public_groups) {
foreach($public_groups as $pb) { $pb_items[ $pb['id'] ] = $pb['title']; }
$fieldset_id = $form->addFieldset( LANG_USER_GROUP );
$form->addFieldToBeginning($fieldset_id,
new fieldList
('groups', array( 'items' => $pb_items
)
)
);
}
// Форма отправлена?
$is_submitted = $this->request->has('submit');
if ($is_submitted){
// Парсим форму и получаем поля записи
$new = $form->parse($this->request, $is_submitted, $profile);
$old = $profile;
// Проверям правильность заполнения
$errors = $form->validate($this, $profile);
if (!$errors){
$is_allowed = cmsEventsManager::hookAll('user_profile_update', $profile, true);
if ($is_allowed !== true && in_array(false, $is_allowed)) { $errors = true; } }
if (!$errors && !empty($new['groups'])){ $profile['groups'] = array($new['groups']); }
if (!$errors){
// Обновляем профиль и редиректим на его просмотр
$this->model->updateUser($profile['id'], $profile);
// Отдельно обновляем часовой пояс в сессии
cmsUser::sessionSet('user_data:time_zone', $profile['time_zone']);
// Постим уведомление о смене аватара в ленту
if (!$this->model->isAvatarsEqual($new['avatar'], $old['avatar'])){
$activity_controller = cmsCore::getController('activity');
$activity_controller->deleteEntry($this->name, "avatar", $profile['id']);
if (!empty($new['avatar'])){ $activity_controller->addEntry($this->name, "avatar", array( 'user_id' => $profile['id'],
'subject_title' => $profile['nickname'],
'subject_id' => $profile['id'],
'subject_url' => href_to('users', $profile['id']),
'is_private' => 0,
'group_id' => null,
'url' => href_to('users', $profile['id']),
'src' => html_image_src($new['avatar'], 'normal')
)
),
'images_count' => 1
));
}
}
$this->redirectTo('users', $profile['id']);
}
if ($errors){
cmsUser::addSessionMessage(LANG_FORM_ERRORS, 'error');
}
}
return cmsTemplate
::getInstance()->render('profile_edit_group', array( 'do' => 'edit',
'id' => $profile['id'],
'profile' => $profile,
'form' => $form,
'errors' => isset($errors) ?
$errors : false ));
}
}