1<?php
2
3# Copyright (c) 2010 - 2012  John Reese
4# Copyright (c) 2012 - 2021  MantisBT Team - mantisbt-dev@lists.sourceforge.net
5# Licensed under the MIT license
6
7form_security_validate("plugin_Snippets_list_action");
8
9$global = gpc_get_bool("global");
10
11if ($global) {
12	access_ensure_global_level(plugin_config_get("edit_global_threshold"));
13	$user_id = 0;
14} else {
15	access_ensure_global_level(plugin_config_get("edit_own_threshold"));
16	$user_id = auth_get_current_user_id();
17}
18
19$action = gpc_get_string("action");
20
21$snippet_list = gpc_get_int_array("snippet_list", array());
22
23if (count($snippet_list) < 1) {
24	form_security_purge("plugin_Snippets_list_action");
25	print_header_redirect(plugin_page("snippet_list", true) . Snippet::global_url($global));
26}
27
28$snippets = Snippet::load_by_id($snippet_list, $user_id);
29
30function array_object_properties($arr, $prop) {
31	$props = array();
32	foreach($arr as $key => $obj) {
33		$props[$key] = $obj->$prop;
34	}
35	return $props;
36}
37
38### DELETE
39if ($action == "delete") {
40	$snippet_names = array_object_properties(Snippet::clean($snippets), "name");
41	helper_ensure_confirmed(
42		plugin_lang_get("action_delete_confirm")
43		. "<br>" . implode( ", ", $snippet_names ),
44		plugin_lang_get("action_delete")
45	);
46	Snippet::delete_by_id(array_keys($snippets), $user_id);
47
48	form_security_purge("plugin_Snippets_list_action");
49	print_successful_redirect(plugin_page("snippet_list", true) . Snippet::global_url($global));
50
51### EDIT
52} elseif ($action == "edit") {
53	$snippets = Snippet::clean($snippets, Snippet::TARGET_FORM);
54	layout_page_header();
55	layout_page_begin();
56?>
57
58<br/>
59
60<div id="snippet-div" class="form-container">
61	<form action="<?php echo plugin_page( 'snippet_list_action' ) ?>" method="post">
62		<?php echo form_security_field("plugin_Snippets_list_action") ?>
63		<?php if ($global): ?><input type="hidden" name="global" value="true"/><?php endif ?>
64		<input type="hidden" name="action" value="update"/>
65
66<div class="widget-box widget-color-blue2">
67	<div class="widget-header widget-header-small">
68		<h4 class="widget-title lighter">
69			<i class="ace-icon fa fa-user"></i>
70			<?php echo plugin_lang_get( $global ? 'edit_global_title' : 'edit_title' ) ?>
71		</h4>
72	</div>
73	<div class="widget-body">
74		<div class="widget-main no-padding">
75			<div class="table-responsive">
76				<table class="table table-bordered table-condensed table-striped">
77
78		<fieldset>
79
80<?php
81	$first = true;
82	$single = count( $snippets ) == 1;
83
84	foreach( $snippets as $snippet ) {
85		if ( !$first ) {
86?>
87<tr class="spacer"><td></td></tr>
88<?php
89		}
90?>
91
92<tr>
93<?php
94		# Hide checkbox when operating on a single Snippet
95		if( !$single ) {
96?>
97<td class="center" rowspan="2">
98	<!--suppress HtmlFormInputWithoutLabel -->
99	<input type="checkbox" name="snippet_list[]" value="<?php echo $snippet->id ?>" checked="checked"/>
100</td>
101<?php
102		}
103?>
104<td class="category">
105<?php
106		# Add hidden field with Snippet id
107		if( $single ) {
108?>
109<input type="hidden" name="snippet_list[]" value="<?php echo $snippet->id ?>" checked="checked"/>
110<?php
111		}
112		echo plugin_lang_get("edit_name")
113?>
114</td>
115<td>
116	<!--suppress HtmlFormInputWithoutLabel -->
117	<input type="text" name="name_<?php echo $snippet->id ?>" size="40" value="<?php echo $snippet->name ?>"/>
118</td>
119</tr>
120
121<tr>
122	<td class="category">
123		<label for="value_<?php echo $snippet->id ?>">
124			<?php echo plugin_lang_get("edit_value") ?>
125		</label>
126	</td>
127	<td class="snippetspatternhelp">
128		<textarea id="value_<?php echo $snippet->id ?>" name="value_<?php echo $snippet->id ?>"
129				  cols="80" rows="6"><?php echo $snippet->value ?></textarea>
130	</td>
131</tr>
132
133<?php
134		$first = false;
135	}
136?>
137
138<tfoot>
139<tr>
140<td class="center" colspan="3"><input type="submit" value="<?php echo plugin_lang_get("action_edit") ?>"/></td>
141</tr>
142</tfoot>
143
144	</fieldset>
145</table>
146</form>
147</div>
148
149<?php
150	layout_page_end();
151
152### UPDATE
153} elseif ($action == "update") {
154	foreach($snippets as $snippet_id => $snippet) {
155		$new_name = gpc_get_string("name_{$snippet_id}");
156		$new_value = gpc_get_string("value_{$snippet_id}");
157
158		if ($snippet->name != $new_name
159			|| $snippet->value != $new_value)
160		{
161			if (!is_blank($new_name)) {
162				$snippet->name = $new_name;
163			}
164			if (!is_blank($new_value)) {
165				$snippet->value = $new_value;
166			}
167
168			$snippet->save();
169		}
170	}
171
172	form_security_purge("plugin_Snippets_list_action");
173	print_successful_redirect(plugin_page("snippet_list", true) . Snippet::global_url($global));
174
175}
176