OSDN Git Service

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