Notify.php 16 KB

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