예제 #1
0
 /**
  * Выводит форму для редактирования профиля и обрабатывает её
  *
  */
 protected function EventProfile()
 {
     // * Устанавливаем title страницы
     E::ModuleViewer()->AddHtmlTitle(E::ModuleLang()->Get('settings_menu_profile'));
     E::ModuleViewer()->Assign('aUserFields', E::ModuleUser()->GetUserFields(''));
     E::ModuleViewer()->Assign('aUserFieldsContact', E::ModuleUser()->GetUserFields(array('contact', 'social')));
     // * Загружаем в шаблон JS текстовки
     E::ModuleLang()->AddLangJs(array('settings_profile_field_error_max'));
     // * Если нажали кнопку "Сохранить"
     if ($this->isPost('submit_profile_edit')) {
         E::ModuleSecurity()->ValidateSendForm();
         $bError = false;
         /**
          * Заполняем профиль из полей формы
          */
         // * Определяем гео-объект
         if (F::GetRequest('geo_city')) {
             $oGeoObject = E::ModuleGeo()->GetGeoObject('city', F::GetRequestStr('geo_city'));
         } elseif (F::GetRequest('geo_region')) {
             $oGeoObject = E::ModuleGeo()->GetGeoObject('region', F::GetRequestStr('geo_region'));
         } elseif (F::GetRequest('geo_country')) {
             $oGeoObject = E::ModuleGeo()->GetGeoObject('country', F::GetRequestStr('geo_country'));
         } else {
             $oGeoObject = null;
         }
         // * Проверяем имя
         if (F::CheckVal(F::GetRequestStr('profile_name'), 'text', 2, Config::Get('module.user.name_max'))) {
             $this->oUserCurrent->setProfileName(F::GetRequestStr('profile_name'));
         } else {
             $this->oUserCurrent->setProfileName(null);
         }
         // * Проверяем пол
         if (in_array(F::GetRequestStr('profile_sex'), array('man', 'woman', 'other'))) {
             $this->oUserCurrent->setProfileSex(F::GetRequestStr('profile_sex'));
         } else {
             $this->oUserCurrent->setProfileSex('other');
         }
         // * Проверяем дату рождения
         $nDay = intval(F::GetRequestStr('profile_birthday_day'));
         $nMonth = intval(F::GetRequestStr('profile_birthday_month'));
         $nYear = intval(F::GetRequestStr('profile_birthday_year'));
         if (checkdate($nMonth, $nDay, $nYear)) {
             $this->oUserCurrent->setProfileBirthday(date('Y-m-d H:i:s', mktime(0, 0, 0, $nMonth, $nDay, $nYear)));
         } else {
             $this->oUserCurrent->setProfileBirthday(null);
         }
         // * Проверяем информацию о себе
         if (F::CheckVal(F::GetRequestStr('profile_about'), 'text', 1, 3000)) {
             $this->oUserCurrent->setProfileAbout(E::ModuleText()->Parser(F::GetRequestStr('profile_about')));
         } else {
             $this->oUserCurrent->setProfileAbout(null);
         }
         // * Ставим дату последнего изменения профиля
         $this->oUserCurrent->setProfileDate(F::Now());
         // * Запускаем выполнение хуков
         E::ModuleHook()->Run('settings_profile_save_before', array('oUser' => $this->oUserCurrent, 'bError' => &$bError));
         // * Сохраняем изменения профиля
         if (!$bError) {
             if (E::ModuleUser()->Update($this->oUserCurrent)) {
                 // * Обновляем название личного блога
                 $oBlog = $this->oUserCurrent->getBlog();
                 if (F::GetRequestStr('blog_title') && $this->checkBlogFields($oBlog)) {
                     $oBlog->setTitle(strip_tags(F::GetRequestStr('blog_title')));
                     E::ModuleBlog()->UpdateBlog($oBlog);
                 }
                 // * Создаем связь с гео-объектом
                 if ($oGeoObject) {
                     E::ModuleGeo()->CreateTarget($oGeoObject, 'user', $this->oUserCurrent->getId());
                     if ($oCountry = $oGeoObject->getCountry()) {
                         $this->oUserCurrent->setProfileCountry($oCountry->getName());
                     } else {
                         $this->oUserCurrent->setProfileCountry(null);
                     }
                     if ($oRegion = $oGeoObject->getRegion()) {
                         $this->oUserCurrent->setProfileRegion($oRegion->getName());
                     } else {
                         $this->oUserCurrent->setProfileRegion(null);
                     }
                     if ($oCity = $oGeoObject->getCity()) {
                         $this->oUserCurrent->setProfileCity($oCity->getName());
                     } else {
                         $this->oUserCurrent->setProfileCity(null);
                     }
                 } else {
                     E::ModuleGeo()->DeleteTargetsByTarget('user', $this->oUserCurrent->getId());
                     $this->oUserCurrent->setProfileCountry(null);
                     $this->oUserCurrent->setProfileRegion(null);
                     $this->oUserCurrent->setProfileCity(null);
                 }
                 E::ModuleUser()->Update($this->oUserCurrent);
                 // * Обрабатываем дополнительные поля, type = ''
                 $aFields = E::ModuleUser()->GetUserFields('');
                 $aData = array();
                 foreach ($aFields as $iId => $aField) {
                     if (isset($_REQUEST['profile_user_field_' . $iId])) {
                         $aData[$iId] = F::GetRequestStr('profile_user_field_' . $iId);
                     }
                 }
                 E::ModuleUser()->SetUserFieldsValues($this->oUserCurrent->getId(), $aData);
                 // * Динамические поля контактов, type = array('contact','social')
                 $aType = array('contact', 'social');
                 $aFields = E::ModuleUser()->GetUserFields($aType);
                 // * Удаляем все поля с этим типом
                 E::ModuleUser()->DeleteUserFieldValues($this->oUserCurrent->getId(), $aType);
                 $aFieldsContactType = F::GetRequest('profile_user_field_type');
                 $aFieldsContactValue = F::GetRequest('profile_user_field_value');
                 if (is_array($aFieldsContactType)) {
                     foreach ($aFieldsContactType as $k => $v) {
                         $v = (string) $v;
                         if (isset($aFields[$v]) && isset($aFieldsContactValue[$k]) && is_string($aFieldsContactValue[$k])) {
                             E::ModuleUser()->SetUserFieldsValues($this->oUserCurrent->getId(), array($v => $aFieldsContactValue[$k]), Config::Get('module.user.userfield_max_identical'));
                         }
                     }
                 }
                 E::ModuleMessage()->AddNoticeSingle(E::ModuleLang()->Get('settings_profile_submit_ok'));
                 E::ModuleHook()->Run('settings_profile_save_after', array('oUser' => $this->oUserCurrent));
             } else {
                 E::ModuleMessage()->AddErrorSingle(E::ModuleLang()->Get('system_error'));
             }
         }
     }
     // * Загружаем гео-объект привязки
     $oGeoTarget = E::ModuleGeo()->GetTargetByTarget('user', $this->oUserCurrent->getId());
     E::ModuleViewer()->Assign('oGeoTarget', $oGeoTarget);
     // * Загружаем в шаблон список стран, регионов, городов
     $aCountries = E::ModuleGeo()->GetCountries(array(), array('sort' => 'asc'), 1, 300);
     E::ModuleViewer()->Assign('aGeoCountries', $aCountries['collection']);
     if ($oGeoTarget) {
         if ($oGeoTarget->getCountryId()) {
             $aRegions = E::ModuleGeo()->GetRegions(array('country_id' => $oGeoTarget->getCountryId()), array('sort' => 'asc'), 1, 500);
             E::ModuleViewer()->Assign('aGeoRegions', $aRegions['collection']);
         }
         if ($oGeoTarget->getRegionId()) {
             $aCities = E::ModuleGeo()->GetCities(array('region_id' => $oGeoTarget->getRegionId()), array('sort' => 'asc'), 1, 500);
             E::ModuleViewer()->Assign('aGeoCities', $aCities['collection']);
         }
     }
     E::ModuleLang()->AddLangJs(array('settings_profile_avatar_resize_title', 'settings_profile_avatar_resize_text', 'settings_profile_photo_resize_title', 'settings_profile_photo_resize_text'));
 }