! Tickets' custom fields should now be able to be moved to the forum alongside their respective ticket/replies.
gruffen

gruffen commited on 2011-05-17 05:52:40
Showing 3 changed files, with 187 additions and 4 deletions.

... ...
@@ -316,6 +316,7 @@ $txt['shd_ticket_move_cfs_admin'] = 'Currently visible to administrators';
316 316
 $txt['shd_ticket_move_accept'] = 'I accept that some of the fields being manipulated here are not visible to all users, and that this topic should be moved to the forum, with the above settings.';
317 317
 $txt['shd_ticket_move_reqd'] = 'This option must be selected in order for you to move this ticket to the forum.';
318 318
 $txt['shd_ticket_move_ok'] = 'This field is safe to move, all the users who can see the ticket can see this field, there is no information hidden from users or staff.';
319
+$txt['shd_ticket_move_reqd_nonselected'] = 'This ticket has fields that users or staff may not be able to see, as such you specifically need to confirm you are aware of this - please go back to the previous page, the checkbox for confirming your awareness of this is at the bottom of the form.';
319 320
 
320 321
 // Merge ticket
321 322
 $txt['shd_merge_ticket'] = 'Merge ticket';
... ...
@@ -133,7 +133,6 @@ function shd_tickettotopic()
133 133
 			'name' => strip_tags($row['name']),
134 134
 			'category' => strip_tags($row['cat_name']),
135 135
 			'child_level' => $row['child_level'],
136
-			'selected' => !empty($_SESSION['move_to_topic']) && $_SESSION['move_to_topic'] == $row['id_board'] && $row['id_board'] != $board,
137 136
 		);
138 137
 	}
139 138
 
... ...
@@ -361,6 +360,113 @@ function shd_tickettotopic2()
361 360
 	if (!empty($context['deleted_prompt']) && $context['deleted_prompt'] == 'abort')
362 361
 		redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id'] . ';recycle');
363 362
 
363
+	// Now the madness that is custom fields. First, load the custom fields we might/will be using.
364
+	$query = $smcFunc['db_query']('', '
365
+		SELECT hdcf.id_field, hdcf.field_name, hdcf.field_order, hdcf.field_type, hdcf.can_see, hdcf.field_options, hdcf.bbc, hdcf.placement
366
+		FROM {db_prefix}helpdesk_custom_fields_depts AS hdd
367
+			INNER JOIN {db_prefix}helpdesk_custom_fields AS hdcf ON (hdd.id_field = hdcf.id_field)
368
+		WHERE hdd.id_dept = {int:dept}
369
+			AND hdcf.active = {int:active}
370
+		ORDER BY hdcf.field_order',
371
+		array(
372
+			'dept' => $dept,
373
+			'active' => 1,
374
+		)
375
+	);
376
+	$context['custom_fields'] = array();
377
+	$is_staff = shd_allowed_to('shd_staff', $dept);
378
+	$is_admin = shd_allowed_to('admin_helpdesk', $dept);
379
+	while ($row = $smcFunc['db_fetch_assoc']($query))
380
+	{
381
+		list($user_see, $staff_see) = explode(',', $row['can_see']);
382
+		$context['custom_fields'][$row['id_field']] = array(
383
+			'id_field' => $row['id_field'],
384
+			'name' => $row['field_name'],
385
+			'type' => $row['field_type'],
386
+			'bbc' => !empty($row['bbc']),
387
+			'options' => !empty($row['field_options']) ? unserialize($row['field_options']) : array(),
388
+			'placement' => $row['placement'],
389
+			'visible' => array(
390
+				'user' => $user_see,
391
+				'staff' => $staff_see,
392
+				'admin' => true,
393
+			),
394
+			'values' => array(),
395
+		);
396
+	}
397
+	$smcFunc['db_free_result']($query);
398
+
399
+	// Having got all the possible fields for this ticket, let's fetch the values for it. That way if we don't have any values for a field, we don't have to care about showing the user.
400
+	// But first, we need all the message ids.
401
+	$context['ticket_messages'] = array();
402
+	$query = $smcFunc['db_query']('', '
403
+		SELECT id_msg
404
+		FROM {db_prefix}helpdesk_ticket_replies AS hdtr
405
+		WHERE id_ticket = {int:ticket}',
406
+		array(
407
+			'ticket' => $context['ticket_id'],
408
+		)
409
+	);
410
+	while ($row = $smcFunc['db_fetch_row']($query))
411
+		$context['ticket_messages'][] = $row[0];
412
+	$smcFunc['db_free_result']($query);
413
+
414
+	// Now get a reference for the field values.
415
+	$query = $smcFunc['db_query']('', '
416
+		SELECT cfv.id_post, cfv.id_field, cfv.post_type, cfv.value
417
+		FROM {db_prefix}helpdesk_custom_fields_values AS cfv
418
+		WHERE (cfv.id_post = {int:ticket} AND cfv.post_type = 1)' . (!empty($context['ticket_messages']) ? '
419
+			OR (cfv.id_post IN ({array_int:msgs}) AND cfv.post_type = 2)' : ''),
420
+		array(
421
+			'ticket' => $context['ticket_id'],
422
+			'msgs' => $context['ticket_messages'],
423
+		)
424
+	);
425
+	while ($row = $smcFunc['db_fetch_assoc']($query))
426
+		$context['custom_fields'][$row['id_field']]['values'][$row['post_type']][$row['id_post']] = $row['value'];
427
+	$smcFunc['db_free_result']($query);
428
+
429
+	// Having now established what fields we do actually have values for, let's proceed to deal with them.
430
+	foreach ($context['custom_fields'] as $field_id => $field)
431
+	{
432
+		// Didn't we have any values? If not, prune it, not interested.
433
+		if (empty($field['values']))
434
+			unset($context['custom_fields'][$field_id]);
435
+
436
+		// If the user is an administrator, they can always see the fields.
437
+		if ($is_admin)
438
+		{
439
+			// But users might not be able to, in which case we need to validate that actually, they did need to authorise this one.
440
+			if (!$field['visible']['user'] || !$field['visible']['staff'])
441
+				$context['custom_fields_warning'] = true;
442
+		}
443
+		elseif ($is_staff)
444
+		{
445
+			// So they're staff. But the field might not be visible to them; they can't deal with it whatever.
446
+			if (!$field['visible']['staff'])
447
+				fatal_lang_error('cannot_shd_move_ticket_topic_hidden_cfs', false);
448
+			elseif (!$field['visible']['user'])
449
+				$context['custom_fields_warning'] = true;
450
+		}
451
+		else
452
+		{
453
+			// Non staff aren't special. They should not be able to make this decision. If someone can't see it, they don't get to make the choice.
454
+			if (!$field['visible']['user'] || !$field['visible']['staff'])
455
+				fatal_lang_error('cannot_shd_move_ticket_topic_hidden_cfs', false);
456
+		}
457
+
458
+		// Are we ignoring this field? If so, we can now safely get rid of it at this very point.
459
+		if (isset($_POST['field' . $field_id]) && $_POST['field' . $field_id] == 'lose')
460
+			unset($context['custom_fields'][$field_id]);
461
+	}
462
+
463
+	// Were there any special fields? We need to check - and check that they ticked the right box!
464
+	if (!empty($context['custom_fields_warning']) && empty($_POST['accept_move']))
465
+		fatal_lang_error('shd_ticket_move_reqd_nonselected', false);
466
+
467
+	// OK, so we have some fields, and we're doing something with them. First we need to attach the fields from the ticket to the opening post.
468
+	shd_append_custom_fields($body, $context['ticket_id'], CFIELD_TICKET);
469
+
364 470
 	// All okay, it seems. Let's go create the topic.
365 471
 	$msgOptions = array(
366 472
 		'subject' => $subject,
... ...
@@ -409,7 +515,8 @@ function shd_tickettotopic2()
409 515
 				modified_time, modified_member, modified_name, poster_time, id_msg, message_status
410 516
 			FROM {db_prefix}helpdesk_ticket_replies AS hdtr
411 517
 			WHERE id_ticket = {int:ticket}
412
-				AND id_msg > {int:ticket_msg}',
518
+				AND id_msg > {int:ticket_msg}
519
+			ORDER BY id_msg',
413 520
 			array(
414 521
 				'ticket' => $context['ticket_id'],
415 522
 				'ticket_msg' => $firstmsg,
... ...
@@ -427,6 +534,8 @@ function shd_tickettotopic2()
427 534
 				if ($row['message_status'] == MSG_STATUS_DELETED && !empty($context['deleted_prompt']) && $context['deleted_prompt'] == 'delete') // we don't want these replies!
428 535
 					continue;
429 536
 
537
+				shd_append_custom_fields($row['body'], $row['id_msg'], CFIELD_REPLY);
538
+
430 539
 				$msgOptions = array(
431 540
 					'subject' => $txt['response_prefix'] . $subject,
432 541
 					'body' => $row['body'],
... ...
@@ -622,7 +731,16 @@ function shd_tickettotopic2()
622 731
 				'ticket' => $context['ticket_id'],
623 732
 			)
624 733
 		);
625
-
734
+		// And custom fields.
735
+		shd_db_query('', '
736
+			DELETE FROM {db_prefix}helpdesk_custom_fields_values
737
+			WHERE (id_post = {int:ticket} AND post_type = 1)' . (!empty($context['ticket_messages']) ? '
738
+				OR (id_post IN ({array_int:msgs}) AND post_type = 2)' : ''),
739
+			array(
740
+				'ticket' => $context['ticket_id'],
741
+				'msgs' => $context['ticket_messages'],
742
+			)
743
+		);
626 744
 	}
627 745
 	else
628 746
 		fatal_lang_error('shd_move_topic_not_created',false);
... ...
@@ -632,7 +750,71 @@ function shd_tickettotopic2()
632 750
 
633 751
 	// Send them to the topic.
634 752
 	redirectexit('topic=' . $topic . '.0');
753
+}
754
+
755
+/**
756
+ *	Processes the custom fields for making them part of the body of the new post.
757
+ *
758
+ *	Expects $context['custom_fields'] to have been prepared already in {@link shd_tickettotopic2()}
759
+ *
760
+ *	@param string &$body The body of the message, to which the custom fields should be appended.
761
+ *	@param int $id_msg The message id to consider, or in the case of the ticket's opening post, the ticket id.
762
+ *	@param int $type Should contain either CFIELD_TICKET or CFIELD_REPLY to confirm whether we are looking at the ticket or the post.
763
+ *	@see shd_tickettotopic2()
764
+ *	@since 2.0
765
+*/
766
+function shd_append_custom_fields(&$body, $id_msg, $type)
767
+{
768
+	global $context, $txt;
769
+
770
+	$content = array();
771
+
772
+	foreach ($context['custom_fields'] as $field_id => $field)
773
+	{
774
+		if (empty($field['values'][$type][$id_msg]))
775
+			continue;
776
+
777
+		switch ($field['type'])
778
+		{
779
+			case CFIELD_TYPE_TEXT:
780
+			case CFIELD_TYPE_LARGETEXT:
781
+				// If the field was used for bbc, display bbc. Otherwise convert it to a nobbc entry.
782
+				if ($field['bbc'])
783
+					$item = $field['name'] . ': ' . $field['values'][$type][$id_msg];
784
+				else
785
+					$item = $field['name'] . ': [nobbc]' . strtr($field['values'][$type][$id_msg], array('[' => '[', ']' => ']', ':' => ':', '@' => '@')) . '[/nobbc]';
786
+				break;
787
+			case CFIELD_TYPE_INT:
788
+			case CFIELD_TYPE_FLOAT:
789
+				$item = $field['name'] . ': ' . $field['values'][$type][$id_msg];
790
+				break;
791
+			case CFIELD_TYPE_CHECKBOX:
792
+				$item = $field['name'] . ': ' . (!empty($field['values'][$type][$id_msg]) ? $txt['yes'] : $txt['no']);
793
+				break;
794
+			case CFIELD_TYPE_SELECT:
795
+			case CFIELD_TYPE_RADIO:
796
+				if (isset($field['options'][$field['values'][$type][$id_msg]]))
797
+					$item = $field['name'] . ': ' . $field['options'][$field['values'][$type][$id_msg]];
798
+				break;
799
+			case CFIELD_TYPE_MULTI:
800
+				$opts = array();
801
+				$values = explode(',', $field['values'][$type][$id_msg]);
802
+				foreach ($values as $value)
803
+					if (isset($field['options'][$value]))
804
+						$opts[] = $field['options'][$value];
805
+				if (!empty($opts))
806
+					$item = $field['name'] . ': ' . implode(', ', $opts);
807
+				break;
808
+
809
+
810
+		}
811
+		if (!empty($item))
812
+			$content[] = $item;
813
+		unset($item);
814
+	}
635 815
 
816
+	if (!empty($content))
817
+		$body .= '[hr]<br />' . implode('<br />', $content);
636 818
 }
637 819
 
638 820
 /**
... ...
@@ -56,7 +56,7 @@ function template_shd_tickettotopic()
56 56
 
57 57
 		foreach ($category['boards'] as $board)
58 58
 			echo '
59
-								<option value="', $board['id'], '"', $board['selected'] ? ' selected="selected"' : '', '>', $board['child_level'] > 0 ? str_repeat('==', $board['child_level']-1) . '=&gt; ' : '', $board['name'], '</option>';
59
+								<option value="', $board['id'], '">', $board['child_level'] > 0 ? str_repeat('==', $board['child_level']-1) . '=&gt; ' : '', $board['name'], '</option>';
60 60
 		echo '
61 61
 							</optgroup>';
62 62
 	}
63 63