id)->delete(); } /** * Called just before an item deletion. */ public static function item_before_delete($item) { if( $item->is_album() ) { ORM::factory('user_chroot')->where('album_id', '=', $item->id)->delete(); } } /** * Called when building the 'Add user' form for an admin. */ static function user_add_form_admin($user, $form) { $form->add_user->dropdown('user_chroot') ->label(t("Root Album")) ->options(self::albumsTreeArray()) ->selected(1); } /** * Called just after a user has been added by an admin. */ public static function user_add_form_admin_completed($user, $form) { if( $form->add_user->user_chroot->value > 1 ) { $user_chroot = ORM::factory('user_chroot'); $user_chroot->id = $user->id; $user_chroot->album_id = $form->add_user->user_chroot->value; $user_chroot->save(); } } /** * Called when building the 'Edit user' form for an admin. */ public static function user_edit_form_admin($user, $form) { $user_chroot = ORM::factory('user_chroot', $user->id); $selected = ( $user_chroot->loaded() ) ? $user_chroot->album_id : 1; $form->edit_user->dropdown('user_chroot') ->label(t("Root Album")) ->options(self::albumsTreeArray()) ->selected($selected); } /** * Called just after a user has been edited by an admin. */ public static function user_edit_form_admin_completed($user, $form) { if( $form->edit_user->user_chroot->value <= 1 ) { ORM::factory('user_chroot')->delete($user->id); } else { $user_chroot = ORM::factory('user_chroot', $user->id); if( !$user_chroot->loaded() ) { $user_chroot = ORM::factory('user_chroot'); $user_chroot->id = $user->id; } $user_chroot->album_id = $form->edit_user->user_chroot->value; $user_chroot->save(); } } /** * Generate an array representing the hierarchy of albums. */ private static function albumsTreeArray($level_marker = '    ') { $tree = array(); $albums = ORM::factory('item') ->where('type', '=', 'album') ->order_by('left_ptr', 'ASC') ->find_all(); foreach($albums as $album) { $tree[$album->id] = html::clean( str_repeat($level_marker, $album->level - 1).' '.$album->title ); } return $tree; } }