Started to work on ttRegistrator class to encapsulate restration related tasks.
[timetracker.git] / register.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 import('ttRoleHelper');
33
34 if (!isTrue(MULTITEAM_MODE) || $auth->isPasswordExternal()) {
35   header('Location: login.php');
36   exit();
37 }
38
39 $auth->doLogout();
40
41 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
42
43 if ($request->isPost()) {
44   $cl_team_name = trim($request->getParameter('team_name'));
45   $cl_currency = trim($request->getParameter('currency'));
46   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
47   $cl_lang = $request->getParameter('lang');
48   $cl_manager_name = trim($request->getParameter('manager_name'));
49   $cl_manager_login = trim($request->getParameter('manager_login'));
50   $cl_password1 = $request->getParameter('password1');
51   $cl_password2 = $request->getParameter('password2');
52   $cl_manager_email = trim($request->getParameter('manager_email'));
53 } else {
54   $cl_currency = CURRENCY_DEFAULT;
55   $cl_lang = $i18n->lang; // Browser setting from initialize.php.
56 }
57
58 $form = new Form('profileForm');
59 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name));
60 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
61
62 // Prepare an array of available languages.
63 $lang_files = I18n::getLangFileList();
64 foreach ($lang_files as $lfile) {
65   $content = file(RESOURCE_DIR."/".$lfile);
66   $lname = '';
67   foreach ($content as $line) {
68     if (strstr($line, 'i18n_language')) {
69       $a = explode('=', $line);
70       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
71       break;
72     }
73   }
74   unset($content);
75   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
76 }
77 $longname_lang = mu_sort($longname_lang, 'name');
78 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
79
80 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
81 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
82 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
83 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
84 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
85 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
86
87 if ($request->isPost()) {
88   // Create fields array for ttRegistrator instance.
89   $fields = array(
90     'user_name' => $cl_manager_name,
91     'login' => $cl_manager_login,
92     'password1' => $cl_password1,
93     'password2' => $cl_password2,
94     'email' => $cl_manager_email,
95     'group_name' => $cl_team_name,
96     'currency' => $cl_currency,
97     'lang' => $cl_lang);
98
99   // Create an instance of ttRegistrator class.
100   import('ttRegistrator');
101   $registrator = new ttRegistrator($fields, $err);
102   // Validation of user input occurs in ttRegistrator constructor above.
103
104   if ($err->no()) {
105     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {
106       // Create a new team.
107       $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency,'lang'=>$cl_lang));
108       if ($team_id) {
109         if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang))
110           $err->add($i18n->getKey('error.db'));
111
112         $role_id = ttRoleHelper::getTopManagerRoleID();
113
114         // Team created, now create a team manager.
115         $user_id = ttUserHelper::insert(array(
116           'team_id' => $team_id,
117           'role_id' => $role_id,
118           'name' => $cl_manager_name,
119           'login' => $cl_manager_login,
120           'password' => $cl_password1,
121           'email' => $cl_manager_email));
122       }
123       if ($team_id && $user_id) {
124         if ($auth->doLogin($cl_manager_login, $cl_password1)) {
125           setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/');
126           header('Location: time.php');
127         } else {
128           header('Location: login.php');
129         }
130         exit();
131       } else
132         $err->add($i18n->getKey('error.db'));
133     } else
134       $err->add($i18n->getKey('error.user_exists'));
135   }
136 } // isPost
137
138 $smarty->assign('title', $i18n->getKey('title.create_team'));
139 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
140 $smarty->assign('onload', 'onLoad="document.profileForm.team.focus()"');
141 $smarty->assign('content_page_name', 'register.tpl');
142 $smarty->display('index.tpl');