api.oefails.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Electrical failures reporting class
  4. */
  5. class OeFails {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * System caching abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $cache = '';
  18. /**
  19. * Default caching timeout
  20. *
  21. * @var int
  22. */
  23. protected $cacheTimeout = 600;
  24. /**
  25. * Contains default data source URL or file path
  26. *
  27. * @var string
  28. */
  29. protected $dataSource = '';
  30. /**
  31. * Contains raw data received from data source
  32. *
  33. * @var string
  34. */
  35. protected $rawData = '';
  36. /**
  37. * Contains basically preprocessed data
  38. *
  39. * @var array
  40. */
  41. protected $parsedData = array();
  42. /**
  43. * Contains default city filters for extraction
  44. *
  45. * @var string
  46. */
  47. protected $cityFilter = '';
  48. /**
  49. * Contains scheduled power outages string mask
  50. *
  51. * @var string
  52. */
  53. protected $scheduledMask = '';
  54. /**
  55. * Contains emergency power outages string mask
  56. *
  57. * @var string
  58. */
  59. protected $emergencyMask = '';
  60. /**
  61. * Contains default cache key name
  62. */
  63. const CACHE_KEY = 'OEFAILS';
  64. /**
  65. * Contains basic module controller URL
  66. */
  67. const URL_ME = '?module=oefails';
  68. /**
  69. * Options string parsing offsets
  70. */
  71. const OFFSET_SOURCE = 0;
  72. const OFFSET_FILTER = 1;
  73. const OFFSET_EMERG = 2;
  74. const OFFSET_SCHED = 3;
  75. /**
  76. * Creates new fails instance
  77. *
  78. * @param string $dataSource
  79. */
  80. public function __construct($dataSource = '') {
  81. $this->loadAlter();
  82. $this->setOptions();
  83. $this->initCache();
  84. $this->setDataSource($dataSource);
  85. }
  86. /**
  87. * Loads alter config file into protected prop
  88. *
  89. * @global object $ubillingConfig
  90. *
  91. * @return void
  92. */
  93. protected function loadAlter() {
  94. global $ubillingConfig;
  95. $this->altCfg = $ubillingConfig->getAlter();
  96. }
  97. /**
  98. * Sets config based options for current instance
  99. *
  100. * @return void
  101. */
  102. protected function setOptions() {
  103. if (isset($this->altCfg['OEFAILS_OPTIONS'])) {
  104. $optionsString = @$this->altCfg['OEFAILS_OPTIONS'];
  105. $options = explode('|', $optionsString);
  106. if (!empty($options)) {
  107. $this->setDataSource($options[self::OFFSET_SOURCE]);
  108. $this->setFilter($options[self::OFFSET_FILTER]);
  109. if (isset($options[self::OFFSET_EMERG])) {
  110. $this->scheduledMask = $options[self::OFFSET_SCHED];
  111. $this->emergencyMask = $options[self::OFFSET_EMERG];
  112. }
  113. } else {
  114. throw new Exception('EX_EMPTY_OPTIONS');
  115. }
  116. } else {
  117. throw new Exception('EX_NO_OPTIONS');
  118. }
  119. }
  120. /**
  121. * Inits caching object for further usage
  122. */
  123. protected function initCache() {
  124. $this->cache = new UbillingCache();
  125. }
  126. /**
  127. * Sets data source file path/URL into obj prop
  128. *
  129. * @param string $dataSource
  130. *
  131. * @return void
  132. */
  133. protected function setDataSource($dataSource = '') {
  134. if (!empty($dataSource)) {
  135. $this->dataSource = $dataSource;
  136. }
  137. }
  138. /**
  139. * Sets city filter for data extraction
  140. *
  141. * @param string $mask
  142. *
  143. * @return void
  144. */
  145. protected function setFilter($mask = '') {
  146. if (!empty($mask)) {
  147. $this->cityFilter = $mask;
  148. }
  149. }
  150. /**
  151. * Gets raw CSV data from datasource and stores it in protected property
  152. *
  153. * @return void
  154. */
  155. protected function getRawData() {
  156. $this->rawData = $this->cache->get(self::CACHE_KEY, $this->cacheTimeout);
  157. if (empty($this->rawData)) {
  158. $remoteData = new OmaeUrl($this->dataSource);
  159. $remoteData->setTimeout(42);
  160. $this->rawData = $remoteData->response();
  161. $this->cache->set(self::CACHE_KEY, $this->rawData, $this->cacheTimeout);
  162. }
  163. }
  164. /**
  165. * Returns basically preprocessed and filtered data
  166. *
  167. * @return void
  168. */
  169. protected function parseData() {
  170. $this->getRawData();
  171. if (!empty($this->rawData)) {
  172. $rawTmp = explodeRows($this->rawData);
  173. $filteringRequired = (!empty($this->cityFilter)) ? true : false;
  174. if (!empty($rawTmp)) {
  175. foreach ($rawTmp as $io => $eachLine) {
  176. if ($filteringRequired) {
  177. if (ispos($eachLine, $this->cityFilter)) {
  178. $this->parsedData[] = str_getcsv($eachLine);
  179. }
  180. } else {
  181. $this->parsedData[] = $eachLine;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * Basic data preprocessing method. May be customizable in far far future.
  189. * Now its hardcodded for oe.if.ua data format
  190. *
  191. * @return void
  192. */
  193. public function ajGetData($dateFilter = '', $allTime = false) {
  194. $this->parseData();
  195. $json = new wf_JqDtHelper();
  196. $magicNumber = 3; //f**k that s**t
  197. $dateFilter = (!empty($dateFilter)) ? $dateFilter : curdate();
  198. if ($allTime) {
  199. $dateFilter = '';
  200. }
  201. if (!empty($this->parsedData)) {
  202. foreach ($this->parsedData as $io => $each) {
  203. $indexOffset = 0;
  204. $region = $each[0];
  205. $cityName = $each[1];
  206. $address = $each[2];
  207. $recordsCount = sizeof($each);
  208. if ($recordsCount >= $magicNumber) {
  209. foreach ($each as $index => $failRecord) {
  210. if ($indexOffset >= $magicNumber) {
  211. $data[] = $region;
  212. $data[] = $cityName;
  213. $data[] = $address;
  214. $failRecord = str_replace('{', '', $failRecord);
  215. $failRecord = str_replace('}', '', $failRecord);
  216. $failText = $failRecord;
  217. $typeIcon = '';
  218. if (!empty($this->emergencyMask) and ! empty($this->scheduledMask)) {
  219. $typeIcon = '';
  220. if (ispos($failText, $this->emergencyMask)) {
  221. $typeIcon = web_red_led() . ' ' . trim($this->emergencyMask, ',');
  222. $failText = str_replace($this->emergencyMask, '', $failText);
  223. }
  224. if (ispos($failText, $this->scheduledMask)) {
  225. $typeIcon = web_yellow_led() . ' ' . trim($this->scheduledMask, ',');
  226. $failText = str_replace($this->scheduledMask, '', $failText);
  227. }
  228. }
  229. $data[] = $typeIcon;
  230. $data[] = $failText;
  231. //some date filtering
  232. if (!empty($dateFilter)) {
  233. if (ispos($failText, $dateFilter)) {
  234. $json->addRow($data);
  235. }
  236. } else {
  237. //all time data
  238. $json->addRow($data);
  239. }
  240. unset($data);
  241. }
  242. $indexOffset++;
  243. }
  244. }
  245. }
  246. }
  247. $json->getJson();
  248. }
  249. /**
  250. * Renders power outages list container
  251. *
  252. * @return string
  253. */
  254. public function renderList() {
  255. $result = '';
  256. //some controls here
  257. $curdateFilter = (ubRouting::checkPost('datefilter')) ? ubRouting::post('datefilter') : curdate();
  258. $alltimeCall = (ubRouting::checkPost('alltime')) ? '&alltime=true' : '';
  259. $inputs = wf_DatePickerPreset('datefilter', $curdateFilter) . ' ';
  260. $inputs .= wf_CheckInput('alltime', __('All time'), false, ubRouting::checkPost('alltime')) . ' ';
  261. $inputs .= wf_Submit(__('Show'));
  262. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  263. $columns = array(__('District'), __('City'), __('Address'), __('Type'), __('Date') . '/' . __('Time'));
  264. $opts = '';
  265. $result .= wf_JqDtLoader($columns, self::URL_ME . '&ajaxlist=true&datefilter=' . $curdateFilter . $alltimeCall, false, __('Power outages'), 100, $opts);
  266. return($result);
  267. }
  268. }