File Coverage

blib/lib/Biblio/Zotero/DB/Schema/ResultSet/Item.pm
Criterion Covered Total %
statement 45 45 100.0
branch 1 2 50.0
condition n/a
subroutine 12 12 100.0
pod 0 6 0.0
total 58 65 89.2


line stmt bran cond sub pod time code
1             package Biblio::Zotero::DB::Schema::ResultSet::Item;
2             $Biblio::Zotero::DB::Schema::ResultSet::Item::VERSION = '0.003';
3 13     13   95300 use strict;
  13         36  
  13         464  
4 13     13   71 use warnings;
  13         30  
  13         1618  
5 13     13   70 use Moo;
  13         31  
  13         148  
6              
7             extends 'DBIx::Class::ResultSet';
8              
9             has _attachment_itemtypeid => ( is => 'ro', default => sub {
10             my $self = shift;
11             my $attachment_itemtypeid = $self->result_source->schema
12             ->resultset('ItemType')
13             ->search({ 'typename' => 'attachment' })
14             ->single->itemtypeid;
15             });
16              
17             has _note_itemtypeid => ( is => 'ro', default => sub {
18             my $self = shift;
19             my $attachment_itemtypeid = $self->result_source->schema
20             ->resultset('ItemType')
21             ->search({ 'typename' => 'note' })
22             ->single->itemtypeid;
23             });
24              
25             has _item_attachment_resultset => ( is => 'rw', default => sub { 'ItemAttachment' } );
26              
27             # pass the args as is to the parent --- new() takes no params for the current object
28 184     184 0 1916878 sub BUILDARGS { shift; { }; }
  184         4343  
29 184     184 0 1562 sub FOREIGNBUILDARGS { shift; @_; }
  184         1286  
30              
31             sub with_item_attachment_resultset {
32 4     4 0 19939 my ($self, $resultset) = @_;
33 4         32 my $self_clone = $self->search({});
34 4         9221 $self_clone->_item_attachment_resultset($resultset);
35 4         38 $self_clone;
36             }
37              
38             sub search_by_field {
39 21     21 0 67416 my ($self, $field_queries) = @_;
40              
41 21         126 my $schema = $self->result_source->schema;
42 21         146 my $subqueries;
43              
44 21         165 while (my ($key, $value) = each %$field_queries) {
45 22         9529 push @$subqueries,
46             $schema->resultset('ItemData')->search(
47             {
48             'fieldid.fieldname' => $key,
49             'valueid.value' => $value,
50             },
51             {
52             prefetch => [ 'fieldid', 'valueid' ],
53             }
54             )->get_column('itemid')->as_query;
55             }
56              
57 22         362 return $self->search_rs(
58             {
59             -and => [ map {
60 21         192783 { 'me.itemid' => { '-in' => $_ } }
61             } @$subqueries
62             ],
63             },
64             {
65             prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] },
66             },
67             );
68             }
69              
70             sub items_with_attachments_of_mimetypes {
71 3     3 0 2670 my ($self, @mimetypes) = @_;
72 3 50       17 return unless @mimetypes;
73 3         38 my $subquery = $self->_attachment_subquery({ mimetype => [ -or => @mimetypes ] });
74              
75 3         10319 return $self->search_rs(
76             { 'me.itemid' => { '-in' => $subquery } },
77             {
78             prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] },
79             },
80             );
81             }
82              
83             sub items_with_pdf_attachments {
84 2     2 0 5149 my ($self) = @_;
85 2         13 $self->items_with_attachments_of_mimetypes('application/pdf');
86             }
87              
88             sub _attachment_subquery {
89 5     5   23 my ($self, $search_query) = @_;
90 5         92 my $schema = $self->result_source->schema;
91             my $subquery = $schema->resultset($self->_item_attachment_resultset)
92             ->search(
93             $search_query,
94             { '+columns' =>
95             { outputitemid =>
96 5         75 \do { "IFNULL(me.sourceitemid, me.itemid)" } },
  5         2051  
97             }
98             )->get_column('outputitemid')->as_query;
99             }
100              
101             sub _toplevel_items {
102 2     2   6 my $self = shift;
103 2         12 my $subquery = $self->_attachment_subquery({});
104 2         3934 return $self->search_rs(
105             { -or => [
106             { 'me.itemid' => { '-in' => $subquery } },
107             { 'itemtypeid' => { 'not in', [$self->_attachment_itemtypeid, $self->_note_itemtypeid] } },
108             ]
109             },
110             {
111             prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] },
112             },
113             );
114             }
115              
116             sub _trash_items {
117 1     1   2334 my $self = shift;
118 1         8 my $schema = $self->result_source->schema;
119             # an item is in the trash if either the item itself is in deletedItems.itemid
120 1         11 my $deleted = $schema->resultset('DeletedItem')
121             ->get_column('itemid')->as_query;
122              
123             # or if it has an attachment that is in deletedItems
124 1         1974 my $deletedAttachment = $schema->resultset('ItemAttachment')
125             ->search( { 'itemid' => { -in => $deleted } })
126             ->get_column('sourceitemid')->as_query;
127              
128             # or if it has an attachment that is in deletedItems
129 1         2577 my $deletedNote = $schema->resultset('ItemNote')
130             ->search({ 'itemid' => { -in => $deleted } })
131             ->get_column('sourceitemid')->as_query;
132              
133 1         2147 my $attached = $schema->resultset('ItemAttachment')
134             ->search( { 'sourceitemid' => { '!=' => undef } })
135             ->get_column('itemid')->as_query;
136              
137 1         2196 my $noted = $schema->resultset('ItemNote')
138             ->search( { 'sourceitemid' => { '!=' => undef } })
139             ->get_column('itemid')->as_query;
140              
141 1         2106 $schema->resultset('Item')->search(
142             {
143             -or => [
144             { 'me.itemid' => { -in => $deleted } },
145             { 'me.itemid' => { -in => $deletedAttachment } },
146             { 'me.itemid' => { -in => $deletedNote } },
147             ]
148             },
149             )->search(
150             {
151             -and => [
152             { 'me.itemid' => { 'not in' => $attached } },
153             { 'me.itemid' => { 'not in' => $noted } }
154             ]
155             },
156             {
157             prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] },
158             },
159             );
160             }
161              
162              
163             1;
164              
165             __END__