energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file UserProfile.class.php

Documentation is available at UserProfile.class.php

  1. <?php
  2. /**
  3.  * Содержит класс UserProfile
  4.  *
  5.  * @package energine
  6.  * @subpackage user
  7.  * @author dr.Pavka
  8.  * @copyright Energine 2006
  9.  * @version $Id$
  10.  */
  11.  
  12. //require_once('core/modules/share/components/DBDataSet.class.php');
  13. //require_once('core/framework/AuthUser.class.php');
  14.  
  15. /**
  16.  * Форма редактирования данных пользователя
  17.  *
  18.  * @package energine
  19.  * @subpackage user
  20.  * @author dr.Pavka
  21.  */
  22. class UserProfile extends DBDataSet {
  23.     /**
  24.      * Конструктор класса
  25.      *
  26.      * @return void 
  27.      * @access public
  28.      */
  29.     public function __construct($name$moduleDocument $document,  array $params null{
  30.         parent::__construct($name$module$document,  $params);
  31.         $this->setTableName('user_users');
  32.         $this->setType(self::COMPONENT_TYPE_FORM_ALTER);
  33.     }
  34.  
  35.  
  36.     /**
  37.      * Действие по умолчанию
  38.      *
  39.      * @return type 
  40.      * @access protected
  41.      */
  42.  
  43.     protected function main({
  44.         if (!$this->document->user->isAuthenticated()) {
  45.             throw new SystemException('ERR_DEV_NO_AUTH_USER'SystemException::ERR_DEVELOPER);
  46.         }
  47.         $this->setFilter($this->document->user->getID());
  48.         
  49.         $this->setDataSetAction('save-user');
  50.         $this->setTitle($this->translate('TXT_USER_PROFILE'));
  51.         $this->addTranslation('MSG_PWD_MISMATCH');
  52.         $this->prepare();
  53.     }
  54.  
  55.     /**
  56.      * Переопределен параметр active
  57.      *
  58.      * @return int 
  59.      * @access protected
  60.      */
  61.  
  62.     protected function defineParams({
  63.         $result array_merge(parent::defineParams(),
  64.         array(
  65.         'active'=>true,
  66.         ));
  67.         return $result;
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Метод сохранения
  73.      * Переписан родительский
  74.      *
  75.      * @return void 
  76.      * @access protected
  77.      */
  78.  
  79.     protected function save({
  80.         if($this->document->user->getValue('u_password'!= sha1($_POST[$this->getTableName()]['u_password'])) {
  81.             $_SESSION['error'true;
  82.             $this->response->redirectToCurrentSection('error/');
  83.         }
  84.  
  85.         if (!empty($_POST[$this->getTableName()]['u_password'])) {
  86.             if ($_POST[$this->getTableName()]['u_password'!= $_POST['u_password2']{
  87.                 $this->generateError(SystemException::ERR_WARNING'ERR_PWD_MISMATCH');
  88.             }
  89.             unset($_POST['u_password2']);
  90.             $_POST[$this->getTableName()]['u_password'sha1($_POST[$this->getTableName()]['u_password']);
  91.         }
  92.         $_POST[$this->getTableName()]['u_id'$this->document->getUser()->getID();
  93.  
  94.         $this->prepare();
  95.         $fields $this->getDataDescription()->getFieldDescriptionList();
  96.         if (array_diff($fieldsarray_keys($_POST[$this->getTableName()])) != array()) {
  97.             throw new SystemException('ERR_BAD_DATA'SystemException::ERR_CRITICAL);
  98.         }
  99.  
  100.         $data $_POST[$this->getTableName()];
  101.  
  102.         try {
  103.             $this->document->user->update($data);
  104.             $_SESSION['saved'true;
  105.  
  106.             //переадресация
  107.             $this->response->redirectToCurrentSection('success/');
  108.         }
  109.         //Отлавливаем все ошибки которые могли произойти при сохранении в БД, чтобы вывести нужную информацию об ошибке на уровне компонента
  110.         catch (FormException $formError{
  111.             $errors $this->saver->getErrors();
  112.             foreach ($errors as $errorFieldName{
  113.                 $message $this->saver->getDataDescription()->getFieldDescriptionByName($errorFieldName)->getPropertyValue('message');
  114.                 $this->generateError(SystemException::ERR_NOTICE$message);
  115.             }
  116.             //переадресация
  117.             $this->response->redirectToCurrentSection();
  118.         }
  119.         catch (SystemException $e){
  120.             $this->generateError(SystemException::ERR_NOTICE$e->getMessage()$e->getCustomMessage());
  121.             //переадресация
  122.             $this->response->redirectToCurrentSection();
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Метод, выводящий сообщение об успешном сохранении данных
  128.      *
  129.      * @return void 
  130.      * @access protected
  131.      */
  132.  
  133.     protected function success({
  134.         //если в сессии нет переменной saved, значит этот метод пытаются дернуть напрямую. Не выйдет!
  135.         if (!isset($_SESSION['saved'])) {
  136.             throw new SystemException('ERR_404'SystemException::ERR_404);
  137.         }
  138.         //Мавр сделал свое дело...
  139.         unset($_SESSION['saved']);
  140.  
  141.         $this->setBuilder($this->createBuilder());
  142.  
  143.         $dd new DataDescription();
  144.         $this->setDataDescription($dd);
  145.  
  146.         $ddi new FieldDescription('success_message');
  147.         $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  148.         $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  149.         $ddi->removeProperty('title');
  150.         $dd->addFieldDescription($ddi);
  151.  
  152.         $d new Data();
  153.         $this->setData($d);
  154.  
  155.         $di new Field('success_message');
  156.         $di->setData($this->translate('TXT_USER_PROFILE_SAVED'));
  157.         $d->addField($di);
  158.  
  159.         $this->document->componentManager->getComponentByName('breadCrumbs')->addCrumb();
  160.     }
  161.  
  162.  
  163.     /**
  164.      * Метод, выводящий сообщение о неверно введенном пароле
  165.      *
  166.      * @return void 
  167.      * @access protected
  168.      */
  169.  
  170.     protected function error({
  171.         //если в сессии нет переменной error, значит этот метод пытаются дернуть напрямую. Не выйдет!
  172.         if (!isset($_SESSION['error'])) {
  173.             throw new SystemException('ERR_404'SystemException::ERR_404);
  174.         }
  175.         //Мавр сделал свое дело...
  176.         unset($_SESSION['error']);
  177.  
  178.         $this->setBuilder($this->createBuilder());
  179.  
  180.         $dd new DataDescription();
  181.         $this->setDataDescription($dd);
  182.  
  183.         $ddi new FieldDescription('error_message');
  184.         $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  185.         $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  186.         $ddi->removeProperty('title');
  187.         $dd->addFieldDescription($ddi);
  188.  
  189.         $d new Data();
  190.         $this->setData($d);
  191.  
  192.         $di new Field('error_message');
  193.         $di->setData($this->translate('TXT_USER_PROFILE_WRONG_PWD'));
  194.         $d->addField($di);
  195.  
  196.         $this->document->componentManager->getComponentByName('breadCrumbs')->addCrumb();
  197.     }
  198.  
  199.     /**
  200.      * Для метода success переопределен метод создания объекта метаданных
  201.      *
  202.      * @return DataDescription 
  203.      * @access protected
  204.      */
  205.  
  206.     protected function createDataDescription({
  207.         $result parent::createdataDescription();
  208.         if ($field $result->getFieldDescriptionByName('u_is_active')) {
  209.             $result->removeFieldDescription($field);
  210.         }
  211.  
  212.         $field $result->getFieldDescriptionByName('u_password');
  213.         $field->setProperty('message2'$this->translate('ERR_PWD_MISMATCH'));
  214.         $result->removeFieldDescription($field);
  215.         $result->addFieldDescription($field);
  216.  
  217.         if ($this->getAction(!== 'save'{
  218.             $field new FieldDescription('u_password2');
  219.             $field->setProperty('message2'$this->translate('ERR_PWD_MISMATCH'));
  220.             $field->setType(FieldDescription::FIELD_TYPE_PWD);
  221.             $field->setProperty('customField'true);
  222.             $field->setProperty('title'$this->translate('FIELD_U_PASSWORD2'));
  223.             $result->addFieldDescription($field);
  224.         }
  225.  
  226.  
  227.         return $result;
  228.     }
  229.  
  230.     /**
  231.      * Для метода success создаем свой объект данных
  232.      *
  233.      * @return Data 
  234.      * @access protected
  235.      */
  236.  
  237.     protected function createData({
  238.         $result parent::createData();
  239.         $result->getFieldByName('u_password')->setData('');
  240.         return $result;
  241.     }
  242. }
В создании документации нам помог: phpDocumentor