forked from BabDev/Tweet-Display-Back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.php
155 lines (134 loc) · 3.85 KB
/
version.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
<?php
/**
* Tweet Display Back Module for Joomla!
*
* @package TweetDisplayBack
*
* @copyright Copyright (C) 2010-2013 Michael Babker. All rights reserved.
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
*/
defined('_JEXEC') or die;
JLoader::register('ModTweetDisplayBackHelper', dirname(__DIR__) . '/helper.php');
/**
* Field type to display the version and check for a newer version.
*
* @package TweetDisplayBack
* @since 2.1
*/
class JFormFieldVersion extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 2.1
*/
protected $type = 'Version';
/**
* Method to get the field input.
*
* @return string
*
* @since 2.1
*/
protected function getInput()
{
return '';
}
/**
* Method to get the field label.
*
* @return string A message containing the installed version and,
* if necessary, information on a new version.
*
* @since 2.1
*/
protected function getLabel()
{
// Get the module's XML
$xmlfile = dirname(__DIR__) . '/mod_tweetdisplayback.xml';
$data = JApplicationHelper::parseXMLInstallFile($xmlfile);
$cacheFile = JPATH_CACHE . '/tweetdisplayback_update.json';
// The module's version
$version = $data['version'];
// The target to check against
$target = 'http://www.babdev.com/updates/TDB_version_new';
// Get the module params
$params = static::getModuleParams($this->form->getValue('id', null, 0));
// Get the stability level we want to show data for
$stability = $params->get('stability', 'stable');
// Check if we have cached data and use it if unexpired
if (!file_exists($cacheFile) || (time() - @filemtime($cacheFile) > 86400))
{
// Get the data from remote
$helper = new ModTweetDisplayBackHelper(new JRegistry);
$data = $helper->getJSON($target);
$update = $data->$stability;
// Write the cache if data exists
if (isset($update->notice))
{
$cache = json_encode($data);
file_put_contents($cacheFile, $cache);
}
}
else
{
// Render from the cached data
$data = json_decode(file_get_contents($cacheFile));
$update = $data->$stability;
}
// Message containing the version
if (version_compare(JVERSION, '3.0', 'ge'))
{
$message = '<div class="alert alert-info">';
$close = '</div>';
}
else
{
$message = '<label style="max-width:100%">';
$close = '</label>';
}
$message .= JText::sprintf('MOD_TWEETDISPLAYBACK_VERSION_INSTALLED', $version);
// Make sure that the $update object actually has data
if (!isset($update->notice))
{
$message .= ' ' . JText::_('MOD_TWEETDISPLAYBACK_VERSION_FAILED') . $close;
}
// If an update is available, and compatible with the current Joomla! version, notify the user
elseif (version_compare($update->version, $version, 'gt') && version_compare(JVERSION, $update->jversion, 'ge'))
{
$message .= ' <a href="' . $update->notice . '" target="_blank">' . JText::sprintf('MOD_TWEETDISPLAYBACK_VERSION_UPDATE', $update->version) . '</a></label>';
}
// No updates, or the Joomla! version is not compatible, so let the user know they're using the current version
else
{
$message .= ' ' . JText::_('MOD_TWEETDISPLAYBACK_VERSION_CURRENT') . $close;
}
return $message;
}
/**
* Method to get the module's params
*
* @param integer $id The module ID
*
* @return JRegistry
*
* @since 3.0
*/
protected static function getModuleParams($id)
{
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Query the params column
$query->select($db->quoteName('params'));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('id') . ' = ' . $id);
$db->setQuery($query);
$result = $db->loadResult();
// Convert the result to a JRegistry object
$params = new JRegistry($result);
// Return the params
return $params;
}
}