OSDN Git Service

a75480b9e3d5fd65689e433275227e72ef0c708b
[magic3/magic3.git] / widgets / m / chacha / include / container / m_chachaBaseWidgetContainer.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: m_chachaBaseWidgetContainer.php 3509 2010-08-19 03:18:16Z fishbone $
14  * @link       http://www.m-media.co.jp
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseMobileWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/chachaDb.php');
18
19 class m_chachaBaseWidgetContainer extends BaseMobileWidgetContainer
20 {
21         protected $_db;                 // DB接続オブジェクト
22         protected $_mobileId;           // 携帯ID
23         protected $_boardId;    // 掲示板ID
24         protected $_autolink;           // リンクを自動作成
25         protected $_spacer;             // スペーサ
26         const ERR_MESSAGE_COLOR = '#ff0000';                    // エラーメッセージカラー
27         const SPACER_FORMAT = '<div style="background-color:#aaaaaa;margin:1px 0;height:1px;"><img src="%s/images/system/spacer.gif" width="1" height="1" /></div>';            // スペーサフォーマット
28         const CREATE_CODE_RETRY_COUNT = 10;                             // コード生成のリトライ数
29         const DEFAULT_BBS_ID_HEAD = 'board';            // デフォルトの掲示板ID
30         const AVATAR_DIR = '/widgets/chacha/avatar32';                  // アバター格納ディレクトリ
31         const DEFAULT_AVATAR_ICON_FILE = '/images/default_avatar32.gif';                // デフォルトのアバターアイコン(携帯)
32         const DEFAULT_AVATAR_FILE_EXT = 'gif';                  // アバターファイルのデフォルト拡張子
33         const AVATAR_SIZE = 32;                 // アバター画像のサイズ
34         // 値定義
35         const CF_AUTOLINK = 'autolink';                 // 自動的にリンクを作成
36         const CF_MESSAGE_COUNT_MYPAGE = 'm:message_count_mypage';       // マイページのメッセージ表示項目数(携帯)
37         const CF_TOP_CONTENTS = 'm:top_contents';               // トップコンテンツ(携帯)
38         const CF_MESSAGE_LENGTH = 'message_length';     // 最大メッセージ長
39         // 画面
40         const DEFAULT_TASK = 'top';             // デフォルトの画面
41         const TASK_TOP = 'top';                 // トップ画面
42         const TASK_THREAD = 'thread';                   // スレッド処理
43         const TASK_READ = 'read';               // スレッド表示
44         const TASK_PROFILE = 'profile';                 // プロフィール表示
45         const TASK_MYPAGE = 'mypage';                   // マイページ表示
46         // URL用パラメータ
47         const URL_PARAM_MEMBER_ID = 'memberid';         // 会員ID
48         const URL_PARAM_MESSAGE_ID = 'messageid';               // メッセージID
49         // 共通のCSS
50         const CSS_LINK_STYLE_TOP = 'text-align:right;'; // 上のリンク部のスタイル
51         const CSS_LINK_STYLE_BOTTOM = 'text-align:center;';                                     // 下のリンク部のスタイル
52 //      const CSS_LINK_STYLE_BOTTOM_RIGHT = 'text-align:right;';// 下右のリンク部のスタイル
53         const CSS_LINK_STYLE_INNER_BOTTOM = 'text-align:right;';        // 内枠の下のリンク部のスタイル
54
55         /**
56          * コンストラクタ
57          */
58         function __construct()
59         {
60                 // 親クラスを呼び出す
61                 parent::__construct();
62                                 
63                 // 端末IDを取得
64                 $this->_mobileId = $this->gEnv->getMobileId();
65                                         
66                 // サブウィジェット起動のときだけ初期処理実行
67                 if ($this->gEnv->getIsSubWidget()){
68                         // DBオブジェクト作成
69                         $this->_db = new chachaDb();
70                 
71                         // 定義ID取得
72                         // 定義IDから掲示板IDを作成
73                         $configId = $this->gEnv->getCurrentWidgetConfigId();
74                         $this->_boardId = self::DEFAULT_BBS_ID_HEAD . intval($configId);
75                 
76                         // BBS定義を読み込む
77                         $this->_loadConfig($this->_boardId);
78                 
79                         //$this->_currentPageUrl = $this->gEnv->createCurrentPageUrl(); // 現在のページのURL
80                         $this->_currentPageUrl = $this->gEnv->createCurrentPageUrlForMobile();// 現在のページのURL(携帯用のパラメータ付き)
81                         $this->_autolink = $this->_configArray[self::CF_AUTOLINK];              // リンクを自動作成
82                         
83                         // スペーサ作成
84                         $this->_spacer = sprintf(self::SPACER_FORMAT, $this->getUrl($this->gEnv->getRootUrl()));
85                 }
86         }
87         /**
88          * テンプレートにデータ埋め込む
89          *
90          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
91          *
92          * @param RequestManager $request               HTTPリクエスト処理クラス
93          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
94          * @return                                                              なし
95          */
96         function _postAssign($request, &$param)
97         {
98                 // 共通のリンク設定
99                 $this->tmpl->addVar("_widget", "link_style_top", self::CSS_LINK_STYLE_TOP);// 上下のリンク部のスタイル
100                 $this->tmpl->addVar("_widget", "link_style_bottom", self::CSS_LINK_STYLE_BOTTOM);// 下のリンク部のスタイル
101                 //$this->tmpl->addVar("_widget", "link_style_bottom_right", self::CSS_LINK_STYLE_BOTTOM_RIGHT);// 下右のリンク部のスタイル
102                 $this->tmpl->addVar("_widget", "link_style_inner_bottom", self::CSS_LINK_STYLE_INNER_BOTTOM);// 内枠下のリンク部のスタイル
103                 $this->tmpl->addVar('_widget', 'top_url', $this->gEnv->createCurrentPageUrlForMobile(''));
104                 
105                 // メッセージカラーを設定
106                 if ($this->getMsgCount(1) > 0 || $this->getMsgCount(2) > 0){            // エラーメッセージが出力されているとき
107                         $errMessageColor = self::ERR_MESSAGE_COLOR;             // エラーメッセージ色
108                         $errMessageStyle = '';
109                         if (!empty($errMessageColor)) $errMessageStyle .= 'color:' . $errMessageColor . ';';
110                         $attr = 'style="' . $errMessageStyle . 'text-align:center;"';
111                         $this->setMessageAttr($attr);
112                 } else if ($this->getMsgCount(3) > 0){                  // ガイダンスメッセージが出力されているとき
113                         $attr = 'style="text-align:center;"';
114                         $this->setMessageAttr($attr);
115                 }
116         }
117         /**
118          * ブログ定義値をDBから取得
119          *
120          * @param string $boardId       掲示板ID
121          * @return bool                         true=取得成功、false=取得失敗
122          */
123         function _loadConfig($boardId)
124         {
125                 $this->_configArray = array();
126
127                 // BBS定義を読み込み
128                 $ret = $this->_db->getAllConfig($rows, $boardId);
129                 if ($ret){
130                         // 取得データを連想配列にする
131                         $configCount = count($rows);
132                         for ($i = 0; $i < $configCount; $i++){
133                                 $key = $rows[$i]['mc_id'];
134                                 $value = $rows[$i]['mc_value'];
135                                 $this->_configArray[$key] = $value;
136                         }
137                 }
138                 return $ret;
139         }
140         /**
141          * アバターファイルのURLを取得
142          *
143          * @param string $memberId              会員ID
144          * @return string                               URL
145          */
146         function getAvatarUrl($memberId)
147         {
148                 $avatarImagePath = $this->gEnv->getResourcePath() . self::AVATAR_DIR . '/' . $memberId . '.' . self::DEFAULT_AVATAR_FILE_EXT;
149                 if (file_exists($avatarImagePath)){             // アバターファイルが見つからないときはデフォルトを使用
150                         $avatarImageUrl = $this->gEnv->getResourceUrl() . self::AVATAR_DIR . '/' . $memberId . '.' . self::DEFAULT_AVATAR_FILE_EXT;
151                 } else {
152                         $avatarImageUrl = $this->gEnv->getCurrentWidgetRootUrl() . self::DEFAULT_AVATAR_ICON_FILE;
153                 }
154                 return $avatarImageUrl;
155         }
156         /**
157          * メッセージを表示用に変換
158          *
159          * @param string $message               変換元メッセージ
160          * @param string $threadId              スレッドID
161          * @return string                               変換後メッセージ
162          */
163         function convDispMessage($message, $threadId)
164         {
165                 // リンク変換
166                 if (!empty($this->_autolink)){          // 自動リンク作成のとき
167                         $message = preg_replace("/(https?):\/\/([\w;\/\?:\@&=\+\$,\-\.!~\*'\(\)%#]+)/", "<a href=\"$1://$2\" target=\"_blank\">$1://$2</a>", $message);
168                 
169                         // メッセージへのリンク
170                 //      $baseUrl = $this->convertUrlToHtmlEntity($this->getUrl($this->_currentPageUrl . '&' . M3_REQUEST_PARAM_BBS_THREAD_ID . '=' . $threadId, true));
171                         $messageUrl = $baseUrl . $this->convertUrlToHtmlEntity('&' . M3_REQUEST_PARAM_ITEM_NO . '=');
172                         $messageListUrl = $baseUrl . $this->convertUrlToHtmlEntity('&' . M3_REQUEST_PARAM_LIST_NO . '=');
173                         $message = preg_replace("/&gt;&gt;([0-9]+)(?![-\d])/", '<a href="' . $messageUrl . '$1" target="_blank">&gt;&gt;$1</a>', $message);
174                         $message = preg_replace("/&gt;&gt;([0-9]+)\-([0-9]+)/", '<a href="' . $messageListUrl . '$1-$2" target="_blank">&gt;&gt;$1-$2</a>', $message);
175                 }
176                 return $message;
177         }
178         /**
179          * ランダム文字列を作成
180          *
181          * @param string $baseChars             使用する文字
182          * @param int $length                   作成する文字列の長さ
183          * @return string                               ランダム文字列。作成できなかった場合は空文字列。
184          */
185         function _createRandString($baseChars, $length)
186         {
187         // 文字列の初期化
188         $destStr = '';
189
190                 if (!(is_numeric($length) && $length > 0)) return $destStr;
191     
192                 for ($i = 0; $i < $length; $i++){
193                         $pos = rand(0, strlen($baseChars) -1);
194                         $destStr .= $baseChars[$pos];
195                 }
196                 return $destStr;
197         }
198         /**
199          * テキストデータを表示用のテキストに変換
200          *
201          * 変換内容 ・改行コードをスペース「&nbsp;」に変換
202          *
203          * @param string $src                   変換するデータ
204          * @return string                               変換後データ
205          */
206         function _convertToPreviewTextWithSpace($src)
207         {
208                 return preg_replace("/(\015\012)|(\015)|(\012)/", "&nbsp;", $src);
209         }
210 }
211 ?>