Notify.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ?>
  20. <div class="container">
  21. <h1>Nofitication system</h1>
  22. ...
  23. </div>
  24. <?php
  25. SE_Layout::dol();
  26. }
  27. public function reinstallAction() {
  28. try {
  29. $this->reinstall();
  30. } catch (Exception $e) {
  31. echo'<div style="border:1px solid red; background:red; color:#fff;">';
  32. echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage();
  33. echo'</div>';
  34. }
  35. die('OK');
  36. }
  37. public function reinstallFunctionsAction() {
  38. $this->reinstallFunctions();
  39. die('OK');
  40. }
  41. public function runAction() {
  42. $msgId = V::get('_msgId', 0, $_REQUEST, 'int');
  43. if ($msgId > 0) {
  44. $this->runByMessageId($msgId);
  45. }
  46. $jsonData = new stdClass();
  47. $jsonData->type = 'success';
  48. $jsonData->msg = 'Gotowe';
  49. echo json_encode($jsonData);
  50. exit;
  51. }
  52. public function reinstallFunctions() {
  53. $sqlList = array();
  54. $db = DB::getDB();
  55. if ($db->has_errors()) {
  56. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  57. }
  58. foreach ($sqlList as $sqlName => $sql) {
  59. $res = $db->query($sql);
  60. if ($db->has_errors()) {
  61. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  62. }
  63. }
  64. }
  65. public function testReinstallTable() {
  66. $pdo = DB::getPDO();
  67. if(1){// TEST
  68. $expectedStruct = array();
  69. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  70. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  71. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  72. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  73. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  74. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  75. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  76. // $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  77. // $expectedStruct['t2'] = array('type'=>'int');
  78. // $expectedStruct['t3'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  79. // $expectedStruct['t_dec_11_x'] = array('type'=>'decimal', 'num_precision'=>11);
  80. // $expectedStruct['t_dec_11_2'] = array('type'=>'decimal', 'num_precision'=>11, 'num_scale'=>2);
  81. $expectedStruct['t_int'] = array('type'=>'int');
  82. $expectedStruct['t_tinyint'] = array('type'=>'tinyint');
  83. $expectedStruct['t_smallint'] = array('type'=>'smallint');
  84. $expectedStruct['t_mediumint'] = array('type'=>'mediumint');
  85. $expectedStruct['t_bigint'] = array('type'=>'bigint');
  86. $expectedStruct['t_decimal'] = array('type'=>'decimal');
  87. $expectedStruct['t_float'] = array('type'=>'float');
  88. $expectedStruct['t_double'] = array('type'=>'double');
  89. $expectedStruct['t_real'] = array('type'=>'real');
  90. $expectedStruct['t_date'] = array('type'=>'date');
  91. $expectedStruct['t_datetime'] = array('type'=>'datetime');
  92. $expectedStruct['t_timestamp'] = array('type'=>'timestamp');
  93. $expectedStruct['t_time'] = array('type'=>'time');
  94. $expectedStruct['t_year'] = array('type'=>'year');
  95. $expectedStruct['t_char'] = array('type'=>'char');
  96. $expectedStruct['t_varchar'] = array('type'=>'varchar');
  97. $expectedStruct['t_text'] = array('type'=>'text');
  98. $expectedStruct['t_tinytext'] = array('type'=>'tinytext');
  99. $expectedStruct['t_mediumtext'] = array('type'=>'mediumtext');
  100. $expectedStruct['t_longtext'] = array('type'=>'longtext');
  101. $expectedStruct['t_enum'] = array('type'=>'enum', 'values'=>array('v1'));
  102. $expectedStruct['t_set'] = array('type'=>'set', 'values'=>array('v1'));
  103. $expectedStruct['t_bit'] = array('type'=>'bit', 'max_length'=>2);
  104. $expectedStruct['t_boolean'] = array('type'=>'boolean');
  105. $expectedStruct['t_serial'] = array('type'=>'serial');
  106. $expectedStruct['t_binary'] = array('type'=>'binary');
  107. $expectedStruct['t_varbinary'] = array('type'=>'varbinary');
  108. $expectedStruct['t_blob'] = array('type'=>'blob');
  109. $expectedStruct['t_tinyblob'] = array('type'=>'tinyblob');
  110. $expectedStruct['t_mediumblob'] = array('type'=>'mediumblob');
  111. $expectedStruct['t_longblob'] = array('type'=>'longblob');
  112. $expectedStruct['t_geometry'] = array('type'=>'geometry');
  113. $expectedStruct['t_point'] = array('type'=>'point');
  114. $expectedStruct['t_linestring'] = array('type'=>'linestring');
  115. $expectedStruct['t_polygon'] = array('type'=>'polygon');
  116. $expectedStruct['t_multipoint'] = array('type'=>'multipoint');
  117. $expectedStruct['t_multilinestring'] = array('type'=>'multilinestring');
  118. $expectedStruct['t_multipolygon'] = array('type'=>'multipolygon');
  119. $expectedStruct['t_geometrycollection'] = array('type'=>'geometrycollection');
  120. $sqlCreate = $pdo->showCreateStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  121. DBG::_(true, true, "fixedEpectedStruct", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  122. $pdo->assertTableStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  123. {// force - drop/create
  124. $pdo->exec("DROP TABLE IF EXISTS _test_table");
  125. $pdo->exec($sqlCreate);
  126. }
  127. }
  128. if(0){// TEST nulls
  129. $sth = $pdo->prepare("DROP TABLE IF EXISTS CRM_NOTIFY");
  130. $sth->execute();
  131. $sth = $pdo->prepare("
  132. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  133. `who` varchar(20)
  134. , `when` varchar(255) NOT NULL
  135. , `what` varchar(255) DEFAULT ''
  136. , `t1` varchar(255) NOT NULL -- same as with DEFAULT ''
  137. , `t2` varchar(255) NOT NULL DEFAULT ''
  138. , `t2z` varchar(255) NOT NULL DEFAULT '0'
  139. -- , `t3` varchar(255) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  140. , `t4` varchar(255) NULL -- same as with DEFAULT NULL
  141. , `t5` varchar(255) NULL DEFAULT ''
  142. , `t6` varchar(255) NULL DEFAULT NULL
  143. , `i1` int(11) NOT NULL -- same as with DEFAULT 0
  144. , `i2` int(11) NOT NULL DEFAULT 0
  145. -- , `i3` int(11) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  146. , `i4` int(11) NULL -- same as with DEFAULT NULL
  147. , `i5` int(11) NULL DEFAULT 0
  148. , `i6` int(11) NULL DEFAULT NULL
  149. , `last_exec_time` datetime DEFAULT NULL
  150. , KEY `who` (`who`)
  151. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  152. ");
  153. $sth->execute();
  154. $sth = $pdo->prepare("insert into CRM_NOTIFY (`last_exec_time`) values (NULL)");
  155. $sth->execute();
  156. // INSERT INTO CRM_NOTIFY (t1, t2, t2z, t4, t5, t6, i1, i2, i4, i5, i6, last_exec_time)
  157. // VALUES ('', '', '0', NULL, '', NULL, 0, 0, NULL, 0, NULL, NULL);
  158. $struct = $pdo->getTableStruct('CRM_NOTIFY');
  159. DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  160. DBG::_(true, true, "`t2`: is_null(struct[4]['Default'])=(".is_null($struct[4]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  161. DBG::_(true, true, "`t4`: is_null(struct[6]['Default'])=(".is_null($struct[6]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  162. DBG::_(true, true, "`t5`: is_null(struct[7]['Default'])=(".is_null($struct[7]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  163. DBG::_(true, true, "`t6`: is_null(struct[8]['Default'])=(".is_null($struct[8]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  164. $expectedStruct = array();
  165. $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  166. //$expectedStruct['t2'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  167. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct);
  168. }
  169. }
  170. public function reinstall() {
  171. $sqlList = array();
  172. $pdo = DB::getPDO();
  173. $sqlCreate = <<<EOF_SQL_CREATE
  174. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  175. `who` varchar(20) NOT NULL
  176. , `when` varchar(255) NOT NULL
  177. , `what` varchar(255) NOT NULL
  178. , `last_exec_time` datetime DEFAULT NULL
  179. , `_created` datetime NOT NULL
  180. , UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  181. , KEY `key_who` (`who`)
  182. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  183. EOF_SQL_CREATE;
  184. /*
  185. `CRM_NOTIFY`:
  186. - who - user login
  187. - when - shedule (once a day, once on houd)
  188. - what - action type
  189. - last_exec_time
  190. */
  191. if(1){
  192. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  193. $pdo->exec("
  194. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  195. `who` varchar(20) NOT NULL
  196. , `when` varchar(255) NOT NULL
  197. , `what` varchar(255) NOT NULL DEFAULT ''
  198. , `last_exec_time` datetime
  199. , KEY `who` (`who`)
  200. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  201. ");
  202. }
  203. {// assertTableStruct CRM_NOTIFY
  204. $expectedStruct = array();
  205. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  206. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  207. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  208. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  209. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  210. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  211. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  212. DBG::_(true, true, "sqlCreate - raw", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  213. $sqlCreate = $pdo->showCreateStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  214. DBG::_(true, true, "sqlCreate - generated", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  215. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  216. if(0){// force - drop/create
  217. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  218. $pdo->exec($sqlCreate);
  219. }
  220. }
  221. {// assertTableStruct CRM_NOTIFY_EXEC_LOG
  222. // TODO: create table CRM_NOTIFY_EXEC_LOG with action log
  223. }
  224. //$this->reinstallFunctions();
  225. }
  226. }