root/trunk/lib/MT/Association.pm

Revision 4196, 7.3 kB (checked in by takayama, 3 months ago)

* Set svn keywords

  • Property svn:keywords set to Author Date Id Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Association;
8
9use strict;
10use base qw( MT::Object );
11
12__PACKAGE__->install_properties({
13    column_defs => {
14        'id'           => 'integer not null auto_increment',
15        'type'         => 'integer not null',
16        'author_id'    => 'integer',
17        'blog_id'      => 'integer',
18        'group_id'     => 'integer',
19        'role_id'      => 'integer',
20    },
21    indexes => {
22        blog_id        => 1,
23        author_id      => 1,
24        role_id        => 1,
25        group_id       => 1,
26        type           => 1,
27        created_on     => 1,
28    },
29    defaults => {
30        author_id      => 0,
31        group_id       => 0,
32        blog_id        => 0,
33        role_id        => 0,
34    },
35    audit => 1,
36    datasource  => 'association',
37    primary_key => 'id',
38});
39
40sub USER_BLOG_ROLE ()  { 1 }
41sub GROUP_BLOG_ROLE () { 2 }
42sub USER_GROUP ()      { 3 }
43sub USER_ROLE ()       { 4 }
44sub GROUP_ROLE ()      { 5 }
45
46sub class_label {
47    MT->translate("Association");
48}
49
50sub class_label_plural {
51    MT->translate("Associations");
52}
53
54sub save {
55    my $assoc = shift;
56    my $res = $assoc->SUPER::save(@_) or return;
57    $assoc->rebuild_permissions;
58    $res;
59}
60
61sub remove {
62    my $assoc = shift;
63    my $res = $assoc->SUPER::remove(@_) or return;
64    if (ref $assoc) {
65        $assoc->rebuild_permissions;
66    }
67    $res;
68}
69
70sub rebuild_permissions {
71    my $assoc = shift;
72    require MT::Permission;
73    MT::Permission->rebuild($assoc);
74}
75
76sub user {
77    my $assoc = shift;
78    $assoc->cache_property('user', sub {
79        require MT::Author;
80        $assoc->author_id ? MT::Author->load($assoc->author_id) : undef;
81    });
82}
83
84sub blog {
85    my $assoc = shift;
86    $assoc->cache_property('blog', sub {
87        require MT::Blog;
88        $assoc->blog_id ? MT::Blog->load($assoc->blog_id) : undef;
89    });
90}
91
92sub group {
93    my $assoc = shift;
94    $assoc->cache_property('group', sub {
95        require MT::Group;
96        $assoc->group_id ? MT::Group->load($assoc->group_id) : undef;
97    });
98}
99
100sub role {
101    my $assoc = shift;
102    $assoc->cache_property('role', sub {
103        require MT::Role;
104        $assoc->role_id ? MT::Role->load($assoc->role_id) : undef;
105    });
106}
107
108# Creates an association between 2 or 3 objects
109sub link {
110    my $pkg = shift;
111    my $terms = $pkg->objects_to_terms(@_);
112    return unless $terms;
113    my $assoc = $pkg->get_by_key($terms);
114    if (!$assoc->id) {
115        if (MT->instance->isa('MT::App')) {
116            $assoc->created_by(MT->instance->user->id) if (defined(MT->instance->user));
117        }
118        $assoc->save or return;
119    }
120    $assoc;
121}
122
123# Removes an association between 2 or 3 objects
124sub unlink {
125    my $pkg = shift;
126    my $terms = $pkg->objects_to_terms(@_);
127    return unless $terms;
128    my $assoc = $pkg->get_by_key($terms);
129    $assoc->id ? $assoc->remove : 1;
130}
131
132sub objects_to_terms {
133    my $pkg = shift;
134    my %param = map { ref $_ => $_ } @_;
135    my $terms = {};
136    $terms->{author_id} = $param{'MT::Author'}->id if $param{'MT::Author'};
137    $terms->{group_id} = $param{'MT::Group'}->id if $param{'MT::Group'};
138    $terms->{role_id} = $param{'MT::Role'}->id if $param{'MT::Role'};
139    $terms->{blog_id} = $param{'MT::Blog'}->id if $param{'MT::Blog'};
140    $terms->{blog_id} = $param{'MT::Website'}->id if $param{'MT::Website'};
141    if ($terms->{author_id} && $terms->{blog_id} && $terms->{role_id}) {
142        $terms->{type} = USER_BLOG_ROLE;
143    } elsif ($terms->{group_id} && $terms->{blog_id} && $terms->{role_id}) {
144        $terms->{type} = GROUP_BLOG_ROLE;
145    } elsif ($terms->{group_id} && $terms->{author_id}) {
146        $terms->{type} = USER_GROUP;
147    # To be defined...
148    #} elsif ($terms->{user_id} && $terms->{role_id}) {
149    #    $terms->{type} = USER_ROLE;
150    #} elsif ($terms->{group_id} && $terms->{role_id}) {
151    #    $terms->{type} = GROUP_ROLE;
152    } else {
153        return undef;
154    }
155    $terms;
156}
157
1581;
159
160#trans('association')
161#trans('associations')
162
163__END__
164
165=head1 NAME
166
167MT::Association - Relational table for Author/Group-Role-Blog relationships.
168
169=head1 SYNOPSIS
170
171    use MT::Association;
172
173    # Define a Group - Role - Blog relationship
174    MT::Association->link( $group => $role => $blog );
175
176    # Define a User - Role - Blog relationship
177    MT::Association->link( $user => $role => $blog );
178
179    # Define a User - Group relationship
180    MT::Association->link( $user => $group );
181
182=head1 DESCRIPTION
183
184This module handles relational mappings between L<MT::Author>, L<MT::Group>,
185L<MT::Role> and L<MT::Blog> objects.
186
187=head1 METHODS
188
189=head2 $assoc->save()
190
191Saves the association and calls the L<rebuild_permissions> method to
192ensure the related permissions are updated.
193
194=head2 $assoc->remove()
195
196Removes the association and calls the L<rebuild_permissions> method to
197ensure the related permissions are updated.
198
199=head2 $assoc->rebuild_permissions()
200
201An alias for calling C<MT::Permission->rebuild($assoc)>.
202
203=head2 $assoc->user()
204
205Returns the L<MT::Author> object tied to this association. Returns undef if
206the author_id property is undefined.
207
208=head2 $assoc->blog()
209
210Returns the L<MT::Blog> object tied to this association. Returns undef if
211the blog_id property is undefined.
212
213=head2 $assoc->group()
214
215Returns the L<MT::Group> object tied to this association. Returns undef if
216the group_id property is undefined.
217
218=head2 $assoc->role()
219
220Returns the L<MT::Role> object tied to this association. Returns undef if
221the role_id property is undefined.
222
223=head2 MT::Association->link(@things)
224
225Creates a new association record that ties the elements of C<@things>
226together. The list of C<@things> may contain:
227
228=over 4
229
230=item 1. user, role, blog
231
232=item 2. group, role, blog
233
234=item 3. user, group
235
236=back
237
238Any other combination will fail horribly.
239
240=head2 MT::Association->unlink(@things)
241
242Removes any association record that exists that ties the elements of
243C<@things> together. See the L<link> method for valid values to pass
244for the C<@things> parameter.
245
246=head2 MT::Association->objects_to_terms(@things)
247
248Utility method that takes an array containing user, group, role, blog
249objects and returns a hashref suitable to use for terms for the
250C<MT::Association-E<gt>load> method.
251
252=head1 DATA ACCESS METHODS
253
254The I<MT::Association> object holds the following pieces of data. These
255fields can be accessed and set using the standard data access methods
256described in the I<MT::Object> documentation.
257
258=over 4
259
260=item * id
261
262Primary key for the association object.
263
264=item * type
265
266Identifies the type of relationship. Valid types are defined by the
267following constants:
268
269=over 4
270
271=item * MT::Association::USER_BLOG_ROLE
272
273=item * MT::Association::GROUP_BLOG_ROLE
274
275=item * MT::Association::USER_GROUP
276
277=back
278
279=item * author_id
280
281L<MT::Author> id for associations related to a user. For other
282association types, this value is undefined.
283
284=item * blog_id
285
286L<MT::Blog> id for associations related to a blog. For other
287association types, this value is undefined.
288
289=item * group_id
290
291L<MT::Group> id for associations related to a group. For other
292association types, this value is undefined.
293
294=item * role_id
295
296L<MT::Role> id for associations related to a role. For other
297association types, this value is undefined.
298
299=back
300
301=head1 AUTHOR & COPYRIGHT
302
303Please see the I<MT> manpage for author, copyright, and license information.
304
305=cut
Note: See TracBrowser for help on using the browser.