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

Source for file Register.class.php

Documentation is available at Register.class.php

  1. <?php
  2. /**
  3.  * Содержит класс Register
  4.  *
  5.  * @package energine
  6.  * @subpackage user
  7.  * @author 1m.dm
  8.  * @copyright Energine 2006
  9.  * @version $Id$
  10.  */
  11.  
  12. //require_once('core/modules/share/components/DBDataSet.class.php');
  13. //require_once('core/framework/User.class.php');
  14. //require_once('core/framework/Mail.class.php');
  15.  
  16. /**
  17.  * Форма регистрации
  18.  *
  19.  * @package energine
  20.  * @subpackage user
  21.  * @author 1m.dm
  22.  */
  23. class Register extends DBDataSet {
  24.  
  25.     /**
  26.      * Экземпляр класса User
  27.      *
  28.      * @var User 
  29.      * @access private
  30.      */
  31.     protected $user;
  32.  
  33.     /**
  34.      * Конструктор класса
  35.      *
  36.      * @param string $name 
  37.      * @param string $module 
  38.      * @param Document $document 
  39.      * @param array $params 
  40.      * @access public
  41.      */
  42.     public function __construct($name$moduleDocument $document,  array $params null{
  43.         parent::__construct($name$module$document,  $params);
  44.         $this->setDataSetAction('save-new-user');
  45.         $this->setType(self::COMPONENT_TYPE_FORM_ADD);
  46.         $this->user = new User();
  47.         $this->setTableName(User::USER_TABLE_NAME);
  48.     }
  49.     /**
  50.      * Переопределен параметр active
  51.      *
  52.      * @return int 
  53.      * @access protected
  54.      */
  55.  
  56.     protected function defineParams({
  57.         $result array_merge(parent::defineParams(),
  58.         array(
  59.         'active'=>true,
  60.         ));
  61.         return $result;
  62.     }
  63.     /**
  64.      * Метод проверки логина
  65.      * Вызывается AJAXом при заполнении формы регистрации
  66.      *
  67.      * @access protected
  68.      * @return void 
  69.      */
  70.     protected function check(){
  71.         if($_SESSION['captchaChecked'$result (
  72.         isset($_POST['captcha'])
  73.         &&
  74.         ($_SESSION['captchaCode'== sha1($_POST['captcha']))
  75.         )){
  76.             $login trim($_POST['login']);
  77.             $result !(bool)simplifyDBResult(
  78.             $this->dbh->select(
  79.             $this->getTableName(),
  80.             array('COUNT(u_id) as number'),
  81.             array('u_name' => $login)
  82.             ),
  83.                 'number'
  84.             true
  85.             );
  86.             $field 'login';
  87.             $message ($result)?$this->translate('TXT_LOGIN_AVAILABLE'):$this->translate('TXT_LOGIN_ENGAGED');
  88.         }
  89.         else{
  90.             $message $this->translate('TXT_BAD_CAPTCHA');
  91.             $field 'captcha';
  92.         }
  93.  
  94.         $result array(
  95.                 'result'=> $result,
  96.                 'message' => $message,
  97.                 'field' => $field,  
  98.         );
  99.         $result json_encode($result);
  100.         $this->response->setHeader('Content-Type''text/javascript; charset=utf-8');
  101.         $this->response->write($result);
  102.         $this->response->commit();
  103.     }
  104.     /**
  105.      * Обработка возможных ошибок сохранения + редирект на страницу результата
  106.      *
  107.      * @return void 
  108.      * @access protected
  109.      */
  110.     protected function save({
  111.         try {
  112.             $this->saveData();
  113.             $_SESSION['saved'true;
  114.             $this->response->redirectToCurrentSection('success/');
  115.         }
  116.         catch (SystemException $e{
  117.             $this->generateError(SystemException::ERR_NOTICE$e->getMessage());
  118.             $this->setParam('action''main');
  119.             $this->prepare();
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Сохранение данных.
  125.      *
  126.      * @return array 
  127.      * @access protected
  128.      */
  129.     protected function saveData({
  130.         $password $_POST[$this->getTableName()]['u_password'User::generatePassword();
  131.         try {
  132.             $result $this->user->create($_POST[$this->getTableName()]);
  133.  
  134.             $mailer new Mail();
  135.             $mailer->setFrom($this->getConfigValue('mail.from'));
  136.             $mailer->setSubject($this->translate('TXT_SUBJ_REGISTER'));
  137.             $mailer->setText(
  138.             $this->translate('TXT_BODY_REGISTER'),
  139.             array(
  140.                  'login' => $this->user->getValue('u_name')
  141.                  'name' => $this->user->getValue('u_fullname'),
  142.                  'password' => $password
  143.             )
  144.             );
  145.             $mailer->addTo($this->user->getValue('u_name'));
  146.             $mailer->send();
  147.  
  148.         }
  149.         catch (Exception $error{
  150.             throw new SystemException($error->getMessage()SystemException::ERR_WARNING);
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Получает список доступных полей из таблицы пользователей и генерит форму
  156.      *
  157.      * @return void 
  158.      * @access protected
  159.      */
  160.     protected function prepare({
  161.         parent::prepare();
  162.         //u_id и u_is_active нам не нужны ни при каких раскладах
  163.         if ($this->getDataDescription()->getFieldDescriptionByName('u_id')) {
  164.             $this->getDataDescription()->removeFieldDescription($this->getDataDescription()->getFieldDescriptionByName('u_id'));
  165.         }
  166.  
  167.         if ($this->getDataDescription()->getFieldDescriptionByName('u_is_active')) {
  168.             $this->getDataDescription()->removeFieldDescription($this->getDataDescription()->getFieldDescriptionByName('u_is_active'));
  169.         }
  170.         //Тут таки нужно вернуться к параметру confirmationNeeded
  171.         if ($this->getDataDescription()->getFieldDescriptionByName('u_password')) {
  172.             $this->getDataDescription()->removeFieldDescription($this->getDataDescription()->getFieldDescriptionByName('u_password'));
  173.         }
  174.         $this->getDataDescription()->getFieldDescriptionByName('u_name')->setType(FieldDescription::FIELD_TYPE_EMAIL);
  175.     }
  176.  
  177.     /**
  178.      * Выводит результат регистрации.
  179.      *
  180.      * @return void 
  181.      * @access protected
  182.      */
  183.     protected function success({
  184.         //если в сессии нет переменной saved значит этот метод пытаются вызвать напрямую. Не выйдет!
  185.         if (!isset($_SESSION['saved'])) {
  186.             throw new SystemException('ERR_404'SystemException::ERR_404);
  187.         }
  188.         //unset($_SESSION['saved']);
  189.         if ($textBlock $this->document->componentManager->getComponentByName('RegTextBlock')) {
  190.             $textBlock->disable();
  191.         }
  192.         $this->setBuilder($this->createBuilder());
  193.  
  194.         $dataDescription new DataDescription();
  195.         $ddi new FieldDescription('success_message');
  196.         $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  197.         $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  198.         $ddi->removeProperty('title');
  199.         $dataDescription->addFieldDescription($ddi);
  200.  
  201.         $data new Data();
  202.         $di new Field('success_message');
  203.         $di->setData($this->translate('TXT_USER_REGISTRED'));
  204.         $data->addField($di);
  205.  
  206.         $this->setDataDescription($dataDescription);
  207.         $this->setData($data);
  208.     }
  209. }
В создании документации нам помог: phpDocumentor