OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / blog_main / include / container / blog_mainCommentWidgetContainer.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: blog_mainCommentWidgetContainer.php 5145 2012-08-29 13:21:42Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() .    '/blog_mainBaseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/blog_mainDb.php');
18 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/blog_commentDb.php');
19
20 class blog_mainCommentWidgetContainer extends blog_mainBaseWidgetContainer
21 {
22         private $db;    // DB接続オブジェクト
23         private $mainDb;        // DB接続オブジェクト
24         private $serialNo;              // 選択中の項目のシリアル番号
25         private $entryId;
26         private $lang;          // 現在の選択言語
27         private $serialArray = array();         // 表示されている項目シリアル番号
28         private $entryArray = array();          // 表示されている項目の記事ID
29         const CONTENT_TYPE = 'bg';              // 記事参照数取得用
30         const DEFAULT_LIST_COUNT = 20;                  // 最大リスト表示数
31         const CATEGORY_COUNT = 2;                               // 記事カテゴリーの選択可能数
32         const COMMENT_SIZE = 40;                        // コメント内容の最大文字列長
33         
34         /**
35          * コンストラクタ
36          */
37         function __construct()
38         {
39                 // 親クラスを呼び出す
40                 parent::__construct();
41                 
42                 // DBオブジェクト作成
43                 $this->db = new blog_commentDb();
44                 $this->mainDb = new blog_mainDb();
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 == 'comment_detail'){         // 詳細画面
60                         return 'admin_comment_detail.tmpl.html';
61                 } else {                        // 一覧画面
62                         return 'admin_comment.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 == 'comment_detail'){ // 詳細画面
78                         return $this->createDetail($request);
79                 } else {                        // 一覧画面
80                         return $this->createList($request);
81                 }
82         }
83         /**
84          * JavascriptファイルをHTMLヘッダ部に設定
85          *
86          * JavascriptファイルをHTMLのheadタグ内に追加出力する。
87          * _assign()よりも後に実行される。
88          *
89          * @param RequestManager $request               HTTPリクエスト処理クラス
90          * @param object         $param                 任意使用パラメータ。
91          * @return string                                               Javascriptファイル。出力しない場合は空文字列を設定。
92          */
93         function _addScriptFileToHead($request, &$param)
94         {
95                 $scriptArray = array($this->getUrl($this->gEnv->getScriptsUrl() . self::CALENDAR_SCRIPT_FILE),          // カレンダースクリプトファイル
96                                                         $this->getUrl($this->gEnv->getScriptsUrl() . self::CALENDAR_LANG_FILE), // カレンダー言語ファイル
97                                                         $this->getUrl($this->gEnv->getScriptsUrl() . self::CALENDAR_SETUP_FILE));       // カレンダーセットアップファイル
98                 return $scriptArray;
99
100         }
101         /**
102          * CSSファイルをHTMLヘッダ部に設定
103          *
104          * CSSファイルをHTMLのheadタグ内に追加出力する。
105          * _assign()よりも後に実行される。
106          *
107          * @param RequestManager $request               HTTPリクエスト処理クラス
108          * @param object         $param                 任意使用パラメータ。
109          * @return string                                               CSS文字列。出力しない場合は空文字列を設定。
110          */
111         function _addCssFileToHead($request, &$param)
112         {
113                 return $this->getUrl($this->gEnv->getScriptsUrl() . self::CALENDAR_CSS_FILE);
114         }
115         /**
116          * 一覧画面作成
117          *
118          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
119          *
120          * @param RequestManager $request               HTTPリクエスト処理クラス
121          * @param                                                               なし
122          */
123         function createList($request)
124         {
125                 // ユーザ情報、表示言語
126                 $defaultLangId = $this->gEnv->getDefaultLanguage();
127                 
128                 $act = $request->trimValueOf('act');
129                 $this->langId = $request->trimValueOf('item_lang');                             // 現在メニューで選択中の言語
130                 if (empty($this->langId)) $this->langId = $defaultLangId;                       // 言語が選択されていないときは、デフォルト言語を設定  
131                 $blogId = $request->trimValueOf(M3_REQUEST_PARAM_BLOG_ID);              // 所属ブログ
132                 
133                 // ##### 検索条件 #####
134                 $pageNo = $request->trimValueOf('page');                                // ページ番号
135                 if (empty($pageNo)) $pageNo = 1;
136                 
137                 // DBの保存設定値を取得
138                 $maxListCount = self::DEFAULT_LIST_COUNT;
139                 $serializedParam = $this->_db->getWidgetParam($this->gEnv->getCurrentWidgetId());
140                 if (!empty($serializedParam)){
141                         $dispInfo = unserialize($serializedParam);
142                         $maxListCount = $dispInfo->maxMemberListCountByAdmin;           // 会員リスト最大表示数
143                 }
144
145                 $search_startDt = $request->trimValueOf('search_start');                // 検索範囲開始日付
146                 if (!empty($search_startDt)) $search_startDt = $this->convertToProperDate($search_startDt);
147                 $search_endDt = $request->trimValueOf('search_end');                    // 検索範囲終了日付
148                 if (!empty($search_endDt)) $search_endDt = $this->convertToProperDate($search_endDt);
149                 $search_keyword = $request->trimValueOf('search_keyword');                      // 検索キーワード
150
151                 if ($act == 'delete'){          // 項目削除の場合
152                         $listedItem = explode(',', $request->trimValueOf('seriallist'));
153                         $delItems = array();
154                         for ($i = 0; $i < count($listedItem); $i++){
155                                 // 項目がチェックされているかを取得
156                                 $itemName = 'item' . $i . '_selected';
157                                 $itemValue = ($request->trimValueOf($itemName) == 'on') ? 1 : 0;
158                                 
159                                 if ($itemValue){                // チェック項目
160                                         $delItems[] = $listedItem[$i];
161                                 }
162                         }
163                         if (count($delItems) > 0){
164                                 $ret = $this->db->delCommentItem($delItems);
165                                 if ($ret){              // データ削除成功のとき
166                                         $this->setGuidanceMsg('データを削除しました');
167                                 } else {
168                                         $this->setAppErrorMsg('データ削除に失敗しました');
169                                 }
170                         }
171                 } else if ($act == 'search'){           // 検索のとき
172                         if (!empty($search_startDt) && !empty($search_endDt) && $search_startDt > $search_endDt){
173                                 $this->setUserErrorMsg('期間の指定範囲にエラーがあります。');
174                         }
175                         $pageNo = 1;            // ページ番号初期化
176                 } else if ($act == 'selpage'){                  // ページ選択
177                 }
178                 // ###### 一覧の取得条件を作成 ######
179                 if (!empty($search_endDt)) $endDt = $this->getNextDay($search_endDt);
180                 
181                 // 総数を取得
182                 $totalCount = $this->db->getCommentItemCount($search_startDt, $endDt, $search_keyword, $this->langId, $blogId);
183
184                 // 表示するページ番号の修正
185                 $pageCount = (int)(($totalCount -1) / $maxListCount) + 1;               // 総ページ数
186                 if ($pageNo < 1) $pageNo = 1;
187                 if ($pageNo > $pageCount) $pageNo = $pageCount;
188                 $this->firstNo = ($pageNo -1) * $maxListCount + 1;              // 先頭番号
189                 
190                 // ページング用リンク作成
191                 $pageLink = '';
192                 if ($pageCount > 1){    // ページが2ページ以上のときリンクを作成
193                         for ($i = 1; $i <= $pageCount; $i++){
194                                 if ($i == $pageNo){
195                                         $link = '&nbsp;' . $i;
196                                 } else {
197                                         $link = '&nbsp;<a href="#" onclick="selpage(\'' . $i . '\');return false;">' . $i . '</a>';
198                                 }
199                                 $pageLink .= $link;
200                         }
201                 }
202                 
203                 // 記事項目リストを取得
204                 $this->db->searchCommentItems($maxListCount, $pageNo, $search_startDt, $endDt, $search_keyword, $this->langId, array($this, 'itemListLoop'), $blogId);
205                 if (count($this->serialArray) <= 0) $this->tmpl->setAttribute('itemlist', 'visibility', 'hidden');// 投稿記事がないときは、一覧を表示しない
206                 
207                 // 検索結果
208                 $this->tmpl->addVar("_widget", "page_link", $pageLink);
209                 $this->tmpl->addVar("_widget", "total_count", $totalCount);
210                 
211                 // 検索条件
212                 $this->tmpl->addVar("_widget", "search_start", $search_startDt);        // 開始日付
213                 $this->tmpl->addVar("_widget", "search_end", $search_endDt);    // 終了日付
214                 $this->tmpl->addVar("_widget", "search_keyword", $search_keyword);      // 検索キーワード
215
216                 // 非表示項目を設定
217                 $this->tmpl->addVar("_widget", "serial_list", implode($this->serialArray, ','));// 表示項目のシリアル番号を設定
218                 $this->tmpl->addVar("_widget", "entry_list", implode($this->entryArray, ','));// 表示項目の記事IDを設定
219                 $this->tmpl->addVar("_widget", "page", $pageNo);        // ページ番号
220                 $this->tmpl->addVar("_widget", "list_count", $maxListCount);    // 一覧表示項目数
221         }
222         /**
223          * 詳細画面作成
224          *
225          * @param RequestManager $request               HTTPリクエスト処理クラス
226          * @param                                                               なし
227          */
228         function createDetail($request)
229         {
230                 // デフォルト値
231                 $defaultLangId  = $this->gEnv->getDefaultLanguage();
232                 $regUserId              = $this->gEnv->getCurrentUserId();                      // 記事投稿ユーザ
233                 $regDt                  = date("Y/m/d H:i:s");                                          // 投稿日時
234
235                 $act = $request->trimValueOf('act');
236                 $this->langId = $request->trimValueOf('item_lang');                             // 現在メニューで選択中の言語
237                 if (empty($this->langId)) $this->langId = $defaultLangId;                       // 言語が選択されていないときは、デフォルト言語を設定
238                 $this->entryId = $request->trimValueOf('entry');                // 記事エントリーID
239                 $this->serialNo = $request->trimValueOf('serial');              // 選択項目のシリアル番号
240                 if (empty($this->serialNo)) $this->serialNo = 0;
241                 $name = $request->trimValueOf('item_name');
242                 $html = $request->valueOf('item_html');
243                 $url = $request->valueOf('item_url');
244                 $email = $request->valueOf('item_email');
245                 $reg_user = $request->valueOf('item_reg_user');
246                 $status = $request->trimValueOf('item_status');         // エントリー状態(0=未設定、1=編集中、2=公開、3=非公開)
247
248                 $dataReload = false;            // データの再ロード
249                 if ($act == 'add'){             // 項目追加の場合
250                 /*
251                         // 入力チェック
252                         $this->checkInput($name, 'タイトル');
253                                         
254                         // エラーなしの場合は、データを登録
255                         if ($this->getMsgCount() == 0){
256                                 $ret = $this->db->addEntryItem(0, $this->langId, $name, $html, $status, $this->categoryArray, $regUserId, $regDt, $newSerial);
257                                 if ($ret){
258                                         $this->setGuidanceMsg('データを追加しました');
259                                         // シリアル番号更新
260                                         $this->serialNo = $newSerial;
261                                         $dataReload = true;             // データの再ロード
262                                 } else {
263                                         $this->setAppErrorMsg('データ追加に失敗しました');
264                                 }
265                         }*/
266                 } else if ($act == 'update'){           // 項目更新の場合
267                         // エラーなしの場合は、データを更新
268                         if ($this->getMsgCount() == 0){
269                                 $ret = $this->db->updateCommentItem($this->serialNo, $name, $html, $url, $reg_user, $email);
270                                 if ($ret){
271                                         $this->setGuidanceMsg('データを更新しました');
272                                         $dataReload = true;             // データの再ロード
273                                 } else {
274                                         $this->setAppErrorMsg('データ更新に失敗しました');
275                                 }
276                         }
277                 } else if ($act == 'delete'){           // 項目削除の場合
278                         if (empty($this->serialNo)){
279                                 $this->setUserErrorMsg('削除項目が選択されていません');
280                         }
281                         // エラーなしの場合は、データを削除
282                         if ($this->getMsgCount() == 0){
283                                 $ret = $this->db->delCommentItem(array($this->serialNo));
284                                 if ($ret){              // データ削除成功のとき
285                                         $this->setGuidanceMsg('データを削除しました');
286                                 } else {
287                                         $this->setAppErrorMsg('データ削除に失敗しました');
288                                 }
289                         }
290                 } else if ($act == 'deleteid'){         // ID項目削除の場合
291                 } else {        // 初期画面表示のとき
292                         $dataReload = true;             // データの再ロード
293                 }
294                 
295                 // 設定データを再取得
296                 if ($dataReload){               // データの再ロード
297                         $ret = $this->db->getCommentBySerial($this->serialNo, $row);
298                         if ($ret){
299                                 $this->entryId = $row['be_id'];         // エントリーID
300                                 $entryTitle = $row['be_name'];                          // 記事タイトル
301                                 $name = $row['bo_name'];                                // コメントタイトル
302                                 $html = $row['bo_html'];                                // HTML
303                                 $email = $row['bo_email'];                              // eメール
304                                 $url = $row['bo_url'];                          // URL
305                                 $reg_user = $row['bo_user_name'];                               // 投稿者
306                                 $reg_dt = $this->convertToDispDateTime($row['bo_regist_dt']);                           // 投稿日時
307                                 $update_user = $row['update_user_name'];                // 更新者
308                                 $update_dt = $this->convertToDispDateTime($row['bo_update_dt']);                // 更新日時
309                         }
310                 }
311                 
312                 // 非表示項目を設定
313                 $this->tmpl->addVar("_widget", "serial", $this->serialNo);      // シリアル番号
314
315                 // 入力フィールドの設定、共通項目のデータ設定
316                 if ($this->entryId == 0){               // 記事IDが0のときは、新規追加モードにする
317                         $this->tmpl->addVar('_widget', 'id', '新規');
318                         
319                         $this->tmpl->setAttribute('add_button', 'visibility', 'visible');
320                 } else {
321                         $this->tmpl->addVar('_widget', 'id', $this->entryId);
322                         
323                         if ($this->serialNo == 0){              // 未登録データのとき
324                                 // データ追加ボタン表示
325                                 $this->tmpl->setAttribute('add_button', 'visibility', 'visible');
326                         } else {
327                                 // データ更新、削除ボタン表示
328                                 $this->tmpl->setAttribute('delete_button', 'visibility', 'visible');// デフォルト言語以外はデータ削除
329                                 $this->tmpl->setAttribute('update_button', 'visibility', 'visible');
330                         }
331                 }
332                 // ### 入力値を再設定 ###
333                 $this->tmpl->addVar("_widget", "entry_title", $this->convertToDispString($entryTitle));         // 記事タイトル
334                 $this->tmpl->addVar("_widget", "name", $this->convertToDispString($name));              // コメントタイトル
335                 $this->tmpl->addVar("_widget", "html", $this->convertToDispString($html));              // HTML
336                 $this->tmpl->addVar("_widget", "email", $this->convertToDispString($email));            // Eメール
337                 $this->tmpl->addVar("_widget", "url", $this->convertToDispString($url));                // URL
338                 $this->tmpl->addVar("_widget", "reg_user", $this->convertToDispString($reg_user));              // 投稿者
339                 $this->tmpl->addVar("_widget", "entry_dt", $reg_dt);    // 投稿日時
340                 $this->tmpl->addVar("_widget", "update_user", $this->convertToDispString($update_user));        // 更新者
341                 $this->tmpl->addVar("_widget", "update_dt", $update_dt);        // 更新日時
342         }
343         /**
344          * 取得したデータをテンプレートに設定する
345          *
346          * @param int $index                    行番号(0~)
347          * @param array $fetchedRow             フェッチ取得した行
348          * @param object $param                 未使用
349          * @return bool                                 true=処理続行の場合、false=処理終了の場合
350          */
351         function itemListLoop($index, $fetchedRow, $param)
352         {
353                 // シリアル番号
354                 $serial = $fetchedRow['bo_serial'];
355                 
356                 // 記事ID
357                 $entryId = $fetchedRow['be_id'];
358                 
359                 // 公開状態
360                 /*switch ($fetchedRow['bo_status']){
361                         case 1: $status = '<font color="green">公開</font>';  break;
362                         case 2: $status = '非公開';  break;
363                 }*/
364                 
365                 // コメント内容
366                 if (function_exists('mb_strimwidth')){
367                         $comment = mb_strimwidth($fetchedRow['bo_html'], 0, self::COMMENT_SIZE, '…');
368                 } else {
369                         $comment = substr($fetchedRow['bo_html'], 0, self::COMMENT_SIZE) . '...';
370                 }
371                 $row = array(
372                         'index' => $index,              // 項目番号
373                         'no' => $index + 1,                                                                                                     // 行番号
374                         'serial' => $serial,                    // シリアル番号
375                         'entry_name' => $this->convertToDispString($fetchedRow['be_name']),             // 記事タイトル
376                         'name' => $this->convertToDispString($fetchedRow['bo_name']),           // コメントタイトル
377                         'content' => $this->convertToDispString($comment),              // コメント内容
378                         'lang' => $lang,                                                                                                        // 対応言語
379                         'status' => $status,                                                                                                    // 公開状況
380                         'reg_user' => $this->convertToDispString($fetchedRow['bo_user_name']),  // 投稿者
381                         'reg_date' => $this->convertToDispDateTime($fetchedRow['bo_regist_dt']) // 投稿日時
382                 );
383                 $this->tmpl->addVars('itemlist', $row);
384                 $this->tmpl->parseTemplate('itemlist', 'a');
385                 
386                 // 表示中項目のシリアル番号を保存
387                 $this->serialArray[] = $serial;
388                 if (!in_array($entryId, $this->entryArray)) $this->entryArray[] = $entryId;             // 存在しない場合は追加
389                 return true;
390         }
391 }
392 ?>