[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / Controller / IndexingStatisticsController.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Controller;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * Module extension (addition to function menu) 'Indexed search statistics' for the 'indexed_search' extension.
20 *
21 * @author Dimitri Ebert <dimitri.ebert@dkd.de>
22 */
23 class IndexingStatisticsController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
24
25 /**
26 * Default constructor loads additional locallang
27 */
28 public function __construct() {
29 $GLOBALS['LANG']->includeLLFile('EXT:indexed_search/modfunc2/locallang.xlf');
30 }
31
32 /**
33 * Calls showStats to generate output.
34 *
35 * @return string html table with results from showStats()
36 * @todo Define visibility
37 */
38 public function main() {
39 // Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
40 $theOutput = $this->pObj->doc->header($GLOBALS['LANG']->getLL('title'));
41 $theOutput .= $this->pObj->doc->section('', $this->showStats(), 0, 1);
42 $menu = array();
43 $functionMenu = \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->pObj->id, 'SET[tx_indexedsearch_modfunc2_check]', $this->pObj->MOD_SETTINGS['tx_indexedsearch_modfunc2_check'], '', '', 'id="checkTx_indexedsearch_modfunc2_check"');
44 $menu[] = $functionMenu . '<label for="checkTx_indexedsearch_modfunc2_check"' . $GLOBALS['LANG']->getLL('checklabel') . '</label>';
45 $theOutput .= $this->pObj->doc->spacer(5);
46 return $theOutput;
47 }
48
49 /**
50 * Generates html table containing the statistics.
51 * Calls listSeveralStats 3 times, for all statistics, statistics of the last 30 days and statistics of the last 24 hours.
52 *
53 * @return string html table with results
54 * @todo Define visibility
55 */
56 public function showStats() {
57 $conf['words'] = 50;
58
59 // max words in result list
60 $conf['bid'] = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('id');
61
62 // pageid for several statistics
63 $addwhere1 = '';
64
65 // all records
66 $addwhere2 = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 30 * 24 * 60 * 60);
67
68 // last 30 days
69 $addwhere3 = ' AND tstamp > ' . ($GLOBALS['EXEC_TIME'] - 24 * 60 * 60);
70
71 // last 24 hours
72 $content = '<p class="lead">' . $GLOBALS['LANG']->getLL('title2') . '</p>';
73 $content .= '<table cellpading="5" cellspacing="5" valign="top" class=""><tr><td valign="top">' . $this->listSeveralStats($GLOBALS['LANG']->getLL('all'), $addwhere1, $conf) . '</td><td valign="top">' . $this->listSeveralStats($GLOBALS['LANG']->getLL('last30days'), $addwhere2, $conf) . '</td><td valign="top">' . $this->listSeveralStats($GLOBALS['LANG']->getLL('last24hours'), $addwhere3, $conf) . '</td></tr></table>';
74 $content .= '<p>' . $this->note . '</p>';
75
76 // Ask hook to include more on the page:
77 if ($hookObj = $this->hookRequest('additionalSearchStat')) {
78 $content .= $hookObj->additionalSearchStat();
79 }
80 return $content;
81 }
82
83 /**
84 * Generates html table with title and several statistics
85 *
86 * @param string title for statistic, like 'Last 30 days' or 'Last 24 hours'
87 * @param string add where for sql query
88 * @param array configuration: words = max words for results, bid = pageid
89 * @return string html table with results
90 * @todo Define visibility
91 */
92 public function listSeveralStats($title, $addwhere, $conf) {
93 global $LANG;
94 $queryParts['SELECT'] = 'word, COUNT(*) AS c';
95 $queryParts['FROM'] = 'index_stat_word';
96 $queryParts['WHERE'] = sprintf('pageid= %d ' . $addwhere, $conf['bid']);
97 $queryParts['GROUPBY'] = 'word';
98 $queryParts['ORDERBY'] = 'c DESC,word';
99 $queryParts['LIMIT'] = $conf['words'];
100 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($queryParts['SELECT'], $queryParts['FROM'], $queryParts['WHERE'], $queryParts['GROUPBY'], $queryParts['ORDERBY'], $queryParts['LIMIT']);
101 if ($res) {
102 $count = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
103 } else {
104 $count = 0;
105 }
106 // exist several statistics for this page?
107 if ($count > 0) {
108 $this->note = $LANG->getLL('justthispage');
109 } else {
110 // Limit access to pages of the current site
111 $secureaddwhere = ' AND pageid IN (' . $this->extGetTreeList($conf['bid'], 100, 0, '1=1') . $conf['bid'] . ') ';
112 $this->note = $LANG->getLL('allpages');
113 $queryParts['WHERE'] = '1=1 ' . $addwhere . $secureaddwhere;
114 }
115 // make real query
116 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($queryParts['SELECT'], $queryParts['FROM'], $queryParts['WHERE'], $queryParts['GROUPBY'], $queryParts['ORDERBY'], $queryParts['LIMIT']);
117 $table1 = '';
118 $i = 0;
119 if ($res) {
120 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
121 $i++;
122 $table1 .= '<tr class="bgColor4"><td>' . $i . '.</td><td>' . htmlspecialchars($row['word']) . '</td><td>&nbsp;&nbsp;' . $row['c'] . '</td></tr>';
123 }
124 }
125 if ($i == 0) {
126 $table1 = '<tr class="bgColor4"><td callspan="3">' . $LANG->getLL('noresults') . '</td></tr>';
127 }
128 $table1 = '<table class="bgColor5" cellpadding="2" cellspacing="1"><tr class="tableheader"><td colspan="3">' . $title . '</td></tr>' . $table1 . '</table>';
129 return $note . $table1;
130 }
131
132 /**
133 * Calls \TYPO3\CMS\Backend\FrontendBackendUserAuthentication::extGetTreeList.
134 * Although this duplicates the function \TYPO3\CMS\Backend\FrontendBackendUserAuthentication::extGetTreeList
135 * this is necessary to create the object that is used recursively by the original function.
136 *
137 * Generates a list of Page-uid's from $id. List does not include $id itself
138 * The only pages excluded from the list are deleted pages.
139 *
140 * @param integer Start page id
141 * @param integer Depth to traverse down the page tree.
142 * @param integer $begin is an optional integer that determines at which level in the tree to start collecting uid's. Zero means 'start right away', 1 = 'next level and out'
143 * @param string Perms clause
144 * @return string Returns the list with a comma in the end (if any pages selected!)
145 * @todo Define visibility
146 */
147 public function extGetTreeList($id, $depth, $begin = 0, $perms_clause) {
148 return GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\FrontendBackendUserAuthentication')->extGetTreeList($id, $depth, $begin, $perms_clause);
149 }
150
151 /**
152 * Returns an object reference to the hook object if any
153 *
154 * @param string Name of the function you want to call / hook key
155 * @return object Hook object, if any. Otherwise NULL.
156 * @author Kasper Skårhøj
157 * @todo Define visibility
158 */
159 public function hookRequest($functionName) {
160 // Hook: menuConfig_preProcessModMenu
161 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['be_hooks'][$functionName]) {
162 $hookObj = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['be_hooks'][$functionName]);
163 if (method_exists($hookObj, $functionName)) {
164 $hookObj->pObj = $this;
165 return $hookObj;
166 }
167 }
168 }
169
170 }