api.buildpassport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Extended Build information base class
  4. */
  5. class BuildPassport {
  6. /**
  7. * Contains all builds passpord data as buildid=>data
  8. *
  9. * @var array
  10. */
  11. protected $allPassportData = array();
  12. /**
  13. * Predefined owner types array
  14. *
  15. * @var array
  16. */
  17. protected $ownersArr = array('' => '-');
  18. /**
  19. * Predefined floors counts
  20. *
  21. * @var array
  22. */
  23. protected $floorsArr = array('' => '-');
  24. /**
  25. * Contains predefined entrances counts
  26. *
  27. * @var array
  28. */
  29. protected $entrancesArr = array('' => '-');
  30. /**
  31. * Database abstraction layer placeholder
  32. *
  33. * @var object
  34. */
  35. protected $passportsDb = '';
  36. /**
  37. * Some static defines, routes etc here
  38. */
  39. const URL_PASSPORT = '?module=buildpassport';
  40. const ROUTE_BUILD = 'buildid';
  41. const DATA_SOURCE = 'buildpassport';
  42. const EX_NO_OWNERS = 'EMPTY_OWNERS_PARAM';
  43. const EX_NO_OPTS = 'NOT_ENOUGHT_OPTIONS';
  44. public function __construct() {
  45. $this->initDb();
  46. $this->loadData();
  47. $this->savePassport();
  48. $this->loadConfig();
  49. }
  50. /**
  51. * Inits passports database abstraction layer
  52. */
  53. protected function initDb() {
  54. $this->passportsDb = new NyanORM(self::DATA_SOURCE);
  55. }
  56. /**
  57. * loads all existing builds passport data into protected prop
  58. *
  59. * @return void
  60. */
  61. protected function loadData() {
  62. $this->allPassportData = $this->passportsDb->getAll('buildid');
  63. }
  64. /**
  65. * load build passport data options
  66. *
  67. * @return void
  68. */
  69. protected function loadConfig() {
  70. global $ubillingConfig;
  71. $altCfg = $ubillingConfig->getAlter();
  72. //extracting owners
  73. if (!empty($altCfg['BUILD_OWNERS'])) {
  74. $rawOwners = explode(',', $altCfg['BUILD_OWNERS']);
  75. foreach ($rawOwners as $ia => $eachowner) {
  76. $this->ownersArr[$eachowner] = $eachowner;
  77. }
  78. } else {
  79. throw new Exception(self::EX_NO_OWNERS);
  80. }
  81. //extracting floors and entrances
  82. if (!empty($altCfg['BUILD_EXTOPTS'])) {
  83. $rawOpts = explode(',', $altCfg['BUILD_EXTOPTS']);
  84. if (sizeof($rawOpts) < 3) {
  85. $maxFloors = $rawOpts[0];
  86. $maxEntrances = $rawOpts[1];
  87. for ($floors = 1; $floors <= $maxFloors; $floors++) {
  88. $this->floorsArr[$floors] = $floors;
  89. }
  90. for ($entrances = 1; $entrances <= $maxEntrances; $entrances++) {
  91. $this->entrancesArr[$entrances] = $entrances;
  92. }
  93. } else {
  94. throw new Exception(self::EX_NO_OPTS);
  95. }
  96. } else {
  97. throw new Exception(self::EX_NO_OPTS);
  98. }
  99. }
  100. /**
  101. * returns some build passport edit form
  102. *
  103. * @praram $buildid existing build id
  104. *
  105. * @return string
  106. */
  107. public function renderEditForm($buildid) {
  108. $buildid = ubRouting::filters($buildid, 'int');
  109. if (isset($this->allPassportData[$buildid])) {
  110. $currentData = $this->allPassportData[$buildid];
  111. } else {
  112. $currentData = array();
  113. }
  114. $inputs = wf_HiddenInput('savebuildpassport', $buildid);
  115. $inputs .= wf_Selector('powner', $this->ownersArr, __('Owner'), @$currentData['owner'], true);
  116. $inputs .= wf_TextInput('pownername', __('Owner name'), @$currentData['ownername'], true, 30);
  117. $inputs .= wf_TextInput('pownerphone', __('Owner phone'), @$currentData['ownerphone'], true, 30, 'mobile');
  118. $inputs .= wf_TextInput('pownercontact', __('Owner contact person'), @$currentData['ownercontact'], true, 30);
  119. $keys = (@$currentData['keys'] == 1) ? true : false;
  120. $inputs .= wf_CheckInput('pkeys', __('Keys available'), true, $keys);
  121. $inputs .= wf_TextInput('paccessnotices', __('Build access notices'), @$currentData['accessnotices'], true, 40);
  122. $inputs .= wf_CheckInput('panthill', __('Apartment house'), true, @$currentData['anthill']) . ' ';
  123. $inputs .= wf_Selector('pfloors', $this->floorsArr, __('Floors'), @$currentData['floors'], false);
  124. $inputs .= wf_Selector('pentrances', $this->entrancesArr, __('Entrances'), @$currentData['entrances'], false);
  125. $inputs .= wf_TextInput('papts', __('Apartments'), @$currentData['apts'], true, 5);
  126. $inputs .= __('Notes') . wf_tag('br');
  127. $inputs .= wf_TextArea('pnotes', '', @$currentData['notes'], true, '50x6');
  128. $inputs .= wf_CheckInput('pcontract', __('Contract signed'), false, @$currentData['contract']) . ' ';
  129. $inputs .= wf_CheckInput('pmediator', __('Signed through an intermediary'), true, @$currentData['mediator']) . ' ';
  130. $inputs .= wf_delimiter(0);
  131. $inputs .= wf_Submit(__('Save'));
  132. $result = wf_Form('', 'POST', $inputs, 'glamour');
  133. return ($result);
  134. }
  135. /**
  136. * Returns some build passport data preview
  137. *
  138. * @param int $buildid existing build id
  139. * @param string $buildAddress optional address string
  140. *
  141. * @return string
  142. */
  143. public function renderPassportData($buildid, $buildAddress = '') {
  144. $result = '';
  145. $buildid = ubRouting::filters($buildid, 'int');
  146. $rows = '';
  147. if (!empty($buildAddress)) {
  148. $cells = wf_TableCell(__('Address'), '30%', 'row2');
  149. $cells .= wf_TableCell($buildAddress);
  150. $rows .= wf_TableRow($cells, 'row3');
  151. }
  152. if (isset($this->allPassportData[$buildid])) {
  153. $currentData = $this->allPassportData[$buildid];
  154. $cells = wf_TableCell(__('Owner'), '30%', 'row2');
  155. $cells .= wf_TableCell($currentData['owner']);
  156. $rows .= wf_TableRow($cells, 'row3');
  157. $cells = wf_TableCell(__('Owner name'), '', 'row2');
  158. $cells .= wf_TableCell($currentData['ownername']);
  159. $rows .= wf_TableRow($cells, 'row3');
  160. $cells = wf_TableCell(__('Owner phone'), '', 'row2');
  161. $cells .= wf_TableCell($currentData['ownerphone']);
  162. $rows .= wf_TableRow($cells, 'row3');
  163. $cells = wf_TableCell(__('Owner contact person'), '', 'row2');
  164. $cells .= wf_TableCell($currentData['ownercontact']);
  165. $rows .= wf_TableRow($cells, 'row3');
  166. $cells = wf_TableCell(__('Keys available'), '', 'row2');
  167. $keysLabel = ($currentData['keys']) ? wf_img_sized('skins/icon_key.gif', __('Keys available'), 12) . ' ' . __('Yes') : __('No');
  168. $cells .= wf_TableCell($keysLabel);
  169. $rows .= wf_TableRow($cells, 'row3');
  170. $cells = wf_TableCell(__('Build access notices'), '', 'row2');
  171. $cells .= wf_TableCell($currentData['accessnotices']);
  172. $rows .= wf_TableRow($cells, 'row3');
  173. $antHillLabel = ($currentData['anthill']) ? wf_img_sized('skins/ymaps/build.png', '', 12) . ' ' . __('Yes') : wf_img_sized('skins/ymaps/home.png', '', 12) . ' ' . __('No');
  174. $cells = wf_TableCell(__('Apartment house'), '', 'row2');
  175. $cells .= wf_TableCell($antHillLabel);
  176. $rows .= wf_TableRow($cells, 'row3');
  177. $cells = wf_TableCell(__('Floors'), '', 'row2');
  178. $cells .= wf_TableCell($currentData['floors']);
  179. $rows .= wf_TableRow($cells, 'row3');
  180. $cells = wf_TableCell(__('Entrances'), '', 'row2');
  181. $cells .= wf_TableCell($currentData['entrances']);
  182. $rows .= wf_TableRow($cells, 'row3');
  183. $cells = wf_TableCell(__('Apartments'), '', 'row2');
  184. $cells .= wf_TableCell($currentData['apts']);
  185. $rows .= wf_TableRow($cells, 'row3');
  186. $cells = wf_TableCell(__('Notes'), '', 'row2');
  187. $cells .= wf_TableCell($currentData['notes']);
  188. $rows .= wf_TableRow($cells, 'row3');
  189. $contractSignLabel = ($currentData['contract']) ? __('Yes') : __('No');
  190. $cells = wf_TableCell(__('Contract signed'), '', 'row2');
  191. $cells .= wf_TableCell($contractSignLabel);
  192. $rows .= wf_TableRow($cells, 'row3');
  193. $mediatorLabel = ($currentData['mediator']) ? __('Yes') : __('No');
  194. $cells = wf_TableCell(__('Signed through an intermediary'), '', 'row2');
  195. $cells .= wf_TableCell($mediatorLabel);
  196. $rows .= wf_TableRow($cells, 'row3');
  197. $result = wf_TableBody($rows, '100%', 0);
  198. }
  199. return ($result);
  200. }
  201. /**
  202. * saves new passport data for some build
  203. *
  204. * @return void
  205. */
  206. protected function savePassport() {
  207. if (ubRouting::checkPost('savebuildpassport')) {
  208. $buildid = ubRouting::post('savebuildpassport', 'int');
  209. $owner = ubRouting::post('powner', 'mres');
  210. $ownername = ubRouting::post('pownername', 'mres');
  211. $ownerphone = ubRouting::post('pownerphone', 'mres');
  212. $ownercontact = ubRouting::post('pownercontact', 'mres');
  213. $keys = (ubRouting::checkPost('pkeys')) ? 1 : 0;
  214. $accessnotices = ubRouting::post('paccessnotices', 'mres');
  215. $floors = ubRouting::post('pfloors', 'mres');
  216. $entrances = ubRouting::post('pentrances', 'mres');
  217. $apts = ubRouting::post('papts', 'mres');
  218. $notes = ubRouting::post('pnotes', 'mres');
  219. $contract = (ubRouting::checkPost('pcontract')) ? 1 : 0;
  220. $mediator = (ubRouting::checkPost('pmediator')) ? 1 : 0;
  221. $anthill = (ubRouting::checkPost('panthill')) ? 1 : 0;
  222. //filling new data
  223. $this->passportsDb->data('owner', $owner);
  224. $this->passportsDb->data('ownername', $ownername);
  225. $this->passportsDb->data('ownerphone', $ownerphone);
  226. $this->passportsDb->data('ownercontact', $ownercontact);
  227. $this->passportsDb->data('keys', $keys);
  228. $this->passportsDb->data('accessnotices', $accessnotices);
  229. $this->passportsDb->data('floors', $floors);
  230. $this->passportsDb->data('apts', $apts);
  231. $this->passportsDb->data('entrances', $entrances);
  232. $this->passportsDb->data('notes', $notes);
  233. $this->passportsDb->data('contract', $contract);
  234. $this->passportsDb->data('mediator', $mediator);
  235. $this->passportsDb->data('anthill', $anthill);
  236. if (isset($this->allPassportData[$buildid])) {
  237. //updating existing record
  238. $this->passportsDb->where('buildid', '=', $buildid);
  239. $this->passportsDb->save();
  240. log_register('BUILD PASSPORT SAVE [' . $buildid . ']');
  241. } else {
  242. //new record
  243. $this->passportsDb->data('buildid', $buildid);
  244. $this->passportsDb->create();
  245. log_register('BUILD PASSPORT CREATE [' . $buildid . ']');
  246. }
  247. //reload actual data after saving changes
  248. $this->loadData();
  249. }
  250. }
  251. /**
  252. * Returns build passport data if it exists
  253. *
  254. * @param int $buildId
  255. *
  256. * @return array
  257. */
  258. public function getPassportData($buildId) {
  259. $result = array();
  260. if (isset($this->allPassportData[$buildId])) {
  261. $result = $this->allPassportData[$buildId];
  262. }
  263. return($result);
  264. }
  265. }