1 |
234 |
ahitrov |
package blogs::Keeper; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings 'all'; |
5 |
|
|
use base qw(Contenido::Keeper); |
6 |
|
|
|
7 |
|
|
|
8 |
|
|
use Contenido::Globals; |
9 |
|
|
|
10 |
|
|
|
11 |
|
|
sub get_blogs { |
12 |
|
|
my $self = shift; |
13 |
|
|
my (%opts) = @_; |
14 |
|
|
my $uid = delete $opts{uid} || 0; |
15 |
|
|
my $owner = delete $opts{owner} || 0; |
16 |
|
|
my $type = delete $opts{type}; |
17 |
|
|
my $id = delete $opts{id}; |
18 |
|
|
|
19 |
|
|
my $blog; |
20 |
|
|
my %getopts; |
21 |
|
|
if ( $uid ) { |
22 |
|
|
$getopts{status} = 'positive'; |
23 |
|
|
} else { |
24 |
|
|
$getopts{status} = [1,2,3]; |
25 |
|
|
} |
26 |
|
|
$getopts{uid} = $owner if $owner; |
27 |
|
|
$getopts{type} = defined $type ? $type : 'positive'; |
28 |
|
|
if ( $id ) { |
29 |
|
|
$blog = $keeper->get_document_by_id ( $id, class => 'blogs::Blog', %getopts ); |
30 |
|
|
if ( ref $blog ) { |
31 |
|
|
if ( $blog->type == 1 && $uid && $blog->uid != $uid ) { |
32 |
|
|
return undef unless grep { $_ == $uid } $blog->members; |
33 |
|
|
} |
34 |
|
|
} |
35 |
|
|
} else { |
36 |
|
|
if ( $uid ) { |
37 |
|
|
$getopts{lclass} = 'blogs::MemberLink', |
38 |
|
|
$getopts{lsource} = $uid; |
39 |
|
|
} |
40 |
|
|
$blog = $keeper->get_documents ( |
41 |
|
|
class => 'blogs::Blog', |
42 |
|
|
%getopts, |
43 |
|
|
%opts, |
44 |
|
|
return_mode => 'array_ref', |
45 |
|
|
); |
46 |
|
|
if ( $owner && defined $type && $type == 0 ) { |
47 |
|
|
$blog = $blog->[0]; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
return $blog; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
sub add_member { |
54 |
|
|
my $self = shift; |
55 |
|
|
my (%opts) = @_; |
56 |
|
|
my $id = delete $opts{blog} || 0; |
57 |
|
|
my $uid = delete $opts{uid} || []; |
58 |
|
|
my $moderator = delete $opts{moderator} || 0; |
59 |
|
|
return undef if !$uid || (ref $uid eq 'ARRAY' && !@$uid); |
60 |
|
|
return undef unless $id; |
61 |
|
|
|
62 |
|
|
$uid = [$uid] unless ref $uid; |
63 |
|
|
my $blog = $keeper->get_document_by_id ( $id, class => 'blogs::Blog' ); |
64 |
|
|
my @members = grep { my $mem = $_; !grep { $_ == $mem } @$uid } $blog->members; |
65 |
|
|
push @members, @$uid; |
66 |
|
|
$blog->members( @members ? @members : undef ); |
67 |
|
|
if ( $moderator ) { |
68 |
|
|
my @moderators = grep { my $mem = $_; !grep { $_ == $mem } @$uid } $blog->moderators; |
69 |
|
|
push @moderators, @$uid; |
70 |
|
|
$blog->moderators( @moderators ? @moderators : undef ); |
71 |
|
|
} |
72 |
|
|
$blog->store; |
73 |
|
|
|
74 |
|
|
my @links = $keeper->get_links ( class => 'blogs::MemberLink', dest_id => $blog->id ); |
75 |
|
|
foreach my $user_id ( @$uid ) { |
76 |
|
|
my ($link) = grep { $_->source_id == $user_id } @links; |
77 |
|
|
if ( ref $link && $moderator ) { |
78 |
|
|
$link->moderator( 1 ); |
79 |
|
|
$link->store; |
80 |
|
|
} elsif ( ref $link ) { |
81 |
|
|
$link->moderator( 0 ); |
82 |
|
|
$link->store; |
83 |
|
|
} else { |
84 |
|
|
$link = blogs::MemberLink->new( $keeper ); |
85 |
|
|
$link->source_id( $user_id ); |
86 |
|
|
$link->dest_id ( $blog->id ); |
87 |
|
|
$link->dest_class ( $blog->class ); |
88 |
|
|
$link->status( 1 ); |
89 |
|
|
$link->moderator( $moderator ); |
90 |
|
|
$link->store; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
1; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
sub remove_member { |
97 |
|
|
my $self = shift; |
98 |
|
|
my (%opts) = @_; |
99 |
|
|
my $id = delete $opts{blog} || 0; |
100 |
|
|
my $uid = delete $opts{uid} || 0; |
101 |
|
|
return undef unless $uid; |
102 |
|
|
return undef unless $id; |
103 |
|
|
|
104 |
|
|
my $blog = $keeper->get_document_by_id ( $id, class => 'blogs::Blog' ); |
105 |
|
|
my @members = grep { $_ != $uid } $blog->members; |
106 |
|
|
$blog->members( @members ? @members : undef ); |
107 |
|
|
my @moderators = grep { $_!= $uid } $blog->moderators; |
108 |
|
|
$blog->moderators( @moderators ? @moderators : undef ); |
109 |
|
|
$blog->store; |
110 |
|
|
|
111 |
|
|
my @links = $keeper->get_links ( class => 'blogs::MemberLink', dest_id => $blog->id ); |
112 |
|
|
my ($link) = grep { $_->source_id == $uid } @links; |
113 |
|
|
if ( ref $link ) { |
114 |
|
|
$link->delete; |
115 |
|
|
} |
116 |
|
|
1; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
sub delete_blog { |
121 |
|
|
my $self = shift; |
122 |
|
|
my $id = shift; |
123 |
|
|
my (%opts) = @_; |
124 |
|
|
|
125 |
|
|
if ( $opts{soft} ) { |
126 |
|
|
my $blog = $keeper->get_document_by_id ( $id, class => 'blogs::Blog' ); |
127 |
|
|
$blog->status(0); |
128 |
|
|
$blog->store; |
129 |
|
|
} else { |
130 |
|
|
my $comments_db_table = blogs::SQL::CommentsTable->db_table; |
131 |
|
|
my $sthcom = $keeper->SQL->prepare( "delete from $comments_db_table where blog_id = ?" ); |
132 |
|
|
$sthcom->execute( $id ); |
133 |
|
|
$sthcom->finish; |
134 |
|
|
my $records = $keeper->get_documents ( |
135 |
|
|
blog_id => $id, |
136 |
|
|
class => 'blogs::Record', |
137 |
|
|
no_limit => 1, |
138 |
|
|
return_mode => 'array_ref', |
139 |
|
|
); |
140 |
|
|
foreach my $rec ( @$records ) { |
141 |
|
|
$rec->delete( attachments => 1 ); |
142 |
|
|
} |
143 |
|
|
my $blog = $keeper->get_document_by_id ( $id, class => 'blogs::Blog' ); |
144 |
|
|
$blog->delete( attachments => 1 ); |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
sub delete_record { |
150 |
|
|
my $self = shift; |
151 |
|
|
my $id = shift; |
152 |
|
|
my (%opts) = @_; |
153 |
|
|
|
154 |
|
|
my $comments_db_table = blogs::SQL::CommentsTable->db_table; |
155 |
|
|
my $sthcom = $keeper->SQL->prepare( "delete from $comments_db_table where record_id = ?" ); |
156 |
|
|
$sthcom->execute( $id ); |
157 |
|
|
$sthcom->finish; |
158 |
|
|
my $record = $keeper->get_document_by_id ( $id, class => 'blogs::Record' ); |
159 |
|
|
$record->delete( attachments => 1 ); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
sub add_comment { |
163 |
|
|
my $self = shift; |
164 |
|
|
my (%opts) = @_; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
sub get_comments { |
169 |
|
|
my $self = shift; |
170 |
|
|
my (%opts) = @_; |
171 |
|
|
my $record_id = delete $opts{record_id}; |
172 |
|
|
|
173 |
|
|
return undef unless $record_id; |
174 |
|
|
my $comment_tree = {}; |
175 |
|
|
my $comments = $keeper->get_documents ( |
176 |
|
|
class => 'blogs::Comment', |
177 |
|
|
record_id => $record_id, |
178 |
|
|
return_mode => 'array_ref', |
179 |
|
|
) || []; |
180 |
|
|
foreach my $comment ( @$comments ) { |
181 |
|
|
$comment_tree->{$comment->pid} = [] unless exists $comment_tree->{$comment->pid}; |
182 |
|
|
push @{ $comment_tree->{$comment->pid} }, $comment; |
183 |
|
|
} |
184 |
|
|
return ( $comment_tree, $comments ); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
1; |