OSDN Git Service

299d4dd64df6e3ed274357542e2a83d2965e74b3
[magic3/magic3.git] / widgets / m / content / include / container / m_contentWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
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-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: m_contentWidgetContainer.php 3749 2010-10-27 12:09:56Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/contentDb.php');
18 require_once($gEnvManager->getCommonPath() . '/valueCheck.php');
19
20 class m_contentWidgetContainer extends BaseWidgetContainer
21 {
22         private $db;                    // DB接続オブジェクト
23         private $_contentCreated;       // コンテンツが取得できたかどうか
24         private $currentDay;            // 現在日
25         private $currentHour;           // 現在時間
26         private $headTitle;             // HTMLヘッダタイトル
27         const CONTENT_TYPE = 'mobile';                  // コンテンツタイプ
28         const VIEW_CONTENT_TYPE = 'mc';                 // 参照数カウント用
29         const DEFAULT_SEARCH_LIST_COUNT = 10;                   // 最大リスト表示数
30         const MESSAGE_NO_CONTENT                = 'コンテンツが見つかりません';
31         const CONTENT_SIZE = 100;                       // 検索結果コンテンツの文字列最大長
32         
33         /**
34          * コンストラクタ
35          */
36         function __construct()
37         {
38                 // 親クラスを呼び出す
39                 parent::__construct();
40                 
41                 // DBオブジェクト作成
42                 $this->db = new contentDb();
43         }
44         /**
45          * テンプレートファイルを設定
46          *
47          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
48          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
49          *
50          * @param RequestManager $request               HTTPリクエスト処理クラス
51          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
52          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
53          */
54         function _setTemplate($request, &$param)
55         {
56                 $act = $request->trimValueOf('act');
57                 if ($act == 'search'){
58                         return 'search.tmpl.html';
59                 } else {
60                         return 'main.tmpl.html';
61                 }
62         }
63         /**
64          * テンプレートにデータ埋め込む
65          *
66          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
67          *
68          * @param RequestManager $request               HTTPリクエスト処理クラス
69          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
70          * @param                                                               なし
71          */
72         function _assign($request, &$param)
73         {
74                 // 現在日時を取得
75                 $this->currentDay = date("Y/m/d");              // 日
76                 $this->currentHour = (int)date("H");            // 時間
77                 $this->currentPageUrl = $this->gEnv->createCurrentPageUrl();// 現在のページURL
78
79                 // ログインユーザでないときは、ユーザ制限のない項目だけ表示
80                 $all = false;
81                 if ($this->gEnv->isCurrentUserLogined()) $all = true;
82                 
83                 $act = $request->trimValueOf('act');
84                 $keyword = $request->mobileTrimValueOf('keyword');
85                 $contentid = $request->trimValueOf('contentid');
86                 
87                 if ($act == 'search'){                  // 検索
88                         $itemCount = self::DEFAULT_SEARCH_LIST_COUNT;           // 取得数
89                         
90                         // キーワード検索のとき
91                         if (empty($keyword)){
92                                 $msg = '検索キーワードが入力されていません';
93                                 $this->headTitle = 'コンテンツ検索';
94                         } else {
95                                 $this->db->searchContentByKeyword(self::CONTENT_TYPE, $itemCount, 1, $keyword, $this->gEnv->getCurrentLanguage(), $all, array($this, 'searchItemsLoop'));
96                                 $this->headTitle = 'コンテンツ検索[' . $keyword . ']';
97                                 if (!$this->isExistsViewData) $msg = self::MESSAGE_NO_CONTENT;
98                         }
99                         $this->tmpl->addVar("_widget", "keyword", $keyword);
100                         if (!empty($msg)){
101                                 $this->tmpl->setAttribute('message', 'visibility', 'visible');// メッセージ表示
102                                 $this->tmpl->addVar("message", "msg", $msg);
103                         }
104                 } else if (empty($contentid)){  // コンテンツIDがないときはデフォルトデータを取得
105                         $this->db->getContentItems(self::CONTENT_TYPE, array($this, 'itemsLoop'), null, $this->gEnv->getCurrentLanguage());
106                         if (!$this->_contentCreated){           // コンテンツが取得できなかったときはデフォルト言語で取得
107                                 $this->db->getContentItems(self::CONTENT_TYPE, array($this, 'itemsLoop'), null, $this->gEnv->getDefaultLanguage());
108                         }
109                 } else {
110                         // データエラーチェック
111                         $contentIdArray = explode(',', $contentid);
112                         if (ValueCheck::isNumeric($contentIdArray)){            // すべて数値であるかチェック
113                                 $this->db->getContentItems(self::CONTENT_TYPE, array($this, 'itemsLoop'), $contentIdArray, $this->gEnv->getCurrentLanguage());
114                                 if (!$this->_contentCreated){           // コンテンツが取得できなかったときはデフォルト言語で取得
115                                         $this->db->getContentItems(self::CONTENT_TYPE, array($this, 'itemsLoop'), $contentIdArray, $this->gEnv->getDefaultLanguage());
116                                 }
117                         } else {
118                                 $this->setAppErrorMsg('IDにエラー値があります');
119                         }
120                 }
121                 // HTMLサブタイトルを設定
122                 if (!empty($this->headTitle)) $this->gPage->setHeadSubTitle($this->headTitle);
123         }
124         /**
125          * 取得したコンテンツ項目をテンプレートに設定する
126          *
127          * @param int           $index                  行番号
128          * @param array         $fetchedRow             取得行
129          * @param object        $param                  任意使用パラメータ
130          * @return bool                                         trueを返すとループ続行。falseを返すとその時点で終了。
131          */
132         function itemsLoop($index, $fetchedRow)
133         {
134                 // ビューカウントを更新
135                 if (!$this->gEnv->isSystemManageUser()){                // システム運用者以上の場合はカウントしない
136                         $this->gInstance->getAnalyzeManager()->updateContentViewCount(self::VIEW_CONTENT_TYPE, $fetchedRow['cn_serial'], $this->currentDay, $this->currentHour);
137                 }
138
139                 // タイトルを設定
140                 $title = $fetchedRow['cn_name'];
141                 if (empty($this->headTitle)) $this->headTitle = $title;
142                 
143                 // HTMLを出力
144                 // 出力内容は特にエラーチェックしない
145                 $contentText = $fetchedRow['cn_html'];
146                 $contentText = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $contentText);// アプリケーションルートを変換
147                 
148                 // 登録したキーワードを変換
149                 $this->gInstance->getTextConvManager()->convByKeyValue($contentText, $contentText, true/*改行コーをbrタグに変換*/);
150                 
151                 // 携帯用HTMLをきれいにする
152                 $contentText = $this->gInstance->getTextConvManager()->cleanMobileTag($contentText);
153                 
154                 // 表示属性を取得
155                 $showTitle = 0;
156                 $titleBgColor = '';
157                 $contentId = $fetchedRow['cn_id'];
158                 $paramObj = $this->getWidgetParamObjByConfigId($contentId);
159                 if (!empty($paramObj)){
160                         $showTitle = $paramObj->showTitle;              // タイトルの表示
161                         $titleBgColor = $paramObj->titleBgColor;                // タイトルバックグランドカラー
162                 }
163                 // タイトルの表示
164                 $titleStr = '';
165                 if ($showTitle){
166                         $titleStr = '<div align="center" style="text-align:center;';
167                         if (!empty($titleBgColor)) $titleStr .= 'background-color:' . $titleBgColor . ';';// タイトルバックグランドカラー
168                         $titleStr .= '">' . $this->convertToDispString($title) . '</div>';
169                 }
170                 $row = array(
171                         'title' => $titleStr,
172                         'content' => $contentText       // コンテンツ
173                 );
174                 $this->tmpl->addVars('contentlist', $row);
175                 $this->tmpl->parseTemplate('contentlist', 'a');
176                 
177                 // コンテンツが取得できた
178                 $this->_contentCreated = true;
179                 return true;
180         }
181         /**
182          * 取得したコンテンツ項目をテンプレートに設定する
183          *
184          * @param int           $index                  行番号
185          * @param array         $fetchedRow             取得行
186          * @param object        $param                  任意使用パラメータ
187          * @return bool                                         trueを返すとループ続行。falseを返すとその時点で終了。
188          */
189         function searchItemsLoop($index, $fetchedRow)
190         {
191                 // タイトルを設定
192                 $title = $fetchedRow['cn_name'];
193
194                 // 記事へのリンクを生成
195                 $linkUrl = $this->currentPageUrl . '&contentid=' . $fetchedRow['cn_id'];
196                 $link = '<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >' . $title . '</a>';
197
198                 // テキストに変換
199                 //$contentText = strip_tags($fetchedRow['cn_html']);
200                 $contentText = $this->gInstance->getTextConvManager()->htmlToText($fetchedRow['cn_html']);
201                 
202                 // アプリケーションルートを変換
203                 $contentText = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $contentText);
204                 
205                 // 登録したキーワードを変換
206                 $this->gInstance->getTextConvManager()->convByKeyValue($contentText, $contentText);
207                 
208                 // Magic3タグ削除(絵文字タグ削除)
209                 $contentText = $this->gInstance->getTextConvManager()->deleteM3Tag($contentText);
210                 
211                 // 検索結果用にテキストを詰める。改行、タブ、スペース削除。
212                 $contentText = str_replace(array("\r", "\n", "\t", " "), '', $contentText);
213                 
214                 // 文字列長を修正
215                 if (function_exists('mb_strimwidth')){
216                         $contentText = mb_strimwidth($contentText, 0, self::CONTENT_SIZE, '…');
217                 } else {
218                         $contentText = substr($contentText, 0, self::CONTENT_SIZE) . '...';
219                 }
220
221                 $row = array(
222                         'title' => $link,                       // リンク付きタイトル
223                         'content' => $this->convertToDispString($contentText)   // コンテンツ
224                 );
225                 $this->tmpl->addVars('contentlist', $row);
226                 $this->tmpl->parseTemplate('contentlist', 'a');
227                 $this->isExistsViewData = true;                         // 表示データがあるかどうか
228                 return true;
229         }
230 }
231 ?>