OSDN Git Service

cd64838e7462e91797eb8908a807a532bca9cca5
[magic3/magic3.git] / widgets / m / pc_content / include / container / admin_m_pc_contentWidgetContainer.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_m_pc_contentWidgetContainer.php 2568 2009-11-18 13:05:26Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/pc_contentDb.php');
18
19 class admin_m_pc_contentWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();         // 表示されているコンテンツシリアル番号
24         private $_menuId = 'mobile_menu';                               // メインメニューID
25         private $isExistsContent;               // コンテンツ項目が存在するかどうか
26         const INC_INDEX = 1;            // メニュー項目表示順の増加分
27         const ADMIN_WIDGET_ID = 'admin_main';           // 管理ウィジェットのウィジェットID
28         const CONTENT_TYPE = 'ct';              // 参照数カウント用
29         
30         /**
31          * コンストラクタ
32          */
33         function __construct()
34         {
35                 // 親クラスを呼び出す
36                 parent::__construct();
37                 
38                 // DBオブジェクト作成
39                 $this->db = new pc_contentDb();
40         }
41         /**
42          * テンプレートファイルを設定
43          *
44          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
45          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
46          *
47          * @param RequestManager $request               HTTPリクエスト処理クラス
48          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
49          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
50          */
51         function _setTemplate($request, &$param)
52         {
53                 $task = $request->trimValueOf('task');
54                 if ($task == 'detail'){         // 詳細画面
55                         return 'admin_main_detail.tmpl.html';
56                 } else {                        // 一覧画面
57                         return 'admin_main.tmpl.html';
58                 }
59         }
60         /**
61          * テンプレートにデータ埋め込む
62          *
63          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
64          *
65          * @param RequestManager $request               HTTPリクエスト処理クラス
66          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
67          * @param                                                               なし
68          */
69         function _assign($request, &$param)
70         {
71                 $task = $request->trimValueOf('task');
72                 if ($task == 'detail'){ // 詳細画面
73                         return $this->createDetail($request);
74                 } else {                        // 一覧画面
75                         return $this->createList($request);
76                 }
77         }
78         /**
79          * コンテンツ一覧画面作成
80          *
81          * @param RequestManager $request               HTTPリクエスト処理クラス
82          * @param                                                               なし
83          */
84         function createList($request)
85         {
86                 // ユーザ情報、表示言語
87                 $userId = $this->gEnv->getCurrentUserId();
88                 $langId = $this->gEnv->getDefaultLanguage();
89                 
90                 $act = $request->trimValueOf('act');
91                 if ($act == 'addtomenu'){                       // メニューに項目を追加
92                         $contentId = $request->trimValueOf('contentid');                // コンテンツID
93                         
94                         /*
95                         // このウィジェットがマップされているページサブIDを取得
96                         $subPageId = $this->gPage->getPageSubIdByWidget($this->gEnv->getDefaultPageId(), $this->gEnv->getCurrentWidgetId());
97                         $sub = '';
98                         if (!empty($subPageId)) $sub = 'sub=' . $subPageId . '&';
99                         
100                         // URLの作成
101                         $url = M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END . '/index.php?' . $sub . 'contentid=' . $contentId;*/
102                         // このウィジェットがマップされているページサブIDを取得
103                         $subPageId = $this->gPage->getPageSubIdByWidget($this->gEnv->getDefaultMobilePageId(), $this->gEnv->getCurrentWidgetId());
104                         $sub = '';
105                         if (!empty($subPageId)) $sub = 'sub=' . $subPageId . '&';
106                         
107                         // URLの作成
108                         $url = M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END . '/m/index.php?' . $sub . 'contentid=' . $contentId;
109
110                         // コンテンツ名を取得
111                         $menutItemName = '';
112                         $ret = $this->db->getContentByContentId($contentId, $langId, $row);
113                         if ($ret){
114                                 // 取得値を設定
115                                 $menutItemName = $row['cn_name'];               // 名前
116                         }
117                         
118                         // メニュー項目追加
119                         $ret = $this->db->addMenuItem($this->_menuId, $langId, $menutItemName, $url, self::INC_INDEX, $userId);
120                         if ($ret){
121                                 $this->setGuidanceMsg('メインメニューに項目を追加しました');
122                         } else {
123                                 $this->setAppErrorMsg('メインメニューの項目追加に失敗しました');
124                         }
125                 } else if ($act == 'delete'){           // 項目削除の場合
126                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
127                         $delItems = array();
128                         for ($i = 0; $i < count($listedItem); $i++){
129                                 // 項目がチェックされているかを取得
130                                 $itemName = 'item' . $i . '_selected';
131                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
132                                 
133                                 if ($itemValue){                // チェック項目
134                                         $delItems[] = $listedItem[$i];
135                                 }
136                         }
137                         if (count($delItems) > 0){
138                                 $ret = $this->db->delContentItem($delItems);
139                                 if ($ret){              // データ削除成功のとき
140                                         $this->setGuidanceMsg('データを削除しました');
141                                 } else {
142                                         $this->setAppErrorMsg('データ削除に失敗しました');
143                                 }
144                         }
145                 }
146                 
147                 // コンテンツ項目リストをデフォルト言語で取得
148                 $this->db->getAllContentItems(array($this, 'itemListLoop'), $langId);
149                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
150                 // コンテンツ項目がないときは、一覧を表示しない
151                 if (!$this->isExistsContent) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');             
152         }
153         /**
154          * コンテンツ詳細画面作成
155          *
156          * @param RequestManager $request               HTTPリクエスト処理クラス
157          * @param                                                               なし
158          */
159         function createDetail($request)
160         {
161                 // ユーザ情報、表示言語
162                 $langId = $this->gEnv->getDefaultLanguage();
163                 
164                 // ウィンドウ表示状態
165                 $openby = $request->trimValueOf(M3_REQUEST_PARAM_OPEN_BY);
166                 
167                 $act = $request->trimValueOf('act');
168                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
169                 $name = $request->trimValueOf('item_name');
170                 $showTitle = ($request->trimValueOf('show_title') == 'on') ? 1 : 0;             // タイトルの表示
171                 $titleBgColor = $request->trimValueOf('item_title_bgcolor');            // タイトルバックグランドカラー
172                 $html = $request->valueOf('item_html');         // HTMLタグを可能とする
173                 $key = $request->valueOf('item_key');           // 外部参照用キー
174                 $visible = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0;             // チェックボックス
175                 $limited = ($request->trimValueOf('item_limited') == 'on') ? 1 : 0;             // チェックボックス
176                 $default = ($request->trimValueOf('item_default') == 'on') ? 1 : 0;             // チェックボックス
177                 
178                 $reloadData = false;            // データの再読み込み
179                 if ($act == 'add'){             // 項目追加の場合
180                         // 入力チェック
181                         $this->checkInput($name, '名前');
182                                         
183                         // エラーなしの場合は、データを登録
184                         if ($this->getMsgCount() == 0){
185                                 $ret = $this->db->addContentItem(0, $langId, $name, ''/*説明*/, $html, $visible, $default, $limited, $key, $newSerial);
186                                 
187                                 // 表示属性はコンテンツIDを定義IDにして、ウィジェットパラメータとして保存
188                                 /*if ($ret){
189                                         $paramObj = new stdClass;
190                                         $paramObj->showTitle    = $showTitle;           // タイトルの表示
191                                         $paramObj->titleBgColor = $titleBgColor;                // タイトルバックグランドカラー
192                                         $ret = $this->updateWidgetParamObjByConfigId($newContentId, $paramObj);
193                                 }*/
194                                 if ($ret){
195                                         $this->setGuidanceMsg('データを追加しました');
196                                         // シリアル番号更新
197                                         $this->serialNo = $newSerial;
198                                         $reloadData = true;             // データの再読み込み
199                                 } else {
200                                         $this->setAppErrorMsg('データ追加に失敗しました');
201                                 }
202                         }
203                 } else if ($act == 'update'){           // 項目更新の場合
204                         // 入力チェック
205                         $this->checkInput($name, '名前');
206                         
207                         // エラーなしの場合は、データを更新
208                         if ($this->getMsgCount() == 0){
209                                 $ret = $this->db->updateContentItem($this->serialNo, $name, $html, $visible, $default, $limited, $key, $newSerial);
210                                 if ($ret){
211                                         $this->setGuidanceMsg('データを更新しました');
212                                         // シリアル番号更新
213                                         $this->serialNo = $newSerial;
214                                         $reloadData = true;             // データの再読み込み
215                                 } else {
216                                         $this->setAppErrorMsg('データ更新に失敗しました');
217                                 }
218                         }                               
219                 } else if ($act == 'delete'){           // 項目削除の場合
220                         if (empty($this->serialNo)){
221                                 $this->setUserErrorMsg('削除項目が選択されていません');
222                         }
223                         // エラーなしの場合は、データを削除
224                         if ($this->getMsgCount() == 0){
225                                 $ret = $this->db->delContentItem(array($this->serialNo));
226                                 if ($ret){              // データ削除成功のとき
227                                         $this->setGuidanceMsg('データを削除しました');
228                                         $reloadData = true;             // データの再読み込み
229                                 } else {
230                                         $this->setAppErrorMsg('データ削除に失敗しました');
231                                 }
232                         }
233                 } else {
234                         // コンテンツIDが設定されているとき(他ウィジェットからの表示)は、データを取得
235                         $contentId = $request->trimValueOf('contentid');                // コンテンツID
236                         if (empty($contentId)){
237                                 if (empty($this->serialNo)){            // 新規項目追加のとき
238                                         $visible = 1;           // 初期状態は表示
239                                 } else {
240                                         $reloadData = true;             // データの再読み込み
241                                 }
242                         } else {
243                                 // コンテンツを取得
244                                 $ret = $this->db->getContentByContentId($contentId, $langId, $row);
245                                 if ($ret){
246                                         $this->serialNo = $row['cn_serial'];            // コンテンツシリアル番号
247                                         $reloadData = true;             // データの再読み込み
248                                 } else {
249                                         $this->serialNo = 0;
250                                 }
251                         }
252                 }
253                 if ($reloadData){               // データの再読み込み
254                         $ret = $this->db->getContentBySerial($this->serialNo, $row);
255                         if ($ret){
256                                 $contentId = $row['cn_id'];             // コンテンツID
257                                 $name = $row['cn_name'];                // コンテンツ名前
258                                 $html = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $row['cn_html']);                             // HTML
259                                 $key = $row['cn_key'];                                  // 外部参照用キー
260                                 $update_user = $this->convertToDispString($row['lu_name']);// 更新者
261                                 $update_dt = $this->convertToDispDateTime($row['cn_create_dt']);
262                         
263                                 // 項目表示、デフォルト値チェックボックス
264                                 $visible = $row['cn_visible'];
265                                 $default = $row['cn_default'];
266                                 $limited = $row['cn_user_limited'];             // ユーザ制限
267                         } else {
268                                 $this->serialNo = 0;
269                         }
270                 }
271                 
272                 // ### 入力値を再設定 ###
273                 $this->tmpl->addVar("_widget", "sel_item_name", $name);         // 名前
274                 $this->tmpl->addVar("_widget", "sel_item_html", $html);         // HTML
275                 $this->tmpl->addVar("_widget", "sel_item_key", $key);           // 外部参照用キー
276                 $this->tmpl->addVar("_widget", "update_user", $update_user);    // 更新者
277                 $this->tmpl->addVar("_widget", "update_dt", $update_dt);        // 更新日時
278         
279                 // 項目表示、項目利用可否チェックボックス
280                 $visibleStr = '';
281                 if ($visible) $visibleStr = 'checked';
282                 $this->tmpl->addVar("_widget", "sel_item_visible", $visibleStr);
283                 $defaultStr = '';
284                 if ($default) $defaultStr = 'checked';
285                 $this->tmpl->addVar("_widget", "sel_item_default", $defaultStr);
286                 $limitedStr = '';
287                 if ($limited) $limitedStr = 'checked';
288                 $this->tmpl->addVar("_widget", "sel_item_limited", $limitedStr);
289         
290                 // 選択中のシリアル番号を設定
291                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
292                         
293                 // パスの設定
294                 $this->tmpl->addVar('_widget', 'admin_url', $this->getUrl($this->gEnv->getDefaultAdminUrl()));// 管理者URL
295                 $this->tmpl->addVar('_widget', 'custom_value_task', 'usercustom');              // ユーザ定義値参照用
296                 $this->tmpl->addVar('_widget', 'admin_widget_id', self::ADMIN_WIDGET_ID);// ユーザ定義値参照用(管理ウィジェットのウィジェットID)
297                 
298                 // ボタンの表示制御
299                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
300                         $this->tmpl->addVar("_widget", "sel_item_id", '新規');                        // コンテンツID
301                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
302                 } else {
303                         $this->tmpl->addVar("_widget", "sel_item_id", $contentId);                      // コンテンツID
304                         $this->tmpl->setAttribute('del_button', 'visibility', 'visible');// 「削除」ボタン
305                 }
306                 // 「戻る」ボタンの表示
307                 if ($openby == 'simple') $this->tmpl->setAttribute('cancel_button', 'visibility', 'hidden');            // 詳細画面のみの表示のときは戻るボタンを隠す
308         }
309         /**
310          * 取得したデータをテンプレートに設定する
311          *
312          * @param int $index                    行番号(0~)
313          * @param array $fetchedRow             フェッチ取得した行
314          * @param object $param                 未使用
315          * @return bool                                 true=処理続行の場合、false=処理終了の場合
316          */
317         function itemListLoop($index, $fetchedRow, $param)
318         {
319                 $serial = $this->convertToDispString($fetchedRow['cn_serial']);
320                 
321                 // 表示状態
322                 $visible = '';
323                 if ($fetchedRow['cn_visible']) $visible = 'checked';
324
325                 // ユーザ制限
326                 $limited = '';
327                 if ($fetchedRow['cn_user_limited']) $limited = 'checked';
328
329                 // デフォルト時の項目かどうか
330                 $default = '';
331                 if ($fetchedRow['cn_default']) $default = 'checked';
332                 
333                 // 対応言語を取得
334                 $lang = '';
335                 $ret = $this->db->getLangByContentId($fetchedRow['cn_id'], $rows);
336                 if ($ret){
337                         $count = count($rows);
338                         for ($i = 0; $i < $count; $i++){
339                                 if ($this->gEnv->getCurrentLanguage() == 'ja'){ // 日本語の場合
340                                         $lang .= $rows[$i]['ln_name'];
341                                         if ($i != $count -1) $lang .= ',';
342                                 } else {
343                                         $lang .= $rows[$i]['ln_name_en'];
344                                         if ($i != $count -1) $lang .= ',';
345                                 }
346                         }
347                 }
348                 // 総参照数
349                 $totalViewCount = $this->gInstance->getAnalyzeManager()->getTotalContentViewCount(self::CONTENT_TYPE, $serial);
350                 
351                 $row = array(
352                         'index' => $index,                                                                                                      // 項目番号
353                         'serial' => $serial,                    // シリアル番号
354                         'id' => $this->convertToDispString($fetchedRow['cn_id']),                       // ID
355                         'name' => $this->convertToDispString($fetchedRow['cn_name']),           // 名前
356                         'lang' => $lang,                                                                                                        // 対応言語
357                         'view_count' => $totalViewCount,                                                                        // 総参照数
358                         'update_user' => $this->convertToDispString($fetchedRow['lu_name']),    // 更新者
359                         'update_dt' => $this->convertToDispDateTime($fetchedRow['cn_create_dt']),       // 更新日時
360                         'visible' => $visible,                                                                                  // メニュー項目表示制御
361                         'limited' => $limited,                                                                                  // ユーザ制限
362                         'default' => $default                                                                                   // デフォルト項目
363                 );
364                 $this->tmpl->addVars('itemlist', $row);
365                 $this->tmpl->parseTemplate('itemlist', 'a');
366                 
367                 // 表示中のコンテンツIDを保存
368                 $this->serialArray[] = $fetchedRow['cn_serial'];
369                 
370                 $this->isExistsContent = true;          // コンテンツ項目が存在するかどうか
371                 return true;
372         }
373 }
374 ?>