issue.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class Issue implements JsonSerializable{
  3. public $id;
  4. public $cache = array(
  5. 'p_id'=>null,
  6. 'u_id'=>null,
  7. 'pr_id'=>null,
  8. 's_id'=>null,
  9. 'name'=>null,
  10. 'description'=>null,
  11. 'date_created'=>null,
  12. 'date_modified'=>null
  13. );
  14. public function __construct($id){
  15. //name,description[,priority[,user[,project]]]
  16. switch(func_num_args()){
  17. case 5:case 4:case 3:case 2:
  18. $name = func_get_arg(0);
  19. if(
  20. Bugs::$sql->query("
  21. INSERT INTO issues (name,description,pr_id,u_id,p_id,s_id)
  22. VALUES (?,?,?,?,?,getsetting('issue.default.status'));
  23. ",'ssiii',
  24. $name,
  25. func_get_arg(1),
  26. func_num_args()>=3&&!empty(func_get_arg(2))?func_get_arg(2):Bugs::setting('issue.default.priority'),
  27. func_num_args()>=4&&!empty(func_get_arg(3))?func_get_arg(3)->id:Bugs::$user->id,
  28. func_num_args()==5&&!empty(func_get_arg(4))?func_get_arg(4)->id:null
  29. )->execute()
  30. ){
  31. $id = Bugs::$sql->insert_id;
  32. if($id === 0){
  33. trigger_error("Failed to create issue with name {$name}.");
  34. }
  35. }else{
  36. trigger_error(Bugs::$sql->error);
  37. }
  38. case 1:
  39. $this->id = intval($id);
  40. $cache = Bugs::$sql->query("
  41. SELECT p_id,
  42. u_id,
  43. pr_id,
  44. s_id,
  45. name,
  46. description,
  47. date_created,
  48. date_modified
  49. FROM issues
  50. WHERE id = ?;
  51. ",'i',$this->id)->assoc_result;
  52. foreach($cache as $key => $value){
  53. $this->cache[$key] = $value;
  54. }
  55. break;
  56. default:
  57. trigger_error('Invalid Arguments');
  58. }
  59. }
  60. public function jsonSerialize(){
  61. return array(
  62. 'id'=> $this->id,
  63. 'name'=> $this->name,
  64. 'description'=> $this->description,
  65. 'p_id'=> $this->p_id,
  66. 'date_created'=> $this->date_created,
  67. 'date_modified'=> $this->date_modified
  68. );
  69. }
  70. public function __toString(){
  71. return $this->path;
  72. }
  73. public function __set($name,$value){
  74. switch($name){
  75. case 'name':case 'description':
  76. Bugs::$sql->query("
  77. UPDATE issues
  78. SET {$name} = ?
  79. WHERE id = ?
  80. ",'si',$value,$this->id)->execute();
  81. $this->cache[$name] = $value;
  82. break;
  83. case 'p_id':case 's_id':case 'u_id':case 'pr_id':
  84. Bugs::$sql->query("
  85. UPDATE issues
  86. SET {$name} = ?
  87. WHERE id = ?
  88. ",'ii',$value,$this->id)->execute();
  89. break;
  90. case 'project':case 'parent':
  91. if($value instanceof Project){
  92. $this->p_id = $value->id;
  93. }
  94. break;
  95. case 'user':
  96. if($value instanceof User){
  97. $this->u_id = $value->id;
  98. }
  99. break;
  100. default:
  101. if(isset($this->cache[$name])){
  102. $this->cache[$name] = $value;
  103. }
  104. }
  105. }
  106. public function __get($name){
  107. switch($name){
  108. case 'date_created':case 'date_modified':
  109. return strtotime($this->cache[$name]);
  110. break;
  111. case 'user_ids':
  112. return array_column(
  113. Bugs::$sql->query("
  114. SELECT distinct r.u_id
  115. FROM r_issue_user r
  116. RIGHT JOIN issue_roles ir
  117. ON ir.id = r.r_id
  118. WHERE r.i_id = ?
  119. ",'i',$this->id)->assoc_results,
  120. 'u_id'
  121. );
  122. break;
  123. case 'users':
  124. $users = Bugs::$sql->query("
  125. SELECT r.u_id,
  126. pr.name
  127. FROM r_issue_user r
  128. RIGHT JOIN issue_roles ir
  129. ON ir.id = r.r_id
  130. WHERE r.i_id = ?
  131. ",'i',$this->id)->assoc_results;
  132. $ret = array();
  133. foreach($users as $user){
  134. if(!isset($ret[$user['name']])){
  135. $ret[$user['name']] = array();
  136. }
  137. if(!is_null($user['u_id'])){
  138. array_push($ret[$user['name']],Bugs::user($user['u_id']));
  139. }
  140. }
  141. return $ret;
  142. break;
  143. case 'roles':
  144. return $this->roles(Bugs::$user);
  145. break;
  146. case 'user':
  147. return Bugs::user($this->u_id);
  148. break;
  149. case 'project':
  150. return $this->p_id?Bugs::project($this->p_id):false;
  151. break;
  152. case 'status':
  153. return Bugs::status($this->s_id);
  154. break;
  155. case 'priority':
  156. return Bugs::priority($this->pr_id);
  157. break;
  158. default:
  159. if(isset($this->cache)){
  160. return $this->cache[$name];
  161. }
  162. }
  163. }
  164. public function permission($permission,$user=null){
  165. $user = is_null($user)?Bugs::$user:$user;
  166. return $user->admin ||
  167. $user->permission('issue.'.$permission)||
  168. Bugs::$sql->query("
  169. SELECT count(1) count
  170. FROM r_issue_role_permission r
  171. JOIN permissions p
  172. ON p.id = r.per_id
  173. AND p.name IN (?,'*')
  174. JOIN r_issue_user iu
  175. ON iu.r_id = r.r_id
  176. AND iu.u_id = ?
  177. AND iu.p_id = ?
  178. ",'sii','issue.'.$permission,$user->id,$this->id)->assoc_result['count']!==0;
  179. }
  180. public function roles($user){
  181. return array_column(
  182. Bugs::$sql->query("
  183. SELECT distinct pr.name
  184. FROM r_issue_user r
  185. RIGHT JOIN issue_roles ir
  186. ON ir.id = r.r_id
  187. WHERE r.i_id = ?
  188. AND r.u_id = ?
  189. ",'ii',$this->id,$user->id)->assoc_results,
  190. 'name'
  191. );
  192. }
  193. public function role($role,$user=null){
  194. return Bugs::$sql->query("
  195. SELECT count(1) count
  196. FROM r_issue_user r
  197. RIGHT JOIN issue_roles ir
  198. ON ir.id = r.r_id
  199. AND ir.name = ?
  200. WHERE r.i_id = ?
  201. AND r.u_id = ?
  202. ",'sii',$role,$this->id,$user?$user->id:Bugs::$user->id)->assoc_result['count']!==0;
  203. }
  204. }
  205. ?>