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('ttProjectHelper');
 
  32 import('ttTeamHelper');
 
  35 if (!ttAccessAllowed('manage_projects') || (MODE_PROJECTS != $user->tracking_mode && MODE_PROJECTS_AND_TASKS != $user->tracking_mode)) {
 
  36   header('Location: access_denied.php');
 
  40 $cl_project_id = (int)$request->getParameter('id');
 
  42 $users = ttTeamHelper::getActiveUsers();
 
  43 foreach ($users as $user_item)
 
  44   $all_users[$user_item['id']] = $user_item['name'];
 
  46 $tasks = ttTeamHelper::getActiveTasks($user->team_id);
 
  47 foreach ($tasks as $task_item)
 
  48   $all_tasks[$task_item['id']] = $task_item['name'];
 
  50 if ($request->isPost()) {
 
  51   $cl_name = trim($request->getParameter('project_name'));
 
  52   $cl_description = trim($request->getParameter('description'));
 
  53   $cl_status = $request->getParameter('status');
 
  54   $cl_users = $request->getParameter('users', array());
 
  55   $cl_tasks = $request->getParameter('tasks', array());
 
  57   $project = ttProjectHelper::get($cl_project_id);
 
  58   $cl_name = $project['name'];
 
  59   $cl_description = $project['description'];
 
  60   $cl_status = $project['status'];
 
  62   $mdb2 = getConnection();
 
  63   $sql = "select user_id from tt_user_project_binds where status = 1 and project_id = $cl_project_id";
 
  64   $res = $mdb2->query($sql);
 
  65   if (is_a($res, 'PEAR_Error'))
 
  66     die($res->getMessage());
 
  67   while ($row = $res->fetchRow())
 
  68     $cl_users[] = $row['user_id'];
 
  70   $cl_tasks = explode(',', $project['tasks']);
 
  73 $form = new Form('projectForm');
 
  74 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_project_id));
 
  75 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'project_name','value'=>$cl_name));
 
  76 $form->addInput(array('type'=>'textarea','name'=>'description','class'=>'mobile-textarea','value'=>$cl_description));
 
  77 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
 
  78   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
 
  79 $form->addInput(array('type'=>'checkboxgroup','name'=>'users','data'=>$all_users,'layout'=>'H','value'=>$cl_users));
 
  80 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
  81   $form->addInput(array('type'=>'checkboxgroup','name'=>'tasks','data'=>$all_tasks,'layout'=>'H','value'=>$cl_tasks));
 
  82 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 
  83 $form->addInput(array('type'=>'submit','name'=>'btn_copy','value'=>$i18n->get('button.copy')));
 
  84 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
 
  86 if ($request->isPost()) {
 
  87   // Validate user input.
 
  88   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
 
  89   if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
 
  92     if ($request->getParameter('btn_save')) {
 
  93       $existing_project = ttProjectHelper::getProjectByName($cl_name);
 
  94       if (!$existing_project || ($cl_project_id == $existing_project['id'])) {
 
  95          // Update project information.
 
  96          if (ttProjectHelper::update(array(
 
  97            'id' => $cl_project_id,
 
  99            'description' => $cl_description,
 
 100            'status' => $cl_status,
 
 101            'users' => $cl_users,
 
 102            'tasks' => $cl_tasks))) {
 
 103            header('Location: projects.php');
 
 106            $err->add($i18n->get('error.db'));
 
 108         $err->add($i18n->get('error.project_exists'));
 
 111     if ($request->getParameter('btn_copy')) {
 
 112       if (!ttProjectHelper::getProjectByName($cl_name)) {
 
 113         if (ttProjectHelper::insert(array(
 
 114           'team_id' => $user->team_id,
 
 116           'description' => $cl_description,
 
 117           'users' => $cl_users,
 
 118           'tasks' => $cl_tasks,
 
 119           'status' => ACTIVE))) {
 
 120           header('Location: projects.php');
 
 123           $err->add($i18n->get('error.db'));
 
 125         $err->add($i18n->get('error.project_exists'));
 
 128     if ($request->getParameter('btn_delete')) {
 
 129       header("Location: project_delete.php?id=$cl_project_id");
 
 135 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 136 $smarty->assign('onload', 'onLoad="document.projectForm.project_name.focus()"');
 
 137 $smarty->assign('title', $i18n->get('title.edit_project'));
 
 138 $smarty->assign('content_page_name', 'mobile/project_edit.tpl');
 
 139 $smarty->display('mobile/index.tpl');