Notify.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Przypomnij');
  4. /*
  5. # Nofitication system:
  6. Sends mail by settings `CRM_NOTIFY`:
  7. - who - user login
  8. - when - shedule (once a day, once on houd)
  9. - what - action type
  10. - last_exec_time
  11. */
  12. class Route_Notify extends RouteBase {
  13. public function handleAuth() {
  14. if (!User::logged()) {
  15. throw new HttpException('Unauthorized', 401);
  16. }
  17. }
  18. public function defaultAction() {
  19. SE_Layout::gora();
  20. try {
  21. $this->formUserTableReminder();
  22. } catch (Exception $e) {
  23. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  24. }
  25. SE_Layout::dol();
  26. }
  27. public function generateUserRemindersAction() {
  28. $usrLogin = User::getLogin();
  29. $usrLogin = (isset($_GET['usrLogin']))? V::get('usrLogin', '', $_GET, 'login') : $usrLogin;
  30. if (!$usrLogin) die("Wrong user login!");
  31. $userReminders = $this->getUserReminders($usrLogin);
  32. if (empty($userReminders)) die("Brak zdefiniowanych powiadomień");
  33. header('Content-Type: text/html; charset="UTF-8"');
  34. $this->generateUserReminders($usrLogin, $userReminders);
  35. }
  36. public function generateUserReminders($usrLogin, $userReminders) {
  37. echo '<html>';
  38. echo '<body>';
  39. echo '<div>';
  40. ?>
  41. <h1 style="<?php echo $this->inlineCss('h1'); ?>">Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
  42. <table rules="all" cellspacing="0" style="border-color:#bbb;" cellpadding="8">
  43. <?php foreach ($userReminders as $reminder) : ?>
  44. <tr>
  45. <td colspan="4" style="<?php echo $this->inlineCss('td.header'); ?>"><strong><?php echo $this->printValue('what', $reminder['what']); ?></strong></td>
  46. </tr>
  47. <?php $lp = 0; foreach ($this->getTodoForUserReminder($usrLogin, $reminder['what']) as $todo) : ?>
  48. <tr>
  49. <td style="<?php echo $this->inlineCss('td.lp'); ?>"><?php echo ++$lp; ?></td>
  50. <td style="<?php echo $this->inlineCss('td.' . $todo['_l_app_class']); ?>">
  51. <?php echo $todo['_l_app_date']; ?>
  52. <br><i style="color:#333;"><?php echo $todo['_l_app']; ?></i>
  53. <?php if ($todo['ID'] > 0) : ?>
  54. <br><a href="<?php echo $this->editLink($todo); ?>" target="_blank" style="<?php echo $this->inlineCss('a'); ?>">Edytuj rekord <?php echo $todo['ID']; ?></a>
  55. <?php endif; ?>
  56. </td>
  57. <td style="<?php echo $this->inlineCss('td.info'); ?>"><?php echo $todo['L_APPOITMENT_INFO']; ?></td>
  58. <td style="<?php echo $this->inlineCss('td.record-info'); ?>">
  59. <br><?php echo $todo['_title']; ?>
  60. </td>
  61. <?php if (V::get('DBG', '', $_GET)) : ?>
  62. <td style="<?php echo $this->inlineCss('td.record-desc'); ?>"><pre style="border:1px solid red;max-height:80px;overflow:scroll"><?php print_r($todo); ?></pre></td>
  63. <?php endif; ?>
  64. </tr>
  65. <?php endforeach; ?>
  66. <?php endforeach; ?>
  67. </table>
  68. <?php
  69. echo '</div>';// .container
  70. echo '</body>';
  71. echo '</html>';
  72. }
  73. public function editLink($todo) {
  74. $idTblZasob = 0;
  75. $idRecord = 0;
  76. if ('zasob' == $todo['_task_type']) {
  77. $idTblZasob = $todo['_idZasobTable'];
  78. $idRecord = $todo['ID'];
  79. }
  80. if (!$idTblZasob || !$idRecord) return '#';
  81. $urlParts = array();
  82. $urlParts[] = (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']))? $_SERVER['HTTP_X_FORWARDED_PROTO'] : V::get('REQUEST_SCHEME', 'http', $_SERVER);
  83. $urlParts[] = '://';
  84. $urlParts[] = $_SERVER['SERVER_NAME'];
  85. $urlParts[] = $_SERVER['SCRIPT_URL'];
  86. // https://biuro.biall-net.pl/dev-pl/se-master/index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=1466#EDIT/3954
  87. $urlParts[] = "?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$idTblZasob}#EDIT/{$idRecord}";
  88. return implode("", $urlParts);
  89. }
  90. public function inlineCss($cssSelector) {
  91. $baseCss = array();
  92. $baseCss['font-family'] = 'Arial, Helvetica, sans-serif';
  93. $baseCss['font-size'] = 'small';
  94. $baseCss['color'] = '#333';
  95. $customCss = array(
  96. 'h1' => array(),
  97. 'td.header' => array('background' => '#eee'),
  98. 'td.date-base' => array('white-space' => 'nowrap'),
  99. 'td.lp' => array('color' => 'silver'),
  100. 'a' => array('color' => '#337ab7', 'text-decoration' => 'none')
  101. );
  102. $css = $baseCss;
  103. if (array_key_exists($cssSelector, $customCss)) $css = V::extend($css, $customCss[$cssSelector]);
  104. if ('td.date-' == substr($cssSelector, 0, 8)) {
  105. $css = V::extend($css, $customCss['td.date-base']);
  106. $css = V::extend($css, array('color' => $this->getTodoDateCssColor(substr($cssSelector, 3))));
  107. }
  108. $inlineCss = array();
  109. foreach ($css as $cssPropName => $cssValue) {
  110. if (true === $cssValue) {
  111. if (!array_key_exists($cssPropName, $baseCss)) continue;
  112. $cssValue = $baseCss[$cssPropName];
  113. }
  114. $inlineCss[] = "{$cssPropName}:{$cssValue}";
  115. }
  116. return implode(";", $inlineCss);
  117. }
  118. public function getTodoDateCssColor($lAppClass) {
  119. $color = '#000';
  120. switch ($lAppClass) {
  121. case 'date-PO_TERMINIE': $color = '#FD4242'; break;
  122. case 'date-DZISIAJ': $color = '#34B934'; break;
  123. case 'date-W_CIAGU_7_DNI': $color = '#C58B1F'; break;
  124. case 'date-PO_7_DNIACH': $color = '#87847D'; break;
  125. }
  126. return $color;
  127. }
  128. public function getTodoForUserReminder($usrLogin, $reminderWhat) {
  129. $listTodo = array();
  130. if ('msgs' == $reminderWhat) {
  131. // $todo = array();
  132. // $todo['_title'] = 'test msgs id';
  133. // $todo['L_APPOITMENT_INFO'] = 'test msgs id';
  134. // $listTodo[] = $todo;
  135. }
  136. else if ('l_app_projekty' == $reminderWhat) {
  137. $tasks = $this->getPrzypomnijTasks($usrLogin);
  138. foreach ($tasks as $task) {
  139. if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
  140. if ('projekt' != $task['_task_type']) continue;
  141. //$task['DESC'] = $task['A_PROBLEM_DESC'];
  142. $listTodo[] = $task;
  143. }
  144. }
  145. else if ('l_app_koresp' == $reminderWhat) {
  146. $tasks = $this->getPrzypomnijTasks($usrLogin);
  147. foreach ($tasks as $task) {
  148. if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
  149. if ('koresp' != $task['_task_type']) continue;
  150. //$task['DESC'] = $task['A_PROBLEM_DESC'];
  151. $listTodo[] = $task;
  152. }
  153. }
  154. else if ('l_app_zadania' == $reminderWhat) {
  155. $tasks = $this->getPrzypomnijTasks($usrLogin);
  156. foreach ($tasks as $task) {
  157. if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
  158. if ('task' != $task['_task_type']) continue;
  159. //$task['DESC'] = $task['A_PROBLEM_DESC'];
  160. $listTodo[] = $task;
  161. }
  162. }
  163. else if ('l_app_procesy' == $reminderWhat) {
  164. $tasks = $this->getPrzypomnijTasks($usrLogin);
  165. foreach ($tasks as $task) {
  166. if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
  167. if ('proces' != $task['_task_type']) continue;
  168. $listTodo[] = $task;
  169. }
  170. }
  171. else if ('l_app_zasoby' == $reminderWhat) {
  172. $tasks = $this->getPrzypomnijTasks($usrLogin);
  173. foreach ($tasks as $task) {
  174. if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
  175. if ('zasob' != $task['_task_type']) continue;
  176. $listTodo[] = $task;
  177. }
  178. }
  179. return $listTodo;
  180. }
  181. public function getPrzypomnijTasks($usrLogin) {
  182. static $_tasks = array();
  183. if (empty($_tasks)) {
  184. $przypomnij = new Przypomnij();
  185. $przypomnij->setRecurseLimit(3);
  186. $przypomnij->fetchData();
  187. $przypomnij->setFltrUser($usrLogin);
  188. $tasks = $przypomnij->getTasksByDate();
  189. $usrGroupNames = User::getLdapGroupsNames();
  190. foreach ($tasks as $task) {
  191. if (!$task->_show) continue;
  192. if (
  193. $task->A_CLASSIFIED != ''
  194. and !(in_array($task->A_CLASSIFIED, $usrGroupNames))
  195. and $task->A_ADM_COMPANY != ''
  196. and !(in_array($task->A_ADM_COMPANY, $usrGroupNames))
  197. // !($allowedUsers[$task->_l_app]) - to jest bez sensu - wystarczy widocznosc sprawy?
  198. ) {
  199. continue;
  200. }
  201. $task->_l_app_class = $przypomnij->getTaskDateFltrType($task->_l_app_date);
  202. $task->_idZasobTable = $przypomnij->getZasobIdByType($task->_task_type);
  203. $_tasks[] = (array)$task;
  204. }
  205. }
  206. return $_tasks;
  207. }
  208. public function removeUserTableReminder($args) {
  209. $usrLogin = User::getLogin();
  210. $who = V::validate('who', $args, array('type'=>'word', 'equal'=>$usrLogin, 'error_msg_equal'=>"Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników"));
  211. $what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('what')));
  212. $when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$this->getFieldValues('when')));
  213. if ($usrLogin != $who) throw new Exception("Brak uprawnień do wprowadzania zmian w powiadomieniach innych użytwkoników");
  214. $pdo = DB::getPDO();
  215. $sth = $pdo->prepare("
  216. delete from `CRM_NOTIFY`
  217. where `who` = :who
  218. and `what` = :what
  219. and `when` = :when
  220. ");
  221. $sth->bindValue(':who', $usrLogin, PDO::PARAM_STR);
  222. $sth->bindValue(':what', $what, PDO::PARAM_STR);
  223. $sth->bindValue(':when', $when, PDO::PARAM_STR);
  224. $sth->execute();
  225. }
  226. public function formUserTableReminder() {
  227. $usrLogin = User::getLogin();
  228. echo '<div class="container">';
  229. $subTask = V::get('_subTask', '', $_POST);
  230. if ('_add_table_reminder' == $subTask) {
  231. try {
  232. $this->addUserTableReminder($usrLogin, $_POST);
  233. SE_Layout::alert('info', "Dodano powiadomienie");
  234. } catch (PDOException $e) {
  235. if (!empty($e->errorInfo) && !empty($e->errorInfo[1]) && '1062' == $e->errorInfo[1]) {
  236. SE_Layout::alert('warning', "Powiadomienie tego typu zostało już wcześniej dodane");
  237. } else {
  238. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage() . '<br>' . "Info: " . json_encode($e->errorInfo));
  239. }
  240. } catch (Exception $e) {
  241. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  242. }
  243. }
  244. else if ('_rm_table_reminder' == $subTask) {
  245. try {
  246. $this->removeUserTableReminder($_POST);
  247. SE_Layout::alert('info', "Usunięto powiadomienie");
  248. } catch (Exception $e) {
  249. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  250. }
  251. }
  252. $userReminders = $this->getUserReminders($usrLogin);
  253. ?>
  254. <h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
  255. <?php if (empty($userReminders)) : ?>
  256. <?php SE_Layout::alert('warning', "Brak zdefiniowanych powiadomień"); ?>
  257. <?php else : ?>
  258. <table class="table table-hovered">
  259. <thead>
  260. <tr>
  261. <th>rodzaj</th>
  262. <th>jak często</th>
  263. <th>utworzony</th>
  264. <th>ostatnio uruchomiony</th>
  265. <th></th>
  266. </tr>
  267. </thead>
  268. <tbody>
  269. <?php foreach ($userReminders as $reminder) : ?>
  270. <tr>
  271. <td><?php echo $this->printValue('what', $reminder['what']); ?></td>
  272. <td><?php echo $this->printValue('when', $reminder['when']); ?></td>
  273. <td><?php echo $reminder['_created']; ?></td>
  274. <td><?php echo ($reminder['last_exec_time'])? $reminder['last_exec_time'] : '<i>brak danych</i>'; ?></td>
  275. <td>
  276. <form method="post" class="form-inline">
  277. <input type="hidden" name="_subTask" value="_rm_table_reminder">
  278. <input type="hidden" name="who" value="<?php echo $usrLogin; ?>">
  279. <input type="hidden" name="what" value="<?php echo $reminder['what']; ?>">
  280. <input type="hidden" name="when" value="<?php echo $reminder['when']; ?>">
  281. <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>
  282. </form>
  283. </td>
  284. </tr>
  285. <?php endforeach; ?>
  286. </tbody>
  287. </table>
  288. <?php endif; ?>
  289. <hr>
  290. <h4>Dodaj powiadomienia:</h4>
  291. <form method="post" class="form-inline">
  292. <input type="hidden" name="_subTask" value="_add_table_reminder">
  293. <label>rodzaj:</label>
  294. <select type="select" name="what" class="form-control">
  295. <option value="">[ Wybierz ]</option>
  296. <?php foreach ($this->getFieldValueLabels('what') as $value => $label) : ?>
  297. <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
  298. <?php endforeach; ?>
  299. </select>
  300. <label>jak często:</label>
  301. <select type="select" name="when" class="form-control">
  302. <option value="">[ Wybierz ]</option>
  303. <option value="once_a_day">Raz dziennie</option>
  304. </select>
  305. <input type="submit" value="Dodaj" class="btn btn-primary">
  306. </form>
  307. <?php
  308. echo '</div>';// .container
  309. }
  310. public function printValue($fldName, $argValue) {
  311. if ('what' == $fldName || 'when' == $fldName) {
  312. $valueLabels = $this->getFieldValueLabels($fldName);
  313. return V::get($argValue, $argValue, $valueLabels);
  314. }
  315. return $argValue;
  316. }
  317. public function getFieldValueLabels($fldName) {
  318. $valueLabels = array();
  319. if ('what' == $fldName) {
  320. $valueLabels['msgs'] = "wiadomości";
  321. $valueLabels['l_app_projekty'] = "projekty";
  322. $valueLabels['l_app_koresp'] = "korespondencja";
  323. $valueLabels['l_app_zadania'] = "zadania";
  324. $valueLabels['l_app_procesy'] = "procesy";
  325. $valueLabels['l_app_zasoby'] = "zasoby";
  326. $valueLabels['l_app_all_przypomnij'] = "*wszystkie";
  327. } else if ('when' == $fldName) {
  328. $valueLabels['once_a_day'] = "Raz dziennie";
  329. }
  330. return $valueLabels;
  331. }
  332. public function getFieldValues($fldName) {
  333. $valueLabels = $this->getFieldValueLabels($fldName);
  334. return array_keys($valueLabels);
  335. }
  336. public function addUserTableReminder($usrLogin, $args) {
  337. $what_values = $this->getFieldValues('what');
  338. $what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$what_values));
  339. $when_values = $this->getFieldValues('when');
  340. $when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
  341. $pdo = DB::getPDO();
  342. $sth = $pdo->prepare("
  343. insert into `CRM_NOTIFY` (
  344. `who`,
  345. `what`,
  346. `when`,
  347. `_created`
  348. ) values (
  349. :who,
  350. :what,
  351. :when,
  352. NOW()
  353. )
  354. ");
  355. $bindValues = array();
  356. $bindValues['who'] = array($usrLogin, PDO::PARAM_STR);
  357. $bindValues['what'] = array($what, PDO::PARAM_STR);
  358. $bindValues['when'] = array($when, PDO::PARAM_STR);
  359. $pdo->bindValues($sth, $bindValues);
  360. //DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
  361. // try {
  362. $sth->execute();
  363. // } catch (PDOException $e) {
  364. // throw new Exception("Błąd bazy danych: " . json_encode($e->errorInfo));
  365. // } catch (Exception $e) {
  366. // throw $e;
  367. // }
  368. }
  369. public function getUserReminders($usrLogin) {
  370. $pdo = DB::getPDO();
  371. $sth = $pdo->prepare("
  372. select n.*
  373. from `CRM_NOTIFY` n
  374. where n.`who` = '{$usrLogin}'
  375. ");
  376. $sth->execute();
  377. $reminders = $sth->fetchAll();
  378. return $reminders;
  379. }
  380. public function reinstallAction() {
  381. try {
  382. $force = ('true' == V::get('_force', '', $_GET));
  383. $this->reinstall($force);
  384. } catch (Exception $e) {
  385. echo'<div style="border:1px solid red; background:red; color:#fff;">';
  386. echo "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage();
  387. echo'</div>';
  388. }
  389. die('OK');
  390. }
  391. public function reinstallFunctionsAction() {
  392. $this->reinstallFunctions();
  393. die('OK');
  394. }
  395. public function runAction() {
  396. $msgId = V::get('_msgId', 0, $_REQUEST, 'int');
  397. if ($msgId > 0) {
  398. $this->runByMessageId($msgId);
  399. }
  400. $jsonData = new stdClass();
  401. $jsonData->type = 'success';
  402. $jsonData->msg = 'Gotowe';
  403. echo json_encode($jsonData);
  404. exit;
  405. }
  406. public function reinstallFunctions() {
  407. $sqlList = array();
  408. $db = DB::getDB();
  409. if ($db->has_errors()) {
  410. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  411. }
  412. foreach ($sqlList as $sqlName => $sql) {
  413. $res = $db->query($sql);
  414. if ($db->has_errors()) {
  415. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  416. }
  417. }
  418. }
  419. public function testReinstallTable() {
  420. $pdo = DB::getPDO();
  421. if(1){// TEST
  422. $expectedStruct = array();
  423. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  424. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  425. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  426. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  427. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  428. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  429. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  430. // $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  431. // $expectedStruct['t2'] = array('type'=>'int');
  432. // $expectedStruct['t3'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  433. // $expectedStruct['t_dec_11_x'] = array('type'=>'decimal', 'num_precision'=>11);
  434. // $expectedStruct['t_dec_11_2'] = array('type'=>'decimal', 'num_precision'=>11, 'num_scale'=>2);
  435. $expectedStruct['t_int'] = array('type'=>'int');
  436. $expectedStruct['t_tinyint'] = array('type'=>'tinyint');
  437. $expectedStruct['t_smallint'] = array('type'=>'smallint');
  438. $expectedStruct['t_mediumint'] = array('type'=>'mediumint');
  439. $expectedStruct['t_bigint'] = array('type'=>'bigint');
  440. $expectedStruct['t_decimal'] = array('type'=>'decimal');
  441. $expectedStruct['t_float'] = array('type'=>'float');
  442. $expectedStruct['t_double'] = array('type'=>'double');
  443. $expectedStruct['t_real'] = array('type'=>'real');
  444. $expectedStruct['t_date'] = array('type'=>'date');
  445. $expectedStruct['t_datetime'] = array('type'=>'datetime');
  446. $expectedStruct['t_timestamp'] = array('type'=>'timestamp');
  447. $expectedStruct['t_time'] = array('type'=>'time');
  448. $expectedStruct['t_year'] = array('type'=>'year');
  449. $expectedStruct['t_char'] = array('type'=>'char');
  450. $expectedStruct['t_varchar'] = array('type'=>'varchar');
  451. $expectedStruct['t_text'] = array('type'=>'text');
  452. $expectedStruct['t_tinytext'] = array('type'=>'tinytext');
  453. $expectedStruct['t_mediumtext'] = array('type'=>'mediumtext');
  454. $expectedStruct['t_longtext'] = array('type'=>'longtext');
  455. $expectedStruct['t_enum'] = array('type'=>'enum', 'values'=>array('v1'));
  456. $expectedStruct['t_set'] = array('type'=>'set', 'values'=>array('v1'));
  457. $expectedStruct['t_bit'] = array('type'=>'bit', 'max_length'=>2);
  458. $expectedStruct['t_boolean'] = array('type'=>'boolean');
  459. $expectedStruct['t_serial'] = array('type'=>'serial');
  460. $expectedStruct['t_binary'] = array('type'=>'binary');
  461. $expectedStruct['t_varbinary'] = array('type'=>'varbinary');
  462. $expectedStruct['t_blob'] = array('type'=>'blob');
  463. $expectedStruct['t_tinyblob'] = array('type'=>'tinyblob');
  464. $expectedStruct['t_mediumblob'] = array('type'=>'mediumblob');
  465. $expectedStruct['t_longblob'] = array('type'=>'longblob');
  466. $expectedStruct['t_geometry'] = array('type'=>'geometry');
  467. $expectedStruct['t_point'] = array('type'=>'point');
  468. $expectedStruct['t_linestring'] = array('type'=>'linestring');
  469. $expectedStruct['t_polygon'] = array('type'=>'polygon');
  470. $expectedStruct['t_multipoint'] = array('type'=>'multipoint');
  471. $expectedStruct['t_multilinestring'] = array('type'=>'multilinestring');
  472. $expectedStruct['t_multipolygon'] = array('type'=>'multipolygon');
  473. $expectedStruct['t_geometrycollection'] = array('type'=>'geometrycollection');
  474. $sqlCreate = $pdo->showCreateStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  475. DBG::_(true, true, "fixedEpectedStruct", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  476. $pdo->assertTableStruct('_test_table', $expectedStruct, array('char_encoding'=>'latin2'));
  477. {// force - drop/create
  478. $pdo->exec("DROP TABLE IF EXISTS _test_table");
  479. $pdo->exec($sqlCreate);
  480. }
  481. }
  482. if(0){// TEST nulls
  483. $sth = $pdo->prepare("DROP TABLE IF EXISTS CRM_NOTIFY");
  484. $sth->execute();
  485. $sth = $pdo->prepare("
  486. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  487. `who` varchar(20)
  488. , `when` varchar(255) NOT NULL
  489. , `what` varchar(255) DEFAULT ''
  490. , `t1` varchar(255) NOT NULL -- same as with DEFAULT ''
  491. , `t2` varchar(255) NOT NULL DEFAULT ''
  492. , `t2z` varchar(255) NOT NULL DEFAULT '0'
  493. -- , `t3` varchar(255) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  494. , `t4` varchar(255) NULL -- same as with DEFAULT NULL
  495. , `t5` varchar(255) NULL DEFAULT ''
  496. , `t6` varchar(255) NULL DEFAULT NULL
  497. , `i1` int(11) NOT NULL -- same as with DEFAULT 0
  498. , `i2` int(11) NOT NULL DEFAULT 0
  499. -- , `i3` int(11) NOT NULL DEFAULT NULL -- SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value
  500. , `i4` int(11) NULL -- same as with DEFAULT NULL
  501. , `i5` int(11) NULL DEFAULT 0
  502. , `i6` int(11) NULL DEFAULT NULL
  503. , `last_exec_time` datetime DEFAULT NULL
  504. , KEY `who` (`who`)
  505. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  506. ");
  507. $sth->execute();
  508. $sth = $pdo->prepare("insert into CRM_NOTIFY (`last_exec_time`) values (NULL)");
  509. $sth->execute();
  510. // INSERT INTO CRM_NOTIFY (t1, t2, t2z, t4, t5, t6, i1, i2, i4, i5, i6, last_exec_time)
  511. // VALUES ('', '', '0', NULL, '', NULL, 0, 0, NULL, 0, NULL, NULL);
  512. $struct = $pdo->getTableStruct('CRM_NOTIFY');
  513. DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  514. DBG::_(true, true, "`t2`: is_null(struct[4]['Default'])=(".is_null($struct[4]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  515. DBG::_(true, true, "`t4`: is_null(struct[6]['Default'])=(".is_null($struct[6]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  516. DBG::_(true, true, "`t5`: is_null(struct[7]['Default'])=(".is_null($struct[7]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  517. DBG::_(true, true, "`t6`: is_null(struct[8]['Default'])=(".is_null($struct[8]['Default']).")", null, __CLASS__, __FUNCTION__, __LINE__);
  518. $expectedStruct = array();
  519. $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null);
  520. //$expectedStruct['t2'] = array('type'=>'int', 'num_precision'=>11, 'default_value'=>null);
  521. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct);
  522. }
  523. }
  524. public function reinstall($force = false) {
  525. $sqlList = array();
  526. $pdo = DB::getPDO();
  527. $sqlCreate = <<<EOF_SQL_CREATE
  528. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  529. `who` varchar(20) NOT NULL
  530. , `when` varchar(255) NOT NULL
  531. , `what` varchar(255) NOT NULL
  532. , `last_exec_time` datetime DEFAULT NULL
  533. , `_created` datetime NOT NULL
  534. , UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  535. , KEY `key_who` (`who`)
  536. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  537. EOF_SQL_CREATE;
  538. /*
  539. `CRM_NOTIFY`:
  540. - who - user login
  541. - when - shedule (once a day, once on houd)
  542. - what - action type
  543. - last_exec_time
  544. */
  545. if($force){
  546. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  547. $pdo->exec("
  548. CREATE TABLE IF NOT EXISTS CRM_NOTIFY (
  549. `who` varchar(20) NOT NULL
  550. , `when` varchar(255) NOT NULL
  551. , `what` varchar(255) NOT NULL DEFAULT ''
  552. , `last_exec_time` datetime
  553. , KEY `key_who` (`who`)
  554. , UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  555. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  556. ");
  557. }
  558. {// assertTableStruct CRM_NOTIFY
  559. $expectedStruct = array();
  560. $expectedStruct['who'] = array('type'=>'varchar', 'max_length'=>20, 'is_nullable'=>false);// `who` varchar(20) -- TODO: NOT NULL
  561. $expectedStruct['when'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `when` varchar(255) NOT NULL
  562. $expectedStruct['what'] = array('type'=>'varchar', 'max_length'=>255, 'is_nullable'=>false);// `what` varchar(255) NOT NULL
  563. $expectedStruct['last_exec_time'] = array('type'=>'datetime', 'is_nullable'=>true);// `last_exec_time` datetime
  564. $expectedStruct['_created'] = array('type'=>'datetime', 'is_nullable'=>false);// `_created` datetime
  565. $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  566. $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  567. DBG::_(true, true, "sqlCreate - raw", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  568. $sqlCreate = $pdo->showCreateStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  569. DBG::_(true, true, "sqlCreate - generated", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
  570. $pdo->assertTableStruct('CRM_NOTIFY', $expectedStruct, array('char_encoding'=>'latin2'));
  571. if(0){// force - drop/create
  572. $pdo->exec("DROP TABLE IF EXISTS CRM_NOTIFY");
  573. $pdo->exec($sqlCreate);
  574. }
  575. }
  576. {// assertTableStruct CRM_NOTIFY_EXEC_LOG
  577. // TODO: create table CRM_NOTIFY_EXEC_LOG with action log
  578. }
  579. //$this->reinstallFunctions();
  580. }
  581. }