api.capabdir.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. /**
  3. * Capabilities directory base class
  4. */
  5. class CapabilitiesDirectory {
  6. /**
  7. * Contains all available capabilities list as id=>data
  8. *
  9. * @var array
  10. */
  11. protected $allcapab = array();
  12. /**
  13. * Contains array of available capabilities states as id=>data
  14. *
  15. * @var array
  16. */
  17. protected $capabstates = array();
  18. /**
  19. * Contains array of all employee
  20. *
  21. * @var array
  22. */
  23. protected $employees = array();
  24. /**
  25. * Contains array of available capabs ids
  26. *
  27. * @var array
  28. */
  29. protected $availids = array();
  30. /**
  31. * Contains array of capabilities history
  32. *
  33. * @var array
  34. */
  35. protected $history = array();
  36. /**
  37. * System telepathy object placeholder
  38. *
  39. * @var object
  40. */
  41. protected $telepathy = '';
  42. const NO_ID = 'NO_SUCH_CAPABILITY_ID';
  43. const URL_CREATE = '?module=capabilities';
  44. const URL_ME = '?module=capabilities';
  45. const URL_CAPAB = '&edit=';
  46. /**
  47. * @param bool $noloaders Do not protess load subroutines at object creation
  48. *
  49. * @return void
  50. */
  51. public function __construct($noloaders = false) {
  52. if (!$noloaders) {
  53. //load existing capabilities
  54. $this->loadCapabilities();
  55. //load they ids
  56. $this->loadAllIds();
  57. //load existing states
  58. $this->loadCapabStates();
  59. //loads capabs history
  60. $this->loadHistory();
  61. //load employees
  62. $this->loadEmployees();
  63. //init telepathy
  64. $this->initTelepathy();
  65. }
  66. }
  67. /**
  68. * stores all available capab ids into protected prop - used in pagination
  69. *
  70. * @return void
  71. */
  72. protected function loadAllIds() {
  73. $query = "SELECT `id` from `capab`";
  74. $all = simple_queryall($query);
  75. if (!empty($all)) {
  76. foreach ($all as $io => $each) {
  77. $this->availids[$each['id']] = $each['id'];
  78. }
  79. }
  80. }
  81. /**
  82. * Loads capabs history into protected prop
  83. *
  84. * @return void
  85. */
  86. protected function loadHistory() {
  87. $query = "SELECT * from `capabhist` ORDER BY `id` ASC";
  88. $all = simple_queryall($query);
  89. if (!empty($all)) {
  90. foreach ($all as $io => $each) {
  91. $this->history[$each['capabid']][] = $each;
  92. }
  93. }
  94. }
  95. /**
  96. * Returns capab created admin login
  97. *
  98. * @param int $capabId
  99. *
  100. * @return void/string
  101. */
  102. protected function getHistoryCreated($capabId) {
  103. $result = '';
  104. if (!empty($this->history)) {
  105. if (isset($this->history[$capabId])) {
  106. $capabHist = $this->history[$capabId];
  107. if (!empty($capabHist)) {
  108. foreach ($capabHist as $io => $each) {
  109. if ($each['type'] == 'create') {
  110. $result = $each['admin'];
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return ($result);
  117. }
  118. /**
  119. * Inits system telepathy object
  120. *
  121. * @return void
  122. */
  123. protected function initTelepathy() {
  124. $this->telepathy = new Telepathy(false, true);
  125. }
  126. /**
  127. * loads all of available capabilities as protected prop allcapab
  128. *
  129. * @return void
  130. */
  131. protected function loadCapabilities() {
  132. $query = "SELECT * from `capab`;";
  133. $all = simple_queryall($query);
  134. if (!empty($all)) {
  135. foreach ($all as $io => $each) {
  136. $this->allcapab[$each['id']]['id'] = $each['id'];
  137. $this->allcapab[$each['id']]['date'] = $each['date'];
  138. $this->allcapab[$each['id']]['address'] = $each['address'];
  139. $this->allcapab[$each['id']]['phone'] = $each['phone'];
  140. $this->allcapab[$each['id']]['stateid'] = $each['stateid'];
  141. $this->allcapab[$each['id']]['notes'] = $each['notes'];
  142. $this->allcapab[$each['id']]['price'] = $each['price'];
  143. $this->allcapab[$each['id']]['employeeid'] = $each['employeeid'];
  144. $this->allcapab[$each['id']]['donedate'] = $each['donedate'];
  145. }
  146. }
  147. }
  148. /**
  149. * loads available capability states into protected prop capabstates
  150. *
  151. * @return void
  152. */
  153. protected function loadCapabStates() {
  154. $query = "SELECT * from `capabstates`";
  155. $all = simple_queryall($query);
  156. $this->capabstates[0]['id'] = 0;
  157. $this->capabstates[0]['state'] = __('Was not processed');
  158. $this->capabstates[0]['color'] = 'FF0000';
  159. if (!empty($all)) {
  160. foreach ($all as $io => $each) {
  161. $this->capabstates[$each['id']]['id'] = $each['id'];
  162. $this->capabstates[$each['id']]['state'] = $each['state'];
  163. $this->capabstates[$each['id']]['color'] = $each['color'];
  164. }
  165. }
  166. }
  167. /**
  168. * Loads all existing employees into protected employees prop
  169. *
  170. * @return void
  171. */
  172. protected function loadEmployees() {
  173. $query = "SELECT * from `employee`";
  174. $all = simple_queryall($query);
  175. if (!empty($all)) {
  176. foreach ($all as $io => $each) {
  177. $this->employees[$each['id']]['id'] = $each['id'];
  178. $this->employees[$each['id']]['name'] = $each['name'];
  179. $this->employees[$each['id']]['active'] = $each['active'];
  180. }
  181. }
  182. }
  183. /**
  184. * Renders base capabilities list
  185. *
  186. * @return type
  187. */
  188. public function render() {
  189. global $ubillingConfig;
  190. $dateSort = ($ubillingConfig->getAlterParam('CAPABDIR_DATE_SORT')) ? true : false;
  191. $result = '';
  192. $columns = array(__('Admin'), __('Date'), __('Address'), __('Phone'), __('Status'), __('Notes'), __('Price'), __('Employee'), __('Changed'), __('Actions'));
  193. $result = $this->panel();
  194. $opts = '"order": [[ 4, "asc" ]]';
  195. if ($dateSort) {
  196. $opts = '"order": [[ 1, "desc" ]]';
  197. }
  198. $result .= wf_JqDtLoader($columns, self::URL_ME . '&ajlist=true', false, __('Objects'), 100, $opts);
  199. return ($result);
  200. }
  201. /**
  202. * Returns all capabs states color styles
  203. *
  204. * @return string
  205. */
  206. protected function getColorStyles() {
  207. $result = wf_tag('style', false);
  208. if (!empty($this->capabstates)) {
  209. foreach ($this->capabstates as $io => $each) {
  210. $customColorStyleName = 'capabcolorcustom_' . $io;
  211. $result .= '.' . $customColorStyleName . ',
  212. .' . $customColorStyleName . ' div,
  213. .' . $customColorStyleName . ' span {
  214. background-color: #' . $each['color'] . ';
  215. border-color: #' . $each['color'] . ';
  216. color: #FFFFFF;
  217. }';
  218. }
  219. }
  220. $result .= wf_tag('style', true);
  221. return ($result);
  222. }
  223. /**
  224. * Renders capabs as calendar
  225. *
  226. * @return string
  227. */
  228. public function renderCalendar() {
  229. $result = '';
  230. $result .= $this->panel();
  231. $result .= $this->getColorStyles();
  232. $data = '';
  233. if (!empty($this->allcapab)) {
  234. foreach ($this->allcapab as $io => $each) {
  235. $stateName = @$this->capabstates[$each['stateid']]['state'];
  236. $employeeName = @$this->employees[$each['employeeid']]['name'];
  237. $coloring = "className : 'capabcolorcustom_" . $each['stateid'] . "',";
  238. $timestamp = strtotime($each['date']);
  239. $startDate = date("Y, n-1, j", $timestamp);
  240. $doneTimestamp = (!empty($each['donedate'])) ? strtotime($each['donedate']) : time();
  241. $doneDate = date("Y, n-1, j", $doneTimestamp);
  242. $daysSpent = zb_formatTime($doneTimestamp - $timestamp);
  243. $address = str_replace("'", '`', $each['address']);
  244. $data .= "
  245. {
  246. title: '" . $address . ' - ' . $stateName . ' (' . $daysSpent . ')' . "',
  247. start: new Date(" . $startDate . "),
  248. end: new Date(" . $doneDate . "),
  249. " . $coloring . "
  250. url: '" . self::URL_ME . "&edit=" . $each['id'] . "'
  251. },";
  252. }
  253. $data = zb_CutEnd($data);
  254. }
  255. $result .= wf_FullCalendar($data);
  256. return ($result);
  257. }
  258. /**
  259. * Renders capab json data
  260. *
  261. *
  262. * @rerturn string
  263. */
  264. public function ajCapabList() {
  265. $jsonAAData = array();
  266. if (!empty($this->allcapab)) {
  267. foreach ($this->allcapab as $io => $each) {
  268. $jsonItem = array();
  269. $stateName = @$this->capabstates[$each['stateid']]['state'];
  270. $stateColor = @$this->capabstates[$each['stateid']]['color'];
  271. $employeeName = @$this->employees[$each['employeeid']]['name'];
  272. $actions = '';
  273. if (cfr('ROOT')) {
  274. $actions .= wf_JSAlert(self::URL_ME . "&delete=" . $each['id'], web_delete_icon(), __('Removing this may lead to irreparable results')) . ' ';
  275. }
  276. $actions .= wf_link(self::URL_ME . "&edit=" . $each['id'], web_edit_icon(), false);
  277. $loginGuess = $this->telepathy->getLogin($each['address']);
  278. $profileLink = (!empty($loginGuess)) ? wf_Link('?module=userprofile&username=' . $loginGuess, web_profile_icon(), false, '') . ' (' . __('guessed') . ')' : '';
  279. $adminCreated = $this->getHistoryCreated($each['id']);
  280. $jsonItem[] = $adminCreated;
  281. $jsonItem[] = $each['date'];
  282. $jsonItem[] = $each['address'] . ' ' . $profileLink;
  283. $jsonItem[] = $each['phone'];
  284. $jsonItem[] = wf_tag('span', false, '', 'style = "display:none;"') . $each['stateid'] . wf_tag('span', true) . wf_tag('font', false, '', 'color = "#' . $stateColor . '"') . $stateName . wf_tag('font', true);
  285. $jsonItem[] = $each['notes'];
  286. $jsonItem[] = $each['price'];
  287. $jsonItem[] = $employeeName;
  288. $jsonItem[] = $each['donedate'];
  289. $jsonItem[] = $actions;
  290. $jsonAAData[] = $jsonItem;
  291. }
  292. }
  293. $result = array("aaData" => $jsonAAData);
  294. return (json_encode($result));
  295. return ($result);
  296. }
  297. /**
  298. * delete some capability from database
  299. *
  300. * @param $id - capability id
  301. *
  302. * @return void
  303. */
  304. public function deleteCapability($id) {
  305. $id = vf($id, 3);
  306. if (isset($this->availids[$id])) {
  307. $query = "DELETE from `capab` WHERE `id`='" . $id . "'";
  308. nr_query($query);
  309. log_register("CAPABILITY DELETE [" . $id . "]");
  310. $this->logCapability($id, 'delete');
  311. } else {
  312. throw new Exception(self::NO_ID);
  313. }
  314. }
  315. /**
  316. * Saves some capab actions into history
  317. *
  318. * @param int $capabId
  319. * @param string $type
  320. * @param string $event
  321. */
  322. protected function logCapability($capabId, $type, $event = '') {
  323. $capabId = vf($capabId, 3);
  324. $type = vf($type);
  325. $eventF = mysql_real_escape_string($event);
  326. $admin = whoami();
  327. $date = curdatetime();
  328. $query = "INSERT INTO `capabhist` (`id`,`capabid`,`admin`,`date`,`type`,`event`) VALUES "
  329. . "(NULL, '" . $capabId . "','" . $admin . "','" . $date . "','" . $type . "','" . $eventF . "');";
  330. nr_query($query);
  331. }
  332. /**
  333. * creates new capability in database
  334. *
  335. * @param $address - users address
  336. * @param $phone - users phone
  337. * @param $notes - text notes to task
  338. *
  339. * @return integer
  340. */
  341. public function addCapability($address, $phone, $notes) {
  342. $date = curdatetime();
  343. $address = mysql_real_escape_string($address);
  344. $phone = mysql_real_escape_string($phone);
  345. $notes = mysql_real_escape_string($notes);
  346. $query = "INSERT INTO `capab` (`id` , `date` , `address` , `phone` ,`stateid` ,`notes` ,`price` ,`employeeid` ,`donedate`)
  347. VALUES ( NULL , '" . $date . "', '" . $address . "', '" . $phone . "', '0', '" . $notes . "', NULL , NULL , NULL);";
  348. nr_query($query);
  349. $lastId = simple_get_lastid('capab');
  350. log_register("CAPABILITY ADD [" . $lastId . "] `" . $address . "`");
  351. $this->logCapability($lastId, 'create');
  352. }
  353. /**
  354. * Generates random HTML color
  355. *
  356. * @return string
  357. */
  358. protected function genRandomColor() {
  359. $result = strtoupper(dechex(rand(0, 10000000)));
  360. return ($result);
  361. }
  362. /**
  363. * returns capability creation form
  364. *
  365. * @param string $address Pre set address fo form
  366. * @param string $phone Pre set phone
  367. * @param string $notes Pre set notes
  368. *
  369. * @return string
  370. */
  371. public function createForm($address = '', $phone = '', $notes = '') {
  372. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  373. $allAddress = zb_AddressGetFulladdresslistCached();
  374. natsort($allAddress);
  375. $inputs = wf_AutocompleteTextInput('newaddress', $allAddress, __('Address') . $sup, $address, false);
  376. $inputs .= wf_TextInput('newphone', __('Phone') . $sup, $phone, true);
  377. $inputs .= __('Notes') . wf_tag('br');
  378. $inputs .= wf_TextArea('newnotes', '', $notes, true, '40x5');
  379. $inputs .= wf_Submit(__('Create'));
  380. $result = wf_Form(self::URL_CREATE, 'POST', $inputs, 'glamour');
  381. return ($result);
  382. }
  383. /**
  384. * returns capability editing form by existing cap id
  385. *
  386. * @return string
  387. */
  388. public function editForm($id) {
  389. $id = vf($id, 3);
  390. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  391. $result = '';
  392. $messages = new UbillingMessageHelper();
  393. $stateSelector = array();
  394. $employeeSelector = array();
  395. $employeeSelector['NULL'] = '-';
  396. $employeeLogins = ts_GetAllEmployeeLoginsCached();
  397. $employeeLogins = unserialize($employeeLogins);
  398. if (isset($this->availids[$id])) {
  399. //states preprocessing
  400. if (!empty($this->capabstates)) {
  401. foreach ($this->capabstates as $io => $eachcap) {
  402. $stateSelector[$eachcap['id']] = $eachcap['state'];
  403. }
  404. }
  405. //employee preprocessing
  406. if (!empty($this->employees)) {
  407. foreach ($this->employees as $ia => $eachemp) {
  408. if ($eachemp['active']) {
  409. $employeeSelector[$eachemp['id']] = $eachemp['name'];
  410. }
  411. }
  412. }
  413. //task creation form
  414. $customData = wf_HiddenInput('unifiedformcapabdirgobackid', $id);
  415. $customData .= wf_CheckInput('unifiedformcapabdirgobackflag', __('Back to capability after creation'), true, true);
  416. $taskForm = ts_TaskCreateFormUnified($this->allcapab[$id]['address'], $this->allcapab[$id]['phone'], '', '', $customData, $this->allcapab[$id]['notes']);
  417. $taskControl = wf_modalAuto(wf_img('skins/createtask.gif') . ' ' . __('Create task'), __('Create task'), $taskForm, 'ubButton', '420', '500');
  418. $result = wf_BackLink(self::URL_ME) . ' ';
  419. $result .= $taskControl . wf_delimiter();
  420. $inputs = wf_TextInput('editaddress', __('Full address') . $sup, $this->allcapab[$id]['address'], true);
  421. $inputs .= wf_TextInput('editphone', __('Phone') . $sup, $this->allcapab[$id]['phone'], true);
  422. $inputs .= __('Notes') . wf_tag('br');
  423. $inputs .= wf_TextArea('editnotes', '', $this->allcapab[$id]['notes'], true, '40x5');
  424. $inputs .= wf_TextInput('editprice', __('Price'), $this->allcapab[$id]['price'], true);
  425. $inputs .= wf_Selector('editstateid', $stateSelector, __('Status'), $this->allcapab[$id]['stateid'], true);
  426. $inputs .= wf_Selector('editemployeeid', $employeeSelector, __('Worker'), $this->allcapab[$id]['employeeid'], true);
  427. $inputs .= wf_delimiter();
  428. $inputs .= wf_Submit(__('Save'));
  429. $form = wf_Form("", 'POST', $inputs, 'glamour');
  430. $form .= wf_CleanDiv();
  431. $capabHist = '';
  432. if (isset($this->history[$id])) {
  433. if (!empty($this->history[$id])) {
  434. foreach ($this->history[$id] as $io => $each) {
  435. $employeeName = (isset($employeeLogins[$each['admin']])) ? $employeeLogins[$each['admin']] : $each['admin'];
  436. $label = '';
  437. switch ($each['type']) {
  438. case 'create':
  439. $style = 'success';
  440. $label = __('Created');
  441. break;
  442. case 'edit':
  443. $style = 'warning';
  444. $label = __('Changed');
  445. break;
  446. case 'delete':
  447. $style = 'error';
  448. $label = __('Deleted');
  449. break;
  450. default:
  451. $style = 'info';
  452. break;
  453. }
  454. $capabHist .= $messages->getStyledMessage($label . ' ' . $each['date'] . ' ' . $employeeName, $style);
  455. }
  456. }
  457. }
  458. $cells = wf_TableCell($form, '', '', 'valign="top"');
  459. $cells .= wf_TableCell($capabHist, '', '', 'valign="top"');
  460. $rows = wf_TableRow($cells);
  461. $result .= wf_TableBody($rows, '100%', 0);
  462. } else {
  463. throw new Exception(self::NO_ID);
  464. }
  465. return ($result);
  466. }
  467. /**
  468. * update capability into database by its id
  469. *
  470. * @param int $id
  471. * @param int $address
  472. * @param string $phone
  473. * @param int $stateid
  474. * @param string $notes
  475. * @param string $price
  476. * @param int $employeeid
  477. * @throws Exception
  478. *
  479. * @return void
  480. */
  481. public function editCapability($id, $address, $phone, $stateid, $notes, $price, $employeeid) {
  482. $id = vf($id, 3);
  483. $address = mysql_real_escape_string($address);
  484. $phone = mysql_real_escape_string($phone);
  485. $stateid = vf($stateid, 3);
  486. $price = mysql_real_escape_string($price);
  487. $employeeid = vf($employeeid, 3);
  488. $curdate = curdatetime();
  489. if (isset($this->availids[$id])) {
  490. simple_update_field('capab', 'donedate', $curdate, "WHERE `id`='" . $id . "';");
  491. simple_update_field('capab', 'address', $address, "WHERE `id`='" . $id . "';");
  492. simple_update_field('capab', 'phone', $phone, "WHERE `id`='" . $id . "';");
  493. simple_update_field('capab', 'stateid', $stateid, "WHERE `id`='" . $id . "';");
  494. simple_update_field('capab', 'notes', $notes, "WHERE `id`='" . $id . "';");
  495. simple_update_field('capab', 'price', $price, "WHERE `id`='" . $id . "';");
  496. simple_update_field('capab', 'employeeid', $employeeid, "WHERE `id`='" . $id . "';");
  497. log_register("CAPABILITY EDIT [" . $id . "] `" . $address . "`");
  498. $this->logCapability($id, 'edit');
  499. } else {
  500. throw new Exception(self::NO_ID);
  501. }
  502. }
  503. /**
  504. * shows currently available capability states in table grid
  505. *
  506. * @return string
  507. */
  508. public function statesList() {
  509. $cells = wf_TableCell(__('ID'));
  510. $cells .= wf_TableCell(__('Status'));
  511. $cells .= wf_TableCell(__('Color'));
  512. $cells .= wf_TableCell(__('Actions'));
  513. $rows = wf_TableRow($cells, 'row1');
  514. if (!empty($this->capabstates)) {
  515. foreach ($this->capabstates as $io => $each) {
  516. $cells = wf_TableCell($each['id']);
  517. $cells .= wf_TableCell($each['state']);
  518. $color = wf_tag('font', false, '', 'color = "#' . $each['color'] . '"') . $each['color'] . wf_tag('font', true);
  519. $cells .= wf_TableCell($color);
  520. if ($each['id'] != 0) {
  521. $actions = wf_JSAlert(self::URL_ME . "&states=true&deletestate=" . $each['id'], web_delete_icon(), __('Removing this may lead to irreparable results'));
  522. $actions .= wf_JSAlert(self::URL_ME . "&states=true&editstate=" . $each['id'], web_edit_icon(), __('Are you serious'));
  523. } else {
  524. $actions = '';
  525. }
  526. $cells .= wf_TableCell($actions);
  527. $rows .= wf_TableRow($cells, 'row3');
  528. }
  529. }
  530. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  531. return ($result);
  532. }
  533. /**
  534. * returns capability states adding form
  535. *
  536. * @return string
  537. */
  538. public function statesAddForm() {
  539. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  540. $result = wf_BackLink(self::URL_ME, '', true);
  541. $inputs = wf_TextInput('createstate', __('New status') . $sup, '', true, '20');
  542. $inputs .= wf_ColPicker('createstatecolor', __('New status color') . $sup, '#' . $this->genRandomColor(), true, '10');
  543. $inputs .= wf_Submit(__('Create'));
  544. $result .= wf_Form("", 'POST', $inputs, 'glamour');
  545. return ($result);
  546. }
  547. /**
  548. * returns capability states adding form
  549. *
  550. * @param int $id
  551. * @return string
  552. */
  553. public function statesEditForm($id) {
  554. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  555. $result = wf_BackLink(self::URL_ME . '&states=true', '', true);
  556. $inputs = wf_TextInput('editstate', __('New status') . $sup, $this->capabstates[$id]['state'], true, '20');
  557. $inputs .= wf_ColPicker('editstatecolor', __('New status color') . $sup, '#' . $this->capabstates[$id]['color'], true, '10');
  558. $inputs .= wf_Submit(__('Save'));
  559. $result .= wf_Form("", 'POST', $inputs, 'glamour');
  560. return ($result);
  561. }
  562. /**
  563. * creates new capability state
  564. *
  565. * @param string $state new state label
  566. * @param string $color new state color
  567. *
  568. * @return void
  569. */
  570. public function statesCreate($state, $color) {
  571. $state = mysql_real_escape_string($state);
  572. $color = mysql_real_escape_string($color);
  573. $color = str_replace('#', '', $color);
  574. $query = "INSERT INTO `capabstates` (`id` , `state` , `color`)
  575. VALUES ( NULL , '" . $state . "', '" . $color . "');";
  576. nr_query($query);
  577. log_register("CAPABILITY STATE ADD `" . $state . "`");
  578. }
  579. /**
  580. * delete state by its id
  581. *
  582. * @param int $id - state id in database
  583. *
  584. * @return void
  585. */
  586. public function statesDelete($id) {
  587. $id = vf($id, 3);
  588. if (!empty($id)) {
  589. $query = "DELETE FROM `capabstates` WHERE `id`='" . $id . "'";
  590. nr_query($query);
  591. log_register("CAPABILITY STATE DELETE [" . $id . "]");
  592. }
  593. }
  594. /**
  595. * updates state into database
  596. *
  597. * @param int $id - existing state id
  598. * @param int $state - new state title
  599. * @param string $color - new state color
  600. *
  601. * @return void
  602. */
  603. public function statesChange($id, $state, $color) {
  604. $id = vf($id, 3);
  605. $state = mysql_real_escape_string($state);
  606. $color = mysql_real_escape_string($color);
  607. $color = str_replace('#', '', $color);
  608. if (!empty($id)) {
  609. simple_update_field('capabstates', 'state', $state, "WHERE `id`='" . $id . "'");
  610. simple_update_field('capabstates', 'color', $color, "WHERE `id`='" . $id . "'");
  611. log_register("CAPABILITY STATE EDIT [" . $id . "] ON `" . $state . "`");
  612. }
  613. }
  614. /**
  615. * Renders capab sources stats
  616. *
  617. * @return string
  618. */
  619. public function renderSourcesStats() {
  620. $result = '';
  621. $capabSources = new Stigma('CAPABSOURCE');
  622. $result .= $capabSources->renderBasicReport();
  623. return ($result);
  624. }
  625. /**
  626. * Renders states charts
  627. *
  628. * @return string
  629. */
  630. public function renderStatesStats() {
  631. $result = '';
  632. $statsTmp = array();
  633. if (!empty($this->allcapab)) {
  634. $total = sizeof($this->allcapab);
  635. foreach ($this->allcapab as $io => $each) {
  636. if (isset($statsTmp[$each['stateid']])) {
  637. $statsTmp[$each['stateid']]++;
  638. } else {
  639. $statsTmp[$each['stateid']] = 1;
  640. }
  641. }
  642. if (!empty($statsTmp)) {
  643. $cells = wf_TableCell(__('Status'));
  644. $cells .= wf_TableCell(__('Count'));
  645. $cells .= wf_TableCell(__('Visual'));
  646. $rows = wf_TableRow($cells, 'row1');
  647. foreach ($statsTmp as $stateid => $count) {
  648. $cells = wf_TableCell(@$this->capabstates[$stateid]['state']);
  649. $cells .= wf_TableCell($count);
  650. $cells .= wf_TableCell(web_bar($count, $total));
  651. $rows .= wf_TableRow($cells, 'row3');
  652. }
  653. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  654. }
  655. } else {
  656. $messages = new UbillingMessageHelper();
  657. $result .= $messages->getStyledMessage(__('Nothing found'), 'info');
  658. }
  659. return ($result);
  660. }
  661. /**
  662. * returns capabilities directory control panel
  663. *
  664. * @return string
  665. */
  666. protected function panel() {
  667. $result = '';
  668. if (cfr('ROOT')) {
  669. $result .= wf_Link(self::URL_ME . "&states=true", wf_img('skins/settings.png', __('Modify states')), false, '') . '&nbsp;';
  670. }
  671. $result .= wf_modal(wf_img('skins/add_icon.png') . ' ' . __('Create'), __('Create'), $this->createForm(), 'ubButton', '400', '300');
  672. $result .= wf_Link(self::URL_ME . '&stats=true', wf_img('skins/icon_stats_16.gif') . ' ' . __('Stats'), false, 'ubButton');
  673. if (wf_CheckGet(array('calendar'))) {
  674. $result .= wf_Link(self::URL_ME, wf_img('skins/icon_table.png') . ' ' . __('Grid view'), false, 'ubButton');
  675. } else {
  676. $result .= wf_Link(self::URL_ME . '&calendar=true', wf_img('skins/icon_calendar.gif') . ' ' . __('As calendar'), false, 'ubButton');
  677. }
  678. $result .= wf_delimiter();
  679. return ($result);
  680. }
  681. }