api.taskstates.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Performs tasks processing states management in taskman
  4. */
  5. class TaskStates {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains available state types as type=>label
  14. *
  15. * @var array
  16. */
  17. protected $stateTypes = array();
  18. /**
  19. * Contains available state type icons as type=>iconpath
  20. *
  21. * @var array
  22. */
  23. protected $stateIcons = array();
  24. /**
  25. * Contains available princess list logins as login=>login
  26. *
  27. * @var array
  28. */
  29. protected $princessList = array();
  30. /**
  31. * Contains current administrator login
  32. *
  33. * @var string
  34. */
  35. protected $myLogin = '';
  36. /**
  37. * Database abstraction layer placeholder
  38. *
  39. * @var object
  40. */
  41. protected $statesDb = '';
  42. /**
  43. * Default states icon file extension
  44. */
  45. const ICON_EXT = '.png';
  46. /**
  47. * Default state icons path
  48. */
  49. const ICON_PATH = 'skins/';
  50. /**
  51. * Name of default state icon if normal isnt found on icons path
  52. */
  53. const ICON_DEFAULT = 'state_default';
  54. /**
  55. * Base callback URL for controller
  56. */
  57. const URL_BASE = '?module=taskman';
  58. /**
  59. * Contains all of task states as taskid=>statedata
  60. *
  61. * @var array
  62. */
  63. protected $allStates = array();
  64. /**
  65. * Creates new task states instance
  66. *
  67. * @param bool $loadDb Load states from DB
  68. *
  69. * @return void
  70. */
  71. public function __construct($loadStatesDb = true) {
  72. $this->setMyLogin();
  73. $this->loadAlter();
  74. $this->loadPrincessList();
  75. $this->setTypes();
  76. $this->setTypesIcons();
  77. if ($loadStatesDb) {
  78. $this->InitDatabase();
  79. $this->loadAllTasksStates();
  80. }
  81. }
  82. /**
  83. * Sets default task states types for further usage
  84. *
  85. * @return void
  86. */
  87. protected function setTypes() {
  88. $this->stateTypes['STATE_INPROGRESS'] = ' ' . __('Task is in progress');
  89. $this->stateTypes['STATE_DONE'] = ' ' . __('Done');
  90. $this->stateTypes['STATE_UNDONE'] = ' ' . __('Undone');
  91. $this->stateTypes['STATE_MOVED'] = ' ' . __('Moved');
  92. $this->stateTypes['STATE_CALLFAIL'] = ' '. __('Missed a phone call');
  93. $this->stateTypes['STATE_CANCELLED'] = ' ' . __('Canceled');
  94. $this->stateTypes['STATE_LATE'] = ' ' . __('Didnt have time');
  95. if (!empty($this->princessList)) {
  96. $this->stateTypes['STATE_PRINCESS'] = ' ' . __('Princess was here'); //protected state. May be modified only by princess.
  97. }
  98. }
  99. /**
  100. * Sets current administrator login
  101. *
  102. * @return void
  103. */
  104. protected function setMyLogin() {
  105. $this->myLogin = whoami();
  106. }
  107. /**
  108. * Checks is me an princess or not?
  109. *
  110. * @return bool
  111. */
  112. public function iAmPrincess() {
  113. $result = false;
  114. if (isset($this->princessList[$this->myLogin])) {
  115. $result = true;
  116. }
  117. return($result);
  118. }
  119. /**
  120. * Loads system alter config into protected prop
  121. *
  122. * @global object $ubillingConfig
  123. *
  124. * @return void
  125. */
  126. protected function loadAlter() {
  127. global $ubillingConfig;
  128. $this->altCfg = $ubillingConfig->getAlter();
  129. }
  130. /**
  131. * Preloads princess list from config option
  132. *
  133. * @return void
  134. */
  135. protected function loadPrincessList() {
  136. if (isset($this->altCfg['PRINCESS_LIST'])) {
  137. if (!empty($this->altCfg['PRINCESS_LIST'])) {
  138. $princessRaw = explode(',', $this->altCfg['PRINCESS_LIST']);
  139. if (!empty($princessRaw)) {
  140. foreach ($princessRaw as $io => $eachPrincess) {
  141. $eachPrincess = trim($eachPrincess);
  142. $this->princessList[$eachPrincess] = $eachPrincess;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Returns localized state name by its ID
  150. *
  151. * @param string $stateId
  152. *
  153. * @return string
  154. */
  155. public function getStateName($stateId) {
  156. $result = '';
  157. if (isset($this->stateTypes[$stateId])) {
  158. $result .= $this->stateTypes[$stateId];
  159. } else {
  160. $result .= $stateId;
  161. }
  162. return($result);
  163. }
  164. /**
  165. * Fills icons array for all of available task state types
  166. *
  167. * @return void
  168. */
  169. protected function setTypesIcons() {
  170. if (!empty($this->stateTypes)) {
  171. foreach ($this->stateTypes as $eachStateId => $eachStateLabel) {
  172. $fileName = self::ICON_PATH . strtolower($eachStateId) . self::ICON_EXT;
  173. if (file_exists($fileName)) {
  174. $this->stateIcons[$eachStateId] = $fileName;
  175. } else {
  176. $this->stateIcons[$eachStateId] = self::ICON_PATH . self::ICON_DEFAULT . self::ICON_EXT;
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Initializes states database abstraction layer
  183. *
  184. * @return void
  185. */
  186. protected function InitDatabase() {
  187. $this->statesDb = new NyanORM('taskstates');
  188. }
  189. /**
  190. * Loads all tasks states from database
  191. *
  192. * @return void
  193. */
  194. protected function loadAllTasksStates() {
  195. $this->allStates = $this->statesDb->getAll('taskid');
  196. }
  197. /**
  198. * Returns all available state types as type=>name
  199. *
  200. * @return array
  201. */
  202. public function getStateTypes() {
  203. return($this->stateTypes);
  204. }
  205. /**
  206. * Returns all available state icons as type=>iconpath
  207. *
  208. * @return array
  209. */
  210. public function getStateIcons() {
  211. return($this->stateIcons);
  212. }
  213. /**
  214. * Returns task state if it exists
  215. *
  216. * @param int $taskId
  217. *
  218. * @return string/void
  219. */
  220. public function getTaskState($taskId) {
  221. $result = '';
  222. if (isset($this->allStates[$taskId])) {
  223. $result = $this->allStates[$taskId]['state'];
  224. }
  225. return($result);
  226. }
  227. /**
  228. * Sets new state for the some task
  229. *
  230. * @param int $taskId
  231. * @param string $stateId
  232. *
  233. * @return void/string on error
  234. */
  235. public function setTaskState($taskId, $stateId) {
  236. $result = '';
  237. $taskId = ubRouting::filters($taskId, 'int');
  238. $stateId = ubRouting::filters($stateId, 'mres');
  239. if (isset($this->stateTypes[$stateId])) {
  240. //need some cleanup
  241. if (isset($this->allStates[$taskId])) {
  242. $this->statesDb->where('taskid', '=', $taskId);
  243. $this->statesDb->delete();
  244. }
  245. //setting new state for the task
  246. $this->statesDb->data('taskid', $taskId);
  247. $this->statesDb->data('state', $stateId);
  248. $this->statesDb->data('date', curdatetime());
  249. $this->statesDb->create();
  250. //saving logs
  251. $this->logStateChange($taskId, $stateId);
  252. //updating internal struct
  253. $this->loadAllTasksStates();
  254. } else {
  255. $result .= __('Something went wrong') . ': ' . __('Status') . ' ' . $stateId . ' ' . __('Not found');
  256. }
  257. return($result);
  258. }
  259. /**
  260. * Logs state change for some task
  261. *
  262. * @param int $taskId
  263. * @param string $stateId
  264. *
  265. * @retrun void
  266. */
  267. protected function logStateChange($taskId, $stateId) {
  268. $taskId = ubRouting::filters($taskId, 'int');
  269. $log_data_arr = array();
  270. $prevState = (isset($this->allStates[$taskId])) ? $this->allStates[$taskId]['state'] : '';
  271. $logData['taskstate']['old'] = $prevState;
  272. $logData['taskstate']['new'] = $stateId;
  273. $storeLogData = serialize($logData);
  274. $taskmanLogs = new NyanORM('taskmanlogs');
  275. $taskmanLogs->data('taskid', $taskId);
  276. $taskmanLogs->data('date', curdatetime());
  277. $taskmanLogs->data('admin', whoami());
  278. $taskmanLogs->data('ip', @$_SERVER['REMOTE_ADDR']);
  279. $taskmanLogs->data('event', 'modify');
  280. $taskmanLogs->data('logs', $storeLogData);
  281. $taskmanLogs->create();
  282. log_register('TASKSTATE CHANGE TASK [' . $taskId . '] STATE `' . $stateId . '`');
  283. }
  284. /**
  285. * Renders states control panel
  286. *
  287. * @param int $takskId Existing task ID
  288. * @param bool $protected Deny of modification tasks states
  289. *
  290. * @return string
  291. */
  292. public function renderStatePanel($takskId, $protected = false) {
  293. $result = '';
  294. $containerName = 'ajTaskState_' . $takskId;
  295. $result .= wf_AjaxLoader(true);
  296. $result .= wf_tag('div', false, '', 'id="' . $containerName . '"');
  297. if (!empty($this->stateTypes)) {
  298. if (isset($this->allStates[$takskId])) {
  299. $currentTaskState = $this->allStates[$takskId]['state'];
  300. } else {
  301. $currentTaskState = 'NONE';
  302. }
  303. //take some decision about state change protection
  304. if ($protected) {
  305. $stateChangeble = false;
  306. } else {
  307. if ($currentTaskState == 'STATE_PRINCESS') { //protected state
  308. if ($this->iAmPrincess()) {
  309. $stateChangeble = true;
  310. } else {
  311. $stateChangeble = false;
  312. }
  313. } else {
  314. //normal states
  315. $stateChangeble = true;
  316. }
  317. }
  318. foreach ($this->stateTypes as $stateId => $stateLabel) {
  319. $stateIcon = $this->stateIcons[$stateId];
  320. $controlClass = 'dashtask';
  321. //setting state as currently selected
  322. if ($currentTaskState == $stateId) {
  323. $controlClass .= ' todaysig';
  324. }
  325. if ($stateId == 'STATE_PRINCESS') {
  326. if ($protected) {
  327. $stateChangeble = false;
  328. } else {
  329. if ($this->iAmPrincess()) {
  330. $stateChangeble = true;
  331. } else {
  332. $stateChangeble = false;
  333. }
  334. }
  335. }
  336. if ($stateChangeble) {
  337. $controlUrl = wf_AjaxLink(self::URL_BASE . '&edittask=' . $takskId . '&changestate=' . $stateId, wf_img($stateIcon), $containerName);
  338. } else {
  339. $controlUrl = wf_img($stateIcon);
  340. }
  341. $result .= wf_tag('div', false, $controlClass, '');
  342. $result .= $controlUrl;
  343. $result .= wf_delimiter(0) . $stateLabel;
  344. $result .= wf_tag('div', true);
  345. }
  346. }
  347. $result .= wf_tag('div', true);
  348. $result .= wf_CleanDiv();
  349. if ($protected) {
  350. $messages = new UbillingMessageHelper();
  351. $result .= $messages->getStyledMessage(__('You cant modify closed tasks state'), 'warning');
  352. }
  353. return($result);
  354. }
  355. }