Reduced label Cron schedule to just Schedule as it creates a difficulty and a mainten...
[timetracker.git] / notification_edit.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
31 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
32 import('form.Form');
33 import('ttFavReportHelper');
34 import('ttNotificationHelper');
35
36 // Access check.
37 if (!ttAccessCheck(right_manage_team) || !$user->isPluginEnabled('no')) {
38   header('Location: access_denied.php');
39   exit();
40 }
41
42 $notification_id = (int) $request->getParameter('id');
43 $fav_reports = ttFavReportHelper::getReports($user->id);
44
45 if ($request->isPost()) {
46   $cl_fav_report = trim($request->getParameter('fav_report'));
47   $cl_cron_spec = trim($request->getParameter('cron_spec'));
48   $cl_email = trim($request->getParameter('email'));
49   $cl_cc = trim($request->getParameter('cc'));
50   $cl_subject = trim($request->getParameter('subject'));
51   $cl_report_condition = trim($request->getParameter('report_condition'));
52 } else {
53   $notification = ttNotificationHelper::get($notification_id);
54   $cl_fav_report = $notification['report_id'];
55   $cl_cron_spec = $notification['cron_spec'];
56   $cl_email = $notification['email'];
57   $cl_cc = $notification['cc'];
58   $cl_subject = $notification['subject'];
59   $cl_report_condition = $notification['report_condition'];
60 }
61
62 $form = new Form('notificationForm');
63 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$notification_id));
64 $form->addInput(array('type'=>'combobox',
65   'name'=>'fav_report',
66   'style'=>'width: 250px;',
67   'value'=>$cl_fav_report,
68   'data'=>$fav_reports,
69   'datakeys'=>array('id','name'),
70   'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
71 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'cron_spec','style'=>'width: 250px;','value'=>$cl_cron_spec));
72 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 250px;','value'=>$cl_email));
73 $form->addInput(array('type'=>'text','name'=>'cc','style'=>'width: 300px;','value'=>$cl_cc));
74 $form->addInput(array('type'=>'text','name'=>'subject','style'=>'width: 300px;','value'=>$cl_subject));
75 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'report_condition','style'=>'width: 250px;','value'=>$cl_report_condition));
76 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.save')));
77
78 if ($request->isPost()) {
79   // Validate user input.
80   if (!$cl_fav_report) $err->add($i18n->getKey('error.report'));
81   if (!ttValidCronSpec($cl_cron_spec)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.schedule'));
82   if (!ttValidEmail($cl_email)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
83   if (!ttValidEmail($cl_cc, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cc'));
84   if (!ttValidString($cl_subject, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.subject'));
85   if (!ttValidCondition($cl_report_condition)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.condition'));
86
87   if ($err->no()) {
88     // Calculate next execution time.
89     $next = tdCron::getNextOccurrence($cl_cron_spec, mktime());
90
91     if (ttNotificationHelper::update(array(
92         'id' => $notification_id,
93         'team_id' => $user->team_id,
94         'cron_spec' => $cl_cron_spec,
95         'next' => $next,
96         'report_id' => $cl_fav_report,
97         'email' => $cl_email,
98         'cc' => $cl_cc,
99         'subject' => $cl_subject,
100         'report_condition' => $cl_report_condition,
101         'status' => ACTIVE))) {
102         header('Location: notifications.php');
103         exit();
104       } else
105         $err->add($i18n->getKey('error.db'));
106   }
107 } // isPost
108
109 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
110 $smarty->assign('title', $i18n->getKey('title.edit_notification'));
111 $smarty->assign('content_page_name', 'notification_edit.tpl');
112 $smarty->display('index.tpl');