PageRenderTime 128ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/phreeform/pages/main/pre_process.php

http://phreedom.googlecode.com/
PHP | 177 lines | 145 code | 7 blank | 25 comment | 21 complexity | e429d613b9c5f368992bb92b856a5cdc MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  6. // | http://www.PhreeSoft.com |
  7. // +-----------------------------------------------------------------+
  8. // | This program is free software: you can redistribute it and/or |
  9. // | modify it under the terms of the GNU General Public License as |
  10. // | published by the Free Software Foundation, either version 3 of |
  11. // | the License, or any later version. |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  16. // | GNU General Public License for more details. |
  17. // +-----------------------------------------------------------------+
  18. // Path: /modules/phreeform/pages/main/pre_process.php
  19. //
  20. $security_level = validate_user(SECURITY_ID_PHREEFORM);
  21. /************** include page specific files *********************/
  22. require(DIR_FS_WORKING . 'defaults.php');
  23. require(DIR_FS_WORKING . 'functions/phreeform.php');
  24. /************** page specific initialization *************************/
  25. $error = false;
  26. $processed = false;
  27. $search_text = ($_POST['search_text']) ? db_input($_POST['search_text']) : db_input($_GET['search_text']);
  28. if ($search_text == TEXT_SEARCH) $search_text = '';
  29. $action = isset($_GET['action']) ? $_GET['action'] : $_POST['todo'];
  30. if (!$action && $search_text <> '') $action = 'search'; // if enter key pressed and search not blank
  31. $group = isset($_GET['group']) ? $_GET['group'] : false;
  32. $rID = isset($_POST['rowSeq']) ? db_prepare_input($_POST['rowSeq']) : db_prepare_input($_GET['docID']);
  33. $list = isset($_GET['list']) ? $_GET['list'] : $_POST['list'];
  34. $tab = $_GET['tab'];
  35. $groups = build_groups();
  36. // load the sort fields
  37. $_GET['sf'] = $_POST['sort_field'] ? $_POST['sort_field'] : $_GET['sf'];
  38. $_GET['so'] = $_POST['sort_order'] ? $_POST['sort_order'] : $_GET['so'];
  39. /*************** Act on the action request *************************/
  40. switch ($action) {
  41. case 'copy':
  42. case 'rename':
  43. $doc_title = db_prepare_input($_POST['newName']);
  44. $report = get_report_details($rID);
  45. $report->title = $doc_title;
  46. if ($action == 'rename') {
  47. $sql_array = array(
  48. 'doc_title' => $doc_title,
  49. 'last_update' => date('Y-m-d'),
  50. );
  51. db_perform(TABLE_PHREEFORM, $sql_array, 'update', 'id = ' . $rID);
  52. $message = PHREEFORM_RENAME_SUCCESS;
  53. } else {
  54. $result = $db->Execute("select * from " . TABLE_PHREEFORM . " where id = '" . $rID . "'");
  55. $sql_array = array(
  56. 'parent_id' => $result->fields['parent_id'],
  57. 'doc_title' => $doc_title,
  58. 'doc_group' => $report->groupname,
  59. 'doc_ext' => $report->reporttype,
  60. 'security' => $report->security,
  61. 'create_date' => date('Y-m-d'),
  62. );
  63. db_perform(TABLE_PHREEFORM, $sql_array, 'insert');
  64. $rID = db_insert_id();
  65. $message = PHREEFORM_COPY_SUCCESS;
  66. }
  67. $filename = PF_DIR_MY_REPORTS . 'pf_' . $rID;
  68. $output = object_to_xml($report);
  69. if (!$handle = @fopen($filename, 'w')) {
  70. $db->Execute("delete from " . TABLE_PHREEFORM . " where id = " . $rID);
  71. $messageStack->add(sprintf(PHREEFORM_WRITE_ERROR, $filename), 'error');
  72. break;
  73. }
  74. fwrite($handle, $output);
  75. fclose($handle);
  76. $messageStack->add($message, 'success');
  77. break;
  78. case 'export':
  79. $result = $db->Execute("select doc_title from " . TABLE_PHREEFORM . " where id = '" . $rID . "'");
  80. $filename = PF_DIR_MY_REPORTS . 'pf_' . $rID;
  81. $source_filename = str_replace(' ', '', $result->fields['doc_title']);
  82. $source_filename = str_replace('/', '_', $source_filename) . '.xml';
  83. $backup_filename = str_replace(' ', '', $result->fields['doc_title']);
  84. $backup_filename = str_replace('/', '_', $backup_filename) . '.zip';
  85. $dest_dir = DIR_FS_MY_FILES . 'backups/';
  86. if (!class_exists('ZipArchive')) {
  87. $messageStack->add(PHREEFORM_NO_ZIP,'error');
  88. break;
  89. }
  90. $zip = new ZipArchive;
  91. $res = $zip->open($dest_dir . $backup_filename, ZipArchive::CREATE);
  92. if ($res === TRUE) {
  93. $res = $zip->addFromString($source_filename, file_get_contents($filename));
  94. $zip->close();
  95. } else {
  96. $messageStack->add(PHREEFORM_ZIP_ERROR . $dest_dir, 'error');
  97. break;
  98. }
  99. // download file and exit script
  100. $contents = file_get_contents($dest_dir . $backup_filename);
  101. unlink($dest_dir . $backup_filename); // delete zip file in the temp dir
  102. header("Content-type: application/zip");
  103. header("Content-disposition: attachment; filename=" . $backup_filename . "; size=" . strlen($contents));
  104. header('Pragma: cache');
  105. header('Cache-Control: public, must-revalidate, max-age=0');
  106. header('Connection: close');
  107. header('Expires: ' . date('r', time() + 60 * 60));
  108. header('Last-Modified: ' . date('r', time()));
  109. print $contents;
  110. exit();
  111. break;
  112. case 'go_first': $_GET['list'] = 1; $action = 'search'; break;
  113. case 'go_previous': $_GET['list']--; $action = 'search'; break;
  114. case 'go_next': $_GET['list']++; $action = 'search'; break;
  115. case 'go_last': $_GET['list'] = 99999; $action = 'search'; break;
  116. case 'search':
  117. case 'search_reset':
  118. case 'go_page': $action = 'search'; break;
  119. default:
  120. }
  121. /***************** prepare to display templates *************************/
  122. $result = $db->Execute('select id, parent_id, doc_type, doc_title, doc_group, security from ' . TABLE_PHREEFORM . '
  123. order by doc_title, id, parent_id');
  124. $toc_array = array();
  125. $toc_array[-1][] = array('id' => 0, 'doc_type' => '0', 'doc_title' => TEXT_HOME); // home dir
  126. while (!$result->EOF) {
  127. if (security_check($result->fields['security'])) {
  128. $toc_array[$result->fields['parent_id']][] = array(
  129. 'id' => $result->fields['id'],
  130. 'doc_type' => $result->fields['doc_type'],
  131. 'doc_title' => $result->fields['doc_title'],
  132. 'show' => $result->fields['doc_group'] == $tab ? true : false,
  133. );
  134. }
  135. $result->MoveNext();
  136. }
  137. $toggle_list = false;
  138. if ($group) {
  139. $result = $db->Execute("select id from " . TABLE_PHREEFORM . " where doc_group = '" . $group . "'");
  140. if ($result->RecordCount() > 0) $toggle_list = buildToggleList($result->fields['id']);
  141. }
  142. switch ($action) { // figure which detail page to load
  143. case 'search':
  144. case 'view':
  145. $result = html_heading_bar(array(), $_GET['sf'], $_GET['so'], array(' ', TEXT_DOCUMENT_TITLE, TEXT_ACTION));
  146. $list_header = $result['html_code'];
  147. // build the list for the page selected
  148. if (isset($search_text) && $search_text <> '') {
  149. $search_fields = array('doc_title');
  150. $search = ' where ' . implode(' like \'%' . $search_text . '%\' or ', $search_fields) . ' like \'%' . $search_text . '%\'';
  151. } else {
  152. $search = '';
  153. }
  154. $field_list = array('id', 'doc_title', 'doc_ext');
  155. $query_raw = "select " . implode(', ', $field_list) . " from " . TABLE_PHREEFORM . $search;
  156. $query_split = new splitPageResults($_GET['list'], MAX_DISPLAY_SEARCH_RESULTS, $query_raw, $query_numrows);
  157. $query_result = $db->Execute($query_raw);
  158. $div_template = DIR_FS_WORKING . 'pages/main/' . ($id ? 'tab_report.php' : 'tab_folder.php');
  159. break;
  160. case 'home':
  161. default:
  162. $div_template = DIR_FS_WORKING . 'pages/main/tab_home.php';
  163. }
  164. $include_header = true;
  165. $include_footer = true;
  166. $include_tabs = false;
  167. $include_calendar = false;
  168. $include_template = 'template_main.php';
  169. define('PAGE_TITLE', TEXT_REPORTS);
  170. ?>