PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/modules/options/database_sqlpatches.php

http://simpleinvoices.googlecode.com/
PHP | 395 lines | 249 code | 99 blank | 47 comment | 28 complexity | d60162368b8ecd39578c466e40e011ef MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0, LGPL-3.0
  1. <?php
  2. //stop the direct browsing to this file - let index.php handle which files get displayed
  3. //checkLogin();
  4. require($include_dir . 'sys/include/sql_patches.php');
  5. // Made it into 2 functions to get rid of the old defaults table
  6. $db = new db();
  7. function getNumberOfDonePatches() {
  8. global $dbh;
  9. $db = new db();
  10. $check_patches_sql = "SELECT max(sql_patch_ref) AS count FROM ".TB_PREFIX."sql_patchmanager ";
  11. $sth = $db->query($check_patches_sql) or die(htmlsafe(end($dbh->errorInfo())));
  12. $patches = $sth->fetch();
  13. //Returns number of patches applied
  14. return $patches['count'];
  15. }
  16. function getNumberOfPatches() {
  17. global $patch;
  18. #Max patches applied - start
  19. $patches = getNumberOfDonePatches();
  20. //$patch_count = count($patch);
  21. $patch_count = max( array_keys( $patch ) );
  22. //Returns number of patches to be applied
  23. return $patch_count - $patches;
  24. }
  25. function runPatches() {
  26. global $patch;
  27. global $db_server;
  28. global $dbh;
  29. $db = new db();
  30. #DEFINE SQL PATCH
  31. $display_block = "";
  32. $sql = "SHOW TABLES LIKE '".TB_PREFIX."sql_patchmanager'";
  33. if ($db_server == 'pgsql') {
  34. $sql = "SELECT 1 FROM pg_tables WHERE tablename ='".TB_PREFIX."sql_patchmanager'";
  35. }
  36. $sth = $db->query($sql);
  37. $rows = $sth->fetchAll();
  38. $display_block .= <<<EOD
  39. <br />
  40. <b>Simple Invoices :: Database Upgrade Manager</b><br />
  41. <hr />
  42. <br />The database patches have now been applied. You can now start working with Simple Invoices.<br />
  43. <p align=middle><br /><a href="index.php"><font color="green">HOME</font></a></p>
  44. <table align='center'>
  45. EOD;
  46. if(count($rows) == 1) {
  47. if ($db_server == 'pgsql') {
  48. // Yay! Transactional DDL
  49. $dbh->beginTransaction();
  50. }
  51. for($i=0;$i < count($patch);$i++) {
  52. // run_sql_patch($i,$patch[$i]); // use instead of following line if patch application status display is to be suppressed
  53. $display_block .= run_sql_patch($i,$patch[$i]);
  54. }
  55. if ($db_server == 'pgsql') {
  56. // Yay! Transactional DDL
  57. $dbh->commit();
  58. }
  59. $display_block .= "\n</table>";
  60. //exit();
  61. $refresh = '<meta http-equiv="refresh" content="5;url=index.php">';
  62. } else {
  63. $display_block .= "\n<tr><td><br /><br />Step 1 - This is the first time Database Updates has been run<br /></td></tr>";
  64. initialise_sql_patch();
  65. $display_block .= "\n<tr><td><br />Now that the Database upgrade table has been initialised, please go back to the Database Upgrade Manger page by clicking <a href='index.php?module=options&amp;view=database_sqlpatches'>HERE</a> to run the remaining patches</td></tr>";
  66. $display_block .= "\n</table>";
  67. }
  68. global $smarty;
  69. $smarty-> assign("display_block",$display_block);
  70. $smarty-> assign("refresh",$refresh);
  71. }
  72. function donePatches() {
  73. $display_block = "<table align='center'>";
  74. $display_block .= <<<EOD
  75. <br />
  76. <b>Simple Invoices :: Database Upgrade Manager</b><br />
  77. <hr />
  78. <tr><td><br />The database patches are uptodate. You can continue working with Simple Invoices.<br /><p align=middle><br /><a href="index.php"><font color="green">HOME</font></a></p></tr>
  79. </table>
  80. EOD;
  81. //exit();
  82. $refresh = '<meta http-equiv="refresh" content="2;url=index.php">';
  83. global $smarty;
  84. $smarty-> assign("display_block",$display_block);
  85. $smarty-> assign("refresh",$refresh);
  86. }
  87. function listPatches() {
  88. global $patch;
  89. global $include_dir;
  90. $display_block = <<<EOD
  91. <b>Simple Invoices :: Database Upgrade Manager</b><br /><br />
  92. Your version of Simple Invoices has been upgraded<br /><br />
  93. With this new release there are database patches that need to be applied<br /><br />
  94. <hr />
  95. <table align="center">
  96. <tr>
  97. <td>
  98. <br />
  99. The list below describes which patches have and have not been applied to the database, the aim is to have them all applied. If there are patches that have not been applied to the Simple Invoices database, please run the Update database by clicking update
  100. </td>
  101. </tr>
  102. </table>
  103. <table class="buttons" align="center">
  104. <tr>
  105. <td>
  106. <a href="./index.php?case=run" class="positive">
  107. <img src="{$include_dir}images/common/tick.png" alt="" />
  108. Update
  109. </a>
  110. </td>
  111. </tr>
  112. </table>
  113. <br />
  114. <img src="{$include_dir}images/common/important.png" alt="" /><font color="red">Warning: Please backup your database before upgrading!</font>
  115. <br />
  116. <br />
  117. <table align="center">
  118. EOD;
  119. for($p = 0; $p < count($patch);$p++) {
  120. $patch_name = htmlsafe($patch[$p]['name']);
  121. $patch_date = htmlsafe($patch[$p]['date']);
  122. if(check_sql_patch($p,$patch[$p]['name'])) {
  123. $display_block .= "\n<tr><td>SQL patch $p, $patch_name <i>has</i> already been applied in release $patch_date</td></tr>";
  124. }
  125. else {
  126. $display_block .= "\n<tr><td>SQL patch $p, $patch_name <b>has not</b> been applied to the database</td></tr>";
  127. }
  128. }
  129. $display_block .= "\n</table>";
  130. global $smarty;
  131. $smarty-> assign("display_block",$display_block);
  132. }
  133. function check_sql_patch($check_sql_patch_ref, $check_sql_patch_field) {
  134. global $dbh;
  135. $db = new db();
  136. $sql = "SELECT * FROM ".TB_PREFIX."sql_patchmanager WHERE sql_patch_ref = :patch" ;
  137. $sth = $db->query($sql, ':patch', $check_sql_patch_ref) or die(htmlsafe(end($dbh->errorInfo())));
  138. if(count($sth->fetchAll()) > 0) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. function run_sql_patch($id, $patch) {
  144. global $dbh;
  145. global $db_server;
  146. $db = new db();
  147. $display_block = "";
  148. $sql = "SELECT * FROM ".TB_PREFIX."sql_patchmanager WHERE sql_patch_ref = :id" ;
  149. $sth = $db->query($sql, ':id', $id) or die(htmlsafe(end($dbh->errorInfo())));
  150. //echo $sql;
  151. $escaped_id = htmlsafe($id);
  152. $patch_name = htmlsafe($patch['name']);
  153. #forget about it!! the patch as its already been run
  154. if (count($sth->fetchAll()) != 0) {
  155. $display_block .= "\n <tr><td>Skipping SQL patch $escaped_id, $patch_name as it <i>has</i> already been applied</td></tr>";
  156. }
  157. else {
  158. //patch hasn't been run
  159. #so do the bloody patch
  160. try {
  161. $db->query($patch['patch']); // or die(htmlsafe(end($dbh->errorInfo())));
  162. $display_block = "\n <tr><td>SQL patch $escaped_id, $patch_name <i>has</i> been applied to the database</td></tr>";
  163. } catch (Exception $e){
  164. $display_block = "\n <tr><td>SQL patch $escaped_id, $patch_name <i>has</i>><b> NOT </b>been applied to the database due to $e </td></tr>";
  165. }
  166. # now update the ".TB_PREFIX."sql_patchmanager table
  167. $sql_update = "INSERT INTO ".TB_PREFIX."sql_patchmanager ( sql_patch_ref , sql_patch , sql_release , sql_statement ) VALUES (:id, :name, :date, :patch)";
  168. /*echo $sql_update;*/
  169. $db->query($sql_update, ':id', $id, ':name', $patch['name'], ':date', $patch['date'], ':patch', $patch['patch']) or die(htmlsafe(end($dbh->errorInfo())));
  170. if($id == 126) {
  171. patch126();
  172. }
  173. /*
  174. * cusom_fields to new customFields patch - commented out till future
  175. */
  176. /*
  177. elseif($id == 137) {
  178. convertInitCustomFields();
  179. }
  180. */
  181. $display_block .= "\n <tr><td>SQL patch $escaped_id, $patch_name <b>has</b> been applied</td></tr>";
  182. }
  183. return $display_block;
  184. }
  185. function initialise_sql_patch() {
  186. //SC: MySQL-only function, not porting to PostgreSQL
  187. global $dbh;
  188. $db = new db();
  189. #check sql patch 1
  190. $sql_patch_init = "CREATE TABLE ".TB_PREFIX."sql_patchmanager (sql_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,sql_patch_ref VARCHAR( 50 ) NOT NULL ,sql_patch VARCHAR( 255 ) NOT NULL ,sql_release VARCHAR( 25 ) NOT NULL ,sql_statement TEXT NOT NULL) TYPE = MYISAM ";
  191. dbQuery($sql_patch_init) or die(end($dbh->errorInfo()));
  192. $display_block = "<tr><td>Step 2 - The SQL patch table has been created<br /></td></tr>";
  193. echo $display_block;
  194. $sql_insert = "INSERT INTO ".TB_PREFIX."sql_patchmanager
  195. ( sql_id ,sql_patch_ref , sql_patch , sql_release , sql_statement )
  196. VALUES ('','1','Create ".TB_PREFIX."sql_patchmanger table','20060514', :patch)";
  197. $db->query($sql_insert, ':patch', $sql_patch_init) or die(end($dbh->errorInfo()));
  198. $display_block2 = "<tr><td>Step 3 - The SQL patch has been inserted into the SQL patch table<br /></td></tr>";
  199. echo $display_block2;
  200. }
  201. function patch126() {
  202. //SC: MySQL-only function, not porting to PostgreSQL
  203. $db = new db();
  204. $sql = "SELECT * FROM ".TB_PREFIX."invoice_items WHERE product_id = 0";
  205. $sth = $db->query($sql);
  206. while($res = $sth->fetch()) {
  207. $sql = "INSERT INTO ".TB_PREFIX."products (id, description, unit_price, enabled, visible)
  208. VALUES (NULL, :description, :gross_total, '0', '0')";
  209. $db->query($sql, ':description', $res[description], ':total', $res[gross_total]);
  210. $id = lastInsertId();
  211. $sql = "UPDATE ".TB_PREFIX."invoice_items SET product_id = :id, unit_price = :price WHERE ".TB_PREFIX."invoice_items.id = :item";
  212. $db->query($sql,
  213. ':id', $id[0],
  214. ':price', $res[gross_total],
  215. ':item', $res[id]
  216. );
  217. }
  218. }
  219. function convertInitCustomFields() {
  220. // This function is exactly the same as convertCustomFields() in ./sys/include/customFieldConversion.php but without the print_r and echo output while storing
  221. /* check if any value set -> keeps all data for sure */
  222. global $dbh;
  223. $db = new db();
  224. $sql = "SELECT * FROM ".TB_PREFIX."custom_fields";
  225. $sth = $dbh->prepare($sql);
  226. $sth->execute();
  227. while($custom = $sth->fetch()) {
  228. if(preg_match("/(.+)_cf([1-4])/",$custom['cf_custom_field'],$match)) {
  229. //print_r($match);
  230. switch($match[1]) {
  231. case "biller": $cat = 1; break;
  232. case "customer": $cat = 2; break;
  233. case "product": $cat = 3; break;
  234. case "invoice": $cat = 4; break;
  235. default: $case = 0;
  236. }
  237. $cf_field = "custom_field".$match[2];
  238. if($match[1] != "biller") {
  239. $sql = "SELECT id, :field FROM :table";
  240. $tablename = TB_PREFIX.$match[1]."s";
  241. }
  242. else {
  243. $sql = "SELECT id, :field FROM :table";
  244. $tablename = TB_PREFIX.$match[1];
  245. }
  246. $store = false;
  247. /*
  248. * If custom field name is set
  249. */
  250. if($custom['cf_custom_label'] != NULL) {
  251. $store = true;
  252. }
  253. //error_log($sql);
  254. $tth = $dbh->prepare($sql);
  255. $tth->bindValue(':table', $tablename);
  256. $tth->bindValue(':field', $cf_field);
  257. $tth->execute();
  258. /*
  259. * If any field is set, create custom field
  260. */
  261. while($res = $tth->fetch()) {
  262. if($res[1] != NULL) {
  263. $store = true;
  264. break;
  265. }
  266. //echo($res[0]."<br />");
  267. }
  268. if($store) {
  269. // print_r($res);
  270. // echo "<br />".$sql." ".$res['id'];
  271. //create new text custom field
  272. saveInitCustomField(3,$cat,$custom['cf_custom_field'],$custom['cf_custom_label']);
  273. $id = lastInsertId();
  274. error_log($id);
  275. $plugin = getPluginById(3);
  276. $plugin->setFieldId($id);
  277. //insert all data
  278. $uth = $dbh->prepare($sql);
  279. $uth->bindValue(':table', $tablename);
  280. $uth->bindValue(':field', $cf_field);
  281. $uth->execute();
  282. while($res2 = $uth->fetch()) {
  283. $plugin->saveInput($res2[$cf_field], $res2['id']);
  284. }
  285. }
  286. }
  287. }
  288. }
  289. function saveInitCustomField($id, $category, $name, $description) {
  290. // This function is exactly same as saveCustomField() in ./sys/include/manageCustomFields.php but without the final echo output
  291. $db = new db();
  292. $sql = "INSERT INTO ".TB_PREFIX."customFields (pluginId, categorieId, name, description)
  293. VALUES (:id, :category, :name, :description)";
  294. $db->query($sql, ':id', $id, ':category', $category, ':name', $name, ':description', $description);
  295. // echo "SAVED<br />";
  296. }