OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / featured_content / include / container / admin_featured_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-2013 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_featured_contentWidgetContainer.php 5544 2013-01-14 04:57:24Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/featured_contentDb.php');
18
19 class admin_featured_contentWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();                 // 表示中のシリアル番号
24         private $langId;
25         private $configId;              // 定義ID
26         private $paramObj;              // パラメータ保存用オブジェクト
27         private $fieldInfoArray = array();                      // フィールド情報
28         const DEFAULT_NAME_HEAD = '名称未設定';                    // デフォルトの設定名
29         const CONTENT_WIDGET_ID_PC = 'default_content';                 // コンテンツ編集ウィジェット(PC用)
30         const CONTENT_TYPE = '';                        // コンテンツタイプ
31         const DEFAULT_COLUMN_COUNT = 2;         // カラム数
32         const DEFAULT_LEAD_CONTENT_COUNT = 1;           // 先頭のコンテンツ数
33         const DEFAULT_COLUMN_CONTENT_COUNT = 2;         // カラム部のコンテンツ数
34         
35         /**
36          * コンストラクタ
37          */
38         function __construct()
39         {
40                 // 親クラスを呼び出す
41                 parent::__construct();
42                 
43                 // DBオブジェクト作成
44                 $this->db = new featured_contentDb();
45         }
46         /**
47          * テンプレートファイルを設定
48          *
49          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
50          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
51          *
52          * @param RequestManager $request               HTTPリクエスト処理クラス
53          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
54          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
55          */
56         function _setTemplate($request, &$param)
57         {
58                 $task = $request->trimValueOf('task');
59                 if ($task == 'list'){           // 一覧画面
60                         return 'admin_list.tmpl.html';
61                 } else {                        // 一覧画面
62                         return 'admin.tmpl.html';
63                 }
64         }
65         /**
66          * テンプレートにデータ埋め込む
67          *
68          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
69          *
70          * @param RequestManager $request               HTTPリクエスト処理クラス
71          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
72          * @param                                                               なし
73          */
74         function _assign($request, &$param)
75         {
76                 $task = $request->trimValueOf('task');
77                 if ($task == 'list'){           // 一覧画面
78                         return $this->createList($request);
79                 } else {                        // 詳細設定画面
80                         return $this->createDetail($request);
81                 }
82         }
83         /**
84          * 詳細画面作成
85          *
86          * @param RequestManager $request               HTTPリクエスト処理クラス
87          * @param                                                               なし
88          */
89         function createDetail($request)
90         {
91                 // ページ定義IDとページ定義のレコードシリアル番号を取得
92                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
93                 
94                 $userId         = $this->gEnv->getCurrentUserId();
95                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
96                 $act = $request->trimValueOf('act');
97                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
98                 $this->configId = $request->trimValueOf('item_id');             // 定義ID
99                 if (empty($this->configId)) $this->configId = $defConfigId;             // 呼び出しウィンドウから引き継いだ定義ID
100                 
101                 // 入力値を取得
102                 $defName        = $request->trimValueOf('item_def_name');                       // 定義名
103                 $fieldCount = $request->trimValueOf('fieldcount');              // 表示項目数
104 //              $names = $request->trimValueOf('item_name');            // 表示項目、名前
105                 $contentIds = $request->trimValueOf('item_contentid');          // 表示項目、コンテンツID
106                 $showReadMore = ($request->trimValueOf('item_show_read_more') == 'on') ? 1 : 0;         // 「続きを読む」ボタンを表示
107                 $readMoreTitle = $request->trimValueOf('item_read_more_title');                                         // 「続きを読む」ボタンタイトル
108                 $leadContentCount = $request->trimValueOf('item_lead_content_count');                                           // 先頭のコンテンツ数
109                 $columnContentCount = $request->trimValueOf('item_column_content_count');                                               // カラム部のコンテンツ数
110                 $columnCount    = $request->trimValueOf('item_column_count');                                           // カラム数
111                 $showCreateDate = ($request->trimValueOf('item_show_created_date') == 'on') ? 1 : 0;            // 表示項目(作成日)
112                 $showModifiedDate = ($request->trimValueOf('item_show_modified_date') == 'on') ? 1 : 0;         // 表示項目(更新日)
113                 $showPublishedDate = ($request->trimValueOf('item_show_published_date') == 'on') ? 1 : 0;               // 表示項目(公開日)
114                 
115                 $replaceNew = false;            // データを再取得するかどうか
116                 if ($act == 'add'){// 新規追加
117                         // 入力値のエラーチェック
118                         $this->checkNumeric($leadContentCount, '先頭部のコンテンツ数');
119                         $this->checkNumeric($columnContentCount, 'カラム部のコンテンツ数');
120                         $this->checkNumeric($columnCount, 'カラム数');
121                         
122                         // 設定名の重複チェック
123                         for ($i = 0; $i < count($this->paramObj); $i++){
124                                 $targetObj = $this->paramObj[$i]->object;
125                                 if ($defName == $targetObj->name){              // 定義名
126                                         $this->setUserErrorMsg('名前が重複しています');
127                                         break;
128                                 }
129                         }
130                         
131                         // エラーなしの場合は、データを登録
132                         if ($this->getMsgCount() == 0){
133                                 // 追加オブジェクト作成
134                                 $newObj = new stdClass;
135                                 $newObj->name           = $defName;// 表示名
136                                 $newObj->showReadMore   = $showReadMore;                // 「続きを読む」ボタンを表示
137                                 $newObj->readMoreTitle  = $readMoreTitle;               // 「続きを読む」ボタンタイトル
138                                 $newObj->leadContentCount       = $leadContentCount;                                            // 先頭のコンテンツ数
139                                 $newObj->columnContentCount     = $columnContentCount;                                          // カラム部のコンテンツ数
140                                 $newObj->columnCount    = $columnCount;                                         // カラム数
141                                 $newObj->showCreateDate         = $showCreateDate;              // 表示項目(作成日)
142                                 $newObj->showModifiedDate       = $showModifiedDate;            // 表示項目(更新日)
143                                 $newObj->showPublishedDate      = $showPublishedDate;           // 表示項目(公開日)
144                 
145                                 $newObj->fieldInfo      = array();
146                                 
147                                 for ($i = 0; $i < $fieldCount; $i++){
148                                         $newInfoObj = new stdClass;
149 //                                      $newInfoObj->name               = $names[$i];
150                                         $newInfoObj->contentId  = $contentIds[$i];
151                                         $newObj->fieldInfo[] = $newInfoObj;
152                                 }
153                                 
154                                 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
155                                 if ($ret){
156                                         $this->setGuidanceMsg('データを追加しました');
157                                         
158                                         $this->configId = $defConfigId;         // 定義定義IDを更新
159                                         $replaceNew = true;                     // データ再取得
160                                 } else {
161                                         $this->setAppErrorMsg('データ追加に失敗しました');
162                                 }
163                         }
164                 } else if ($act == 'update'){           // 設定更新のとき
165                         // 入力値のエラーチェック
166                         $this->checkNumeric($leadContentCount, '先頭部のコンテンツ数');
167                         $this->checkNumeric($columnContentCount, 'カラム部のコンテンツ数');
168                         $this->checkNumeric($columnCount, 'カラム数');
169                         
170                         if ($this->getMsgCount() == 0){                 // エラーのないとき
171                                 // 現在の設定値を取得
172                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
173                                 if ($ret){
174                                         // ウィジェットオブジェクト更新
175                                         $targetObj->showReadMore        = $showReadMore;                // 「続きを読む」ボタンを表示
176                                         $targetObj->readMoreTitle       = $readMoreTitle;               // 「続きを読む」ボタンタイトル
177                                         $targetObj->leadContentCount    = $leadContentCount;                                            // 先頭のコンテンツ数
178                                         $targetObj->columnContentCount  = $columnContentCount;                                          // カラム部のコンテンツ数
179                                         $targetObj->columnCount = $columnCount;                                         // カラム数
180                                         $targetObj->showCreateDate              = $showCreateDate;              // 表示項目(作成日)
181                                         $targetObj->showModifiedDate    = $showModifiedDate;            // 表示項目(更新日)
182                                         $targetObj->showPublishedDate   = $showPublishedDate;           // 表示項目(公開日)
183                                         $targetObj->fieldInfo   = array();
184                                 
185                                         for ($i = 0; $i < $fieldCount; $i++){
186                                                 $newInfoObj = new stdClass;
187 //                                              $newInfoObj->name               = $names[$i];
188                                                 $newInfoObj->contentId  = $contentIds[$i];
189                                                 $targetObj->fieldInfo[] = $newInfoObj;
190                                         }
191                                 }
192                                 
193                                 // 設定値を更新
194                                 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
195                                 if ($ret){
196                                         $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
197                                         $replaceNew = true;                     // データ再取得
198                                 } else {
199                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
200                                 }
201                         }
202                 } else if ($act == 'select'){   // 定義IDを変更
203                         $replaceNew = true;                     // データ再取得
204                 } else {        // 初期起動時、または上記以外の場合
205                         // デフォルト値設定
206                         $this->configId = $defConfigId;         // 呼び出しウィンドウから引き継いだ定義ID
207                         $replaceNew = true;                     // データ再取得
208                 }
209                 // 設定項目選択メニュー作成
210                 $this->createItemMenu();
211                                 
212                 // 表示用データを取得
213                 if (empty($this->configId)){            // 新規登録の場合
214                         $this->tmpl->setAttribute('item_def_name_visible', 'visibility', 'visible');// 名前入力フィールド表示
215                         if ($replaceNew){               // データ再取得時
216                                 $defName = $this->createDefaultName();                  // デフォルト登録項目名
217                                 $showReadMore = 0;              // 「続きを読む」ボタンを表示
218                                 $readMoreTitle  = '';           // 「続きを読む」ボタンタイトル
219                                 $leadContentCount       = self::DEFAULT_LEAD_CONTENT_COUNT;                                             // 先頭のコンテンツ数
220                                 $columnContentCount     = self::DEFAULT_COLUMN_CONTENT_COUNT;                                           // カラム部のコンテンツ数
221                                 $columnCount            = self::DEFAULT_COLUMN_COUNT;                                           // カラム数
222                                 $showCreateDate         = 0;            // 表示項目(作成日)
223                                 $showModifiedDate       = 0;            // 表示項目(更新日)
224                                 $showPublishedDate      = 0;            // 表示項目(公開日)
225                                 $this->fieldInfoArray = array();                        // フィールド情報
226                         }
227                         $this->serialNo = 0;
228                 } else {
229                         if ($replaceNew){// データ再取得時
230                                 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
231                                 if ($ret){
232                                         $defName        = $targetObj->name;// 名前
233                                         $showReadMore = $targetObj->showReadMore;               // 「続きを読む」ボタンを表示
234                                         $readMoreTitle  = $targetObj->readMoreTitle;            // 「続きを読む」ボタンタイトル
235                                         $leadContentCount       = $targetObj->leadContentCount;                                         // 先頭のコンテンツ数
236                                         $columnContentCount     = $targetObj->columnContentCount;                                               // カラム部のコンテンツ数
237                                         $columnCount            = $targetObj->columnCount;                                              // カラム数
238                                         $showCreateDate         = $targetObj->showCreateDate;           // 表示項目(作成日)
239                                         $showModifiedDate       = $targetObj->showModifiedDate;         // 表示項目(更新日)
240                                         $showPublishedDate      = $targetObj->showPublishedDate;                // 表示項目(公開日)
241                                         if (!empty($targetObj->fieldInfo)) $this->fieldInfoArray = $targetObj->fieldInfo;                       // フィールド情報
242                                 }
243                         }
244                         $this->serialNo = $this->configId;
245                                 
246                         // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
247                         if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
248                 }
249                 
250                 // 表示項目一覧作成
251                 $this->createFieldList();
252                 if (empty($this->fieldInfoArray)) $this->tmpl->setAttribute('field_list', 'visibility', 'hidden');// 表示項目一覧を表示
253                         
254                 // 画面にデータを埋め込む(ウィジェット共通部)
255                 if (!empty($this->configId)) $this->tmpl->addVar("_widget", "id", $this->configId);             // 定義ID
256                 $this->tmpl->addVar("item_def_name_visible", "def_name",        $defName);
257                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);// 選択中のシリアル番号、IDを設定
258                 
259                 // 画面にデータを埋め込む
260                 $checked = '';
261                 if ($showReadMore) $checked = 'checked';
262                 $this->tmpl->addVar("_widget", "show_read_more", $checked);     // 「続きを読む」ボタンを表示
263                 $this->tmpl->addVar("_widget", "read_more_title", $readMoreTitle);              // 「続きを読む」ボタンタイトル
264                 $this->tmpl->addVar("_widget", "lead_content_count", $leadContentCount);                // 先頭のコンテンツ数
265                 $this->tmpl->addVar("_widget", "column_content_count", $columnContentCount);                                            // カラム部のコンテンツ数
266                 $this->tmpl->addVar("_widget", "column_count", $columnCount);                                           // カラム数
267                 $checked = '';
268                 if ($showCreateDate) $checked = 'checked';
269                 $this->tmpl->addVar("_widget", "show_created_date", $checked);  // 表示項目(作成日)
270                 $checked = '';
271                 if ($showModifiedDate) $checked = 'checked';
272                 $this->tmpl->addVar("_widget", "show_modified_date", $checked); // 表示項目(更新日)
273                 $checked = '';
274                 if ($showPublishedDate) $checked = 'checked';
275                 $this->tmpl->addVar("_widget", "show_published_date", $checked);        // 表示項目(公開日)
276                                         
277                 $this->tmpl->addVar('_widget', 'content_widget_id', self::CONTENT_WIDGET_ID_PC);// コンテンツ表示ウィジェット
278                 $this->tmpl->addVar('_widget', 'admin_url', $this->getUrl($this->gEnv->getDefaultAdminUrl()));// 管理者URL
279                 
280                 // ボタンの表示制御
281                 if (empty($this->serialNo)){            // 新規追加項目を選択しているとき
282                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
283                         
284                         // プレビューボタン作成
285                         $this->tmpl->addVar("_widget", "preview_disabled", 'disabled ');// 「プレビュー」ボタン
286                 } else {
287                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
288                         
289                         // このウィジェットがマップされているページサブIDを取得
290                         $subPageId = $this->gPage->getPageSubIdByWidget($this->gEnv->getDefaultPageId(), $this->gEnv->getCurrentWidgetId(), $defConfigId);
291                         $previewUrl = $this->gEnv->getDefaultUrl();
292                         if (!empty($subPageId)) $previewUrl .= '?sub=' . $subPageId;
293                         $this->tmpl->addVar("_widget", "preview_url", $this->getUrl($previewUrl));
294                 }
295                 
296                 // ページ定義IDとページ定義のレコードシリアル番号を更新
297                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
298         }
299         /**
300          * 選択用メニューを作成
301          *
302          * @return なし                                               
303          */
304         function createItemMenu()
305         {
306                 for ($i = 0; $i < count($this->paramObj); $i++){
307                         $id = $this->paramObj[$i]->id;// 定義ID
308                         $targetObj = $this->paramObj[$i]->object;
309                         $defName = $targetObj->name;// 定義名
310                         $selected = '';
311                         if ($this->configId == $id) $selected = 'selected';
312
313                         $row = array(
314                                 'name' => $defName,             // 名前
315                                 'value' => $id,         // 定義ID
316                                 'selected' => $selected // 選択中の項目かどうか
317                         );
318                         $this->tmpl->addVars('title_list', $row);
319                         $this->tmpl->parseTemplate('title_list', 'a');
320                 }
321         }
322         /**
323          * 表示項目一覧を作成
324          *
325          * @return なし                                               
326          */
327         function createFieldList()
328         {
329                 $fieldCount = count($this->fieldInfoArray);
330                 for ($i = 0; $i < $fieldCount; $i++){
331                         $infoObj = $this->fieldInfoArray[$i];
332 //                      $name = $infoObj->name;// 名前
333                         $contentId = $infoObj->contentId;               // コンテンツID
334                         
335                         // コンテンツを取得
336                         $ret = $this->db->getContentByContentId(self::CONTENT_TYPE, $contentId, $this->langId, $row);
337                         if ($ret){
338                                 $contentName = $row['cn_name'];
339                         }
340                         
341                         $row = array(
342 //                              'name' => $this->convertToDispString($name),    // 名前
343                                 'name' => $this->convertToDispString($contentName),     // コンテンツ名
344                                 'content_id' => $this->convertToDispString($contentId), // コンテンツID
345                                 'root_url' => $this->convertToDispString($this->getUrl($this->gEnv->getRootUrl()))
346                         );
347                         $this->tmpl->addVars('field_list', $row);
348                         $this->tmpl->parseTemplate('field_list', 'a');
349                 }
350         }
351         /**
352          * デフォルトの名前を取得
353          *
354          * @return string       デフォルト名                                              
355          */
356         function createDefaultName()
357         {
358                 $name = self::DEFAULT_NAME_HEAD;
359                 for ($j = 1; $j < 100; $j++){
360                         $name = self::DEFAULT_NAME_HEAD . $j;
361                         // 設定名の重複チェック
362                         for ($i = 0; $i < count($this->paramObj); $i++){
363                                 $targetObj = $this->paramObj[$i]->object;
364                                 if ($name == $targetObj->name){         // 定義名
365                                         break;
366                                 }
367                         }
368                         // 重複なしのときは終了
369                         if ($i == count($this->paramObj)) break;
370                 }
371                 return $name;
372         }
373         /**
374          * 一覧画面作成
375          *
376          * @param RequestManager $request               HTTPリクエスト処理クラス
377          * @param                                                               なし
378          */
379         function createList($request)
380         {
381                 // ページ定義IDとページ定義のレコードシリアル番号を取得
382                 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
383                 
384                 $userId         = $this->gEnv->getCurrentUserId();
385                 $langId = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
386                 $act = $request->trimValueOf('act');
387                 
388                 if ($act == 'delete'){          // メニュー項目の削除
389                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
390                         $delItems = array();
391                         for ($i = 0; $i < count($listedItem); $i++){
392                                 // 項目がチェックされているかを取得
393                                 $itemName = 'item' . $i . '_selected';
394                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
395                                 
396                                 if ($itemValue){                // チェック項目
397                                         $delItems[] = $listedItem[$i];
398                                 }
399                         }
400                         if (count($delItems) > 0){
401                                 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
402                                 if ($ret){              // データ削除成功のとき
403                                         $this->setGuidanceMsg('データを削除しました');
404                                 } else {
405                                         $this->setAppErrorMsg('データ削除に失敗しました');
406                                 }
407                         }
408                 }
409                 // 定義一覧作成
410                 $this->createItemList();
411                 
412                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
413                 
414                 // ページ定義IDとページ定義のレコードシリアル番号を更新
415                 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
416         }
417         /**
418          * 定義一覧作成
419          *
420          * @return なし                                               
421          */
422         function createItemList()
423         {
424                 for ($i = 0; $i < count($this->paramObj); $i++){
425                         $id                     = $this->paramObj[$i]->id;// 定義ID
426                         $targetObj      = $this->paramObj[$i]->object;
427                         $defName = $targetObj->name;// 定義名
428                 
429                         // 使用数
430                         $defCount = 0;
431                         if (!empty($id)){
432                                 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
433                         }
434                         $operationDisagled = '';
435                         if ($defCount > 0) $operationDisagled = 'disabled';
436                         
437                         $row = array(
438                                 'index' => $i,
439                                 'id' => $id,
440                                 'ope_disabled' => $operationDisagled,                   // 選択可能かどうか
441                                 'name' => $this->convertToDispString($defName),         // 名前
442                                 'def_count' => $defCount                                                        // 使用数
443                         );
444                         $this->tmpl->addVars('itemlist', $row);
445                         $this->tmpl->parseTemplate('itemlist', 'a');
446                         
447                         // シリアル番号を保存
448                         $this->serialArray[] = $id;
449                 }
450         }
451 }
452 ?>