Refactored register.php by encapsulating stuff in ttRegistrator class.
[timetracker.git] / WEB-INF / lib / ttRegistrator.class.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 // ttRegistrator class is used to register a user in Time Tracker.
30 class ttRegistrator {
31   var $user_name = null;  // User name.
32   var $login = null;      // User login.
33   var $password = null;   // User password.
34   var $email = null;      // User email.
35   var $group_name = null; // Group name.
36   var $currency = null;   // Currency.
37   var $lang = null;       // Language.
38   var $group_id = null;   // Group id, set after we create a group.
39   var $role_id = null;    // Role id for top managers.
40   var $user_id = null;    // User id after registration.
41   var $err = null;        // Error object, passed to us as reference.
42                           // We use it to communicate errors to caller.
43
44   // Constructor.
45   function __construct($fields, &$err) {
46     $this->user_name = $fields['user_name'];
47     $this->login = $fields['login'];    
48     $this->password1 = $fields['password1'];
49     $this->password2 = $fields['password2'];
50     $this->email = $fields['email'];
51     $this->group_name = $fields['group_name'];
52     $this->currency = $fields['currency'];
53     $this->lang = $fields['lang'];
54     if (!$this->lang) $this->lang = 'en';
55     $this->err = $err;
56
57     // Validate passed in parameters.
58     $this->validate();
59   }
60
61   function validate() {
62     global $i18n;
63
64     if (!ttValidString($this->group_name, true))
65       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
66     if (!ttValidString($this->currency, true))
67       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
68     if (!ttValidString($this->user_name))
69       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
70     if (!ttValidString($this->login))
71       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
72     if (!ttValidString($this->password1))
73       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
74     if (!ttValidString($this->password2))
75       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
76     if ($this->password1 !== $this->password2)
77       $this->err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));    
78     if (!ttValidEmail($this->email, true))
79       $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
80   }
81
82   // The register function registers a user in Time Tracker.
83   function register() {
84     global $i18n;
85
86     if ($this->err->yes())
87       return; // There are errors, do not proceed.
88
89     import('ttUserHelper');
90     if (ttUserHelper::getUserByLogin($this->login)) {
91       // User login already exists.
92       $this->err->add($i18n->getKey('error.user_exists'));
93       return;
94     }
95
96     // Create a new group.
97     $this->group_id = $this->createGroup();
98     if (!$this->group_id) {
99       $this->err->add($i18n->getKey('error.db'));
100       return;
101     }
102
103     import('ttRoleHelper');
104     if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
105       $err->add($i18n->getKey('error.db'));
106       return;
107     }
108     $this->role_id = ttRoleHelper::getTopManagerRoleID();
109     $this->user_id = $this->createUser();
110
111     if (!$this->user_id) {
112       $err->add($i18n->getKey('error.db'));
113       return;
114     }
115   }
116
117   // The createGroup function creates a group in Time Tracker as part
118   // of user registration process. This is a top group for user as top manager.
119   function createGroup() {
120     $mdb2 = getConnection();
121
122     $name = $mdb2->quote($this->group_name);
123     $currency = $mdb2->quote($this->currency);
124     $lang = $mdb2->quote($this->lang);
125
126     $sql = "insert into tt_teams (name, currency, lang) values($name, $currency, $lang)";
127     $affected = $mdb2->exec($sql);
128
129     if (!is_a($affected, 'PEAR_Error')) {
130       $group_id = $mdb2->lastInsertID('tt_teams', 'id');
131       return $group_id;
132     }
133     return false;
134   }
135
136   // The createUser creates a user in database as part of registration process.
137   function createUser() {
138     $mdb2 = getConnection();
139
140     $login = $mdb2->quote($this->login);
141     $password = 'md5('.$mdb2->quote($this->password1).')';
142     $name = $mdb2->quote($this->user_name);
143     $email = $mdb2->quote($this->email);
144     $created = 'now()';
145     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
146     $values = "values($login, $password, $name, $this->group_id, $this->role_id, $email, $created, $created_ip)";
147
148     $sql = 'insert into tt_users (login, password, name, team_id, role_id, email, created, created_ip) '.$values;
149     $affected = $mdb2->exec($sql);
150     if (!is_a($affected, 'PEAR_Error')) {
151       $user_id = $mdb2->lastInsertID('tt_users', 'id');
152       return $user_id;
153     }
154     return false;
155   }
156 }