Notify.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /*
  4. # Nofitication system:
  5. Sends mail by settings `CRM_NOTIFY`:
  6. - who - user login
  7. - when - shedule (once a day, once on houd)
  8. - what - action type
  9. - last_exec_time
  10. */
  11. class Route_Notify extends RouteBase {
  12. public function handleAuth() {
  13. if (!User::logged()) {
  14. throw new HttpException('Unauthorized', 401);
  15. }
  16. }
  17. public function defaultAction() {
  18. SE_Layout::gora();
  19. try {
  20. $this->formUserTableReminder();
  21. } catch (Exception $e) {
  22. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  23. }
  24. SE_Layout::dol();
  25. }
  26. public function removeUserTableReminder($args) {
  27. $usrLogin = User::getLogin();
  28. $who = V::validate('who', $args, array('type'=>'word', 'equal'=>$usrLogin, 'error_msg_equal'=>"Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników"));
  29. $what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('what')));
  30. $when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('when')));
  31. if ($usrLogin != $who) throw new Exception("Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników");
  32. $pdo = DB::getPDO();
  33. $sth = $pdo->prepare("
  34. delete from `CRM_NOTIFY`
  35. where `who` = :who
  36. and `what` = :what
  37. and `when` = :when
  38. ");
  39. $sth->bindValue(':who', $usrLogin, PDO::PARAM_STR);
  40. $sth->bindValue(':what', $what, PDO::PARAM_STR);
  41. $sth->bindValue(':when', $when, PDO::PARAM_STR);
  42. $sth->execute();
  43. }
  44. public function formUserTableReminder() {
  45. $usrLogin = User::getLogin();
  46. echo '<div class="container">';
  47. $subTask = V::get('_subTask', '', $_POST);
  48. if ('_add_table_reminder' == $subTask) {
  49. try {
  50. $this->addUserTableReminder($usrLogin, $_POST);
  51. SE_Layout::alert('info', "Dodano powiadomienie");
  52. } catch (PDOException $e) {
  53. if (!empty($e->errorInfo) && !empty($e->errorInfo[1]) && '1062' == $e->errorInfo[1]) {
  54. SE_Layout::alert('warning', "Powiadomienie tego typu zostało już wcześniej dodane");
  55. } else {
  56. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage() . '<br>' . "Info: " . json_encode($e->errorInfo));
  57. }
  58. } catch (Exception $e) {
  59. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  60. }
  61. }
  62. else if ('_rm_table_reminder' == $subTask) {
  63. try {
  64. $this->removeUserTableReminder($_POST);
  65. SE_Layout::alert('info', "Usunięto powiadomienie");
  66. } catch (Exception $e) {
  67. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  68. }
  69. }
  70. $userReminders = $this->getUserReminders($usrLogin);
  71. ?>
  72. <h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
  73. <?php if (empty($userReminders)) : ?>
  74. <?php SE_Layout::alert('warning', "Brak zdefiniowanych powiadomień"); ?>
  75. <?php else : ?>
  76. <table class="table table-hovered">
  77. <thead>
  78. <tr>
  79. <th>rodzaj</th>
  80. <th>jak często</th>
  81. <th>utworzony</th>
  82. <th>ostatnio uruchomiony</th>
  83. <th></th>
  84. </tr>
  85. </thead>
  86. <tbody>
  87. <?php foreach ($userReminders as $reminder) : ?>
  88. <tr>
  89. <td><?php echo $this->printValue('what', $reminder['what']); ?></td>
  90. <td><?php echo $this->printValue('when', $reminder['when']); ?></td>
  91. <td><?php echo $reminder['_created']; ?></td>
  92. <td><?php echo ($reminder['last_exec_time'])? $reminder['last_exec_time'] : '<i>brak danych</i>'; ?></td>
  93. <td>
  94. <form method="post" class="form-inline">
  95. <input type="hidden" name="_subTask" value="_rm_table_reminder">
  96. <input type="hidden" name="who" value="<?php echo $usrLogin; ?>">
  97. <input type="hidden" name="what" value="<?php echo $reminder['what']; ?>">
  98. <input type="hidden" name="when" value="<?php echo $reminder['when']; ?>">
  99. <button class="btn btn-link btn-xs" onclick="return confirm('Czy usunąć powiadomienie?')" type="submit"><i class="glyphicon glyphicon-remove" style="color:red;opacity:0.4;"></i></button>
  100. </form>
  101. </td>
  102. </tr>
  103. <?php endforeach; ?>
  104. </tbody>
  105. </table>
  106. <?php endif; ?>
  107. <hr>
  108. <h4>Dodaj powiadomienia:</h4>
  109. <form method="post" class="form-inline">
  110. <input type="hidden" name="_subTask" value="_add_table_reminder">
  111. <label>rodzaj:</label>
  112. <select type="select" name="what" class="form-control">
  113. <option value="">[ Wybierz ]</option>
  114. <?php foreach ($this->getFieldValueLabels('what') as $value => $label) : ?>
  115. <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
  116. <?php endforeach; ?>
  117. </select>
  118. <label>jak często:</label>
  119. <select type="select" name="when" class="form-control">
  120. <option value="">[ Wybierz ]</option>
  121. <option value="once_a_day">Raz dziennie</option>
  122. </select>
  123. <input type="submit" value="Dodaj" class="btn btn-primary">
  124. </form>
  125. <?php
  126. echo '</div>';// .container
  127. }
  128. public function printValue($fldName, $argValue) {
  129. if ('what' == $fldName || 'when' == $fldName) {
  130. $valueLabels = $this->getFieldValueLabels($fldName);
  131. return V::get($argValue, $argValue, $valueLabels);
  132. }
  133. return $argValue;
  134. }
  135. public function getFieldValueLabels($fldName) {
  136. $valueLabels = array();
  137. if ('what' == $fldName) {
  138. $valueLabels['msgs'] = "wiadomości";
  139. $valueLabels['l_app_projekty'] = "projekty";
  140. $valueLabels['l_app_koresp'] = "korespondencja";
  141. $valueLabels['l_app_zadania'] = "zadania";
  142. $valueLabels['l_app_procesy'] = "procesy";
  143. $valueLabels['l_app_zasoby'] = "zasoby";
  144. $valueLabels['l_app_all_przypomnij'] = "*wszystkie";
  145. } else if ('when' == $fldName) {
  146. $valueLabels['once_a_day'] = "Raz dziennie";
  147. }
  148. return $valueLabels;
  149. }
  150. public function getFieldValues($fldName) {
  151. $valueLabels = $this->getFieldValueLabels($fldName);
  152. return array_keys($valueLabels);
  153. }
  154. public function addUserTableReminder($usrLogin, $args) {
  155. $what_values = $this->getFieldValues('what');
  156. $what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$what_values));
  157. $when_values = $this->getFieldValues('when');
  158. $when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
  159. $pdo = DB::getPDO();
  160. $sth = $pdo->prepare("
  161. insert into `CRM_NOTIFY` (
  162. `who`,
  163. `what`,
  164. `when`,
  165. `_created`
  166. ) values (
  167. :who,
  168. :what,
  169. :when,
  170. NOW()
  171. )
  172. ");
  173. $bindValues = array();
  174. $bindValues['who'] = array($usrLogin, PDO::PARAM_STR);
  175. $bindValues['what'] = array($what, PDO::PARAM_STR);
  176. $bindValues['when'] = array($when, PDO::PARAM_STR);
  177. $pdo->bindValues($sth, $bindValues);
  178. //DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
  179. // try {
  180. $sth->execute();
  181. // } catch (PDOException $e) {
  182. // throw new Exception("Błąd bazy danych: " . json_encode($e->errorInfo));
  183. // } catch (Exception $e) {
  184. // throw $e;
  185. // }
  186. }
  187. public function getUserReminders($usrLogin) {
  188. $pdo = DB::getPDO();
  189. $sth = $pdo->prepare("
  190. select n.*
  191. from `CRM_NOTIFY` n
  192. where n.`who` = '{$usrLogin}'
  193. ");
  194. $sth->execute();
  195. $reminders = $sth->fetchAll();
  196. return $reminders;
  197. }
  198. public function reinstallAction() {
  199. try {
  200. $force = ('true' == V::get('_force', '', $_GET));
  201. $this->reinstall($force);
  202. } catch (Exception $e) {
  203. echo'<div style="border:1px solid red; background:red; color:#fff;">';
  204. echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage();
  205. echo'</div>';
  206. }
  207. die('OK');
  208. }
  209. public function reinstallFunctionsAction() {
  210. $this->reinstallFunctions();
  211. die('OK');
  212. }
  213. public function runAction() {
  214. $msgId = V::get('_msgId', 0, $_REQUEST, 'int');
  215. if ($msgId > 0) {
  216. $this->runByMessageId($msgId);
  217. }
  218. $jsonData = new stdClass();
  219. $jsonData->type = 'success';
  220. $jsonData->msg = 'Gotowe';
  221. echo json_encode($jsonData);
  222. exit;
  223. }
  224. public function reinstallFunctions() {
  225. $sqlList = array();
  226. $db = DB::getDB();
  227. if ($db->has_errors()) {
  228. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  229. }
  230. foreach ($sqlList as $sqlName => $sql) {
  231. $res = $db->query($sql);
  232. if ($db->has_errors()) {
  233. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  234. }
  235. }
  236. }
  237. public function testReinstallTable() {
  238. $pdo = DB::getPDO();
  239. if(1){// TEST
  240. $expectedStruct = array();
  241. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  242. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  243. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  244. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  245. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  246. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  247. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  248. // $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  249. // $expectedStruct['t2'] = array('type'=>'int');
  250. // $expectedStruct['t3'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  251. // $expectedStruct['t_dec_11_x'] = array('type'=>'decimal', 'num_precision'=>11);
  252. // $expectedStruct['t_dec_11_2'] = array('type'=>'decimal', 'num_precision'=>11, 'num_scale'=>2);
  253. $expectedStruct['t_int'] = array('type'=>'int');
  254. $expectedStruct['t_tinyint'] = array('type'=>'tinyint');
  255. $expectedStruct['t_smallint'] = array('type'=>'smallint');
  256. $expectedStruct['t_mediumint'] = array('type'=>'mediumint');
  257. $expectedStruct['t_bigint'] = array('type'=>'bigint');
  258. $expectedStruct['t_decimal'] = array('type'=>'decimal');
  259. $expectedStruct['t_float'] = array('type'=>'float');
  260. $expectedStruct['t_double'] = array('type'=>'double');
  261. $expectedStruct['t_real'] = array('type'=>'real');
  262. $expectedStruct['t_date'] = array('type'=>'date');
  263. $expectedStruct['t_datetime'] = array('type'=>'datetime');
  264. $expectedStruct['t_timestamp'] = array('type'=>'timestamp');
  265. $expectedStruct['t_time'] = array('type'=>'time');
  266. $expectedStruct['t_year'] = array('type'=>'year');
  267. $expectedStruct['t_char'] = array('type'=>'char');
  268. $expectedStruct['t_varchar'] = array('type'=>'varchar');
  269. $expectedStruct['t_text'] = array('type'=>'text');
  270. $expectedStruct['t_tinytext'] = array('type'=>'tinytext');
  271. $expectedStruct['t_mediumtext'] = array('type'=>'mediumtext');
  272. $expectedStruct['t_longtext'] = array('type'=>'longtext');
  273. $expectedStruct['t_enum'] = array('type'=>'enum', 'values'=>array('v1'));
  274. $expectedStruct['t_set'] = array('type'=>'set', 'values'=>array('v1'));
  275. $expectedStruct['t_bit'] = array('type'=>'bit', 'max_length'=>2);
  276. $expectedStruct['t_boolean'] = array('type'=>'boolean');
  277. $expectedStruct['t_serial'] = array('type'=>'serial');
  278. $expectedStruct['t_binary'] = array('type'=>'binary');
  279. $expectedStruct['t_varbinary'] = array('type'=>'varbinary');
  280. $expectedStruct['t_blob'] = array('type'=>'blob');
  281. $expectedStruct['t_tinyblob'] = array('type'=>'tinyblob');
  282. $expectedStruct['t_mediumblob'] = array('type'=>'mediumblob');
  283. $expectedStruct['t_longblob'] = array('type'=>'longblob');
  284. $expectedStruct['t_geometry'] = array('type'=>'geometry');
  285. $expectedStruct['t_point'] = array('type'=>'point');
  286. $expectedStruct['t_linestring'] = array('type'=>'linestring');
  287. $expectedStruct['t_polygon'] = array('type'=>'polygon');
  288. $expectedStruct['t_multipoint'] = array('type'=>'multipoint');
  289. $expectedStruct['t_multilinestring'] = array('type'=>'multilinestring');
  290. $expectedStruct['t_multipolygon'] = array('type'=>'multipolygon');
  291. $expectedStruct['t_geometrycollection'] = array('type'=>'geometrycollection');
  292. $sqlCreate = $pdo->showCreateStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  293. DBG::_(true, true, "fixedEpectedStruct", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  294. $pdo->assertTableStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  295. {// force - drop/create
  296. $pdo->exec("DROP TABLE IF EXISTS _test_table");
  297. $pdo->exec($sqlCreate);
  298. }
  299. }
  300. if(0){// TEST nulls
  301. $sth = $pdo->prepare("DROP TABLE IF EXISTS CRM_NOTIFY");
  302. $sth->execute();
  303. $sth = $pdo->prepare("
  304. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  305. `who` varchar(20)
  306. , `when` varchar(255) NOT NULL
  307. , `what` varchar(255) DEFAULT ''
  308. , `t1` varchar(255) NOT NULL -- same as with DEFAULT ''
  309. , `t2` varchar(255) NOT NULL DEFAULT ''
  310. , `t2z` varchar(255) NOT NULL DEFAULT '0'
  311. -- , `t3` varchar(255) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  312. , `t4` varchar(255) NULL -- same as with DEFAULT NULL
  313. , `t5` varchar(255) NULL DEFAULT ''
  314. , `t6` varchar(255) NULL DEFAULT NULL
  315. , `i1` int(11) NOT NULL -- same as with DEFAULT 0
  316. , `i2` int(11) NOT NULL DEFAULT 0
  317. -- , `i3` int(11) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  318. , `i4` int(11) NULL -- same as with DEFAULT NULL
  319. , `i5` int(11) NULL DEFAULT 0
  320. , `i6` int(11) NULL DEFAULT NULL
  321. , `last_exec_time` datetime DEFAULT NULL
  322. , KEY `who` (`who`)
  323. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  324. ");
  325. $sth->execute();
  326. $sth = $pdo->prepare("insert into CRM_NOTIFY (`last_exec_time`) values (NULL)");
  327. $sth->execute();
  328. // INSERT INTO CRM_NOTIFY (t1, t2, t2z, t4, t5, t6, i1, i2, i4, i5, i6, last_exec_time)
  329. // VALUES ('', '', '0', NULL, '', NULL, 0, 0, NULL, 0, NULL, NULL);
  330. $struct = $pdo->getTableStruct('CRM_NOTIFY');
  331. DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  332. DBG::_(true, true, "`t2`: is_null(struct[4]['Default'])=(".is_null($struct[4]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  333. DBG::_(true, true, "`t4`: is_null(struct[6]['Default'])=(".is_null($struct[6]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  334. DBG::_(true, true, "`t5`: is_null(struct[7]['Default'])=(".is_null($struct[7]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  335. DBG::_(true, true, "`t6`: is_null(struct[8]['Default'])=(".is_null($struct[8]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  336. $expectedStruct = array();
  337. $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  338. //$expectedStruct['t2'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  339. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct);
  340. }
  341. }
  342. public function reinstall($force = false) {
  343. $sqlList = array();
  344. $pdo = DB::getPDO();
  345. $sqlCreate = <<<EOF_SQL_CREATE
  346. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  347. `who` varchar(20) NOT NULL
  348. , `when` varchar(255) NOT NULL
  349. , `what` varchar(255) NOT NULL
  350. , `last_exec_time` datetime DEFAULT NULL
  351. , `_created` datetime NOT NULL
  352. , UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  353. , KEY `key_who` (`who`)
  354. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  355. EOF_SQL_CREATE;
  356. /*
  357. `CRM_NOTIFY`:
  358. - who - user login
  359. - when - shedule (once a day, once on houd)
  360. - what - action type
  361. - last_exec_time
  362. */
  363. if($force){
  364. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  365. $pdo->exec("
  366. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  367. `who` varchar(20) NOT NULL
  368. , `when` varchar(255) NOT NULL
  369. , `what` varchar(255) NOT NULL DEFAULT ''
  370. , `last_exec_time` datetime
  371. , KEY `key_who` (`who`)
  372. , UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  373. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  374. ");
  375. }
  376. {// assertTableStruct CRM_NOTIFY
  377. $expectedStruct = array();
  378. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  379. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  380. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  381. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  382. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  383. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  384. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  385. DBG::_(true, true, "sqlCreate - raw", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  386. $sqlCreate = $pdo->showCreateStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  387. DBG::_(true, true, "sqlCreate - generated", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  388. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  389. if(0){// force - drop/create
  390. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  391. $pdo->exec($sqlCreate);
  392. }
  393. }
  394. {// assertTableStruct CRM_NOTIFY_EXEC_LOG
  395. // TODO: create table CRM_NOTIFY_EXEC_LOG with action log
  396. }
  397. //$this->reinstallFunctions();
  398. }
  399. }