OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / access_count / include / container / access_countWidgetContainer.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-2011 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: access_countWidgetContainer.php 4423 2011-11-07 05:31:29Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath()           . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath()     . '/access_countDb.php');
18
19 class access_countWidgetContainer extends BaseWidgetContainer
20 {
21         private $db;                    // DB接続オブジェクト
22     private $config = array();
23         private $isSystemManageUser;    // システム運用可能ユーザかどうか
24         private $isHiddenCounter;       // システム管理者、システム運用者の場合のみカウンターを表示するかどうか
25         
26         /**
27          * コンストラクタ
28          */
29         function __construct()
30         {
31                 // 親クラスを呼び出す
32                 parent::__construct();
33                 
34                 // ###### アクセスカウンターの表示設定 ######
35                 // 画像のURL
36                 $this->config['img'] = $this->getUrl($this->gEnv->getCurrentWidgetRootUrl()) . '/images/';
37         $this->config['animated_img'] = $this->config['img'];
38                 $this->config['animated_img_prefix'] = 'a';             // アニメーション画像ファイル用プレフィックス
39                 
40         // 表示桁数
41         $this->config['pad'] = 7;
42
43         // 画像のサイズ
44         $this->config['width']  = 16;
45         $this->config['height'] = 22;
46                 
47                 // DBオブジェクト作成
48                 $this->db = new access_countDb();
49         }
50         /**
51          * テンプレートファイルを設定
52          *
53          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
54          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
55          *
56          * @param RequestManager $request               HTTPリクエスト処理クラス
57          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
58          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
59          */
60         function _setTemplate($request, &$param)
61         {       
62                 return 'index.tmpl.html';
63         }
64         /**
65          * テンプレートにデータ埋め込む
66          *
67          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
68          *
69          * @param RequestManager $request               HTTPリクエスト処理クラス
70          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
71          * @return                                                              なし
72          */
73         function _assign($request, &$param)
74         {
75                 $this->isSystemManageUser = $this->gEnv->isSystemManageUser();  // システム運用可能ユーザかどうか
76                 
77                 // 表示設定取得
78                 $this->isHiddenCounter = 0;     // システム管理者、システム運用者の場合のみカウンターを表示するかどうか
79                 $paramObj = $this->getWidgetParamObj();
80                 if (!empty($paramObj)){
81                         $this->isHiddenCounter  = $paramObj->isHiddenCounter;
82                 }
83                         
84                 // 作成したHTMLを出力
85                 $numberList = $this->createOutput();
86                 if (empty($numberList)){                // データが空のときは出力をキャンセル
87                         $this->cancelParse();
88                 } else {
89                         $this->tmpl->addVar("_widget", "number_list", $numberList);
90                 }
91         }
92         /**
93          * 新規アクセスかどうかのチェック
94          *
95          * @return bool                                 true=新規アクセス、false=2回目以降アクセス
96          */
97     function isNewVisitor()
98         {
99                 // セッションIDを取得
100                 $ssid = session_id();
101                 
102                 // 登録済みかチェック
103                 $ret = $this->db->isSessionExists($ssid);
104                 if ($ret){              // レコードが存在しているときは、既存訪問者
105                         return false;
106                 } else {
107                         return true;
108                 }
109     }
110         /**
111          * 現在のカウントを取得
112          *
113          * @param bool         $up              新規のアクセスかどうか(true=新規アクセス、false=2回目以降アクセス)
114          * @return int                                  アクセス数
115          */
116     function readCounter(&$up)
117         {
118                 $date = date('Y-m-d');
119                 $up = false;            // 新規かどうか
120                 
121                 // カウンタを更新
122                 if (!$this->isSystemManageUser){        // システム管理者、システム運用者の場合はカウントしない
123                         // 1セッションに付き、一回だけカウンタを更新する
124                         // 新規セッションであれば、カウンタを更新
125                         if ($this->isNewVisitor()){
126                                 $this->db->incrementCount($date);
127                                 $up = true;
128                         }
129                 }
130                 $count = $this->db->getCount($date);
131                 return $count;
132     }
133         /**
134          * カウンタのHTMLを作成
135          *
136          * @return string                                       HTMLテキスト
137          */
138     function createOutput()
139         {
140                 // カウンター更新
141         $count = $this->readCounter($up);
142                 
143                 if (!empty($this->isHiddenCounter) && !$this->isSystemManageUser){              // システム管理者、システム運用者以外はカウンターを表示しない
144                         return '';
145                 } else {
146                         $countStr = sprintf("%0"."".$this->config['pad'].""."d", $count -1);    // 1つ前のカウント
147                 $destCountStr = sprintf("%0"."".$this->config['pad'].""."d", $count);   // 実際のカウント
148                         $html_output = '';
149                         if ($up){
150                                 for ($i = 0; $i < strlen($countStr); $i++){
151                                         if (substr($countStr, $i, 1) == substr($destCountStr, $i, 1)){
152                                                 $digit_pos = substr($countStr, $i, 1);
153                                                 $html_output .= "<img src=\"".$this->config['img']."$digit_pos.gif\"";
154                                         } else {
155                                                 $digit_pos = substr($destCountStr, $i, 1);
156                                                 $html_output .= "<img src=\"".$this->config['animated_img']. $this->config['animated_img_prefix'] . "$digit_pos.gif\"";
157                                         }
158                                         $html_output .= " width=\"".$this->config['width']."\" height=\"".$this->config['height']."\" style=\"border:0;margin:0;padding:0;\" />";
159                                 }
160                         } else {
161                                 for ($i = 0; $i < strlen($countStr); $i++){
162                                         $digit_pos = substr($destCountStr, $i, 1);
163                                         $html_output .= "<img src=\"".$this->config['img']."$digit_pos.gif\"";
164                                         $html_output .= " width=\"".$this->config['width']."\" height=\"".$this->config['height']."\" style=\"border:0;margin:0;padding:0;\" />";
165                                 }
166                         }
167                 return $html_output;
168                 }
169     }
170 }
171 ?>