JSlip  1.0
UserItemModel.php
Go to the documentation of this file.
1 <?php
8 require_once(dirname(__FILE__) . '/../../lib/Model.php');
9 
10 class UserItemModel extends Model
11 {
12  public $bid;
13 
14  function __construct($bid) {
15  $this->bid = $bid;
16  }
17 
18  public function getList($cnd) {
19 
20  $this->connect();
21 
22  $where = $this->_getListWhere($cnd);
23  $cnt = $this->_getListCnt($where);
24  $list = $this->_getListDat($where, $cnt, $cnd['pager']);
25 
26  $this->close();
27 
28  return $list;
29  }
30 
31  private function _getListWhere($cnd) {
32 
33  $where = " WHERE `bid` = '" . $this->esc($this->bid) . "'";
34 
35  if ($cnd['cnd_kana'] != '') {
36  $where .= " AND `kana` LIKE '%" . $this->esc($cnd['cnd_kana']) . "%'";
37  }
38 
39  if ($cnd['cnd_name'] != '') {
40  $where .= " AND `name` LIKE '%" . $this->esc($cnd['cnd_name']) . "%'";
41  }
42 
43  return $where;
44  }
45 
46  private function _getListCnt($where) {
47 
48  $sql = "SELECT COUNT(*) AS `cnt` FROM `t_item`" . $where;
49  $rec = $this->getRecord($sql);
50 
51  return $rec[0]['cnt'];
52  }
53 
54  private function _getListDat($where, $cnt, $pager) {
55 
56  $pg = $this->getPaging($cnt, $pager['page'], $pager['rpp']);
57 
58  if ($cnt < 0) {
59  $rec = [];
60  } else {
61  $sql = "SELECT * FROM `t_item`" . $where
62  . " ORDER BY `ccd`, `account`, `item`, `kana`"
63  . " LIMIT " . $pg['ofst'] . ", " . $pg['rpp']
64  ;
65  $rec = $this->getRecord($sql);
66  }
67 
68  return [
69  'cnt' => $pg['cnt'],
70  'rpp' => $pg['rpp'],
71  'last' => $pg['last'],
72  'page' => $pg['page'],
73  'rec' => $rec,
74  ];
75  }
76 
77  public function getData($id) {
78 
79  $this->connect();
80  $sql = "SELECT * FROM `t_item` WHERE `id` = '" . $this->esc($id) . "'";
81  $rec = $this->getRecord($sql);
82  $this->close();
83 
84  return (empty($rec[0])) ? [] : $rec[0];
85  }
86 
87  public function regist($param) {
88 
89  $err = '';
90 
91  $this->connect();
92  $this->begin();
93 
94  $ccd = substr($param['kcode'], 0, 4);
95  $account = substr($param['kcode'], 4, 2);
96 
97  try {
98 
99  $sql = "UPDATE `t_item` SET"
100  . " `ccd`" . " = '" . $this->esc($ccd) . "'"
101  . ", `account`" . " = '" . $this->esc($account) . "'"
102  . ", `item`" . " = '" . $this->esc($param['item']) . "'"
103  . ", `kana`" . " = '" . $this->esc($param['kana']) . "'"
104  . ", `name`" . " = '" . $this->esc($param['name']) . "'"
105  . ", `valid_flg`" . " = " . $this->esc($param['valid_flg'])
106  . ", `update_person`" . " = " . $_SESSION['minfo']['mid']
107  . " WHERE `id` = '" . $this->esc($param['id']) . "'"
108  ;
109  $ans = $this->query($sql);
110 
111  } catch(Exception $e) {
112  $err = $e->getMessage();
113  }
114 
115  if (empty($err)) {
116  $this->commit();
117  } else {
118  $this->rollback();
119  }
120 
121  $this->close();
122 
123  return $err;
124  }
125 
126  public function insert($param) {
127 
128  $err = '';
129 
130  $this->connect();
131  $this->begin();
132 
133  $ccd = substr($param['kcode'], 0, 4);
134  $account = substr($param['kcode'], 4, 2);
135 
136  try {
137 
138  $sql = "INSERT INTO `t_item`"
139  . " (`bid`, `kcd`, `ccd`, `account`, `item`, `kana`, `name`, `valid_flg`, `delete_flg`, `edit_flg`, `update_person`)"
140  . " VALUES"
141  . " ('" . $this->esc($param['bid']) . "'"
142  . ", '" . sprintf("%s%s%02d", $ccd, $account, $param['item']) . "'"
143  . ", '" . $this->esc($ccd) . "'"
144  . ", '" . $this->esc($account) . "'"
145  . ", '" . $this->esc($param['item']) . "'"
146  . ", '" . $this->esc($param['kana']) . "'"
147  . ", '" . $this->esc($param['name']) . "'"
148  . ", " . $param['valid_flg']
149  . ", TRUE"
150  . ", TRUE"
151  . ", '" . $_SESSION['minfo']['mid']. "'"
152  . ")"
153  ;
154  $ans = $this->query($sql);
155 
156  } catch(Exception $e) {
157  $err = $e->getMessage();
158  }
159 
160  if (empty($err)) {
161  $this->commit();
162  } else {
163  $this->rollback();
164  }
165 
166  $this->close();
167 
168  return $err;
169  }
170 
171  public function delete($param) {
172 
173  $err = '';
174  $id = $param['id'];
175 
176  $this->connect();
177  $this->begin();
178 
179  try {
180 
181  $sql = "DELETE FROM `t_item` WHERE `id` = '" . $this->esc($id) . "'";
182  $ans = $this->query($sql);
183 
184  } catch(Exception $e) {
185  $err = $e->getMessage();
186  }
187 
188  if (empty($err)) {
189  $this->commit();
190  } else {
191  $this->rollback();
192  }
193 
194  $this->close();
195 
196  return $err;
197  }
198 
199  public function chkDup($param) {
200 
201  $ccd = substr($param['kcode'], 0, 4);
202  $account = substr($param['kcode'], 4, 2);
203 
204  $this->connect();
205 
206  $sql = "SELECT COUNT(*) AS `cnt`"
207  . " FROM `t_item`"
208  . " WHERE `bid` = '" . $this->esc($param['bid']) . "'"
209  . " AND `ccd` = '" . $this->esc($ccd) . "'"
210  . " AND `account` = '" . $this->esc($account) . "'"
211  . " AND `item` = '" . $this->esc($param['item']) . "'"
212  ;
213 
214  if (empty($param['insert'])) {
215  $sql .= " AND `id` != '" . $this->esc($param['id']) . "'";
216  }
217 
218  $rec = $this->getRecord($sql);
219 
220  $this->close();
221 
222  return $rec[0]['cnt'];
223  }
224 
225  public function setValidFlg($param) {
226 
227  $err = '';
228 
229  $this->connect();
230  $this->begin();
231 
232  try {
233 
234  $sql = "UPDATE `t_item` SET"
235  . " `valid_flg` = " . $param['valid_flg']
236  . " WHERE `id` = '" . $this->esc($param['id']) . "'"
237  ;
238  $ans = $this->query($sql);
239 
240  } catch(Exception $e) {
241  $err = $e->getMessage();
242  }
243 
244  if (empty($err)) {
245  $this->commit();
246  } else {
247  $this->rollback();
248  }
249 
250  $this->close();
251 
252  return $err;
253  }
254 }
UserItemModel\$bid
$bid
Definition: UserItemModel.php:12
UserItemModel\_getListCnt
_getListCnt($where)
Definition: UserItemModel.php:46
Model\connect
connect()
Definition: Model.php:12
Model\begin
begin()
Definition: Model.php:31
UserItemModel\getData
getData($id)
Definition: UserItemModel.php:77
UserItemModel\getList
getList($cnd)
Definition: UserItemModel.php:18
Model\query
query($sql)
Definition: Model.php:47
Model\getRecord
getRecord($sql)
Definition: Model.php:55
UserItemModel
Definition: UserItemModel.php:10
UserItemModel\chkDup
chkDup($param)
Definition: UserItemModel.php:199
UserItemModel\_getListWhere
_getListWhere($cnd)
Definition: UserItemModel.php:31
UserItemModel\_getListDat
_getListDat($where, $cnt, $pager)
Definition: UserItemModel.php:54
Model
Definition: Model.php:8
Model\commit
commit()
Definition: Model.php:35
Model\close
close()
Definition: Model.php:27
Model\esc
esc($str)
Definition: Model.php:43
UserItemModel\regist
regist($param)
Definition: UserItemModel.php:87
Model\getPaging
getPaging($cnt, $page, $rpp)
Definition: Model.php:69
UserItemModel\insert
insert($param)
Definition: UserItemModel.php:126
UserItemModel\__construct
__construct($bid)
Definition: UserItemModel.php:14
$cnt
$cnt
Definition: tex_tmplt_bs.php:319
UserItemModel\setValidFlg
setValidFlg($param)
Definition: UserItemModel.php:225
Model\rollback
rollback()
Definition: Model.php:39