OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / s / photo / include / db / s_photo_categoryDb.php
1 <?php
2 /**
3  * DBクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: s_photo_categoryDb.php 4711 2012-02-22 13:20:46Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getDbPath() . '/baseDb.php');
17
18 class s_photo_categoryDb extends BaseDb
19 {
20         /**
21          * すべての言語を取得
22          *
23          * @param function      $callback                       コールバック関数
24          * @return                      true=取得、false=取得せず
25          */
26         function getAllLang($callback)
27         {
28                 $queryStr = 'SELECT * FROM _language ORDER BY ln_priority';
29                 $this->selectLoop($queryStr, array(), $callback, null);
30         }
31         /**
32          * 画像カテゴリー一覧を取得
33          *
34          * @param string        $lang                           言語
35          * @param function      $callback                       コールバック関数
36          * @param bool      $onlyVisible                表示可能なカテゴリーのみ取得
37          * @return                      なし
38          */
39         function getAllCategory($lang, $callback, $onlyVisible=false)
40         {
41                 $queryStr = 'SELECT * FROM photo_category LEFT JOIN _login_user ON hc_create_user_id = lu_id AND lu_deleted = false ';
42                 $queryStr .=  'WHERE hc_language_id = ? ';
43                 $queryStr .=    'AND hc_deleted = false ';              // 削除されていない
44                 if ($onlyVisible) $queryStr .=    'AND hc_visible = true ';             // 表示可能項目のみ
45                 $queryStr .=  'ORDER BY hc_sort_order';
46                 $this->selectLoop($queryStr, array($lang), $callback);
47         }
48         /**
49          * 画像親カテゴリー一覧を取得
50          *
51          * @param string        $lang           言語
52          * @param array     $row                レコード
53          * @return bool                                 true=取得、false=取得せず
54          */
55         function getAllPCategory($lang, &$rows)
56         {
57                 $queryStr = 'SELECT DISTINCT pc1.hc_parent_id AS parent, pc2.hc_name AS name FROM photo_category AS pc1 RIGHT JOIN photo_category AS pc2 ON pc1.hc_parent_id = pc2.hc_id AND pc1.hc_language_id = pc2.hc_language_id AND pc2.hc_deleted = false ';
58                 $queryStr .=  'WHERE pc1.hc_deleted = false ';          // 削除されていない
59                 $queryStr .=    'AND pc1.hc_parent_id > 0 ';            // 親カテゴリーあり
60                 $queryStr .=    'AND pc1.hc_language_id = ? ';
61                 $queryStr .=  'ORDER BY pc2.hc_sort_order';
62                 $retValue = $this->selectRecords($queryStr, array($lang), $rows);
63                 return $retValue;
64         }
65         /**
66          * メニュー作成用のカテゴリ一覧を取得
67          *
68          * @param string  $langId                       言語ID
69          * @param array   $categoryArray        取得するカテゴリID
70          * @param array  $rows                          取得レコード
71          * @return                                                      true=取得、false=取得せず
72          */
73         function getAllCategoryForMenu($langId, $categoryArray, &$rows)
74         {
75                 if (count($categoryArray) <= 0) return false;
76                 
77                 // CASE文作成
78                 $categoryId = '';
79                 $caseStr = 'CASE hc_parent_id ';
80                 for ($i = 0; $i < count($categoryArray); $i++){
81                         //$id = '\'' . addslashes($categoryArray[$i]) . '\'';
82                         $id = addslashes($categoryArray[$i]);
83                         $caseStr .= 'WHEN ' . $id . ' THEN ' . $i . ' ';
84                         $categoryId .= $id . ',';
85                 }
86                 $caseStr .= 'END AS no ';
87                 $categoryId = rtrim($categoryId, ',');
88
89                 $queryStr  = 'SELECT *, ' . $caseStr . ' FROM photo_category ';
90                 $queryStr .=   'WHERE hc_deleted = false ';             // 削除されていない
91                 $queryStr .=     'AND hc_visible = true ';              // 表示
92                 $queryStr .=     'AND hc_language_id = ? ';
93                 $queryStr .=     'AND hc_parent_id in (' . $categoryId . ') ';
94                 $queryStr .=   'ORDER BY no, hc_sort_order';
95                 $retValue = $this->selectRecords($queryStr, array($langId), $rows);
96                 return $retValue;
97         }
98         /**
99          * 画像カテゴリーの対応言語を取得
100          *
101          * @param int           $id                     画像カテゴリーID
102          * @param array     $row                レコード
103          * @return bool                                 true=取得、false=取得せず
104          */
105         function getLangByCategoryId($id, &$rows)
106         {
107                 $queryStr = 'SELECT ln_id, ln_name, ln_name_en FROM photo_category LEFT JOIN _language ON hc_language_id = ln_id ';
108                 $queryStr .=  'WHERE hc_deleted = false ';      // 削除されていない
109                 $queryStr .=    'AND hc_id = ? ';
110                 $queryStr .=  'ORDER BY hc_id, ln_priority';
111                 $retValue = $this->selectRecords($queryStr, array($id), $rows);
112                 return $retValue;
113         }
114         /**
115          * 画像カテゴリーの新規追加
116          *
117          * @param int     $id   カテゴリーID
118          * @param string  $lang                 言語ID
119          * @param string  $name                 名前
120          * @param string  $password             パスワード
121          * @param int     $pcategory    親カテゴリーID
122          * @param int     $index                表示順
123          * @param bool    $visible              表示、非表示
124          * @param int     $userId               更新者ユーザID
125          * @param int     $newSerial    新規シリアル番号
126          * @return bool                                 true = 成功、false = 失敗
127          */
128         function addCategory($id, $lang, $name, $password, $pcategory, $index, $visible, $userId, &$newSerial)
129         {       
130                 // トランザクション開始
131                 $this->startTransaction();
132                 
133                 if ($id == 0){          // IDが0のときは、カテゴリーIDを新規取得
134                         // コンテンツIDを決定する
135                         $queryStr = 'select max(hc_id) as mid from photo_category ';
136                         $ret = $this->selectRecord($queryStr, array(), $row);
137                         if ($ret){
138                                 $cId = $row['mid'] + 1;
139                         } else {
140                                 $cId = 1;
141                         }
142                 } else {
143                         $cId = $id;
144                 }
145                 
146                 // 前レコードの削除状態チェック
147                 $historyIndex = 0;
148                 $queryStr = 'SELECT * FROM photo_category ';
149                 $queryStr .=  'WHERE hc_id = ? ';
150                 $queryStr .=    'AND hc_language_id = ? ';
151                 $queryStr .=  'ORDER BY hc_history_index DESC ';
152                 $ret = $this->selectRecord($queryStr, array($cId, $lang), $row);
153                 if ($ret){
154                         if (!$row['hc_deleted']){               // レコード存在していれば終了
155                                 $this->endTransaction();
156                                 return false;
157                         }
158                         $historyIndex = $row['hc_history_index'] + 1;
159                 }
160                 
161                 // データを追加
162                 $queryStr = 'INSERT INTO photo_category ';
163                 $queryStr .=  '(hc_id, hc_language_id, hc_history_index, hc_name, hc_password, hc_parent_id, hc_sort_order, hc_visible, hc_create_user_id, hc_create_dt) ';
164                 $queryStr .=  'VALUES ';
165                 $queryStr .=  '(?, ?, ?, ?, ?, ?, ?, ?, ?, now())';
166                 $this->execStatement($queryStr, array($cId, $lang, $historyIndex, $name, $password, $pcategory, $index, $visible, $userId));
167                 
168                 // 新規のシリアル番号取得
169                 $queryStr = 'select max(hc_serial) as ns from photo_category ';
170                 $ret = $this->selectRecord($queryStr, array(), $row);
171                 if ($ret) $newSerial = $row['ns'];
172                         
173                 // トランザクション確定
174                 $ret = $this->endTransaction();
175                 return $ret;
176         }
177         /**
178          * 画像カテゴリーの更新
179          *
180          * @param int     $serial               シリアル番号
181          * @param string  $name                 名前
182          * @param string  $password             パスワード
183          * @param int     $pcategory    親カテゴリーID
184          * @param int     $index                表示順
185          * @param bool    $visible              表示、非表示
186          * @param int     $userId               更新者ユーザID
187          * @param int     $newSerial    新規シリアル番号
188          * @return bool                                 true = 成功、false = 失敗
189          */
190         function updateCategory($serial, $name, $password, $pcategory, $index, $visible, $userId, &$newSerial)
191         {       
192                 $now = date("Y/m/d H:i:s");     // 現在日時
193                                 
194                 // トランザクション開始
195                 $this->startTransaction();
196                 
197                 // 指定のシリアルNoのレコードが削除状態でないかチェック
198                 $changePCategory = false;               // 親カテゴリを変更かどうか
199                 $historyIndex = 0;              // 履歴番号
200                 $queryStr  = 'select * from photo_category ';
201                 $queryStr .=   'where hc_serial = ? ';
202                 $ret = $this->selectRecord($queryStr, array($serial), $row);
203                 if ($ret){              // 既に登録レコードがあるとき
204                         if ($row['hc_deleted']){                // レコードが削除されていれば終了
205                                 $this->endTransaction();
206                                 return false;
207                         }
208                         $historyIndex = $row['hc_history_index'] + 1;
209                         if ($pcategory != $row['hc_parent_id']) $changePCategory = true;
210                 } else {                // 存在しない場合は終了
211                         $this->endTransaction();
212                         return false;
213                 }
214                 // 親カテゴリーが変更のときは、同じカテゴリーIDの親カテゴリーも変更
215                 if ($changePCategory){
216                         $queryStr = 'SELECT hc_serial FROM photo_category ';
217                         $queryStr .=  'WHERE hc_deleted = false ';      // 削除されていない
218                         $queryStr .=    'AND hc_id = ? ';
219                         $ret = $this->selectRecords($queryStr, array($row['hc_id']), $rows);
220                         if ($ret){
221                                 for ($i = 0; $i < count($rows); $i++){
222                                         if ($rows[$i]['hc_serial'] != $serial){
223                                                 if (!$this->updatePCategory($rows[$i]['hc_serial'], $pcategory, $userId, $now)){
224                                                         $this->endTransaction();
225                                                         return false;           
226                                                 }
227                                         }
228                                 }
229                         } else {
230                                 $this->endTransaction();
231                                 return false;                   
232                         }
233                 }
234                 
235                 // 古いレコードを削除
236                 $queryStr  = 'UPDATE photo_category ';
237                 $queryStr .=   'SET hc_deleted = true, ';       // 削除
238                 $queryStr .=     'hc_update_user_id = ?, ';
239                 $queryStr .=     'hc_update_dt = ? ';
240                 $queryStr .=   'WHERE hc_serial = ?';
241                 $this->execStatement($queryStr, array($userId, $now, $serial));
242                 
243                 // 新規レコード追加
244                 $queryStr = 'INSERT INTO photo_category ';
245                 $queryStr .=  '(hc_id, hc_language_id, hc_history_index, hc_name, hc_password, hc_parent_id, hc_sort_order, hc_visible, hc_create_user_id, hc_create_dt) ';
246                 $queryStr .=  'VALUES ';
247                 $queryStr .=  '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
248                 $this->execStatement($queryStr, array($row['hc_id'], $row['hc_language_id'], $historyIndex, $name, $password, $pcategory, $index, $visible, $userId, $now));
249
250                 // 新規のシリアル番号取得
251                 $queryStr = 'select max(hc_serial) as ns from photo_category ';
252                 $ret = $this->selectRecord($queryStr, array(), $row);
253                 if ($ret) $newSerial = $row['ns'];
254                 
255                 // トランザクション確定
256                 $ret = $this->endTransaction();
257                 return $ret;
258         }
259         /**
260          * 画像カテゴリーをシリアル番号で取得
261          *
262          * @param int           $serial                         シリアル番号
263          * @param array     $row                                レコード
264          * @return bool                                                 取得 = true, 取得なし= false
265          */
266         function getCategoryBySerial($serial, &$row)
267         {
268                 $queryStr  = 'select * from photo_category LEFT JOIN _login_user ON hc_create_user_id = lu_id AND lu_deleted = false ';
269                 $queryStr .=   'WHERE hc_serial = ? ';
270                 $ret = $this->selectRecord($queryStr, array($serial), $row);
271                 return $ret;
272         }       
273         /**
274          * 画像カテゴリーをカテゴリーIDで取得
275          *
276          * @param int           $id                                     カテゴリーID
277          * @param string        $langId                         言語ID
278          * @param array     $row                                レコード
279          * @return bool                                                 取得 = true, 取得なし= false
280          */
281         function getCategoryByCategoryId($id, $langId, &$row)
282         {
283                 $queryStr  = 'SELECT * FROM photo_category LEFT JOIN _login_user ON hc_create_user_id = lu_id AND lu_deleted = false ';
284                 $queryStr .=   'WHERE hc_deleted = false ';     // 削除されていない
285                 $queryStr .=   'AND hc_id = ? ';
286                 $queryStr .=   'AND hc_language_id = ? ';
287                 $ret = $this->selectRecord($queryStr, array($id, $langId), $row);
288                 return $ret;
289         }
290         /**
291          * 親画像カテゴリーの更新
292          *
293          * @param int     $serial               シリアル番号
294          * @param int     $pcategory    親カテゴリーID
295          * @param int     $userId               更新者ユーザID
296          * @param string  $now                  現在日時
297          * @return bool                                 true = 成功、false = 失敗
298          */
299         function updatePCategory($serial, $pcategory, $userId, $now)
300         {
301                 // 指定のシリアルNoのレコードが削除状態でないかチェック
302                 $historyIndex = 0;              // 履歴番号
303                 $queryStr  = 'select * from photo_category ';
304                 $queryStr .=   'where hc_serial = ? ';
305                 $ret = $this->selectRecord($queryStr, array($serial), $row);
306                 if ($ret){              // 既に登録レコードがあるとき
307                         if ($row['hc_deleted']){                // レコードが削除されていれば終了
308                                 return false;
309                         }
310                         $historyIndex = $row['hc_history_index'] + 1;
311                 } else {                // 存在しない場合は終了
312                         return false;
313                 }
314                 
315                 // 古いレコードを削除
316                 $queryStr  = 'UPDATE photo_category ';
317                 $queryStr .=   'SET hc_deleted = true, ';       // 削除
318                 $queryStr .=     'hc_update_user_id = ?, ';
319                 $queryStr .=     'hc_update_dt = ? ';
320                 $queryStr .=   'WHERE hc_serial = ?';
321                 $ret = $this->execStatement($queryStr, array($userId, $now, $serial));
322                 if (!$ret) return false;
323                 
324                 // 新規レコード追加
325                 $queryStr = 'INSERT INTO photo_category ';
326                 $queryStr .=  '(hc_id, hc_language_id, hc_history_index, hc_name, hc_parent_id, hc_sort_order, hc_create_user_id, hc_create_dt) ';
327                 $queryStr .=  'VALUES ';
328                 $queryStr .=  '(?, ?, ?, ?, ?, ?, ?, ?)';
329                 $ret = $this->execStatement($queryStr, array($row['hc_id'], $row['hc_language_id'], $historyIndex, $row['hc_name'], $pcategory, $row['hc_sort_order'], $userId, $now));
330                 if ($ret){
331                         return true;
332                 } else {
333                         return false;
334                 }
335         }
336         /**
337          * カテゴリーの削除
338          *
339          * @param int $serialNo                 シリアルNo
340          * @param int $userId                   ユーザID(データ更新者)
341          * @return                                              true=成功、false=失敗
342          */
343         function delCategory($serialNo, $userId)
344         {
345                 // トランザクション開始
346                 $this->startTransaction();
347                 
348                 // 指定のシリアルNoのレコードが削除状態でないかチェック
349                 $queryStr  = 'select * from photo_category ';
350                 $queryStr .=   'where hc_deleted = false ';             // 未削除
351                 $queryStr .=     'and hc_serial = ? ';
352                 $ret = $this->isRecordExists($queryStr, array($serialNo));
353                 // 存在しない場合は、既に削除されたとして終了
354                 if (!$ret){
355                         $this->endTransaction();
356                         return false;
357                 }
358                 
359                 // レコードを削除
360                 $queryStr  = 'UPDATE photo_category ';
361                 $queryStr .=   'SET hc_deleted = true, ';       // 削除
362                 $queryStr .=     'hc_update_user_id = ?, ';
363                 $queryStr .=     'hc_update_dt = now() ';
364                 $queryStr .=   'WHERE hc_serial = ?';
365                 $this->execStatement($queryStr, array($userId, $serialNo));
366                 
367                 // トランザクション確定
368                 $ret = $this->endTransaction();
369                 return $ret;
370         }
371         /**
372          * カテゴリーをシリアル番号で削除
373          *
374          * @param array   $serial               シリアルNo
375          * @return                                              true=成功、false=失敗
376          */
377         function delCategoryBySerial($serial)
378         {
379                 global $gEnvManager;
380                 
381                 // 引数のエラーチェック
382                 if (!is_array($serial)) return false;
383                 if (count($serial) <= 0) return true;
384                 
385                 $now = date("Y/m/d H:i:s");     // 現在日時
386                 $userId = $gEnvManager->getCurrentUserId();     // 現在のユーザ
387                 
388                 // トランザクション開始
389                 $this->startTransaction();
390                 
391                 // 指定のシリアルNoのレコードが削除状態でないかチェック
392                 for ($i = 0; $i < count($serial); $i++){
393                         $queryStr  = 'SELECT * FROM photo_category ';
394                         $queryStr .=   'WHERE hc_deleted = false ';             // 未削除
395                         $queryStr .=     'AND hc_serial = ? ';
396                         $ret = $this->isRecordExists($queryStr, array($serial[$i]));
397                         // 存在しない場合は、既に削除されたとして終了
398                         if (!$ret){
399                                 $this->endTransaction();
400                                 return false;
401                         }
402                 }
403                 
404                 // レコードを削除
405                 $queryStr  = 'UPDATE photo_category ';
406                 $queryStr .=   'SET hc_deleted = true, ';       // 削除
407                 $queryStr .=     'hc_update_user_id = ?, ';
408                 $queryStr .=     'hc_update_dt = ? ';
409                 $queryStr .=   'WHERE hc_serial in (' . implode($serial, ',') . ') ';
410                 $this->execStatement($queryStr, array($userId, $now));
411                 
412                 // トランザクション確定
413                 $ret = $this->endTransaction();
414                 return $ret;
415         }
416         /**
417          * カテゴリーIDで削除
418          *
419          * @param int $serial                   シリアルNo
420          * @param int $userId                   ユーザID(データ更新者)
421          * @return                                              true=成功、false=失敗
422          */
423         function delCategoryById($serial, $userId)
424         {
425                 // トランザクション開始
426                 $this->startTransaction();
427                 
428                 // コンテンツIDを取得
429                 $queryStr  = 'select * from photo_category ';
430                 $queryStr .=   'where hc_deleted = false ';             // 未削除
431                 $queryStr .=     'and hc_serial = ? ';
432                 $ret = $this->selectRecord($queryStr, array($serial), $row);
433                 if ($ret){              // 既に登録レコードがあるとき
434                         if ($row['hc_deleted']){                // レコードが削除されていれば終了
435                                 $this->endTransaction();
436                                 return false;
437                         }
438                 } else {                // 存在しない場合は終了
439                         $this->endTransaction();
440                         return false;
441                 }
442                 $contId = $row['hc_id'];
443                 
444                 // レコードを削除
445                 $queryStr  = 'UPDATE photo_category ';
446                 $queryStr .=   'SET hc_deleted = true, ';       // 削除
447                 $queryStr .=     'hc_update_user_id = ?, ';
448                 $queryStr .=     'hc_update_dt = now() ';
449                 $queryStr .=   'WHERE hc_id = ?';
450                 $this->execStatement($queryStr, array($userId, $contId));
451                 
452                 // トランザクション確定
453                 $ret = $this->endTransaction();
454                 return $ret;
455         }
456         /**
457          * 指定したカテゴリーが親であるカテゴリーを取得
458          *
459          * @param int           $id                     親画像カテゴリーID(0はトップレベル)
460          * @param string        $langId                         言語ID
461          * @return array                                カテゴリーIDの配列
462          */
463         function getChildCategory($id, $lang)
464         {
465                 $retArray = array();
466                 $queryStr = 'SELECT hc_id FROM photo_category ';
467                 $queryStr .=  'WHERE hc_deleted = false ';      // 削除されていない
468                 $queryStr .=    'AND hc_parent_id = ? ';
469                 $queryStr .=    'AND hc_language_id = ? ';
470                 $queryStr .=  'ORDER BY hc_sort_order';
471                 $ret = $this->selectRecords($queryStr, array($id, $lang), $rows);
472                 if ($ret){
473                         for ($i = 0; $i < count($rows); $i++){
474                                 $retArray[] = $rows[$i]['hc_id'];
475                         }
476                 }
477                 return $retArray;
478         }
479         /**
480          * 指定したカテゴリーが親であるカテゴリーを取得
481          *
482          * @param int           $id                     親画像カテゴリーID(0はトップレベル)
483          * @param string        $langId                         言語ID
484          * @param array         $rows           取得した行データ
485          * @return int                                  取得した行数
486          */
487         function getChildCategoryWithRows($id, $lang, &$rows)
488         {
489                 $retCount = 0;
490                 $queryStr = 'SELECT hc_id,hc_name FROM photo_category ';
491                 $queryStr .=  'WHERE hc_deleted = false ';      // 削除されていない
492                 $queryStr .=    'AND hc_parent_id = ? ';
493                 $queryStr .=    'AND hc_language_id = ? ';
494                 $queryStr .=  'ORDER BY hc_sort_order';
495                 $ret = $this->selectRecords($queryStr, array($id, $lang), $rows);
496                 if ($ret){
497                         $retCount = count($rows);
498                 }
499                 return $retCount;
500         }
501         /**
502          * カテゴリーを取得
503          *
504          * @param function      $callback                       コールバック関数
505          * @param array         $idArray                        カテゴリーID
506          * @param string        $lang                           言語
507          * @return                      なし
508          */
509         function getCategoryByIdArray($callback, $idArray, $lang)
510         {
511                 $catId = implode(',', $idArray);
512                 
513                 // CASE文作成
514                 $caseStr = 'CASE hc_id ';
515                 for ($i = 0; $i < count($idArray); $i++){
516                         $caseStr .= 'WHEN ' . $idArray[$i] . ' THEN ' . $i . ' ';
517                 }
518                 $caseStr .= 'END AS no';
519
520                 $queryStr = 'SELECT *, ' . $caseStr . ' FROM photo_category ';
521                 $queryStr .=  'WHERE hc_deleted = false ';              // 削除されていない
522                 $queryStr .=    'AND hc_id in (' . $catId . ') ';
523                 $queryStr .=    'AND hc_language_id = ? ';
524                 $queryStr .=  'ORDER BY no';
525                 $this->selectLoop($queryStr, array($lang), $callback, null);
526         }
527         /**
528          * 最大表示順を取得
529          *
530          * @param string        $lang           言語
531          * @return int                                  最大表示順
532          */
533         function getMaxIndex($lang)
534         {
535                 $queryStr = 'SELECT max(hc_sort_order) as mi FROM photo_category ';
536                 $queryStr .=  'WHERE hc_deleted = false ';
537                 $queryStr .=  'AND hc_language_id = ? ';
538                 $ret = $this->selectRecord($queryStr, array($lang), $row);
539                 if ($ret){
540                         $index = $row['mi'];
541                 } else {
542                         $index = 0;
543                 }
544                 return $index;
545         }
546 }
547 ?>