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

Source for file UserEditor.class.php

Documentation is available at UserEditor.class.php

  1. <?php
  2.  
  3. /**
  4.  * Содержит класс UserEditor
  5.  *
  6.  * @package energine
  7.  * @subpackage user
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/modules/share/components/Grid.class.php');
  14.  
  15. /**
  16.  * Класс - редактор пользователей
  17.  *
  18.  * @package energine
  19.  * @subpackage user
  20.  * @author dr.Pavka
  21.  */
  22. class UserEditor extends Grid {
  23.  
  24.  
  25.     /**
  26.      * Конструктор класса
  27.      *
  28.      * @return void 
  29.      */
  30.     public function __construct($name$moduleDocument $document,  array $params null{
  31.         parent::__construct($name$module$document,  $params);
  32.         $this->setTableName('user_users');
  33.         $this->setTitle($this->translate('TXT_USER_EDITOR'));
  34.     }
  35.  
  36.     /**
  37.      * Переопределенный родительский метод
  38.      * проверяет а не пытаемся ли мы удалить текущего пользователя
  39.      *
  40.      * @return boolean 
  41.      * @access public
  42.      */
  43.  
  44.     public function deleteData($id{
  45.         //если мы пытаемся удалить текущего пользователя
  46.         //генерим ошибку
  47.         if ($this->document->user->getID(== $id{
  48.             throw new SystemException('ERR_CANT_DELETE_YOURSELF'SystemException::ERR_CRITICAL);
  49.         }
  50.         parent::deleteData($id);
  51.     }
  52.  
  53.  
  54.     /**
  55.      * Сохранение данных о ролях пользователя
  56.      *
  57.      * @return boolean 
  58.      * @access protected
  59.      */
  60.  
  61.     protected function saveData({
  62.         //При сохранении данных из формы редактирования
  63.         //Если не пришел пароль - не трогаем его
  64.  
  65.         if ($this->getPreviousAction(== 'edit' && $_POST[$this->getTableName()]['u_password'=== ''{
  66.             unset($_POST[$this->getTableName()]['u_password']);
  67.         }
  68.         else {
  69.             $_POST[$this->getTableName()]['u_password'sha1($_POST[$this->getTableName()]['u_password']);
  70.         }
  71.  
  72.         $result parent::saveData();
  73.  
  74.         $UID (is_int($result))?$result:current($this->getFilter());
  75.         
  76.         //если задана аватарка
  77.         if(
  78.             isset($_POST[$this->getTableName()]['u_avatar_prfile'])
  79.             &&
  80.             file_exists($_POST[$this->getTableName()]['u_avatar_prfile'])
  81.         ){
  82.             //и ее размеры отличаются от необходимых
  83.             list($realWidth$realHeightgetimagesize($_POST[$this->getTableName()]['u_avatar_prfile']);
  84.             list($thumbnailInfo$this->getConfigValue('thumbnails.thumbnail');
  85.             list($neededWidth$neededHeightarray((string)$thumbnailInfo->width(string)$thumbnailInfo->height);
  86.              
  87.             if(
  88.                ($realWidth != $neededWidth
  89.                || 
  90.                ($realHeight != $neededHeight)
  91.             ){
  92.                 $fileName $_POST[$this->getTableName()]['u_avatar_prfile'];
  93.                 $image new Image();
  94.                 $image->loadFromFile($fileName);
  95.                 $image->resize($neededWidth,$neededHeight);
  96.                 $image->saveToFile($fileName);          
  97.             }
  98.         }
  99.         
  100.         $this->dbh->modify(QAL::DELETE'user_user_groups'nullarray('u_id'=>$UID));
  101.         
  102.         if(isset($_POST['group_id']&& is_array($_POST['group_id']))
  103.         foreach ($_POST['group_id'as $groupID{
  104.             $this->dbh->modify(QAL::INSERT'user_user_groups',array('u_id'=>$UID'group_id'=>$groupID));
  105.         }
  106.  
  107.         return $result;
  108.     }
  109.     /**
  110.      * toggles user activity status
  111.      * 
  112.      * @return void 
  113.      * @access protected
  114.      */
  115.     protected function activate(){
  116.         $transactionStarted $this->dbh->beginTransaction();
  117.         try {
  118.             list($id$this->getActionParams();
  119.             if (!$this->recordExists($id)) {
  120.                 throw new SystemException('ERR_404'SystemException::ERR_404);
  121.             }
  122.             
  123.             if ($this->document->user->getID(== $id{
  124.                throw new SystemException('ERR_CANT_ACTIVATE_YOURSELF'SystemException::ERR_CRITICAL);
  125.             }
  126.             
  127.             $this->dbh->modifyRequest('UPDATE '.$this->getTableName().' SET u_is_active = NOT u_is_active WHERE u_id = %s'$id);
  128.  
  129.             $JSONResponse array(
  130.             'result'=>true
  131.             );
  132.             $this->dbh->commit();
  133.         }
  134.         catch (SystemException $e){
  135.             if ($transactionStarted{
  136.                 $this->dbh->rollback();
  137.             }
  138.             $JSONResponse $this->generateError($e->getCode()$e->getMessage());
  139.  
  140.         }
  141.         $this->response->setHeader('Content-Type''text/javascript; charset=utf-8');
  142.         $this->response->write(json_encode($JSONResponse));
  143.         $this->response->commit();        
  144.     }
  145.  
  146.     /**
  147.      * Для метода редактирования убирается пароль
  148.      *
  149.      * @return array 
  150.      * @access protected
  151.      */
  152.  
  153.     protected function loadData({
  154.         $result parent::loadData();
  155.  
  156.         /*if ($this->getAction() == 'save') {
  157.             $result[0]['u_password'] = sha1($result[0]['u_password']);
  158.         }
  159.         else*/if ($this->getAction(== 'getRawData' && $result{
  160.             $result array_map(array($this'printUserGroups')$result);
  161.         }
  162.         elseif ($this->getAction(== 'edit'{
  163.             $result[0]['u_password''';
  164.         }
  165.         elseif ($this->getAction(== 'view'{
  166.             $result[0]['u_password''';
  167.         }
  168.         return $result;
  169.     }
  170.  
  171.     /**
  172.      * Callback метод вызывающийся при загрузке данных
  173.      * Добавляет к массиву строку с перечнем групп в которіе входит пользователь
  174.      *
  175.      * @return array 
  176.      * @access private
  177.      */
  178.  
  179.     private function printUserGroups($row{
  180.         $userGroup UserGroup::getInstance();
  181.         $userGroupIDs $userGroup->getUserGroups($row['u_id']);
  182.         $userGroupName array();
  183.         foreach ($userGroupIDs as $UGID{
  184.             $groupInfo $userGroup->getInfo($UGID);
  185.             $userGroupName[$groupInfo['group_name'];
  186.         }
  187.         $row['u_group'implode(', '$userGroupName);
  188.         return $row;
  189.     }
  190.  
  191.     /**
  192.      * Для методов add и edit добавляется поле роли
  193.      *
  194.      * @return DataDescription 
  195.      * @access protected
  196.      */
  197.  
  198.     protected function createDataDescription({
  199.         $result parent::createDataDescription();
  200.  
  201.         if (in_array($this->getAction()array('add''edit'))) {
  202.             foreach ($result as $fieldDescription{
  203.                 $fieldDescription->setProperty('tabName'$this->translate('TXT_USER_EDITOR'));
  204.             }
  205.             $result->getFieldDescriptionByName('u_name')->setType(FieldDescription::FIELD_TYPE_EMAIL);
  206.             if ($fd $result->getFieldDescriptionByName('u_is_active')) {
  207.                 $result->removeFieldDescription($fd);
  208.             }
  209.             $fd new FieldDescription('group_id');
  210.             $fd->setSystemType(FieldDescription::FIELD_TYPE_INT);
  211.             $fd->setType(FieldDescription::FIELD_TYPE_MULTI);
  212.             $fd->setProperty('tabName'$this->translate('TXT_USER_GROUPS'));
  213.             $fd->setProperty('customField'true);
  214.  
  215.             $data $this->dbh->select('user_groups'array('group_id''group_name')'group_id IN(select group_id from user_groups where group_default=0)');
  216.  
  217.             $fd->loadAvailableValues($data'group_id''group_name');
  218.             $result->addFieldDescription($fd);
  219.         }
  220.  
  221.         if (
  222.             ($this->getType(== self::COMPONENT_TYPE_FORM_ALTER)
  223.             &&
  224.             ($f $result->getFieldDescriptionByName('u_password'))
  225.         {
  226.             $f->removeProperty('pattern');
  227.             $f->removeProperty('message');
  228.             $f->setProperty('nullable'true);
  229.         }
  230.  
  231.         return $result;
  232.     }
  233.     /**
  234.      * Load
  235.      *
  236.      * @return array 
  237.      * @access protected
  238.      */
  239.  
  240.      protected function loadDataDescription({
  241.         $result parent::loadDataDescription();
  242.         if ($this->getAction(== 'save' && isset($result['u_password'])) {
  243.                $result['u_password']['nullable'true;
  244.         }
  245.  
  246.         return $result;
  247.      }
  248.  
  249.     /**
  250.       * Для методов add и edit добавляется инфо о роли
  251.       *
  252.       * @return Data 
  253.       * @access protected
  254.       */
  255.  
  256.     protected function createData({
  257.         $result parent::createData();
  258.         $id $this->getFilter();
  259.         $id (!empty($id&& is_array($id))?current($id):false;
  260.         if ($this->getType(!= self::COMPONENT_TYPE_LIST && $id{
  261.             //создаем переменную содержащую идентификторы групп в которые входит пользователь
  262.             $f new Field('group_id');
  263.             $result->addField($f);
  264.  
  265.             $data $this->dbh->select('user_user_groups'array('group_id')array('u_id'=>$id));
  266.             if(is_array($data)) {
  267.                 $f->addRowData(array_keys(convertDBResult($data'group_id'true)));
  268.             }
  269.             else {
  270.                 $f->setData(array());
  271.             }
  272.         }
  273.         return $result;
  274.     }
  275. }
В создании документации нам помог: phpDocumentor