PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bonfire/modules/ui/controllers/Settings.php

http://github.com/ci-bonfire/Bonfire
PHP | 210 lines | 109 code | 27 blank | 74 comment | 19 complexity | 3336a7272198dd3bb918d6f21768e60d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('BASEPATH') || exit('No direct script access allowed');
  2. /**
  3. * Bonfire
  4. *
  5. * An open source project to allow developers to jumpstart their development of
  6. * CodeIgniter applications
  7. *
  8. * @package Bonfire
  9. * @author Bonfire Dev Team
  10. * @copyright Copyright (c) 2011 - 2018, Bonfire Dev Team
  11. * @license http://opensource.org/licenses/MIT The MIT License
  12. * @link http://cibonfire.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. /**
  17. * UI settings controller
  18. *
  19. * Manages the keyboard shortcuts used in the Bonfire admin interface.
  20. *
  21. * @package Bonfire\Modules\UI\Controllers\Settings
  22. * @author Bonfire Dev Team
  23. * @link http://cibonfire.com/docs/developer/keyboard_shortcuts
  24. */
  25. class Settings extends Admin_Controller
  26. {
  27. /**
  28. * Setup the required permissions and load required classes.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->auth->restrict('Bonfire.UI.View');
  36. $this->auth->restrict('Bonfire.UI.Manage');
  37. $this->lang->load('ui');
  38. Template::set('toolbar_title', lang('ui_default_title'));
  39. }
  40. /**
  41. * Display the available shortcuts and the details of the keys setup for these
  42. * shortcut options.
  43. *
  44. * Manages adding, editing and deleting of the shortcut keys.
  45. *
  46. * @return void
  47. */
  48. public function index()
  49. {
  50. if (isset($_POST['add_shortcut'])) {
  51. if ($this->addShortcut()) {
  52. Template::set_message(lang('ui_shortcut_success'), 'success');
  53. } else {
  54. Template::set_message(lang('ui_shortcut_add_error'), 'error');
  55. }
  56. } elseif (isset($_POST['remove_shortcut'])) {
  57. if ($this->removeShortcut()) {
  58. Template::set_message(lang('ui_shortcut_remove_success'), 'success');
  59. } else {
  60. Template::set_message(lang('ui_shortcut_remove_error'), 'error');
  61. }
  62. } elseif (isset($_POST['save'])) {
  63. if ($this->saveSettings()) {
  64. Template::set_message(lang('ui_shortcut_save_success'), 'success');
  65. } else {
  66. Template::set_message(lang('ui_shortcut_save_error'), 'error');
  67. }
  68. }
  69. // Read available shortcuts from the application config.
  70. Template::set('current', config_item('ui.current_shortcuts'));
  71. Template::set('settings', $this->settings_lib->find_all_by('module', 'core.ui'));
  72. Template::set('toolbar_title', lang('ui_shortcuts'));
  73. Template::render();
  74. }
  75. //--------------------------------------------------------------------------
  76. // !PRIVATE METHODS
  77. //--------------------------------------------------------------------------
  78. /**
  79. * Add a shortcut key for an option.
  80. *
  81. * @return boolean False on failure, true on success.
  82. */
  83. private function addShortcut()
  84. {
  85. $this->form_validation->set_rules('new_action', 'lang:ui_actions', 'required');
  86. $this->form_validation->set_rules('new_shortcut', 'lang:ui_shortcuts', 'required|callback__validate_shortcuts');
  87. if ($this->form_validation->run() === false) {
  88. return false;
  89. }
  90. $action = $this->input->post('new_action');
  91. $shortcut = $this->input->post('new_shortcut');
  92. // Read available shortcuts from the application config.
  93. $availableActions = config_item('ui.current_shortcuts');
  94. if (array_key_exists($action, $availableActions)) {
  95. return $this->saveSettings(array($action => $shortcut));
  96. }
  97. return false;
  98. }
  99. /**
  100. * Remove a shortcut key.
  101. *
  102. * @return boolean False on failure, true on success.
  103. */
  104. private function removeShortcut()
  105. {
  106. $this->form_validation->set_rules('remove_shortcut[]', 'lang:ui_actions', 'required');
  107. if ($this->form_validation->run() === false) {
  108. return false;
  109. }
  110. $action = key($this->input->post('remove_shortcut'));
  111. // Read the current settings
  112. $availableActions = $this->settings_lib->find_all_by('module', 'core.ui');
  113. if (array_key_exists($action, $availableActions)) {
  114. return $this->settings_lib->delete($action, 'core.ui');
  115. }
  116. return false;
  117. }
  118. /**
  119. * Save multiple shortcut keys at the same time allowing the user to edit the
  120. * settings.
  121. *
  122. * @param array $settings Array of shortcuts.
  123. *
  124. * @return boolean False on failure, true on success.
  125. */
  126. private function saveSettings($settings = array())
  127. {
  128. if (empty($settings)) {
  129. // Read available shortcuts from the application config.
  130. $availableActions = config_item('ui.current_shortcuts');
  131. // The text inputs need set_value(), so an array can't be used the
  132. // way the remove buttons use them.
  133. // set_value("shortcut[$action]") is not supported
  134. foreach ($availableActions as $action => $shortcut) {
  135. if (isset($_POST["shortcut_$action"])) {
  136. $this->form_validation->set_rules(
  137. "shortcut_$action",
  138. 'lang:ui_shortcuts',
  139. 'required|callback__validate_shortcuts'
  140. );
  141. $settings[$action] = $this->input->post("shortcut_$action");
  142. }
  143. }
  144. if ($this->form_validation->run() === false) {
  145. return false;
  146. }
  147. }
  148. if (empty($settings) || ! is_array($settings)) {
  149. return false;
  150. }
  151. // Continue saving settings if any of them fail, but save the failure result
  152. // to return afterwards.
  153. $updated = true;
  154. foreach ($settings as $action => $shortcut) {
  155. $updatedSetting = $this->settings_lib->set($action, $shortcut, 'core.ui');
  156. if (! $updatedSetting) {
  157. $updated = false;
  158. }
  159. }
  160. log_activity(
  161. $this->auth->user_id(),
  162. lang('bf_act_settings_saved') . ': ' . $this->input->ip_address(),
  163. 'ui'
  164. );
  165. return $updated;
  166. }
  167. /**
  168. * Callback method to validate the shortcut keys.
  169. *
  170. * @param string $shortcut The shortcut key.
  171. *
  172. * @return boolean False if validation fails, else true.
  173. */
  174. public function _validate_shortcuts($shortcut)
  175. {
  176. // Make sure that the shortcuts don't have spaces.
  177. if (stristr($shortcut, " ") !== false) {
  178. $this->form_validation->set_message('_validate_shortcuts', 'lang:ui_shortcut_error');
  179. return false;
  180. }
  181. return true;
  182. }
  183. }