/src/plugins/orangehrmRESTPlugin/lib/Api/Attendance/PunchTimeAPI.php

https://github.com/orangehrm/OrangeHRM · PHP · 142 lines · 79 code · 16 blank · 47 comment · 4 complexity · 72c74b77baf90ec5298c678aab2397b5 MD5 · raw file

  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. */
  19. namespace Orangehrm\Rest\Api\Attendance;
  20. use Orangehrm\Rest\Api\EndPoint;
  21. use Orangehrm\Rest\Api\Exception\BadRequestException;
  22. use \sfContext;
  23. use \DateTimeZone;
  24. use \DateTime;
  25. use \sfException;
  26. use \AttendanceService;
  27. use \EmployeeService;
  28. use \Employee;
  29. use \Exception;
  30. class PunchTimeAPI extends EndPoint{
  31. const PARAMETER_ID = 'id';
  32. const PARAMETER_TIME_ZONE = 'timezone';
  33. const PARAMETER_NOTE = 'note';
  34. const PARAMETER_DATE_TIME = 'datetime';
  35. const PARAMETER_TIME_ZONE_OFFSET = 'timezoneOffset';
  36. protected $employeeService;
  37. protected $attendanceService;
  38. /**
  39. * @return EmployeeService
  40. */
  41. public function getEmployeeService(){
  42. if(!$this->employeeService){
  43. $this->employeeService = new EmployeeService();
  44. }
  45. return $this->employeeService;
  46. }
  47. /**
  48. * @param $employeeService
  49. * @return $this
  50. */
  51. public function setEmployeeService($employeeService){
  52. $this->employeeService = $employeeService;
  53. return $this;
  54. }
  55. /**
  56. * @return AttendanceService
  57. */
  58. public function getAttendanceService() {
  59. if (is_null($this->attendanceService)) {
  60. $this->attendanceService = new AttendanceService();
  61. }
  62. return $this->attendanceService;
  63. }
  64. /**
  65. * @param AttendanceService $attendanceService
  66. */
  67. public function setAttendanceService(AttendanceService $attendanceService) {
  68. $this->attendanceService = $attendanceService;
  69. }
  70. /**
  71. * @param $empNumber
  72. * @return Employee
  73. */
  74. public function checkValidEmployee($empNumber){
  75. try {
  76. return $this->getEmployeeService()->getEmployee($empNumber);
  77. } catch (Exception $e){
  78. new BadRequestException($e->getMessage());
  79. }
  80. }
  81. /**
  82. * @param $remote_tz
  83. * @param null $origin_tz
  84. * @return int
  85. */
  86. function getTimezoneOffset($remote_tz, $origin_tz = null) {
  87. if($origin_tz === null) {
  88. if(!is_string($origin_tz = date_default_timezone_get())) {
  89. return false;
  90. }
  91. }
  92. $origin_dtz = new DateTimeZone($origin_tz);
  93. $remote_dtz = new DateTimeZone($remote_tz);
  94. $origin_dt = new DateTime("now", $origin_dtz);
  95. $remote_dt = new DateTime("now", $remote_dtz);
  96. return $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
  97. }
  98. public function getCurrentUTCTime()
  99. {
  100. return gmdate('Y-m-d H:i');
  101. }
  102. /**
  103. * @return mixed|null
  104. * @throws sfException
  105. */
  106. public function getLoggedInEmployeeNumber()
  107. {
  108. return sfContext::getInstance()->getUser()->getAttribute("auth.empNumber");
  109. }
  110. /**
  111. * @return array
  112. * @throws Exception
  113. */
  114. public function getValidateTimezoneOffsetList()
  115. {
  116. $offsetList = array();
  117. $zoneList = timezone_identifiers_list();
  118. foreach ($zoneList as $timezoneName) {
  119. $timeZoneDTZ = new DateTimeZone($timezoneName);
  120. $dateTimeObj = new DateTime('now', $timeZoneDTZ);
  121. $offset = $dateTimeObj->getOffset();
  122. array_push($offsetList, $offset / 3600);
  123. }
  124. return $offsetList;
  125. }
  126. }