| 1 | package Biblio::Zotero::DB::Schema::ResultSet::Item; | ||||||||||
| 2 | 13 | $Biblio::Zotero::DB::Schema::ResultSet::Item::VERSION = '0.004'; | |||||||||
| 3 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 13 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 13 | extends 'DBIx::Class::ResultSet'; | |||||||||
| 8 | |||||||||||
| 9 | 184 | ▶ | has _attachment_itemtypeid => ( is => 'ro', default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 184 | my $self = shift; | |||||||||
| 11 | 184 | my $attachment_itemtypeid = $self->result_source->schema | |||||||||
| 12 | ->resultset('ItemType') | ||||||||||
| 13 | ->search({ 'typename' => 'attachment' }) | ||||||||||
| 14 | ->single->itemtypeid; | ||||||||||
| 15 | 13 | }); | |||||||||
| 16 | |||||||||||
| 17 | 184 | ▶ | has _note_itemtypeid => ( is => 'ro', default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 184 | my $self = shift; | |||||||||
| 19 | 184 | my $attachment_itemtypeid = $self->result_source->schema | |||||||||
| 20 | ->resultset('ItemType') | ||||||||||
| 21 | ->search({ 'typename' => 'note' }) | ||||||||||
| 22 | ->single->itemtypeid; | ||||||||||
| 23 | 13 | }); | |||||||||
| 24 | |||||||||||
| 25 | 184 | ▶ | has _item_attachment_resultset => ( is => 'rw', default => sub { 'ItemAttachment' } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | # pass the args as is to the parent --- new() takes no params for the current object | ||||||||||
| 28 | 184 | ▶ | sub BUILDARGS { shift; { }; } | ||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | 184 | ▶ | sub FOREIGNBUILDARGS { shift; @_; } | ||||||||
|
Subroutine
FOREIGNBUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 30 | |||||||||||
| 31 | 4 | ▶ | sub with_item_attachment_resultset { | ||||||||
|
Subroutine
with_item_attachment_resultset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 32 | 4 | my ($self, $resultset) = @_; | |||||||||
| 33 | 4 | my $self_clone = $self->search({}); | |||||||||
| 34 | 4 | $self_clone->_item_attachment_resultset($resultset); | |||||||||
| 35 | 4 | $self_clone; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 21 | ▶ | sub search_by_field { | ||||||||
|
Subroutine
search_by_field: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 39 | 21 | my ($self, $field_queries) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 21 | my $schema = $self->result_source->schema; | |||||||||
| 42 | 21 | my $subqueries; | |||||||||
| 43 | |||||||||||
| 44 | 21 | ▶ | while (my ($key, $value) = each %$field_queries) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 22 | 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 | return $self->search_rs( | ||||||||||
| 58 | { | ||||||||||
| 59 | -and => [ map { | ||||||||||
| 60 | 21 | { 'me.itemid' => { '-in' => $_ } } | |||||||||
| 61 | } @$subqueries | ||||||||||
| 62 | ], | ||||||||||
| 63 | }, | ||||||||||
| 64 | { | ||||||||||
| 65 | prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] }, | ||||||||||
| 66 | }, | ||||||||||
| 67 | ); | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 3 | ▶ | sub items_with_attachments_of_mimetypes { | ||||||||
|
Subroutine
items_with_attachments_of_mimetypes: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 71 | 3 | my ($self, @mimetypes) = @_; | |||||||||
| 72 | 3 | ▶ | return unless @mimetypes; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 3 | my $subquery = $self->_attachment_subquery({ mimetype => [ -or => @mimetypes ] }); | |||||||||
| 74 | |||||||||||
| 75 | 3 | return $self->search_rs( | |||||||||
| 76 | { 'me.itemid' => { '-in' => $subquery } }, | ||||||||||
| 77 | { | ||||||||||
| 78 | prefetch => { 'item_datas' => [ 'fieldid', 'valueid' ] }, | ||||||||||
| 79 | }, | ||||||||||
| 80 | ); | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 2 | ▶ | sub items_with_pdf_attachments { | ||||||||
|
Subroutine
items_with_pdf_attachments: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 2 | my ($self) = @_; | |||||||||
| 85 | 2 | $self->items_with_attachments_of_mimetypes('application/pdf'); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 5 | ▶ | sub _attachment_subquery { | ||||||||
|
Subroutine
_attachment_subquery: called CC=1 SCAR=0.0
| |||||||||||
| 89 | 5 | my ($self, $search_query) = @_; | |||||||||
| 90 | 5 | 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 | \do { "IFNULL(me.sourceitemid, me.itemid)" } }, | |||||||||
| 97 | } | ||||||||||
| 98 | )->get_column('outputitemid')->as_query; | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 2 | ▶ | sub _toplevel_items { | ||||||||
|
Subroutine
_toplevel_items: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 2 | my $self = shift; | |||||||||
| 103 | 2 | my $subquery = $self->_attachment_subquery({}); | |||||||||
| 104 | 2 | 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 | 1 | ▶ | sub _trash_items { | ||||||||
|
Subroutine
_trash_items: called CC=1 SCAR=0.0
| |||||||||||
| 117 | 1 | my $self = shift; | |||||||||
| 118 | 1 | my $schema = $self->result_source->schema; | |||||||||
| 119 | # an item is in the trash if either the item itself is in deletedItems.itemid | ||||||||||
| 120 | 1 | 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 | 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 | my $deletedNote = $schema->resultset('ItemNote') | |||||||||
| 130 | ->search({ 'itemid' => { -in => $deleted } }) | ||||||||||
| 131 | ->get_column('sourceitemid')->as_query; | ||||||||||
| 132 | |||||||||||
| 133 | 1 | my $attached = $schema->resultset('ItemAttachment') | |||||||||
| 134 | ->search( { 'sourceitemid' => { '!=' => undef } }) | ||||||||||
| 135 | ->get_column('itemid')->as_query; | ||||||||||
| 136 | |||||||||||
| 137 | 1 | my $noted = $schema->resultset('ItemNote') | |||||||||
| 138 | ->search( { 'sourceitemid' => { '!=' => undef } }) | ||||||||||
| 139 | ->get_column('itemid')->as_query; | ||||||||||
| 140 | |||||||||||
| 141 | 1 | $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 | 13 | 1; | |||||||||
| 164 | |||||||||||
| 165 | __END__ | ||||||||||