PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/modules/knowhow/export.php

https://github.com/wynerst/New-Knowhow
PHP | 183 lines | 125 code | 12 blank | 46 comment | 21 complexity | b9bda9344491b3b80b156babe00715a8 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2007,2008 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 2 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. /* Biblio data export section */
  21. // key to authenticate
  22. define('INDEX_AUTH', '1');
  23. // main system configuration
  24. require '../../../sysconfig.inc.php';
  25. // start the session
  26. require SENAYAN_BASE_DIR.'admin/default/session.inc.php';
  27. require SIMBIO_BASE_DIR.'simbio_GUI/table/simbio_table.inc.php';
  28. require SIMBIO_BASE_DIR.'simbio_GUI/form_maker/simbio_form_table_AJAX.inc.php';
  29. require SIMBIO_BASE_DIR.'simbio_DB/simbio_dbop.inc.php';
  30. // privileges checking
  31. $can_read = utility::havePrivilege('bibliography', 'r');
  32. $can_write = utility::havePrivilege('bibliography', 'w');
  33. if (!$can_read) {
  34. die('<div class="errorBox">'.__('You are not authorized to view this section').'</div>');
  35. }
  36. if (isset($_POST['doExport'])) {
  37. // check for form validity
  38. if (empty($_POST['fieldSep']) OR empty($_POST['fieldEnc'])) {
  39. utility::jsAlert(__('Required fields (*) must be filled correctly!'));
  40. exit();
  41. } else {
  42. // set PHP time limit
  43. set_time_limit(0);
  44. // create local function to fetch values
  45. function getValues($obj_db, $str_query)
  46. {
  47. // make query from database
  48. $_value_q = $obj_db->query($str_query);
  49. if ($_value_q->num_rows > 0) {
  50. $_value_buffer = '';
  51. while ($_value_d = $_value_q->fetch_row()) {
  52. if ($_value_d[0]) {
  53. $_value_buffer .= '<'.$_value_d[0].'>';
  54. }
  55. }
  56. return $_value_buffer;
  57. }
  58. return null;
  59. }
  60. // limit
  61. $sep = trim($_POST['fieldSep']);
  62. $encloser = trim($_POST['fieldEnc']);
  63. $limit = intval($_POST['recordNum']);
  64. $offset = intval($_POST['recordOffset']);
  65. if ($_POST['recordSep'] === 'NEWLINE') {
  66. $rec_sep = "\n";
  67. } else if ($_POST['recordSep'] === 'RETURN') {
  68. $rec_sep = "\r";
  69. } else {
  70. $rec_sep = trim($_POST['recordSep']);
  71. }
  72. // fetch all data from biblio table
  73. $sql = "SELECT
  74. b.biblio_id, b.title, gmd.gmd_name, b.edition,
  75. b.isbn_issn, publ.publisher_name, b.publish_year,
  76. b.collation, b.series_title, b.call_number,
  77. lang.language_name, pl.place_name, b.classification,
  78. b.notes, b.image, b.file_att
  79. FROM biblio AS b
  80. LEFT JOIN mst_gmd AS gmd ON b.gmd_id=gmd.gmd_id
  81. LEFT JOIN mst_publisher AS publ ON b.publisher_id=publ.publisher_id
  82. LEFT JOIN mst_language AS lang ON b.language_id=lang.language_id
  83. LEFT JOIN mst_place AS pl ON b.publish_place_id=pl.place_id ";
  84. if ($limit > 0) { $sql .= ' LIMIT '.$limit; }
  85. if ($offset > 1) {
  86. if ($limit > 0) {
  87. $sql .= ' OFFSET '.($offset-1);
  88. } else {
  89. $sql .= ' LIMIT '.($offset-1).',99999999999';
  90. }
  91. }
  92. // for debugging purpose only
  93. // die($sql);
  94. $all_data_q = $dbs->query($sql);
  95. if ($dbs->error) {
  96. utility::jsAlert(__('Error on query to database, Export FAILED!'));
  97. } else {
  98. if ($all_data_q->num_rows > 0) {
  99. header('Content-type: text/plain');
  100. header('Content-Disposition: attachment; filename="senayan_biblio_export.csv"');
  101. while ($biblio_d = $all_data_q->fetch_row()) {
  102. $buffer = null;
  103. foreach ($biblio_d as $idx => $fld_d) {
  104. // skip biblio_id field
  105. if ($idx > 0) {
  106. $fld_d = $dbs->escape_string($fld_d);
  107. // data
  108. $buffer .= stripslashes($encloser.$fld_d.$encloser);
  109. // field separator
  110. $buffer .= $sep;
  111. }
  112. }
  113. // authors
  114. $authors = getValues($dbs, 'SELECT a.author_name FROM biblio_author AS ba
  115. LEFT JOIN mst_author AS a ON ba.author_id=a.author_id
  116. WHERE ba.biblio_id='.$biblio_d[0]);
  117. $buffer .= $encloser.$authors.$encloser;
  118. $buffer .= $sep;
  119. // topics
  120. $topics = getValues($dbs, 'SELECT t.topic FROM biblio_topic AS bt
  121. LEFT JOIN mst_topic AS t ON bt.topic_id=t.topic_id
  122. WHERE bt.biblio_id='.$biblio_d[0]);
  123. $buffer .= $encloser.$topics.$encloser;
  124. $buffer .= $sep;
  125. // item code
  126. $items = getValues($dbs, 'SELECT item_code FROM item AS i
  127. WHERE i.biblio_id='.$biblio_d[0]);
  128. $buffer .= $encloser.$items.$encloser;
  129. echo $buffer;
  130. echo $rec_sep;
  131. }
  132. exit();
  133. } else {
  134. utility::jsAlert(__('There is no record in bibliographic database yet, Export FAILED!'));
  135. }
  136. }
  137. }
  138. exit();
  139. }
  140. ?>
  141. <fieldset class="menuBox">
  142. <div class="menuBoxInner exportIcon">
  143. <?php echo __('EXPORT TOOL'); ?>
  144. <hr />
  145. <?php echo __('Export bibliographics data to CSV file'); ?>
  146. </div>
  147. </fieldset>
  148. <?php
  149. // create new instance
  150. $form = new simbio_form_table_AJAX('mainForm', $_SERVER['PHP_SELF'], 'post');
  151. $form->submit_button_attr = 'name="doExport" value="'.__('Export Now').'" class="button"';
  152. // form table attributes
  153. $form->table_attr = 'align="center" id="dataList" cellpadding="5" cellspacing="0"';
  154. $form->table_header_attr = 'class="alterCell" style="font-weight: bold;"';
  155. $form->table_content_attr = 'class="alterCell2"';
  156. /* Form Element(s) */
  157. // field separator
  158. $form->addTextField('text', 'fieldSep', __('Field Separator').'*', ''.htmlentities(',').'', 'style="width: 10%;" maxlength="3"');
  159. // field enclosed
  160. $form->addTextField('text', 'fieldEnc', __('Field Enclosed With').'*', ''.htmlentities('"').'', 'style="width: 10%;"');
  161. // record separator
  162. $rec_sep_options[] = array('NEWLINE', 'NEWLINE');
  163. $rec_sep_options[] = array('RETURN', 'CARRIAGE RETURN');
  164. $form->addSelectList('recordSep', __('Record Separator'), $rec_sep_options);
  165. // number of records to export
  166. $form->addTextField('text', 'recordNum', __('Number of Records To Export (0 for all records)'), '0', 'style="width: 10%;"');
  167. // records offset
  168. $form->addTextField('text', 'recordOffset', __('Start From Record'), '1', 'style="width: 10%;"');
  169. // output the form
  170. echo $form->printOut();
  171. ?>