! [Install-testdata plugin] Rejig most of how it works to accommodate deletion facility, and add in the ability to clean out the action log, the relationships entries and all helpdesk roles. More to come.
gruffen

gruffen commited on 2011-08-01 04:38:18
Showing 6 changed files, with 189 additions and 20 deletions.

... ...
@@ -41,4 +41,17 @@ $txt['shdp_install_testdata_nonstaff'] = 'User to use as "not staff" in the code
41 41
 $txt['shdp_install_testdata_dept'] = 'Department the tickets should be added to';
42 42
 $txt['shdp_install_testdata_create'] = 'Create!';
43 43
 $txt['shdp_install_testdata_added'] = 'Ticket %1$d added with %2$d replies!';
44
+
45
+$txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46
+$txt['shdp_install_testdata_warning'] = 'These options are primarily for testing purposes and are not recommended for general use. They are NOT RECOVERABLE: if you have any doubts, take a backup first.';
47
+$txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48
+
49
+$txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50
+$txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
51
+$txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
52
+
53
+$txt['shdp_install_testdata_completed_purge'] = 'completed';
54
+
55
+$txt['shdp_install_testdata_clear_sure'] = 'Did I mention that this data was NOT RECOVERABLE? If you press OK, the data will be deleted from your helpdesk, permanently and irretrievably without a backup. Are you SURE you want to proceed?';
56
+
44 57
 ?>
45 58
\ No newline at end of file
... ...
@@ -41,4 +41,17 @@ $txt['shdp_install_testdata_nonstaff'] = 'User to use as "not staff" in the code
41 41
 $txt['shdp_install_testdata_dept'] = 'Department the tickets should be added to';
42 42
 $txt['shdp_install_testdata_create'] = 'Create!';
43 43
 $txt['shdp_install_testdata_added'] = 'Ticket %1$d added with %2$d replies!';
44
+
45
+$txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46
+$txt['shdp_install_testdata_warning'] = 'These options are primarily for testing purposes and are not recommended for general use. They are NOT RECOVERABLE: if you have any doubts, take a backup first.';
47
+$txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48
+
49
+$txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50
+$txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
51
+$txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
52
+
53
+$txt['shdp_install_testdata_completed_purge'] = 'completed';
54
+
55
+$txt['shdp_install_testdata_clear_sure'] = 'Did I mention that this data was NOT RECOVERABLE? If you press OK, the data will be deleted from your helpdesk, permanently and irretrievably without a backup. Are you SURE you want to proceed?';
56
+
44 57
 ?>
45 58
\ No newline at end of file
... ...
@@ -41,4 +41,17 @@ $txt['shdp_install_testdata_nonstaff'] = 'User to use as "not staff" in the code
41 41
 $txt['shdp_install_testdata_dept'] = 'Department the tickets should be added to';
42 42
 $txt['shdp_install_testdata_create'] = 'Create!';
43 43
 $txt['shdp_install_testdata_added'] = 'Ticket %1$d added with %2$d replies!';
44
+
45
+$txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46
+$txt['shdp_install_testdata_warning'] = 'These options are primarily for testing purposes and are not recommended for general use. They are NOT RECOVERABLE: if you have any doubts, take a backup first.';
47
+$txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48
+
49
+$txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50
+$txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
51
+$txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
52
+
53
+$txt['shdp_install_testdata_completed_purge'] = 'completed';
54
+
55
+$txt['shdp_install_testdata_clear_sure'] = 'Did I mention that this data was NOT RECOVERABLE? If you press OK, the data will be deleted from your helpdesk, permanently and irretrievably without a backup. Are you SURE you want to proceed?';
56
+
44 57
 ?>
45 58
\ No newline at end of file
... ...
@@ -41,4 +41,17 @@ $txt['shdp_install_testdata_nonstaff'] = 'User to use as "not staff" in the code
41 41
 $txt['shdp_install_testdata_dept'] = 'Department the tickets should be added to';
42 42
 $txt['shdp_install_testdata_create'] = 'Create!';
43 43
 $txt['shdp_install_testdata_added'] = 'Ticket %1$d added with %2$d replies!';
44
+
45
+$txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46
+$txt['shdp_install_testdata_warning'] = 'These options are primarily for testing purposes and are not recommended for general use. They are NOT RECOVERABLE: if you have any doubts, take a backup first.';
47
+$txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48
+
49
+$txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50
+$txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
51
+$txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
52
+
53
+$txt['shdp_install_testdata_completed_purge'] = 'completed';
54
+
55
+$txt['shdp_install_testdata_clear_sure'] = 'Did I mention that this data was NOT RECOVERABLE? If you press OK, the data will be deleted from your helpdesk, permanently and irretrievably without a backup. Are you SURE you want to proceed?';
56
+
44 57
 ?>
45 58
\ No newline at end of file
... ...
@@ -43,8 +43,8 @@ if (!defined('TICKET_STATUS_NEW')) // SD isn't loaded and active
43 43
 	die('<b>SimpleDesk needs to be installed and active before running this script.</b>');
44 44
 elseif (!in_array('install_testdata', $context['shd_plugins']))
45 45
 	die('<b>This plugin needs to be activated in the admin panel.</b>');
46
-else
47
-	isAllowedTo('admin_forum');
46
+elseif (!$user_info['is_admin'])
47
+	die('This can only be accessed by an administrator account.');
48 48
 
49 49
 $db_show_debug = false;
50 50
 shd_load_language('SDPluginInstallTestdata');
... ...
@@ -59,8 +59,10 @@ require_once($sourcedir . '/sd_source/Subs-SimpleDeskPost.php');
59 59
 
60 60
 $lorem = new LoremIpsumGenerator;
61 61
 
62
-if (SMF == 'SSI')
63
-{
62
+if (SMF != 'SSI')
63
+	fatal_error('This script can only be run via direct link, it cannot be embedded into the forum or helpdesk itself.', false);
64
+
65
+$context['shd_delete_rules'] = array('actionlog', 'relationships', 'roles');
64 66
 $context['page_title_html_safe'] = $txt['shdp_install_testdata_title'];
65 67
 template_header();
66 68
 
... ...
@@ -74,9 +76,15 @@ if (SMF == 'SSI')
74 76
 	$depts[$row['id_dept']] = $row['dept_name'];
75 77
 $smcFunc['db_free_result']($query);
76 78
 
77
-	$do_form = false;
79
+$do_form = empty($_REQUEST['go']);
80
+
81
+if (!$do_form)
82
+{
78 83
 	$errors = array();
79 84
 
85
+	if ($_REQUEST['go'] == 'yeah')
86
+	{
87
+		// Check the list for normal test-data
80 88
 		$num_tickets = !empty($_POST['tickets']) ? (int) $_POST['tickets'] : 0;
81 89
 		if ($num_tickets == 0 || $num_tickets > 100000)
82 90
 			$errors[] = $txt['shdp_install_testdata_invalidtickets'];
... ...
@@ -95,15 +103,31 @@ if (SMF == 'SSI')
95 103
 
96 104
 		$user_member = !empty($_POST['user_member']) ? (int) $_POST['user_member'] : 0;
97 105
 
106
+		$dest_dept = !empty($_POST['dept']) ? (int) $_POST['dept'] : 0;
107
+		if (!isset($depts[$dest_dept]))
108
+			$errors[] = $txt['shd_install_testdata_invalid_dept'];
109
+
110
+	}
111
+	elseif ($_REQUEST['go'] == 'yeah-for-delete')
112
+	{
113
+		checkSession();
114
+		validateSession();
115
+		$errors_clearance = array();
116
+		$selected = 0;
117
+		foreach ($context['shd_delete_rules'] as $test)
118
+			if (!empty($_POST['purge_' . $test]))
119
+				$selected++;
120
+
121
+		if ($selected == 0)
122
+			$errors_clearance[] = $txt['shdp_install_testdata_nothingselected'];
123
+	}
124
+}
125
+
98 126
 $staff = shd_members_allowed_to('shd_staff', 0);
99 127
 $names = array(
100 128
 	0 => $txt['shdp_install_testdata_guestuser'],
101 129
 );
102 130
 
103
-	$dest_dept = !empty($_POST['dept']) ? (int) $_POST['dept'] : 0;
104
-	if (!isset($depts[$dest_dept]))
105
-		$errors[] = $txt['shd_install_testdata_invalid_dept'];
106
-
107 131
 $query = $smcFunc['db_query']('', '
108 132
 	SELECT id_member, real_name
109 133
 	FROM {db_prefix}members
... ...
@@ -115,7 +139,10 @@ if (SMF == 'SSI')
115 139
 
116 140
 $nonstaff = array_diff(array_keys($names), $staff);
117 141
 
118
-	if (!empty($errors))
142
+if (!empty($errors) || !empty($errors_clearance))
143
+	$do_form = true;
144
+
145
+if ($do_form)
119 146
 {
120 147
 	echo '
121 148
 				<div class="tborder">
... ...
@@ -125,14 +152,15 @@ if (SMF == 'SSI')
125 152
 						</h3>
126 153
 					</div>
127 154
 					<div class="windowbg">
128
-						<span class="topslice"><span></span></span>';
155
+						<span class="topslice"><span></span></span>
156
+						<div class="content">';
129 157
 
130
-		if (isset($_POST['go']))
158
+	if (!empty($errors))
131 159
 	{
132 160
 		// crap, that means we actually did the form submission but it was borked somehow :(
133 161
 		echo '
134
-						<div class="content">', $txt['shdp_install_testdata_errors'], ':
135
-						<ul>';
162
+						<div class="errorbox" id="errors"><strong>', $txt['shdp_install_testdata_errors'], ':</strong>
163
+						<ul class="error">';
136 164
 		foreach ($errors as $msg)
137 165
 			echo '
138 166
 							<li>', $msg, '</li>';
... ...
@@ -145,7 +173,6 @@ if (SMF == 'SSI')
145 173
 	echo '
146 174
 						<form id="creator" name="creator" action="', $boardurl, '/install-testdata.php" method="post">
147 175
 							<input type="hidden" name="go" value="yeah" />
148
-							<div class="content">
149 176
 							<dl>
150 177
 								<dt><strong>', $txt['shdp_install_testdata_numtickets'], ':</strong></dt>
151 178
 								<dd><input type="text" size="5" value="100" name="tickets" class="input_text" /></dd>
... ...
@@ -177,23 +204,68 @@ if (SMF == 'SSI')
177 204
 								<dd>
178 205
 									<select name="dept">';
179 206
 	foreach ($depts as $id => $dept)
180
-		{
181 207
 		echo '
182 208
 										<option value="', $id, '">', $dept, '</option>';
183
-		}
184 209
 
185 210
 	echo '
186 211
 									</select>
187 212
 								</dd>
188 213
 							</dl>
189 214
 							<input type="submit" value="', $txt['shdp_install_testdata_create'], '" class="button_submit" />
215
+						</form>
216
+						</div>
217
+						<span class="botslice"><span></span></span>
190 218
 					</div>
219
+				</div>';
220
+
221
+	// Now we do the form for content delete.
222
+	echo '
223
+				<br />
224
+				<div class="tborder">
225
+					<div class="cat_bar">
226
+						<h3 class="catbg">
227
+							<img src="', $settings['default_images_url'], '/simpledesk/delete.png" alt="x" /> ', $txt['shdp_install_testdata_del_title'], '
228
+						</h3>
229
+					</div>
230
+					<div class="windowbg">
231
+						<span class="topslice"><span></span></span>
232
+						<div class="content">';
233
+
234
+	if (!empty($errors_clearance))
235
+	{
236
+		// crap, that means we actually did the form submission but it was borked somehow :(
237
+		echo '
238
+							<div class="errorbox" id="errors"><strong>', $txt['shdp_install_testdata_errors'], ':</strong>
239
+							<ul class="error">';
240
+		foreach ($errors_clearance as $msg)
241
+			echo '
242
+								<li>', $msg, '</li>';
243
+
244
+		echo '
245
+							</ul>
246
+							</div>';
247
+	}
248
+
249
+	echo '
250
+							<form id="creator" name="creator" action="', $boardurl, '/install-testdata.php" method="post">
251
+								<dl>';
252
+	foreach ($context['shd_delete_rules'] as $delete_opt)
253
+		echo '
254
+									<dt><strong>', $txt['shdp_install_testdata_purge_' . $delete_opt], ':</strong></dt>
255
+									<dd><input type="checkbox" class="input_check" name="purge_', $delete_opt, '" /></dd>';
256
+	
257
+	echo '
258
+								</dl>
259
+								<input type="submit" value="', $txt['shdp_install_testdata_del_title'], '" class="button_submit" onclick="return confirm(' . JavaScriptEscape($txt['shdp_install_testdata_clear_sure']). ');" />
260
+								<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
261
+								<input type="hidden" name="go" value="yeah-for-delete" />
191 262
 							</form>
263
+						</div>
192 264
 						<span class="botslice"><span></span></span>
193 265
 					</div>
194 266
 				</div>';
195 267
 }
196
-	else
268
+elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah')
197 269
 {
198 270
 	echo '
199 271
 				<div class="tborder">
... ...
@@ -307,10 +379,55 @@ if (SMF == 'SSI')
307 379
 					</div>
308 380
 				</div>';
309 381
 }
382
+elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
383
+{
384
+	echo '
385
+				<div class="tborder">
386
+					<div class="cat_bar">
387
+						<h3 class="catbg">
388
+							<img src="', $settings['default_images_url'], '/simpledesk/delete.png" alt="x" /> ', $txt['shdp_install_testdata_del_title'], '
389
+						</h3>
390
+					</div>
391
+					<div class="windowbg">
392
+						<span class="topslice"><span></span></span>
393
+						<div class="content">';
310 394
 
311
-	template_footer();
395
+	// Purging the action log
396
+	if (!empty($_POST['purge_actionlog']))
397
+	{
398
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_log_action');
399
+		echo $txt['shdp_install_testdata_purge_actionlog'], ' - <strong>', $txt['shdp_install_testdata_completed_purge'], '</strong><br />';
400
+		flush();
401
+	}
402
+
403
+	// Purging relationships
404
+	if (!empty($_POST['purge_relationships']))
405
+	{
406
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_relationships');
407
+		echo $txt['shdp_install_testdata_purge_relationships'], ' - <strong>', $txt['shdp_install_testdata_completed_purge'], '</strong><br />';
408
+		flush();
409
+	}
410
+
411
+	// Purging roles
412
+	if (!empty($_POST['purge_roles']))
413
+	{
414
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_roles');
415
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_role_groups');
416
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_role_permissions');
417
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_dept_roles');
418
+		echo $txt['shdp_install_testdata_purge_roles'], ' - <strong>', $txt['shdp_install_testdata_completed_purge'], '</strong><br />';
419
+		flush();
312 420
 	}
313 421
 
422
+	echo '
423
+						</div>
424
+						<span class="botslice"><span></span></span>
425
+					</div>
426
+				</div>';
427
+}
428
+
429
+template_footer();
430
+
314 431
 /**
315 432
  *	Copyright (c) 2009, Mathew Tinsley (tinsley@tinsology.net)
316 433
  *	All rights reserved.
... ...
@@ -2,7 +2,7 @@
2 2
 <!DOCTYPE package-info SYSTEM "http://www.simplemachines.org/xml/package-info">
3 3
 <package-info xmlns="http://www.simplemachines.org/xml/package-info" xmlns:smf="http://www.simplemachines.org/">
4 4
 	<id>SimpleDeskTeam:installtestdata</id>
5
-	<name>SimpleDesk: Install Test-Data</name>
5
+	<name>Install Test-Data</name>
6 6
 	<version>1.0</version>
7 7
 	<type>sdplugin</type>
8 8
 	<install for="2.0">
9 9