<?php
class UsersController extends AppController {

  var $name = 'Users';
  var $uses = array('User', 'Password');
  var $helpers = array('Html', 'Form');

  function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('admin_init');
    $this->layout = 'admin';
  }

  function admin_login() {
    $this->pageTitle = 'ログイン';
    $this->layout = 'admin_login';
    if ($this->User->findCount() > 0) {
      $this->render('admin_login');
    } else {
      $this->render('admin_init');
    }
  }

  function admin_logout() {
    $this->Auth->logout();
    $this->redirect(array('action' => 'admin_login'));
  }

  function admin_init() {
    $this->pageTitle = '初期ユーザの登録';
    $this->layout = 'admin_login';
    if ($this->User->findCount() > 0) {
      $this->redirect(array('action' => 'admin_login'));
    } else {
      if (!empty($this->data)) {
        $this->Password->create();
        $this->Password->set($this->data);
        if ($this->Password->validates()) {
          $this->data['User']['password'] = $this->Auth->password( $this->data['Password']['password'] );
          $this->data['User']['fullname'] = '初期登録ユーザ';
          $this->User->create();
          $this->User->set($this->data);
          if ($this->User->validates()) {
            $this->User->save();
            $this->flash( 'ユーザを作成いたしました。', 'success' );
            $this->redirect(array('action' => 'admin_login'));
          }
        }
      }
    }
  }

  function admin_index() {
    $this->pageTitle = '管理画面：ユーザ一覧';
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
  }

  function admin_add() {
    $this->pageTitle = '管理画面：ユーザの追加';
    if (!empty($this->data)) {
      $this->Password->create();
      $this->Password->set($this->data);
      if ($this->Password->save()) {
        $this->data['User']['password'] = $this->Auth->password( $this->data['Password']['password'] );
        $this->User->create();
        $this->User->set($this->data);
        if ($this->User->save()) {
          $this->flash( 'ユーザを追加いたしました。', 'success' );
          $this->redirect(array('action' => 'admin_index'));
        }
      } else {
        $this->data['User']['password'] = '';
        $this->User->create();
        $this->User->set($this->data['User']);
        $this->User->save();
      }
    }
  }

  function admin_edit($id = null) {
    $this->pageTitle = '管理画面：ユーザ情報の編集';
    if (!$id && empty($this->data)) {
      $this->flash( '編集するユーザを指定してください。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    if (!empty($this->data)) {
      if (!empty($this->data['Password']['password'])) {
        $this->Password->create();
        $this->Password->set($this->data);
        if ($this->Password->save()) {
          $this->data['User']['password'] = $this->Auth->password( $this->data['Password']['password'] );
        } else {
          $this->data['User']['password'] = '';
        }
      }
      $this->data['User']['id'] = $id;
      $this->User->set($this->data);
      if ($this->User->save()) {
        $this->flash( 'ユーザ情報を更新いたしました。', 'success' );
        $this->redirect(array('action' => 'admin_index'));
      }
    }
    if (empty($this->data)) {
      $this->data = $this->User->read(null, $id);
    }
  }

  function admin_delete($id = null) {
    if (!$id) {
      $this->flash( '削除するユーザを指定してください。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    if ($this->User->del($id)) {
      $this->flash( 'ユーザを削除しました。', 'success' );
      $this->redirect(array('action' => 'admin_index'));
    }
  }

}
?>
