source: branches/camp/camp-2_13-plugin/data/class/plugin/SC_Plugin_Installer.php @ 22675

Revision 22675, 8.4 KB checked in by h_yoshimoto, 11 years ago (diff)

#2181 カラム追加、削除、INSERT、UPDATEを追加

Line 
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 */
23class SC_Plugin_Installer {
24   
25    protected $exec_func;
26   
27    protected $plugin_code;
28   
29    protected $arrPlugin;
30   
31    protected $arrInstallData;
32   
33    public function __construct($exec_func, $arrPlugin) {
34        define('PLUGIN_LOG_REALFILE', DATA_REALDIR . "logs/plugin.log");
35        $this->exec_func   = $exec_func;
36        $this->plugin_code = $arrPlugin['plugin_code'];
37        $this->arrPlugin   = $arrPlugin;
38        $this->arrInstallData = array();
39        $this->arrInstallData['sql'] = array();
40        $this->arrInstallData['copy_file'] = array();
41        $this->arrInstallData['copy_direcrtory'] = array();
42        $this->arrInstallData['remove_file'] = array();
43        $this->arrInstallData['remove_directory'] = array();
44    }
45   
46    public function execInstall() {
47        GC_Utils_Ex::gfPrintLog("start install: " . $this->arrPlugin['plugin_code']);
48       
49        $plugin_code = $this->arrPlugin['plugin_code'];
50
51        $objQuery =& SC_Query::getSingletonInstance();
52       
53        // テーブル作成SQLなどを実行
54        $arrSql = $this->arrInstallData['sql'];
55       
56        foreach ($arrSql as $sql) {
57            GC_Utils_Ex::gfPrintLog("exec sql:" . $sql['sql']);
58            $objQuery->query($sql['sql'], $sql['params']);
59        }
60       
61        $arrInsertQuery = $this->arrInstallData['insert'];
62        foreach ($arrInsertQuery as $insertQuery) {
63            $objQuery->insert(
64                    $insertQuery['table'],
65                    $insertQuery['arrVal'],
66                    $insertQuery['arrSql'],
67                    $insertQuery['arrSqlVal'],
68                    $insertQuery['form'],
69                    $insertQuery['arrFromVal']
70                    );
71        }
72       
73           
74        $arrInsertQuery = $this->arrInstallData['update'];
75        foreach ($arrInsertQuery as $insertQuery) {
76            $objQuery->update(
77                    $insertQuery['table'],
78                    $insertQuery['arrVal'],
79                    $insertQuery['where'],
80                    $insertQuery['arrWhereVal'],
81                    $insertQuery['arrRawSql'],
82                    $insertQuery['arrRawSqlVal']
83                    );
84        }
85                           
86        // プラグインのディレクトリコピー
87        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
88
89        foreach ($arrCopyDirectories as $directory) {
90            GC_Utils_Ex::gfPrintLog("exec dir copy:" . $directory['src']);
91            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
92            SC_Utils::copyDirectory(
93                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
94                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
95        }
96
97        // プラグインのファイルコピー
98        $arrCopyFiles = $this->arrInstallData['copy_file'];
99
100        foreach ($arrCopyFiles as $file) {
101            GC_Utils_Ex::gfPrintLog("exec file copy:" . $file['src']);
102            // ファイルコピー
103            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
104                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
105        }
106
107        GC_Utils_Ex::gfPrintLog("end install: " . $this->arrPlugin['plugin_code']);
108    }
109   
110    public function execPlugin($exec_func) {
111        $this->log("start");
112       
113        $plugin_code = $this->arrPlugin['plugin_code'];
114
115        $objQuery =& SC_Query::getSingletonInstance();
116       
117        // テーブル作成SQLなどを実行
118        $arrSql = $this->arrInstallData['sql'];
119       
120        foreach ($arrSql as $sql) {
121            $this->log("exec sql: " . $sql['sql']);
122            $objQuery->query($sql['sql'], $sql['params']);
123        }
124       
125        // プラグインのディレクトリコピー
126        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
127
128        foreach ($arrCopyDirectories as $directory) {
129            $this->log("exec dir copy: " . $directory['src'] . ' -> ' . $directory['dist']);
130            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
131            SC_Utils::copyDirectory(
132                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
133                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
134        }
135
136        // プラグインのファイルコピー
137        $arrCopyFiles = $this->arrInstallData['copy_file'];
138
139        foreach ($arrCopyFiles as $file) {
140            $this->log("exec file copy: " . $file['src'] . ' -> ' . $file['dist']);
141            // ファイルコピー
142            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
143                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
144        }
145       
146        $this->log("end");         
147    }
148
149    public function copyFile($src, $dist) {
150        $this->arrInstallData['copy_file'][] = array(
151            'src'  => $src,
152            'dist' => $dist
153        );
154    }
155 
156    public function copyDirectory($src, $dist) {
157        $this->arrInstallData['copy_directory'][] = array(
158            'src'  => $src,
159            'dist' => $dist
160        );       
161    }
162   
163    public function removeFile($dist) {
164        $this->arrInstallData['remove_file'][] = array(
165            'dist' => $dist
166        );
167    }
168   
169    public function removeDirectory($dist) {
170       $this->arrInstallData['remove_file'][] = array(
171            'dist' => $dist
172        );     
173    }
174
175    public function sql($sql, array $params = array()) {
176        $this->arrInstallData['sql'][] = array(
177            'sql'    => $sql,
178            'params' => $params
179        );
180    }
181   
182    public function query($sql, array $params = array()) {
183        $this->sql($sql, $params);
184    }
185   
186    protected function log($msg) {
187        $msg = sprintf("%s %s: %s", $this->exec_func, $this->plugin_code, $msg);
188        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
189    }
190   
191    /**
192     * カラム追加クエリの追加
193     *
194     * @param type $table
195     * @param type $col
196     * @param type $type
197     */
198    function sqlAterTableAdd($table_name, $col_name, $col_type) {
199        $sql = ("ALTER TABLE $table_name ADD $col_name $col_type ");
200        $this->sql($sql);
201    }
202   
203    /**
204     * カラム削除クエリの追加
205     *
206     * @param type $table
207     * @param type $col
208     * @param type $type
209     */
210    function sqlAterTableDrop($table_name, $col_name) {
211        $sql = ("ALTER TABLE $table_name DROP $col_name");
212        $this->sql($sql);
213    }
214   
215   
216    function sqlInsert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
217        $this->arrInstallData['insert'] = array(
218            'params'   => array(
219                'table' => $table,
220                'arrVal' => $arrVal,
221                'arrSql' => $arrSql,
222                'arrSqlVal' => $arrSqlVal,
223                'form' =>$from,
224                'arrFromVal' => $arrFromVal)
225        );
226    }
227   
228    function sqlUpdate($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
229        $this->arrInstallData['update'] = array(
230            'params'   => array(
231                'table' => $table,
232                'arrVal' => $arrVal,
233                'where' => $where,
234                'arrWhereVal' => $arrWhereVal,
235                'arrRawSql' =>$arrRawSql,
236                'arrRawSqlVal' => $arrRawSqlVal)
237        );
238    }   
239}
Note: See TracBrowser for help on using the repository browser.