15d7a32526b14a9967261a1c60ad33f891115c2c
[timetracker.git] / admin_team_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 import('form.Form');
31 import('ttUserHelper');
32
33 // Access check.
34 if (!ttAccessCheck(right_administer_site)) {
35   header('Location: access_denied.php');
36   exit();
37 }
38
39 if ($request->getMethod() == 'POST') {
40   $cl_team_name = trim($request->getParameter('team_name'));
41   $cl_manager_name = trim($request->getParameter('manager_name'));
42   $cl_manager_login = trim($request->getParameter('manager_login'));
43   if (!$auth->isPasswordExternal()) {
44     $cl_password1 = $request->getParameter('password1');
45     $cl_password2 = $request->getParameter('password2');
46   }
47   $cl_manager_email = trim($request->getParameter('manager_email'));
48 }
49   
50 $form = new Form('teamForm');
51 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name));
52 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
53 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
54 if (!$auth->isPasswordExternal()) {
55   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password1','aspassword'=>true,'value'=>$cl_password1));
56   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password2','aspassword'=>true,'value'=>$cl_password2));
57 }
58 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
59 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
60
61 if ($request->getMethod() == 'POST') {
62   // Validate user input.
63   if (!ttValidString($cl_team_name, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
64   if (!ttValidString($cl_manager_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
65   if (!ttValidString($cl_manager_login)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
66   if (!$auth->isPasswordExternal()) {
67     if (!ttValidString($cl_password1)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
68     if (!ttValidString($cl_password2)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
69     if ($cl_password1 !== $cl_password2)
70       $errors->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
71   }     
72   if (!ttValidEmail($cl_manager_email, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
73     
74   if ($errors->isEmpty()) {
75     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {
76       // Create a new team.
77       if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
78       $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>CURRENCY_DEFAULT));
79       if ($team_id) {
80       // Team created, now create a team manager.
81         $user_id = ttUserHelper::insert(array(
82           'team_id' => $team_id,
83           'role' => ROLE_MANAGER,
84           'name' => $cl_manager_name,
85           'login' => $cl_manager_login,
86           'password' => $cl_password1,
87           'email' => $cl_manager_email));
88       }
89       if ($team_id && $user_id) {
90         header('Location: admin_teams.php');
91       } else
92         $errors->add($i18n->getKey('error.db'));
93     } else
94       $errors->add($i18n->getKey('error.user_exists'));
95   }
96 }
97
98 $smarty->assign('auth_external', $auth->isPasswordExternal());
99 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
100 $smarty->assign('onload', 'onLoad="document.teamForm.team.focus()"');
101 $smarty->assign('content_page_name', 'admin_team_add.tpl');
102 $smarty->assign('title', $i18n->getKey('title.create_team'));
103 $smarty->display('index.tpl');