OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / user_content / include / container / admin_user_contentRoomWidgetContainer.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    ユーザ作成コンテンツ
10  * @author     株式会社 毎日メディアサービス
11  * @copyright  Copyright 2010 株式会社 毎日メディアサービス.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_user_contentRoomWidgetContainer.php 3026 2010-04-13 05:14:43Z fishbone $
14  * @link       http://www.m-media.co.jp
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_user_contentBaseWidgetContainer.php');
17
18 class admin_user_contentRoomWidgetContainer extends admin_user_contentBaseWidgetContainer
19 {
20         private $langId;                // 言語ID
21         private $serialNo;              // 選択中の項目のシリアル番号
22         private $serialArray = array();         // 表示されている項目シリアル番号
23         private $categoryArray = array();       // 表示されている所属カテゴリのID
24         private $categoryValues = array();      // 選択されているカテゴリの項目値
25         const CONTENT_TYPE = 'uc';              // 参照数カウント用
26         
27         /**
28          * コンストラクタ
29          */
30         function __construct()
31         {
32                 // 親クラスを呼び出す
33                 parent::__construct();
34         }
35         /**
36          * テンプレートファイルを設定
37          *
38          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
39          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
40          *
41          * @param RequestManager $request               HTTPリクエスト処理クラス
42          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
43          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
44          */
45         function _setTemplate($request, &$param)
46         {
47                 $task = $request->trimValueOf('task');
48                 if ($task == 'room_detail'){            // 詳細画面
49                         return 'admin_room_detail.tmpl.html';
50                 } else {                        // 一覧画面
51                         return 'admin_room.tmpl.html';
52                 }
53         }
54         /**
55          * テンプレートにデータ埋め込む
56          *
57          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
58          *
59          * @param RequestManager $request               HTTPリクエスト処理クラス
60          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
61          * @param                                                               なし
62          */
63         function _assign($request, &$param)
64         {
65                 $task = $request->trimValueOf('task');
66                 if ($task == 'room_detail'){    // 詳細画面
67                         return $this->createDetail($request);
68                 } else {                        // 一覧画面
69                         return $this->createList($request);
70                 }
71         }
72         /**
73          * 一覧画面作成
74          *
75          * @param RequestManager $request               HTTPリクエスト処理クラス
76          * @param                                                               なし
77          */
78         function createList($request)
79         {
80                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
81                 $act = $request->trimValueOf('act');
82                 
83                 if ($act == 'delete'){          // 項目削除の場合
84                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
85                         $delItems = array();
86                         for ($i = 0; $i < count($listedItem); $i++){
87                                 // 項目がチェックされているかを取得
88                                 $itemName = 'item' . $i . '_selected';
89                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
90                                 
91                                 if ($itemValue){                // チェック項目
92                                         $delItems[] = $listedItem[$i];
93                                 }
94                         }
95                         if (count($delItems) > 0){
96                                 // ルームIDを取得
97                                 $roomArray = array();
98                                 for ($i = 0; $i < count($delItems); $i++){
99                                         // シリアル番号からデータを取得
100                                         $ret = $this->_localDb->getRoomBySerial($delItems[$i], $row);
101                                         if ($ret) $roomArray[] = $row['ur_id'];                 // ルーム識別ID
102                                 }
103                                 
104                                 // ルームを削除
105                                 $ret = $this->_localDb->delRoom($delItems);
106                                 
107                                 // 削除するルームに対応したカテゴリを削除
108                                 if ($ret){
109                                         for ($i = 0; $i < count($roomArray); $i++){
110                                                 $ret = $this->_localDb->delRoomCategory($roomArray[$i]);
111                                         }
112                                 }
113                                 
114                                 if ($ret){              // データ削除成功のとき
115                                         $this->setGuidanceMsg('データを削除しました');
116                                 } else {
117                                         $this->setAppErrorMsg('データ削除に失敗しました');
118                                 }
119                         }
120                 }
121                 
122                 // 一覧作成
123                 $this->_localDb->getAllRooms(array($this, 'itemLoop'));
124                 
125                 if (count($this->serialArray) > 0){
126                         $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
127                 } else {
128                         // 項目がないときは、一覧を表示しない
129                         $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
130                 }
131         }
132         /**
133          * 詳細画面作成
134          *
135          * @param RequestManager $request               HTTPリクエスト処理クラス
136          * @param                                                               なし
137          */
138         function createDetail($request)
139         {
140                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
141                 
142                 $act = $request->trimValueOf('act');
143                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
144                 $name   = $request->trimValueOf('item_name');   // 名前
145                 $id             = $request->trimValueOf('item_id');     // ルーム識別ID
146                 $groupId        = $request->trimValueOf('item_group_id');       // 所属グループID
147                 $visible = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0;             // 表示するかどうか
148                 $categoryList = $request->trimValueOf('category_list'); // カテゴリIDのリスト
149                 if (!empty($categoryList)) $this->categoryArray = explode(',', $categoryList);
150                 
151                 // カテゴリーの選択項目値を取得
152                 for ($i = 0; $i < count($this->categoryArray); $i++){
153                         $itemName = 'item_category' . $i;
154                         $itemValue = $request->trimValueOf($itemName);
155                         $categoryId = $this->categoryArray[$i];
156                         
157                         // 空以外の値を取得
158                         if (!empty($itemValue)) $this->categoryValues[$categoryId] = $itemValue;
159                 }
160                 
161                 $replaceNew = false;            // データを再取得するかどうか
162                 if ($act == 'add'){             // 新規追加のとき
163                         // 入力チェック
164                         //$this->checkInput($name, '表示名');
165                         $this->checkSingleByte($id, '識別ID');
166                         $this->checkNumeric($groupId, '所属グループID');          // 所属グループID
167                         
168                         // 同じIDがある場合はエラー
169                         if ($this->_localDb->getRoomById($id, $row)) $this->setMsg(self::MSG_USER_ERR, '識別IDが重複しています');
170                         
171                         // エラーなしの場合は、データを更新
172                         if ($this->getMsgCount() == 0){
173                                 $ret = $this->_localDb->updateRoom(0/*新規*/, $id, $name, $groupId, $visible, $newSerial);
174                                 
175                                 // 選択カテゴリを更新
176                                 if ($ret) $ret = $this->_localDb->updateRoomCategory($id, $this->categoryValues);
177                                 
178                                 if ($ret){              // データ追加成功のとき
179                                         $this->setMsg(self::MSG_GUIDANCE, 'データを追加しました');
180                                         
181                                         $this->serialNo = $newSerial;           // シリアル番号を更新
182                                         $replaceNew = true;                     // データを再取得
183                                 } else {
184                                         $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
185                                 }
186                         }
187                 } else if ($act == 'update'){           // 行更新のとき
188                         // 入力チェック
189                         //$this->checkInput($name, '表示名');
190                         $this->checkNumeric($groupId, '所属グループID');          // 所属グループID
191                         
192                         // エラーなしの場合は、データを更新
193                         if ($this->getMsgCount() == 0){
194                                 $ret = $this->_localDb->updateRoom($this->serialNo, $id, $name, $groupId, $visible, $newSerial);
195                                 
196                                 // 選択カテゴリを更新
197                                 if ($ret) $ret = $this->_localDb->updateRoomCategory($id, $this->categoryValues);
198                                 
199                                 if ($ret){              // データ追加成功のとき
200                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
201                                 
202                                         $this->serialNo = $newSerial;           // シリアル番号を更新
203                                         $replaceNew = true;                     // データを再取得
204                                 } else {
205                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
206                                 }
207                         }
208                 } else {                // 初期状態
209                         // シリアル番号からデータを取得
210                         $ret = $this->_localDb->getRoomBySerial($this->serialNo, $row);
211                         if ($ret) $id = $row['ur_id'];                  // ルーム識別ID
212                         
213                         if (empty($id)){                // タブ識別IDが空のときは新規とする
214                                 $this->serialNo = 0;
215                                 $id             = '';           // 識別ID
216                                 $name   = '';   // 名前
217                                 $groupId = 0;   // 所属グループID
218                                 $visible        = 1;            // 公開
219                         } else {
220                                 $replaceNew = true;                     // データを再取得
221                         }
222                 }
223                 // 表示データ再取得
224                 if ($replaceNew){
225                         // タブ識別IDからデータを取得
226                         $ret = $this->_localDb->getRoomById($id, $row);
227                         if ($ret){
228                                 $this->serialNo = $row['ur_serial'];
229                                 $name           = $row['ur_name'];
230                                 $groupId        = $row['ur_group_id'];          // 所属グループID
231                                 $visible        = $row['ur_visible'];           // 公開
232                                 
233                                 // 選択カテゴリを取得
234                                 $ret = $this->_localDb->getRoomCategory($id, $rows);
235                                 if ($ret){
236                                         $this->categoryValues = array();
237                                         for ($i = 0; $i < count($rows); $i++){
238                                                 $key = $rows[$i][um_category_id];
239                                                 $value = $rows[$i][um_category_item_id];
240                                                 $this->categoryValues[$key] = $value;
241                                         }
242                                 }
243                         }
244                 }
245                 
246                 // カテゴリメニュー作成
247                 $this->categoryArray = array();         // 表示カテゴリを一旦初期化
248                 $this->_localDb->getAllCategoryForMenu($this->langId, array($this, 'menuLoop'));
249                 
250                 // 項目がないときは、カテゴリメニューを表示しない
251                 if (empty($this->categoryArray)){
252                         $this->tmpl->setAttribute('category', 'visibility', 'hidden');
253                 } else {        // カテゴリが設定されているとき
254                         $this->tmpl->addVar("_widget", "category_list", implode($this->categoryArray, ','));            // 表示中のカテゴリIDのリスト
255                 }
256                 
257                 if (empty($this->serialNo)){            // シリアル番号が空のときは新規とする
258                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
259                         $this->tmpl->setAttribute('new_id_field', 'visibility', 'visible');// 新規ID入力フィールド表示
260                         
261                         $this->tmpl->addVar("new_id_field", "id", $id);         // 識別キー
262                 } else {
263                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新ボタン表示
264                         $this->tmpl->setAttribute('id_field', 'visibility', 'visible');// 固定IDフィールド表示
265                         
266                         $this->tmpl->addVar("id_field", "id", $id);             // 識別キー
267                 }
268                 
269                 // 画面にデータを埋め込む
270                 $this->tmpl->addVar("_widget", "name", $name);          // 名前
271                 $this->tmpl->addVar("_widget", "group_id", $groupId);           // 所属グループID
272                 
273                 // 項目表示、項目利用可否チェックボックス
274                 $checked = '';
275                 if ($visible) $checked = 'checked';
276                 $this->tmpl->addVar("_widget", "visible", $checked);
277                 
278                 // 選択中のシリアル番号を設定
279                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
280         }
281         /**
282          * 取得したルーム情報をテンプレートに設定する
283          *
284          * @param int $index                    行番号(0~)
285          * @param array $fetchedRow             フェッチ取得した行
286          * @param object $param                 未使用
287          * @return bool                                 true=処理続行の場合、false=処理終了の場合
288          */
289         function itemLoop($index, $fetchedRow, $param)
290         {
291                 $id = $fetchedRow['ur_id'];             // ルームID
292                 $visible = '';
293                 if ($fetchedRow['ur_visible']){ // 項目の表示
294                         $visible = 'checked';
295                 }
296                 
297                 // 総参照数
298                 $contentId = $id;               // コンテンツID
299                 $totalViewCount = $this->gInstance->getAnalyzeManager()->getTotalContentViewCount(self::CONTENT_TYPE, 0/*コンテンツID指定*/, $contentId);
300                 
301                 $row = array(
302                         'index' => $index,
303                         'serial' => $fetchedRow['ur_serial'],
304                         'id' => $this->convertToDispString($id),                // 識別ID
305                         'name'     => $this->convertToDispString($fetchedRow['ur_name']),                       // 表示名
306                         'group'     => $this->convertToDispString($fetchedRow['ur_group_id']),                  // 所属グループID
307                         'visible'       => $visible,                                    // 公開状況
308                         'view_count' => $totalViewCount                                                                 // 総参照数
309                 );
310                 $this->tmpl->addVars('itemlist', $row);
311                 $this->tmpl->parseTemplate('itemlist', 'a');
312                 
313                 // 表示中項目のシリアル番号を保存
314                 $this->serialArray[] = $fetchedRow['ur_serial'];
315                 return true;
316         }
317         /**
318          * 取得したタブ定義をテンプレートに設定する
319          *
320          * @param int $index                    行番号(0~)
321          * @param array $fetchedRow             フェッチ取得した行
322          * @param object $param                 未使用
323          * @return bool                                 true=処理続行の場合、false=処理終了の場合
324          */
325         function menuLoop($index, $fetchedRow, $param)
326         {
327                 static $categoryIndex = 0;      // カテゴリ表示順
328                 static $isFirst = true;         // 最初の項目かどうか
329                 
330                 $id = $fetchedRow['ua_id'];
331                 $itemId = $fetchedRow['ua_item_id'];
332                 if (empty($itemId)){            // カテゴリタイトルのとき
333                         $itemRow = array(               
334                                 'index'         => $categoryIndex,                      // 項目番号
335                                 'title'         => $this->convertToDispString($fetchedRow['ua_name'])                   // 表示名                                                                            
336                         );
337                         $this->tmpl->addVars('category', $itemRow);
338                         $this->tmpl->parseTemplate('category', 'a');
339                         
340                         $categoryIndex++;               // カテゴリ表示順を更新
341                         $isFirst = true;                // 最初の項目かどうか
342                         $this->categoryArray[] = $fetchedRow['ua_id'];// カテゴリIDを保存
343                 } else {
344                         if ($isFirst){          // 最初の項目のとき
345                                 $this->tmpl->clearTemplate('category_list');
346                                 $isFirst = false;
347                         }
348                         // カテゴリの選択状況を取得
349                         $selected = '';
350                         if ($this->categoryValues[$id] == $itemId) $selected = 'selected';
351                         
352                         $menurow = array(
353                                 'value'         => $this->convertToDispString($itemId),                 // カテゴリー項目ID
354                                 'name'          => $this->convertToDispString($fetchedRow['ua_name']),                  // カテゴリー項目名
355                                 'selected'      => $selected                                                                                                    // 選択中かどうか
356                         );
357                         $this->tmpl->addVars('category_list', $menurow);
358                         $this->tmpl->parseTemplate('category_list', 'a');
359                 }
360                 return true;
361         }
362 }
363 ?>