SetupController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Typemill\Controllers;
  3. use \Symfony\Component\Yaml\Yaml;
  4. use Typemill\Models\Field;
  5. use Typemill\Models\Validation;
  6. class SetupController extends Controller
  7. {
  8. public function setup($request, $response, $args)
  9. {
  10. $settings = $this->c->get('settings');
  11. $themes = $this->getThemes();
  12. $copyright = $this->getCopyright();
  13. $plugins = array();
  14. $fields = array();
  15. /* iterate through the plugins in the stored user settings */
  16. foreach($settings['plugins'] as $pluginName => $pluginUserSettings)
  17. {
  18. /* add plugin to plugin Data */
  19. $plugins[$pluginName] = Null;
  20. /* Check if the user has deleted a plugin. Then delete it in the settings and store the updated settings. */
  21. if(!is_dir($settings['rootPath'] . 'plugins' . DIRECTORY_SEPARATOR . $pluginName))
  22. {
  23. /* remove the plugin settings and store updated settings */
  24. \Typemill\Settings::removePluginSettings($pluginName);
  25. continue;
  26. }
  27. /* load the original plugin definitions from the plugin folder (author, version and stuff) */
  28. $pluginOriginalSettings = \Typemill\Settings::getPluginSettings($pluginName);
  29. if($pluginOriginalSettings)
  30. {
  31. /* store them as default plugin data with plugin author, plugin year, default settings and field-definitions */
  32. $plugins[$pluginName] = $pluginOriginalSettings;
  33. }
  34. /* overwrite the original plugin settings with the stored user settings, if they exist */
  35. if($pluginUserSettings)
  36. {
  37. $plugins[$pluginName]['settings'] = $pluginUserSettings;
  38. }
  39. /* check, if the plugin has been disabled in the form-session-data */
  40. /* TODO: Works only, if there is at least one plugin with settings */
  41. if(isset($_SESSION['old']) && !isset($_SESSION['old'][$pluginName]['active']))
  42. {
  43. $plugins[$pluginName]['settings']['active'] = false;
  44. }
  45. /* if the plugin defines forms and fields, so that the user can edit the plugin settings in the frontend */
  46. if(isset($pluginOriginalSettings['forms']))
  47. {
  48. $fields = array();
  49. /* then iterate through the fields */
  50. foreach($pluginOriginalSettings['forms']['fields'] as $fieldName => $fieldConfigs)
  51. {
  52. /* and create a new field object with the field name and the field configurations. */
  53. $field = new Field($fieldName, $fieldConfigs);
  54. /* now you have the configurations of the field. Time to set the values */
  55. /* At first, get the value for the field from the stored user settings */
  56. // $userValue = isset($pluginUserSettings[$fieldName]) ? $pluginUserSettings[$fieldName] : NULL;
  57. $userValue = isset($plugins[$pluginName]['settings'][$fieldName]) ? $plugins[$pluginName]['settings'][$fieldName] : NULL;
  58. /* Then overwrite the value, if there are old input values for the field in the session */
  59. $userValue = isset($_SESSION['old'][$pluginName][$fieldName]) ? $_SESSION['old'][$pluginName][$fieldName] : $userValue;
  60. if($field->getType() == "textarea")
  61. {
  62. if($userValue)
  63. {
  64. $field->setContent($userValue);
  65. }
  66. }
  67. elseIf($field->getType() != "checkbox")
  68. {
  69. $field->setAttributeValue('value', $userValue);
  70. }
  71. /* add the field to the field-List with the plugin-name as key */
  72. $fields[] = $field;
  73. }
  74. /* overwrite original plugin form definitions with enhanced form objects */
  75. $plugins[$pluginName]['forms']['fields'] = $fields;
  76. }
  77. }
  78. $this->c->view->render($response, '/setup.twig', array('settings' => $settings, 'themes' => $themes,'copyright' => $copyright,'plugins' => $plugins));
  79. }
  80. public function save($request, $response, $args)
  81. {
  82. if($request->isPost())
  83. {
  84. $settings = $this->c->get('settings');
  85. $pluginSettings = array();
  86. $params = $request->getParams();
  87. $validate = new Validation();
  88. /* extract the settings for the basic application and validate them */
  89. $appSettings = isset($params['settings']) ? $params['settings'] : false;
  90. if($appSettings)
  91. {
  92. $copyright = $this->getCopyright();
  93. $themes = $this->getThemes();
  94. $appSettings['startpage'] = isset($appSettings['startpage']) ? true : false;
  95. $validate->settings($appSettings, $themes, $copyright, 'settings');
  96. }
  97. /* use the stored user settings and iterate over all original plugin settings, so we do not forget any... */
  98. foreach($settings['plugins'] as $pluginName => $pluginUserSettings)
  99. {
  100. /* if there are no input-data for this plugin, then use the stored plugin settings */
  101. if(!isset($params[$pluginName]))
  102. {
  103. $pluginSettings[$pluginName] = $pluginUserSettings;
  104. }
  105. else
  106. {
  107. /* now fetch the original plugin settings from the plugin folder to get the field definitions */
  108. $pluginOriginalSettings = \Typemill\Settings::getPluginSettings($pluginName);
  109. if($pluginOriginalSettings)
  110. {
  111. /* take the user input data and iterate over all fields and values */
  112. foreach($params[$pluginName] as $fieldName => $fieldValue)
  113. {
  114. /* get the corresponding field definition from original plugin settings */
  115. $fieldDefinition = isset($pluginOriginalSettings['forms']['fields'][$fieldName]) ? $pluginOriginalSettings['forms']['fields'][$fieldName] : false;
  116. if($fieldDefinition)
  117. {
  118. /* validate user input for this field */
  119. $validate->pluginField($fieldName, $fieldValue, $pluginName, $fieldDefinition);
  120. }
  121. }
  122. }
  123. /* use the input data */
  124. $pluginSettings[$pluginName] = $params[$pluginName];
  125. }
  126. /* deactivate the plugin, if there is no active flag */
  127. if(!isset($params[$pluginName]['active']))
  128. {
  129. $pluginSettings[$pluginName]['active'] = false;
  130. }
  131. }
  132. if(!is_writable($this->c->get('settings')['settingsPath']))
  133. {
  134. $_SESSION['errors']['folder'] = 'Your settings-folder is not writable';
  135. }
  136. if(isset($_SESSION['errors']))
  137. {
  138. return $response->withRedirect($this->c->router->pathFor('setup'));
  139. }
  140. /* if everything is valid, add plugin settings to base settings again */
  141. $appSettings['plugins'] = $pluginSettings;
  142. /* store updated settings */
  143. \Typemill\Settings::updateSettings($appSettings);
  144. unset($_SESSION['old']);
  145. $this->c->view->render($response, '/welcome.twig', $appSettings);
  146. }
  147. }
  148. private function getCopyright()
  149. {
  150. return array(
  151. "©",
  152. "CC-BY",
  153. "CC-BY-NC",
  154. "CC-BY-NC-ND",
  155. "CC-BY-NC-SA",
  156. "CC-BY-ND",
  157. "CC-BY-SA",
  158. "None"
  159. );
  160. }
  161. private function getThemes()
  162. {
  163. $themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
  164. $themeFolderC = scandir($themeFolder);
  165. $themes = array();
  166. foreach ($themeFolderC as $key => $theme)
  167. {
  168. if (!in_array($theme, array(".","..")))
  169. {
  170. if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
  171. {
  172. $themes[] = $theme;
  173. }
  174. }
  175. }
  176. return $themes;
  177. }
  178. }