| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2014 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 | /* ---- SQL文を作るクラス ---- */ |
|---|
| 25 | class SC_SelectSql |
|---|
| 26 | { |
|---|
| 27 | public $sql; |
|---|
| 28 | |
|---|
| 29 | public $select; |
|---|
| 30 | public $where; |
|---|
| 31 | public $order; |
|---|
| 32 | public $group; |
|---|
| 33 | public $arrSql; |
|---|
| 34 | public $arrVal; |
|---|
| 35 | |
|---|
| 36 | //-- コンストラクタ。 |
|---|
| 37 | public function __construct($array = '') |
|---|
| 38 | { |
|---|
| 39 | if (is_array($array)) { |
|---|
| 40 | $this->arrSql = $array; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | //-- SQL分生成 |
|---|
| 45 | public function getSql($mode = '') |
|---|
| 46 | { |
|---|
| 47 | $this->sql = $this->select .' '. $this->where .' '. $this->group .' '; |
|---|
| 48 | |
|---|
| 49 | if ($mode == 2) { |
|---|
| 50 | $this->sql .= $this->order; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | return $this->sql; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // 検索用 |
|---|
| 57 | public function addSearchStr($val) |
|---|
| 58 | { |
|---|
| 59 | $return = '%' .$val. '%'; |
|---|
| 60 | |
|---|
| 61 | return $return; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | //-- 範囲検索(○ ~ ○ まで) |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * @param string $column |
|---|
| 68 | */ |
|---|
| 69 | public function selectRange($from, $to, $column) |
|---|
| 70 | { |
|---|
| 71 | // ある単位のみ検索($from = $to) |
|---|
| 72 | if ($from == $to) { |
|---|
| 73 | $this->setWhere($column .' = ?'); |
|---|
| 74 | $return = array($from); |
|---|
| 75 | // ~$toまで検索 |
|---|
| 76 | } elseif (strlen($from) == 0 && strlen($to) > 0) { |
|---|
| 77 | $this->setWhere($column .' <= ? '); |
|---|
| 78 | $return = array($to); |
|---|
| 79 | // ~$from以上を検索 |
|---|
| 80 | } elseif (strlen($from) > 0 && strlen($to) == 0) { |
|---|
| 81 | $this->setWhere($column .' >= ? '); |
|---|
| 82 | $return = array($from); |
|---|
| 83 | // $from~$toの検索 |
|---|
| 84 | } else { |
|---|
| 85 | $this->setWhere($column .' BETWEEN ? AND ?'); |
|---|
| 86 | $return = array($from, $to); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return $return; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | //-- 期間検索(○年○月○日か~○年○月○日まで) |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * @param string $column |
|---|
| 96 | */ |
|---|
| 97 | public function selectTermRange($from_year, $from_month, $from_day, $to_year, $to_month, $to_day, $column) |
|---|
| 98 | { |
|---|
| 99 | $return = array(); |
|---|
| 100 | |
|---|
| 101 | // 開始期間の構築 |
|---|
| 102 | $date1 = $from_year . '/' . $from_month . '/' . $from_day; |
|---|
| 103 | |
|---|
| 104 | // 終了期間の構築 |
|---|
| 105 | // @see http://svn.ec-cube.net/open_trac/ticket/328 |
|---|
| 106 | // FIXME とりあえずintvalで対策... |
|---|
| 107 | $date2 = mktime(0, 0, 0, intval($to_month), intval($to_day), intval($to_year)); |
|---|
| 108 | $date2 = $date2 + 86400; |
|---|
| 109 | // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。 |
|---|
| 110 | $date2 = date('Y/m/d', $date2); |
|---|
| 111 | |
|---|
| 112 | // 開始期間だけ指定の場合 |
|---|
| 113 | if (($from_year != '') && ($from_month != '') && ($from_day != '') && ($to_year == '') && ($to_month == '') && ($to_day == '')) { |
|---|
| 114 | $this->setWhere($column .' >= ?'); |
|---|
| 115 | $return[] = $date1; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // 開始~終了 |
|---|
| 119 | if (($from_year != '') && ($from_month != '') && ($from_day != '') |
|---|
| 120 | && ($to_year != '') && ($to_month != '') && ($to_day != '') |
|---|
| 121 | ) { |
|---|
| 122 | $this->setWhere($column . ' >= ? AND ' . $column . ' < date(?)'); |
|---|
| 123 | $return[] = $date1; |
|---|
| 124 | $return[] = $date2; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // 終了期間だけ指定の場合 |
|---|
| 128 | if (($from_year == '') && ($from_month == '') && ($from_day == '') && ($to_year != '') && ($to_month != '') && ($to_day != '')) { |
|---|
| 129 | $this->setWhere($column . ' < date(?)'); |
|---|
| 130 | $return[] = $date2; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | return $return; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx) AND ... |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * @param string $ItemStr |
|---|
| 140 | */ |
|---|
| 141 | public function setItemTerm($arr, $ItemStr) |
|---|
| 142 | { |
|---|
| 143 | $return = array(); |
|---|
| 144 | foreach ($arr as $data) { |
|---|
| 145 | if (count($arr) > 1) { |
|---|
| 146 | if (!is_null($data)) { |
|---|
| 147 | $item .= $ItemStr . ' = ? OR '; |
|---|
| 148 | } |
|---|
| 149 | } else { |
|---|
| 150 | if (!is_null($data)) { |
|---|
| 151 | $item = $ItemStr . ' = ?'; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | $return[] = $data; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | if (count($arr) > 1) { |
|---|
| 158 | // FIXME 多分この rtrim の使い方は不適切(偶然動作しそうだが) |
|---|
| 159 | $item = '(' . rtrim($item, ' OR ') . ')'; |
|---|
| 160 | } |
|---|
| 161 | $this->setWhere($item); |
|---|
| 162 | |
|---|
| 163 | return $return; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | // NULL値が必要な場合 |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * @param string $ItemStr |
|---|
| 170 | */ |
|---|
| 171 | public function setItemTermWithNull($arr, $ItemStr) |
|---|
| 172 | { |
|---|
| 173 | $return = array(); |
|---|
| 174 | $item = " {$ItemStr} IS NULL "; |
|---|
| 175 | |
|---|
| 176 | if ($arr) { |
|---|
| 177 | foreach ($arr as $data) { |
|---|
| 178 | if ($data != '不明') { |
|---|
| 179 | $item .= " OR {$ItemStr} = ?"; |
|---|
| 180 | $return[] = $data; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | $item = "({$item}) "; |
|---|
| 186 | $this->setWhere($item); |
|---|
| 187 | |
|---|
| 188 | return $return; |
|---|
| 189 | } |
|---|
| 190 | // NULLもしくは''で検索する場合 |
|---|
| 191 | public function setItemTermWithNullAndSpace($arr, $ItemStr) |
|---|
| 192 | { |
|---|
| 193 | $return = array(); |
|---|
| 194 | $count = count($arr); |
|---|
| 195 | $item = " {$ItemStr} IS NULL OR {$ItemStr} = '' "; |
|---|
| 196 | $i = 1; |
|---|
| 197 | if ($arr) { |
|---|
| 198 | foreach ($arr as $data) { |
|---|
| 199 | if ($i == $count) break; |
|---|
| 200 | $item .= " OR {$ItemStr} = ?"; |
|---|
| 201 | $return[] = $data; |
|---|
| 202 | $i ++; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | $item = "({$item}) "; |
|---|
| 206 | $this->setWhere($item); |
|---|
| 207 | |
|---|
| 208 | return $return; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx) AND ... |
|---|
| 212 | |
|---|
| 213 | 配列の構造例 |
|---|
| 214 | if ($_POST['show_site1']) $arrShowsite_1 = array('column' => 'show_site1', |
|---|
| 215 | 'value' => $_POST['show_site1']); |
|---|
| 216 | |
|---|
| 217 | */ |
|---|
| 218 | public function setWhereByOR($arrWhere) |
|---|
| 219 | { |
|---|
| 220 | $count = count($arrWhere); |
|---|
| 221 | |
|---|
| 222 | for ($i = 0; $i < $count; $i++) { |
|---|
| 223 | if (isset($arrWhere[$i]['value'])) { |
|---|
| 224 | $statement .= $arrWhere[$i]['column'] .' = ' . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) .' OR '; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | $statement = '(' . rtrim($statement, ' OR ') . ')'; |
|---|
| 229 | |
|---|
| 230 | if ($this->where) { |
|---|
| 231 | $this->where .= ' AND ' . $statement; |
|---|
| 232 | } else { |
|---|
| 233 | $this->where = 'WHERE ' . $statement; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | * WHERE を取得する。 |
|---|
| 239 | * |
|---|
| 240 | * ヘンテコセッター互換仕様。 |
|---|
| 241 | * @param $with_where boolean 必要に応じて WHERE を前置するか |
|---|
| 242 | * @return string WHERE |
|---|
| 243 | */ |
|---|
| 244 | public function getWhere($with_where = false) |
|---|
| 245 | { |
|---|
| 246 | $where = $this->where; |
|---|
| 247 | |
|---|
| 248 | if (!$with_where) { |
|---|
| 249 | $where = preg_replace('/^\s*WHERE\s+/', '', $where); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | return $where; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | public function setWhere($where) |
|---|
| 256 | { |
|---|
| 257 | if ($where != '') { |
|---|
| 258 | if ($this->where) { |
|---|
| 259 | $this->where .= ' AND ' . $where; |
|---|
| 260 | } else { |
|---|
| 261 | $this->where = 'WHERE ' . $where; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /** |
|---|
| 267 | * @param string $order |
|---|
| 268 | */ |
|---|
| 269 | public function setOrder($order) |
|---|
| 270 | { |
|---|
| 271 | $this->order = 'ORDER BY ' . $order; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | public function setGroup($group) |
|---|
| 275 | { |
|---|
| 276 | $this->group = 'GROUP BY ' . $group; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | public function clearSql() |
|---|
| 280 | { |
|---|
| 281 | $this->select = ''; |
|---|
| 282 | $this->where = ''; |
|---|
| 283 | $this->group = ''; |
|---|
| 284 | $this->order = ''; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | public function setSelect($sql) |
|---|
| 288 | { |
|---|
| 289 | $this->select = $sql; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|