api.contractdates.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Contract dates manipulation class
  4. */
  5. class ContractDates {
  6. /**
  7. * Contract dates database abstraction layer
  8. *
  9. * @var object
  10. */
  11. protected $contractDatesDb = '';
  12. /**
  13. * some predefined stuff
  14. */
  15. const TABLE_CONDATES = 'contractdates';
  16. const PROUTE_RUNEDIT = 'newcontractnum';
  17. const PROUTE_DATE = 'newcontractdate';
  18. const PROUTE_FROM = 'newcontractfrom';
  19. const PROUTE_TILL = 'newcontracttill';
  20. public function __construct() {
  21. $this->initDb();
  22. }
  23. /**
  24. * Inits database abstraction layer
  25. *
  26. * @return void
  27. */
  28. protected function initDb() {
  29. $this->contractDatesDb = new NyanORM(self::TABLE_CONDATES);
  30. }
  31. /**
  32. * Creates new contract date database record
  33. *
  34. * @param $contract - existing contract num
  35. * @param $date - contract creation date
  36. * @param $from - optional start of contract date
  37. * @param $till - optional end of contract date
  38. *
  39. * @return void
  40. */
  41. protected function create($contract, $date, $from = '', $till = '') {
  42. $contract = ubRouting::filters($contract, 'mres');
  43. $date = ubRouting::filters($date, 'mres');
  44. $from = ubRouting::filters($from, 'mres');
  45. $till = ubRouting::filters($till, 'mres');
  46. //not empty contract and valid date?
  47. if (!empty($contract) AND zb_checkDate($date)) {
  48. $this->contractDatesDb->data('contract', $contract);
  49. $this->contractDatesDb->data('date', $date);
  50. $this->contractDatesDb->data('from', $from);
  51. $this->contractDatesDb->data('till', $till);
  52. $this->contractDatesDb->create();
  53. log_register('CREATE UserContractDate [' . $contract . '] D`' . $date . '` F`' . $from . '` T`' . $till . '`');
  54. }
  55. }
  56. /**
  57. * Updates some existing contract date database record
  58. *
  59. * @param $contract - existing contract num
  60. * @param $date - contract creation date
  61. * @param $from - optional start of contract date
  62. * @param $till - optional end of contract date
  63. *
  64. * @return void
  65. */
  66. protected function update($contract, $date, $from = '', $till = '') {
  67. $contract = ubRouting::filters($contract, 'mres');
  68. $date = ubRouting::filters($date, 'mres');
  69. $from = ubRouting::filters($from, 'mres');
  70. $till = ubRouting::filters($till, 'mres');
  71. //not empty contract and valid date?
  72. if (!empty($contract) AND zb_checkDate($date)) {
  73. $this->contractDatesDb->data('date', $date);
  74. $this->contractDatesDb->data('from', $from);
  75. $this->contractDatesDb->data('till', $till);
  76. $this->contractDatesDb->where('contract', '=', $contract);
  77. $this->contractDatesDb->save();
  78. log_register('CHANGE UserContractDate [' . $contract . '] D`' . $date . '` F`' . $from . '` T`' . $till . '`');
  79. }
  80. }
  81. /**
  82. * Sets some contract date in database
  83. *
  84. * @param $contract - existing contract num
  85. * @param $date - contract creation date
  86. * @param $from - optional start of contract date
  87. * @param $till - optional end of contract date
  88. *
  89. * @return void
  90. */
  91. public function set($contract, $date, $from = '', $till = '') {
  92. if (!empty($contract)) {
  93. $currentDatabaseRecord = $this->getAllDatesFull($contract);
  94. //create new
  95. if (empty($currentDatabaseRecord)) {
  96. $this->create($contract, $date, $from, $till);
  97. } else {
  98. //or update existing?
  99. $this->update($contract, $date, $from, $till);
  100. }
  101. }
  102. }
  103. /**
  104. * Get all or selected existing contract basic dates as contract=>date
  105. *
  106. * @return array
  107. */
  108. public function getAllDatesBasic($contract = '') {
  109. $contract = ubRouting::filters($contract, 'mres');
  110. $result = array();
  111. if (!empty($contract)) {
  112. $this->contractDatesDb->where('contract', '=', $contract);
  113. }
  114. $all = $this->contractDatesDb->getAll();
  115. if (!empty($all)) {
  116. foreach ($all as $io => $each) {
  117. $result[$each['contract']] = $each['date'];
  118. }
  119. }
  120. return ($result);
  121. }
  122. /**
  123. * Get all or selected existing contract basic dates as contract=>datesData[id,date,from,till]
  124. *
  125. * @return array
  126. */
  127. public function getAllDatesFull($contract = '') {
  128. $contract = ubRouting::filters($contract, 'mres');
  129. $result = array();
  130. if (!empty($contract)) {
  131. $this->contractDatesDb->where('contract', '=', $contract);
  132. }
  133. $result = $this->contractDatesDb->getAll('contract');
  134. return ($result);
  135. }
  136. /**
  137. * Shows contract create date modify form
  138. *
  139. * @param string $contract
  140. *
  141. * @return string
  142. */
  143. public function renderChangeForm($contract = '') {
  144. $result = '';
  145. $curDate = '';
  146. $curFrom = '';
  147. $curTill = '';
  148. if (!empty($contract)) {
  149. $currentData = $this->getAllDatesFull($contract);
  150. if (!empty($currentData)) {
  151. $curDate = $currentData[$contract]['date'];
  152. $curFrom = $currentData[$contract]['from'];
  153. $curTill = $currentData[$contract]['till'];
  154. }
  155. }
  156. $curDateLabel = ($curDate) ? $curDate : __('None');
  157. $curFromLabel = ($curFrom) ? $curFrom : __('None');
  158. $curTillLabel = ($curTill) ? $curTill : __('None');
  159. $rows = wf_HiddenInput(self::PROUTE_RUNEDIT, $contract);
  160. $cells = wf_TableCell($curDateLabel, '30%', 'row2');
  161. $cells .= wf_TableCell(wf_DatePickerPreset(self::PROUTE_DATE, $curDate) . ' ' . __('User contract date'), '', 'row3');
  162. $rows .= wf_tablerow($cells);
  163. $cells = wf_TableCell($curFromLabel, '', 'row2');
  164. $cells .= wf_TableCell(wf_DatePickerPreset(self::PROUTE_FROM, $curFrom) . ' ' . __('Start date'), '', 'row3');
  165. $rows .= wf_tablerow($cells);
  166. $cells = wf_TableCell($curTillLabel, '', 'row2');
  167. $cells .= wf_TableCell(wf_DatePickerPreset(self::PROUTE_TILL, $curTill) . ' ' . __('End date'), '', 'row3');
  168. $rows .= wf_tablerow($cells);
  169. $form = wf_TableBody($rows, '100%', 0);
  170. $form .= wf_Submit('Save');
  171. $result .= wf_Form("", 'POST', $form, '');
  172. return ($result);
  173. }
  174. }