awit_zacr.php 6.98 KB
Newer Older
1
2
<?php
/**
3
 * AWIT ZACR - COZA EPP Module
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Copyright (c) 2014, AllWorldIT
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


// Make sure we not being accssed directly
if (!defined("WHMCS"))
	die("This file cannot be accessed directly");

// Our global supported field list
26
$AWIT_ZACR_SUPPORTED_FIELDS = array(
Nigel Kukard's avatar
Nigel Kukard committed
27
	"max_items"
28
29
30
);

// Addon configuration
31
function awit_zacr_config()
32
33
34
35
{

	// Configuration
	$configarray = array(
36
		"name" => "AWIT ZACR",
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
		"description" => "This module is to facilitate displaying of epp messages.",
		"version" => "0.1",
		"author" => "AllWorldIT",
		"language" => "english",
		"fields" => array(
			// Admin User
			"max_items" => array (
				"FriendlyName" => "Items Per Page",
				"Description" => "Amount of items to list per page",
				"Type" => "text", "Size" => "30",
				"Default" => "100"
			),
		)
	);

	return $configarray;
}



57
function awit_zacr_get_config_custom_fields()
58
{
59
	global $AWIT_ZACR_SUPPORTED_FIELDS;
60
61
62
63

	// Query modules table
	$table = "tbladdonmodules";
	$fields = "setting,value";
64
	$where = array( 'module' => 'awit_zacr' );
65
66
67
68
69
70
	$result = select_query($table,$fields,$where);

	// Filter out the settings we need
	$custom_fields = array();
	while ($row = mysql_fetch_array($result)) {
		// Check in our global list
71
		if (in_array($row['setting'],$AWIT_ZACR_SUPPORTED_FIELDS)) {
72
73
74
75
76
77
78
79
80
			$custom_fields[$row['setting']] = $row['value'];
		}
	}

	return $custom_fields;
}



81
function _awit_zacr_getMaxItems()
82
{
83
	$customFields = awit_zacr_get_config_custom_fields();
84
85
86
87
88
89
	return $customFields['max_items'];
}



// Addon activation
90
function awit_zacr_activate()
91
92
93
94
{

	// Create Custom DB Table
	$result = mysql_query("
95
		CREATE TABLE `mod_awit_zacr_messages` (
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
			`id` INT( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
			`created` DATETIME NOT NULL,
			`code` VARCHAR(10) NOT NULL,
			`message` TEXT NOT NULL
		)
	");

	// Return Result
	if (!$result) {
		return array("status" => "error", "description"=>"There was a problem activating the module.");
	} else {
		return array("status" => "success","description" =>"Open module configuration for configuration options.");
	}

}



114
function awit_zacr_deactivate()
115
116
117
{
	// Remove custom tables
	$result1 = mysql_query("
118
		DROP TABLE `mod_awit_zacr_messages`
119
120
	");

Nigel Kukard's avatar
Nigel Kukard committed
121
	if (!$result) {
122
123
124
125
126
127
128
129
130
131
		return array("status"=>"error","description"=>"There was an error deactivating the module.");
	} else {
		return array("status"=>"success","description"=>"Module has been deactivated.");
	}

}



// Addon output
132
function awit_zacr_output($vars)
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
{
	// Check if we have to display all records
	if (isset($_POST['button']) && $_POST['button'] == 'Display All') {
		$whereClause = '';

	} else if (!empty($_POST['date_to']) && !empty($_POST['date_from'])) {

		// Default dates
		try {
			$dateTo = new DateTime($_POST['date_to']);
			$dateTo = $dateTo->format('Y-m-d');

			$dateFrom = new DateTime($_POST['date_from']);
			$dateFrom = $dateFrom->format('Y-m-d');
		} catch (Exception $ex) {
			// Catching exception against valid date

			$dateFrom = new DateTime(date("Y-m-d"));
			$dateFrom = $dateFrom->format('Y-m-d');

			$dateFrom->modify('+1 day');
			$dateTo = $dateTo->format('Y-m-d');
		}

		$whereClause = "WHERE Date(created) >= Date('".mysql_real_escape_string($dateFrom)."')
				AND Date(created) <= Date('".mysql_real_escape_string($dateTo)."')";
	}

	// Make link to use
	$link = $vars['modulelink'];

	// Fancy date picker
Nigel Kukard's avatar
Nigel Kukard committed
165
166
167
168
169
170
171
172
173
174
175
	echo '
		<script>
			$(function() {
				$( "#date_from" ).datepicker({
					dateFormat: "yy-mm-dd",
					constrainInput: true
				});
				$( "#date_to" ).datepicker({
					dateFormat: "yy-mm-dd",
					constrainInput: true
				});
176
			});
Nigel Kukard's avatar
Nigel Kukard committed
177
178
		</script>
	';
179
180
181
182
183
184

	// Date search fields
	echo "<p>Select a start and end date and hit search.</p>";
	echo "<form action='$link' method='post'>";
	echo "<input id='date_from' type='text' value='$dateFrom' name='date_from' />";
	echo "<input id='date_to' type='text' value='$dateTo' name='date_to' />";
Nigel Kukard's avatar
Nigel Kukard committed
185
	echo "<input type='submit' name='button' value='Search' />";
186
187
188
189
190
191
	echo "<input type='submit' name='button' value='Display All' />";
	echo "<br /><br />";

	$orderClause = 'ORDER BY created DESC';

	// Max amount of records to show per page
192
	$recordMax = _awit_zacr_getMaxItems();
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

	// Validation
	if (!is_numeric($recordMax)) {
		$recordMax = 100;
	}

	// Setting page number
	if (isset($_GET['page'])) {
		$page = $_GET['page'];
	} else if (isset($_POST['page'])) {
		$page = $_POST['page'];
	}

	// Ensuring valid page number
	if ($page < 1) {
		$page = 1;
	}
	// Pagination button handler
	if (isset($_POST['prevPage'])) {
		// Prev Page
		$page = ($page > 1)? ($page - 1) : 1;

	} else if (isset($_POST['nextPage'])) {
		// Next Page
		$page = $page + 1;
	}

	$recordCurrent = intval(abs($recordMax * $page)) - $recordMax;

	$limitClause = "LIMIT $recordCurrent, $recordMax";

	// Query the database, getting the total amount of records
	$result = mysql_query(sprintf("
Nigel Kukard's avatar
Nigel Kukard committed
226
227
228
			SELECT
				COUNT(*) AS cnt
			FROM
229
				mod_awit_zacr_messages
Nigel Kukard's avatar
Nigel Kukard committed
230
231
232
233
234
			%s %s
		",
		$whereClause,
		$orderClause
	));
235
236
237
238
239
240
241

	$row = mysql_fetch_array($result);
	$totalRecords = $row['cnt'];
	$lastPage = ceil($totalRecords / $recordMax);

	// Query the database
	$result = mysql_query(sprintf("
Nigel Kukard's avatar
Nigel Kukard committed
242
243
244
			SELECT
				*
			FROM
245
				mod_awit_zacr_messages
Nigel Kukard's avatar
Nigel Kukard committed
246
247
248
249
250
251
			%s %s %s
		",
		$whereClause,
		$orderClause,
		$limitClause
	));
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

	// Loop through results and genenrate form
	$includeForm = 0;
	while ($row = mysql_fetch_array($result)) {
		// Open form
		if (!$includeForm) {
			$includeForm = 1;

			echo '<div class="tablebg">';
			echo '<table id="epp-message-log" class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">';
			echo "<tr>";
			echo "<th>Timestamp</th>";
			echo "<th>Code</th>";
			echo "<th>Message</th>";
			echo "</tr>";
		}

		echo "<tr>";
		echo "<td>".$row['created']."</td>";
		echo "<td>".$row['code']."</td>";
		echo "<td>".$row['message']."</td>";
		echo "</tr>";
	}

	// Close form
	if ($includeForm) {
		echo "<tr><td colspan='5'>";
		if ($page >= $lastPage) {
			echo "<button name='prevPage'> &lt;&lt; Previous Page </button> Page $page";
		} else if ($page == 1) {
			echo " Page $page <button name='nextPage'> Next Page &gt;&gt; </button>";
		} else {
			echo "<button name='prevPage'> &lt;&lt; Previous Page </button> Page $page";
			echo "<button name='nextPage'> Next Page &gt;&gt; </button>";
		}

		echo "</td></tr>";
		echo "</table><br>";
		echo "<input type='hidden' name='page' value='$page'>";
		echo "</form>";
		echo "</div>";
	} else {
		echo "<p>No logs yet for selected period..</p>";
	}

}