superedit-AHMES_MNP.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * @param $_GET['task']
  4. * @param $_GET['id_mnp']
  5. *
  6. * if $_GET['task'] == 'create_koresp_out'
  7. * @require $_GET['id_mnp']
  8. *
  9. * @requires TYPESPECIALS:
  10. * [6958] TYPESPECIAL MNP wyślij koresp. link
  11. * [6959] PARAM_OUT Wyślij koresp.
  12. * <a href="index.php?MENU_INIT=AHMES_MNP&task=create_koresp_out&id_mnp={ID_MNP}" target="_blank">Wyślij koresp.</a>
  13. * [6960] (ALIAS DO [6929]) DANE ID_MNP
  14. * [6961] (ALIAS DO [6962]) PARAM_IN ADD_KORESP_OUT_LINK in MNP
  15. *
  16. */
  17. function AHMES_MNP() {
  18. Lib::loadClass('ProcesHelper');
  19. $tblMnpId = ProcesHelper::getZasobTableID('MNP');
  20. $tblKorespId = ProcesHelper::getZasobTableID('IN7_DZIENNIK_KORESP');
  21. Lib::loadClass('ProcesHelper');
  22. $zasobObj = ProcesHelper::getZasobTableInfo($tblMnpId);
  23. if (!$zasobObj) {
  24. echo '<div class="alert alert-danger">' . "Zasob TABELA ID={$tblMnpId} nie istnieje" . '</div>';
  25. //echo UserActivity::showSimpleList();
  26. return;
  27. }
  28. $korespObj = ProcesHelper::getZasobTableInfo($tblKorespId);
  29. if (!$korespObj) {
  30. echo '<div class="alert alert-danger">' . "Zasob TABELA ID={$tblKorespId} nie istnieje" . '</div>';
  31. //echo UserActivity::showSimpleList();
  32. return;
  33. }
  34. //UserActivity::add($tblMnpId);
  35. $userAcl = User::getAcl();
  36. $userAcl->fetchGroups();
  37. if (!$userAcl->hasTableAcl($zasobObj->ID)) {
  38. echo '<div class="alert alert-danger">' . "Brak uprawnień do tabeli ID={$zasobObj->ID}" . '</div>';
  39. //echo UserActivity::showSimpleList();
  40. return;
  41. }
  42. if (!$userAcl->hasTableAcl($korespObj->ID)) {
  43. echo '<div class="alert alert-danger">' . "Brak uprawnień do tabeli Korespondencja (ID={$korespObj->ID})" . '</div>';
  44. //echo UserActivity::showSimpleList();
  45. return;
  46. }
  47. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  48. $korespAcl = $userAcl->getTableAcl($korespObj->ID);
  49. $forceTblAclInit = ('1' == V::get('_force', '', $_GET));
  50. $tblAcl->init($forceTblAclInit);
  51. $korespAcl->init($forceTblAclInit);
  52. $module = new Ahmes_Mnp($tblAcl, $korespAcl, $_GET);
  53. $module->run();
  54. }
  55. class Ahmes_Mnp {
  56. private $_args = null;
  57. private $_tbl = null;
  58. private $_acl = null;
  59. private $_zasobID = null;
  60. private $_dataSource = null;
  61. private $_htmlID = '';
  62. public function __construct($tblAcl, $korespAcl, $args) {
  63. $this->_args = $args;
  64. $this->_tbl = $tblAcl->getName();
  65. $this->_acl = $tblAcl;
  66. $this->_korespAcl = $korespAcl;
  67. $this->_zasobID = $tblAcl->getID();
  68. Lib::loadClass('Data_Source');
  69. $this->_dataSource = $tblAcl->getDataSource();
  70. $this->_htmlID = 'Ahmes_Mnp';
  71. }
  72. public function run() {
  73. Lib::loadClass('SE_Layout');
  74. SE_Layout::menu();
  75. $task = V::get('task', '', $this->_args);
  76. switch ($task) {
  77. case 'create_koresp_out':
  78. $id_mnp = V::get('id_mnp', '', $this->_args);
  79. if (!$id_mnp) {
  80. echo '<div class="alert alert-danger">' . "Brak numeru z dziennika pism!" . '</div>';
  81. } else {
  82. $this->taskCreateKorespOut($id_mnp);
  83. }
  84. break;
  85. default:
  86. echo '<div class="alert alert-danger">' . "Brak zadania do wykonania!" . '</div>';
  87. }
  88. }
  89. private function renderError($msg) {
  90. echo '<div class="alert alert-danger">' . $msg . '</div>';
  91. }
  92. private function getMnp($id_mnp) {
  93. $db = DB::getDB();
  94. if (!$db) return false;
  95. $mnp = $db->get_by_id('MNP', $id_mnp);
  96. return $mnp;
  97. }
  98. private function checkMnp($mnp) {
  99. return true;
  100. }
  101. private function taskCreateKorespOut($id_mnp) {
  102. $mnp = $this->getMnp($id_mnp);
  103. if (!$mnp) {
  104. $this->renderError("Brak dostępu do mnp lub rekord nie istnieje");
  105. return;
  106. }
  107. if (!$this->checkMnp($mnp)) {
  108. $this->renderError("Mnp nie może zostać użyty");
  109. return;
  110. }
  111. if ('1' == V::get('frm_sent', '', $_POST)) {
  112. $frmSaved = $this->saveCreateKorespOutForm($mnp, $_POST);
  113. if ($frmSaved) return;
  114. }
  115. $this->renderCreateKorespOutForm($mnp);
  116. }
  117. public function renderCreateKorespOutForm($mnp) {
  118. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  119. $forceFilterInit = array();
  120. foreach ($this->_args as $k => $v) {
  121. if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  122. $fldName = substr($k, 3);
  123. $forceFilterInit[$fldName] = $v;
  124. }
  125. }
  126. $forceFilterInit['K_TYP_KORESP'] = 'OUT';
  127. $forceFilterInit['K_DATA_OTRZYMANEJ_KORESP'] = date('Y-m-d');
  128. $forceFilterInit['K_OD_KOGO'] = $mnp->Klient_name;
  129. $forceFilterInit['OD_KOGO_ADRES'] = $mnp->Klient_address;
  130. $fieldsList = $this->_korespAcl->getFields();
  131. $fieldsList = array_filter($fieldsList, function($vCol) {
  132. return ($vCol['name'] != 'ID');
  133. });
  134. $fieldsList = array_map(function($vCol) {
  135. $vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
  136. return $vCol;
  137. }, $fieldsList);
  138. $cols = array();
  139. foreach ($fieldsList as $kID => $vCol) {
  140. $defaultValue = '';
  141. if (!empty($forceFilterInit[$vCol['name']])) {
  142. $defaultValue = $forceFilterInit[$vCol['name']];
  143. }
  144. // TODO: read from session cache
  145. $cols[$kID] = V::get("f{$kID}", $defaultValue, $_POST);
  146. }
  147. $mnpFldsList = $this->_acl->getFields();
  148. $mnpFldsList = array_filter($mnpFldsList, function($fld) {
  149. $mnpFldsName = array();
  150. $mnpFldsName[] = 'ID';
  151. $mnpFldsName[] = 'DataZgloszenia';
  152. $mnpFldsName[] = 'dla_kogo';
  153. $mnpFldsName[] = 'Klient_name';
  154. $mnpFldsName[] = 'Klient_address';
  155. $mnpFldsName[] = 'Klient_mail';
  156. $mnpFldsName[] = 'Klient_tel';
  157. $mnpFldsName[] = 'data_real';
  158. $mnpFldsName[] = 'uwagi';
  159. if (in_array($fld['name'], $mnpFldsName)) {
  160. return true;
  161. }
  162. return false;
  163. });
  164. Lib::loadClass('SE_Layout');
  165. ?>
  166. <div class="container AjaxFrmHorizontalEdit">
  167. <form class="form-horizontal" action="" method="post">
  168. <input type="hidden" name="frm_sent" value="1">
  169. <fieldset>
  170. <legend>Dodaj korespondencję na podstawie MNP nr <?php echo $mnp->ID; ?></legend>
  171. <blockquote>
  172. <?php foreach ($mnpFldsList as $fldId => $vFld) : ?>
  173. <p><small><b><?php echo $vFld['label']; ?>: </b> <?php echo $mnp->{$vFld['name']}; ?></small></p>
  174. <?php endforeach; ?>
  175. </blockquote>
  176. <?php $tabindex = 0; foreach ($fieldsList as $kID => $vCol) : ?>
  177. <?php if ($this->_korespAcl->isAllowed($kID, 'C')) : ?>
  178. <div class="form-group">
  179. <label class="col-sm-3 control-label" for="<?php echo "f{$kID}"; ?>"><?php echo $vCol['label']; ?>
  180. <i class="glyphicon glyphicon-info-sign frm-help" data-toggle="popover" data-trigger="hover" title="" data-content="<?php echo htmlspecialchars($vCol['opis']); ?>" data-original-title="<?php echo "[{$kID}] {$vCol['name']}"; ?>"></i>
  181. <?php $perms = $this->_korespAcl->getFieldPerms($kID); SE_Layout::hotKeyDBG($perms); ?>
  182. </label>
  183. <div class="col-sm-9">
  184. <?php
  185. $fieldParams = array('appendBack'=>true, 'tabindex'=>(++$tabindex), 'maxGrid'=>8);
  186. echo $this->_korespAcl->showFormItem('C', $kID, "f{$kID}", $cols[$kID], $fieldParams);
  187. ?>
  188. </div>
  189. </div>
  190. <?php endif; ?>
  191. <?php endforeach; ?>
  192. <div class="form-group">
  193. <div class="col-sm-9 col-sm-offset-3">
  194. <button type="submit" class="btn btn-primary" tabindex="<?php echo (++$tabindex); ?>">Dodaj rekord</button>
  195. </div>
  196. </div>
  197. </fieldset>
  198. </form>
  199. </div>
  200. <script>
  201. jQuery(document).ready(function(){
  202. jQuery('textarea').autosize();
  203. jQuery('.frm-help').popover({trigger:'hover'});
  204. });
  205. </script>
  206. <?php
  207. }
  208. public function saveCreateKorespOutForm($mnp, $args) {
  209. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  210. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save('.$mnp->ID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
  211. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_korespAcl);echo'</pre>';}
  212. $dbID = $this->_korespAcl->getDB();
  213. $db = DB::getDB($dbID);
  214. if (!$db) return false;
  215. $tblName = $this->_korespAcl->getName();
  216. $sqlObj = new stdClass();
  217. $fields = $this->_korespAcl->getFields();
  218. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fields);echo'</pre>';}
  219. foreach ($fields as $kID => $vField) {
  220. if (!$this->_korespAcl->isAllowed($kID, 'C')) {
  221. continue;
  222. }
  223. if (array_key_exists("f{$kID}", $args)) {
  224. $sqlObj->{$vField['name']} = $args["f{$kID}"];
  225. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  226. $sqlObj->{$vField['name']} = $this->_korespAcl->fixEmptyValueFromUser($kID);
  227. }
  228. }
  229. }
  230. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
  231. $retID = $db->ADD_NEW_OBJ($tblName, $sqlObj);
  232. if ($retID > 0) {
  233. ?>
  234. <div class="alert alert-success">
  235. Utworzono pomyślnie rekord Nr <?php echo $retID; ?>
  236. </div>
  237. <?php
  238. return true;
  239. }
  240. ?>
  241. <div class="alert alert-danger">
  242. Wystąpił błąd podczas tworzenia rekordu
  243. <?php
  244. if ($db->has_errors()) {
  245. $outArr = array();
  246. $errorsSql = $db->get_errors();
  247. foreach ($errorsSql as $vErr) {
  248. if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
  249. $vErr = substr($vErr, 18);
  250. // Duplicate entry '123456-1' for key 'P_NIP'
  251. if (substr($vErr, 0, 16) == 'Duplicate entry ') {
  252. }
  253. }
  254. $outArr[] = $vErr;
  255. }
  256. echo implode('<br>', $outArr);
  257. }
  258. ?>
  259. </div>
  260. <?php
  261. return false;
  262. }
  263. public function ___sendAjaxCreateSave($args) {
  264. header("Content-type: application/json");
  265. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  266. sleep(1);// TODO: RMME DBG loading
  267. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save ID(' . $id . ') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
  268. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_acl);echo'</pre>';}
  269. $dbID = $this->_acl->getDB();
  270. $db = DB::getDB($dbID);
  271. if (!$db) {
  272. header('HTTP/1.0 406 Not Acceptable');
  273. echo '{"type":"ERROR", "msg": "' . "Błąd połączenia z bazą danych!" . '"}';
  274. exit;
  275. }
  276. $tblName = $this->_acl->getName();
  277. $sqlObj = new stdClass();
  278. $fields = $this->_acl->getFields();
  279. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fields);echo'</pre>';}
  280. foreach ($fields as $kID => $vField) {
  281. if (!$this->_acl->isAllowed($kID, 'C')) {
  282. continue;
  283. }
  284. if (array_key_exists("f{$kID}", $args)) {
  285. $sqlObj->{$vField['name']} = $args["f{$kID}"];
  286. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  287. $sqlObj->{$vField['name']} = $this->_acl->fixEmptyValueFromUser($kID);
  288. }
  289. }
  290. }
  291. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
  292. $retID = $db->ADD_NEW_OBJ($tblName, $sqlObj);
  293. $retJson = new stdClass();
  294. $retJson->type = '';
  295. $retJson->msg = '';
  296. if ($retID > 0) {
  297. $retJson->type = 'SUCCESS';
  298. $retJson->msg = "Utworzono pomyślnie rekord: ID = {$retID}";
  299. $retJson->id = $retID;
  300. } else {
  301. header('HTTP/1.0 404 Not Found');
  302. $retJson->type = 'ERROR';
  303. $retJson->msg = "";
  304. if ($db->has_errors()) {
  305. $outArr = array();
  306. $errorsSql = $db->get_errors();
  307. foreach ($errorsSql as $vErr) {
  308. if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
  309. $vErr = substr($vErr, 18);
  310. // Duplicate entry '123456-1' for key 'P_NIP'
  311. if (substr($vErr, 0, 16) == 'Duplicate entry ') {
  312. }
  313. }
  314. $outArr[] = $vErr;
  315. }
  316. $retJson->msg .= implode('<br>', $outArr);
  317. }
  318. }
  319. echo json_encode($retJson);
  320. exit;
  321. }
  322. }