bp_document_get_child_folders( $folder_id,  $parent_folder = '' )

Description

Source

File: bp-templates/bp-nouveau/includes/document/functions.php

function bp_document_get_child_folders( $folder_id = 0, $parent_folder = '' ) {

	global $wpdb, $bp;

	$document_folder_table = $bp->document->table_name_folder;

	if ( 0 === $folder_id ) {
		return;
	}

	//$documents_folder_query = $wpdb->prepare( "SELECT * FROM {$document_folder_table} WHERE FIND_IN_SET(id,(SELECT GROUP_CONCAT(lv SEPARATOR ',') FROM ( SELECT @pv:=(SELECT GROUP_CONCAT(id SEPARATOR ',') FROM {$document_folder_table} WHERE parent IN (@pv)) AS lv FROM {$document_folder_table} JOIN (SELECT @pv:=%d)tmp WHERE parent IN (@pv)) a))", $folder_id );
	//$documents_folder_query = $wpdb->prepare( "SELECT * FROM (select * from  {$document_folder_table}  order by parent, id) {$document_folder_table},(select @pv := %d ) initialisation WHERE find_in_set(parent, @pv) > 0 and @pv := concat(@pv, ',', id)", $folder_id );
	$documents_folder_query = $wpdb->prepare( "SELECT DATA.* FROM( SELECT @ids as _ids, (   SELECT @ids := GROUP_CONCAT(id) FROM {$document_folder_table} WHERE FIND_IN_SET(parent, @ids) ) as cids, @l := @l+1 as level FROM {$document_folder_table}, (SELECT @ids :=%d, @l := 0 ) b WHERE @ids IS NOT NULL ) id, {$document_folder_table} DATA WHERE FIND_IN_SET(DATA.id, ID._ids) AND parent > 0 ORDER BY level, id", $folder_id );
	$data                   = $wpdb->get_results( $documents_folder_query, ARRAY_A ); // db call ok; no-cache ok;

	// Build array of item references.
	foreach ( $data as $key => &$item ) {
		$itemsByReference[ $item['id'] ] = &$item;
		// Children array:
		$itemsByReference[ $item['id'] ]['children'] = array();
		// Empty data class (so that json_encode adds "data: {}" )
		$itemsByReference[ $item['id'] ]['data'] = new StdClass();
	}

	// Set items as children of the relevant parent item.
	foreach ( $data as $key => &$item ) {
		if ( $item['parent'] && isset( $itemsByReference[ $item['parent'] ] ) ) {
			$itemsByReference [ $item['parent'] ]['children'][] = &$item;
		}
	}

	// Remove items that were added to parents elsewhere.
	foreach ( $data as $key => &$item ) {
		if ( $item['parent'] && isset( $itemsByReference[ $item['parent'] ] ) ) {
			unset( $data[ $key ] );
		}
	}

	bp_document_create_folder_recursive( $data, $parent_folder );

}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.