PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ajax.php

http://scruwp.googlecode.com/
PHP | 477 lines | 373 code | 68 blank | 36 comment | 52 complexity | 2ad9c0a6f342ed402f4fe8fee45ad1a1 MD5 | raw file
  1. <?php
  2. require_once('config.php');
  3. require_once('functions.php');
  4. header('Content-type: text/x-json;');
  5. $conn = connect();
  6. switch( $_REQUEST['action'] ){
  7. // TEAM
  8. case 'addTeam': addTeam(); break;
  9. case 'getTeam': getTeam(); break;
  10. // USER
  11. case 'addUser': addUser(); break;
  12. case 'getUser': getUser(); break;
  13. case 'edtUser': edtUser(); break;
  14. // SPRINT
  15. case 'addSprint': addSprint(); break;
  16. case 'getSprint': getSprint(); break;
  17. case 'defaultSprint': defaultSprint(); break;
  18. case 'getSprintByTeam': getSprintByTeam(); break;
  19. // HISTORY
  20. case 'addHistory': addHistory(); break;
  21. case 'getHistory': getHistory(); break;
  22. case 'deleteHistory': deleteHistory(); break;
  23. case 'getHistoryBySprint': getHistoryBySprint(); break;
  24. // TASK
  25. case 'getTask': getTask(); break;
  26. case 'addTask': addTask(); break;
  27. case 'edtTask': edtTask(); break;
  28. case 'deleteTask': deleteTask(); break;
  29. case 'getTaskByHistory': getTaskByHistory(); break;
  30. // STATUS
  31. case 'saveStatus': saveStatus(); break;
  32. // OTHER
  33. case 'getColorSet': getColorSet(); break;
  34. // DEFAULT
  35. default: blank();
  36. }
  37. // TEAM
  38. function addTeam(){
  39. $return = insert(
  40. 'teams', array( 'name' ),
  41. array( $_REQUEST['name'] )
  42. );
  43. echo '{ code: ', $return['code'] ,', id: ', $return['id'] ,', message: "',(
  44. $return['code'] ? 'error' : 'Ok!'
  45. ),'" }';
  46. }
  47. function getTeam(){
  48. $return = selectToJSON(
  49. 'teams', array( '*' )
  50. );
  51. if( is_array( $return ) ){
  52. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  53. $return['query'] ? '", query: "'.$return['query'] : ''
  54. ),'" }';
  55. } else {
  56. echo $return;
  57. }
  58. }
  59. // USER
  60. function addUser(){
  61. $return = insert(
  62. 'users', array( 'name','idColorSet' ),
  63. array( $_REQUEST['name'], $_REQUEST['idColorSet'] )
  64. );
  65. echo '{ code: ', $return['code'] ,', id: ', $return['id'] ,', message: "',(
  66. $return['code'] ? 'error' : 'Ok!'
  67. ),'" }';
  68. }
  69. function getUser(){
  70. $return = selectToJSON(
  71. 'users', array( '*' ), false, array('name')
  72. );
  73. if( is_array( $return ) ){
  74. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  75. $return['query'] ? '", query: "'.$return['query'] : ''
  76. ),'" }';
  77. } else {
  78. echo $return;
  79. }
  80. }
  81. function edtUser(){
  82. $return = update(
  83. 'users', array(
  84. 'idColorSet' => $_REQUEST['idColorSet'],
  85. 'name' => $_REQUEST['name'],
  86. ), array( 'id = '.$_REQUEST['id'] )
  87. );
  88. echo '{ code: ', $return['code'] ,', action: "', $_REQUEST['action'] ,'", id: ', $_REQUEST['id'] ,
  89. ', message: "',$return['message'],( $return['query'] ? '", query: "'.$return['query'] : '' ),
  90. '" }';
  91. }
  92. // SPRINT
  93. function addSprint(){
  94. // DEFAULT STATUS
  95. $update['status'] = false;
  96. $return['status'] = false;
  97. // START THE SQL TRANSACTION
  98. if( startTransaction() ){
  99. // CHECK IF HAS THE DEFAULT FLAG
  100. if( $_REQUEST['status'] ){
  101. // CLEAR THE ACTIVE FLAG
  102. $update = update( 'sprints',
  103. array( 'status' => 0 ), array( 'idTeam = '.$_REQUEST['idTeam'] )
  104. );
  105. } else {
  106. $update['status'] = true;
  107. }
  108. // IF THE FIRST UPDATE IS OK
  109. if( $update['status'] ){
  110. // CALC THE WEEKS IN SECONDS
  111. $endDate = date( 'Y-m-d',(
  112. strtotime( toMysql($_REQUEST['beginDate']) ) + ( 604800 * $_REQUEST['duration'] )
  113. ) );
  114. // INSERT THE SPRINT
  115. $return = insert(
  116. 'sprints', array( 'idTeam','status','beginDate','endDate' ),
  117. array( $_REQUEST['idTeam'],$_REQUEST['status'],toMysql($_REQUEST['beginDate']),$endDate )
  118. );
  119. }
  120. }
  121. // CHECK THE STATUS OS THE QUERIES
  122. if( $update['status'] && $return['status'] ){
  123. // SAVE THE CHANGES
  124. commitTransaction();
  125. } else {
  126. // DONT SAVE IT :P
  127. rollbackTransaction();
  128. // DEFAULT ERROR STATUS
  129. $return = array( 'code' => 1, 'id' => 0 );
  130. }
  131. // ECHO THE JSON RESPONSE
  132. echo '{ code: ', $return['code'] ,', id: ', $return['id'] ,', message: "',(
  133. $return['code'] ? 'Error' : 'Ok!'
  134. ),'" }';
  135. }
  136. function getSprint(){
  137. $return = selectToJSON(
  138. 'sprints', array( '*' ), array( 'id = '.$_REQUEST['id'] )
  139. );
  140. if( is_array( $return ) ){
  141. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  142. $return['query'] ? '", query: "'.$return['query'] : ''
  143. ),'" }';
  144. } else {
  145. echo $return;
  146. }
  147. }
  148. function defaultSprint(){
  149. // DEFAULT STATUS
  150. $update['status'] = false;
  151. // START THE SQL TRANSACTION
  152. if( startTransaction() ){
  153. // RESET THE DEFAULT STATUS
  154. $update = update(
  155. 'sprints', array( 'status' => 0 ), array( 'idTeam = '. $_REQUEST['idTeam'] )
  156. );
  157. // IF THE UPDATE IS OK
  158. if( $update['status'] ){
  159. // CHANGE THE STATUS
  160. $return = update(
  161. 'sprints', array( 'status' => 1 ), array( 'id = '.$_REQUEST['id'] )
  162. );
  163. }
  164. }
  165. // CHECK THE STATUS OS THE QUERIES
  166. if( $update['status'] && $return['status'] ){
  167. // SAVE THE CHANGES
  168. commitTransaction();
  169. } else {
  170. // DONT SAVE IT :P
  171. rollbackTransaction();
  172. // DEFAULT ERROR STATUS
  173. $return = array( 'code' => 1, 'id' => 0 );
  174. }
  175. // ECHO THE JSON RESPONSE
  176. echo '{ code: ', $return['code'] ,', message: "',(
  177. $return['code']
  178. ? 'Has been a error while trying to make this the default sprint'
  179. : 'The sprint #'. $_REQUEST['id'] .' is now the default!'
  180. ),'" }';
  181. }
  182. function getSprintByTeam(){
  183. $return = selectToJSON(
  184. 'sprints', array( '*' ), array( 'idTeam = '.$_REQUEST['id'] )
  185. );
  186. if( is_array( $return ) ){
  187. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  188. $return['query'] ? '", query: "'.$return['query'] : ''
  189. ),'" }';
  190. } else {
  191. echo $return;
  192. }
  193. }
  194. // HISTORY
  195. function addHistory(){
  196. $return = insert(
  197. 'histories', array( 'name','text','estimate','idSprint' ),
  198. array( $_REQUEST['name'],$_REQUEST['text'],$_REQUEST['estimate'],$_REQUEST['idSprint'] )
  199. );
  200. echo '{ code: ', $return['code'] ,', id: ', $return['id'] ,', message: "',(
  201. $return['code'] ? 'error' : 'Ok!'
  202. ),'" }';
  203. }
  204. function getHistory(){
  205. $return = selectToJSON(
  206. 'histories', array( '*' ), (
  207. $_REQUEST['id'] != 'undefined' ? array( ' id = '.$_REQUEST['id'].' ' ) : ''
  208. )
  209. );
  210. if( is_array( $return ) ){
  211. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  212. $return['query'] ? '", query: "'.$return['query'] : ''
  213. ),'" }';
  214. } else {
  215. echo $return;
  216. }
  217. }
  218. function deleteHistory(){
  219. if( startTransaction() ){
  220. $tasks = select(
  221. 'tasks', array('*'), array( 'idHistory = '.$_REQUEST['id'] )
  222. );
  223. $isTasksDeleted = true;
  224. foreach( $tasks['data'] as $task ){
  225. $deleted = deleteTask( $task );
  226. if( !$deleted['status'] ){
  227. $isTasksDeleted = false;
  228. break;
  229. }
  230. }
  231. if( $isTasksDeleted ){
  232. $history = delete(
  233. 'histories', array( 'id = '.$_REQUEST['id'] )
  234. );
  235. if( is_array( $history ) && $history['status'] ){
  236. if( commitTransaction() ){
  237. echo '{ code: 0, message: "Yeah, history deleted with success!" }';
  238. return true;
  239. }
  240. }
  241. }
  242. }
  243. echo '{ code: 1, message: "Ops! The history wasn\'t removed." }';
  244. return rollbackTransaction();
  245. }
  246. function getHistoryBySprint(){
  247. $return = selectToJSON(
  248. 'histories', array( '*' ), array( 'idSprint = '.$_REQUEST['id'] )
  249. );
  250. if( is_array( $return ) ){
  251. echo '{ count: 0, code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  252. $return['query'] ? '", query: "'.$return['query'] : ''
  253. ),'" }';
  254. } else {
  255. echo $return;
  256. }
  257. }
  258. // TASKS
  259. function addTask(){
  260. if( startTransaction() ){
  261. $return = insert(
  262. 'tasks',
  263. array( 'idHistory','idStatus','idUser','text' ),
  264. array(
  265. $_REQUEST['history'], $_REQUEST['status'],
  266. $_REQUEST['userAddTask'], $_REQUEST['text']
  267. )
  268. );
  269. if( $return['status'] )
  270. $log = insert(
  271. 'tasks_log', array( 'idTask','oldStatus','newStatus' ),
  272. array( $return['id'], 0, 1 )
  273. );
  274. }
  275. if( $return['status'] && $log['status'] ){
  276. commitTransaction();
  277. } else {
  278. rollbackTransaction();
  279. }
  280. echo '{ code: ', $return['code'] ,', id: ', $return['id'] ,', message: "',(
  281. $return['code'] ? '' : ''
  282. ),'" }';
  283. }
  284. function getTask(){
  285. $return = queryToJSON('
  286. SELECT
  287. t.id,
  288. t.idStatus,
  289. t.idHistory,
  290. t.idUser,
  291. t.text,
  292. u.name,
  293. c.background,
  294. c.border,
  295. c.color
  296. FROM
  297. tasks t
  298. INNER JOIN users u ON t.idUser = u.id
  299. INNER JOIN colors_set c ON u.idColorSet = c.id
  300. WHERE
  301. t.id = '. $_REQUEST['id'] .'
  302. ');
  303. if( is_array( $return ) ){
  304. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  305. $return['query'] ? '", query: "'.$return['query'] : ''
  306. ),'" }';
  307. } else {
  308. echo $return;
  309. }
  310. }
  311. function deleteTask($option = false){
  312. $task = $option ? $option : $_REQUEST;
  313. if( startTransaction() ){
  314. $return = insert(
  315. 'tasks_log', array( 'idTask','oldStatus','newStatus' ),
  316. array( $task['id'], $task['status'], 0 )
  317. );
  318. if( $return['status'] )
  319. $return = delete( 'tasks', array( 'id = '.$task['id'] ) );
  320. }
  321. if( $return['status'] ){
  322. commitTransaction();
  323. } else {
  324. rollbackTransaction();
  325. }
  326. if( $option ){
  327. return $return;
  328. } else {
  329. echo '{ code: ', $return['code'] ,', message: "',$return['message'],'" }';
  330. }
  331. }
  332. function getTaskByHistory(){
  333. $return = queryToJSON('
  334. SELECT
  335. t.id,
  336. t.idStatus,
  337. t.idHistory,
  338. t.idUser,
  339. t.text,
  340. u.name,
  341. c.background,
  342. c.border,
  343. c.color
  344. FROM
  345. tasks t
  346. INNER JOIN users u ON t.idUser = u.id
  347. INNER JOIN colors_set c ON u.idColorSet = c.id
  348. WHERE
  349. t.idHistory = '. $_REQUEST['id'] .'
  350. ');
  351. if( is_array( $return ) ){
  352. echo '{ count: 0, code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  353. $return['query'] ? '", query: "'.$return['query'] : ''
  354. ),'" }';
  355. } else {
  356. echo $return;
  357. }
  358. }
  359. function edtTask(){
  360. $return = update(
  361. 'tasks', array(
  362. 'idUser' => $_REQUEST['idUser'],
  363. 'text' => $_REQUEST['text'],
  364. ), array( 'id = '.$_REQUEST['id'] )
  365. );
  366. echo '{ code: ', $return['code'] ,', action: "', $_REQUEST['action'] ,'", id: ', $_REQUEST['id'] ,
  367. ', message: "',$return['message'],( $return['query'] ? '", query: "'.$return['query'] : '' ),
  368. '" }';
  369. }
  370. function saveStatus(){
  371. if( startTransaction() ){
  372. $return = insert(
  373. 'tasks_log', array( 'idTask','oldStatus','newStatus' ),
  374. array( $_REQUEST['id'],$_REQUEST['oldStatus'],$_REQUEST['status'] )
  375. );
  376. if( $return['status'] ){
  377. $return = update(
  378. 'tasks', array(
  379. 'idStatus' => $_REQUEST['status'],
  380. ), array( 'id = '.$_REQUEST['id'] )
  381. );
  382. }
  383. }
  384. if( $return['status'] ){
  385. commitTransaction();
  386. } else {
  387. rollbackTransaction();
  388. }
  389. echo '{ code: ', $return['code'] ,', message: "',(
  390. $return['code'] ? 'Has been a error while trying to save the task; Ops!' : ''
  391. ),'" }';
  392. }
  393. // OTHER
  394. function getColorSet(){
  395. $return = selectToJSON(
  396. 'colors_set', array( '*' )
  397. );
  398. if( is_array( $return ) ){
  399. echo '{ code: ', $return['code'], ', message: "',sanitize( $return['message'] ),(
  400. $return['query'] ? '", query: "'.$return['query'] : ''
  401. ),'" }';
  402. } else {
  403. echo $return;
  404. }
  405. }
  406. function blank(){
  407. die( '{ code: 1, message: "Invalid action" }' );
  408. }
  409. ?>