api.printcard.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Print card management API
  4. */
  5. /**
  6. * Returns print card lister with some controls
  7. *
  8. * @return string
  9. */
  10. const IMG_CARD = 'content/documents/card_print/card_print.jpg';
  11. const IMG_CARD_TEMPLATE = 'content/documents/card_print/card_print_template.jpg';
  12. const PRINT_TEMPLATE = 'config/printablecards.tpl';
  13. function web_PrintCardLister($ids) {
  14. $cards = zb_GetCardByIds($ids);
  15. $cells = wf_TableCell(__('Serial number'));
  16. $cells.= wf_TableCell(__('Price'));
  17. $rows = wf_TableRow($cells, 'row1');
  18. if (!empty($cards)) {
  19. foreach ($cards as $row) {
  20. $cells = wf_TableCell($row['serial']);
  21. $cells.= wf_TableCell($row['cash']);
  22. $rows.= wf_TableRow($cells, 'row3');
  23. }
  24. }
  25. $result = wf_TableBody($rows, '100%', 0);
  26. $idsQuery = http_build_query(array('id' => $ids));
  27. if (file_exists(IMG_CARD)) {
  28. $printcheck = wf_tag('a', false, '', 'href="#" onClick="window.open(\'?module=printcards&action=page&'.$idsQuery.'\',\'checkwindow\',\'scrollbars=yes,width=800,height=600\')"');
  29. $printcheck.= wf_img_sized('skins/icon_edit.gif', __('Page'), 24, 24);
  30. $printcheck.= wf_tag('a', true);
  31. $printcheck.= wf_tag('a', false, '', 'href="#" onClick="window.open(\'?module=printcards&action=print&'.$idsQuery.'\',\'checkwindow\',\'scrollbars=yes,width=800,height=600\')"');
  32. $printcheck.= wf_img_sized('skins/icon_documents.gif', __('Print'), 24, 24);
  33. $printcheck.= wf_tag('a', true);
  34. } else {
  35. $printcheck = wf_Link("?module=printcards&action=setting", wf_img_sized('skins/icon_edit.gif', __('Page'), 24, 24));
  36. $printcheck.= wf_Link("?module=printcards&action=setting", wf_img_sized('skins/icon_documents.gif', __('Page'), 24, 24));
  37. }
  38. $printcheck.= wf_SubmitClassed(true, 'back', 'back', __('Back'));
  39. $result = wf_Form('?module=cards', 'POST', $result.$printcheck, 'glamour');
  40. return ($result);
  41. }
  42. /**
  43. * Returns print card creation form
  44. *
  45. * @return string
  46. */
  47. function web_PrintCardCreateForm() {
  48. $messages = new UbillingMessageHelper();
  49. if (!file_exists(IMG_CARD)) {
  50. return web_UploadFileForm();
  51. }
  52. $image = IMG_CARD;
  53. if (file_exists(IMG_CARD_TEMPLATE)) {
  54. $image = IMG_CARD_TEMPLATE;
  55. }
  56. $sup = wf_tag('sup').'*'.wf_tag('sup', true);
  57. $form = wf_img($image).'<br/>';
  58. $form.= $messages->getStyledMessage(__('Available macroses').__(': <b>{number} {serial} {sum}</b>'), 'info').wf_tag('br/', false);
  59. $printCardData = zb_SelectAllPrintCardData();
  60. $cells = wf_TableCell(__('Parameter'));
  61. $cells.= wf_TableCell(__('Color'));
  62. $cells.= wf_TableCell(__('Font size'));
  63. $cells.= wf_TableCell(__('Top'));
  64. $cells.= wf_TableCell(__('Left'));
  65. $cells.= wf_TableCell(__('Text'));
  66. $rows = wf_TableRow($cells, 'row1');
  67. foreach ($printCardData as $row) {
  68. $cells = wf_TableCell(__($row['title']));
  69. $cells.= wf_TableCell(wf_TextInput('print_card['.$row['field'].'][color]', $sup, $row['color'], false, 12));
  70. $cells.= wf_TableCell(wf_TextInput('print_card['.$row['field'].'][font_size]', $sup, $row['font_size'], false, 3));
  71. $cells.= wf_TableCell(wf_TextInput('print_card['.$row['field'].'][top]', $sup, $row['top'], false, 3));
  72. $cells.= wf_TableCell(wf_TextInput('print_card['.$row['field'].'][left]', $sup, $row['left'], false, 3));
  73. $cells.= wf_TableCell(wf_TextInput('print_card['.$row['field'].'][text]', $sup, $row['text'], true));
  74. $rows.= wf_TableRow($cells, 'row3');
  75. }
  76. $iputs = wf_SubmitClassed(true, 'save', 'save', __('Save'));
  77. $iputs.= wf_SubmitClassed(true, 'delete', 'delete', __('Delete'));
  78. $iputs.= wf_SubmitClassed(true, 'back', 'back', __('Back'));
  79. $rows.= wf_TableRow(wf_TableCell($iputs));
  80. $result = wf_TableBody($rows, '100%', 0);
  81. $form.= wf_Form('', 'POST', $result, '');
  82. return ($form);
  83. }
  84. function web_UploadFileForm() {
  85. $uploadInputs = wf_HiddenInput('upload', 'true');
  86. $uploadInputs.= __('File').' <input id="fileselector" type="file" name="filename" size="10" /><br>';
  87. $uploadInputs.= wf_Submit('Upload');
  88. $uploadForm = '<form action="" method="POST" class="glamour" enctype="multipart/form-data">
  89. '.$uploadInputs.'
  90. </form>
  91. <div style="clear:both;"></div>
  92. ';
  93. return ($uploadForm);
  94. }
  95. function web_UploadFileCopy($tmpName) {
  96. move_uploaded_file($tmpName, IMG_CARD);
  97. }
  98. function web_CreateTemplateCardPrint() {
  99. $printCardData = zb_SelectAllPrintCardData();
  100. $generateCard = new GenerateCard(IMG_CARD);
  101. $generateCard
  102. ->createStringForImage($printCardData)
  103. ->saveImage(IMG_CARD_TEMPLATE);
  104. }
  105. function web_DeleteImege() {
  106. @unlink(IMG_CARD);
  107. @unlink(IMG_CARD_TEMPLATE);
  108. }
  109. function web_PageCard($ids) {
  110. $cardList = web_GenerateImages($ids);
  111. return (web_ParsePrintable($cardList));
  112. }
  113. function web_GenerateImages($ids) {
  114. $filePathList = array();
  115. web_ClearDirForGenerate();
  116. $cards = zb_GetCardByIds($ids);
  117. $printCardDataFormat = web_PrintCardDataFormatForGenerate(zb_SelectAllPrintCardData());
  118. foreach ($cards as $card) {
  119. $printCardData = $printCardDataFormat;
  120. $printCardData['number']['text'] = str_replace('{number}', $card['serial'], @$printCardData['number']['text']);
  121. $printCardData['serial']['text'] = str_replace('{serial}', $card['part'], @$printCardData['serial']['text']);
  122. $printCardData['rating']['text'] = str_replace('{sum}', $card['cash'], @$printCardData['rating']['text']);
  123. $filePath = sprintf('content/documents/card_print/tmp/%s.jpg', $card['serial']);
  124. $generateCard = new GenerateCard(IMG_CARD);
  125. $generateCard
  126. ->createStringForImage($printCardData)
  127. ->saveImage($filePath);
  128. array_push($filePathList, $filePath);
  129. }
  130. return ($filePathList);
  131. }
  132. function web_PrintCardDataFormatForGenerate($printCards) {
  133. $rc = array();
  134. foreach ($printCards as $printCard) {
  135. if (count(array_filter($printCard)) !== count($printCard)) {
  136. continue;
  137. }
  138. $rc[$printCard['field']] = $printCard;
  139. }
  140. return ($rc);
  141. }
  142. function web_ClearDirForGenerate() {
  143. $dir = 'content/documents/card_print/tmp';
  144. removeRmdir($dir);
  145. mkdir($dir);
  146. }
  147. function removeRmdir($dir) {
  148. if (is_dir($dir)) {
  149. $objects = scandir($dir);
  150. foreach ($objects as $object) {
  151. if ($object != "." && $object != "..") {
  152. if (filetype($dir."/".$object) == "dir") {
  153. removeRmdir($dir."/".$object);
  154. } else {
  155. unlink($dir."/".$object);
  156. }
  157. }
  158. }
  159. reset($objects);
  160. rmdir($dir);
  161. }
  162. }
  163. /**
  164. * @return array|string
  165. */
  166. function zb_SelectAllPrintCardData() {
  167. $query = "SELECT * FROM `print_card` ORDER BY `id` ASC";
  168. $allData = simple_queryall($query);
  169. $allData = !empty($allData) ? $allData : array();
  170. return ($allData);
  171. }
  172. /**
  173. * @param $printCardData
  174. */
  175. function zb_SaveCardPrint($printCardData) {
  176. foreach ($printCardData as $key => $row) {
  177. $field = mysql_real_escape_string($key);
  178. $text = mysql_real_escape_string($row['text']);
  179. $fontSize = vf($row['font_size'], 3);
  180. $top = vf($row['top'], 3);
  181. $left = vf($row['left'], 3);
  182. $color = mysql_real_escape_string($row['color']);
  183. if (count(explode('.', $color)) !== 3) {
  184. $color = '0.0.0';
  185. }
  186. $query = sprintf(
  187. "UPDATE `print_card` SET `text` = '%s', `color` = '%s', `font_size` = '%u', `top` = '%u', `left` = '%u' WHERE `field` = '%s'; ",
  188. $text, $color, $fontSize, $top, $left, $field
  189. );
  190. nr_query($query);
  191. log_register(sprintf('UPDATE PrintCard [%s] `%s` `%s` `%u` `%u` `%u`', $field, $text, $color, $fontSize, $top, $left));
  192. }
  193. }
  194. function web_ParsePrintable($cardList, $title = '') {
  195. $data = '';
  196. foreach ($cardList as $card) {
  197. $data.= wf_img($card);
  198. }
  199. if (file_exists(PRINT_TEMPLATE)) {
  200. $template = file_get_contents(PRINT_TEMPLATE);
  201. $template = str_replace('{PAGE_TITLE}', $title, $template);
  202. $result = $template . $data;
  203. } else {
  204. $result = $data;
  205. $result.= wf_tag('body', true);
  206. $result.= wf_tag('html', true);
  207. }
  208. return ($result);
  209. }
  210. function web_CreatePdf($cardList) {
  211. $indentWidth = 15;
  212. $indentHeight = 3;
  213. $paperWidth = 210;
  214. $paperHeight = 297;
  215. $correctiveToMM = 0.265;
  216. $geImageInfo = getimagesize($cardList[0]);
  217. $width = ($geImageInfo[0] + $indentWidth) * $correctiveToMM;
  218. $height = ($geImageInfo[1] + $indentHeight) * $correctiveToMM;
  219. //ob_clean();
  220. $pdf = new FPDF('P', 'mm', 'A4');
  221. $pdf->AddPage();
  222. $pdf->SetFont('Arial', 'B', 16);
  223. $wPos = $indentWidth;
  224. $hPos = $indentHeight;
  225. foreach ($cardList as $card) {
  226. if ($paperHeight < ($hPos + $height)) {
  227. $hPos = $indentHeight;
  228. $wPos+= $width;
  229. }
  230. if ($paperWidth < ($wPos + $width)) {
  231. $pdf->AddPage();
  232. $wPos = $indentWidth;
  233. $hPos = $indentHeight;
  234. }
  235. $pdf->Image($card, $wPos, $hPos);
  236. $hPos += $height;
  237. }
  238. $pdf->Output();
  239. }
  240. ?>