rt 4.2.13 ticket#13852
[freeside.git] / rt / share / html / Dashboards / Subscription.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <& /Elements/Header, Title => $title &>
49 <& /Elements/Tabs &>
50
51 <& /Elements/ListActions, actions => \@results &>
52
53 <form action="<%RT->Config->Get('WebPath')%>/Dashboards/Subscription.html" method="post" enctype="multipart/form-data" name="SubscribeDashboard">
54 <input type="hidden" class="hidden" name="id" value="<% $fields{'DashboardId'} %>" />
55 <table width="100%" border="0">
56 <tr>
57
58 <td valign="top" class="boxcontainer">
59 <&| /Widgets/TitleBox, title => loc('Dashboard') &>
60
61 <table>
62 <tr><td class="label">
63 <&|/l&>Dashboard</&>:
64 </td><td class="value">
65 <% $Dashboard->Name %>
66 </td></tr>
67
68 <tr><td class="label">
69 <&|/l&>Queries</&>:
70 </td><td class="value">
71 % my @portlets = grep { defined } $Dashboard->Portlets;
72 % if (!@portlets) {
73 (<&|/l&>none</&>)
74 % } else {
75 <ol class="dashboard-queries">
76 %    for my $portlet (@portlets) {
77         <li class="dashboard-query">
78             <% loc($portlet->{description}, $fields{'Rows'}) %>
79         </li>
80 %    }
81 </ol>
82 % }
83 </td></tr>
84
85 </table>
86 </&>
87
88 <&| /Widgets/TitleBox, title => loc('Subscription') &>
89
90 <table>
91 <tr><td class="label">
92 <&|/l&>Frequency</&>:
93 </td><td class="value">
94
95 <input type="radio" id="Frequency-daily" name="Frequency" value="daily" <% $fields{'Frequency'} eq 'daily' ? 'checked="checked"' : "" |n %>></input>
96 <label for="Frequency-daily"><&|/l&>daily</&></label>
97 <br />
98
99 <input type="radio" id="Frequency-m-f" name="Frequency" value="m-f" <% $fields{'Frequency'} eq 'm-f' ? 'checked="checked"' : "" |n %>></input>
100 <label for="Frequency-m-f"><&|/l&>Monday through Friday</&></label>
101 <br />
102
103 <input type="radio" id="Frequency-weekly" name="Frequency" value="weekly"<% $fields{'Frequency'} eq 'weekly' ? 'checked="checked"' : "" |n %>></input>
104 <label for="Frequency-weekly">
105 <&|/l&>weekly</&>, <&|/l&>on</&>
106 <select name="Dow">
107     <option value="Monday" <% $fields{'Dow'} eq 'Monday' ? 'selected="selected"' : '' |n %>><&|/l&>Monday</&></option>
108     <option value="Tuesday" <% $fields{'Dow'} eq 'Tuesday' ? 'selected="selected"' : '' |n %>><&|/l&>Tuesday</&></option>
109     <option value="Wednesday" <% $fields{'Dow'} eq 'Wednesday' ? 'selected="selected"' : '' |n %>><&|/l&>Wednesday</&></option>
110     <option value="Thursday" <% $fields{'Dow'} eq 'Thursday' ? 'selected="selected"' : '' |n %>><&|/l&>Thursday</&></option>
111     <option value="Friday" <% $fields{'Dow'} eq 'Friday' ? 'selected="selected"' : '' |n %>><&|/l&>Friday</&></option>
112     <option value="Saturday" <% $fields{'Dow'} eq 'Saturday' ? 'selected="selected"' : '' |n %>><&|/l&>Saturday</&></option>
113     <option value="Sunday" <% $fields{'Dow'} eq 'Sunday' ? 'selected="selected"' : '' |n %>><&|/l&>Sunday</&></option>
114 </select>
115 <&|/l&>every</&>
116 <select name="Fow">
117 % for my $f ( qw/1 2 3 4/ ) {
118     <option value="<%$f%>" <% $fields{'Fow'} == $f ? 'selected="selected"' : '' |n %>><% $f %></option>
119 % }
120 </select>
121 <&|/l&>weeks</&>
122 </label>
123 <br />
124
125 <input type="radio" id="Frequency-monthly" name="Frequency" value="monthly"<% $fields{'Frequency'} eq 'monthly' ? 'checked="checked"' : "" |n %>></input>
126 <label for="Frequency-monthly">
127 <&|/l&>monthly</&>, <&|/l&>on day</&>
128 <select name="Dom">
129 %   for my $dom (1..31) {
130     <option value="<% $dom %>" <% $fields{'Dom'} == $dom ? 'selected="selected"' : '' |n %>><% loc($dom) %></option>
131 %   }
132 </select>
133 </label>
134 <br />
135
136 <input type="radio" id="Frequency-never" name="Frequency" value="never" <% $fields{'Frequency'} eq 'never' ? 'checked="checked"' : "" |n %>></input>
137 <label for="Frequency-never"><&|/l&>never</&></label>
138
139 </td></tr>
140 <tr><td class="label">
141 <&|/l&>Hour</&>:
142 </td><td class="value">
143 <select name="Hour">
144 % my $formatter = RT::Date->new($session{CurrentUser})->LocaleObj;
145 % my $dt = DateTime->now;
146 % $dt->set_minute(0);
147 % $dt->set_second(0);
148
149 % for my $hour (0..23) {
150 %     $dt->set_hour($hour);
151 %     my $formatted = $dt->format_cldr($formatter->time_format_short);
152
153 %     my $value = sprintf '%02d:00', $hour;
154 %     my $selected = $value eq $fields{'Hour'}
155 %                  ? 'selected="selected"'
156 %                  : '';
157
158     <option value="<% $value %>" <%$selected|n %>><% $formatted %></option>
159 % }
160 </select>
161 (<%$timezone%>)
162 </td></tr>
163 <tr><td class="label">
164 <&|/l&>Rows</&>:
165 </td><td class="value">
166 <select name="Rows">
167 %   for my $rows (1, 2, 5, 10, 15, 20, 25, 50, 75, 100, 0) {
168     <option value="<% $rows %>" <% $fields{'Rows'} eq $rows ? 'selected="selected"' : '' |n %>><% loc($rows || 'Unlimited') %></option>
169 %   }
170 </select>
171 </td></tr>
172
173 <tr><td class="label">
174 <&|/l&>Recipient</&>:
175 </td><td class="value">
176 <input name="Recipient" id="Recipient" size="30" value="<%$fields{Recipient} ? $fields{Recipient} : ''%>" />
177 <div class="hints"><% loc("Leave blank to send to your current email address ([_1])", $session{'CurrentUser'}->EmailAddress) %></div>
178 </td></tr>
179 % $m->callback( %ARGS, CallbackName => 'SubscriptionFormEnd', FieldsRef => \%fields,
180 %     SubscriptionObj => $SubscriptionObj, DashboardObj => $Dashboard );
181 </table>
182 </&>
183 </td>
184 </tr>
185 </table>
186
187 % if ($SubscriptionObj) {
188     <& /Elements/Submit, Name => "Save", Label => loc('Save Changes') &>
189 % } else {
190     <& /Elements/Submit, Name => "Save", Label => loc('Subscribe') &>
191 % }
192 </form>
193
194 <%INIT>
195
196 my ($title, @results);
197 my $Loaded = 0;
198 my $timezone = $session{'CurrentUser'}->UserObj->Timezone || RT->Config->Get('Timezone');
199
200 use RT::Dashboard;
201 my $Dashboard = RT::Dashboard->new($session{'CurrentUser'});
202 my ($ok, $msg) = $Dashboard->LoadById($id);
203 $ok || Abort(loc("Couldn't load dashboard [_1]: [_2]", $id, $msg));
204
205 my $SubscriptionObj = $Dashboard->Subscription;
206
207 $id = $SubscriptionObj ? $SubscriptionObj->SubValue('DashboardId') : $ARGS{'id'};
208
209 my %fields = (
210     DashboardId => $id,
211     Frequency   => 'daily',
212     Hour        => '06:00',
213     Dow         => 'Monday',
214     Dom         => 1,
215     Rows        => 20,
216     Recipient   => '',
217     Fow         => 1,
218     Counter     => 0,
219 );
220
221 $m->callback( %ARGS, CallbackName => 'SubscriptionFields', FieldsRef => \%fields,
222      SubscriptionObj => $SubscriptionObj, DashboardObj => $Dashboard);
223
224 # update any fields with the values from the subscription object
225 if ($SubscriptionObj) {
226     for my $field (keys %fields) {
227         $fields{$field} = $SubscriptionObj->SubValue($field);
228     }
229 }
230
231 # finally, update any fields with arguments passed in by the user
232 for my $field (keys %fields) {
233     next if $field eq 'DashboardId'; # but this one is immutable
234     $fields{$field} = $ARGS{$field}
235         if defined($ARGS{$field});
236 }
237
238 $m->callback( %ARGS, CallbackName => 'MassageSubscriptionFields', FieldsRef => \%fields,
239      SubscriptionObj => $SubscriptionObj, DashboardObj => $Dashboard);
240
241 # this'll be defined on submit
242 if (defined $ARGS{Save}) {
243     my $ok = 1;
244
245     # validation
246     if ($fields{Recipient}) {
247         my @addresses = Email::Address->parse($fields{Recipient});
248         if (@addresses == 0) {
249             push @results, loc('Recipient must be an email address');
250             $ok = 0;
251         }
252     }
253
254     if ($ok) {
255         # update
256         if ($SubscriptionObj) {
257             $id = delete $fields{'DashboardId'}; # immutable
258             ($ok, $msg) = $SubscriptionObj->SetSubValues(%fields);
259             $fields{'DashboardId'} = $id;
260
261             $msg = loc("Subscription updated") if $ok;
262             push @results, $msg;
263         }
264         # create
265         else {
266             Abort(loc("Unable to subscribe to dashboard [_1]: Permission Denied", $id))
267                 unless $Dashboard->CurrentUserCanSubscribe;
268
269             $SubscriptionObj = RT::Attribute->new($session{CurrentUser});
270             ($ok, $msg) = $SubscriptionObj->Create(
271                 Name        => 'Subscription',
272                 Description => 'Subscription to dashboard ' . $id,
273                 ContentType => 'storable',
274                 Object      => $session{'CurrentUser'}->UserObj,
275                 Content     => \%fields,
276             );
277             if ($ok) {
278                 push @results, loc("Subscribed to dashboard [_1]", $Dashboard->Name);
279                 push @results, loc("Warning: you have no email address set, so you will not receive this dashboard until you have it set")
280                     unless $session{'CurrentUser'}->EmailAddress || $fields{Recipient};
281             }
282             else {
283                 push @results, loc('Subscription could not be created: [_1]', $msg);
284             }
285         }
286     }
287 }
288
289 if ($SubscriptionObj) {
290     $title = loc("Modify the subscription to dashboard [_1]", $Dashboard->Name);
291 }
292 else {
293     $title = loc("Subscribe to dashboard [_1]", $Dashboard->Name);
294 }
295
296 </%INIT>
297 <%ARGS>
298 $id => undef
299 $Frequency   => undef
300 $Hour        => undef
301 $Dow         => undef
302 $Dom         => undef
303 $Rows        => undef
304 $Recipient   => undef
305 </%ARGS>
306