| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Model; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Folder { |
| 8 |
|
| 9 |
|
| 10 |
private static $folder_table = 'fbv'; |
| 11 |
private static $relation_table = 'fbv_attachment_folder'; |
| 12 |
|
| 13 |
public static function allFolders( $select = '*', $prepend_default = null, $order_by = null ) { |
| 14 |
//TODO need to convert ord to number using +0 |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
$where = array( 'created_by' => apply_filters( 'fbv_in_not_in_created_by', '0' ) ); |
| 18 |
if ( \is_null( $order_by ) ) { |
| 19 |
$order_by = 'ord+0, id, name'; |
| 20 |
} |
| 21 |
$order_by = apply_filters( 'fbv_order_by', $order_by ); |
| 22 |
|
| 23 |
$conditions = array( '1 = 1' ); |
| 24 |
foreach ( $where as $field => $value ) { |
| 25 |
$conditions[] = "`$field` = " . $value; |
| 26 |
} |
| 27 |
$conditions = implode( ' AND ', $conditions ); |
| 28 |
$sql = "SELECT $select FROM " . self::getTable( self::$folder_table ) . ' WHERE ' . $conditions . ' ORDER BY ' . $order_by; |
| 29 |
|
| 30 |
$folders = $wpdb->get_results( $sql ); |
| 31 |
if ( is_array( $prepend_default ) ) { |
| 32 |
$all = new \stdClass(); |
| 33 |
$all->{$prepend_default[0]} = -1; |
| 34 |
$all->{$prepend_default[1]} = __( 'All Folders', 'filebird' ); |
| 35 |
|
| 36 |
$uncategorized = new \stdClass(); |
| 37 |
$uncategorized->{$prepend_default[0]} = 0; |
| 38 |
$uncategorized->{$prepend_default[1]} = __( 'Uncategorized', 'filebird' ); |
| 39 |
|
| 40 |
array_unshift( $folders, $all, $uncategorized ); |
| 41 |
} |
| 42 |
return $folders; |
| 43 |
} |
| 44 |
public static function countFolder() { |
| 45 |
global $wpdb; |
| 46 |
return $wpdb->get_var( 'SELECT count(*) as c FROM ' . self::getTable( self::$folder_table ) ); |
| 47 |
} |
| 48 |
public static function getRelations() { |
| 49 |
global $wpdb; |
| 50 |
$query = "SELECT `attachment_id`, GROUP_CONCAT(`folder_id`) as folders FROM `{$wpdb->prefix}fbv_attachment_folder` GROUP BY `attachment_id`"; |
| 51 |
$relations = $wpdb->get_results( $query ); |
| 52 |
$res = array(); |
| 53 |
foreach ( $relations as $k => $v ) { |
| 54 |
$res[ $v->attachment_id ] = array_map( 'intval', explode( ',', $v->folders ) ); |
| 55 |
} |
| 56 |
return $res; |
| 57 |
} |
| 58 |
public static function updateOrdAndParent( $id, $new_ord, $new_parent ) { |
| 59 |
global $wpdb; |
| 60 |
$wpdb->update( |
| 61 |
self::getTable( self::$folder_table ), |
| 62 |
array( |
| 63 |
'parent' => $new_parent, |
| 64 |
'ord' => $new_ord, |
| 65 |
), |
| 66 |
array( 'id' => $id ), |
| 67 |
array( '%d', '%d' ), |
| 68 |
array( '%d' ) |
| 69 |
); |
| 70 |
} |
| 71 |
public static function updateAuthor( $from_author, $to_author ) { |
| 72 |
global $wpdb; |
| 73 |
$wpdb->update( |
| 74 |
self::getTable( self::$folder_table ), |
| 75 |
array( |
| 76 |
'created_by' => $to_author, |
| 77 |
), |
| 78 |
array( 'created_by' => $from_author ), |
| 79 |
array( '%d' ), |
| 80 |
array( '%d' ) |
| 81 |
); |
| 82 |
} |
| 83 |
public static function deleteByAuthor( $author ) { |
| 84 |
global $wpdb; |
| 85 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder WHERE folder_id IN (SELECT id FROM {$wpdb->prefix}fbv WHERE created_by = " . (int) $author . ')' ); |
| 86 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}fbv WHERE created_by = " . (int) $author ); |
| 87 |
} |
| 88 |
public static function rawInsert( $query ) { |
| 89 |
global $wpdb; |
| 90 |
$wpdb->query( 'INSERT INTO ' . self::getTable( self::$folder_table ) . ' ' . $query ); |
| 91 |
} |
| 92 |
public static function getFoldersOfPost( $post_id ) { |
| 93 |
global $wpdb; |
| 94 |
return $wpdb->get_col( 'SELECT `folder_id` FROM ' . self::getTable( self::$relation_table ) . ' WHERE `attachment_id` = ' . (int) $post_id . ' GROUP BY `folder_id`' ); |
| 95 |
} |
| 96 |
public static function getFolderFromPostId( $post_id ) { |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$created = 0; |
| 100 |
$user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1'; |
| 101 |
if ( $user_has_own_folder ) { |
| 102 |
$created = get_current_user_id(); |
| 103 |
} |
| 104 |
return $wpdb->get_results( |
| 105 |
$wpdb->prepare( |
| 106 |
"SELECT `folder_id`,`name` FROM {$wpdb->prefix}fbv as fbv |
| 107 |
JOIN {$wpdb->prefix}fbv_attachment_folder as fbva ON fbv.id = fbva.folder_id |
| 108 |
WHERE `attachment_id` = %d AND `created_by` = %d GROUP BY `folder_id`", |
| 109 |
$post_id, |
| 110 |
$created |
| 111 |
), |
| 112 |
OBJECT |
| 113 |
); |
| 114 |
} |
| 115 |
public static function setFoldersForPosts( $post_ids, $folder_ids, $has_action = true ) { |
| 116 |
global $wpdb; |
| 117 |
if ( ! is_array( $post_ids ) ) { |
| 118 |
$post_ids = array( $post_ids ); |
| 119 |
} |
| 120 |
if ( ! is_array( $folder_ids ) ) { |
| 121 |
$folder_ids = array( $folder_ids ); |
| 122 |
} |
| 123 |
$user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1'; |
| 124 |
$current_user_id = get_current_user_id(); |
| 125 |
|
| 126 |
foreach ( $folder_ids as $k => $folder_id ) { |
| 127 |
foreach ( $post_ids as $k2 => $post_id ) { |
| 128 |
do_action( 'fbv_before_setting_folder', (int) $post_id, (int) $folder_id ); |
| 129 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder WHERE `attachment_id` = %d AND `folder_id` IN (SELECT `id` FROM {$wpdb->prefix}fbv WHERE `created_by` = %d)", (int) $post_id, $user_has_own_folder ? $current_user_id : 0 ) ); |
| 130 |
if ( $folder_id > 0 ) { |
| 131 |
$wpdb->insert( |
| 132 |
self::getTable( self::$relation_table ), |
| 133 |
array( |
| 134 |
'attachment_id' => (int) $post_id, |
| 135 |
'folder_id' => (int) $folder_id, |
| 136 |
), |
| 137 |
array( '%d', '%d' ) |
| 138 |
); |
| 139 |
} |
| 140 |
if ( $has_action === true ) { |
| 141 |
do_action( 'fbv_after_set_folder', $post_id, $folder_id ); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
if ( count( $post_ids ) > 0 ) { |
| 146 |
clean_post_cache( $post_ids[0] ); |
| 147 |
} |
| 148 |
} |
| 149 |
public static function detail( $name, $parent ) { |
| 150 |
global $wpdb; |
| 151 |
|
| 152 |
$user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1'; |
| 153 |
if ( $user_has_own_folder ) { |
| 154 |
$created = get_current_user_id(); |
| 155 |
} else { |
| 156 |
$created = 0; |
| 157 |
} |
| 158 |
return $wpdb->get_row( |
| 159 |
$wpdb->prepare( |
| 160 |
"SELECT * FROM {$wpdb->prefix}fbv WHERE `name` = %s AND `parent` = %d AND `created_by` = %d", |
| 161 |
$name, |
| 162 |
$parent, |
| 163 |
$created |
| 164 |
) |
| 165 |
); |
| 166 |
} |
| 167 |
public static function findById( $folder_id, $select = 'id' ) { |
| 168 |
global $wpdb; |
| 169 |
$query = 'SELECT ' . $select . ' FROM ' . self::getTable( self::$folder_table ) . " WHERE `id` = '" . (int) $folder_id . "'"; |
| 170 |
return $wpdb->get_row( $query ); |
| 171 |
} |
| 172 |
public static function updateFolderName( $new_name, $parent, $folder_id ) { |
| 173 |
global $wpdb; |
| 174 |
$check_name = $wpdb->get_row( |
| 175 |
$wpdb->prepare( |
| 176 |
"SELECT * FROM {$wpdb->prefix}fbv WHERE id != %d AND name = %s AND parent = %d", |
| 177 |
$folder_id, |
| 178 |
$new_name, |
| 179 |
$parent |
| 180 |
) |
| 181 |
); |
| 182 |
if ( \is_null( $check_name ) ) { |
| 183 |
$wpdb->update( |
| 184 |
self::getTable( self::$folder_table ), |
| 185 |
array( 'name' => $new_name ), |
| 186 |
array( 'id' => $folder_id ), |
| 187 |
array( '%s' ), |
| 188 |
array( '%d' ) |
| 189 |
); |
| 190 |
return true; |
| 191 |
} |
| 192 |
return false; |
| 193 |
} |
| 194 |
public static function updateParent( $folder_id, $new_parent ) { |
| 195 |
global $wpdb; |
| 196 |
$wpdb->update( |
| 197 |
self::getTable( self::$folder_table ), |
| 198 |
array( 'parent' => $new_parent ), |
| 199 |
array( 'id' => $folder_id ), |
| 200 |
array( '%d' ), |
| 201 |
array( '%d' ) |
| 202 |
); |
| 203 |
} |
| 204 |
public static function deleteAll() { |
| 205 |
global $wpdb; |
| 206 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}fbv" ); |
| 207 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder" ); |
| 208 |
} |
| 209 |
public static function deleteFolderAndItsChildren( $id ) { |
| 210 |
global $wpdb; |
| 211 |
$wpdb->delete( self::getTable( self::$folder_table ), array( 'id' => $id ), array( '%d' ) ); |
| 212 |
$wpdb->delete( self::getTable( self::$relation_table ), array( 'folder_id' => $id ), array( '%d' ) ); |
| 213 |
|
| 214 |
//delete it's children |
| 215 |
$children = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fbv WHERE parent = %d", $id ) ); |
| 216 |
foreach ( $children as $k => $child ) { |
| 217 |
self::deleteFolderAndItsChildren( $child ); |
| 218 |
} |
| 219 |
} |
| 220 |
public static function newFolder( $name, $parent = 0 ) { |
| 221 |
global $wpdb; |
| 222 |
$data = apply_filters( |
| 223 |
'fbv_data_before_inserting_folder', |
| 224 |
array( |
| 225 |
'name' => $name, |
| 226 |
'parent' => $parent, |
| 227 |
'type' => 0, |
| 228 |
) |
| 229 |
); |
| 230 |
$wpdb->insert( self::getTable( self::$folder_table ), $data ); |
| 231 |
return $wpdb->insert_id; |
| 232 |
} |
| 233 |
public static function newOrGet( $name, $parent, $return_id_if_exist = true ) { |
| 234 |
$check = self::detail( $name, $parent ); |
| 235 |
if ( is_null( $check ) ) { |
| 236 |
return self::newFolder( $name, $parent ); |
| 237 |
} else { |
| 238 |
return $return_id_if_exist ? (int) $check->id : false; |
| 239 |
} |
| 240 |
} |
| 241 |
public static function deleteFoldersOfPost( $post_id ) { |
| 242 |
global $wpdb; |
| 243 |
$wpdb->delete( |
| 244 |
self::getTable( self::$relation_table ), |
| 245 |
array( 'attachment_id' => $post_id ), |
| 246 |
array( '%d' ) |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
public static function getChildrenOfFolder( $folder_id, $index = 0 ) { |
| 251 |
global $wpdb; |
| 252 |
$detail = null; |
| 253 |
if ( $index == 0 ) { |
| 254 |
$detail = $wpdb->get_results( 'SELECT name, id FROM ' . $wpdb->prefix . 'fbv WHERE id = ' . (int) $folder_id ); |
| 255 |
} |
| 256 |
$children = $wpdb->get_results( 'SELECT name, id FROM ' . $wpdb->prefix . 'fbv WHERE parent = ' . (int) $folder_id ); |
| 257 |
foreach ( $children as $k => $v ) { |
| 258 |
$children[ $k ]->children = self::getChildrenOfFolder( $v->id, $index + 1 ); |
| 259 |
} |
| 260 |
if ( $detail != null ) { |
| 261 |
$return = new \stdClass(); |
| 262 |
$return->id = $detail[0]->id; |
| 263 |
$return->name = $detail[0]->name; |
| 264 |
$return->children = $children; |
| 265 |
|
| 266 |
return $return; |
| 267 |
} |
| 268 |
return $children; |
| 269 |
} |
| 270 |
|
| 271 |
private static function getTable( $table ) { |
| 272 |
global $wpdb; |
| 273 |
return $wpdb->prefix . $table; |
| 274 |
} |
| 275 |
|
| 276 |
public static function getRelationsWithFolderUser( $clauses ) { |
| 277 |
global $wpdb; |
| 278 |
|
| 279 |
$attachment_in_folder = $wpdb->prepare( |
| 280 |
"SELECT attachment_id |
| 281 |
FROM {$wpdb->prefix}fbv_attachment_folder AS fbva |
| 282 |
JOIN {$wpdb->prefix}fbv AS fbv ON fbva.folder_id = fbv.id |
| 283 |
GROUP BY attachment_id |
| 284 |
HAVING FIND_IN_SET(%d, GROUP_CONCAT(created_by))", |
| 285 |
apply_filters( 'fbv_in_not_in_created_by', 0 ) |
| 286 |
); |
| 287 |
|
| 288 |
$clauses['where'] .= " AND {$wpdb->posts}.ID NOT IN ($attachment_in_folder) "; |
| 289 |
|
| 290 |
return $clauses; |
| 291 |
} |
| 292 |
} |
| 293 |
|