forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.php
440 lines (382 loc) · 12.4 KB
/
categories.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* Allows to hide, show or delete the files assigend to the
* selected client.
*
* @package ProjectSend
* @subpackage Files
*/
$allowed_levels = array(9,8,7);
require_once('bootstrap.php');
$active_nav = 'files';
$page_title = __('Categories administration','cftp_admin');
include_once ADMIN_TEMPLATES_DIR . DS . 'header.php';
?>
<script type="text/javascript">
$(document).ready( function() {
$("#process_category").submit(function() {
clean_form( this );
is_complete( this.category_name, '<?php echo $json_strings['validation']['no_name']; ?>' );
// show the errors or continue if everything is ok
if (show_form_errors() == false) { return false; }
});
});
</script>
<?php
/**
* Messages set when adding or editing a category
*/
if ( !empty( $_GET['status'] ) ) {
$result_status = $_GET['status'];
switch ( $result_status ) {
case 'added':
$msg_text = __('The category was successfully created.','cftp_admin');
$msg_type = 'success';
break;
case 'edited':
$msg_text = __('The category was successfully edited.','cftp_admin');
$msg_type = 'success';
break;
}
echo system_message( $msg_type, $msg_text );
}
/**
* Apply the corresponding action to the selected categories.
*/
if ( isset( $_GET['action'] ) ) {
if ( $_GET['action'] != 'none' ) {
/** Continue only if 1 or more categories were selected. */
if ( !empty($_GET['batch'] ) ) {
/**
* Make a list of categories to avoid individual queries.
*/
$selected_categories = $_GET['batch'];
$get_categories_delete = get_categories(
array(
'id' => $selected_categories,
)
);
foreach ( $get_categories_delete['categories'] as $delete_cat ) {
$all_categories[$delete_cat['id']] = $delete_cat['name'];
}
$my_info = get_user_by_username(get_current_user_username());
$affected_users = 0;
switch($_GET['action']) {
case 'delete':
foreach ($selected_categories as $category) {
$this_category = new \ProjectSend\CategoriesActions();
$delete_category = $this_category->delete_category($category);
}
$msg = __('The selected categories were deleted.','cftp_admin');
echo system_message('success',$msg);
$log_action_number = 36;
break;
}
/** Record the action log */
foreach ($selected_categories as $category) {
global $logger;
$log_action_args = array(
'action' => $log_action_number,
'owner_id' => CURRENT_USER_ID,
'affected_account_name' => $all_categories[$category]
);
$new_record_action = $logger->add_entry($log_action_args);
}
}
else {
$msg = __('Please select at least one category.','cftp_admin');
echo system_message('danger',$msg);
}
}
}
/** Get all the existing categories */
$params = array();
$results_show = 'arranged';
/**
* Add the search terms
*/
if ( isset( $_GET['search'] ) && !empty( $_GET['search'] ) ) {
$params['search'] = $_GET['search'];
$results_show = 'categories'; // show from all categories, not the arranged array
}
$params['page'] = ( isset( $_GET["page"] ) ) ? $_GET["page"] : 1;
$page = $params['page'];
$get_categories = get_categories( $params );
if ( !empty( $get_categories['categories'] ) ) {
$categories = $get_categories['categories'];
$arranged = $get_categories['arranged'];
}
else {
$categories = null;
$arranged = null;
}
/**
* Adding or editing a category
*
* By default, the action is ADD category
*/
$form_information = array(
'type' => 'new_category',
'title' => __('Create new category','cftp_admin'),
);
/** Loading the form in EDIT mode */
if (
( !empty( $_GET['action'] ) && $_GET['action'] == 'edit' ) or
!empty( $_POST['editing_id'] )
) {
$action = 'edit';
$editing = !empty( $_POST['editing_id'] ) ? $_POST['editing_id'] : $_GET['id'];
$form_information = array(
'type' => 'edit_category',
'title' => __('Edit category','cftp_admin'),
);
/**
* Get the current information if just entering edit mode
*/
$category_name = $categories[$editing]['name'];
$category_parent = $categories[$editing]['parent'];
$category_description = $categories[$editing]['description'];
}
/**
* Process the action
*/
if ( isset( $_POST['btn_process'] ) ) {
/**
* Applies for both ADDING a new category as well
* as editing one but with the form already sent.
*/
$category_name = $_POST['category_name'];
$category_parent = $_POST['category_parent'];
$category_description = $_POST['category_description'];
$category_object = new \ProjectSend\CategoriesActions();
$arguments = array(
'name' => $category_name,
'parent' => $category_parent,
'description' => $category_description,
);
$validate = $category_object->validate_category( $arguments );
switch ( $form_information['type'] ) {
case 'new_category':
$arguments['action'] = 'add';
$redirect_status = 'added';
break;
case 'edit_category':
$arguments['action'] = 'edit';
$redirect_status = 'edited';
$arguments['id'] = ( $_POST ) ? $_POST['editing_id'] : $_GET['id'];
break;
}
if ( $validate === 1 ) {
$process = $category_object->save_category( $arguments );
if ( $process['query'] === 1 ) {
$redirect = true;
$status = $redirect_status;
}
else {
$msg = __('There was a problem saving to the database.','cftp_admin');
echo system_message('danger', $msg);
}
}
else {
$msg = __('Please complete all the required fields.','cftp_admin');
echo system_message('danger', $msg);
}
/** Redirect so the actions are reflected immediatly */
if ( isset( $redirect ) && $redirect === true ) {
while (ob_get_level()) ob_end_clean();
$location = BASE_URI . 'categories.php?status=' . $status;
header("Location: $location");
die();
}
}
?>
<div class="col-xs-12 col-sm-12 col-md-8">
<div class="form_actions_left">
<div class="form_actions_limit_results">
<?php show_search_form('categories.php'); ?>
</div>
</div>
<form action="categories.php" class="form-inline" name="selected_categories" id="selected_categories" method="get">
<div class="form_actions_right form-inline">
<div class="form_actions">
<div class="form_actions_submit">
<div class="form-group group_float">
<label class="control-label hidden-xs hidden-sm"><i class="glyphicon glyphicon-check"></i> <?php _e('Selected categories actions','cftp_admin'); ?>:</label>
<select name="action" id="action" class="txtfield form-control">
<?php
$actions_options = array(
'none' => __('Select action','cftp_admin'),
'delete' => __('Delete','cftp_admin'),
);
foreach ( $actions_options as $val => $text ) {
?>
<option value="<?php echo $val; ?>"><?php echo $text; ?></option>
<?php
}
?>
</select>
</div>
<button type="submit" name="do_action" id="do_action" class="btn btn-sm btn-default"><?php _e('Proceed','cftp_admin'); ?></button>
</div>
</div>
</div>
<div class="clear"></div>
<div class="form_actions_count">
<p class="form_count_total"><?php _e('Found','cftp_admin'); ?>: <span><?php echo $get_categories['count']; ?> <?php _e('categories','cftp_admin'); ?></span></p>
</div>
<div class="clear"></div>
<?php
if ( $get_categories['count'] == 0 ) {
if ( !empty( $get_categories['no_results_type'] ) ) {
switch ( $get_categories['no_results_type'] ) {
case 'search':
$no_results_message = __('Your search keywords returned no results.','cftp_admin');
break;
}
}
else {
$no_results_message = __('There are no categories yet.','cftp_admin');
}
echo system_message('danger', $no_results_message);
}
/**
* Generate the table using the class.
*/
$table_attributes = array(
'id' => 'categories_tbl',
'class' => 'footable table',
);
$table = new \ProjectSend\TableGenerate( $table_attributes );
$thead_columns = array(
array(
'select_all' => true,
'attributes' => array(
'class' => array( 'td_checkbox' ),
),
),
array(
'sort_url' => 'name',
'sort_default' => true,
'content' => __('Name','cftp_admin'),
),
array(
'content' => __('Files','cftp_admin'),
),
array(
'content' => __('Description','cftp_admin'),
'hide' => 'phone',
),
array(
'content' => __('View','cftp_admin'),
'hide' => 'phone',
),
array(
'content' => __('Actions','cftp_admin'),
'hide' => 'phone',
),
);
$table->thead( $thead_columns );
/**
* Having the formatting function here seems more convenient
* as the HTML layout is easier to edit on it's real context.
*/
$c = 0;
$pagination_page = $page;
$pagination_start = ( $pagination_page - 1 ) * RESULTS_PER_PAGE;
$limit_start = $pagination_start;
$limit_number = RESULTS_PER_PAGE;
$limit_amount = $limit_start + RESULTS_PER_PAGE;
$i = 0;
function format_category_row( $arranged ) {
global $table, $c, $i, $page, $pagination_page, $pagination_start, $limit_start, $limit_number, $limit_amount;
$c++;
if ( !empty( $arranged ) ) {
foreach ( $arranged as $category ) {
/**
* Horrible hacky way to limit results on the table.
* The real filtered results should come from the
* 'arranged' array of the get_categories results.
*/
$i++;
if ( $i > $limit_start && $i <= $limit_amount ) {
$table->add_row();
$depth = ( $category['depth'] > 0 ) ? str_repeat( '—', $category['depth'] ) . ' ' : false;
$total = $category['file_count'];
if ( $total > 0 ) {
$class = 'success';
$files_link = 'manage-files.php?category=' . $category['id'];
$files_button = 'btn-primary';
}
else {
$class = 'danger';
$files_link = 'javascript:void(0);';
$files_button = 'btn-default disabled';
}
$count_format = '<span class="label label-' . $class . '">' . $total . '</span>';
$tbody_cells = array(
array(
'checkbox' => true,
'value' => $category["id"],
),
array(
'content' => $depth . html_output($category["name"]),
'attributes' => array(
'data-value' => $c,
),
),
array(
'content' => $count_format,
),
array(
'content' => html_output( $category["description"] ),
),
array(
'content' => '<a href="'. $files_link .'" class="btn btn-sm ' . $files_button . '">' . __('Manage files','cftp_admin') . '</a>',
),
array(
'content' => '<a href="categories.php?action=edit&id=' . $category["id"] .'" class="btn btn-primary btn-sm"><i class="fa fa-pencil"></i><span class="button_label">' . __('Edit','cftp_admin') . '</span></a>'
),
);
foreach ( $tbody_cells as $cell ) {
$table->add_cell( $cell );
}
$table->end_row();
}
$children = $category['children'];
if ( !empty( $children ) ) {
format_category_row( $children );
}
}
}
}
if ( $get_categories['count'] > 0 ) {
format_category_row( $get_categories[$results_show] );
}
echo $table->render();
/**
* PAGINATION
*/
$pagination_args = array(
'link' => basename($_SERVER['SCRIPT_FILENAME']),
'current' => $params['page'],
'pages' => ceil( $get_categories['count'] / RESULTS_PER_PAGE ),
);
echo $table->pagination( $pagination_args );
?>
</form>
</div>
<div class="col-xs-12 col-sm-12 col-md-4">
<form action="categories.php" class="form-horizontal" name="process_category" id="process_category" method="post">
<input type="hidden" name="processing" id="processing" value="1">
<?php
if ( !empty( $action ) && $action == 'edit' ) {
?>
<input type="hidden" name="editing_id" id="editing_id" value="<?php echo $editing; ?>">
<?php
}
?>
<?php include_once FORMS_DIR . DS . 'categories.php'; ?>
</form>
</div>
<?php
include_once ADMIN_TEMPLATES_DIR . DS . 'footer.php';