Refactoring. Moved week_menu config option to plugin config.
[timetracker.git] / notification_add.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 checks.
37 if (!ttAccessAllowed('manage_advanced_settings')) {
38   header('Location: access_denied.php');
39   exit();
40 }
41 if (!$user->isPluginEnabled('no')) {
42   header('Location: feature_disabled.php');
43   exit();
44 }
45 if (!$user->exists()) {
46   header('Location: access_denied.php'); // No users in subgroup.
47   exit();
48 }
49 if ($request->isPost()) {
50   $cl_fav_report_id = (int) $request->getParameter('fav_report');
51   if (!ttFavReportHelper::get($cl_fav_report_id)) {
52     header('Location: access_denied.php'); // Invalid fav report id in post.
53     exit();
54   }
55 }
56 // End of access checks.
57
58 $fav_reports = ttFavReportHelper::getReports();
59
60 if ($request->isPost()) {
61   $cl_cron_spec = trim($request->getParameter('cron_spec'));
62   $cl_email = trim($request->getParameter('email'));
63   $cl_cc = trim($request->getParameter('cc'));
64   $cl_subject = trim($request->getParameter('subject'));
65   $cl_report_condition = trim($request->getParameter('report_condition'));
66 } else {
67   $cl_cron_spec = '0 4 * * 1'; // Default schedule - weekly on Mondays at 04:00 (server time).
68 }
69
70 $form = new Form('notificationForm');
71 $form->addInput(array('type'=>'combobox',
72   'name'=>'fav_report',
73   'style'=>'width: 250px;',
74   'value'=>$cl_fav_report_id,
75   'data'=>$fav_reports,
76   'datakeys'=>array('id','name'),
77   'empty'=>array(''=>$i18n->get('dropdown.select'))
78 ));
79 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'cron_spec','style'=>'width: 250px;','value'=>$cl_cron_spec));
80 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 250px;','value'=>$cl_email));
81 $form->addInput(array('type'=>'text','name'=>'cc','style'=>'width: 300px;','value'=>$cl_cc));
82 $form->addInput(array('type'=>'text','name'=>'subject','style'=>'width: 300px;','value'=>$cl_subject));
83 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'report_condition','style'=>'width: 250px;','value'=>$cl_report_condition));
84 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->get('button.add')));
85
86 if ($request->isPost()) {
87   // Validate user input.
88   if (!$cl_fav_report_id) $err->add($i18n->get('error.report'));
89   if (!ttValidCronSpec($cl_cron_spec)) $err->add($i18n->get('error.field'), $i18n->get('label.schedule'));
90   if (!ttValidEmail($cl_email)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
91   if (!ttValidEmail($cl_cc, true)) $err->add($i18n->get('error.field'), $i18n->get('label.cc'));
92   if (!ttValidString($cl_subject, true)) $err->add($i18n->get('error.field'), $i18n->get('label.subject'));
93   if (!ttValidCondition($cl_report_condition)) $err->add($i18n->get('error.field'), $i18n->get('label.condition'));
94
95   if ($err->no()) {
96     // Calculate next execution time.
97     $next = tdCron::getNextOccurrence($cl_cron_spec, mktime()); 
98
99     if (ttNotificationHelper::insert(array(
100         'cron_spec' => $cl_cron_spec,
101         'next' => $next,
102         'report_id' => $cl_fav_report_id,
103         'email' => $cl_email,
104         'cc' => $cl_cc,
105         'subject' => $cl_subject,
106         'report_condition' => $cl_report_condition,
107         'status' => ACTIVE))) {
108         header('Location: notifications.php');
109         exit();
110       } else
111         $err->add($i18n->get('error.db'));
112   }
113 } // isPost
114
115 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
116 $smarty->assign('title', $i18n->get('title.add_notification'));
117 $smarty->assign('content_page_name', 'notification_add.tpl');
118 $smarty->display('index.tpl');