! [Install-testdata plugin] Added ability to purge tickets+replies and departments. Since I don't believe we need to be able to purge replies without purging tickets, this should actually conclude what's needed to be done. [Feature 668]
gruffen

gruffen commited on 2011-08-05 05:05:21
Showing 5 changed files, with 68 additions and 4 deletions.

... ...
@@ -46,11 +46,13 @@ $txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46 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 47
 $txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48 48
 
49
+$txt['shdp_install_testdata_purge_tickets_and_replies'] = 'Remove all existing tickets and replies (and their attachments)';
49 50
 $txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50 51
 $txt['shdp_install_testdata_purge_attachments'] = 'Remove all existing attachments to tickets (leaving forum attachments alone)';
51 52
 $txt['shdp_install_testdata_purge_cannedreplies'] = 'Remove all existing canned replies';
52 53
 $txt['shdp_install_testdata_purge_cf'] = 'Remove ALL custom value information';
53 54
 $txt['shdp_install_testdata_purge_cf_values'] = 'Remove only the values from custom fields (leave the fields intact)';
55
+$txt['shdp_install_testdata_purge_depts'] = 'Remove existing departments (a new one will be created to house any existing tickets)';
54 56
 $txt['shdp_install_testdata_purge_prefs'] = 'Remove all existing user preferences (including monitor/ignore) in the helpdesk';
55 57
 $txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
56 58
 $txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
... ...
@@ -46,11 +46,13 @@ $txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46 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 47
 $txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48 48
 
49
+$txt['shdp_install_testdata_purge_tickets_and_replies'] = 'Remove all existing tickets and replies (and their attachments)';
49 50
 $txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50 51
 $txt['shdp_install_testdata_purge_attachments'] = 'Remove all existing attachments to tickets (leaving forum attachments alone)';
51 52
 $txt['shdp_install_testdata_purge_cannedreplies'] = 'Remove all existing canned replies';
52 53
 $txt['shdp_install_testdata_purge_cf'] = 'Remove ALL custom value information';
53 54
 $txt['shdp_install_testdata_purge_cf_values'] = 'Remove only the values from custom fields (leave the fields intact)';
55
+$txt['shdp_install_testdata_purge_depts'] = 'Remove existing departments (a new one will be created to house any existing tickets)';
54 56
 $txt['shdp_install_testdata_purge_prefs'] = 'Remove all existing user preferences (including monitor/ignore) in the helpdesk';
55 57
 $txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
56 58
 $txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
... ...
@@ -46,11 +46,13 @@ $txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46 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 47
 $txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48 48
 
49
+$txt['shdp_install_testdata_purge_tickets_and_replies'] = 'Remove all existing tickets and replies (and their attachments)';
49 50
 $txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50 51
 $txt['shdp_install_testdata_purge_attachments'] = 'Remove all existing attachments to tickets (leaving forum attachments alone)';
51 52
 $txt['shdp_install_testdata_purge_cannedreplies'] = 'Remove all existing canned replies';
52 53
 $txt['shdp_install_testdata_purge_cf'] = 'Remove ALL custom value information';
53 54
 $txt['shdp_install_testdata_purge_cf_values'] = 'Remove only the values from custom fields (leave the fields intact)';
55
+$txt['shdp_install_testdata_purge_depts'] = 'Remove existing departments (a new one will be created to house any existing tickets)';
54 56
 $txt['shdp_install_testdata_purge_prefs'] = 'Remove all existing user preferences (including monitor/ignore) in the helpdesk';
55 57
 $txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
56 58
 $txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
... ...
@@ -46,11 +46,13 @@ $txt['shdp_install_testdata_del_title'] = 'Clean out the existing tables';
46 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 47
 $txt['shdp_install_testdata_nothingselected'] = 'No options were selected, nothing can be removed for that reason.';
48 48
 
49
+$txt['shdp_install_testdata_purge_tickets_and_replies'] = 'Remove all existing tickets and replies (and their attachments)';
49 50
 $txt['shdp_install_testdata_purge_actionlog'] = 'Remove all existing action log entries';
50 51
 $txt['shdp_install_testdata_purge_attachments'] = 'Remove all existing attachments to tickets (leaving forum attachments alone)';
51 52
 $txt['shdp_install_testdata_purge_cannedreplies'] = 'Remove all existing canned replies';
52 53
 $txt['shdp_install_testdata_purge_cf'] = 'Remove ALL custom value information';
53 54
 $txt['shdp_install_testdata_purge_cf_values'] = 'Remove only the values from custom fields (leave the fields intact)';
55
+$txt['shdp_install_testdata_purge_depts'] = 'Remove existing departments (a new one will be created to house any existing tickets)';
54 56
 $txt['shdp_install_testdata_purge_prefs'] = 'Remove all existing user preferences (including monitor/ignore) in the helpdesk';
55 57
 $txt['shdp_install_testdata_purge_roles'] = 'Remove all existing roles';
56 58
 $txt['shdp_install_testdata_purge_relationships'] = 'Remove all existing relationships between tickets';
... ...
@@ -62,7 +62,7 @@ $lorem = new LoremIpsumGenerator;
62 62
 if (SMF != 'SSI')
63 63
 	fatal_error('This script can only be run via direct link, it cannot be embedded into the forum or helpdesk itself.', false);
64 64
 
65
-$context['shd_delete_rules'] = array('actionlog', 'attachments', 'cannedreplies', 'cf_values', 'cf', 'prefs', 'relationships', 'roles');
65
+$context['shd_delete_rules'] = array('tickets_and_replies', 'actionlog', 'attachments', 'cannedreplies', 'cf_values', 'cf', 'depts', 'prefs', 'relationships', 'roles');
66 66
 $context['page_title_html_safe'] = $txt['shdp_install_testdata_title'];
67 67
 template_header();
68 68
 
... ...
@@ -112,6 +112,14 @@ if (!$do_form)
112 112
 	{
113 113
 		checkSession();
114 114
 		validateSession();
115
+		// Clearing the tickets may clear other data too. Let's just add it to the list.
116
+		if (!empty($_POST['purge_tickets_and_replies']))
117
+		{
118
+			$_POST['purge_attachments'] = 'on';
119
+			$_POST['purge_cf_values'] = 'on';
120
+			$_POST['purge_relationships'] = 'on';
121
+		}
122
+
115 123
 		$errors_clearance = array();
116 124
 		$selected = 0;
117 125
 		foreach ($context['shd_delete_rules'] as $test)
... ...
@@ -392,6 +400,16 @@ elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
392 400
 						<span class="topslice"><span></span></span>
393 401
 						<div class="content">';
394 402
 
403
+	// Purging all tickets and replies
404
+	if (!empty($_POST['purge_tickets_and_replies']))
405
+	{
406
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_tickets');
407
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_ticket_replies');
408
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_log_read');
409
+		echo $txt['shdp_install_testdata_purge_tickets_and_replies'], ' - <strong>', $txt['shdp_install_testdata_completed_purge'], '</strong><br />';
410
+		flush();
411
+	}
412
+
395 413
 	// Purging the action log
396 414
 	if (!empty($_POST['purge_actionlog']))
397 415
 	{
... ...
@@ -400,7 +418,7 @@ elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
400 418
 		flush();
401 419
 	}
402 420
 
403
-	// Purging all SD attachments
421
+	// Purging all SD attachments (this will be called if all tickets and replies are being purged too!)
404 422
 	if (!empty($_POST['purge_attachments']))
405 423
 	{
406 424
 		// Fetch the SD attachments data log.
... ...
@@ -436,7 +454,7 @@ elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
436 454
 		flush();
437 455
 	}
438 456
 
439
-	// Purging custom fields - only the values
457
+	// Purging custom fields - only the values (this will be called if all tickets and replies are being purged too!)
440 458
 	if (!empty($_POST['purge_cf_values']))
441 459
 	{
442 460
 		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_custom_fields_values');
... ...
@@ -454,6 +472,44 @@ elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
454 472
 		flush();
455 473
 	}
456 474
 
475
+	// Purging existing departments, and then creating a new one for any new tickets.
476
+	if (!empty($_POST['purge_depts']))
477
+	{
478
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_depts');
479
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_dept_roles');
480
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_cannedreplies_depts');
481
+		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_custom_fields_depts');
482
+
483
+		$smcFunc['db_insert']('replace',
484
+			'{db_prefix}helpdesk_depts',
485
+			array(
486
+				'dept_name' => 'string', 'board_cat' => 'int', 'description' => 'string', 'before_after' => 'int', 'dept_order' => 'int', 'dept_theme' => 'int',
487
+			),
488
+			array(
489
+				!empty($txt['shd_helpdesk']) ? $txt['shd_helpdesk'] : 'Helpdesk', 0, '', 0, 1, 0,
490
+			),
491
+			array('id_dept')
492
+		);
493
+
494
+		// Move any outstanding tickets into the last department we had, which will be the last one we created. This should normally be 1 but it never hurts to check.
495
+		$query = $smcFunc['db_query']('', 'SELECT MAX(id_dept) FROM {db_prefix}helpdesk_depts');
496
+		list($new_dept) = $smcFunc['db_fetch_row']($query);
497
+		$smcFunc['db_free_result']($query);
498
+		if (!empty($new_dept))
499
+		{
500
+			$smcFunc['db_query']('', '
501
+				UPDATE {db_prefix}helpdesk_tickets
502
+				SET id_dept = {int:new_dept}',
503
+				array(
504
+					'new_dept' => $new_dept,
505
+				)
506
+			);
507
+		}
508
+
509
+		echo $txt['shdp_install_testdata_purge_depts'], ' - <strong>', $txt['shdp_install_testdata_completed_purge'], '</strong><br />';
510
+		flush();
511
+	}
512
+
457 513
 	// Purging user preferences
458 514
 	if (!empty($_POST['purge_prefs']))
459 515
 	{
... ...
@@ -463,7 +519,7 @@ elseif (!empty($_REQUEST['go']) && $_REQUEST['go'] == 'yeah-for-delete')
463 519
 		flush();
464 520
 	}
465 521
 
466
-	// Purging relationships
522
+	// Purging relationships (this will be called if all tickets and replies are being purged too!)
467 523
 	if (!empty($_POST['purge_relationships']))
468 524
 	{
469 525
 		$smcFunc['db_query']('', 'TRUNCATE {db_prefix}helpdesk_relationships');
470 526