Lines 42-56
sub add_to_session {
Link Here
|
42 |
my $total = $params->{total}; |
42 |
my $total = $params->{total}; |
43 |
my $type = $params->{type} || 'biblio'; |
43 |
my $type = $params->{type} || 'biblio'; |
44 |
|
44 |
|
|
|
45 |
# To a cookie (the user is not logged in) |
46 |
my $now = dt_from_string; |
47 |
my $id = $now->year . $now->month . $now->day . $now->hour . $now->minute . $now->second . int(rand(100)); |
45 |
my @recent_searches = get_from_session( { cgi => $cgi } ); |
48 |
my @recent_searches = get_from_session( { cgi => $cgi } ); |
46 |
push @recent_searches, |
49 |
push @recent_searches, { |
47 |
{ |
|
|
48 |
query_desc => $query_desc, |
50 |
query_desc => $query_desc, |
49 |
query_cgi => $query_cgi, |
51 |
query_cgi => $query_cgi, |
50 |
total => "$total", |
52 |
total => "$total", |
51 |
type => $type, |
53 |
type => $type, |
52 |
time => output_pref( { dt => dt_from_string(), dateformat => 'iso', timeformat => '24hr' } ), |
54 |
time => output_pref( { dt => $now, dateformat => 'iso', timeformat => '24hr' } ), |
53 |
}; |
55 |
id => $id, |
|
|
56 |
}; |
54 |
|
57 |
|
55 |
shift @recent_searches if ( @recent_searches > 15 ); |
58 |
shift @recent_searches if ( @recent_searches > 15 ); |
56 |
set_to_session( { cgi => $cgi, search_history => \@recent_searches } ); |
59 |
set_to_session( { cgi => $cgi, search_history => \@recent_searches } ); |
Lines 58-79
sub add_to_session {
Link Here
|
58 |
|
61 |
|
59 |
sub delete { |
62 |
sub delete { |
60 |
my ($params) = @_; |
63 |
my ($params) = @_; |
|
|
64 |
my $id = $params->{id}; |
61 |
my $userid = $params->{userid}; |
65 |
my $userid = $params->{userid}; |
62 |
my $sessionid = $params->{sessionid}; |
66 |
my $sessionid = $params->{sessionid}; |
63 |
my $type = $params->{type} || q{}; |
67 |
my $type = $params->{type} || q{}; |
64 |
my $previous = $params->{previous} || 0; |
68 |
my $previous = $params->{previous} || 0; |
65 |
|
69 |
|
66 |
unless ($userid) { |
70 |
unless ( ref( $id ) ) { |
67 |
warn "ERROR: userid is required for history search"; |
71 |
$id = $id ? [ $id ] : []; |
|
|
72 |
} |
73 |
|
74 |
unless ( $userid or @$id ) { |
75 |
warn "ERROR: userid or id is required for history deletion"; |
68 |
return; |
76 |
return; |
69 |
} |
77 |
} |
70 |
|
78 |
|
71 |
my $dbh = C4::Context->dbh; |
79 |
my $dbh = C4::Context->dbh; |
72 |
my $query = q{ |
80 |
my $query = q{ |
73 |
DELETE FROM search_history |
81 |
DELETE FROM search_history |
74 |
WHERE userid = ? |
82 |
WHERE 1 |
75 |
}; |
83 |
}; |
76 |
|
84 |
|
|
|
85 |
$query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @$id ) . q{ )} |
86 |
if @$id; |
87 |
|
88 |
$query .= q{ |
89 |
AND userid = ? |
90 |
} if $userid; |
91 |
|
77 |
if ($sessionid) { |
92 |
if ($sessionid) { |
78 |
$query .= |
93 |
$query .= |
79 |
$previous |
94 |
$previous |
Lines 85-104
sub delete {
Link Here
|
85 |
if $type; |
100 |
if $type; |
86 |
|
101 |
|
87 |
$dbh->do( |
102 |
$dbh->do( |
88 |
$query, {}, $userid, |
103 |
$query, {}, |
|
|
104 |
( @$id ? ( @$id ) : () ), |
105 |
( $userid ? $userid : () ), |
89 |
( $sessionid ? $sessionid : () ), |
106 |
( $sessionid ? $sessionid : () ), |
90 |
( $type ? $type : () ) |
107 |
( $type ? $type : () ) |
91 |
); |
108 |
); |
92 |
} |
109 |
} |
93 |
|
110 |
|
|
|
111 |
sub delete_from_cookie { |
112 |
my ($params) = @_; |
113 |
my $cookie = $params->{cookie}; |
114 |
my $id = $params->{id}; |
115 |
|
116 |
return unless $cookie; |
117 |
|
118 |
unless ( ref( $id ) ) { |
119 |
$id = $id ? [ $id ] : []; |
120 |
} |
121 |
return unless @$id; |
122 |
|
123 |
my @searches; |
124 |
if ( $cookie ){ |
125 |
$cookie = uri_unescape( $cookie ); |
126 |
if (decode_json( $cookie )) { |
127 |
@searches = @{decode_json( $cookie )} |
128 |
} |
129 |
} |
130 |
|
131 |
@searches = map { |
132 |
my $search = $_; |
133 |
( grep { $_ != $search->{id} } @$id ) ? $search : () |
134 |
} @searches; |
135 |
|
136 |
return uri_escape( encode_json( \@searches ) ); |
137 |
|
138 |
} |
139 |
|
94 |
sub get { |
140 |
sub get { |
95 |
my ($params) = @_; |
141 |
my ($params) = @_; |
|
|
142 |
my $id = $params->{id}; |
96 |
my $userid = $params->{userid}; |
143 |
my $userid = $params->{userid}; |
97 |
my $sessionid = $params->{sessionid}; |
144 |
my $sessionid = $params->{sessionid}; |
98 |
my $type = $params->{type}; |
145 |
my $type = $params->{type}; |
99 |
my $previous = $params->{previous}; |
146 |
my $previous = $params->{previous}; |
100 |
|
147 |
|
101 |
unless ($userid) { |
148 |
unless ( ref( $id ) ) { |
|
|
149 |
$id = $id ? [ $id ] : []; |
150 |
} |
151 |
|
152 |
unless ( $userid or @$id ) { |
102 |
warn "ERROR: userid is required for history search"; |
153 |
warn "ERROR: userid is required for history search"; |
103 |
return; |
154 |
return; |
104 |
} |
155 |
} |
Lines 106-114
sub get {
Link Here
|
106 |
my $query = q{ |
157 |
my $query = q{ |
107 |
SELECT * |
158 |
SELECT * |
108 |
FROM search_history |
159 |
FROM search_history |
109 |
WHERE userid = ? |
160 |
WHERE 1 |
110 |
}; |
161 |
}; |
111 |
|
162 |
|
|
|
163 |
$query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @$id ) . q{ )} |
164 |
if @$id; |
165 |
|
166 |
$query .= q{ |
167 |
AND userid = ? |
168 |
} if $userid; |
169 |
|
112 |
if ($sessionid) { |
170 |
if ($sessionid) { |
113 |
$query .= |
171 |
$query .= |
114 |
$previous |
172 |
$previous |
Lines 122-128
sub get {
Link Here
|
122 |
my $dbh = C4::Context->dbh; |
180 |
my $dbh = C4::Context->dbh; |
123 |
my $sth = $dbh->prepare($query); |
181 |
my $sth = $dbh->prepare($query); |
124 |
$sth->execute( |
182 |
$sth->execute( |
125 |
$userid, |
183 |
( @$id ? ( @$id ) : () ), |
|
|
184 |
( $userid ? $userid : () ), |
126 |
( $sessionid ? $sessionid : () ), |
185 |
( $sessionid ? $sessionid : () ), |
127 |
( $type ? $type : () ) |
186 |
( $type ? $type : () ) |
128 |
); |
187 |
); |
129 |
- |
|
|