OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / admin / analytics / include / container / admin_analyticsWidgetContainer.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-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: admin_analyticsWidgetContainer.php 5807 2013-03-08 05:18:13Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_analyticsDb.php');
18
19 class admin_analyticsWidgetContainer extends BaseAdminWidgetContainer
20 {
21         private $db;    // DB接続オブジェクト
22         private $termTypeArray;         // 期間タイプ
23         private $graphType;                     // グラフ種別
24         private $path;                          // アクセスパス
25         private $pathArray;                             // アクセスパス
26         private $termType;                              // 期間タイプ
27         private $completedDate;                 // 処理終了日付
28         private $graphDataStr;          // グラフデータ
29         private $maxPv = 0;                             // 日次のページビュー最大値
30         private $yTickValueArray;               // Y軸の最大値
31         const CF_LAST_DATE_CALC_PV      = 'last_date_calc_pv';  // ページビュー集計の最終更新日
32         const DEFAULT_STR_NOT_CALC = '未集計';               // 未集計時の表示文字列
33         const DEFAULT_ACCESS_PATH = 'index';            // デフォルトのアクセスパス(PC用アクセスポイント)
34         const ACCESS_PATH_ALL = '_all';                         // アクセスパスすべて選択
35         const DEFAULT_TERM_TYPE = '30day';              // デフォルトの期間タイプ
36         const TERM_TYPE_ALL = '_all';                           // 全データ表示選択
37         const DEFAULT_GRAPH_TYPE = 'pageview';          // デフォルトのグラフ種別
38         const DEFAULT_GRAPH_WIDTH = 800;                // グラフ幅
39         const DEFAULT_GRAPH_HEIGHT = 320;               // グラフ高さ
40         const LIB_JQPLOT = 'jquery.jqplot';             // ライブラリID
41         const LINE_DATA_HEAD = 'line';                  // ラインデータ変数名ヘッダ
42                         
43         /**
44          * コンストラクタ
45          */
46         function __construct()
47         {
48                 // 親クラスを呼び出す
49                 parent::__construct();
50                 
51                 // DBオブジェクト作成
52                 $this->db = new admin_analyticsDb();
53                 
54                 // グラフタイプ
55                 $this->graphTypeArray = array(  array(  'name' => 'ページビュー', 'value' => 'pageview'),
56                                                                                 array(  'name' => '訪問数',                  'value' => 'visit'),
57                                                                                 array(  'name' => '訪問者数',               'value' => 'visitor'));
58                                                                                 
59                 // 期間タイプ
60                 $this->termTypeArray = array(   array(  'name' => '30日',      'value' => '30day'),
61                                                                                 array(  'name' => '3ヶ月',    'value' => '3month'),
62                                                                                 array(  'name' => '6ヶ月',    'value' => '6month'),
63                                                                                 array(  'name' => '1年',       'value' => '1year'),
64                                                                                 array(  'name' => 'すべて',  'value' => self::TERM_TYPE_ALL));
65                 // Y軸の最大値
66                 $this->yTickValueArray = array(1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 0);
67         }
68         /**
69          * テンプレートファイルを設定
70          *
71          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
72          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
73          *
74          * @param RequestManager $request               HTTPリクエスト処理クラス
75          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
76          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
77          */
78         function _setTemplate($request, &$param)
79         {
80                 return 'index.tmpl.html';
81         }
82         /**
83          * テンプレートにデータ埋め込む
84          *
85          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
86          *
87          * @param RequestManager $request               HTTPリクエスト処理クラス
88          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
89          * @param                                                               なし
90          */
91         function _assign($request, &$param)
92         {
93                 // グラフ用のパラメータを取得
94                 $this->graphType = self::DEFAULT_GRAPH_TYPE;// グラフ種別
95                 $paramObj = $this->getWidgetParamObj();
96                 if (empty($paramObj)){          // 既存データなしのとき
97                         // デフォルト値設定
98                         $this->path = self::DEFAULT_ACCESS_PATH;
99                         $this->termType = self::DEFAULT_TERM_TYPE;
100                         $graphWidth = self::DEFAULT_GRAPH_WIDTH;                // グラフ幅
101                         $graphHeight = self::DEFAULT_GRAPH_HEIGHT;              // グラフ高さ
102                 } else {
103                         $this->path = $paramObj->path;                          // アクセスパス
104                         $this->termType = $paramObj->termType;          // 期間タイプ
105                         $graphWidth = $paramObj->graphWidth;            // グラフ幅
106                         $graphHeight = $paramObj->graphHeight;          // グラフ高さ
107                 }
108                 // アクセスポイントパスを取得
109                 $this->pathArray = array();
110                 $ret = $this->db->getAnalyticsAccessPoint($rows);
111                 if ($ret){
112                         for ($i = 0; $i < count($rows); $i++){
113                                 $this->pathArray[] = $rows[$i]['pg_path'];                              // アクセスパス
114                         }
115                 }
116                 
117                 $act = $request->trimValueOf('act');
118                 if ($act == 'analytics_update'){                // 設定更新(再計算)のとき
119                         $messageArray = array();
120                         $ret = $this->gInstance->getAnalyzeManager()->updateAnalyticsData($messageArray);
121                         if ($ret){
122                                 $this->setMsg(self::MSG_GUIDANCE, $messageArray[0]);
123                         } else {
124                                 $this->setMsg(self::MSG_APP_ERR, $messageArray[0]);
125                         }
126                 }
127                 
128                 $showGraph = false;             // グラフを表示するかどうか
129                 $lineDataScript = '';   // ラインデータスクリプト
130                 $lineParam = '';                // ラインパラメータ
131                 
132                 // 出力期間を取得
133                 $endDate = $this->db->getStatus(self::CF_LAST_DATE_CALC_PV);
134                 if (empty($endDate)){           // 集計処理が一度も行われていないとき
135                         // 空のグラフを作成
136                         $yMax = 100;
137                         $lineDataScript .= M3_INDENT_SPACE . 'var ' . self::LINE_DATA_HEAD . ' = [[]];';                // ラインデータ
138                         $lineParam .= self::LINE_DATA_HEAD;
139                         $showGraph = true;              // グラフを表示
140                 } else {
141                         // データの先頭の日付を求める
142                         $ret = $this->db->getOldAccessLog($row);
143                         if ($ret){              // 集計対象のデータが存在するとき
144                                 $logStartDate = date("Y-m-d", strtotime($row['al_dt']));
145                         }
146                         switch ($this->termType){
147                                 case '30day':
148                                         $startDate = date("Y-m-d", strtotime("$endDate -30 day"));                      // 30日前
149                                         break;
150                                 case '3month':
151                                         $startDate = date("Y-m-1", strtotime("$endDate -3 month"));             // 3ヶ月前
152                                         break;
153                                 case '6month':
154                                         $startDate = date("Y-m-1", strtotime("$endDate -6 month"));             // 6ヶ月前
155                                         break;
156                                 case '1year':
157                                         $startDate = date("Y-m-1", strtotime("$endDate -1 year"));                      // 1年前
158                                         break;
159                                 case self::TERM_TYPE_ALL:               // すべてのデータのとき
160                                         $startDate = NULL;
161                                         break;
162                         }
163                         if (!is_null($startDate)){
164                                 // 先頭の日付を修正
165                                 if (strtotime($startDate) < strtotime($logStartDate)) $startDate = $logStartDate;
166                         }
167                                 
168                         // グラフデータ作成
169                         $this->maxPv = 0;
170                         for ($i = 0; $i < count($this->pathArray); $i++){
171                                 // パスパラメータ作成
172                                 $pathParam = $this->pathArray[$i];
173                         
174                                 // データの先頭日付を修正
175                                 $this->graphDataStr = '';               // グラフデータ
176                                 if (is_null($startDate)){
177                                         $this->completedDate = '';
178                                 } else {
179                                         $this->completedDate = date("Y-m-d", strtotime("$startDate -1 day"));// 処理終了日付を前日に設定
180                                 }
181
182                                 // ##### グラフ種別に応じてデータ取得 #####
183                                 switch ($this->graphType){
184                                         case 'pageview':                //ページビュー
185                                                 // データ取得
186                                                 $this->db->getPageViewByDate($pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
187                                                 break;
188 //                                      case 'visit':                   // 訪問数
189 //                                              $this->db->getDailyCountByDate(0/*訪問数*/, $pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
190 //                                              break;
191 //                                      case 'visitor':                 // 訪問者数
192 //                                              $this->db->getDailyCountByDate(1/*訪問者数*/, $pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
193 //                                              break;
194                                 }
195                                 // 集計終了日までのデータを作成
196                                 $nextStartDate = date("Y-m-d", strtotime("$this->completedDate 1 day"));                // 処理終了日翌日
197                                 $this->createGraphData($nextStartDate, $endDate);
198                                         
199                                 $this->graphDataStr = trim($this->graphDataStr, ',');           // グラフデータ
200                                 if (!empty($this->graphDataStr)) $showGraph = true;             // グラフを表示
201                                 $lineDataScript .= M3_INDENT_SPACE . 'var ' . self::LINE_DATA_HEAD . $i . ' = [' . $this->graphDataStr . '];' . M3_NL;
202                                 $lineParam .= self::LINE_DATA_HEAD . $i . ',';  // ラインパラメータ
203                         }
204                         // グラフY座標最大値取得
205                         for ($i = 0; $i < count($this->yTickValueArray) -1; $i++){
206                                 if ($this->maxPv >= $this->yTickValueArray[$i + 1]){
207                                         $yMax = $this->yTickValueArray[$i];// Y座標最大値
208                                         break;
209                                 }
210                         }
211                 }
212                 // グラフ表示制御
213                 if (!$showGraph){       // グラフ非表示の場合
214                         $this->setMsg(self::MSG_GUIDANCE, '集計済みのデータがありません');
215                         $this->tmpl->setAttribute('show_graph', 'visibility', 'hidden');                // グラフ非表示
216                         $this->tmpl->setAttribute('draw_graph', 'visibility', 'hidden');                // グラフ非表示
217                 }
218                 
219                 if (empty($endDate)){
220                         $lastData = self::DEFAULT_STR_NOT_CALC;
221                 } else {
222                         $lastData = $this->convertToDispDate($endDate);         // 最終集計日
223                 }
224                 // 値を埋め込む
225                 $this->tmpl->addVar("draw_graph", "line_data", $lineDataScript);        // ラインデータスクリプト
226                 $this->tmpl->addVar("draw_graph", "line_param", trim($lineParam, ',')); // ラインパラメータ
227                 $this->tmpl->addVar("draw_graph", "y_max", $yMax);              // グラフY座標最大値
228                 $this->tmpl->addVar("_widget", "date", $lastData);// 最終集計日
229                 $this->tmpl->addVar("show_graph", "graph_width", $graphWidth);// グラフ幅
230                 $this->tmpl->addVar("show_graph", "graph_height", $graphHeight);// グラフ高さ
231                 
232                 // ライブラリパス
233                 $libDir = '';
234                 $libInfo = $this->gPage->getScriptLibInfo(self::LIB_JQPLOT);
235                 if (!empty($libInfo)) $libDir = $libInfo['dir'];
236                 $this->tmpl->addVar("_widget", "lib_dir", $libDir);
237         }
238         /**
239          * ページビューデータを取得
240          *
241          * @param int $index                    行番号(0~)
242          * @param array $fetchedRow             フェッチ取得した行
243          * @param object $param                 未使用
244          * @return bool                                 true=処理続行の場合、false=処理終了の場合
245          */
246         function pageViewDataLoop($index, $fetchedRow, $param)
247         {
248                 // 日付を取得
249                 switch ($this->graphType){
250                         case 'pageview':                //ページビュー
251                                 $pvDate = $fetchedRow['ap_date'];
252                                 break;
253                         case 'visit':                   // 訪問数
254                         case 'visitor':                 // 訪問者数
255                                 $pvDate = $fetchedRow['aa_date'];
256                                 break;
257                 }
258                 $endDate = date("Y-m-d", strtotime("$pvDate -1 day"));          // 1日前
259                 $total = $fetchedRow['total'];
260                 if ($total > $this->maxPv) $this->maxPv = $total;                               // 日次のページビュー最大値
261                 
262                 // 0データの期間埋める
263                 if (!is_null($this->completedDate)){
264                         $date = date("Y-m-d", strtotime("$this->completedDate 1 day"));
265
266                         while (true){
267                                 if (strtotime($date) > strtotime($endDate)) break;
268
269                                 // グラフ用のデータ作成
270                                 $this->graphDataStr .= '[\'' . $date . '\',0],';
271                                 $date = date("Y-m-d", strtotime("$date 1 day"));
272                         }
273                 }
274                 
275                 // グラフ用のデータ作成
276                 $this->graphDataStr .= '[\'' . $pvDate . '\',' . $total . '],';
277
278                 // 処理終了日付を更新
279                 $this->completedDate = $pvDate;
280                 return true;
281         }
282         /**
283          * 集計終了日までのグラフデータを作成する
284          *
285          * @param date   $startDate             開始日
286          * @param date   $endDate               終了日
287          * @return                                              なし
288          */
289         function createGraphData($startDate, $endDate)
290         {
291                 $date = $startDate;
292                 while (true){
293                         if (strtotime($date) > strtotime($endDate)) break;
294
295                         // グラフ用のデータ作成
296                         $this->graphDataStr .= '[\'' . $date . '\',0],';
297                         $date = date("Y-m-d", strtotime("$date 1 day"));
298                 }
299         }
300 }
301 ?>