Thread Starter
hnb
(@hnb)
Hi, it’s me again i found a solution for my problem and i wanted to share it :
I decided to assign an album to a user using a new table :
CREATE TABLE wp_user_album(
id INT PRIMARY KEY,
user_id INT NOT NULL,
album_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES WP_USERS(ID)ON DELETE CASCADE,
FOREIGN KEY (album_id) REFERENCES wp_ngg_album(id)ON DELETE CASCADE
);
The assignation will be done manually on phpmyadmin for instance.
Then i created a function which i hooked on the action ‘ngg_created_new_gallery’ which is right after the creation of a new gallery. You can drop this at the bottom of the admin/functions.php :
/**
* Add a gallery automatically in the user's album (set manually in the table wp_user_album)
* @param: $galleryid Id of the gallery created
* Hooked to the action 'ngg_created_new_gallery', return nothing
*/
add_action('ngg_created_new_gallery', 'add_gallery_to_user_album', $galleryid);
function add_gallery_to_user_album($galleryid){
global $wpdb;
//Get the gallery
$gallery = nggdb::find_gallery($galleryid);
//Get the user id
$user_id = $gallery->author;
//Get the album of the user
$user_album = $wpdb->get_row($wpdb->prepare("SELECT * from {$wpdb->prefix}user_album where user_id = %d", $user_id), ARRAY_A);
$album = nggdb::find_album($user_album['album_id']);
//If the user has an album
if($album != null){
//Create an array with the new gallery id
$new = array($gallery->gid);
//Merge it with the array of existing galeries id
if(count($album->gallery_ids) > 0)
$sort = array_merge((array)$album->gallery_ids, $new);
else
$sort = $new;
//serialize it
$serialized = serialize($sort);
//Update the album
$wpdb->query("UPDATE $wpdb->nggalbum SET sortorder = '$serialized' WHERE id = $album->id ");
}
//BEGIN DEBUG
$debug = '[author : '.$gallery->author.'] [album id : '.$album->id.'] [serialized : '.$serialized.']';
nggGallery::show_message($debug);
//END DEBUG
}
Therefore, every time a user with an assigned album will create a gallery it will be automatically added to its album and displayed on the page with the album shortcode (eg : [album id=1 template=compact]).
Hope it will help,
HnB