Started to work on ttRegistrator class to encapsulate restration related tasks.
authorNik Okuntseff <support@anuko.com>
Wed, 21 Mar 2018 17:18:39 +0000 (17:18 +0000)
committerNik Okuntseff <support@anuko.com>
Wed, 21 Mar 2018 17:18:39 +0000 (17:18 +0000)
WEB-INF/lib/ttExportHelper.class.php
WEB-INF/lib/ttRegistrator.class.php [new file with mode: 0644]
register.php

index 77b2db1..375de2f 100644 (file)
@@ -360,16 +360,14 @@ class ttExportHelper {
 
   /*
    * Note about the utility functions below.
-   * We have roughly 3 groups of operations:
+   * We have roughly 4 groups of operations:
    *   1) Regular system usage for tracking time, etc.
-   *   2) Admin usage - used infrequently.
-   *   3) Export - used infrequently.
-   *
-   * TODO: we also have user registration process without initialized user.
-   * Perhaps we need a separate helper class for this. Think about it.
+   *   2) Registration process - used infrequently.
+   *   3) Admin usage - used infrequently.
+   *   4) Export - used infrequently.
    *
    * It is tempting to have a generic function to get things done for
-   * all situations. However, as export and admin access are one-off
+   * all situations. However, as registration, export and admin access are one-off
    * operations, while regular system usage is daily and must be efficient,
    * the current approach is to have SEPARATE functions for each mode.
    *
diff --git a/WEB-INF/lib/ttRegistrator.class.php b/WEB-INF/lib/ttRegistrator.class.php
new file mode 100644 (file)
index 0000000..9a650c9
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+// +----------------------------------------------------------------------+
+// | Anuko Time Tracker
+// +----------------------------------------------------------------------+
+// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
+// +----------------------------------------------------------------------+
+// | LIBERAL FREEWARE LICENSE: This source code document may be used
+// | by anyone for any purpose, and freely redistributed alone or in
+// | combination with other software, provided that the license is obeyed.
+// |
+// | There are only two ways to violate the license:
+// |
+// | 1. To redistribute this code in source form, with the copyright
+// |    notice or license removed or altered. (Distributing in compiled
+// |    forms without embedded copyright notices is permitted).
+// |
+// | 2. To redistribute modified versions of this code in *any* form
+// |    that bears insufficient indications that the modifications are
+// |    not the work of the original author(s).
+// |
+// | This license applies to this document only, not any other software
+// | that it may be combined with.
+// |
+// +----------------------------------------------------------------------+
+// | Contributors:
+// | https://www.anuko.com/time_tracker/credits.htm
+// +----------------------------------------------------------------------+
+
+// ttRegistrator class is used to register a user in Time Tracker.
+class ttRegistrator {
+  var $user_name = null;  // User name.
+  var $login = null;      // User login.
+  var $password = null;   // User password.
+  var $email = null;      // User email.
+  var $group_name = null; // Group name.
+  var $currency = null;   // Currency.
+  var $lang = null;       // Language.
+  var $err = null;        // Error object, passed to us as reference.
+                          // We use it to communicate errors to caller.
+
+  // Constructor.
+  function __construct($fields, &$err) {
+    $this->user_name = $fields['user_name'];
+    $this->login = $fields['login'];    
+    $this->password1 = $fields['password1'];
+    $this->password2 = $fields['password2'];
+    $this->email = $fields['email'];
+    $this->group_name = $fields['group_name'];
+    $this->currency = $fields['currency'];
+    $this->lang = $fields['lang'];
+    if (!$thins->lang) $this->lang = 'en';
+    $this->err = $err;
+
+    // Validate passed in parameters.
+    $this->validate();
+  }
+
+  function validate() {
+    global $i18n;
+
+    if (!ttValidString($this->group_name, true))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
+    if (!ttValidString($this->currency, true))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
+    if (!ttValidString($this->user_name))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
+    if (!ttValidString($this->login))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
+    if (!ttValidString($this->password1))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
+    if (!ttValidString($this->password2))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
+    if ($this->password1 !== $this->password2)
+      $this->err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));    
+    if (!ttValidEmail($this->email, true))
+      $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
+  }
+
+  // The register function registers a user in Time Tracker.
+  function register() {
+    // TODO: work in progress. Not implemented.
+  }
+}
index 92e97be..a34a78f 100644 (file)
@@ -85,16 +85,21 @@ $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email',
 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
 
 if ($request->isPost()) {
-  // Validate user input.
-  if (!ttValidString($cl_team_name, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
-  if (!ttValidString($cl_currency, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
-  if (!ttValidString($cl_manager_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
-  if (!ttValidString($cl_manager_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
-  if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
-  if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
-  if ($cl_password1 !== $cl_password2)
-    $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
-  if (!ttValidEmail($cl_manager_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
+  // Create fields array for ttRegistrator instance.
+  $fields = array(
+    'user_name' => $cl_manager_name,
+    'login' => $cl_manager_login,
+    'password1' => $cl_password1,
+    'password2' => $cl_password2,
+    'email' => $cl_manager_email,
+    'group_name' => $cl_team_name,
+    'currency' => $cl_currency,
+    'lang' => $cl_lang);
+
+  // Create an instance of ttRegistrator class.
+  import('ttRegistrator');
+  $registrator = new ttRegistrator($fields, $err);
+  // Validation of user input occurs in ttRegistrator constructor above.
 
   if ($err->no()) {
     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {