OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / youtube2 / include / container / admin_youtube2WidgetContainer.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_youtube2WidgetContainer.php 2266 2009-08-28 08:25:59Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17
18 class admin_youtube2WidgetContainer extends BaseAdminWidgetContainer
19 {
20         private $sysDb; // DB接続オブジェクト
21         private $serialNo;              // 選択中の項目のシリアル番号
22         private $serialArray = array();                 // 表示中のシリアル番号
23         private $langId;
24         private $configId;              // 定義ID
25         private $paramObj;              // パラメータ保存用オブジェクト
26         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
27         const DEFAULT_WIDTH = 130;              // デフォルトの幅
28         const DEFAULT_HEIGHT = 130;             // デフォルトの高さ
29         const DEFAULT_MOVIE_ID = 'J_W0_VrUSBU';         // デフォルトの動画
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // 親クラスを呼び出す
37                 parent::__construct();
38                 
39                 // DBオブジェクト作成
40                 $this->sysDb = $this->gInstance->getSytemDbObject();
41         }
42         /**
43          * テンプレートファイルを設定
44          *
45          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
46          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
47          *
48          * @param RequestManager $request               HTTPリクエスト処理クラス
49          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
50          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
51          */
52         function _setTemplate($request, &$param)
53         {
54                 $task = $request->trimValueOf('task');
55                 if ($task == 'list'){           // 一覧画面
56                         return 'admin_list.tmpl.html';
57                 } else {                        // 一覧画面
58                         return 'admin.tmpl.html';
59                 }
60         }
61         /**
62          * テンプレートにデータ埋め込む
63          *
64          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
65          *
66          * @param RequestManager $request               HTTPリクエスト処理クラス
67          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
68          * @param                                                               なし
69          */
70         function _assign($request, &$param)
71         {
72                 $task = $request->trimValueOf('task');
73                 if ($task == 'list'){           // 一覧画面
74                         return $this->createList($request);
75                 } else {                        // 詳細設定画面
76                         return $this->createDetail($request);
77                 }
78         }
79         /**
80          * 詳細画面作成
81          *
82          * @param RequestManager $request               HTTPリクエスト処理クラス
83          * @param                                                               なし
84          */
85         function createDetail($request)
86         {
87                 // ページ定義IDとページ定義のレコードシリアル番号を取得
88                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
89                 
90                 $userId         = $this->gEnv->getCurrentUserId();
91                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
92                 $act = $request->trimValueOf('act');
93                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
94
95                 // 入力値を取得
96                 $name   = $request->trimValueOf('item_name');                   // ヘッダタイトル
97                 $width  = $request->trimValueOf('item_width');          // ヘッダの幅
98                 $height = $request->trimValueOf('item_height');         // ヘッダの高さ
99                 $movieId = $request->trimValueOf('item_movie_id');              // YouTubeの動画ID
100                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
101                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
102                 
103                 $replaceNew = false;            // データを再取得するかどうか
104                 if (empty($act)){// 初期起動時
105                         // デフォルト値設定
106                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
107                         $replaceNew = true;                     // データ再取得
108                 } else if ($act == 'add'){// 新規追加
109                         // 入力チェック
110                         $this->checkInput($name, '名前');
111                         $this->checkInput($movieId, '動画ID');
112                         $this->checkNumeric($width, '幅');
113                         $this->checkNumeric($height, '高さ');
114                         
115                         // 設定名の重複チェック
116                         for ($i = 0; $i < count($this->paramObj); $i++){
117                                 $targetObj = $this->paramObj[$i]->object;
118                                 if ($name == $targetObj->name){         // 定義名
119                                         $this->setUserErrorMsg('名前が重複しています');
120                                         break;
121                                 }
122                         }
123                         
124                         // エラーなしの場合は、データを登録
125                         if ($this->getMsgCount() == 0){
126                                 // 追加オブジェクト作成
127                                 $newObj = new stdClass;
128                                 $newObj->name   = $name;// 表示名
129                                 $newObj->width  = $width;               // 幅
130                                 $newObj->height = $height;              // 高さ
131                                 $newObj->movieId = $movieId;            // YouTubeの動画ID
132                                 
133                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
134                                 if ($ret){
135                                         $this->setGuidanceMsg('データを追加しました');
136                                         
137                                         $this->configId = $defConfigId;         // 定義定義IDを更新
138                                         $replaceNew = true;                     // データ再取得
139                                 } else {
140                                         $this->setAppErrorMsg('データ追加に失敗しました');
141                                 }
142                         }
143                 } else if ($act == 'update'){           // 設定更新のとき
144                         // 入力値のエラーチェック
145                         $this->checkInput($movieId, '動画ID');
146                         $this->checkNumeric($width, '幅');
147                         $this->checkNumeric($height, '高さ');
148                         
149                         if ($this->getMsgCount() == 0){                 // エラーのないとき
150                                 // 現在の設定値を取得
151                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
152                                 if ($ret){
153                                         // ウィジェットオブジェクト更新
154                                         $targetObj->width       = $width;               // ヘッダの幅
155                                         $targetObj->height      = $height;              // ヘッダの高さ
156                                         $targetObj->movieId     = $movieId;             // YouTubeの動画ID
157                                 }
158                                 
159                                 // 設定値を更新
160                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
161                                 if ($ret){
162                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
163                                         
164                                         $replaceNew = true;                     // データ再取得
165                                 } else {
166                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
167                                 }
168                         }
169                 } else if ($act == 'select'){   // 定義IDを変更
170                         $replaceNew = true;                     // データ再取得
171                 }
172                 
173                 // 設定項目選択メニュー作成
174                 $this->createItemMenu();
175                 
176                 // 表示用データを取得
177                 if (empty($this->configId)){            // 新規登録の場合
178                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
179                         if ($replaceNew){               // データ再取得時
180                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
181                                 $width  = self::DEFAULT_WIDTH;          // 幅
182                                 $height = self::DEFAULT_HEIGHT;         // 高さ
183                                 $movieId = self::DEFAULT_MOVIE_ID;              // YouTubeの動画ID
184                         }
185                         $this->serialNo = 0;
186                 } else {
187                         if ($replaceNew){
188                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
189                                 if ($ret){
190                                         $name = $targetObj->name;// 名前
191                                         $width  = $targetObj->width;            // 幅
192                                         $height = $targetObj->height;           // 高さ
193                                         $movieId = $targetObj->movieId;         // YouTubeの動画ID                           
194                                 }
195                         }
196                         $this->serialNo = $this->configId;
197                                 
198                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
199                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
200                 }
201                 
202                 // 画面にデータを埋め込む
203                 $this->tmpl->addVar("item_name_visible", "name", $name);                // 名前
204                 $this->tmpl->addVar("_widget", "width", $width);
205                 $this->tmpl->addVar("_widget", "height",        $height);
206                 $this->tmpl->addVar("_widget", "movie_id",      $movieId);
207                                 
208                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
209                 
210                 // ボタンの表示制御
211                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
212                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
213                 } else {
214                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
215                 }
216                 // ページ定義IDとページ定義のレコードシリアル番号を更新
217                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
218         }
219         /**
220          * 選択用メニューを作成
221          *
222          * @return なし                                               
223          */
224         function createItemMenu()
225         {
226                 for ($i = 0; $i < count($this->paramObj); $i++){
227                         $id = $this->paramObj[$i]->id;// 定義ID
228                         $targetObj = $this->paramObj[$i]->object;
229                         $name = $targetObj->name;// 定義名
230                         $selected = '';
231
232                         if ($this->configId == $id) $selected = 'selected';
233                         $row = array(
234                                 'name' => $name,                // 名前
235                                 'value' => $id,         // 定義ID
236                                 'selected' => $selected // 選択中の項目かどうか
237                         );
238                         $this->tmpl->addVars('title_list', $row);
239                         $this->tmpl->parseTemplate('title_list', 'a');
240                 }
241         }
242         /**
243          * デフォルトの名前を取得
244          *
245          * @return string       デフォルト名                                              
246          */
247         function createDefaultName()
248         {
249                 $name = self::DEFAULT_NAME_HEAD;
250                 for ($j = 1; $j < 100; $j++){
251                         $name = self::DEFAULT_NAME_HEAD . $j;
252                         // 設定名の重複チェック
253                         for ($i = 0; $i < count($this->paramObj); $i++){
254                                 $targetObj = $this->paramObj[$i]->object;
255                                 if ($name == $targetObj->name){         // 定義名
256                                         break;
257                                 }
258                         }
259                         // 重複なしのときは終了
260                         if ($i == count($this->paramObj)) break;
261                 }
262                 return $name;
263         }
264         /**
265          * 一覧画面作成
266          *
267          * @param RequestManager $request               HTTPリクエスト処理クラス
268          * @param                                                               なし
269          */
270         function createList($request)
271         {
272                 // ページ定義IDとページ定義のレコードシリアル番号を取得
273                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
274                 
275                 $userId         = $this->gEnv->getCurrentUserId();
276                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
277                 $act = $request->trimValueOf('act');
278                 
279                 if ($act == 'delete'){          // メニュー項目の削除
280                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
281                         $delItems = array();
282                         for ($i = 0; $i < count($listedItem); $i++){
283                                 // 項目がチェックされているかを取得
284                                 $itemName = 'item' . $i . '_selected';
285                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
286                                 
287                                 if ($itemValue){                // チェック項目
288                                         $delItems[] = $listedItem[$i];
289                                 }
290                         }
291                         if (count($delItems) > 0){
292                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
293                                 if ($ret){              // データ削除成功のとき
294                                         $this->setGuidanceMsg('データを削除しました');
295                                 } else {
296                                         $this->setAppErrorMsg('データ削除に失敗しました');
297                                 }
298                         }
299                 }
300                 // 定義一覧作成
301                 $this->createItemList();
302                 
303                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
304                 
305                 // ページ定義IDとページ定義のレコードシリアル番号を更新
306                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
307         }
308         /**
309          * 定義一覧作成
310          *
311          * @return なし                                               
312          */
313         function createItemList()
314         {
315                 for ($i = 0; $i < count($this->paramObj); $i++){
316                         $id = $this->paramObj[$i]->id;// 定義ID
317                         $targetObj = $this->paramObj[$i]->object;
318                         $name = $targetObj->name;// 定義名
319                         
320                         $defCount = 0;
321                         if (!empty($id)){
322                                 $defCount = $this->sysDb->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
323                         }
324                         $operationDisagled = '';
325                         if ($defCount > 0) $operationDisagled = 'disabled';
326                         $row = array(
327                                 'index' => $i,
328                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
329                                 'name' => $this->convertToDispString($name),            // 名前
330                                 'movie_id' => $this->convertToDispString($targetObj->movieId),  // 動画ID
331                                 'width' => $targetObj->width,                                   // 動画幅
332                                 'height' => $targetObj->height,                                 // 動画高さ
333                                 'def_count' => $defCount                                                        // 使用数
334                         );
335                         $this->tmpl->addVars('itemlist', $row);
336                         $this->tmpl->parseTemplate('itemlist', 'a');
337                         
338                         // シリアル番号を保存
339                         $this->serialArray[] = $id;
340                 }
341         }
342 }
343 ?>