Cleaning up whitespace
[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 check.
37 if (!ttAccessCheck(right_manage_team)) {
38   header('Location: access_denied.php');
39   exit();
40 }
41
42 $fav_reports = ttFavReportHelper::getReports($user->id);
43
44 if ($request->getMethod() == 'POST') {
45   $cl_fav_report = trim($request->getParameter('fav_report'));
46   $cl_cron_spec = trim($request->getParameter('cron_spec'));
47   $cl_email = trim($request->getParameter('email'));
48 } else {
49   $cl_cron_spec = '0 4 * * 1'; // Default schedule - weekly on Mondays at 04:00 (server time).
50 }
51
52 $form = new Form('notificationForm');
53 $form->addInput(array('type'=>'combobox',
54   'name'=>'fav_report',
55   'style'=>'width: 250px;',
56   'value'=>$cl_fav_report,
57   'data'=>$fav_reports,
58   'datakeys'=>array('id','name'),
59   'empty'=>array(''=>$i18n->getKey('dropdown.select'))
60 ));
61 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'cron_spec','style'=>'width: 250px;','value'=>$cl_cron_spec));
62 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 250px;','value'=>$cl_email));
63 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->getKey('button.add')));
64
65 if ($request->getMethod() == 'POST') {
66   // Validate user input.
67   if (!$cl_fav_report) $errors->add($i18n->getKey('error.report')); 
68   if (!ttValidCronSpec($cl_cron_spec)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.cron_schedule'));
69   if (!ttValidEmail($cl_email)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
70
71   if ($errors->isEmpty()) {
72     // Calculate next execution time.
73     $next = tdCron::getNextOccurrence($cl_cron_spec, mktime()); 
74
75     if (ttNotificationHelper::insert(array(
76         'team_id' => $user->team_id,
77         'cron_spec' => $cl_cron_spec,
78         'next' => $next,
79         'report_id' => $cl_fav_report,
80         'email' => $cl_email,
81         'status' => ACTIVE))) {
82         header('Location: notifications.php');
83         exit();
84       } else
85         $errors->add($i18n->getKey('error.db'));
86   }
87 } // POST
88
89 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
90 $smarty->assign('title', $i18n->getKey('title.add_notification'));
91 $smarty->assign('content_page_name', 'notification_add.tpl');
92 $smarty->display('index.tpl');