7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getContainerPath() . '/baseAdminWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/admin_analyticsDb.php');
19 class admin_analyticsWidgetContainer extends BaseAdminWidgetContainer
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'; // ラインデータ変数名ヘッダ
46 function __construct()
49 parent::__construct();
52 $this->db = new admin_analyticsDb();
55 $this->graphTypeArray = array( array( 'name' => 'ページビュー', 'value' => 'pageview'),
56 array( 'name' => '訪問数', 'value' => 'visit'),
57 array( 'name' => '訪問者数', 'value' => 'visitor'));
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));
66 $this->yTickValueArray = array(1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 0);
71 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
72 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
74 * @param RequestManager $request HTTPリクエスト処理クラス
75 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
76 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
78 function _setTemplate($request, &$param)
80 return 'index.tmpl.html';
85 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
87 * @param RequestManager $request HTTPリクエスト処理クラス
88 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
91 function _assign($request, &$param)
94 $this->graphType = self::DEFAULT_GRAPH_TYPE;// グラフ種別
95 $paramObj = $this->getWidgetParamObj();
96 if (empty($paramObj)){ // 既存データなしのとき
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; // グラフ高さ
103 $this->path = $paramObj->path; // アクセスパス
104 $this->termType = $paramObj->termType; // 期間タイプ
105 $graphWidth = $paramObj->graphWidth; // グラフ幅
106 $graphHeight = $paramObj->graphHeight; // グラフ高さ
109 $this->pathArray = array();
110 $ret = $this->db->getAnalyticsAccessPoint($rows);
112 for ($i = 0; $i < count($rows); $i++){
113 $this->pathArray[] = $rows[$i]['pg_path']; // アクセスパス
117 $act = $request->trimValueOf('act');
118 if ($act == 'analytics_update'){ // 設定更新(再計算)のとき
119 $messageArray = array();
120 $ret = $this->gInstance->getAnalyzeManager()->updateAnalyticsData($messageArray);
122 $this->setMsg(self::MSG_GUIDANCE, $messageArray[0]);
124 $this->setMsg(self::MSG_APP_ERR, $messageArray[0]);
128 $showGraph = false; // グラフを表示するかどうか
129 $lineDataScript = ''; // ラインデータスクリプト
130 $lineParam = ''; // ラインパラメータ
133 $endDate = $this->db->getStatus(self::CF_LAST_DATE_CALC_PV);
134 if (empty($endDate)){ // 集計処理が一度も行われていないとき
137 $lineDataScript .= M3_INDENT_SPACE . 'var ' . self::LINE_DATA_HEAD . ' = [[]];'; // ラインデータ
138 $lineParam .= self::LINE_DATA_HEAD;
139 $showGraph = true; // グラフを表示
142 $ret = $this->db->getOldAccessLog($row);
143 if ($ret){ // 集計対象のデータが存在するとき
144 $logStartDate = date("Y-m-d", strtotime($row['al_dt']));
146 switch ($this->termType){
148 $startDate = date("Y-m-d", strtotime("$endDate -30 day")); // 30日前
151 $startDate = date("Y-m-1", strtotime("$endDate -3 month")); // 3ヶ月前
154 $startDate = date("Y-m-1", strtotime("$endDate -6 month")); // 6ヶ月前
157 $startDate = date("Y-m-1", strtotime("$endDate -1 year")); // 1年前
159 case self::TERM_TYPE_ALL: // すべてのデータのとき
163 if (!is_null($startDate)){
165 if (strtotime($startDate) < strtotime($logStartDate)) $startDate = $logStartDate;
170 for ($i = 0; $i < count($this->pathArray); $i++){
172 $pathParam = $this->pathArray[$i];
175 $this->graphDataStr = ''; // グラフデータ
176 if (is_null($startDate)){
177 $this->completedDate = '';
179 $this->completedDate = date("Y-m-d", strtotime("$startDate -1 day"));// 処理終了日付を前日に設定
182 // ##### グラフ種別に応じてデータ取得 #####
183 switch ($this->graphType){
184 case 'pageview': //ページビュー
186 $this->db->getPageViewByDate($pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
188 // case 'visit': // 訪問数
189 // $this->db->getDailyCountByDate(0/*訪問数*/, $pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
191 // case 'visitor': // 訪問者数
192 // $this->db->getDailyCountByDate(1/*訪問者数*/, $pathParam, $startDate, $endDate, array($this, 'pageViewDataLoop'));
196 $nextStartDate = date("Y-m-d", strtotime("$this->completedDate 1 day")); // 処理終了日翌日
197 $this->createGraphData($nextStartDate, $endDate);
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 . ','; // ラインパラメータ
205 for ($i = 0; $i < count($this->yTickValueArray) -1; $i++){
206 if ($this->maxPv >= $this->yTickValueArray[$i + 1]){
207 $yMax = $this->yTickValueArray[$i];// Y座標最大値
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'); // グラフ非表示
219 if (empty($endDate)){
220 $lastData = self::DEFAULT_STR_NOT_CALC;
222 $lastData = $this->convertToDispDate($endDate); // 最終集計日
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);// グラフ高さ
234 $libInfo = $this->gPage->getScriptLibInfo(self::LIB_JQPLOT);
235 if (!empty($libInfo)) $libDir = $libInfo['dir'];
236 $this->tmpl->addVar("_widget", "lib_dir", $libDir);
241 * @param int $index 行番号(0~)
242 * @param array $fetchedRow フェッチ取得した行
243 * @param object $param 未使用
244 * @return bool true=処理続行の場合、false=処理終了の場合
246 function pageViewDataLoop($index, $fetchedRow, $param)
249 switch ($this->graphType){
250 case 'pageview': //ページビュー
251 $pvDate = $fetchedRow['ap_date'];
254 case 'visitor': // 訪問者数
255 $pvDate = $fetchedRow['aa_date'];
258 $endDate = date("Y-m-d", strtotime("$pvDate -1 day")); // 1日前
259 $total = $fetchedRow['total'];
260 if ($total > $this->maxPv) $this->maxPv = $total; // 日次のページビュー最大値
263 if (!is_null($this->completedDate)){
264 $date = date("Y-m-d", strtotime("$this->completedDate 1 day"));
267 if (strtotime($date) > strtotime($endDate)) break;
270 $this->graphDataStr .= '[\'' . $date . '\',0],';
271 $date = date("Y-m-d", strtotime("$date 1 day"));
276 $this->graphDataStr .= '[\'' . $pvDate . '\',' . $total . '],';
279 $this->completedDate = $pvDate;
283 * 集計終了日までのグラフデータを作成する
285 * @param date $startDate 開始日
286 * @param date $endDate 終了日
289 function createGraphData($startDate, $endDate)
293 if (strtotime($date) > strtotime($endDate)) break;
296 $this->graphDataStr .= '[\'' . $date . '\',0],';
297 $date = date("Y-m-d", strtotime("$date 1 day"));