PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Frameworks/BaikalAdmin/Controller/Settings/System.php

https://github.com/paulflorianhoberg/Baikal
PHP | 118 lines | 74 code | 18 blank | 26 comment | 9 complexity | e87546940021b645f09ec951c3303264 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. #################################################################
  3. # Copyright notice
  4. #
  5. # (c) 2012 Jérôme Schneider <mail@jeromeschneider.fr>
  6. # All rights reserved
  7. #
  8. # http://baikal.codr.fr
  9. #
  10. # This script is part of the Baïkal Server project. The Baïkal
  11. # Server project is free software; you can redistribute it
  12. # and/or modify it under the terms of the GNU General Public
  13. # License as published by the Free Software Foundation; either
  14. # version 2 of the License, or (at your option) any later version.
  15. #
  16. # The GNU General Public License can be found at
  17. # http://www.gnu.org/copyleft/gpl.html.
  18. #
  19. # This script is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # This copyright notice MUST APPEAR in all copies of the script!
  25. #################################################################
  26. namespace BaikalAdmin\Controller\Settings;
  27. class System extends \Flake\Core\Controller {
  28. public function execute() {
  29. $this->oModel = new \Baikal\Model\Config\System(PROJECT_PATH_SPECIFIC . "config.system.php");
  30. # Assert that config file is writable
  31. if(!$this->oModel->writable()) {
  32. throw new \Exception("System config file is not writable;" . __FILE__ . " > " . __LINE__);
  33. }
  34. $this->oForm = $this->oModel->formForThisModelInstance(array(
  35. "close" => FALSE,
  36. "hook.morphology" => array($this, "morphologyHook"),
  37. "hook.validation" => array($this, "validationHook"),
  38. ));
  39. if($this->oForm->submitted()) {
  40. $this->oForm->execute();
  41. }
  42. }
  43. public function render() {
  44. $oView = new \BaikalAdmin\View\Settings\System();
  45. $oView->setData("message", \Formal\Core\Message::notice(
  46. "Do not change anything on this page unless you really know what you are doing.<br />You might break Baïkal if you misconfigure something here.",
  47. "Warning !",
  48. FALSE
  49. ));
  50. $oView->setData("form", $this->oForm->render());
  51. return $oView->render();
  52. }
  53. public function morphologyHook(\Formal\Form $oForm, \Formal\Form\Morphology $oMorpho) {
  54. if($oForm->submitted()) {
  55. $bMySQL = (intval($oForm->postValue("PROJECT_DB_MYSQL")) === 1);
  56. } else {
  57. $bMySQL = PROJECT_DB_MYSQL;
  58. }
  59. if($bMySQL === TRUE) {
  60. $oMorpho->remove("PROJECT_SQLITE_FILE");
  61. } else {
  62. $oMorpho->remove("PROJECT_DB_MYSQL_HOST");
  63. $oMorpho->remove("PROJECT_DB_MYSQL_DBNAME");
  64. $oMorpho->remove("PROJECT_DB_MYSQL_USERNAME");
  65. $oMorpho->remove("PROJECT_DB_MYSQL_PASSWORD");
  66. }
  67. }
  68. public function validationHook(\Formal\Form $oForm, \Formal\Form\Morphology $oMorpho) {
  69. if(intval($oForm->modelInstance()->get("PROJECT_DB_MYSQL")) === 1) {
  70. # We have to check the MySQL connection
  71. $sHost = $oForm->modelInstance()->get("PROJECT_DB_MYSQL_HOST");
  72. $sDbName = $oForm->modelInstance()->get("PROJECT_DB_MYSQL_DBNAME");
  73. $sUsername = $oForm->modelInstance()->get("PROJECT_DB_MYSQL_USERNAME");
  74. $sPassword = $oForm->modelInstance()->get("PROJECT_DB_MYSQL_PASSWORD");
  75. try {
  76. $oDB = new \Flake\Core\Database\Mysql(
  77. $sHost,
  78. $sDbName,
  79. $sUsername,
  80. $sPassword
  81. );
  82. } catch(\Exception $e) {
  83. $sMessage = "<strong>MySQL error:</strong> " . $e->getMessage();
  84. $sMessage .= "<br /><strong>Nothing has been saved</strong>";
  85. $oForm->declareError($oMorpho->element("PROJECT_DB_MYSQL_HOST"), $sMessage);
  86. $oForm->declareError($oMorpho->element("PROJECT_DB_MYSQL_DBNAME"));
  87. $oForm->declareError($oMorpho->element("PROJECT_DB_MYSQL_USERNAME"));
  88. $oForm->declareError($oMorpho->element("PROJECT_DB_MYSQL_PASSWORD"));
  89. return;
  90. }
  91. if(($aMissingTables = \Baikal\Core\Tools::isDBStructurallyComplete($oDB)) !== TRUE) {
  92. $sMessage = "<strong>MySQL error:</strong> These tables, required by Baïkal, are missing: <strong>" . implode(", ", $aMissingTables) . "</strong><br />";
  93. $sMessage .= "You may want create these tables using the file <strong>Core/Resources/Db/MySQL/db.sql</strong>";
  94. $sMessage .= "<br /><br /><strong>Nothing has been saved</strong>";
  95. $oForm->declareError($oMorpho->element("PROJECT_DB_MYSQL"), $sMessage);
  96. return;
  97. }
  98. }
  99. }
  100. }