JSlip  1.0
UserMenuController.php
Go to the documentation of this file.
1 <?php
8 require_once(dirname(__FILE__) . '/../../lib/View.php');
9 require_once(dirname(__FILE__) . '/../../lib/Controller.php');
10 require_once(dirname(__FILE__) . '/UserMenuModel.php');
11 
12 class UserMenuController extends Controller
13 {
14  public $param;
15  public $rest;
16  public $viewName;
17  public $model;
18  public $view;
19  public $dat;
20  public $csv;
21 
22  public function main($param) {
23 
24  $this->rest = '';
25  $this->csv = '';
26  $this->param = $param;
27  $this->model = new UserMenuModel();
28  $this->view = new View();
29 
30  $this->param['base'] = dirname(__FILE__);
31 
32  if (empty($this->param['act'])) {
33  $this->_menu();
34  } else {
35  switch ($this->param['act']) {
36  case 'useLast': $this->_useLast(); break;
37  case 'cntJournal': $this->_cntJournal(); break;
38  case 'setBid': $this->_setBid(); break;
39  case 'exportCsv': $this->_exportCsv(); break;
40  case 'importCsv': $this->_importCsv(); break;
41  default: $this->_menu(); break;
42  }
43  }
44  }
45 
46  public function dispCsv() {
47  $this->_dispCsv();
48  $this->_clearFiles();
49  }
50 
51  private function _menu() {
52 
53  if ($_SESSION['minfo']['role'] == 'root' && !empty($this->param['bid'])) {
54  $this->dat = ['basic' => $this->model->getBasicByBid($this->param['bid'])];
55  $this->viewName = 'user_menu';
56  } elseif (empty($_SESSION['minfo']['mid']) || empty($_SESSION['minfo']['bcnt'])) {
57  $this->dat = ['err' => 'Out of service.'];
58  $this->viewName = 'user_menu_err';
59  } elseif ($_SESSION['minfo']['bid'] > 0) {
60  $this->dat = ['basic' => $this->model->getBasicByBid($_SESSION['minfo']['bid'])];
61  $this->viewName = 'user_menu';
62  } else {
63  $this->dat = ['basic' => $this->model->getBasicByMid($_SESSION['minfo']['mid'])];
64  $this->viewName = 'user_menu_select';
65  }
66  }
67 
68  private function _useLast() {
69 
70  $bid = $this->param['bid'];
71  $last = $this->model->getLast($bid);
72 
73  if ($last < 0) {
74  $ans = ['sts' => 'NG', 'err' => '前年度のデータが見つかりません。'];
75  } else {
76  $err = $this->model->useLast($_SESSION['minfo']['mid'], $last, $bid);
77  if (empty($err)) {
78  $ans = ['sts' => 'OK', 'err' => ''];
79  } else {
80  $ans = ['sts' => 'NG', 'err' => $err['msg']];
81  }
82  }
83 
84  $this->rest = json_encode($ans);
85  }
86 
87  private function _cntJournal() {
88  $cnt = $this->model->cntJournal($this->param['bid']);
89  $this->rest = json_encode(['sts' => 'OK', 'cnt' => $cnt]);
90  }
91 
92  private function _setBid() {
93  $_SESSION['minfo']['bid'] = $this->param['bid'];
94  $this->dat = ['basic' => $this->model->getBasicByBid($_SESSION['minfo']['bid'])];
95  $this->viewName = 'user_menu';
96  }
97 
98  private function _exportCsv() {
99  $this->csv = $this->param['tmp_dir'] . '/slip_' . $_SESSION['minfo']['bid'] . '_' . date('YmdHis') . '.csv';
100  $this->model->makeCsv($_SESSION['minfo']['bid'], $this->csv);
101  }
102 
103  private function _importCsv() {
104 
105  $err = [];
106  $records = [];
107 
108  $this->viewName = 'user_menu';
109 
110  $upfile = $this->param['tmp_dir'] . '/' . basename($_FILES['upfile']['name']);
111 
112  if (empty($_FILES['upfile']['name'])) {
113  $err['File Name'] = 'ファイル名が指定されていません。';
114  } elseif (!move_uploaded_file($_FILES['upfile']['tmp_name'], $upfile)) {
115  $err['Upload'] = 'ファイルのアップロードに失敗しました。';
116  } else {
117  $file = new SplFileObject($upfile);
118  $file->setFlags(SplFileObject::READ_CSV);
119  foreach ($file as $line) {
120  if (!is_null($line[0])) {
121  $records[] = $line;
122  }
123  }
124  }
125 
126  if (empty($err)) {
127  $validErr = $this->_ValidImportCsv($_SESSION['minfo']['bid'], $records);
128  if (empty($validErr)) {
129  $err = $this->model->setImportedCsvData($_SESSION['minfo']['mid'], $_SESSION['minfo']['bid'], $records);
130  } else {
131  $err['Valid'] = $validErr;
132  }
133  }
134 
135  $basic = $this->model->getBasicByBid($this->param['bid']);
136  $this->dat = ['err' => $err, 'basic' => $basic];
137  }
138 
139  private function _ValidImportCsv($bid, $rec) {
140 
141  $r0 = (empty($rec[0][0])) ? '' : $rec[0][0];
142  $r1 = (empty($rec[1][0])) ? '' : $rec[1][0];
143  $r2 = (empty($rec[2][0])) ? '' : $rec[2][0];
144  $r3 = (empty($rec[3][0])) ? '' : $rec[3][0];
145  $r = $r0 . $r1 . $r2 . $r3;
146 
147  if ($r != 'HEADERBASICHEADERSECTION') {
148  return 'データ・フォーマットが違います。';
149  }
150 
151  $cnt = $this->model->cntJournal($bid);
152 
153  if ($cnt > 0) {
154  return '伝票が入力されています。';
155  }
156 
157  return '';
158  }
159 
160  private function _dispCsv() {
161  header('Content-Type: application/octet-stream');
162  header('Content-Length: ' . filesize($this->csv));
163  header('Content-Disposition: inline; filename=' . basename($this->csv));
164  readfile($this->csv);
165  }
166 
167  private function _clearFiles() {
168  exec(UTL_DELETE . ' -f ' . $this->csv);
169  }
170 }
UserMenuController\$viewName
$viewName
Definition: _UserMenuController.php:16
UserMenuController\_useLast
_useLast()
Definition: _UserMenuController.php:90
UserMenuController\dispCsv
dispCsv()
Definition: UserMenuController.php:46
UserMenuController\_exportCsv
_exportCsv()
Definition: _UserMenuController.php:119
UserMenuController\_setBid
_setBid()
Definition: _UserMenuController.php:114
UserMenuController\main
main($param)
Definition: UserMenuController.php:22
View
Definition: View.php:8
UTL_DELETE
const UTL_DELETE
Definition: local.php:52
UserMenuController\_menu
_menu()
Definition: _UserMenuController.php:51
UserMenuController\$param
$param
Definition: _UserMenuController.php:14
UserMenuController\_ValidImportCsv
_ValidImportCsv($bid, $rec)
Definition: _UserMenuController.php:160
Controller
Definition: Controller.php:15
UserMenuController\_clearFiles
_clearFiles()
Definition: _UserMenuController.php:188
UserMenuController\_dispCsv
_dispCsv()
Definition: _UserMenuController.php:181
UserMenuController
Definition: _UserMenuController.php:12
UserMenuController\$rest
$rest
Definition: _UserMenuController.php:15
UserMenuController\_importCsv
_importCsv()
Definition: _UserMenuController.php:124
UserMenuController\$csv
$csv
Definition: _UserMenuController.php:20
UserMenuController\$model
$model
Definition: _UserMenuController.php:17
UserMenuController\$dat
$dat
Definition: _UserMenuController.php:19
UserMenuController\$view
$view
Definition: _UserMenuController.php:18
UserMenuModel
Definition: UserMenuModel.php:10
$cnt
$cnt
Definition: tex_tmplt_bs.php:319
UserMenuController\_cntJournal
_cntJournal()
Definition: _UserMenuController.php:109