| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * ブロックを管理するヘルパークラス. |
|---|
| 26 | * |
|---|
| 27 | * @package Helper |
|---|
| 28 | * @author pineray |
|---|
| 29 | * @version $Id:$ |
|---|
| 30 | */ |
|---|
| 31 | class SC_Helper_Bloc |
|---|
| 32 | { |
|---|
| 33 | private $device_type_id = NULL; |
|---|
| 34 | |
|---|
| 35 | public function __construct($devide_type_id = DEVICE_TYPE_PC) |
|---|
| 36 | { |
|---|
| 37 | $this->device_type_id = $devide_type_id; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * ブロックの情報を取得. |
|---|
| 42 | * |
|---|
| 43 | * @param integer $bloc_id ブロックID |
|---|
| 44 | * @return array |
|---|
| 45 | */ |
|---|
| 46 | public function getBloc($bloc_id) |
|---|
| 47 | { |
|---|
| 48 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 49 | $col = '*'; |
|---|
| 50 | $where = 'bloc_id = ? AND device_type_id = ?'; |
|---|
| 51 | $arrRet = $objQuery->getRow($col, 'dtb_bloc', $where, array($bloc_id, $this->device_type_id)); |
|---|
| 52 | if (SC_Utils_Ex::isAbsoluteRealPath($arrRet['tpl_path'])) { |
|---|
| 53 | $tpl_path = $arrRet['tpl_path']; |
|---|
| 54 | } else { |
|---|
| 55 | $tpl_path = SC_Helper_PageLayout_Ex::getTemplatePath($this->device_type_id) . BLOC_DIR . $arrRet['tpl_path']; |
|---|
| 56 | } |
|---|
| 57 | if (file_exists($tpl_path)) { |
|---|
| 58 | $arrRet['bloc_html'] = file_get_contents($tpl_path); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | return $arrRet; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * ブロック一覧の取得. |
|---|
| 66 | * |
|---|
| 67 | * @return array |
|---|
| 68 | */ |
|---|
| 69 | public function getList() |
|---|
| 70 | { |
|---|
| 71 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 72 | $col = '*'; |
|---|
| 73 | $where = 'device_type_id = ?'; |
|---|
| 74 | $table = 'dtb_bloc'; |
|---|
| 75 | $arrRet = $objQuery->select($col, $table, $where, array($this->device_type_id)); |
|---|
| 76 | |
|---|
| 77 | return $arrRet; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * where句で条件を指定してブロック一覧を取得. |
|---|
| 82 | * |
|---|
| 83 | * @param string $where |
|---|
| 84 | * @param array $sqlval |
|---|
| 85 | */ |
|---|
| 86 | public function getWhere($where = '', $sqlval = array()) |
|---|
| 87 | { |
|---|
| 88 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 89 | $col = '*'; |
|---|
| 90 | $where = 'device_type_id = ? ' . (SC_Utils_Ex::isBlank($where) ? $where : 'AND ' . $where); |
|---|
| 91 | array_unshift($sqlval, $this->device_type_id); |
|---|
| 92 | $table = 'dtb_bloc'; |
|---|
| 93 | $arrRet = $objQuery->select($col, $table, $where, $sqlval); |
|---|
| 94 | |
|---|
| 95 | return $arrRet; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * ブロックの登録. |
|---|
| 100 | * |
|---|
| 101 | * @param array $sqlval |
|---|
| 102 | * @return multiple 登録成功:ブロックID, 失敗:FALSE |
|---|
| 103 | */ |
|---|
| 104 | public function save($sqlval) |
|---|
| 105 | { |
|---|
| 106 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 107 | $objQuery->begin(); |
|---|
| 108 | |
|---|
| 109 | // blod_id が空の場合は新規登録 |
|---|
| 110 | $is_new = SC_Utils_Ex::isBlank($sqlval['bloc_id']); |
|---|
| 111 | $bloc_dir = SC_Helper_PageLayout_Ex::getTemplatePath($sqlval['device_type_id']) . BLOC_DIR; |
|---|
| 112 | // 既存データの重複チェック |
|---|
| 113 | if (!$is_new) { |
|---|
| 114 | $arrExists = $this->getBloc($sqlval['bloc_id']); |
|---|
| 115 | |
|---|
| 116 | // 既存のファイルが存在する場合は削除しておく |
|---|
| 117 | $exists_file = $bloc_dir . $arrExists[0]['filename'] . '.tpl'; |
|---|
| 118 | if (file_exists($exists_file)) { |
|---|
| 119 | unlink($exists_file); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | $table = 'dtb_bloc'; |
|---|
| 124 | $arrValues = $objQuery->extractOnlyColsOf($table, $sqlval); |
|---|
| 125 | $arrValues['tpl_path'] = $sqlval['filename'] . '.tpl'; |
|---|
| 126 | $arrValues['update_date'] = 'CURRENT_TIMESTAMP'; |
|---|
| 127 | |
|---|
| 128 | // 新規登録 |
|---|
| 129 | if ($is_new || SC_Utils_Ex::isBlank($arrExists)) { |
|---|
| 130 | $objQuery->setOrder(''); |
|---|
| 131 | $arrValues['bloc_id'] = 1 + $objQuery->max('bloc_id', $table, 'device_type_id = ?', |
|---|
| 132 | array($arrValues['device_type_id'])); |
|---|
| 133 | $arrValues['create_date'] = 'CURRENT_TIMESTAMP'; |
|---|
| 134 | $objQuery->insert($table, $arrValues); |
|---|
| 135 | // 更新 |
|---|
| 136 | } else { |
|---|
| 137 | $objQuery->update($table, $arrValues, 'bloc_id = ? AND device_type_id = ?', |
|---|
| 138 | array($arrValues['bloc_id'], $arrValues['device_type_id'])); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | $bloc_path = $bloc_dir . $arrValues['tpl_path']; |
|---|
| 142 | if (!SC_Helper_FileManager_Ex::sfWriteFile($bloc_path, $sqlval['bloc_html'])) { |
|---|
| 143 | $objQuery->rollback(); |
|---|
| 144 | |
|---|
| 145 | return false; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | $objQuery->commit(); |
|---|
| 149 | |
|---|
| 150 | return $arrValues['bloc_id']; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * ブロックの削除. |
|---|
| 155 | * |
|---|
| 156 | * @param integer $bloc_id |
|---|
| 157 | * @return boolean |
|---|
| 158 | */ |
|---|
| 159 | public function delete($bloc_id) |
|---|
| 160 | { |
|---|
| 161 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 162 | $objQuery->begin(); |
|---|
| 163 | |
|---|
| 164 | $arrExists = $this->getWhere('bloc_id = ? AND deletable_flg = 1', array($bloc_id)); |
|---|
| 165 | $is_error = false; |
|---|
| 166 | if (!SC_Utils_Ex::isBlank($arrExists)) { |
|---|
| 167 | $objQuery->delete('dtb_bloc', 'bloc_id = ? AND device_type_id = ?', |
|---|
| 168 | array($arrExists[0]['bloc_id'], $arrExists[0]['device_type_id'])); |
|---|
| 169 | $objQuery->delete('dtb_blocposition', 'bloc_id = ? AND device_type_id = ?', |
|---|
| 170 | array($arrExists[0]['bloc_id'], $arrExists[0]['device_type_id'])); |
|---|
| 171 | |
|---|
| 172 | $bloc_dir = SC_Helper_PageLayout_Ex::getTemplatePath($this->device_type_id) . BLOC_DIR; |
|---|
| 173 | $exists_file = $bloc_dir . $arrExists[0]['filename'] . '.tpl'; |
|---|
| 174 | |
|---|
| 175 | // ファイルの削除 |
|---|
| 176 | if (file_exists($exists_file)) { |
|---|
| 177 | if (!unlink($exists_file)) { |
|---|
| 178 | $is_error = true; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } else { |
|---|
| 182 | $is_error = true; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if ($is_error) { |
|---|
| 186 | $objQuery->rollback(); |
|---|
| 187 | |
|---|
| 188 | return false; |
|---|
| 189 | } |
|---|
| 190 | $objQuery->commit(); |
|---|
| 191 | |
|---|
| 192 | return true; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * 端末種別IDのメンバー変数を取得. |
|---|
| 197 | * |
|---|
| 198 | * @return integer 端末種別ID |
|---|
| 199 | */ |
|---|
| 200 | public function getDeviceTypeID() |
|---|
| 201 | { |
|---|
| 202 | return $this->device_type_id; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|