PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/modules/bibliography/item_barcode_generator.php

https://gitlab.com/mucill/sman7
PHP | 302 lines | 220 code | 15 blank | 67 comment | 43 complexity | 27befeac8939d7c229eb00dffcf242bb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2013 Arie Nugraha (dicarve@yahoo.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /* Item barcode print */
  21. // key to authenticate
  22. define('INDEX_AUTH', '1');
  23. // main system configuration
  24. require '../../../sysconfig.inc.php';
  25. // IP based access limitation
  26. require LIB.'ip_based_access.inc.php';
  27. do_checkIP('smc');
  28. do_checkIP('smc-bibliography');
  29. // start the session
  30. require SB.'admin/default/session.inc.php';
  31. require SIMBIO.'simbio_GUI/table/simbio_table.inc.php';
  32. require SIMBIO.'simbio_GUI/form_maker/simbio_form_table_AJAX.inc.php';
  33. require SIMBIO.'simbio_GUI/paging/simbio_paging.inc.php';
  34. require SIMBIO.'simbio_DB/datagrid/simbio_dbgrid.inc.php';
  35. require SIMBIO.'simbio_DB/simbio_dbop.inc.php';
  36. // privileges checking
  37. $can_read = utility::havePrivilege('bibliography', 'r');
  38. if (!$can_read) {
  39. die('<div class="errorBox">'.__('You are not authorized to view this section').'</div>');
  40. }
  41. $max_print = 50;
  42. /* RECORD OPERATION */
  43. if (isset($_POST['itemID']) AND !empty($_POST['itemID']) AND isset($_POST['itemAction'])) {
  44. if (!$can_read) {
  45. die();
  46. }
  47. if (!is_array($_POST['itemID'])) {
  48. // make an array
  49. $_POST['itemID'] = array((integer)$_POST['itemID']);
  50. }
  51. // loop array
  52. if (isset($_SESSION['barcodes'])) {
  53. $print_count = count($_SESSION['barcodes']);
  54. } else {
  55. $print_count = 0;
  56. }
  57. // barcode size
  58. $size = 2;
  59. // create AJAX request
  60. echo '<script type="text/javascript" src="'.JWB.'jquery.js"></script>';
  61. echo '<script type="text/javascript">';
  62. // loop array
  63. foreach ($_POST['itemID'] as $itemID) {
  64. if ($print_count == $max_print) {
  65. $limit_reach = true;
  66. break;
  67. }
  68. if (isset($_SESSION['barcodes'][$itemID])) {
  69. continue;
  70. }
  71. if (!empty($itemID)) {
  72. $barcode_text = trim($itemID);
  73. /* replace space */
  74. $barcode_text = str_replace(array(' ', '/', '\/'), '_', $barcode_text);
  75. /* replace invalid characters */
  76. $barcode_text = str_replace(array(':', ',', '*', '@'), '', $barcode_text);
  77. // send ajax request
  78. echo 'jQuery.ajax({ url: \''.SWB.'lib/phpbarcode/barcode.php?code='.$itemID.'&encoding='.$sysconf['barcode_encoding'].'&scale='.$size.'&mode=png&act=save\', type: \'GET\', error: function() { alert(\'Error creating barcode!\'); } });'."\n";
  79. // add to sessions
  80. $_SESSION['barcodes'][$itemID] = $itemID;
  81. $print_count++;
  82. }
  83. }
  84. echo 'top.$(\'#queueCount\').html(\''.$print_count.'\')';
  85. echo '</script>';
  86. // update print queue count object
  87. sleep(2);
  88. if (isset($limit_reach)) {
  89. $msg = str_replace('{max_print}', $max_print, __('Selected items NOT ADDED to print queue. Only {max_print} can be printed at once'));
  90. utility::jsAlert($msg);
  91. } else {
  92. utility::jsAlert(__('Selected items added to print queue'));
  93. }
  94. exit();
  95. }
  96. // clean print queue
  97. if (isset($_GET['action']) AND $_GET['action'] == 'clear') {
  98. // update print queue count object
  99. echo '<script type="text/javascript">top.$(\'#queueCount\').html(\'0\');</script>';
  100. utility::jsAlert(__('Print queue cleared!'));
  101. unset($_SESSION['barcodes']);
  102. exit();
  103. }
  104. // barcode pdf download
  105. if (isset($_GET['action']) AND $_GET['action'] == 'print') {
  106. // check if label session array is available
  107. if (!isset($_SESSION['barcodes'])) {
  108. utility::jsAlert(__('There is no data to print!'));
  109. die();
  110. }
  111. if (count($_SESSION['barcodes']) < 1) {
  112. utility::jsAlert(__('There is no data to print!'));
  113. die();
  114. }
  115. // concat all ID together
  116. $item_ids = '';
  117. foreach ($_SESSION['barcodes'] as $id) {
  118. $item_ids .= '\''.$id.'\',';
  119. }
  120. // strip the last comma
  121. $item_ids = substr_replace($item_ids, '', -1);
  122. // send query to database
  123. $item_q = $dbs->query('SELECT b.title, i.item_code FROM item AS i
  124. LEFT JOIN biblio AS b ON i.biblio_id=b.biblio_id
  125. WHERE i.item_code IN('.$item_ids.')');
  126. $item_data_array = array();
  127. while ($item_d = $item_q->fetch_row()) {
  128. if ($item_d[0]) {
  129. $item_data_array[] = $item_d;
  130. }
  131. }
  132. // include printed settings configuration file
  133. require SB.'admin'.DS.'admin_template'.DS.'printed_settings.inc.php';
  134. // check for custom template settings
  135. $custom_settings = SB.'admin'.DS.$sysconf['admin_template']['dir'].DS.$sysconf['template']['theme'].DS.'printed_settings.inc.php';
  136. if (file_exists($custom_settings)) {
  137. include $custom_settings;
  138. }
  139. // load print settings from database to override value from printed_settings file
  140. loadPrintSettings($dbs, 'barcode');
  141. // chunk barcode array
  142. $chunked_barcode_arrays = array_chunk($item_data_array, $sysconf['print']['barcode']['barcode_items_per_row']);
  143. // create html ouput
  144. $html_str = '<!DOCTYPE html>'."\n";
  145. $html_str .= '<html><head><title>Item Barcode Label Print Result</title>'."\n";
  146. $html_str .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  147. $html_str .= '<meta http-equiv="Pragma" content="no-cache" /><meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0" /><meta http-equiv="Expires" content="Sat, 26 Jul 1997 05:00:00 GMT" />';
  148. $html_str .= '<style type="text/css">'."\n";
  149. $html_str .= 'body { padding: 0; margin: 1cm; font-family: '.$sysconf['print']['barcode']['barcode_fonts'].'; font-size: '.$sysconf['print']['barcode']['barcode_font_size'].'pt; background: #fff; }'."\n";
  150. $html_str .= '.labelStyle { width: '.$sysconf['print']['barcode']['barcode_box_width'].'cm; height: '.$sysconf['print']['barcode']['barcode_box_height'].'cm; text-align: center; margin: '.$sysconf['print']['barcode']['barcode_items_margin'].'cm; border: '.$sysconf['print']['barcode']['barcode_border_size'].'px solid #000000;}'."\n";
  151. $html_str .= '.labelHeaderStyle { background-color: #CCCCCC; font-weight: bold; padding: 5px; margin-bottom: 5px; }'."\n";
  152. $html_str .= '</style>'."\n";
  153. $html_str .= '</head>'."\n";
  154. $html_str .= '<body>'."\n";
  155. $html_str .= '<a href="#" onclick="window.print()">Print Again</a>'."\n";
  156. $html_str .= '<table style="margin: 0; padding: 0;" cellspacing="0" cellpadding="0">'."\n";
  157. // loop the chunked arrays to row
  158. foreach ($chunked_barcode_arrays as $barcode_rows) {
  159. $html_str .= '<tr>'."\n";
  160. foreach ($barcode_rows as $barcode) {
  161. $html_str .= '<td valign="top">';
  162. $html_str .= '<div class="labelStyle">';
  163. if ($sysconf['print']['barcode']['barcode_include_header_text']) { $html_str .= '<div class="labelHeaderStyle">'.($sysconf['print']['barcode']['barcode_header_text']?$sysconf['print']['barcode']['barcode_header_text']:$sysconf['library_name']).'</div>'; }
  164. // document title
  165. $html_str .= '<div style="font-size: 7pt;">';
  166. if ($sysconf['print']['barcode']['barcode_cut_title'] && strlen($barcode[0]) > $sysconf['print']['barcode']['barcode_cut_title']) {
  167. $html_str .= substr($barcode[0], 0, $sysconf['print']['barcode']['barcode_cut_title']).'...';
  168. } else { $html_str .= $barcode[0]; }
  169. $html_str .= '</div>';
  170. //~ $html_str .= '<img src="'.SWB.IMG.'/barcodes/'.str_replace(array(' '), '_', $barcode[1]).'.png" style="width: '.$sysconf['print']['barcode']['barcode_scale'].'%;" border="0" />';
  171. $html_str .= '<img src="'.SWB.IMG.'/barcodes/'.urlencode(urlencode($barcode[1])).'.png" style="width: '.$sysconf['print']['barcode']['barcode_scale'].'%;" border="0" />';
  172. $html_str .= '</div>';
  173. $html_str .= '</td>';
  174. }
  175. $html_str .= '<tr>'."\n";
  176. }
  177. $html_str .= '</table>'."\n";
  178. $html_str .= '<script type="text/javascript">self.print();</script>'."\n";
  179. $html_str .= '</body></html>'."\n";
  180. // unset the session
  181. unset($_SESSION['barcodes']);
  182. // write to file
  183. $print_file_name = 'item_barcode_gen_print_result_'.strtolower(str_replace(' ', '_', $_SESSION['uname'])).'.html';
  184. $file_write = @file_put_contents(UPLOAD.$print_file_name, $html_str);
  185. if ($file_write) {
  186. // update print queue count object
  187. echo '<script type="text/javascript">parent.$(\'#queueCount\').html(\'0\');</script>';
  188. // open result in window
  189. echo '<script type="text/javascript">top.$.colorbox({href: "'.SWB.FLS.'/'.$print_file_name.'", iframe: true, width: 800, height: 500, title: "'.__('Item Barcodes Printing').'"})</script>';
  190. } else { utility::jsAlert('ERROR! Item barcodes failed to generate, possibly because '.SB.FLS.' directory is not writable'); }
  191. exit();
  192. }
  193. ?>
  194. <fieldset class="menuBox">
  195. <div class="menuBoxInner printIcon">
  196. <div class="per_title">
  197. <h2><?php echo __('Item Barcodes Printing'); ?></h2>
  198. </div>
  199. <div class="sub_section">
  200. <div class="btn-group">
  201. <a target="blindSubmit" href="<?php echo MWB; ?>bibliography/item_barcode_generator.php?action=clear" class="notAJAX btn btn-default"><i class="glyphicon glyphicon-trash"></i>&nbsp;<?php echo __('Clear Print Queue'); ?></a>
  202. <a target="blindSubmit" href="<?php echo MWB; ?>bibliography/item_barcode_generator.php?action=print" class="notAJAX btn btn-default"><i class="glyphicon glyphicon-print"></i>&nbsp;<?php echo __('Print Barcodes for Selected Data');?></a>
  203. <a href="<?php echo MWB; ?>bibliography/pop_print_settings.php?type=barcode" class="notAJAX btn btn-default openPopUp" title="<?php echo __('Change print barcode settings'); ?>"><i class="glyphicon glyphicon-wrench"></i></a>
  204. </div>
  205. <form name="search" action="<?php echo MWB; ?>bibliography/item_barcode_generator.php" id="search" method="get" style="display: inline;"><?php echo __('Search'); ?> :
  206. <input type="text" name="keywords" size="30" />
  207. <input type="submit" id="doSearch" value="<?php echo __('Search'); ?>" class="btn btn-default" />
  208. </form>
  209. </div>
  210. <div class="infoBox">
  211. <?php
  212. echo __('Maximum').' <font style="color: #f00">'.$max_print.'</font> '.__('records can be printed at once. Currently there is').' ';
  213. if (isset($_SESSION['barcodes'])) {
  214. echo '<font id="queueCount" style="color: #f00">'.count($_SESSION['barcodes']).'</font>';
  215. } else { echo '<font id="queueCount" style="color: #f00">0</font>'; }
  216. echo ' '.__('in queue waiting to be printed.');
  217. ?>
  218. </div>
  219. </div>
  220. </fieldset>
  221. <?php
  222. /* search form end */
  223. // create datagrid
  224. $datagrid = new simbio_datagrid();
  225. /* ITEM LIST */
  226. require SIMBIO.'simbio_UTILS/simbio_tokenizecql.inc.php';
  227. require LIB.'biblio_list_model.inc.php';
  228. // index choice
  229. if ($sysconf['index']['type'] == 'index' || ($sysconf['index']['type'] == 'sphinx' && file_exists(LIB.'sphinx/sphinxapi.php'))) {
  230. if ($sysconf['index']['type'] == 'sphinx') {
  231. require LIB.'sphinx/sphinxapi.php';
  232. require LIB.'biblio_list_sphinx.inc.php';
  233. } else {
  234. require LIB.'biblio_list_index.inc.php';
  235. }
  236. // table spec
  237. $table_spec = 'item LEFT JOIN search_biblio AS `index` ON item.biblio_id=`index`.biblio_id';
  238. $datagrid->setSQLColumn('item.item_code',
  239. 'item.item_code AS \''.__('Item Code').'\'',
  240. 'index.title AS \''.__('Title').'\'');
  241. } else {
  242. require LIB.'biblio_list.inc.php';
  243. // table spec
  244. $table_spec = 'item LEFT JOIN biblio ON item.biblio_id=biblio.biblio_id';
  245. $datagrid->setSQLColumn('item.item_code',
  246. 'item.item_code AS \''.__('Item Code').'\'',
  247. 'biblio.title AS \''.__('Title').'\'');
  248. }
  249. $datagrid->setSQLorder('item.last_update DESC');
  250. // is there any search
  251. if (isset($_GET['keywords']) AND $_GET['keywords']) {
  252. $keywords = $dbs->escape_string(trim($_GET['keywords']));
  253. $searchable_fields = array('title', 'author', 'subject', 'itemcode');
  254. $search_str = '';
  255. // if no qualifier in fields
  256. if (!preg_match('@[a-z]+\s*=\s*@i', $keywords)) {
  257. foreach ($searchable_fields as $search_field) {
  258. $search_str .= $search_field.'='.$keywords.' OR ';
  259. }
  260. } else {
  261. $search_str = $keywords;
  262. }
  263. $biblio_list = new biblio_list($dbs, 20);
  264. $criteria = $biblio_list->setSQLcriteria($search_str);
  265. }
  266. if (isset($criteria)) {
  267. $datagrid->setSQLcriteria('('.$criteria['sql_criteria'].')');
  268. }
  269. // set table and table header attributes
  270. $datagrid->table_attr = 'align="center" id="dataList" cellpadding="5" cellspacing="0"';
  271. $datagrid->table_header_attr = 'class="dataListHeader" style="font-weight: bold;"';
  272. // edit and checkbox property
  273. $datagrid->edit_property = false;
  274. $datagrid->chbox_property = array('itemID', __('Add'));
  275. $datagrid->chbox_action_button = __('Add To Print Queue');
  276. $datagrid->chbox_confirm_msg = __('Add to print queue?');
  277. $datagrid->column_width = array('10%', '85%');
  278. // set checkbox action URL
  279. $datagrid->chbox_form_URL = $_SERVER['PHP_SELF'];
  280. // put the result into variables
  281. $datagrid_result = $datagrid->createDataGrid($dbs, $table_spec, 20, $can_read);
  282. if (isset($_GET['keywords']) AND $_GET['keywords']) {
  283. $msg = str_replace('{result->num_rows}', $datagrid->num_rows, __('Found <strong>{result->num_rows}</strong> from your keywords'));
  284. echo '<div class="infoBox">'.$msg.' : "'.$_GET['keywords'].'"<div>'.__('Query took').' <b>'.$datagrid->query_time.'</b> '.__('second(s) to complete').'</div></div>';
  285. }
  286. echo $datagrid_result;
  287. /* main content end */