Guest User

Untitled

a guest
Dec 4th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. <?php
  2. /**
  3. * Database.php
  4. *
  5. * The Database class is meant to simplify the task of accessing
  6. * information from the website's database.
  7. *
  8. */
  9. include_once("constants.php");
  10. include_once("NewDB.php");
  11.  
  12. class MySQLDB {
  13. // var $connection; //The MySQL database connection
  14. // var $num_active_users; //Number of active users viewing site
  15. // var $num_active_guests; //Number of active guests viewing site
  16. // var $num_members; //Number of signed-up users
  17.  
  18. /* New constuctor */
  19. function __construct() {
  20. // assume something went wrong by default
  21. $return = FALSE;
  22.  
  23. // Keep around the old pg connection for testing
  24. $conn_string = "dbname=".DB_NAME." host=".DB_SERVER." user=".DB_USER." password=".DB_PASS;
  25. $this->connection = pg_connect($conn_string);
  26.  
  27. if ($this->connection == false) {
  28. echo "error connecting to database<br />";
  29. }
  30.  
  31. // Create handle for SSDB collection
  32. $dbh = new NewDB(DB_SERVER, DB_NAME, DB_USER, DB_PASS);
  33.  
  34. // Check for errors, return object
  35. if ($dbh !== FALSE && $dbh_users !== FALSE) {
  36. $this->PDO = $dbh->PDO;
  37. $return = $this;
  38. }
  39.  
  40. return $return;
  41. }
  42. //#######################
  43.  
  44.  
  45. /**
  46. * addUploadFile single upload -- adds a file/username entry to the upload_files table in the SSDB database
  47. **/
  48. function addUploadFileSingle($username, $filename, $localpath, $proposal, $type, $status, $accesscontrol, $filesize, $sitenames)
  49. {
  50. $date = date("Y-m-d");
  51. $destination_path = "../upload/$proposal/";
  52. //$filesize = filesize("$destination_path$filename");
  53.  
  54. $filename = addslashes($filename);
  55. $localpath = addslashes($localpath);
  56.  
  57. $sql = "INSERT INTO ".TBL_UPLOAD_FILES."(id, username, filename, localpath, proposal, type, status, accesscontrol, filesize, sitenames) VALUES (DEFAULT, :username, :filename, :localpath, :proposal, :type, :status, :accesscontrol, :filesize, :sitenames)";
  58.  
  59. $stmt = $this->PDO->prepare($sql);
  60. $stmt->bindParam(':username', $username);
  61. $stmt->bindParam(':filename', $filename);
  62. $stmt->bindParam(':localpath', $localpath);
  63. $stmt->bindParam(':proposal', $proposal);
  64. $stmt->bindParam(':type', $type);
  65. $stmt->bindParam(':status', $status);
  66. $stmt->bindParam(':accesscontrol', $accesscontrol);
  67. $stmt->bindParam(':filesize', $filesize);
  68. $stmt->bindParam(':sitenames', $sitenames);
  69.  
  70. $stmt->execute();
  71.  
  72. $sql = 'SELECT last_value from public.upload_files_id_seq';
  73. $stmt = $this->PDO->prepare($sql);
  74.  
  75. try {
  76. $stmt->execute();
  77. $result = $stmt->fetchAll(PDO::FETCH_ASSOC)[0];
  78. $insert_id = $result['last_value'];
  79. $return = $insert_id;
  80. } catch (PDOException $e) {
  81. $return = $e;
  82. }
  83.  
  84. return $return;
  85.  
  86. }
  87.  
  88.  
  89. /**
  90. * addUploadFile single upload -- adds a file/username entry to the upload_files table in the SSDB database
  91. **/
  92. function editUploadFileSingle($id, $accesscontrol, $sitenames)
  93. {
  94. # make a copy in history
  95. $sql = "INSERT INTO upload_files_history(id, username, filename, localpath, proposal, type, status, accesscontrol, filesize, upload_date, submit_date,qc_id, sitenames) SELECT id, username, filename, localpath, proposal, type, status, accesscontrol::accesscontrol, filesize, upload_date, submit_date,qc_id, sitenames from upload_files WHERE id = :id";
  96.  
  97. $stmt = $this->PDO->prepare($sql);
  98. $stmt->bindParam(':id', $id);
  99. $stmt->execute();
  100.  
  101. $date = date("Y-m-d");
  102. $destination_path = "../upload/$proposal/";
  103. //$filesize = filesize("$destination_path$filename");
  104.  
  105. $filename = addslashes($filename);
  106. $localpath = addslashes($localpath);
  107.  
  108. $sql = "UPDATE ".TBL_UPLOAD_FILES." SET accesscontrol = :accesscontrol, sitenames = :sitenames WHERE id = :id";
  109.  
  110. $stmt = $this->PDO->prepare($sql);
  111. $stmt->bindParam(':accesscontrol', $accesscontrol);
  112. $stmt->bindParam(':sitenames', $sitenames);
  113. $stmt->bindParam(':id', $id);
  114.  
  115. try {
  116. $stmt->execute();
  117. } catch (PDOException $e) {
  118. $return = $e;
  119. }
  120.  
  121. return $return;
  122. }
  123.  
  124.  
  125. /**
  126. * getAllDataPackagesArray - Return all of the data packages in SSDB, so
  127. * they can be sorted/filtered by the caller
  128. *
  129. * @param (none)
  130. *
  131. * @returns allDataPackagesArray
  132.  
  133. **/
  134. function getAllDataPackagesArray( )
  135. {
  136. $dataPackageArray = [];
  137.  
  138. // set up the SQL query
  139. $sql = <<<SQL
  140. SELECT * FROM package
  141. ORDER BY package.date DESC;
  142. SQL;
  143. $stmt = $this->PDO->prepare($sql);
  144.  
  145. try {
  146. $ex_status = $stmt->execute();
  147. if ( $ex_status === TRUE ) {
  148. $dataPackageArray = $stmt->fetchAll();
  149. }
  150. } catch (PDOException $e) {
  151. error_log($e);
  152. return FALSE;
  153. }
  154.  
  155. // return either the empty array (if there were no data packages), or
  156. // whatever data packages are in the database
  157. return $dataPackageArray;
  158. }
  159.  
  160.  
  161. /**
  162. * getAllUploadedFiles - returns an array of files from the
  163. * 'upload_files' table for a given proposal ID
  164. *
  165. * @param proposal_id
  166. * @param file_status (optional)
  167. * @param order_by (optional)
  168. *
  169. * @returns array uploadedFiles
  170. *
  171. **/
  172. function getAllUploadedFiles(
  173. $proposal_id = P918,
  174. $file_type = Documents
  175. )
  176. {
  177. // assume failure
  178. $return = FALSE;
  179.  
  180. // no proposal ID, return
  181. if ( $proposal_id === NULL )
  182. return $return;
  183.  
  184. $sql = "SELECT * from " . TBL_UPLOAD_FILES
  185. . " WHERE proposal = :proposal_id ";
  186.  
  187. if ( $file_type !== NULL )
  188. $sql .= "AND type = :file_type ";
  189.  
  190. # "prepare" the SQL query
  191. $stmt = $this->PDO->prepare($sql);
  192. $stmt->bindParam(':proposal_id', $proposal_id);
  193. if ( $file_type !== NULL )
  194. $stmt->bindParam(':file_type', $file_type);
  195.  
  196. try {
  197. $ex_status = $stmt->execute();
  198. if ( $ex_status === TRUE ) {
  199. $fetchedArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
  200. // return the array if there is one, otherwise, return an empty
  201. // array
  202. if ( count($fetchedArray) > 0 ) {
  203. return $fetchedArray;
  204. } else {
  205. return [];
  206. }
  207. }
  208. } catch (PDOException $e) {
  209. error_log($e);
  210. return FALSE;
  211. }
  212.  
  213. // we should only get here if the database query failed for whatever
  214. // reason, or there are no package records in the database
  215. return $return;
  216. }
  217.  
  218.  
  219. function runMyQuery()
  220. {
  221. $dataPackageArray = [];
  222.  
  223. // set up the SQL query
  224. $sql = <<<SQL
  225. SELECT COUNT(proposal) FROM upload_files
  226. WHERE proposal = 'P918' AND type = 'SeismicMultiChannel';
  227. SQL;
  228. $stmt = $this->PDO->prepare($sql);
  229.  
  230. try {
  231. $ex_status = $stmt->execute();
  232. if ( $ex_status === TRUE ) {
  233. $dataPackageArray = $stmt->fetchAll();
  234. }
  235. } catch (PDOException $e) {
  236. error_log($e);
  237. return FALSE;
  238. }
  239.  
  240. // return either the empty array (if there were no data packages), or
  241. // whatever data packages are in the database
  242. return $dataPackageArray;
  243. }
  244.  
  245.  
  246. // Helen's workspace
  247.  
  248. function getProposalList()
  249. {
  250. $dataPackageArray = [];
  251. $sql1 = <<<SQL
  252. SELECT DISTINCT(proposal) FROM upload_files
  253. WHERE proposal LIKE 'P___' ORDER BY proposal
  254. SQL;
  255. $stmt = $this->PDO->prepare($sql1);
  256. try {
  257. $ex_status = $stmt->execute();
  258. if ( $ex_status === TRUE ) {
  259. $dataPackageArray = $stmt->fetchAll();
  260. }
  261. } catch (PDOException $e) {
  262. error_log($e);
  263. return FALSE;
  264. }
  265. return $dataPackageArray;
  266. }
  267.  
  268.  
  269. function getTypeList()
  270. {
  271. $dataPackageArray = [];
  272. $sql2 = <<<SQL
  273. SELECT DISTINCT(type) FROM upload_files
  274. SQL;
  275. $stmt = $this->PDO->prepare($sql2);
  276. try {
  277. $ex_status = $stmt->execute();
  278. if ( $ex_status === TRUE ) {
  279. $dataPackageArray = $stmt->fetchAll();
  280. }
  281. } catch (PDOException $e) {
  282. error_log($e);
  283. return FALSE;
  284. }
  285. return $dataPackageArray;
  286. }
  287.  
  288. function countsEachType() {
  289. $typeCounts = [];
  290.  
  291. $sql2 = <<<SQL
  292. SELECT type, count(type) from upload_files where type<>'' group by type
  293. SQL;
  294. $stmt = $this->PDO->prepare($sql2);
  295. try {
  296. $ex_status = $stmt->execute();
  297. if ( $ex_status === TRUE ) {
  298. $typeCounts = $stmt->fetchAll();
  299. }
  300. } catch (PDOException $e) {
  301. error_log($e);
  302. return FALSE;
  303. }
  304. return $typeCounts;
  305. }
  306.  
  307. function typeCountByProposal() {
  308. $counts = [];
  309.  
  310. $sql2 = <<<SQL
  311. SELECT proposal, type, COUNT(type)
  312. FROM (
  313. SELECT proposal, type
  314. FROM upload_files
  315. WHERE proposal<>'' AND proposal LIKE 'P___'
  316. ORDER BY proposal, type
  317. ) as results
  318. GROUP BY proposal, type
  319. SQL;
  320. $stmt = $this->PDO->prepare($sql2);
  321. try {
  322. $ex_status = $stmt->execute();
  323. if ( $ex_status === TRUE ) {
  324. $counts = $stmt->fetchAll();
  325. }
  326. } catch (PDOException $e) {
  327. error_log($e);
  328. return FALSE;
  329. }
  330. return $counts;
  331. }
  332.  
  333.  
  334. function runQueryTest(
  335. $proposal_id = array("P913", "P915", "P918"),
  336. $type_id = LocationMaps
  337. )
  338. {
  339. $dataPackageArray = [];
  340. $sql = "SELECT COUNT(*) FROM " . TBL_UPLOAD_FILES
  341. . " WHERE proposal = :proposal_id "
  342. . " AND type = :type_id ";
  343.  
  344. // prepare the SQL query
  345.  
  346. $stmt = $this->PDO->prepare($sql);
  347.  
  348. $i = 0;
  349. while ($i < count($proposal_id)) {
  350. $stmt->bindParam(':proposal_id', $proposal_id[$i]);
  351. $i += 1;
  352. }
  353. $stmt->bindParam(':type_id', $type_id);
  354. $stmt->execute();
  355. $dataPackageArray = $stmt->fetchAll();
  356.  
  357. return $dataPackageArray;
  358.  
  359. }
  360.  
  361.  
  362.  
  363. };
  364.  
  365. /* Create database connection */
  366. $database = new MySQLDB;
  367. ?>
Add Comment
Please, Sign In to add comment