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