7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/static_contentDb.php');
19 class admin_static_contentWidgetContainer extends BaseAdminWidgetContainer
21 private $db; // DB接続オブジェクト
22 private $serialNo; // 選択中の項目のシリアル番号
23 private $serialArray = array(); // 表示中のシリアル番号
25 private $paramObj; // パラメータ保存用オブジェクト
26 private $contentId; // コンテンツID
27 private $menuHtml; // コンテンツメニュー
28 const DEFAULT_NAME_HEAD = '名称未設定'; // デフォルトの設定名
29 const CONTENT_WIDGET_ID = 'default_content'; // コンテンツ編集ウィジェット
34 function __construct()
37 parent::__construct();
40 $this->db = new static_contentDb();
45 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
46 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
48 * @param RequestManager $request HTTPリクエスト処理クラス
49 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
50 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
52 function _setTemplate($request, &$param)
54 $task = $request->trimValueOf('task');
55 if ($task == 'list'){ // 一覧画面
56 return 'admin_main_list.tmpl.html';
58 return 'admin_main_detail.tmpl.html';
64 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
66 * @param RequestManager $request HTTPリクエスト処理クラス
67 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
70 function _assign($request, &$param)
72 $task = $request->trimValueOf('task');
73 if ($task == 'list'){ // 一覧画面
74 return $this->createList($request);
76 return $this->createDetail($request);
82 * @param RequestManager $request HTTPリクエスト処理クラス
85 function createDetail($request)
87 // ページ定義IDとページ定義のレコードシリアル番号を取得
88 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
90 $userId = $this->gEnv->getCurrentUserId();
91 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
92 $act = $request->trimValueOf('act');
93 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
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
103 $replaceNew = false; // データを再取得するかどうか
104 if (empty($act)){// 初期起動時
106 $this->configId = $defConfigId; // 呼び出しウィンドウから引き継いだ定義ID
107 $replaceNew = true; // データ再取得
108 } else if ($act == 'add'){// 新規追加
110 $this->checkInput($name, '名前');
111 if (empty($this->contentId)) $this->setUserErrorMsg('表示コンテンツが選択されていません');
114 for ($i = 0; $i < count($this->paramObj); $i++){
115 $targetObj = $this->paramObj[$i]->object;
116 if ($name == $targetObj->name){ // 定義名
117 $this->setUserErrorMsg('名前が重複しています');
123 if ($this->getMsgCount() == 0){
125 $newObj = new stdClass;
126 $newObj->name = $name;// 表示名
127 $newObj->contentId = $this->contentId; // コンテンツID
128 $newObj->showReadMore = $showReadMore; // 「続きを読む」ボタンを表示
129 $newObj->readMoreTitle = $readMoreTitle; // 「続きを読む」ボタンタイトル
131 $ret = $this->addPageDefParam($defSerial, $defConfigId, $this->paramObj, $newObj);
133 $this->setGuidanceMsg('データを追加しました');
135 $this->configId = $defConfigId; // 定義定義IDを更新
136 $replaceNew = true; // データ再取得
138 $this->setAppErrorMsg('データ追加に失敗しました');
141 } else if ($act == 'update'){ // 項目更新の場合
143 if ($this->getMsgCount() == 0){
145 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
148 $targetObj->contentId = $this->contentId;
149 $targetObj->showReadMore = $showReadMore; // 「続きを読む」ボタンを表示
150 $targetObj->readMoreTitle = $readMoreTitle; // 「続きを読む」ボタンタイトル
154 if ($ret) $ret = $this->updatePageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
156 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
158 $replaceNew = true; // データ再取得
160 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
163 } else if ($act == 'getmenu'){ // コンテンツ選択メニュー取得
165 $contentSerial = $request->trimValueOf('content_serial');
166 $ret = $this->db->getContentBySerial($contentSerial, $row);
167 if ($ret) $this->contentId = $row['cn_id'];
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>';
175 $this->gInstance->getAjaxManager()->addData('menu_html', $this->menuHtml);
176 } else if ($act == 'select'){ // 定義IDを変更
177 $replaceNew = true; // データ再取得
181 $this->createDefListMenu();
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 = ''; // 「続きを読む」ボタンタイトル
194 $ret = $this->getPageDefParam($defSerial, $defConfigId, $this->paramObj, $this->configId, $targetObj);
196 $name = $targetObj->name;// 名前
197 $this->contentId = $targetObj->contentId; // コンテンツID
198 $showReadMore = $targetObj->showReadMore; // 「続きを読む」ボタンを表示
199 $readMoreTitle = $targetObj->readMoreTitle; // 「続きを読む」ボタンタイトル
202 $this->serialNo = $this->configId;
204 // 新規作成でないときは、メニューを変更不可にする(画面作成から呼ばれている場合のみ)
205 if (!empty($defConfigId) && !empty($defSerial)) $this->tmpl->addVar("_widget", "id_disabled", 'disabled');
208 // コンテンツ項目リストをデフォルト言語で取得
209 $this->db->getAllContentItems(array($this, 'itemListLoop'), $this->langId);
212 $this->tmpl->addVar("item_name_visible", "name", $name); // 名前
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);// コンテンツ表示ウィジェット
221 if (empty($this->serialNo)){ // 新規追加項目を選択しているとき
222 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 「新規追加」ボタン
224 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 「更新」ボタン
227 // 一度設定を保存している場合は、メニュー定義を前面にする(初期起動時のみ)
228 //if (empty($act) && !empty($this->configId)) $this->tmpl->setAttribute('select_edit_content', 'visibility', 'visible');
230 // ページ定義IDとページ定義のレコードシリアル番号を更新
231 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
238 function createDefListMenu()
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;// 定義名
246 if ($this->configId == $id) $selected = 'selected';
248 'name' => $name, // 名前
249 'value' => $id, // 定義ID
250 'selected' => $selected // 選択中の項目かどうか
252 $this->tmpl->addVars('title_list', $row);
253 $this->tmpl->parseTemplate('title_list', 'a');
257 * 取得したデータをテンプレートに設定する
259 * @param int $index 行番号(0~)
260 * @param array $fetchedRow フェッチ取得した行
261 * @param object $param 未使用
262 * @return bool true=処理続行の場合、false=処理終了の場合
264 function itemListLoop($index, $fetchedRow, $param)
266 $id = $fetchedRow['cn_id'];
268 if ($this->contentId == $id) $selected = 'selected';
271 'value' => $id, // ID
272 'name' => $this->convertToDispString($fetchedRow['cn_name']), // 名前
273 'selected' => $selected // 選択中の項目かどうか
275 $this->tmpl->addVars('content_list', $row);
276 $this->tmpl->parseTemplate('content_list', 'a');
279 $this->menuHtml .= '<option value="' . $id . '" ' . $selected . '>' . $this->convertToDispString($fetchedRow['cn_name']) . '</option>';
285 * @return string デフォルト名
287 function createDefaultName()
289 $name = self::DEFAULT_NAME_HEAD;
290 for ($j = 1; $j < 100; $j++){
291 $name = self::DEFAULT_NAME_HEAD . $j;
293 for ($i = 0; $i < count($this->paramObj); $i++){
294 $targetObj = $this->paramObj[$i]->object;
295 if ($name == $targetObj->name){ // 定義名
300 if ($i == count($this->paramObj)) break;
307 * @param RequestManager $request HTTPリクエスト処理クラス
310 function createList($request)
312 // ページ定義IDとページ定義のレコードシリアル番号を取得
313 $this->startPageDefParam($defSerial, $defConfigId, $this->paramObj);
315 $userId = $this->gEnv->getCurrentUserId();
316 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
317 $act = $request->trimValueOf('act');
320 $contentId = $request->trimValueOf('contentid');
322 if ($act == 'delete'){ // メニュー項目の削除
323 $listedItem = explode(',', $request->trimValueOf('seriallist'));
325 for ($i = 0; $i < count($listedItem); $i++){
327 $itemName = 'item' . $i . '_selected';
328 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
330 if ($itemValue){ // チェック項目
331 $delItems[] = $listedItem[$i];
334 if (count($delItems) > 0){
335 $ret = $this->delPageDefParam($defSerial, $defConfigId, $this->paramObj, $delItems);
336 if ($ret){ // データ削除成功のとき
337 $this->setGuidanceMsg('データを削除しました');
339 $this->setAppErrorMsg('データ削除に失敗しました');
344 $this->createDefList();
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);
351 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
353 // ページ定義IDとページ定義のレコードシリアル番号を更新
354 $this->endPageDefParam($defSerial, $defConfigId, $this->paramObj);
361 function createDefList()
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;// 定義名
370 $defCount = $this->_db->getPageDefCount($this->gEnv->getCurrentWidgetId(), $id);
372 $operationDisagled = '';
373 if ($defCount > 0) $operationDisagled = 'disabled';
376 'ope_disabled' => $operationDisagled, // 選択可能かどうか
377 'name' => $this->convertToDispString($name), // 名前
378 'def_count' => $defCount // 使用数
380 $this->tmpl->addVars('itemlist', $row);
381 $this->tmpl->parseTemplate('itemlist', 'a');
384 $this->serialArray[] = $id;