| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller; |
| 4 |
|
| 5 |
use FileBird\Model\Folder as FolderModel; |
| 6 |
use FileBird\Model\SettingModel; |
| 7 |
use FileBird\Classes\Tree; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
class FolderController extends Controller { |
| 11 |
public function getFolders( \WP_REST_Request $request ) { |
| 12 |
$order = sanitize_key( $request->get_param( 'order' ) ); |
| 13 |
$orderby = sanitize_key( $request->get_param( 'orderby' ) ); |
| 14 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 15 |
$search = sanitize_text_field( $request->get_param( 'search' ) ); |
| 16 |
|
| 17 |
$tree = ( new Tree( $orderby, $order, $search ) )->get(); |
| 18 |
|
| 19 |
return rest_ensure_response( |
| 20 |
array( |
| 21 |
'tree' => $tree, |
| 22 |
'allAttachmentsCount' => Tree::getCount( -1, $lang ), |
| 23 |
'attachmentsCount' => FolderModel::countAttachments( $lang ), |
| 24 |
) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function setFolderCounter( \WP_REST_Request $request ) { |
| 29 |
$type = sanitize_key( $request->get_param( 'type' ) ); |
| 30 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 31 |
|
| 32 |
// add_filter( |
| 33 |
// 'fbv_counter_type', |
| 34 |
// function() use ( $type ) { |
| 35 |
// return $type; |
| 36 |
// } |
| 37 |
// ); |
| 38 |
|
| 39 |
SettingModel::getInstance()->setFolderCounterType( $type ); |
| 40 |
|
| 41 |
return rest_ensure_response( FolderModel::countAttachments( $lang ) ); |
| 42 |
} |
| 43 |
|
| 44 |
public function updateFolderColor( \WP_REST_Request $request ) { |
| 45 |
$id = sanitize_key( $request->get_param( 'folderId' ) ); |
| 46 |
$color = sanitize_hex_color( $request->get_param( 'color' ) ); |
| 47 |
|
| 48 |
$option = get_option( 'fbv_folder_colors', array() ); |
| 49 |
$option[ $id ] = $color; |
| 50 |
|
| 51 |
update_option( 'fbv_folder_colors', $option ); |
| 52 |
|
| 53 |
return rest_ensure_response( true ); |
| 54 |
} |
| 55 |
|
| 56 |
public function createFolder( \WP_REST_Request $request ) { |
| 57 |
$name = $request->get_param( 'title' ); |
| 58 |
$parent = $request->get_param( 'parent' ); |
| 59 |
$name = isset( $name ) ? sanitize_text_field( wp_unslash( $name ) ) : ''; |
| 60 |
$parent = isset( $parent ) ? sanitize_text_field( $parent ) : ''; |
| 61 |
if ( $name != '' && $parent != '' ) { |
| 62 |
$insert = FolderModel::newOrGet( $name, $parent, false ); |
| 63 |
if ( $insert !== false ) { |
| 64 |
return rest_ensure_response( $insert ); |
| 65 |
} else { |
| 66 |
return new \WP_Error( 'folder_name_exist', __( 'A folder with this name already exists. Please choose another one.', 'filebird' ) ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
return new \WP_Error( 'validation_failed', __( 'Validation failed', 'filebird' ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function updateFolder( \WP_REST_Request $request ) { |
| 74 |
$id = $request->get_param( 'id' ); |
| 75 |
$parent = $request->get_param( 'parent' ); |
| 76 |
$name = $request->get_param( 'title' ); |
| 77 |
|
| 78 |
$id = isset( $id ) ? sanitize_text_field( $id ) : ''; |
| 79 |
$parent = isset( $parent ) ? intval( sanitize_text_field( $parent ) ) : ''; |
| 80 |
$name = isset( $name ) ? sanitize_text_field( wp_unslash( $name ) ) : ''; |
| 81 |
|
| 82 |
$current_user_id = get_current_user_id(); |
| 83 |
$folder_per_user = get_option( 'njt_fbv_folder_per_user', '0' ) === '1'; |
| 84 |
|
| 85 |
if ( is_numeric( $id ) && is_numeric( $parent ) && $name != '' && FolderModel::verifyAuthor( $id, $current_user_id, $folder_per_user ) ) { |
| 86 |
$update = FolderModel::updateFolderName( $name, $parent, $id ); |
| 87 |
if ( true === $update ) { |
| 88 |
return rest_ensure_response( $update ); |
| 89 |
} else { |
| 90 |
return new \WP_Error( 'folder_name_exist', __( 'A folder with this name already exists. Please choose another one.', 'filebird' ) ); |
| 91 |
} |
| 92 |
} |
| 93 |
return new \WP_Error( 'validation_failed', __( 'Validation failed', 'filebird' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
public function updateFolderOrder( \WP_REST_Request $request ) { |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$data = array( |
| 100 |
'dropPosition' => $request->get_param( 'dropPosition' ), |
| 101 |
'dropNodeId' => $request->get_param( 'dropNodeId' ), |
| 102 |
'dragNodeId' => $request->get_param( 'dragNodeId' ), |
| 103 |
'toParentId' => $request->get_param( 'toParentId' ), |
| 104 |
); |
| 105 |
|
| 106 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 107 |
|
| 108 |
foreach ( $data as $param ) { |
| 109 |
if ( strlen( $param ) === 0 || ! is_numeric( $param ) ) { |
| 110 |
return new \WP_Error( 'invalid_params', __( 'Invalid params', 'filebird' ), array( 'status' => 400 ) ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$old_node = $wpdb->get_row( |
| 115 |
$wpdb->prepare( |
| 116 |
"SELECT id, parent, ord FROM {$wpdb->prefix}fbv WHERE `id` = %d AND `created_by` = %d", |
| 117 |
$data['dropNodeId'], |
| 118 |
apply_filters( 'fbv_folder_created_by', 0 ) |
| 119 |
), |
| 120 |
ARRAY_A |
| 121 |
); |
| 122 |
|
| 123 |
// Drop folder inside another folder |
| 124 |
if ( 0 === $data['dropPosition'] ) { |
| 125 |
$children = $wpdb->get_results( |
| 126 |
$wpdb->prepare( |
| 127 |
"SELECT id, ord FROM {$wpdb->prefix}fbv WHERE (parent = %d and created_by = %d and id != %d) ORDER BY ord+0 ASC", |
| 128 |
$data['dropNodeId'], |
| 129 |
apply_filters( 'fbv_folder_created_by', 0 ), |
| 130 |
$data['dragNodeId'] |
| 131 |
) |
| 132 |
); |
| 133 |
|
| 134 |
array_unshift( |
| 135 |
$children, |
| 136 |
(object) array( |
| 137 |
'id' => $data['dragNodeId'], |
| 138 |
'ord' => 0, |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
foreach ( $children as $key => $child ) { |
| 143 |
$wpdb->update( |
| 144 |
"{$wpdb->prefix}fbv", |
| 145 |
array( |
| 146 |
'ord' => $key + 1, |
| 147 |
'parent' => $data['dropNodeId'], |
| 148 |
), |
| 149 |
array( |
| 150 |
'id' => $child->id, |
| 151 |
), |
| 152 |
array( '%d', '%d' ), |
| 153 |
array( '%d' ) |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if ( 1 === $data['dropPosition'] ) { |
| 159 |
$children = $wpdb->get_results( |
| 160 |
$wpdb->prepare( |
| 161 |
"SELECT id, ord FROM {$wpdb->prefix}fbv WHERE (parent = %d and created_by = %d and id != %d) ORDER BY ord+0 ASC", |
| 162 |
$data['toParentId'], |
| 163 |
apply_filters( 'fbv_folder_created_by', 0 ), |
| 164 |
$data['dragNodeId'] |
| 165 |
) |
| 166 |
); |
| 167 |
|
| 168 |
$ord = 0; |
| 169 |
|
| 170 |
foreach ( $children as $child ) { |
| 171 |
$wpdb->update( |
| 172 |
"{$wpdb->prefix}fbv", |
| 173 |
array( |
| 174 |
'ord' => $ord++, |
| 175 |
'parent' => $data['toParentId'], |
| 176 |
), |
| 177 |
array( |
| 178 |
'id' => $child->id, |
| 179 |
), |
| 180 |
array( '%d', '%d' ), |
| 181 |
array( '%d' ) |
| 182 |
); |
| 183 |
if ( intval( $child->id ) === $data['dropNodeId'] ) { |
| 184 |
$wpdb->update( |
| 185 |
"{$wpdb->prefix}fbv", |
| 186 |
array( |
| 187 |
'ord' => $ord++, |
| 188 |
'parent' => $data['toParentId'], |
| 189 |
), |
| 190 |
array( |
| 191 |
'id' => $data['dragNodeId'], |
| 192 |
), |
| 193 |
array( '%d', '%d' ), |
| 194 |
array( '%d' ) |
| 195 |
); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if ( -1 === $data['dropPosition'] ) { |
| 201 |
$children = $wpdb->get_results( |
| 202 |
$wpdb->prepare( |
| 203 |
"SELECT id, ord FROM {$wpdb->prefix}fbv WHERE (parent = %d and created_by = %d and id != %d) ORDER BY ord+0 ASC", |
| 204 |
$data['toParentId'], |
| 205 |
apply_filters( 'fbv_folder_created_by', 0 ), |
| 206 |
$data['dragNodeId'] |
| 207 |
) |
| 208 |
); |
| 209 |
|
| 210 |
array_unshift( |
| 211 |
$children, |
| 212 |
(object) array( |
| 213 |
'id' => $data['dragNodeId'], |
| 214 |
'ord' => 0, |
| 215 |
) |
| 216 |
); |
| 217 |
|
| 218 |
foreach ( $children as $key => $child ) { |
| 219 |
$wpdb->update( |
| 220 |
"{$wpdb->prefix}fbv", |
| 221 |
array( |
| 222 |
'ord' => $key + 1, |
| 223 |
'parent' => $data['toParentId'], |
| 224 |
), |
| 225 |
array( |
| 226 |
'id' => $child->id, |
| 227 |
), |
| 228 |
array( '%d', '%d' ), |
| 229 |
array( '%d' ) |
| 230 |
); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if( ! is_null( $old_node ) && ( $old_node['parent'] != $data['toParentId'] ) ) { |
| 235 |
do_action( 'fbv_folder_parent_updated', $data['dragNodeId'], $data['toParentId'] ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( SettingModel::getInstance()->get( 'FOLDER_COUNTER_TYPE' ) === 'counter_file_in_folder_and_sub' ) { |
| 239 |
return rest_ensure_response( FolderModel::countAttachments( $lang ) ); |
| 240 |
} |
| 241 |
|
| 242 |
return rest_ensure_response( true ); |
| 243 |
} |
| 244 |
|
| 245 |
public function deleteFolder( \WP_REST_Request $request ) { |
| 246 |
$ids = $request->get_param( 'ids' ); |
| 247 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 248 |
|
| 249 |
if ( empty( $ids ) ) { |
| 250 |
return new \WP_Error( 'delete_failed', __( 'Can\'t delete folder, please try again later', 'filebird' ) ); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
$ids = array_map( 'intval', $ids ); |
| 255 |
|
| 256 |
$current_user_id = get_current_user_id(); |
| 257 |
$folder_per_user = get_option( 'njt_fbv_folder_per_user', '0' ) === '1'; |
| 258 |
foreach ( $ids as $k => $id ) { |
| 259 |
if ( ! FolderModel::verifyAuthor( $id, $current_user_id, $folder_per_user ) ) { |
| 260 |
unset( $ids[ $k ] ); |
| 261 |
} |
| 262 |
} |
| 263 |
$folderCounter = FolderModel::delete( $ids, $lang ); |
| 264 |
|
| 265 |
return rest_ensure_response( $folderCounter ); |
| 266 |
} |
| 267 |
|
| 268 |
public function assignFolder( \WP_REST_Request $request ) { |
| 269 |
$folderId = $request->get_param( 'folderId' ); |
| 270 |
$ids = $request->get_param( 'ids' ); |
| 271 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 272 |
|
| 273 |
if ( empty( $folderId ) && ! is_numeric( $folderId ) && empty( $ids ) ) { |
| 274 |
return new \WP_Error( 'validation_failed', __( 'Validation failed', 'filebird' ) ); |
| 275 |
} |
| 276 |
|
| 277 |
$ids = array_map( 'intval', apply_filters( 'fbv_ids_assigned_to_folder', $ids ) ); |
| 278 |
|
| 279 |
$folderCounter = FolderModel::assignFolder( $folderId, $ids, $lang ); |
| 280 |
|
| 281 |
return rest_ensure_response( $folderCounter ); |
| 282 |
} |
| 283 |
|
| 284 |
public function gutenbergGetFolder() { |
| 285 |
$_folders = Tree::getFolders( null, null, true ); |
| 286 |
$folders = array( |
| 287 |
array( |
| 288 |
'value' => 0, |
| 289 |
'label' => __( 'Please choose folder', 'filebird' ), |
| 290 |
'disabled' => true, |
| 291 |
), |
| 292 |
); |
| 293 |
foreach ( $_folders as $k => $v ) { |
| 294 |
$folders[] = array( |
| 295 |
'value' => $v['id'], |
| 296 |
'label' => $v['text'], |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
wp_send_json_success( $folders ); |
| 301 |
} |
| 302 |
} |
| 303 |
|