OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / user_content / include / container / admin_user_contentTabWidgetContainer.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    ユーザ作成コンテンツ
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
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() . '/admin_user_contentBaseWidgetContainer.php');
17
18 class admin_user_contentTabWidgetContainer extends admin_user_contentBaseWidgetContainer
19 {
20         private $langId;                // 言語ID
21         private $contentItems = array();        // コンテンツ項目定義
22         private $serialNo;              // 選択中の項目のシリアル番号
23         private $serialArray = array();         // 表示されている項目シリアル番号
24         
25         /**
26          * コンストラクタ
27          */
28         function __construct()
29         {
30                 // 親クラスを呼び出す
31                 parent::__construct();
32         }
33         /**
34          * テンプレートファイルを設定
35          *
36          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
37          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
38          *
39          * @param RequestManager $request               HTTPリクエスト処理クラス
40          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
41          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
42          */
43         function _setTemplate($request, &$param)
44         {
45                 $task = $request->trimValueOf('task');
46                 if ($task == 'tab_detail'){             // 詳細画面
47                         return 'admin_tab_detail.tmpl.html';
48                 } else {                        // 一覧画面
49                         return 'admin_tab.tmpl.html';
50                 }
51         }
52         /**
53          * テンプレートにデータ埋め込む
54          *
55          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
56          *
57          * @param RequestManager $request               HTTPリクエスト処理クラス
58          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
59          * @param                                                               なし
60          */
61         function _assign($request, &$param)
62         {
63                 $task = $request->trimValueOf('task');
64                 if ($task == 'tab_detail'){     // 詳細画面
65                         return $this->createDetail($request);
66                 } else {                        // 一覧画面
67                         return $this->createList($request);
68                 }
69         }
70         /**
71          * 一覧画面作成
72          *
73          * @param RequestManager $request               HTTPリクエスト処理クラス
74          * @param                                                               なし
75          */
76         function createList($request)
77         {
78                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
79                 $act = $request->trimValueOf('act');
80                 
81                 if ($act == 'delete'){          // 項目削除の場合
82                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
83                         $delItems = array();
84                         for ($i = 0; $i < count($listedItem); $i++){
85                                 // 項目がチェックされているかを取得
86                                 $itemName = 'item' . $i . '_selected';
87                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
88                                 
89                                 if ($itemValue){                // チェック項目
90                                         $delItems[] = $listedItem[$i];
91                                 }
92                         }
93                         if (count($delItems) > 0){
94                                 $ret = $this->_localDb->delTab($delItems);
95                                 if ($ret){              // データ削除成功のとき
96                                         $this->setGuidanceMsg('データを削除しました');
97                                 } else {
98                                         $this->setAppErrorMsg('データ削除に失敗しました');
99                                 }
100                         }
101                 }
102                 // コンテンツ項目定義を取得
103                 $this->contentItems = $this->getAllContentItems();
104                 
105                 // 一覧作成
106                 $this->_localDb->getAllTabs($this->langId, array($this, 'itemLoop'));
107                 
108                 if (count($this->serialArray) > 0){
109                         $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
110                 } else {
111                         // 項目がないときは、一覧を表示しない
112                         $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');
113                 }
114         }
115         /**
116          * 詳細画面作成
117          *
118          * @param RequestManager $request               HTTPリクエスト処理クラス
119          * @param                                                               なし
120          */
121         function createDetail($request)
122         {
123                 $this->langId = $this->gEnv->getDefaultLanguage();              // デフォルト言語
124                 
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;             // 表示するかどうか
133                 
134                 $replaceNew = false;            // データを再取得するかどうか
135                 if ($act == 'add'){             // 新規追加のとき
136                         // 入力チェック
137                         $this->checkInput($name, '表示名');
138                         $this->checkSingleByte($id, '識別ID');
139                         $this->checkNumeric($groupId, '所属グループID');          // 所属グループID
140                         $this->checkNumeric($index, '表示順');
141                         
142                         // 同じIDがある場合はエラー
143                         if ($this->_localDb->getTabById($this->langId, $id, $row)) $this->setMsg(self::MSG_USER_ERR, '識別IDが重複しています');
144                         
145                         // エラーなしの場合は、データを更新
146                         if ($this->getMsgCount() == 0){
147                                 // テンプレートから埋め込まれているコンテンツ項目のIDを取得
148                                 $useItemId = '';
149                                 $idArray = $this->getItemId($html);
150                                 if (!empty($idArray)) $useItemId = implode(',', $idArray);
151                                 
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, 'データを追加しました');
155                                         
156                                         $this->serialNo = $newSerial;           // シリアル番号を更新
157                                         $replaceNew = true;                     // データを再取得
158                                 } else {
159                                         $this->setMsg(self::MSG_APP_ERR, 'データ追加に失敗しました');
160                                 }
161                         }
162                 } else if ($act == 'update'){           // 行更新のとき
163                         // 入力チェック
164                         $this->checkInput($name, '表示名');
165                         $this->checkNumeric($groupId, '所属グループID');          // 所属グループID
166                         $this->checkNumeric($index, '表示順');
167
168                         // エラーなしの場合は、データを更新
169                         if ($this->getMsgCount() == 0){
170                                 // テンプレートから埋め込まれているコンテンツ項目のIDを取得
171                                 $useItemId = '';
172                                 $idArray = $this->getItemId($html);
173                                 if (!empty($idArray)) $useItemId = implode(',', $idArray);
174                                 
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, 'データを更新しました');
178                                 
179                                         $this->serialNo = $newSerial;           // シリアル番号を更新
180                                         $replaceNew = true;                     // データを再取得
181                                 } else {
182                                         $this->setMsg(self::MSG_APP_ERR, 'データ更新に失敗しました');
183                                 }
184                         }
185                 } else {                // 初期状態
186                         // シリアル番号からデータを取得
187                         $ret = $this->_localDb->getTabBySerial($this->serialNo, $row);
188                         if ($ret) $id = $row['ub_id'];                  // タブ識別ID
189                         
190                         if (empty($id)){                // タブ識別IDが空のときは新規とする
191                                 $this->serialNo = 0;
192                                 $id             = '';           // 識別ID
193                                 $name   = '';   // 名前
194                                 $groupId = 0;   // 所属グループID
195                                 $html   = '';           // テンプレート
196                                 $index  = $this->_localDb->getMaxTabIndex() + 1;        // 表示順
197                                 $visible        = 1;            // 公開
198                         } else {
199                                 $replaceNew = true;                     // データを再取得
200                         }
201                 }
202                 // コンテンツ項目定義を取得
203                 $this->contentItems = $this->getAllContentItems();
204                 
205                 // 表示データ再取得
206                 if ($replaceNew){
207                         // タブ識別IDからデータを取得
208                         $ret = $this->_localDb->getTabById($this->langId, $id, $row);
209                         if ($ret){
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];         // 使用中のコンテンツ項目
217                         }
218                 }
219                 // 使用しているコンテンツ項目IDを取得
220                 $useItemStr = '';
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 . '),';
227                         }
228                         $useItemStr = rtrim($useItemStr, ',');
229                 }
230                 
231                 if (empty($this->serialNo)){            // シリアル番号が空のときは新規とする
232                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');// 新規登録ボタン表示
233                         $this->tmpl->setAttribute('new_id_field', 'visibility', 'visible');// 新規ID入力フィールド表示
234                         
235                         $this->tmpl->addVar("new_id_field", "id", $id);         // 識別キー
236                 } else {
237                         $this->tmpl->setAttribute('update_button', 'visibility', 'visible');// 更新ボタン表示
238                         $this->tmpl->setAttribute('id_field', 'visibility', 'visible');// 固定IDフィールド表示
239                         
240                         $this->tmpl->addVar("id_field", "id", $id);             // 識別キー
241                 }
242                 
243                 // 画面にデータを埋め込む
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));            // コンテンツ項目
249                 
250                 // 項目表示、項目利用可否チェックボックス
251                 $checked = '';
252                 if ($visible) $checked = 'checked';
253                 $this->tmpl->addVar("_widget", "visible", $checked);
254                 
255                 // 選択中のシリアル番号を設定
256                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);
257         }
258         /**
259          * 取得したタブ定義をテンプレートに設定する
260          *
261          * @param int $index                    行番号(0~)
262          * @param array $fetchedRow             フェッチ取得した行
263          * @param object $param                 未使用
264          * @return bool                                 true=処理続行の場合、false=処理終了の場合
265          */
266         function itemLoop($index, $fetchedRow, $param)
267         {
268                 $visible = '';
269                 if ($fetchedRow['ub_visible']){ // 項目の表示
270                         $visible = 'checked';
271                 }
272                 
273                 // 使用しているコンテンツ項目IDを取得
274                 $useItem = $fetchedRow['ub_use_item_id'];
275                 $useItemStr = '';
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 . '),';
282                         }
283                         $useItemStr = rtrim($useItemStr, ',');
284                 }
285                 
286                 $row = array(
287                         'index' => $index,
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
294                 );
295                 $this->tmpl->addVars('itemlist', $row);
296                 $this->tmpl->parseTemplate('itemlist', 'a');
297                 
298                 // 表示中項目のシリアル番号を保存
299                 $this->serialArray[] = $fetchedRow['ub_serial'];
300                 return true;
301         }
302         /**
303          * テンプレートから埋め込まれているコンテンツ項目IDを取得
304          *
305          * @param string $src           検索対象データ
306          * @return array                        コンテンツ項目ID
307          */
308         function getItemId($src)
309         {
310                 $idArray = array();
311                 $matches = array();
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;
319                         }
320                 }
321                 return $idArray;
322         }
323         /**
324          * すべてのコンテンツ項目IDを取得
325          *
326          * @return array                        コンテンツ項目IDをキーにしたコンテンツ項目レコードの連想配列
327          */
328         function getAllContentItems()
329         {
330                 $destArray = array();
331                 
332                 $ret = $this->_localDb->getAllContentItems($rows);
333                 if ($ret){
334                         $count = count($rows);
335                         for ($i = 0; $i < $count; $i++){
336                                 $key = $rows[$i]['ui_id'];
337                                 $destArray[$key] = $rows[$i];
338                         }
339                 }
340                 return $destArray;
341         }
342 }
343 ?>