This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbibleactivitycron.php
332 lines (309 loc) · 11.3 KB
/
getbibleactivitycron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
*
* @version 1.0.7 January 16, 2015
* @package Get Bible API
* @author Llewellyn van der Merwe <[email protected]>
* @copyright Copyright (C) 2013 Vast Development Method <http://www.vdm.io>
* @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
*
**/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
jimport('joomla.application.component.helper');
// Added for Joomla 3.0
if(!defined('DS')){
define('DS',DIRECTORY_SEPARATOR);
};
/**
* Get Bible Activity Cron Plugin
*/
class plgSystemGetBibleActivityCron extends JPlugin
{
protected $document;
protected $com_params;
protected function canRun() {
if (file_exists(JPATH_ADMINISTRATOR.'/components/com_getbible/helpers/activityCron.php')) {
require_once JPATH_ADMINISTRATOR.'/components/com_getbible/helpers/activityCron.php';
// get timer
$timer = $this->params->get('timer', '-1 day');
if(GetBibleActivityCron::canRun($timer)){
return true;
}
}
return false;
}
public function onAfterDispatch() {
if (!$this->canRun()) {
return;
}
// run the cron job
GetBibleActivityCron::cronJob();
}
/**
* Event Triggered in Back-end [on Before cPanel display]
*/
public function getbible_bk_onBefore_cPanel_display($array)
{
// set the return url
$uri = (string) JUri::getInstance();
$return = urlencode(base64_encode($uri));
// load language
JFactory::getLanguage()->load('plg_system_getbibleactivitycron', JPATH_ADMINISTRATOR);
// get Bible Parameters
$this->com_params = &JComponentHelper::getParams('com_getbible');
// get plugin id
$pluginId = $this->pluginId('plg_system_getbibleactivitycron','plugin','getbibleactivitycron','system');
if($pluginId){
$pluginUrl = JURI::base().'index.php?option=com_plugins&task=plugin.edit&extension_id='.(int) $pluginId;
} else {
$pluginUrl = JURI::base().'index.php?option=com_plugins&view=plugins&filter_search=System - getBible Activity Cron';
}
// set the ip update tab
$div = '<div class="span12"><h2>Activity Cron Job!</h2><div class="well well-small">';
$div .= '<h2 class="nav-header">'.$this->lastActivityUpdate().'</h2>';
$div .= '<p>The cron job is set to update your activity table <code>'.JText::_($this->updaterText('timer')).'</code>
To change the timer <a href="'.$pluginUrl.'" >click here</a>.</p>';
// First check user access
$canDo = JHelperContent::getActions('com_getbible', 'getbible');
if($this->com_params->get('log') == 0 && $canDo->get('core.admin')){
$div .= '<h3>You must turn logging on in the "Global Settings" tab on the Component <a href="index.php?option=com_config&view=component&component=com_getbible&path=&return='.$return.'">Options</a> page.</h3>';
$div .= '<p>Failure to doing so will prevent these activity charts from being accurate and up to date.</p>';
}
$div .= '</div>';
$script = '(function($) {
var $event = $.event,
$special,
resizeTimeout;
$special = $event.special.debouncedresize = {
setup: function() {
$( this ).on( "resize", $special.handler );
},
teardown: function() {
$( this ).off( "resize", $special.handler );
},
handler: function( event, execAsap ) {
// Save the context
var context = this,
args = arguments,
dispatch = function() {
// set correct event type
event.type = "debouncedresize";
$event.dispatch.apply( context, args );
};
if ( resizeTimeout ) {
clearTimeout( resizeTimeout );
}
execAsap ?
dispatch() :
resizeTimeout = setTimeout( dispatch, $special.threshold );
},
threshold: 150
};
})(jQuery);';
$data = $this->getData();
if($data['country']){
$div .= '<h2>Country Chart</h2>';
$div .= '<div class="well well-small">';
$div .= '<p>The total API queries made per country.</p>';
$div .= '<div class="well" style="background-color:#fff;">';
$div .= '<div id="regions_div" style="height: 500px;"></div>';
$div .= '</div></div>';
// setup the needed script
$script .= "google.load('visualization', '1', {'packages': ['geochart']});";
$script .= 'google.setOnLoadCallback(drawRegionsMap);';
$script .= "jQuery(document).ready(function() {
jQuery('a[data-toggle=\"tab\"]').on('shown.bs.tab', function (e) {
drawRegionsMap();
});
jQuery(window).bind('debouncedresize', function() {
drawRegionsMap();
});
});";
$script .= 'function drawRegionsMap() {';
$script .= 'var data = google.visualization.arrayToDataTable([';
$script .= "['Country', 'Queries']";
foreach($data['country'] as $country){
if(strlen($country->name) > 0){
$script.= ",['".addslashes($country->name)."', ".$country->nr."]";
}
}
$script .= ']);';
$script .= "var options = { displayMode: 'regions', minValue: 0, width: '100%', height: '100%', colors: ['#44C479', '#047232']};";
$script .= "var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));";
$script .= "chart.draw(data, options);}";
}
if($data['version']){
$div .= '<h2>Version Chart</h2>';
$div .= '<div class="well well-small">';
$div .= '<p>The total API queries made per version.</p>';
$div .= '<div class="well" style="background-color:#fff;">';
$div .= '<div id="versions_div" style="height: 500px;"></div>';
$div .= '</div></div>';
// setup the needed script
$script .= 'google.load("visualization", "1", {packages:["corechart"]});';
$script .= 'google.setOnLoadCallback(drawVersions);';
$script .= "jQuery(document).ready(function() {
jQuery('a[data-toggle=\"tab\"]').on('shown.bs.tab', function (e) {
drawVersions();
});
jQuery(window).bind('debouncedresize', function() {
drawVersions();
});
});";
$script .= 'function drawVersions() {';
$script .= 'var data = google.visualization.arrayToDataTable([';
$script .= "['Version', 'Query']";
foreach($data['version'] as $version){
if(strlen($version->name) > 0){
$script .= ",['".addslashes($version->name)."', ".$version->nr."]";
}
}
$script .= ']);';
$script .= "var options = { is3D: true };";
$script .= "var chart = new google.visualization.PieChart(document.getElementById('versions_div'));";
$script .= "chart.draw(data, options);}";
}
$div .= '</div>';
// Set Activity Tab
$array[4]->div = $div;
// load the script into the document
if(count($data['country']) || count($data['version'])){
// get the document
$this->document = &JFactory::getDocument();
// add the Javascript to page
if (!$this->js_loaded('jquery')) {
JHtml::_('jquery.framework');
}
$this->document->addScript('https://www.google.com/jsapi');
$this->document->addScriptDeclaration($script);
}
}
protected function getData()
{
if (file_exists(JPATH_ADMINISTRATOR.'/components/com_getbible/helpers/activityCron.php')) {
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('b.name', 'a.counter', 'a.country'),array('name', 'nr', 'code')))
->from($db->quoteName('#__getbible_activity_country', 'a'))
->join('INNER', $db->quoteName('#__ipdata_country', 'b') . ' ON (' . $db->quoteName('a.country') . ' = ' . $db->quoteName('b.codethree') . ')')
->order($db->quoteName('a.counter') . ' DESC');
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
$results['country'] = $db->loadObjectList();
} else {
$results['country'] = false;
}
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('b.name', 'a.counter'),array('name', 'nr')))
->from($db->quoteName('#__getbible_activity_version', 'a'))
->join('INNER', $db->quoteName('#__getbible_versions', 'b') . ' ON (' . $db->quoteName('a.version') . ' = ' . $db->quoteName('b.version') . ')')
->order($db->quoteName('a.counter') . ' DESC');
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
$results['version'] = $db->loadObjectList();
} else {
$results['version'] = false;
}
return $results;
}
return false;
}
protected function updaterText($type)
{
switch($this->params->get($type.'timer', '-1 day')){
case "-1 hour":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_ONCE_A_HOUR';
break;
case "-5 hours":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_EVERY_FIVE_HOURS';
break;
case "-12 hours":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_EVERY_TWELVE_HOURS';
break;
case "-1 day":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_ONCE_A_DAY';
break;
case "-2 day":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_SECOND_DAY';
break;
case "-5 day":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_FIFTH_DAY';
break;
case "-7 day":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_ONCE_A_WEEK';
break;
case "0":
return 'PLG_SYSTEM_GETBIBLEACTIVITYCRON_CONFIG_NEVER';
break;
}
}
protected function lastActivityUpdate()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('#__getbible_activity_cron.date');
$query->from('#__getbible_activity_cron');
$query->where('#__getbible_activity_cron.active = 1');
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
return 'The last update was: '.$db->loadResult().' <i>(no update pending)</i>';
}
$query = $db->getQuery(true);
$query->select('#__getbible_activity_cron.date');
$query->from('#__getbible_activity_cron');
$query->where('#__getbible_activity_cron.active = 8');
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
return 'An update has started at: '.$db->loadResult();
}
$query = $db->getQuery(true);
$query->select('#__getbible_activity_cron.date');
$query->from('#__getbible_activity_cron');
$query->where('#__getbible_activity_cron.active = 0');
$query->order('#__getbible_activity_cron.date DESC');
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
return 'The last update was: '.$db->loadResult().' <i>(update pending)</i>';
}
return 'No Update Yet!';
}
protected function js_loaded($script_name)
{
$head_data = $this->document->getHeadData();
foreach (array_keys($head_data['scripts']) as $script) {
if (stristr($script, $script_name)) {
return true;
}
}
return false;
}
protected function pluginId($name,$type,$element,$folder)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select($db->quoteName('a.extension_id'))
->from($db->quoteName('#__extensions', 'a'))
->where($db->quoteName('a.name').' = '.$db->quote($name))
->where($db->quoteName('a.type').' = '.$db->quote($type))
->where($db->quoteName('a.element').' = '.$db->quote($element))
->where($db->quoteName('a.folder').' = '.$db->quote($folder));
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
return $db->loadResult();
}
return false;
}
}