+ Add ability to move canned replies between categories of replies. [Bug 713]
gruffen

gruffen commited on 2011-05-31 16:53:06
Showing 3 changed files, with 160 additions and 1 deletions.

... ...
@@ -398,6 +398,11 @@ $txt['shd_admin_cannedreplies_departments'] = 'Departments this canned reply is
398 398
 $txt['shd_admin_cannedreplies_notitle'] = 'No title was given for this canned reply, one must be provided.';
399 399
 $txt['shd_admin_cannedreplies_nobody'] = 'No body content was given for this canned reply, one must be provided.';
400 400
 $txt['shd_admin_cannedreplies_notcreated'] = 'The new reply could not be created.';
401
+$txt['shd_admin_cannedreplies_onlyonecat'] = 'You cannot move this reply to another category, there is only one category of replies.';
402
+$txt['shd_admin_cannedreplies_newcategory'] = 'The new category this reply should belong to';
403
+$txt['shd_admin_cannedreplies_selectcat'] = '-- Select a category --';
404
+$txt['shd_admin_cannedreplies_movereply'] = 'Move This Reply';
405
+$txt['shd_admin_cannedreplies_destnoexist'] = 'The category you are trying to move this reply to does not exist.';
401 406
 
402 407
 //! Departments
403 408
 //@{
... ...
@@ -812,4 +812,105 @@ function shd_admin_canned_savereply()
812 812
 	redirectexit('action=admin;area=helpdesk_cannedreplies');
813 813
 }
814 814
 
815
+function shd_admin_canned_movereplycat()
816
+{
817
+	global $context, $smcFunc, $txt, $sourcedir, $scripturl;
818
+
819
+	// Before we go any further, establish that the user specified a reply to move and that there is at least one category not including the one the reply is in.
820
+	$_REQUEST['reply'] = isset($_REQUEST['reply']) ? (int) $_REQUEST['reply'] : 0;
821
+	if (empty($_REQUEST['reply']) || $_REQUEST['reply'] < 0)
822
+		fatal_lang_error('shd_admin_cannedreplies_thereplyisalie', false);
823
+
824
+	$query = $smcFunc['db_query']('', '
825
+		SELECT id_cat, reply_order
826
+		FROM {db_prefix}helpdesk_cannedreplies
827
+		WHERE id_reply = {int:reply}',
828
+		array(
829
+			'reply' => $_REQUEST['reply'],
830
+		)
831
+	);
832
+	if ($smcFunc['db_num_rows']($query) == 0)
833
+		fatal_lang_error('shd_admin_cannedreplies_thereplyisalie', false);
834
+
835
+	list($current_cat, $current_reply_pos) = $smcFunc['db_fetch_row']($query);
836
+	$smcFunc['db_free_result']($query);
837
+
838
+	// So, the reply exists. Now to check categories. We need to verify it regardless of calling context here, so might as well get the entire table.
839
+	$context['cannedreply_cats'] = array();
840
+	$query = $smcFunc['db_query']('', '
841
+		SELECT id_cat, cat_name
842
+		FROM {db_prefix}helpdesk_cannedreplies_cats
843
+		WHERE id_cat != {int:current_cat}
844
+		ORDER BY cat_order',
845
+		array(
846
+			'current_cat' => $current_cat,
847
+		)
848
+	);
849
+	if ($smcFunc['db_num_rows']($query) == 0)
850
+		fatal_lang_error('shd_admin_cannedreplies_onlyonecat', false);
851
+	while ($row = $smcFunc['db_fetch_assoc']($query))
852
+		$context['cannedreply_cats'][$row['id_cat']] = $row['cat_name'];
853
+	$smcFunc['db_free_result']($query);
854
+
855
+	// So, either we're moving, or we're displaying the form. Either way, it's time to make that decision.
856
+	if (empty($_GET['part']) || $_GET['part'] != '2')
857
+	{
858
+		$context['page_title'] = $txt['shd_admin_cannedreplies_move_between_cat'];
859
+		$context['sub_template'] = 'shd_move_reply_cat';
860
+
861
+		checkSubmitOnce('register');
862
+	}
863
+	else
864
+	{
865
+		// OK, so they're moving. We know the reply exists, we know the possible list of departments they can move to.
866
+		// 1. Is the new department valid?
867
+		$_REQUEST['newcat'] = isset($_REQUEST['newcat']) ? (int) $_REQUEST['newcat'] : 0;
868
+		if (!isset($context['cannedreply_cats'][$_REQUEST['newcat']]))
869
+			fatal_lang_error('shd_admin_cannedreplies_destnoexist', false);
870
+
871
+		// 1a. Everything is valid, just double check it's not a random double submission.
872
+		checkSubmitOnce('check');
873
+
874
+		// 2. Everything's OK. Figure out where the reply will move to in the new category.
875
+		$query = $smcFunc['db_query']('', '
876
+			SELECT MAX(reply_order)
877
+			FROM {db_prefix}helpdesk_cannedreplies
878
+			WHERE id_cat = {int:newcat}',
879
+			array(
880
+				'newcat' => $_REQUEST['newcat'],
881
+			)
882
+		);
883
+		list($newpos) = $smcFunc['db_fetch_row']($query);
884
+		$smcFunc['db_free_result']($query);
885
+
886
+		// 3. Move the reply.
887
+		$smcFunc['db_query']('', '
888
+			UPDATE {db_prefix}helpdesk_cannedreplies
889
+			SET id_cat = {int:newcat},
890
+				reply_order = {int:newpos}
891
+			WHERE id_reply = {int:reply}',
892
+			array(
893
+				'newcat' => $_REQUEST['newcat'],
894
+				'newpos' => (int) $newpos + 1,
895
+				'reply' => $_REQUEST['reply'],
896
+			)
897
+		);
898
+
899
+		// 4. Shunt the rest back down.
900
+		$smcFunc['db_query']('', '
901
+			UPDATE {db_prefix}helpdesk_cannedreplies
902
+			SET reply_order = reply_order - 1
903
+			WHERE id_cat = {int:current_cat}
904
+				AND reply_order > {int:current_pos}',
905
+			array(
906
+				'current_cat' => $current_cat,
907
+				'current_pos' => $current_reply_pos,
908
+			)
909
+		);
910
+
911
+		// 5. Scram.
912
+		redirectexit('action=admin;area=helpdesk_cannedreplies');
913
+	}
914
+}
915
+
815 916
 ?>
816 917
\ No newline at end of file
... ...
@@ -92,7 +92,7 @@ function template_shd_cannedreplies_home()
92 92
 							</td>
93 93
 							<td>', !empty($reply['move_up']) ? ('<a href="' . $scripturl . '?action=admin;area=helpdesk_cannedreplies;sa=movereply;reply=' . $reply['id_reply'] . ';direction=up;' . $context['session_var'] . '=' . $context['session_id'] . '"><img src="' . $settings['default_images_url'] . '/simpledesk/move_up.png" alt="' . $txt['shd_admin_move_up'] . '" title="' . $txt['shd_admin_move_up'] . '" /></a>') : '', '</td>
94 94
 							<td>', !empty($reply['move_down']) ? ('<a href="' . $scripturl . '?action=admin;area=helpdesk_cannedreplies;sa=movereply;reply=' . $reply['id_reply'] . ';direction=down;' . $context['session_var'] . '=' . $context['session_id'] . '"><img src="' . $settings['default_images_url'] . '/simpledesk/move_down.png" alt="' . $txt['shd_admin_move_down'] . '" title="' . $txt['shd_admin_move_down'] . '" /></a>') : '', '</td>
95
-							<td>', $context['move_between_cats'] ? ('<a href="' . $scripturl . '?action=admin;area=helpdesk_cannedreplies;reply=' . $reply['id_reply'] . '"><img src="' . $settings['default_images_url'] . '/simpledesk/movedept.png" alt="' . $txt['shd_admin_cannedreplies_move_between_cat'] . '" title="' . $txt['shd_admin_cannedreplies_move_between_cat'] . '" /></a>') : '', '</td>
95
+							<td>', $context['move_between_cats'] ? ('<a href="' . $scripturl . '?action=admin;area=helpdesk_cannedreplies;sa=movereplycat;reply=' . $reply['id_reply'] . '"><img src="' . $settings['default_images_url'] . '/simpledesk/movedept.png" alt="' . $txt['shd_admin_cannedreplies_move_between_cat'] . '" title="' . $txt['shd_admin_cannedreplies_move_between_cat'] . '" /></a>') : '', '</td>
96 96
 							<td><a href="', $scripturl, '?action=admin;area=helpdesk_cannedreplies;sa=editreply;reply=' . $reply['id_reply'] . ';', $context['session_var'], '=', $context['session_id'], '"><img src="', $settings['default_images_url'], '/simpledesk/edit.png" class="icon" alt="', $txt['shd_ticket_edit'],'" title="', $txt['shd_ticket_edit'], '" /></a></td>
97 97
 							<td><a href="', $scripturl, '?action=admin;area=helpdesk_cannedreplies;sa=savereply;reply=' . $reply['id_reply'] . ';delete=yes;', $context['session_var'], '=', $context['session_id'], '" onclick="return confirm(' . JavaScriptEscape($txt['shd_admin_cannedreplies_deletereply_confirm']). ');"><img src="', $settings['default_images_url'], '/simpledesk/delete.png" class="icon" alt="', $txt['shd_ticket_delete'],'" title="', $txt['shd_ticket_delete'], '" /></a></td>
98 98
 						</tr>';
... ...
@@ -248,4 +248,57 @@ function template_shd_edit_canned_reply()
248 248
 				</form>
249 249
 				<br />';
250 250
 }
251
+
252
+function template_shd_move_reply_cat()
253
+{
254
+	global $context, $settings, $txt, $modSettings, $scripturl;
255
+
256
+	echo '
257
+				<div class="tborder">
258
+					<div class="cat_bar">
259
+						<h3 class="catbg">
260
+							<img src="', $settings['default_images_url'], '/simpledesk/cannedreplies.png" class="icon" alt="*" />
261
+							', $txt['shd_admin_cannedreplies_home'], '
262
+						</h3>
263
+					</div>
264
+					<p class="description">
265
+						', $txt['shd_admin_cannedreplies_homedesc'], '
266
+					</p>
267
+				</div>
268
+				<div class="cat_bar grid_header">
269
+					<h3 class="catbg">
270
+						<img src="', $settings['default_images_url'], '/simpledesk/movedept.png" alt="*" />
271
+						', $context['page_title'], '
272
+					</h3>
273
+				</div>
274
+				<div class="roundframe">
275
+					<form action="', $scripturl, '?action=admin;area=helpdesk_cannedreplies;sa=movereplycat;part=2" method="post">
276
+						<div class="content">
277
+							<dl class="settings">
278
+								<dt><strong>', $txt['shd_admin_cannedreplies_newcategory'], '</strong></dt>
279
+								<dd>
280
+									<select name="newcat">
281
+										<option value="0">', $txt['shd_admin_cannedreplies_selectcat'], '</option>';
282
+
283
+	foreach ($context['cannedreply_cats'] as $cat_id => $cat_name)
284
+		echo '
285
+										<option value="', $cat_id, '">', $cat_name, '</option>';
286
+
287
+	echo '
288
+									</select>
289
+								</dd>
290
+							</dl>
291
+						</div>
292
+						<input type="submit" value="', $txt['shd_admin_cannedreplies_movereply'], '" onclick="return submitThisOnce(this);" class="button_submit" />';
293
+
294
+
295
+	echo '
296
+						<input type="hidden" name="reply" value="', $_REQUEST['reply'], '" />
297
+						<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
298
+						<input type="hidden" name="seqnum" value="', $context['form_sequence_number'], '" />
299
+					</form>
300
+				</div>
301
+				<span class="lowerframe"><span></span></span>';
302
+}
303
+
251 304
 ?>
252 305
\ No newline at end of file
253 306