/core/functions.save/functions.projects.php

https://bitbucket.org/unixcrab/colab-lims · PHP · 199 lines · 134 code · 32 blank · 33 comment · 20 complexity · 46681a40a4e4d902aa21294d84e807a5 MD5 · raw file

  1. <?php
  2. include "config.common.php";
  3. include_once "classes.php";
  4. include_once "functions.db_connect.php";
  5. function addProjectMember($project_id, $person_id) {
  6. global $dbtbl_projects_members;
  7. $sql = "select * from ".$dbtbl_projects_members." where project_id=".$project_id." and person_id=".$person_id;
  8. // error_log($sql);
  9. $r = dbq($sql);
  10. if( $r->num_rows >= 1 ) return -1;
  11. $sql = "insert into ".$dbtbl_projects_members."(project_id,person_id) values(".$project_id.",".$person_id.")";
  12. // error_log($sql);
  13. dbq($sql);
  14. return 1;
  15. }
  16. function removeProjectMember($project_id, $person_id) {
  17. global $dbtbl_projects_members;
  18. $sql = "delete from ".$dbtbl_projects_members." where project_id=".$project_id." and person_id=".$person_id;
  19. // error_log($sql);
  20. $r = dbq($sql);
  21. return 1;
  22. }
  23. function getProjectMembers($project_id) {
  24. global $dbtbl_projects_members;
  25. $retarr = array();
  26. $sql = "select person_id from ".$dbtbl_projects_members." where project_id=".$project_id;
  27. //error_log($sql);
  28. $r = dbq($sql);
  29. while( $p = $r->fetch_object() ) {
  30. // print $p->person_id."<br/>";
  31. $u = getUserDetails($p->person_id);
  32. // print "<br/>".$u->first_name;
  33. if( $u != null ) $retarr[] = $u;
  34. }
  35. return $retarr;
  36. }
  37. function getProject($person_id, $project_id) {
  38. global $dbtbl_projects;
  39. $pmems = getProjectMembers($project_id);
  40. $pids = array();
  41. foreach($pmems as $pmem) {
  42. $pids[] = $pmem->person_id;
  43. }
  44. $sql = "select * from ".$dbtbl_projects." where active=1 and person_id=".$person_id." and project_id=".$project_id." limit 1";
  45. // error_log($sql);
  46. $r = dbq($sql);
  47. if( $r->num_rows == 1 ) {
  48. $p = $r->fetch_object('Project');
  49. return $p;
  50. }
  51. if( in_array( $person_id, $pids ) ) {
  52. // it may not be the owner but rather a member, so get it.
  53. $sql = "select * from ".$dbtbl_projects." where active=1 and project_id=".$project_id." limit 1";
  54. // error_log($sql);
  55. $r = dbq($sql);
  56. if( $r->num_rows == 1 ) {
  57. $p = $r->fetch_object('Project');
  58. return $p;
  59. }
  60. }
  61. return null;
  62. }
  63. function getActiveProjects($person_id) {
  64. global $dbtbl_projects;
  65. global $projects_max_listsize;
  66. $retarr = array();
  67. $sql = "select * from ".$dbtbl_projects." where active=1 and person_id=".$person_id." order by name limit ".$projects_max_listsize;
  68. // error_log($sql);
  69. $r = dbq($sql);
  70. while( $p = $r->fetch_object('Project') ) {
  71. $retarr[] = $p;
  72. }
  73. return $retarr;
  74. }
  75. function getActiveProjectsForMember($person_id) {
  76. global $dbtbl_projects;
  77. global $dbtbl_projects_members;
  78. global $projects_max_listsize;
  79. // $pids = array();
  80. // $sql = "SELECT project_id FROM ".$dbtbl_projects_members." WHERE person_id=".$person_id;
  81. // $r = dbq($sql);
  82. // while( $p = $r->fetch_object() ) {
  83. // $pids[] = (int)($p->project_id);
  84. // }
  85. // var_dump($pids);
  86. $retarr = array();
  87. // $inlist = implode(",", $pids);
  88. // $sql = "select * from ".$dbtbl_projects." where active=1 and project_id in (".$inlist.") order by name limit ".$projects_max_listsize;
  89. $sql = "select * from ".$dbtbl_projects." where active=1 and project_id in (SELECT project_id FROM ".$dbtbl_projects_members." WHERE person_id=".$person_id.") order by name limit ".$projects_max_listsize;
  90. // print $sql;
  91. $r = dbq($sql);
  92. // print "num_rows ".$r->num_rows;
  93. if( isset($r->num_rows) && ($r->num_rows > 0) ) {
  94. while( $p = $r->fetch_object('Project') ) {
  95. $retarr[] = $p;
  96. }
  97. }
  98. return $retarr;
  99. }
  100. function addProject($person_id,$projname,$start_date,$end_date) {
  101. global $dbtbl_projects;
  102. global $mysqli;
  103. $sql = "select * from ".$dbtbl_projects." where name like '".mysqli_real_escape_string($mysqli,$projname)."' limit 1";
  104. // error_log($sql);
  105. $r = dbq($sql);
  106. if( $r->num_rows == 1 ) {
  107. // $sql = "update ".$dbtbl_modules." set active=1 where name like '".$mname."'";
  108. // error_log($sql);
  109. // dbq($sql);
  110. return -1;
  111. }
  112. else {
  113. $sql = "insert into ".$dbtbl_projects."(person_id,name,start_date,end_date,active) values(".$person_id.",'".mysqli_real_escape_string($mysqli,$projname)."',".$start_date.",".$end_date.",1)";
  114. // error_log($sql);
  115. dbq($sql);
  116. return mysqli_insert_id($mysqli); // return new project id
  117. }
  118. return 0;
  119. }
  120. function addProjectFile($person_id, $project_id, $file_name, $timestamp) {
  121. global $dbtbl_projects_files;
  122. global $mysqli;
  123. $sql = "insert into ".$dbtbl_projects_files."(person_id,project_id,file_name,timestamp) values(".$person_id.",".$project_id.",'".mysqli_real_escape_string($mysqli,$file_name)."',".$timestamp.")";
  124. dbq($sql);
  125. // error_log($sql);
  126. }
  127. function getProjectFile($file_id) {
  128. global $dbtbl_projects_files;
  129. $sql = "select * from ".$dbtbl_projects_files." where file_id=".$file_id;
  130. $r = dbq($sql);
  131. return $r->fetch_object('ProjectFile');
  132. }
  133. function getProjectFiles($project_id) {
  134. global $dbtbl_projects_files;
  135. global $projects_max_files;
  136. // global $mysqli;
  137. $retarr = array();
  138. $sql = "select * from ".$dbtbl_projects_files." where project_id=".$project_id." order by timestamp desc limit ".$projects_max_files;
  139. // error_log($sql);
  140. $r = dbq($sql);
  141. while( $f = $r->fetch_object('ProjectFile') ) {
  142. $retarr[$f->file_id] = $f;
  143. }
  144. return $retarr;
  145. }
  146. function deleteProjectFile($person_id,$file_id) {
  147. global $dbtbl_projects_files;
  148. global $projects_dir;
  149. //error_log(__FILE__." deleting ".$file_id." for ".$person_id);
  150. $pf = getProjectFile($file_id);
  151. $proj = getProject($person_id, $pf->project_id);
  152. $sql = "";
  153. if( $person_id == $pf->person_id ) // if file is owned by person
  154. $sql = "delete from ".$dbtbl_projects_files." where file_id=".$file_id." and person_id=".$person_id;
  155. if( $person_id == $proj->person_id ) // if project is owned by person
  156. $sql = "delete from ".$dbtbl_projects_files." where file_id=".$file_id." and project_id=".$proj->project_id;
  157. //error_log($sql);
  158. $r = dbq($sql);
  159. if( isset($r->num_rows) && ($r->num_rows > 0) ) return 1; // something was deleted
  160. // unlink the actual file
  161. if( file_exists($projects_dir.$pf->project_id."/files/".$pf->file_name) ) {
  162. //error_log("deletig ".$projects_dir.$pf->project_id."/files/".$pf->file_name);
  163. unlink($projects_dir.$pf->project_id."/files/".$pf->file_name);
  164. }
  165. return 0;
  166. }
  167. ?>