Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / share / html / Dashboards / Subscription.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2015 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 </table>
180 </&>
181 </td>
182 </tr>
183 </table>
184
185 % if ($SubscriptionObj) {
186     <& /Elements/Submit, Name => "Save", Label => loc('Save Changes') &>
187 % } else {
188     <& /Elements/Submit, Name => "Save", Label => loc('Subscribe') &>
189 % }
190 </form>
191
192 <%INIT>
193
194 my ($title, @results);
195 my $Loaded = 0;
196 my $timezone = $session{'CurrentUser'}->UserObj->Timezone || RT->Config->Get('Timezone');
197
198 use RT::Dashboard;
199 my $Dashboard = RT::Dashboard->new($session{'CurrentUser'});
200 my ($ok, $msg) = $Dashboard->LoadById($id);
201 $ok || Abort(loc("Couldn't load dashboard [_1]: [_2]", $id, $msg));
202
203 my $SubscriptionObj = $Dashboard->Subscription;
204
205 $id = $SubscriptionObj ? $SubscriptionObj->SubValue('DashboardId') : $ARGS{'id'};
206
207 my %fields = (
208     DashboardId => $id,
209     Frequency   => 'daily',
210     Hour        => '06:00',
211     Dow         => 'Monday',
212     Dom         => 1,
213     Rows        => 20,
214     Recipient   => '',
215     Fow         => 1,
216     Counter     => 0,
217 );
218
219 # update any fields with the values from the subscription object
220 if ($SubscriptionObj) {
221     for my $field (keys %fields) {
222         $fields{$field} = $SubscriptionObj->SubValue($field);
223     }
224 }
225
226 # finally, update any fields with arguments passed in by the user
227 for my $field (keys %fields) {
228     next if $field eq 'DashboardId'; # but this one is immutable
229     $fields{$field} = $ARGS{$field}
230         if defined($ARGS{$field});
231 }
232
233
234 # this'll be defined on submit
235 if (defined $ARGS{Save}) {
236     my $ok = 1;
237
238     # validation
239     if ($fields{Recipient}) {
240         my @addresses = Email::Address->parse($fields{Recipient});
241         if (@addresses == 0) {
242             push @results, loc('Recipient must be an email address');
243             $ok = 0;
244         }
245     }
246
247     if ($ok) {
248         # update
249         if ($SubscriptionObj) {
250             $id = delete $fields{'DashboardId'}; # immutable
251             ($ok, $msg) = $SubscriptionObj->SetSubValues(%fields);
252             $fields{'DashboardId'} = $id;
253
254             $msg = loc("Subscription updated") if $ok;
255             push @results, $msg;
256         }
257         # create
258         else {
259             Abort(loc("Unable to subscribe to dashboard [_1]: Permission Denied", $id))
260                 unless $Dashboard->CurrentUserCanSubscribe;
261
262             $SubscriptionObj = RT::Attribute->new($session{CurrentUser});
263             ($ok, $msg) = $SubscriptionObj->Create(
264                 Name        => 'Subscription',
265                 Description => 'Subscription to dashboard ' . $id,
266                 ContentType => 'storable',
267                 Object      => $session{'CurrentUser'}->UserObj,
268                 Content     => \%fields,
269             );
270             if ($ok) {
271                 push @results, loc("Subscribed to dashboard [_1]", $Dashboard->Name);
272                 push @results, loc("Warning: you have no email address set, so you will not receive this dashboard until you have it set")
273                     unless $session{'CurrentUser'}->EmailAddress || $fields{Recipient};
274             }
275             else {
276                 push @results, loc('Subscription could not be created: [_1]', $msg);
277             }
278         }
279     }
280 }
281
282 if ($SubscriptionObj) {
283     $title = loc("Modify the subscription to dashboard [_1]", $Dashboard->Name);
284 }
285 else {
286     $title = loc("Subscribe to dashboard [_1]", $Dashboard->Name);
287 }
288
289 </%INIT>
290 <%ARGS>
291 $id => undef
292 $Frequency   => undef
293 $Hour        => undef
294 $Dow         => undef
295 $Dom         => undef
296 $Rows        => undef
297 $Recipient   => undef
298 </%ARGS>
299