7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_mainDb.php');
18 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_serverDb.php');
20 class admin_mainTenantserverWidgetContainer extends admin_mainBaseWidgetContainer
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
34 function __construct()
37 parent::__construct();
40 //$this->db = new admin_mainDb();
41 $this->serverDb = new admin_serverDb();
46 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
47 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
49 * @param RequestManager $request HTTPリクエスト処理クラス
50 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
51 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
53 function _setTemplate($request, &$param)
55 $task = $request->trimValueOf('task');
56 if ($task == 'tenantserver_detail'){ // 詳細画面
57 return 'serverlist_detail.tmpl.html';
59 return 'serverlist.tmpl.html';
65 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
67 * @param RequestManager $request HTTPリクエスト処理クラス
68 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
71 function _assign($request, &$param)
73 $task = $request->trimValueOf('task');
74 if ($task == 'tenantserver_detail'){ // 詳細画面
75 return $this->createDetail($request);
77 return $this->createList($request);
83 * @param RequestManager $request HTTPリクエスト処理クラス
86 function createList($request)
88 $act = $request->trimValueOf('act');
90 if ($act == 'delete'){ // メニュー項目の削除
91 $listedItem = explode(',', $request->trimValueOf('seriallist'));
93 for ($i = 0; $i < count($listedItem); $i++){
95 $itemName = 'item' . $i . '_selected';
96 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
98 if ($itemValue){ // チェック項目
99 $delItems[] = $listedItem[$i];
102 if (count($delItems) > 0){
103 $ret = $this->serverDb->delServerBySerial($delItems);
104 if ($ret){ // データ削除成功のとき
105 $this->setGuidanceMsg('データを削除しました');
107 $this->setAppErrorMsg('データ削除に失敗しました');
112 $maxListCount = self::DEFAULT_LIST_COUNT; // 表示項目数
113 $pageNo = $request->trimValueOf('page'); // ページ番号
114 if (empty($pageNo)) $pageNo = 1;
117 $totalCount = $this->serverDb->getAllSeverListCount();
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; // 先頭番号
127 if ($pageCount > 1){ // ページが2ページ以上のときリンクを作成
128 for ($i = 1; $i <= $pageCount; $i++){
130 $link = ' ' . $i;
132 $link = ' <a href="#" onclick="selpage(\'' . $i . '\');return false;">' . $i . '</a>';
137 $this->tmpl->addVar("_widget", "page_link", $pageLink);
138 $this->tmpl->addVar("_widget", "total_count", $totalCount);
139 $this->tmpl->addVar("_widget", "page", $pageNo); // ページ番号
142 $this->serverDb->getAllServerList($maxListCount, $pageNo, array($this, 'serverListLoop'));
143 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
145 // サーバ項目がないときは、一覧を表示しない
146 if (!$this->isExistsServer) $this->tmpl->setAttribute('serverlist', 'visibility', 'hidden');
151 * @param RequestManager $request HTTPリクエスト処理クラス
154 function createDetail($request)
156 $this->lang = $this->gEnv->getDefaultLanguage();
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; // アクセスできるかどうか
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;
181 $reloadData = false; // データの再読み込み
182 if ($act == 'update'){ // 行更新のとき
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);
193 if ($this->serverDb->isExistsServerName($name, $this->serialNo)) $this->setMsg(self::MSG_USER_ERR, 'すでに同名のサーバが存在しています');
196 if ($this->serverDb->isExistsServerId($serverId, $this->serialNo)) $this->setMsg(self::MSG_USER_ERR, 'すでに同じサーバIDのサーバが存在しています');
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; // データの再読み込み
206 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
209 } else if ($act == 'add'){ // 新規追加のとき
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);
220 if ($this->serverDb->isExistsServerName($name)) $this->setMsg(self::MSG_USER_ERR, 'すでに同名のサーバが存在しています');
223 if ($this->serverDb->isExistsServerId($serverId)) $this->setMsg(self::MSG_USER_ERR, 'すでに同じサーバIDのサーバが存在しています');
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; // データの再読み込み
233 $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
236 } else if ($act == 'delete'){ // 削除のとき
237 $ret = $this->serverDb->delServerBySerial(array($this->serialNo));
238 if ($ret){ // データ削除成功のとき
239 $this->setMsg(self::MSG_GUIDANCE, 'データを削除しました');
241 $this->setMsg(self::MSG_APP_ERR, 'データ削除に失敗しました');
243 } else if ($act == 'testdb'){ // DB接続テスト
245 if ($this->serverDb->testDbConnection($dbDsn, $dbAccount, $dbPassword)){
246 $msg = ' DB接続 => <b><font color="green">OK</font></b>';
249 $db = new admin_mainDb();
250 $db->openLocalDb($dbDsn, $dbAccount, $dbPassword);// 接続先を変更
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>';
258 $msg .= '<b><font color="red">サーバIDが異なっています。ID:'. $svrId . '</font></b>';
262 $siteName = $db->getSiteDef($this->lang, M3_TB_FIELD_SITE_TITLE);
263 $msg .= '<br />サイト名取得 => ';
264 if (empty($siteName)){
271 $svrUrl = $db->getSystemConfig(self::CF_SERVER_URL);
272 $msg .= '<br />URL取得 => ';
275 $db->closeLocalDb();// 接続先を戻す
277 $msg .= '<b><font color="red">接続エラー</font></b>';
279 $this->tmpl->addVar('_widget', 'db_test', $msg);
281 $reloadData = true; // データの再読み込み
283 if ($reloadData){ // データの再読み込み
284 if (empty($this->serialNo)){ // 新規項目追加のとき
286 $serverId = ''; // サーバID
289 $dbDsn = ''; // DB接続情報
290 $dbAccount = ''; // DB接続アカウント
291 $dbPassword = '';// DB接続パスワード
292 $canAccess = 1; // アクセス可能かどうか
295 $ret = $this->serverDb->getServerBySerial($this->serialNo, $row);
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']; // アクセス可能かどうか
309 $pos = strpos($dbDsn, ':');
313 $dbType = trim(substr($dbDsn, 0, $pos));
316 $dsnParams = explode(";", substr($dbDsn, $pos + 1));
317 for ($i = 0; $i < count($dsnParams); $i++){
318 list($key, $value) = explode("=", $dsnParams[$i]);
320 $value = trim($value);
323 } else if ($key == 'port'){
325 } else if ($key == 'dbname'){
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");
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);
351 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
353 if (empty($this->serialNo)){ // ユーザIDが空のときは新規とする
354 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
356 $this->tmpl->setAttribute('del_button', 'visibility', 'visible');// 削除、更新ボタン表示
360 * ユーザリスト、取得したデータをテンプレートに設定する
362 * @param int $index 行番号(0~)
363 * @param array $fetchedRow フェッチ取得した行
364 * @param object $param 未使用
365 * @return bool true=処理続行の場合、false=処理終了の場合
367 function serverListLoop($index, $fetchedRow, $param)
370 if ($fetchedRow['ts_enable_access']){
371 $canAccess = 'checked';
373 if (empty($fetchedRow['ts_url'])){
376 $linkUrlStr = '<a href="#" onclick="showUrl(\'' . $fetchedRow['ts_url'] . '\');">' . $this->convertToDispString($fetchedRow['ts_url']) . '</a>';
379 $opelogDisabled = '';
380 if (empty($fetchedRow['ts_db_connect_dsn'])) $opelogDisabled = 'disabled';
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 // 運用ログ参照ボタン
394 $this->tmpl->addVars('serverlist', $row);
395 $this->tmpl->parseTemplate('serverlist', 'a');
398 $this->serialArray[] = $fetchedRow['ts_serial'];
400 $this->isExistsServer = true; // 一覧にサーバが存在するかどうか