| 1 | use utf8; | ||
| 2 | package Rapi::Blog::DB::Result::Comment; | ||
| 3 | |||
| 4 | # Created by DBIx::Class::Schema::Loader | ||
| 5 | # DO NOT MODIFY THE FIRST PART OF THIS FILE | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Moose; | ||
| 11 | use MooseX::NonMoose; | ||
| 12 | use MooseX::MarkAsMethods autoclean => 1; | ||
| 13 | extends 'DBIx::Class::Core'; | ||
| 14 | __PACKAGE__->load_components("InflateColumn::DateTime"); | ||
| 15 | __PACKAGE__->table("comment"); | ||
| 16 | __PACKAGE__->add_columns( | ||
| 17 | "id", | ||
| 18 | { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, | ||
| 19 | "parent_id", | ||
| 20 | { | ||
| 21 | data_type => "integer", | ||
| 22 | default_value => \"null", | ||
| 23 | is_foreign_key => 1, | ||
| 24 | is_nullable => 1, | ||
| 25 | }, | ||
| 26 | "post_id", | ||
| 27 | { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, | ||
| 28 | "user_id", | ||
| 29 | { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, | ||
| 30 | "ts", | ||
| 31 | { data_type => "datetime", is_nullable => 0 }, | ||
| 32 | "body", | ||
| 33 | { data_type => "text", default_value => "", is_nullable => 1 }, | ||
| 34 | ); | ||
| 35 | __PACKAGE__->set_primary_key("id"); | ||
| 36 | __PACKAGE__->has_many( | ||
| 37 | "comments", | ||
| 38 | "Rapi::Blog::DB::Result::Comment", | ||
| 39 | { "foreign.parent_id" => "self.id" }, | ||
| 40 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 41 | ); | ||
| 42 | __PACKAGE__->belongs_to( | ||
| 43 | "parent", | ||
| 44 | "Rapi::Blog::DB::Result::Comment", | ||
| 45 | { id => "parent_id" }, | ||
| 46 | { | ||
| 47 | is_deferrable => 0, | ||
| 48 | join_type => "LEFT", | ||
| 49 | on_delete => "CASCADE", | ||
| 50 | on_update => "CASCADE", | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | __PACKAGE__->belongs_to( | ||
| 54 | "post", | ||
| 55 | "Rapi::Blog::DB::Result::Post", | ||
| 56 | { id => "post_id" }, | ||
| 57 | { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" }, | ||
| 58 | ); | ||
| 59 | __PACKAGE__->belongs_to( | ||
| 60 | "user", | ||
| 61 | "Rapi::Blog::DB::Result::User", | ||
| 62 | { id => "user_id" }, | ||
| 63 | { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" }, | ||
| 64 | ); | ||
| 65 | |||
| 66 | |||
| 67 | # Created by DBIx::Class::Schema::Loader v0.07045 @ 2017-05-24 12:10:40 | ||
| 68 | # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vTk0Nu215wTcvpKtY8xUcg | ||
| 69 | |||
| 70 | use RapidApp::Util ':all'; | ||
| 71 | use Rapi::Blog::Util; | ||
| 72 | |||
| 73 | __PACKAGE__->load_components('+Rapi::Blog::DB::Component::SafeResult'); | ||
| 74 | |||
| 75 | sub insert { | ||
| 76 | my $self = shift; | ||
| 77 | my $columns = shift; | ||
| 78 | |||
| 79 | if(my $User = Rapi::Blog::Util->get_User) { | ||
| 80 | die usererr "Insert Comment: PERMISSION DENIED" if ($User->id && !$User->can_comment); | ||
| 81 | } | ||
| 82 | |||
| 83 | $self->set_inflated_columns($columns) if $columns; | ||
| 84 | |||
| 85 | $self->_set_column_defaults('insert'); | ||
| 86 | |||
| 87 | $self->next::method; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub update { | ||
| 91 | my $self = shift; | ||
| 92 | my $columns = shift; | ||
| 93 | $self->set_inflated_columns($columns) if $columns; | ||
| 94 | |||
| 95 | $self->_set_column_defaults('update'); | ||
| 96 | |||
| 97 | $self->next::method; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub _set_column_defaults { | ||
| 103 | my $self = shift; | ||
| 104 | my $for = shift || ''; | ||
| 105 | |||
| 106 | my $uid = Rapi::Blog::Util->get_uid; | ||
| 107 | my $now_ts = Rapi::Blog::Util->now_ts; | ||
| 108 | |||
| 109 | $self->ts($now_ts) unless $self->ts; | ||
| 110 | $self->user_id( $uid ) unless $self->user_id; | ||
| 111 | |||
| 112 | if(! $self->post_id && $self->parent_id && $self->parent) { | ||
| 113 | $self->post_id( $self->parent->post_id ); | ||
| 114 | } | ||
| 115 | |||
| 116 | if($self->parent_id && $self->post_id && $self->parent) { | ||
| 117 | my ($p_post_id,$post_id) = ($self->parent->post_id,$self->post_id); | ||
| 118 | die usererr join('', | ||
| 119 | "parent/post mismatch: the post_id of the parent comment ($p_post_id) ", | ||
| 120 | "doesn't match the supplied post_id ($post_id)" | ||
| 121 | ) unless ( $p_post_id == $post_id ); | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub html_id { | ||
| 127 | my $self = shift; | ||
| 128 | join('','comment-',$self->id) | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub body_html { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $body = $self->get_column('body'); | ||
| 136 | |||
| 137 | # We are doing it this way for speed; if there are no characters which look | ||
| 138 | # like tags, convert newlimes to <br> otherwise assume the content is already | ||
| 139 | # HTML and return as-is | ||
| 140 | return $body if ($body =~ /\<.+\>/); | ||
| 141 | |||
| 142 | $body =~ s/\r?\n/\<br\>\r\n/g; | ||
| 143 | |||
| 144 | return $body | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | # You can replace this text with custom code or comments, and it will be preserved on regeneration | ||
| 150 | __PACKAGE__->meta->make_immutable; | ||
| 151 | 1; |