superedit-AHMES_MNP.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 = new Data_Source($tblAcl->getDB());
  70. $this->_dataSource->setTable($tblAcl->getName());
  71. $realFieldList = $tblAcl->getRealFieldList();
  72. $this->_dataSource->setCols($realFieldList);
  73. $this->_dataSource->setColTypes($tblAcl->getTypes());
  74. $this->_dataSource->setVirtualCols($tblAcl->getVirtualFieldList());
  75. $this->_dataSource->setFieldGroupWrite('A_ADM_COMPANY', $tblAcl->hasFieldType('A_ADM_COMPANY'));
  76. $this->_dataSource->setFieldGroupRead('A_CLASSIFIED', $tblAcl->hasFieldType('A_CLASSIFIED'));
  77. $this->_dataSource->setAccessFltrAllowed(!$tblAcl->hasSuperAccessPerms());
  78. $this->_htmlID = 'Ahmes_Mnp';
  79. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  80. foreach ($adminFields as $vAdmFld) {
  81. if (!in_array($vAdmFld, $realFieldList) && $tblAcl->hasFieldType($vAdmFld)) {
  82. $this->_dataSource->addCol($vAdmFld);
  83. }
  84. }
  85. }
  86. public function run() {
  87. Lib::loadClass('SE_Layout');
  88. SE_Layout::menu();
  89. $task = V::get('task', '', $this->_args);
  90. switch ($task) {
  91. case 'create_koresp_out':
  92. $id_mnp = V::get('id_mnp', '', $this->_args);
  93. if (!$id_mnp) {
  94. echo '<div class="alert alert-danger">' . "Brak numeru z dziennika pism!" . '</div>';
  95. } else {
  96. $this->taskCreateKorespOut($id_mnp);
  97. }
  98. break;
  99. default:
  100. echo '<div class="alert alert-danger">' . "Brak zadania do wykonania!" . '</div>';
  101. }
  102. }
  103. private function renderError($msg) {
  104. echo '<div class="alert alert-danger">' . $msg . '</div>';
  105. }
  106. private function getMnp($id_mnp) {
  107. $db = DB::getDB();
  108. if (!$db) return false;
  109. $mnp = $db->get_by_id('MNP', $id_mnp);
  110. return $mnp;
  111. }
  112. private function checkMnp($mnp) {
  113. return true;
  114. }
  115. private function taskCreateKorespOut($id_mnp) {
  116. $mnp = $this->getMnp($id_mnp);
  117. if (!$mnp) {
  118. $this->renderError("Brak dostępu do mnp lub rekord nie istnieje");
  119. return;
  120. }
  121. if (!$this->checkMnp($mnp)) {
  122. $this->renderError("Mnp nie może zostać użyty");
  123. return;
  124. }
  125. if ('1' == V::get('frm_sent', '', $_POST)) {
  126. $frmSaved = $this->saveCreateKorespOutForm($mnp, $_POST);
  127. if ($frmSaved) return;
  128. }
  129. $this->renderCreateKorespOutForm($mnp);
  130. }
  131. public function renderCreateKorespOutForm($mnp) {
  132. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  133. $forceFilterInit = array();
  134. foreach ($this->_args as $k => $v) {
  135. if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  136. $fldName = substr($k, 3);
  137. $forceFilterInit[$fldName] = $v;
  138. }
  139. }
  140. $forceFilterInit['K_TYP_KORESP'] = 'OUT';
  141. $forceFilterInit['K_DATA_OTRZYMANEJ_KORESP'] = date('Y-m-d');
  142. $forceFilterInit['K_OD_KOGO'] = $mnp->Klient_name;
  143. $forceFilterInit['OD_KOGO_ADRES'] = $mnp->Klient_address;
  144. $fieldsList = $this->_korespAcl->getFields();
  145. $fieldsList = array_filter($fieldsList, function($vCol) {
  146. return ($vCol['name'] != 'ID');
  147. });
  148. $fieldsList = array_map(function($vCol) {
  149. $vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
  150. return $vCol;
  151. }, $fieldsList);
  152. $cols = array();
  153. foreach ($fieldsList as $kID => $vCol) {
  154. $defaultValue = '';
  155. if (!empty($forceFilterInit[$vCol['name']])) {
  156. $defaultValue = $forceFilterInit[$vCol['name']];
  157. }
  158. // TODO: read from session cache
  159. $cols[$kID] = V::get("f{$kID}", $defaultValue, $_POST);
  160. }
  161. $mnpFldsList = $this->_acl->getFields();
  162. $mnpFldsList = array_filter($mnpFldsList, function($fld) {
  163. $mnpFldsName = array();
  164. $mnpFldsName[] = 'ID';
  165. $mnpFldsName[] = 'DataZgloszenia';
  166. $mnpFldsName[] = 'dla_kogo';
  167. $mnpFldsName[] = 'Klient_name';
  168. $mnpFldsName[] = 'Klient_address';
  169. $mnpFldsName[] = 'Klient_mail';
  170. $mnpFldsName[] = 'Klient_tel';
  171. $mnpFldsName[] = 'data_real';
  172. $mnpFldsName[] = 'uwagi';
  173. if (in_array($fld['name'], $mnpFldsName)) {
  174. return true;
  175. }
  176. return false;
  177. });
  178. Lib::loadClass('SE_Layout');
  179. ?>
  180. <div class="container AjaxFrmHorizontalEdit">
  181. <form class="form-horizontal" action="" method="post">
  182. <input type="hidden" name="frm_sent" value="1">
  183. <fieldset>
  184. <legend>Dodaj korespondencję na podstawie MNP nr <?php echo $mnp->ID; ?></legend>
  185. <blockquote>
  186. <?php foreach ($mnpFldsList as $fldId => $vFld) : ?>
  187. <p><small><b><?php echo $vFld['label']; ?>: </b> <?php echo $mnp->{$vFld['name']}; ?></small></p>
  188. <?php endforeach; ?>
  189. </blockquote>
  190. <?php $tabindex = 0; foreach ($fieldsList as $kID => $vCol) : ?>
  191. <?php if ($this->_korespAcl->isAllowed($kID, 'C')) : ?>
  192. <div class="form-group">
  193. <label class="col-sm-3 control-label" for="<?php echo "f{$kID}"; ?>"><?php echo $vCol['label']; ?>
  194. <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>
  195. <?php $perms = $this->_korespAcl->getFieldPerms($kID); SE_Layout::hotKeyDBG($perms); ?>
  196. </label>
  197. <div class="col-sm-9">
  198. <?php
  199. $fieldParams = array('appendBack'=>true, 'tabindex'=>(++$tabindex), 'maxGrid'=>8);
  200. echo $this->_korespAcl->showFormItem('C', $kID, "f{$kID}", $cols[$kID], $fieldParams);
  201. ?>
  202. </div>
  203. </div>
  204. <?php endif; ?>
  205. <?php endforeach; ?>
  206. <div class="form-group">
  207. <div class="col-sm-9 col-sm-offset-3">
  208. <button type="submit" class="btn btn-primary" tabindex="<?php echo (++$tabindex); ?>">Dodaj rekord</button>
  209. </div>
  210. </div>
  211. </fieldset>
  212. </form>
  213. </div>
  214. <script>
  215. jQuery(document).ready(function(){
  216. jQuery('textarea').autosize();
  217. jQuery('.frm-help').popover({trigger:'hover'});
  218. });
  219. </script>
  220. <?php
  221. }
  222. public function saveCreateKorespOutForm($mnp, $args) {
  223. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  224. 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>';}
  225. 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>';}
  226. $dbID = $this->_korespAcl->getDB();
  227. $db = DB::getDB($dbID);
  228. if (!$db) return false;
  229. $tblName = $this->_korespAcl->getName();
  230. $sqlObj = new stdClass();
  231. $fields = $this->_korespAcl->getFields();
  232. 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>';}
  233. foreach ($fields as $kID => $vField) {
  234. if (!$this->_korespAcl->isAllowed($kID, 'C')) {
  235. continue;
  236. }
  237. if (array_key_exists("f{$kID}", $args)) {
  238. $sqlObj->{$vField['name']} = $args["f{$kID}"];
  239. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  240. $sqlObj->{$vField['name']} = $this->_korespAcl->fixEmptyValueFromUser($kID);
  241. }
  242. }
  243. }
  244. 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>';}
  245. $retID = $db->ADD_NEW_OBJ($tblName, $sqlObj);
  246. if ($retID > 0) {
  247. ?>
  248. <div class="alert alert-success">
  249. Utworzono pomyślnie rekord Nr <?php echo $retID; ?>
  250. </div>
  251. <?php
  252. return true;
  253. }
  254. ?>
  255. <div class="alert alert-danger">
  256. Wystąpił błąd podczas tworzenia rekordu
  257. <?php
  258. if ($db->has_errors()) {
  259. $outArr = array();
  260. $errorsSql = $db->get_errors();
  261. foreach ($errorsSql as $vErr) {
  262. if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
  263. $vErr = substr($vErr, 18);
  264. // Duplicate entry '123456-1' for key 'P_NIP'
  265. if (substr($vErr, 0, 16) == 'Duplicate entry ') {
  266. }
  267. }
  268. $outArr[] = $vErr;
  269. }
  270. echo implode('<br>', $outArr);
  271. }
  272. ?>
  273. </div>
  274. <?php
  275. return false;
  276. }
  277. public function ___sendAjaxCreateSave($args) {
  278. header("Content-type: application/json");
  279. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  280. sleep(1);// TODO: RMME DBG loading
  281. 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>';}
  282. 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>';}
  283. $dbID = $this->_acl->getDB();
  284. $db = DB::getDB($dbID);
  285. if (!$db) {
  286. header('HTTP/1.0 406 Not Acceptable');
  287. echo '{"type":"ERROR", "msg": "' . "Błąd połączenia z bazą danych!" . '"}';
  288. exit;
  289. }
  290. $tblName = $this->_acl->getName();
  291. $sqlObj = new stdClass();
  292. $fields = $this->_acl->getFields();
  293. 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>';}
  294. foreach ($fields as $kID => $vField) {
  295. if (!$this->_acl->isAllowed($kID, 'C')) {
  296. continue;
  297. }
  298. if (array_key_exists("f{$kID}", $args)) {
  299. $sqlObj->{$vField['name']} = $args["f{$kID}"];
  300. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  301. $sqlObj->{$vField['name']} = $this->_acl->fixEmptyValueFromUser($kID);
  302. }
  303. }
  304. }
  305. 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>';}
  306. $retID = $db->ADD_NEW_OBJ($tblName, $sqlObj);
  307. $retJson = new stdClass();
  308. $retJson->type = '';
  309. $retJson->msg = '';
  310. if ($retID > 0) {
  311. $retJson->type = 'SUCCESS';
  312. $retJson->msg = "Utworzono pomyślnie rekord: ID = {$retID}";
  313. $retJson->id = $retID;
  314. } else {
  315. header('HTTP/1.0 404 Not Found');
  316. $retJson->type = 'ERROR';
  317. $retJson->msg = "";
  318. if ($db->has_errors()) {
  319. $outArr = array();
  320. $errorsSql = $db->get_errors();
  321. foreach ($errorsSql as $vErr) {
  322. if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
  323. $vErr = substr($vErr, 18);
  324. // Duplicate entry '123456-1' for key 'P_NIP'
  325. if (substr($vErr, 0, 16) == 'Duplicate entry ') {
  326. }
  327. }
  328. $outArr[] = $vErr;
  329. }
  330. $retJson->msg .= implode('<br>', $outArr);
  331. }
  332. }
  333. echo json_encode($retJson);
  334. exit;
  335. }
  336. }