7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
10 * @author 株式会社 毎日メディアサービス
11 * @copyright Copyright 2010 株式会社 毎日メディアサービス.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
13 * @version SVN: $Id: admin_user_contentTabWidgetContainer.php 3057 2010-04-22 10:14:03Z fishbone $
14 * @link http://www.m-media.co.jp
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_user_contentBaseWidgetContainer.php');
18 class admin_user_contentTabWidgetContainer extends admin_user_contentBaseWidgetContainer
20 private $langId; // 言語ID
21 private $contentItems = array(); // コンテンツ項目定義
22 private $serialNo; // 選択中の項目のシリアル番号
23 private $serialArray = array(); // 表示されている項目シリアル番号
28 function __construct()
31 parent::__construct();
36 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
37 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
39 * @param RequestManager $request HTTPリクエスト処理クラス
40 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
41 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
43 function _setTemplate($request, &$param)
45 $task = $request->trimValueOf('task');
46 if ($task == 'tab_detail'){ // 詳細画面
47 return 'admin_tab_detail.tmpl.html';
49 return 'admin_tab.tmpl.html';
55 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
57 * @param RequestManager $request HTTPリクエスト処理クラス
58 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
61 function _assign($request, &$param)
63 $task = $request->trimValueOf('task');
64 if ($task == 'tab_detail'){ // 詳細画面
65 return $this->createDetail($request);
67 return $this->createList($request);
73 * @param RequestManager $request HTTPリクエスト処理クラス
76 function createList($request)
78 $this->langId = $this->gEnv->getDefaultLanguage(); // デフォルト言語
79 $act = $request->trimValueOf('act');
81 if ($act == 'delete'){ // 項目削除の場合
82 $listedItem = explode(',', $request->trimValueOf('seriallist'));
84 for ($i = 0; $i < count($listedItem); $i++){
86 $itemName = 'item' . $i . '_selected';
87 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
89 if ($itemValue){ // チェック項目
90 $delItems[] = $listedItem[$i];
93 if (count($delItems) > 0){
94 $ret = $this->_localDb->delTab($delItems);
95 if ($ret){ // データ削除成功のとき
96 $this->setGuidanceMsg('データを削除しました');
98 $this->setAppErrorMsg('データ削除に失敗しました');
103 $this->contentItems = $this->getAllContentItems();
106 $this->_localDb->getAllTabs($this->langId, array($this, 'itemLoop'));
108 if (count($this->serialArray) > 0){
109 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
112 $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
118 * @param RequestManager $request HTTPリクエスト処理クラス
121 function createDetail($request)
123 $this->langId = $this->gEnv->getDefaultLanguage(); // デフォルト言語
125 $act = $request->trimValueOf('act');
126 $this->serialNo = $request->trimValueOf('serial'); // 選択項目のシリアル番号
127 $name = $request->trimValueOf('item_name'); // 名前
128 $id = $request->trimValueOf('item_id'); // 識別ID
129 $groupId = $request->trimValueOf('item_group_id'); // 所属グループID
130 $index = $request->trimValueOf('item_index'); // 表示順
131 $html = $request->valueOf('item_html'); // テンプレート用HTML
132 $visible = ($request->trimValueOf('item_visible') == 'on') ? 1 : 0; // 表示するかどうか
134 $replaceNew = false; // データを再取得するかどうか
135 if ($act == 'add'){ // 新規追加のとき
137 $this->checkInput($name, '表示名');
138 $this->checkSingleByte($id, '識別ID');
139 $this->checkNumeric($groupId, '所属グループID'); // 所属グループID
140 $this->checkNumeric($index, '表示順');
143 if ($this->_localDb->getTabById($this->langId, $id, $row)) $this->setMsg(self::MSG_USER_ERR, '識別IDが重複しています');
146 if ($this->getMsgCount() == 0){
147 // テンプレートから埋め込まれているコンテンツ項目のIDを取得
149 $idArray = $this->getItemId($html);
150 if (!empty($idArray)) $useItemId = implode(',', $idArray);
152 $ret = $this->_localDb->updateTab(0/*新規*/, $this->langId, $id, $name, $html, $index, $visible, $useItemId, $groupId, $newSerial);
153 if ($ret){ // データ追加成功のとき
154 $this->setMsg(self::MSG_GUIDANCE, 'データを追加しました');
156 $this->serialNo = $newSerial; // シリアル番号を更新
157 $replaceNew = true; // データを再取得
159 $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
162 } else if ($act == 'update'){ // 行更新のとき
164 $this->checkInput($name, '表示名');
165 $this->checkNumeric($groupId, '所属グループID'); // 所属グループID
166 $this->checkNumeric($index, '表示順');
169 if ($this->getMsgCount() == 0){
170 // テンプレートから埋め込まれているコンテンツ項目のIDを取得
172 $idArray = $this->getItemId($html);
173 if (!empty($idArray)) $useItemId = implode(',', $idArray);
175 $ret = $this->_localDb->updateTab($this->serialNo, $this->langId, $id, $name, $html, $index, $visible, $useItemId, $groupId, $newSerial);
176 if ($ret){ // データ追加成功のとき
177 $this->setMsg(self::MSG_GUIDANCE, 'データを更新しました');
179 $this->serialNo = $newSerial; // シリアル番号を更新
180 $replaceNew = true; // データを再取得
182 $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
187 $ret = $this->_localDb->getTabBySerial($this->serialNo, $row);
188 if ($ret) $id = $row['ub_id']; // タブ識別ID
190 if (empty($id)){ // タブ識別IDが空のときは新規とする
194 $groupId = 0; // 所属グループID
195 $html = ''; // テンプレート
196 $index = $this->_localDb->getMaxTabIndex() + 1; // 表示順
199 $replaceNew = true; // データを再取得
203 $this->contentItems = $this->getAllContentItems();
208 $ret = $this->_localDb->getTabById($this->langId, $id, $row);
210 $this->serialNo = $row['ub_serial'];
211 $name = $row['ub_name'];
212 $groupId = $row['ub_group_id']; // 所属グループID
213 $html = $row['ub_template_html'];
214 $index = $row['ub_index'];
215 $visible = $row['ub_visible']; // 公開
216 $useItem = $row[ub_use_item_id]; // 使用中のコンテンツ項目
219 // 使用しているコンテンツ項目IDを取得
221 if (!empty($useItem)){
222 $useItemArray = explode(',', $useItem);
223 for ($i = 0; $i < count($useItemArray); $i++){
224 $key = $useItemArray[$i];
225 $contentName = $this->contentItems[$key]['ui_name'];
226 $useItemStr .= $contentName . '(' . $key . '),';
228 $useItemStr = rtrim($useItemStr, ',');
231 if (empty($this->serialNo)){ // シリアル番号が空のときは新規とする
232 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
233 $this->tmpl->setAttribute('new_id_field', 'visibility', 'visible');// 新規ID入力フィールド表示
235 $this->tmpl->addVar("new_id_field", "id", $id); // 識別キー
237 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新ボタン表示
238 $this->tmpl->setAttribute('id_field', 'visibility', 'visible');// 固定IDフィールド表示
240 $this->tmpl->addVar("id_field", "id", $id); // 識別キー
244 $this->tmpl->addVar("_widget", "name", $name); // 名前
245 $this->tmpl->addVar("_widget", "group_id", $groupId); // 所属グループID
246 $this->tmpl->addVar("_widget", "index", $index); // 表示順
247 $this->tmpl->addVar("_widget", "html", $html); // テンプレート
248 $this->tmpl->addVar("_widget", "use_item", $this->convertToDispString($useItemStr)); // コンテンツ項目
250 // 項目表示、項目利用可否チェックボックス
252 if ($visible) $checked = 'checked';
253 $this->tmpl->addVar("_widget", "visible", $checked);
256 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
259 * 取得したタブ定義をテンプレートに設定する
261 * @param int $index 行番号(0~)
262 * @param array $fetchedRow フェッチ取得した行
263 * @param object $param 未使用
264 * @return bool true=処理続行の場合、false=処理終了の場合
266 function itemLoop($index, $fetchedRow, $param)
269 if ($fetchedRow['ub_visible']){ // 項目の表示
270 $visible = 'checked';
273 // 使用しているコンテンツ項目IDを取得
274 $useItem = $fetchedRow['ub_use_item_id'];
276 if (!empty($useItem)){
277 $useItemArray = explode(',', $useItem);
278 for ($i = 0; $i < count($useItemArray); $i++){
279 $key = $useItemArray[$i];
280 $name = $this->contentItems[$key]['ui_name'];
281 $useItemStr .= $name . '(' . $key . '),';
283 $useItemStr = rtrim($useItemStr, ',');
288 'serial' => $fetchedRow['ub_serial'],
289 'id' => $this->convertToDispString($fetchedRow['ub_id']), // 識別ID
290 'name' => $this->convertToDispString($fetchedRow['ub_name']), // 表示名
291 'group' => $this->convertToDispString($fetchedRow['ub_group_id']), // 所属グループID
292 'visible' => $visible, // 公開
293 'use_item' => $useItemStr, // 使用しているコンテンツ項目ID
295 $this->tmpl->addVars('itemlist', $row);
296 $this->tmpl->parseTemplate('itemlist', 'a');
299 $this->serialArray[] = $fetchedRow['ub_serial'];
303 * テンプレートから埋め込まれているコンテンツ項目IDを取得
305 * @param string $src 検索対象データ
306 * @return array コンテンツ項目ID
308 function getItemId($src)
312 $pattern = '/(' . preg_quote(M3_TAG_START) . '([A-Za-z0-9_]+)' . preg_quote(M3_TAG_END) . ')/u';
313 preg_match_all($pattern, $src, $matches, PREG_SET_ORDER);
314 for ($i = 0; $i < count($matches); $i++){
315 $value = $matches[$i][2];
316 if (strStartsWith($value, M3_TAG_MACRO_ITEM_KEY)){
317 $value = substr($value, strlen(M3_TAG_MACRO_ITEM_KEY));
318 if (!in_array($value, $idArray)) $idArray[] = $value;
326 * @return array コンテンツ項目IDをキーにしたコンテンツ項目レコードの連想配列
328 function getAllContentItems()
330 $destArray = array();
332 $ret = $this->_localDb->getAllContentItems($rows);
334 $count = count($rows);
335 for ($i = 0; $i < $count; $i++){
336 $key = $rows[$i]['ui_id'];
337 $destArray[$key] = $rows[$i];