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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('initialize.php');
 
  31 import('ttUserHelper');
 
  32 import('ttRoleHelper');
 
  34 if (!isTrue(MULTITEAM_MODE) || $auth->isPasswordExternal()) {
 
  35   header('Location: login.php');
 
  41 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
 
  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'));
 
  54   $cl_currency = CURRENCY_DEFAULT;
 
  55   $cl_lang = $i18n->lang; // Browser setting from initialize.php.
 
  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));
 
  62 // Prepare an array of available languages.
 
  63 $lang_files = I18n::getLangFileList();
 
  64 foreach ($lang_files as $lfile) {
 
  65   $content = file(RESOURCE_DIR."/".$lfile);
 
  67   foreach ($content as $line) {
 
  68     if (strstr($line, 'i18n_language')) {
 
  69       $a = explode('=', $line);
 
  70       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
 
  75   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
 
  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));
 
  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')));
 
  87 if ($request->isPost()) {
 
  88   // Validate user input.
 
  89   if (!ttValidString($cl_team_name, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
 
  90   if (!ttValidString($cl_currency, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
 
  91   if (!ttValidString($cl_manager_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
 
  92   if (!ttValidString($cl_manager_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
 
  93   if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
 
  94   if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
 
  95   if ($cl_password1 !== $cl_password2)
 
  96     $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
 
  97   if (!ttValidEmail($cl_manager_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
 
 100     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {
 
 101       // Create a new team.
 
 102       $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency,'lang'=>$cl_lang));
 
 104         if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang))
 
 105           $err->add($i18n->getKey('error.db'));
 
 107         $role_id = ttRoleHelper::getTopManagerRoleID();
 
 109         // Team created, now create a team manager.
 
 110         $user_id = ttUserHelper::insert(array(
 
 111           'team_id' => $team_id,
 
 112           'role_id' => $role_id,
 
 113           'name' => $cl_manager_name,
 
 114           'login' => $cl_manager_login,
 
 115           'password' => $cl_password1,
 
 116           'email' => $cl_manager_email));
 
 118       if ($team_id && $user_id) {
 
 119         if ($auth->doLogin($cl_manager_login, $cl_password1)) {
 
 120           setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/');
 
 121           header('Location: time.php');
 
 123           header('Location: login.php');
 
 127         $err->add($i18n->getKey('error.db'));
 
 129       $err->add($i18n->getKey('error.user_exists'));
 
 133 $smarty->assign('title', $i18n->getKey('title.create_team'));
 
 134 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 135 $smarty->assign('onload', 'onLoad="document.profileForm.team.focus()"');
 
 136 $smarty->assign('content_page_name', 'register.tpl');
 
 137 $smarty->display('index.tpl');