/public/application/controllers/widget.php

https://gitlab.com/MichelZuniga/neoinvoice · PHP · 169 lines · 130 code · 24 blank · 15 comment · 12 complexity · 7d0794c5b3380e71ccdbd451d8357ef4 MD5 · raw file

  1. <?php
  2. class Widget extends Controller {
  3. protected $username;
  4. protected $password;
  5. protected $user = array();
  6. static $WIDGET_VERSION = 20110705;
  7. function __construct() {
  8. parent::Controller();
  9. }
  10. /**
  11. * Get list of projects and worktypes
  12. * @todo make it work
  13. */
  14. function get_worktypes_projects() {
  15. $this->_authenticate();
  16. $this->load->model('worktype_model');
  17. $this->load->model('project_model');
  18. $worktypes = $this->worktype_model->select_multiple($this->user['company_id']);
  19. $projects = $this->project_model->select_multiple($this->user['company_id'], null, null, FALSE);
  20. $this->_output_data(
  21. array(
  22. 'worktypes' => $worktypes,
  23. 'projects' => $projects,
  24. )
  25. );
  26. }
  27. function debug() {
  28. ?>
  29. <h3>tickets</h3>
  30. <form method="post" action="<?=base_url()?>widget/tickets">
  31. user: <input name="username" /><br />
  32. pass: <input name="password" type="password" /><br />
  33. project: <input name="project" /><br />
  34. <input type="submit" />
  35. </form>
  36. <h3>get_worktypes_projects</h3>
  37. <form method="post" action="<?=base_url()?>widget/get_worktypes_projects">
  38. user: <input name="username" /><br />
  39. pass: <input name="password" type="password" /><br />
  40. <input type="submit" />
  41. </form>
  42. <h3>save</h3>
  43. <form method="post" action="<?=base_url()?>widget/save">
  44. user: <input name="username" /><br />
  45. pass: <input name="password" type="password" /><br />
  46. project: <input name="project" /><br />
  47. worktype: <input name="worktype" /><br />
  48. ticket: <input name="ticket" /><br />
  49. notes: <textarea name="notes"></textarea><br />
  50. <input type="submit" />
  51. </form>
  52. <?php
  53. }
  54. /**
  55. * Save time
  56. * @todo make it work
  57. */
  58. function save() {
  59. $this->_authenticate();
  60. if (!$project = $this->input->post('project')) {
  61. $this->_output_error("Missing Project ID");
  62. }
  63. if (!$worktype = $this->input->post('worktype')) {
  64. $this->_output_error("Missing Worktype ID");
  65. }
  66. $ticket = $this->input->post('ticket');
  67. $notes = htmlentities($this->input->post('notes'));
  68. if (!$duration = $this->input->post('duration')) {
  69. $this->_output_error("Missing Duration");
  70. }
  71. if (!$starttime = $this->input->post('starttime')) {
  72. $this->_output_error("Missing Start Time");
  73. }
  74. $this->load->model('segment_model');
  75. $data = array(
  76. 'company_id' => $this->user['company_id'],
  77. 'project_id' => $project,
  78. 'user_id' => $this->user['id'],
  79. 'worktype_id' => $worktype,
  80. 'date' => date('Y-m-d'),
  81. 'time_start' => $starttime . ':00',
  82. 'duration' => $duration . ':00',
  83. 'content' => $notes,
  84. );
  85. if ($ticket) {
  86. $data['ticket_id'] = $ticket;
  87. }
  88. if ($this->segment_model->insert($data)) {
  89. $this->_output_data(array('success' => 'Time was inserted successfully.'));
  90. } else {
  91. $this->_output_error('There was an error recording your time.');
  92. }
  93. }
  94. /**
  95. * Display the current version of the widget software
  96. */
  97. function version() {
  98. $data = array('version' => self::$WIDGET_VERSION);
  99. $this->_output_data($data);
  100. }
  101. /**
  102. * Get a list of tickets associated with a project
  103. */
  104. function tickets() {
  105. $this->_authenticate();
  106. if (!$project_id = $this->input->post('project')) {
  107. $this->_output_error('Missing Project ID');
  108. }
  109. $this->load->model('security_model');
  110. if (!$this->security_model->own_project($project_id, $this->user['company_id'])) {
  111. $this->_output_error('Project Doesn\'t Exist');
  112. }
  113. $this->load->model('ticket_model');
  114. $data = array('tickets' => $this->ticket_model->select_by_project($project_id));
  115. $this->_output_data($data);
  116. }
  117. function _authenticate() {
  118. $this->load->library('rm_user');
  119. $this->username = $this->input->post('username');
  120. $this->password = $this->input->post('password');
  121. if (!$this->username || !$this->password) {
  122. $this->_output_error('Missing Username or Password');
  123. }
  124. if (!$user_id = $this->rm_user->auth($this->username, $this->password)) {
  125. $this->_output_error('Invalid Username or Password');
  126. }
  127. $this->user = $this->rm_user->get($user_id, array(
  128. 'id',
  129. 'company_id',
  130. ));
  131. return true;
  132. }
  133. function _output_error($data) {
  134. $this->_output_data(array(
  135. 'error' => $data
  136. ));
  137. exit();
  138. }
  139. function _output_data($data) {
  140. header("Cache-Control: no-cache, must-revalidate");
  141. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  142. #header('Content-type: application/json');
  143. echo json_encode($data);
  144. }
  145. }