OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / admin_main / include / container / admin_mainTenantserverWidgetContainer.php
1 <?php
2 /**
3  * コンテナクラス
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-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_mainTenantserverWidgetContainer.php 2734 2009-12-25 05:51:56Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() .    '/admin_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_mainDb.php');
18 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_serverDb.php');
19
20 class admin_mainTenantserverWidgetContainer extends admin_mainBaseWidgetContainer
21 {
22         //private $db;  // DB接続オブジェクト
23         private $serverDb;              // DB接続オブジェクト
24         private $serialNo;      // シリアルNo
25         private $serialArray = array();         // 表示されているコンテンツシリアル番号
26         private $isExistsServer;                // 一覧にサーバが存在するかどうか
27         const DEFAULT_LIST_COUNT = 30;                  // 最大リスト表示数
28         const CF_SERVER_ID = 'server_id';               // サーバID
29         const CF_SERVER_URL = 'server_url';             // サーバURL
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // 親クラスを呼び出す
37                 parent::__construct();
38                 
39                 // DB接続オブジェクト作成
40                 //$this->db = new admin_mainDb();
41                 $this->serverDb = new admin_serverDb();
42         }
43         /**
44          * テンプレートファイルを設定
45          *
46          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
47          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
48          *
49          * @param RequestManager $request               HTTPリクエスト処理クラス
50          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
51          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
52          */
53         function _setTemplate($request, &$param)
54         {
55                 $task = $request->trimValueOf('task');
56                 if ($task == 'tenantserver_detail'){            // 詳細画面
57                         return 'serverlist_detail.tmpl.html';
58                 } else {                        // 一覧画面
59                         return 'serverlist.tmpl.html';
60                 }
61         }
62         /**
63          * テンプレートにデータ埋め込む
64          *
65          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
66          *
67          * @param RequestManager $request               HTTPリクエスト処理クラス
68          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
69          * @param                                                               なし
70          */
71         function _assign($request, &$param)
72         {
73                 $task = $request->trimValueOf('task');
74                 if ($task == 'tenantserver_detail'){    // 詳細画面
75                         return $this->createDetail($request);
76                 } else {                        // 一覧画面
77                         return $this->createList($request);
78                 }
79         }
80         /**
81          * 一覧画面作成
82          *
83          * @param RequestManager $request               HTTPリクエスト処理クラス
84          * @param                                                               なし
85          */
86         function createList($request)
87         {
88                 $act = $request->trimValueOf('act');
89                 
90                 if ($act == 'delete'){          // メニュー項目の削除
91                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
92                         $delItems = array();
93                         for ($i = 0; $i < count($listedItem); $i++){
94                                 // 項目がチェックされているかを取得
95                                 $itemName = 'item' . $i . '_selected';
96                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
97                                 
98                                 if ($itemValue){                // チェック項目
99                                         $delItems[] = $listedItem[$i];
100                                 }
101                         }
102                         if (count($delItems) > 0){
103                                 $ret = $this->serverDb->delServerBySerial($delItems);
104                                 if ($ret){              // データ削除成功のとき
105                                         $this->setGuidanceMsg('データを削除しました');
106                                 } else {
107                                         $this->setAppErrorMsg('データ削除に失敗しました');
108                                 }
109                         }
110                 }
111                 
112                 $maxListCount = self::DEFAULT_LIST_COUNT;                               // 表示項目数
113                 $pageNo = $request->trimValueOf('page');                                // ページ番号
114                 if (empty($pageNo)) $pageNo = 1;
115                 
116                 // 総数を取得
117                 $totalCount = $this->serverDb->getAllSeverListCount();
118
119                 // 表示するページ番号の修正
120                 $pageCount = (int)(($totalCount -1) / $maxListCount) + 1;               // 総ページ数
121                 if ($pageNo < 1) $pageNo = 1;
122                 if ($pageNo > $pageCount) $pageNo = $pageCount;
123                 $this->firstNo = ($pageNo -1) * $maxListCount + 1;              // 先頭番号
124                 
125                 // ページング用リンク作成
126                 $pageLink = '';
127                 if ($pageCount > 1){    // ページが2ページ以上のときリンクを作成
128                         for ($i = 1; $i <= $pageCount; $i++){
129                                 if ($i == $pageNo){
130                                         $link = '&nbsp;' . $i;
131                                 } else {
132                                         $link = '&nbsp;<a href="#" onclick="selpage(\'' . $i . '\');return false;">' . $i . '</a>';
133                                 }
134                                 $pageLink .= $link;
135                         }
136                 }
137                 $this->tmpl->addVar("_widget", "page_link", $pageLink);
138                 $this->tmpl->addVar("_widget", "total_count", $totalCount);
139                 $this->tmpl->addVar("_widget", "page", $pageNo);        // ページ番号
140                 
141                 // サーバリストを取得
142                 $this->serverDb->getAllServerList($maxListCount, $pageNo, array($this, 'serverListLoop'));
143                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
144                 
145                 // サーバ項目がないときは、一覧を表示しない
146                 if (!$this->isExistsServer) $this->tmpl->setAttribute('serverlist', 'visibility', 'hidden');
147         }
148         /**
149          * 詳細画面作成
150          *
151          * @param RequestManager $request               HTTPリクエスト処理クラス
152          * @param                                                               なし
153          */
154         function createDetail($request)
155         {
156                 $this->lang             = $this->gEnv->getDefaultLanguage();
157                 
158                 $act = $request->trimValueOf('act');
159                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
160                 $name = $request->trimValueOf('item_name');                             // サーバ名
161                 $serverId = $request->trimValueOf('item_server_id');            // サーバID
162                 $ip = $request->trimValueOf('item_ip');         // サーバIP
163                 $url = rtrim($request->trimValueOf('item_url'), '/');           // サーバURL
164                 $dbType = $request->trimValueOf('item_db_type');                // DB種別
165                 $dbHost = $request->trimValueOf('item_db_host');                // DBホスト名
166                 $dbPort = $request->trimValueOf('item_db_port');                // DBポート番号
167                 $dbName = $request->trimValueOf('item_db_name');                // DB名
168                 $dbAccount = $request->trimValueOf('item_db_account');  // DB接続アカウント
169                 $dbPassword = $request->trimValueOf('item_db_password');// DB接続パスワード
170                 $canAccess = ($request->trimValueOf('item_can_access') == 'on') ? 1 : 0;                // アクセスできるかどうか
171
172                 // DSN作成
173                 $dbDsn = '';
174                 if (!empty($dbType) && !empty($dbHost)){
175                         $dbDsn .= $dbType . ':';
176                         if (!empty($dbHost)) $dbDsn .= 'host=' . $dbHost;
177                         if (!empty($dbPort)) $dbDsn .= ';port=' . $dbPort;
178                         $dbDsn .= ';dbname=' . $dbName;
179                 }
180                 
181                 $reloadData = false;            // データの再読み込み
182                 if ($act == 'update'){          // 行更新のとき
183                         // 入力チェック
184                         $this->checkInput($name, '名前');
185                         $this->checkSingleByte($serverId, 'サーバID');
186                         $this->checkIp($ip, 'IPアドレス');
187                         $this->checkUrl($url, 'URL');
188                         if ($dbHost != 'localhost') $this->checkIp($dbHost, 'ホストIP', true);
189                         $this->checkSingleByte($dbName, 'DB名', true);
190                         $this->checkSingleByte($dbAccount, 'DB接続アカウント', true);
191                         
192                         // 名前重複チェック
193                         if ($this->serverDb->isExistsServerName($name, $this->serialNo)) $this->setMsg(self::MSG_USER_ERR, 'すでに同名のサーバが存在しています');
194                         
195                         // サーバID重複チェック
196                         if ($this->serverDb->isExistsServerId($serverId, $this->serialNo)) $this->setMsg(self::MSG_USER_ERR, 'すでに同じサーバIDのサーバが存在しています');
197                         
198                         // エラーなしの場合は、データを更新
199                         if ($this->getMsgCount() == 0){
200                                 $ret = $this->serverDb->updateServer($this->serialNo, $name, $serverId, $ip, $url, $dbDsn, $dbAccount, $dbPassword, $canAccess, $newSerial);
201                                 if ($ret){              // データ追加成功のとき
202                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
203                                         $this->serialNo = $newSerial;
204                                         $reloadData = true;             // データの再読み込み
205                                 } else {
206                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
207                                 }
208                         }
209                 } else if ($act == 'add'){              // 新規追加のとき
210                         // 入力チェック
211                         $this->checkInput($name, '名前');
212                         $this->checkSingleByte($serverId, 'サーバID');
213                         $this->checkIp($ip, 'IPアドレス');
214                         $this->checkUrl($url, 'URL');
215                         if ($dbHost != 'localhost') $this->checkIp($dbHost, 'ホストIP', true);
216                         $this->checkSingleByte($dbName, 'DB名', true);
217                         $this->checkSingleByte($dbAccount, 'DB接続アカウント', true);
218                         
219                         // 名前重複チェック
220                         if ($this->serverDb->isExistsServerName($name)) $this->setMsg(self::MSG_USER_ERR, 'すでに同名のサーバが存在しています');
221
222                         // サーバID重複チェック
223                         if ($this->serverDb->isExistsServerId($serverId)) $this->setMsg(self::MSG_USER_ERR, 'すでに同じサーバIDのサーバが存在しています');
224                         
225                         // エラーなしの場合は、データを登録
226                         if ($this->getMsgCount() == 0){
227                                 $ret = $this->serverDb->updateServer(0/*新規*/, $name, $serverId, $ip, $url, $dbDsn, $dbAccount, $dbPassword, $canAccess, $newSerial);
228                                 if ($ret){              // データ追加成功のとき
229                                         $this->setMsg(self::MSG_GUIDANCE, 'データを追加しました');
230                                         $this->serialNo = $newSerial;
231                                         $reloadData = true;             // データの再読み込み
232                                 } else {
233                                         $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
234                                 }
235                         }
236                 } else if ($act == 'delete'){           // 削除のとき
237                         $ret = $this->serverDb->delServerBySerial(array($this->serialNo));
238                         if ($ret){              // データ削除成功のとき
239                                 $this->setMsg(self::MSG_GUIDANCE, 'データを削除しました');
240                         } else {
241                                 $this->setMsg(self::MSG_APP_ERR, 'データ削除に失敗しました');
242                         }
243                 } else if ($act == 'testdb'){           // DB接続テスト
244                         // 接続可能かチェック
245                         if ($this->serverDb->testDbConnection($dbDsn, $dbAccount, $dbPassword)){
246                                 $msg = ' DB接続 => <b><font color="green">OK</font></b>';
247                                                                 
248                                 // テスト用DBオブジェクト作成
249                                 $db = new admin_mainDb();
250                                 $db->openLocalDb($dbDsn, $dbAccount, $dbPassword);// 接続先を変更
251                                 
252                                 // サーバID取得
253                                 $svrId = $db->getSystemConfig(self::CF_SERVER_ID);
254                                 $msg .= '<br />サーバID取得 => ';
255                                 if ($svrId == $serverId){
256                                         $msg .= '<b><font color="green">OK</font></b>';
257                                 } else {
258                                         $msg .= '<b><font color="red">サーバIDが異なっています。ID:'. $svrId . '</font></b>';
259                                 }
260                                 
261                                 // サイト名取得
262                                 $siteName = $db->getSiteDef($this->lang, M3_TB_FIELD_SITE_TITLE);
263                                 $msg .= '<br />サイト名取得 => ';
264                                 if (empty($siteName)){
265                                         $msg .= 'サイト名未設定';
266                                 } else {
267                                         $msg .= $siteName;
268                                 }
269                                 
270                                 // サーバURL取得
271                                 $svrUrl = $db->getSystemConfig(self::CF_SERVER_URL);
272                                 $msg .= '<br />URL取得 => ';
273                                 $msg .= $svrUrl;
274                                 
275                                 $db->closeLocalDb();// 接続先を戻す
276                         } else {
277                                 $msg .= '<b><font color="red">接続エラー</font></b>';
278                         }
279                         $this->tmpl->addVar('_widget', 'db_test',       $msg);
280                 } else {
281                         $reloadData = true;             // データの再読み込み
282                 }
283                 if ($reloadData){               // データの再読み込み
284                         if (empty($this->serialNo)){            // 新規項目追加のとき
285                                 $name = '';             // サーバ名
286                                 $serverId = '';         // サーバID
287                                 $ip = '';               // サーバIP
288                                 $url = '';              // サーバURL
289                                 $dbDsn = '';            // DB接続情報
290                                 $dbAccount = '';        // DB接続アカウント
291                                 $dbPassword = '';// DB接続パスワード
292                                 $canAccess = 1; // アクセス可能かどうか
293                         } else {
294                                 // 設定データを取得
295                                 $ret = $this->serverDb->getServerBySerial($this->serialNo, $row);
296                                 if ($ret){
297                                         $name = $row['ts_name'];                // サーバ名
298                                         $serverId = $row['ts_server_id'];               // サーバID
299                                         $ip = $row['ts_ip'];            // サーバIP
300                                         $url = $row['ts_url'];          // サーバURL
301                                         $dbDsn = $row['ts_db_connect_dsn'];             // DB接続情報
302                                         $dbAccount = $row['ts_db_account'];     // DB接続アカウント
303                                         $dbPassword = $row['ts_db_password'];// DB接続パスワード
304                                         $canAccess = $row['ts_enable_access'];  // アクセス可能かどうか
305                                 }
306                         }
307                 }
308                 // DB種別を取得
309                 $pos = strpos($dbDsn, ':');
310                 if ($pos === false){
311                         $pos = -1;
312                 } else {
313                         $dbType = trim(substr($dbDsn, 0, $pos));
314                 }
315                 // ホスト名、DB名を取得
316                 $dsnParams = explode(";", substr($dbDsn, $pos + 1));
317                 for ($i = 0; $i < count($dsnParams); $i++){
318                         list($key, $value) = explode("=", $dsnParams[$i]);
319                         $key = trim($key);
320                         $value = trim($value);
321                         if ($key == 'host'){
322                                 $dbHost = $value;
323                         } else if ($key == 'port'){
324                                 $dbPort = $value;
325                         } else if ($key == 'dbname'){
326                                 $dbName = $value;
327                         }
328                 }
329                                 
330                 // 取得データを設定
331                 $this->tmpl->addVar("_widget", "name", $this->convertToDispString($name));// サーバ名
332                 $this->tmpl->addVar("_widget", "SERVER_ID", $this->convertToDispString($serverId));// サーバID
333                 $this->tmpl->addVar("_widget", "IP", $this->convertToDispString($ip));// サーバIP
334                 $this->tmpl->addVar("_widget", "URL", $this->convertToDispString($url));// サーバURL
335                 if ($dbType == 'mysql'){
336                         $this->tmpl->addVar("_widget", "MYSQL_CHECKED", "checked");
337                 } else if ($dbType == 'pgsql'){
338                         $this->tmpl->addVar("_widget", "PGSQL_CHECKED", "checked");
339                 }
340                 $this->tmpl->addVar("_widget", "DB_HOST", $this->convertToDispString($dbHost));// DBホスト名
341                 $this->tmpl->addVar("_widget", "DB_PORT", $this->convertToDispString($dbPort));// DBポート番号
342                 $this->tmpl->addVar("_widget", "DB_NAME", $this->convertToDispString($dbName));// DB名
343                 $this->tmpl->addVar("_widget", "DB_DSN", $this->convertToDispString($dbDsn));// DB接続情報
344                 $this->tmpl->addVar("_widget", "DB_ACCOUNT", $dbAccount);// DB接続アカウント
345                 $this->tmpl->addVar("_widget", "DB_PASSWORD", $dbPassword);// DB接続パスワード
346                 $canAccessCheck = '';
347                 if ($canAccess) $canAccessCheck = 'checked';
348                 $this->tmpl->addVar("_widget", "can_access", $canAccessCheck);
349                 
350                 // 共通データを設定
351                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
352                                 
353                 if (empty($this->serialNo)){            // ユーザIDが空のときは新規とする
354                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
355                 } else {
356                         $this->tmpl->setAttribute('del_button', 'visibility', 'visible');// 削除、更新ボタン表示
357                 }
358         }
359         /**
360          * ユーザリスト、取得したデータをテンプレートに設定する
361          *
362          * @param int $index                    行番号(0~)
363          * @param array $fetchedRow             フェッチ取得した行
364          * @param object $param                 未使用
365          * @return bool                                 true=処理続行の場合、false=処理終了の場合
366          */
367         function serverListLoop($index, $fetchedRow, $param)
368         {
369                 $canAccess = '';
370                 if ($fetchedRow['ts_enable_access']){
371                         $canAccess = 'checked';
372                 }
373                 if (empty($fetchedRow['ts_url'])){
374                         $linkUrlStr = '';
375                 } else {
376                         $linkUrlStr = '<a href="#" onclick="showUrl(\'' . $fetchedRow['ts_url'] . '\');">' . $this->convertToDispString($fetchedRow['ts_url']) . '</a>';
377                 }
378                 // 運用ログ参照ボタンの制御
379                 $opelogDisabled = '';
380                 if (empty($fetchedRow['ts_db_connect_dsn'])) $opelogDisabled = 'disabled';
381                 
382                 $row = array(
383                         'index' => $index,                                                                                                      // 行番号
384                         'serial' => $this->convertToDispString($fetchedRow['ts_serial']),                       // シリアル番号
385                         'id' => $this->convertToDispString($fetchedRow['ts_id']),                       // ID
386                         'name' => $this->convertToDispString($fetchedRow['ts_name']),           // 名前
387                         'url' => $linkUrlStr,           // URL
388                         'ip' => $this->convertToDispString($fetchedRow['ts_ip']),               // IP
389                         'update_dt' => $this->convertToDispDateTime($fetchedRow['ts_create_dt']),       // 更新日時
390                         'can_access' => $canAccess,                                                                                             // アクセス可能かどうか
391                         'selected' => $selected,                                                                                                // 項目選択用ラジオボタン
392                         'opelog_disabled' => $opelogDisabled                                    // 運用ログ参照ボタン
393                 );
394                 $this->tmpl->addVars('serverlist', $row);
395                 $this->tmpl->parseTemplate('serverlist', 'a');
396                 
397                 // 表示中のコンテンツIDを保存
398                 $this->serialArray[] = $fetchedRow['ts_serial'];
399                 
400                 $this->isExistsServer = true;           // 一覧にサーバが存在するかどうか
401                 return true;
402         }
403 }
404 ?>