User.php 6.33 KB
Newer Older
Laurent's avatar
Laurent committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


class ZendAfi_Controller_Plugin_Manager_User extends ZendAfi_Controller_Plugin_Manager_Manager {
24 25
  public function getActions($model) {
    return [['url' => ['module' => 'admin',
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                       'controller' => 'users',
                       'action' => 'edit',
                       'id' => '%s'],
             'icon' => 'edit',
             'label' => $this->_('modifier')],

            ['url' => ['module' => 'admin',
                       'controller' => 'users',
                       'action' => 'delete',
                       'id' => '%s'],
             'icon' => 'delete',
             'label' => $this->_('supprimer')],

            ['url' => ['module' => 'opac',
                       'controller' => 'blog',
                       'action' => 'viewauteur',
                       'id' => '%s'],
             'icon' => 'star',
             'label' => $this->_('avi(s)'),
             'condition' => 'hasAvis',
             'anchorOptions' => ['target' => '_blank']],

            ['url' => ['module' => 'opac',
                       'controller' => 'panier',
                       'action' => 'viewauteur',
                       'id' => '%s'],
             'icon' => 'basket',
             'label' => $this->_('panier(s)'),
             'condition' => 'hasPaniers',
             'anchorOptions' => ['target' => '_blank']],

            ['url' => ['module' => 'admin',
                       'controller' => 'users',
                       'action' => 'manage-double-user',
                       'id_user' => '%s'],
             'icon' => 'help',
             'label' => $this->_('Doublons'),
             'condition' => function ($model) {
                return (new Class_User_DbDoubleFinder())->hasDoubleFor($model);
              }
Laurent's avatar
Laurent committed
66
            ],
67

68 69
            ['url' => ['module' => 'opac',
                       'controller' => 'auth',
Laurent's avatar
Laurent committed
70 71 72 73 74
                       'action' => 'become',
                       'id' => '%s'],
             'icon' => 'users',
             'condition' => function() { return Class_Users::getIdentity()->isSuperAdmin();},
             'label' => $this->_('Se connecter avec l\'identité')]
75 76
    ];
  }
77

78

efalcy's avatar
efalcy committed
79
  protected function _getPost($key = null, $default = null) {
80
    $post = $this->_request->getPost();
81 82 83 84 85 86 87 88
    $mapper = function($id) {
      return ($group = Class_UserGroup::find((int)$id)) && !$group->isDynamic()
      ? $group
      : null;
    };

    $post['user_groups'] = array_filter(array_map($mapper,
                                                  explode('-', $this->_getParam('user_group_ids',''))));
89

Laurent's avatar
Laurent committed
90 91 92 93
    $post['date_fin'] = isset($post['date_fin']) && trim($post['date_fin'])
      ? Class_Date::frToIso(trim($post['date_fin']))
      : '';

94 95 96
    if(isset($post['naissance']))
      $post['naissance'] = Class_Date::humanDate($post['naissance'],'yyyy-MM-dd');

97
    unset($post['id_categories']);
98

99 100
    return $post;
  }
101

102

103 104 105 106
  protected function _getFormValues($model) {
    $array_model = parent::_getFormValues($model);
    $array_model['user_group_ids'] = implode('-',array_map(function($group) { return $group->getId();},$model->getUserGroups()));
    $array_model = (new Class_User_Settings(Class_Users::getIdentity()))->injectAttributesIn($array_model);
107

108 109
    return $array_model;
  }
110 111


112 113 114 115 116
  protected function _doBeforeSave($model) {
    parent::_doBeforeSave($model);
    $model->getEmprunteur()->updateFromUser($model);
    return $this;
  }
117

118 119 120 121 122 123 124 125

  protected function _doAfterSave($model) {
    $model->getEmprunteur()->ensureService($model)->save();
    return parent::_doAfterSave($model);
  }


  protected function _setupFormAndSave($model) {
126 127 128 129 130
    try {
      return parent::_setupFormAndSave($model);
    } catch (Exception $e) {
      $this->_helper->notify($e->getMessage());
    }
131 132 133
  }


134 135 136
  protected function _canEdit($model) {
    return $model->getRoleLevel() <= Class_Users::getIdentity()->getRoleLevel();
  }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195


  public function massDeleteAction() {
    $this->_massDelete();
  }


  public function massDeleteRunAction() {
    $this->_massDelete();
  }


  protected function _massDelete() {
    if (!$criteria = $this->_massDeleteCriteria())
      return;

    $this->_view->titre = $this->_('Suppression de %s utilisateurs', $this->_view->count);
    $this->_view->criteria_description = $criteria->describeOn($this->_view);
    $this->_view->criteria_params = $criteria->getFormValues();
  }


  public function massDeleteRunStepAction() {
    if (!$criteria = $this->_massDeleteCriteria()) {
      $this->_helper->json([]);
      return $this->_response->setHttpResponseCode(400);
    }

    $criteria
      ->addParam('id_user not', Class_Users::getIdentity()->getId())
      ->addParam('role_level not', ZendAfi_Acl_AdminControllerRoles::SUPER_ADMIN);

    if (!$page = $criteria->findPage(1, 100)) {
      $total = $this->_getParam('total', 0);
      return $this->_helper->json(['total' => $total, 'done' => $total]);
    }

    foreach($page as $user)
      $user->delete();

    $this->_helper->json(['total' => $this->_getParam('total', 0),
                          'done' => $this->_getParam('done', 0) + count($page)]);
  }


  protected function _massDeleteCriteria() {
    $params = $this->_request->getParams();
    $criteria = (new Class_User_SearchCriteria($params))
      ->addCriteria(new Class_User_SearchCriteria_RoleLevelLimit($params));

    $this->_view->count = $criteria->count();
    if (1 < $this->_view->count)
      return $criteria;

    $this->_helper
      ->notify($this->_('Impossible de supprimer une sélection de moins de 2 utilisateurs'));

    $this->_redirectToReferer();
  }
196
}