OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / static_content / include / container / admin_static_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-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_static_contentWidgetContainer.php 5489 2012-12-28 13:00:45Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/static_contentDb.php');
18
19 class admin_static_contentWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();                 // 表示中のシリアル番号
24         private $langId;
25         private $paramObj;              // パラメータ保存用オブジェクト
26         private $contentId;             // コンテンツID
27         private $menuHtml;      // コンテンツメニュー
28         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
29         const CONTENT_WIDGET_ID = 'default_content';                    // コンテンツ編集ウィジェット
30         
31         /**
32          * コンストラクタ
33          */
34         function __construct()
35         {
36                 // 親クラスを呼び出す
37                 parent::__construct();
38                 
39                 // DBオブジェクト作成
40                 $this->db = new static_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 == 'list'){           // 一覧画面
56                         return 'admin_main_list.tmpl.html';
57                 } else {                        // 一覧画面
58                         return 'admin_main_detail.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                 $showReadMore = ($request->trimValueOf('item_show_read_more') == 'on') ? 1 : 0;         // 「続きを読む」ボタンを表示
98                 $readMoreTitle = $request->trimValueOf('item_read_more_title');                                         // 「続きを読む」ボタンタイトル
99                 $this->contentId = $request->trimValueOf('contentid');  // コンテンツ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                         if (empty($this->contentId)) $this->setUserErrorMsg('表示コンテンツが選択されていません');
112
113                         // 設定名の重複チェック
114                         for ($i = 0; $i < count($this->paramObj); $i++){
115                                 $targetObj = $this->paramObj[$i]->object;
116                                 if ($name == $targetObj->name){         // 定義名
117                                         $this->setUserErrorMsg('名前が重複しています');
118                                         break;
119                                 }
120                         }
121                         
122                         // エラーなしの場合は、データを登録
123                         if ($this->getMsgCount() == 0){
124                                 // 追加オブジェクト作成
125                                 $newObj = new stdClass;
126                                 $newObj->name   = $name;// 表示名
127                                 $newObj->contentId = $this->contentId;          // コンテンツID
128                                 $newObj->showReadMore   = $showReadMore;                // 「続きを読む」ボタンを表示
129                                 $newObj->readMoreTitle  = $readMoreTitle;               // 「続きを読む」ボタンタイトル
130                                 
131                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
132                                 if ($ret){
133                                         $this->setGuidanceMsg('データを追加しました');
134                                         
135                                         $this->configId = $defConfigId;         // 定義定義IDを更新
136                                         $replaceNew = true;                     // データ再取得
137                                 } else {
138                                         $this->setAppErrorMsg('データ追加に失敗しました');
139                                 }
140                         }
141                 } else if ($act == 'update'){           // 項目更新の場合
142                         // エラーなしの場合は、データを更新
143                         if ($this->getMsgCount() == 0){
144                                 // 現在の設定値を取得
145                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
146                                 if ($ret){
147                                         // ウィジェットオブジェクト更新
148                                         $targetObj->contentId = $this->contentId;
149                                         $targetObj->showReadMore        = $showReadMore;                // 「続きを読む」ボタンを表示
150                                         $targetObj->readMoreTitle       = $readMoreTitle;               // 「続きを読む」ボタンタイトル
151                                 }
152                                 
153                                 // 設定値を更新
154                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
155                                 if ($ret){
156                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
157                                         
158                                         $replaceNew = true;                     // データ再取得
159                                 } else {
160                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
161                                 }
162                         }
163                 } else if ($act == 'getmenu'){          // コンテンツ選択メニュー取得
164                         // コンテンツIDを取得
165                         $contentSerial = $request->trimValueOf('content_serial');
166                         $ret = $this->db->getContentBySerial($contentSerial, $row);
167                         if ($ret) $this->contentId = $row['cn_id'];
168                         
169                         // コンテンツ選択メニューを作成
170                         $this->menuHtml  = '<select name="contentid">';
171                 $this->menuHtml .= '<option value="0">-- 未選択 --</option>';
172                         $this->db->getAllContentItems(array($this, 'itemListLoop'), $this->langId);
173                         $this->menuHtml .= '</select>';
174                         
175                         $this->gInstance->getAjaxManager()->addData('menu_html', $this->menuHtml);
176                 } else if ($act == 'select'){   // 定義IDを変更
177                         $replaceNew = true;                     // データ再取得
178                 }
179
180                 // 定義選択メニュー作成
181                 $this->createDefListMenu();
182
183                 // 表示用データを取得
184                 if (empty($this->configId)){            // 新規登録の場合
185                         $this->tmpl->setAttribute('item_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
186                         if ($replaceNew){               // データ再取得時
187                                 $name = $this->createDefaultName();                     // デフォルト登録項目名
188                                 $showReadMore = 0;              // 「続きを読む」ボタンを表示
189                                 $readMoreTitle  = '';           // 「続きを読む」ボタンタイトル
190                         }
191                         $this->serialNo = 0;
192                 } else {
193                         if ($replaceNew){
194                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
195                                 if ($ret){
196                                         $name = $targetObj->name;// 名前
197                                         $this->contentId = $targetObj->contentId;                       // コンテンツID
198                                         $showReadMore = $targetObj->showReadMore;               // 「続きを読む」ボタンを表示
199                                         $readMoreTitle  = $targetObj->readMoreTitle;            // 「続きを読む」ボタンタイトル
200                                 }
201                         }
202                         $this->serialNo = $this->configId;
203                                 
204                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
205                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
206                 }
207                 
208                 // コンテンツ項目リストをデフォルト言語で取得
209                 $this->db->getAllContentItems(array($this, 'itemListLoop'), $this->langId);
210                 
211                 // ### 入力値を再設定 ###
212                 $this->tmpl->addVar("item_name_visible", "name", $name);                // 名前
213                 $checked = '';
214                 if ($showReadMore) $checked = 'checked';
215                 $this->tmpl->addVar("_widget", "show_read_more", $checked);     // 「続きを読む」ボタンを表示
216                 $this->tmpl->addVar("_widget", "read_more_title", $readMoreTitle);              // 「続きを読む」ボタンタイトル
217                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
218                 $this->tmpl->addVar('_widget', 'content_widget_id', self::CONTENT_WIDGET_ID);// コンテンツ表示ウィジェット
219                 
220                 // ボタンの表示制御
221                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
222                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
223                 } else {
224                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
225                 }
226                 // タブの選択状態を設定
227                 // 一度設定を保存している場合は、メニュー定義を前面にする(初期起動時のみ)
228                 //if (empty($act) && !empty($this->configId)) $this->tmpl->setAttribute('select_edit_content', 'visibility', 'visible');
229                 
230                 // ページ定義IDとページ定義のレコードシリアル番号を更新
231                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
232         }
233         /**
234          * 定義選択用メニュー
235          *
236          * @return なし                                               
237          */
238         function createDefListMenu()
239         {
240                 for ($i = 0; $i < count($this->paramObj); $i++){
241                         $id = $this->paramObj[$i]->id;// 定義ID
242                         $targetObj = $this->paramObj[$i]->object;
243                         $name = $targetObj->name;// 定義名
244                         $selected = '';
245
246                         if ($this->configId == $id) $selected = 'selected';
247                         $row = array(
248                                 'name' => $name,                // 名前
249                                 'value' => $id,         // 定義ID
250                                 'selected' => $selected // 選択中の項目かどうか
251                         );
252                         $this->tmpl->addVars('title_list', $row);
253                         $this->tmpl->parseTemplate('title_list', 'a');
254                 }
255         }
256         /**
257          * 取得したデータをテンプレートに設定する
258          *
259          * @param int $index                    行番号(0~)
260          * @param array $fetchedRow             フェッチ取得した行
261          * @param object $param                 未使用
262          * @return bool                                 true=処理続行の場合、false=処理終了の場合
263          */
264         function itemListLoop($index, $fetchedRow, $param)
265         {
266                 $id = $fetchedRow['cn_id'];
267                 $selected = '';
268                 if ($this->contentId == $id) $selected = 'selected';
269                         
270                 $row = array(
271                         'value' => $id,                 // ID
272                         'name' => $this->convertToDispString($fetchedRow['cn_name']),           // 名前
273                         'selected' => $selected // 選択中の項目かどうか
274                 );
275                 $this->tmpl->addVars('content_list', $row);
276                 $this->tmpl->parseTemplate('content_list', 'a');
277                 
278                 // コンテンツ選択メニューHTML
279                 $this->menuHtml .= '<option value="' . $id . '" ' . $selected . '>' . $this->convertToDispString($fetchedRow['cn_name']) . '</option>';
280                 return true;
281         }
282         /**
283          * デフォルトの名前を取得
284          *
285          * @return string       デフォルト名                                              
286          */
287         function createDefaultName()
288         {
289                 $name = self::DEFAULT_NAME_HEAD;
290                 for ($j = 1; $j < 100; $j++){
291                         $name = self::DEFAULT_NAME_HEAD . $j;
292                         // 設定名の重複チェック
293                         for ($i = 0; $i < count($this->paramObj); $i++){
294                                 $targetObj = $this->paramObj[$i]->object;
295                                 if ($name == $targetObj->name){         // 定義名
296                                         break;
297                                 }
298                         }
299                         // 重複なしのときは終了
300                         if ($i == count($this->paramObj)) break;
301                 }
302                 return $name;
303         }
304         /**
305          * 一覧画面作成
306          *
307          * @param RequestManager $request               HTTPリクエスト処理クラス
308          * @param                                                               なし
309          */
310         function createList($request)
311         {
312                 // ページ定義IDとページ定義のレコードシリアル番号を取得
313                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
314                 
315                 $userId         = $this->gEnv->getCurrentUserId();
316                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
317                 $act = $request->trimValueOf('act');
318                 
319                 // 詳細画面からの引継ぎデータ
320                 $contentId = $request->trimValueOf('contentid');
321                 
322                 if ($act == 'delete'){          // メニュー項目の削除
323                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
324                         $delItems = array();
325                         for ($i = 0; $i < count($listedItem); $i++){
326                                 // 項目がチェックされているかを取得
327                                 $itemName = 'item' . $i . '_selected';
328                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
329                                 
330                                 if ($itemValue){                // チェック項目
331                                         $delItems[] = $listedItem[$i];
332                                 }
333                         }
334                         if (count($delItems) > 0){
335                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
336                                 if ($ret){              // データ削除成功のとき
337                                         $this->setGuidanceMsg('データを削除しました');
338                                 } else {
339                                         $this->setAppErrorMsg('データ削除に失敗しました');
340                                 }
341                         }
342                 }
343                 // 選択リスト作成
344                 $this->createDefList();
345                 
346                 // メニュー定義画面のURLを作成
347                 $editUrl = $this->gEnv->getDefaultAdminUrl() . '?cmd=configwidget&openby=tabs&widget=' . self::CONTENT_WIDGET_ID . '&task=content_detail&contentid=' . $contentId;
348                 $this->tmpl->addVar("_widget", "url", $this->getUrl($editUrl));
349                 $this->tmpl->addVar("_widget", "content_id", $contentId);
350                 
351                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
352                 
353                 // ページ定義IDとページ定義のレコードシリアル番号を更新
354                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
355         }
356         /**
357          * 選択リスト作成
358          *
359          * @return なし                                               
360          */
361         function createDefList()
362         {
363                 for ($i = 0; $i < count($this->paramObj); $i++){
364                         $id = $this->paramObj[$i]->id;// 定義ID
365                         $targetObj = $this->paramObj[$i]->object;
366                         $name = $targetObj->name;// 定義名
367                         
368                         $defCount = 0;
369                         if (!empty($id)){
370                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
371                         }
372                         $operationDisagled = '';
373                         if ($defCount > 0) $operationDisagled = 'disabled';
374                         $row = array(
375                                 'index' => $i,
376                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
377                                 'name' => $this->convertToDispString($name),            // 名前
378                                 'def_count' => $defCount                                                        // 使用数
379                         );
380                         $this->tmpl->addVars('itemlist', $row);
381                         $this->tmpl->parseTemplate('itemlist', 'a');
382                         
383                         // シリアル番号を保存
384                         $this->serialArray[] = $id;
385                 }
386         }
387 }
388 ?>