| 1 |
<?php |
| 2 |
namespace FileBird\Classes; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
use FileBird\Model\Folder as FolderModel; |
| 7 |
|
| 8 |
class Tree { |
| 9 |
public static function getFolders( $order_by = null, $flat = false, $level = 0, $show_level = false ) { |
| 10 |
$folders_from_db = FolderModel::allFolders( '*', null, $order_by ); |
| 11 |
$default_folders = array(); |
| 12 |
|
| 13 |
$tree = self::getTree( $folders_from_db, 0, $default_folders, $flat, $level, $show_level ); |
| 14 |
return $tree; |
| 15 |
} |
| 16 |
public static function getCount( $folder_id, $lang = null ) { |
| 17 |
global $wpdb; |
| 18 |
|
| 19 |
$select = "SELECT COUNT(*) FROM {$wpdb->posts} as posts WHERE "; |
| 20 |
$where = array( "post_type = 'attachment'" ); |
| 21 |
|
| 22 |
// With $folder_id == -1. We get all |
| 23 |
$where[] = "(posts.post_status = 'inherit' OR posts.post_status = 'private')"; |
| 24 |
|
| 25 |
// with specific folder |
| 26 |
if ( $folder_id > 0 && ! apply_filters( 'fbv_speedup_get_count_query', false ) ) { |
| 27 |
$post__in = $wpdb->get_col( "SELECT `attachment_id` FROM {$wpdb->prefix}fbv_attachment_folder WHERE `folder_id` = " . (int) $folder_id ); |
| 28 |
if ( count( $post__in ) == 0 ) { |
| 29 |
$post__in = array( 0 ); |
| 30 |
} |
| 31 |
$where[] = '(ID IN (' . implode( ', ', $post__in ) . '))'; |
| 32 |
} elseif ( $folder_id == 0 ) { |
| 33 |
return 0;//return 0 if this is uncategorized folder |
| 34 |
} |
| 35 |
$where = apply_filters( 'fbv_get_count_where_query', $where ); |
| 36 |
$query = apply_filters( 'fbv_get_count_query', $select . implode( ' AND ', $where ), $folder_id, $lang ); |
| 37 |
return (int) $wpdb->get_var( $query ); |
| 38 |
} |
| 39 |
public static function getAllFoldersAndCount( $lang = null ) { |
| 40 |
global $wpdb; |
| 41 |
$query = $wpdb->prepare( |
| 42 |
"SELECT fbva.folder_id, count(fbva.attachment_id) as count FROM {$wpdb->prefix}fbv_attachment_folder as fbva |
| 43 |
INNER JOIN {$wpdb->prefix}fbv as fbv ON fbv.id = fbva.folder_id |
| 44 |
INNER JOIN {$wpdb->posts} as posts ON fbva.attachment_id = posts.ID |
| 45 |
WHERE (posts.post_status = 'inherit' OR posts.post_status = 'private') |
| 46 |
AND (posts.post_type = 'attachment') |
| 47 |
AND fbv.created_by = %d |
| 48 |
GROUP BY fbva.folder_id", |
| 49 |
apply_filters( 'fbv_in_not_in_created_by', '0' ) |
| 50 |
); |
| 51 |
$query = apply_filters( 'fbv_all_folders_and_count', $query, $lang ); |
| 52 |
|
| 53 |
$results = $wpdb->get_results( $query ); |
| 54 |
$return = array(); |
| 55 |
if ( is_array( $results ) ) { |
| 56 |
foreach ( $results as $k => $v ) { |
| 57 |
$return[ $v->folder_id ] = $v->count; |
| 58 |
} |
| 59 |
} |
| 60 |
return $return; |
| 61 |
} |
| 62 |
private static function getTree( $data = array(), $parent = 0, $default = null, $flat = false, $level = 0, $show_level = false ) { |
| 63 |
$tree = is_null( $default ) ? array() : $default; |
| 64 |
foreach ( $data as $k => $v ) { |
| 65 |
if ( $v->parent == $parent ) { |
| 66 |
$children = self::getTree( $data, $v->id, null, $flat, $level + 1, $show_level ); |
| 67 |
$f = array( |
| 68 |
'id' => (int) $v->id, |
| 69 |
'text' => $show_level ? str_repeat( '-', $level ) . $v->name : $v->name, |
| 70 |
'li_attr' => array( |
| 71 |
'data-count' => 0, |
| 72 |
'data-parent' => (int) $parent, |
| 73 |
), |
| 74 |
'count' => 0, |
| 75 |
); |
| 76 |
|
| 77 |
if ( $flat === true ) { |
| 78 |
$tree[] = $f; |
| 79 |
foreach ( $children as $k2 => $v2 ) { |
| 80 |
$tree[] = $v2; |
| 81 |
} |
| 82 |
} else { |
| 83 |
$f['children'] = $children; |
| 84 |
$tree[] = $f; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return $tree; |
| 89 |
} |
| 90 |
|
| 91 |
public static function getFolder( $folder_id ) { |
| 92 |
$tree = self::getFolders(); |
| 93 |
return Helpers::findFolder( $folder_id, $tree ); |
| 94 |
} |
| 95 |
} |
| 96 |
|