OSDN Git Service

DB更新スクリプト更新。
[magic3/magic3.git] / widgets / m / chacha / include / container / m_chachaProfileWidgetContainer.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    マイクロブログ
10  * @author     株式会社 毎日メディアサービス
11  * @copyright  Copyright 2010 株式会社 毎日メディアサービス.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: m_chachaProfileWidgetContainer.php 3306 2010-06-28 07:07:39Z fishbone $
14  * @link       http://www.m-media.co.jp
15  */
16 require_once($gEnvManager->getCurrentWidgetContainerPath() .    '/m_chachaBaseWidgetContainer.php');
17
18 class m_chachaProfileWidgetContainer extends m_chachaBaseWidgetContainer
19 {
20         const DEFAULT_PAGE_TITLE = 'プロフィール';
21         const MEMBER_ID_LENGTH = 5;             // 会員IDの桁数
22         
23         /**
24          * コンストラクタ
25          */
26         function __construct()
27         {
28                 // 親クラスを呼び出す
29                 parent::__construct();
30         }
31         /**
32          * テンプレートファイルを設定
33          *
34          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
35          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
36          *
37          * @param RequestManager $request               HTTPリクエスト処理クラス
38          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
39          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
40          */
41         function _setTemplate($request, &$param)
42         {
43                 return 'profile.tmpl.html';
44         }
45         /**
46          * テンプレートにデータ埋め込む
47          *
48          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
49          *
50          * @param RequestManager $request               HTTPリクエスト処理クラス
51          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
52          * @param                                                               なし
53          */
54         function _assign($request, &$param)
55         {
56                 $act = $request->trimValueOf('act');
57                 $postTicket = $request->trimValueOf('ticket');          // POST確認用
58                 $memberId = $request->trimValueOf(self::URL_PARAM_MEMBER_ID);   // 会員ID
59
60                 // ##### ユーザの識別 #####
61                 // ユーザIDでユーザのプロフィールを参照。ユーザIDがない場合は自分自身のプロフィールを表示。
62                 // クライアントIDを取得
63                 $canEdit = false;                       // データ編集可能かどうか
64                 $isNew = false;                         // 新規登録かどうか
65                 
66                 // クライアントIDを取得
67                 $clientId = $this->_mobileId;
68
69                 // 会員情報が登録されている場合は更新。登録されていない場合は新規登録。
70                 $clientMemberId = '';                   // 現在の端末の会員ID
71                 $ret = $this->_db->getMemberInfoByDeviceId($clientId, $row);
72                 if ($ret) $clientMemberId = $row['mb_id'];
73                 if (empty($memberId)){
74                         if (empty($clientMemberId)){
75                                 $isNew = true;                                  // 新規登録処理
76                         } else {
77                                 $memberId = $clientMemberId;
78                         }
79                         $canEdit = true;                        // データ編集可能かどうか
80                 } else if ($memberId == $clientMemberId){               // 自分自身のデータのとき
81                         $canEdit = true;                        // データ編集可能かどうか
82                 }
83
84                 $name = $request->mobileTrimValueOf('name');                                    // ユーザ名
85                 $email = $request->mobileTrimValueOf('email');                          // Eメール
86                 $url = $request->mobileTrimValueOf('url');                                      // URL
87                 $avatar = '';                   // アバターファイル名
88                 $showEmail = ($request->trimValueOf('show_email') == 'on') ? 1 : 0;             // Eメールアドレスを公開するかどうか
89                 
90                 $reloadData = false;            // データの再読み込み
91                 if ($act == 'add'){             // 新規追加のとき
92                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET)){           // 正常なPOST値のとき
93                                 // 入力チェック
94                                 $this->checkInput($name, 'ニックネーム');
95                                 $this->checkMailAddress($email, 'Eメール', true/*空OK*/);
96                                 $this->checkInput($clientId, '端末ID');
97                         
98                                 if ($this->_db->isExistsMemberName($name)) $this->setUserErrorMsg('このニックネームはすでに存在しています');
99                                 
100                                 // エラーなしの場合は、データを登録
101                                 if ($this->getMsgCount() == 0){
102                                         // 会員ID作成
103                                         $memberId = $this->createMemberId();
104                                         if (empty($memberId)){
105                                                 $this->setAppErrorMsg('IDが作成できません');
106                                         } else {
107                                                 // 一般ユーザの場合はユーザIDも登録
108                                                 $userId = 0;
109                                                 $userInfo = $this->gEnv->getCurrentUserInfo();
110                                                 if (!is_null($userInfo) && $userInfo->userType == UserInfo::USER_TYPE_NORMAL) $userId = $this->gEnv->getCurrentUserId();
111                                                 
112                                                 $ret = $this->_db->addMember($clientId, $memberId, $userId, $name, $email, $url, ''/*アバターファイル名*/, $showEmail);
113                                                 if ($ret){
114                                                         $this->setGuidanceMsg('登録完了しました');
115                                                         $reloadData = true;             // データの再読み込み
116                                                         
117                                                         $isNew = false;                                 // 更新処理画面を表示
118                                                 }
119                                         }
120                                 }
121                         }
122                         $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
123                 } else if ($act == 'update'){           // 設定更新のとき
124                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET)){           // 正常なPOST値のとき
125                                 // 更新権限のチェック
126                                 if (!$canEdit) $this->setUserErrorMsg('更新権限がありません');
127                                 
128                                 // 入力チェック
129                                 $this->checkInput($name, 'ニックネーム');
130                                 $this->checkMailAddress($email, 'Eメール', true/*空OK*/);
131                                 $this->checkInput($clientId, '端末ID');
132                         
133                                 $ret = $this->_db->getMemberInfoByDeviceId($clientId, $row);
134                                 if ($ret){
135                                         if ($name != $row['mb_name'] && $this->_db->isExistsMemberName($name)) $this->setUserErrorMsg('このニックネームはすでに存在しています');
136                                 }
137                                                                 
138                                 // エラーなしの場合は、データを更新
139                                 if ($this->getMsgCount() == 0){
140                                         $ret = $this->_db->updateMember($clientId, $name, $email, $url, $avatar, $showEmail, $newSerial);
141                                         if ($ret){
142                                                 $this->setGuidanceMsg('更新完了しました');
143                                                 $reloadData = true;             // データの再読み込み
144                                         }
145                                 }
146                         }
147                         $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
148                 } else {
149                         $reloadData = true;             // データの再読み込み
150                 }
151                 
152                 // データの再取得
153                 if ($reloadData){
154                         $ret = $this->_db->getMemberInfoById($memberId, $row);
155                         if ($ret){
156                                 $name = $row['mb_name'];                // ユーザ名
157                                 $email = $row['mb_email'];      // Eメール
158                                 $url = $row['mb_url'];          // URL
159                                 $showEmail = $row['mb_show_email'];             // Eメールを公開するかどうか
160                         } else if (!empty($memberId)){
161                                 $this->setUserErrorMsg('登録されていないユーザです');
162                                 $memberId = '';         // 会員ID初期化
163                                 $canEdit = false;                       // データ編集不可
164                         }
165                 }
166                 // リンク作成
167                 $mypageUrl = '';
168                 //if (!empty($memberId)) $mypageUrl = $this->convertUrlToHtmlEntity($this->getUrl($this->_currentPageUrl . '&' . self::URL_PARAM_MEMBER_ID . '=' . $memberId, true));
169                 if (!empty($memberId)) $mypageUrl = $this->convertUrlToHtmlEntity($this->getUrl($this->gEnv->createCurrentPageUrlForMobile(self::URL_PARAM_MEMBER_ID . '=' . $memberId)));
170                                         
171                 // 編集状態の設定
172                 if ($canEdit){          // 編集可の場合
173                         // 各部の表示制御
174                         if ($isNew){            // 新規登録のとき
175                                 // メッセージ
176                                 if ($this->getMsgCount() == 0) $this->setGuidanceMsg('ユーザ登録して下さい');
177                                 
178                                 $this->tmpl->setAttribute('add_area', 'visibility', 'visible');// 新規登録ボタン表示
179                                 $this->tmpl->addVar('_widget', 'act', 'add');           // 新規登録
180                         } else {                // 更新のとき
181                                 $this->tmpl->setAttribute('update_area', 'visibility', 'visible');// 更新ボタン表示
182                                 $this->tmpl->addVar('_widget', 'act', 'update');                // 更新
183                         }
184                         // 各入力部表示
185                         $this->tmpl->setAttribute('name_input_area', 'visibility', 'visible');  // 名前編集
186                         $this->tmpl->setAttribute('name_required_area', 'visibility', 'visible');               // 「必須」メッセージ
187                         $this->tmpl->setAttribute('email_area', 'visibility', 'visible');                               // Eメール入力
188                         $this->tmpl->setAttribute('url_area', 'visibility', 'visible');                         // URL入力
189                         $this->tmpl->setAttribute('show_email_area', 'visibility', 'visible');                  // Eメール公開
190                         
191                         // リンクを表示
192                         $mypageName = '';// マイページURL
193                         $mypageLink = '';
194                         if (!empty($mypageUrl)){
195                                 $mypageName = '投稿[3]';
196                                 $mypageLink = '<a href="' . $mypageUrl . '" accesskey="3">投稿[3]</a><br />';
197                         }
198                         $this->tmpl->addVar("_widget", "mypage_name", $mypageName);
199                         $this->tmpl->addVar("_widget", "mypage_link", $mypageLink);
200                         //$this->tmpl->setAttribute('top_link_area', 'visibility', 'visible');
201                         //$this->tmpl->addVar("_widget", "mypage_url", $mypageUrl);                     // マイページURL
202                         
203                         // 値の埋め込み
204                         $this->tmpl->addVar("name_input_area", "name", $name);          // ユーザ名
205                         $this->tmpl->addVar("show_email_area", "email", $email);        // Eメール
206                         $this->tmpl->addVar("url_area", "url", $url);           // URL
207                         if ($showEmail) $this->tmpl->addVar("show_email_area", "show_email", 'checked');                // Eメールを公開するかどうか
208                         $this->tmpl->addVar('_widget', 'current_url', $this->gEnv->createCurrentPageUrlForMobile('task=' . self::TASK_PROFILE));
209                         
210                         // ハッシュキー作成
211                         $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
212                         $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
213                         $this->tmpl->addVar("_widget", "ticket", $postTicket);                          // 画面に書き出し
214                 } else {
215                         if (!empty($memberId)){
216                                 // 各部の表示制御
217                                 $this->tmpl->setAttribute('name_area', 'visibility', 'visible');        // 名前表示
218                                 if ($showEmail) $this->tmpl->setAttribute('email_area', 'visibility', 'visible');
219                                 
220                                 // 値の埋め込み
221                                 $this->tmpl->addVar("name_area", "name", $name);                // ユーザ名
222                                 $this->tmpl->addVar("name_area", "mypage_url", $mypageUrl);                     // マイページURL
223                                 $this->tmpl->addVar("email_area", "email", $email);                     // Eメール
224                                 $urlStr = '';
225                                 if (!empty($url)) $urlStr = '<a href="' . $this->convertUrlToHtmlEntity($url) . '">' . $this->convertToDispString($url) . '</a>';
226                                 $this->tmpl->addVar("_widget", "url", $urlStr);         // URL
227                         }
228                 }
229                 
230                 $this->tmpl->addVar("_widget", "page_title", self::DEFAULT_PAGE_TITLE);         // ページタイトル
231                 
232                 $memberIdStr = '';
233                 if (!empty($mypageUrl)) $memberIdStr = '<a href="' . $mypageUrl . '">' . $this->convertToDispString($memberId) . '</a>';
234                 $this->tmpl->addVar("_widget", "id", $memberIdStr);             // マイブログページへのリンク
235                 
236                 // アバター画像
237                 $avatarImageUrl = $this->getAvatarUrl($memberId);// アバター画像URL
238                 $imageTag = '<img src="' . $this->getUrl($avatarImageUrl) . '" width="' . self::AVATAR_SIZE . '" height="' . self::AVATAR_SIZE .'" />';
239                 $this->tmpl->addVar("_widget", "avatar_img", $imageTag);                // 画像
240         }
241         /**
242          * 会員IDを作成
243          *
244          * @return string                               会員ID
245          */
246         function createMemberId()
247         {
248                 $memberId = '';
249                 
250                 for ($i = 0; $i < self::CREATE_CODE_RETRY_COUNT; $i++){
251                         // 「0」除くランダム文字列を作成
252                         $memberId = $this->_createRandString('123456789', self::MEMBER_ID_LENGTH);
253                 
254                         // すでに登録済みかどうかチェック
255                         $ret = $this->_db->isExistsMemberId($memberId);
256                         if (!$ret) break;
257                 }
258                 return $memberId;
259         }
260         /**
261          * 画像の種別を取得
262          *
263          * @param string $mime  MIMEコンテンツタイプ
264          * @return string               画像の種別
265          */
266         function getImageType($mime)
267         {
268                 if ($mime != ''){
269                         if ($mime == 'image/gif')       return 'gif';
270                         if ($mime == 'image/jpeg')      return 'jpeg';
271                         if ($mime == 'image/jpg')       return 'jpeg';
272                         if ($mime == 'image/pjpeg')     return 'jpeg';
273                         if ($mime == 'image/png')       return 'png';
274                 }
275                 return '';
276         }               
277         /**
278          * サムネールを作成
279          *
280          * @param string $type  MIMEコンテンツタイプ
281          * @param string $path  拡張子
282          * @param int $size             サムネールの縦横サイズ
283          * @return object               画像オブジェクト
284          */
285         function createThumb($type, $path, $size)
286         {
287                 // 画像作成
288                 switch ($type){
289                         case "jpeg":
290                                 $img = @imagecreatefromjpeg($path);
291                                 break;
292                         case "gif":
293                                 $img = @imagecreatefromgif($path);
294                                 break;
295                         case "png":
296                                 $img = @imagecreatefrompng($path);
297                                 break;
298                         default:
299                                 return false;
300                 }
301                 
302                 // size for thumbnail
303                 $width = imagesx($img);
304                 $height = imagesy($img);
305                 
306                 if ($width > $height){
307                         $n_height = $height * ($size / $width);
308                         $n_width = $size;
309                 } else {
310                         $n_width = $width * ($size / $height);
311                         $n_height = $size;
312                 }
313                 
314                 $x = 0;
315                 $y = 0;
316                 if ($n_width < $size) $x = round(($size - $n_width) / 2);
317                 if ($n_height < $size) $y = round(($size - $n_height) / 2);
318                 
319                 // imagecreatetruecolor
320                 $thumb = imagecreatetruecolor($size, $size);
321                 
322                 $bgcolor = imagecolorallocate($thumb, 255, 255, 255);
323                 imagefill($thumb, 0, 0, $bgcolor);
324                 
325                 // imagecopyresized (imagecopyresampled)
326                 if (function_exists("imagecopyresampled")){
327                         if (!imagecopyresampled($thumb, $img, $x, $y, 0, 0, $n_width, $n_height, $width, $height)){
328                                 if (!imagecopyresized($thumb, $img, $x, $y, 0, 0, $n_width, $n_height, $width, $height)) return false;
329                         }
330                 } else {
331                         if (!imagecopyresized($thumb, $img, $x, $y, 0, 0, $n_width, $n_height, $width, $height)) return false;
332                 }
333                 return $thumb;
334         }
335         /**
336          * サムネールを出力
337          *
338          * @param string $type  MIMEコンテンツタイプ
339          * @param object $image 画像オブジェクト
340          * @param string $path  ファイル保存の場合のパス
341          * @return bool                 true=成功、false=失敗
342          */
343         function outputThumb($type, &$image, $path = null)
344         {
345                 $ret = false;
346                 if (is_null($path)){
347                         switch ($type){
348                                 case "jpeg":
349                                         $ret = imagejpeg($image);
350                                         break;
351                                 case "gif":
352                                         $ret = imagegif($image);
353                                         break;
354                                 case "png":
355                                         $ret = imagepng($image);
356                                         break;
357                         }
358                 } else {
359                         switch ($type){
360                                 case "jpeg":
361                                         $ret = imagejpeg($image, $path);
362                                         break;
363                                 case "gif":
364                                         $ret = imagegif($image, $path);
365                                         break;
366                                 case "png":
367                                         $ret = imagepng($image, $path);
368                                         break;
369                         }
370                 }
371                 // イメージを破棄
372                 imagedestroy($image);
373                 
374                 return $ret;
375         }
376 }
377 ?>