PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/applications/noviusos_user/classes/controller/admin/account.ctrl.php

https://github.com/jay3/core
PHP | 159 lines | 130 code | 15 blank | 14 comment | 8 complexity | 90553c617bfbccae7a2edd6e20e964da MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * NOVIUS OS - Web OS for digital communication
  4. *
  5. * @copyright 2011 Novius
  6. * @license GNU Affero General Public License v3 or (at your option) any later version
  7. * http://www.gnu.org/licenses/agpl-3.0.html
  8. * @link http://www.novius-os.org
  9. */
  10. namespace Nos\User;
  11. use View;
  12. class Controller_Admin_Account extends \Nos\Controller_Admin_Application
  13. {
  14. public function prepare_i18n()
  15. {
  16. parent::prepare_i18n();
  17. \Nos\I18n::current_dictionary('noviusos_user::common');
  18. }
  19. public function before()
  20. {
  21. try {
  22. parent::before();
  23. } catch (\Nos\Access_Exception $e) {
  24. }
  25. }
  26. public function action_index()
  27. {
  28. $user = \Session::user();
  29. $config_user = \Config::load('noviusos_user::controller/admin/user', true);
  30. $fields = $config_user['fields'];
  31. unset($fields['user_password']);
  32. // Form target is Controller_Admin_User_User, we only display the fieldset here
  33. $fieldset_infos = \Fieldset::build_from_config(
  34. $fields,
  35. $user,
  36. array(
  37. 'save' => false,
  38. )
  39. );
  40. $fieldset_infos->js_validation();
  41. $fieldset_infos->set_config(
  42. 'field_template',
  43. '<tr><th>{label}{required}</th><td class="{error_class}">{field} {error_msg}</td></tr>'
  44. );
  45. $fieldset_display = static::fieldset_display($user)->set_config('field_template', '<tr><th>{label}{required}</th><td class="{error_class}">{field} {error_msg}</td></tr>');
  46. return View::forge(
  47. 'noviusos_user::admin/account',
  48. array(
  49. 'logged_user' => $user,
  50. 'fieldset_infos' => $fieldset_infos,
  51. 'fieldset_display' => $fieldset_display,
  52. ),
  53. false
  54. );
  55. }
  56. public function action_disconnect()
  57. {
  58. \Nos\Auth::disconnect();
  59. \Response::redirect('admin/nos/login/reset');
  60. exit();
  61. }
  62. /**
  63. * Changes the lang of the user
  64. *
  65. * @param $lang The new locale code (like en_GB, fr_FR or ja_JP)
  66. */
  67. public function action_lang($lang)
  68. {
  69. $languages =\Config::get('novius-os.locales', array());
  70. if (array_key_exists($lang, $languages)) {
  71. $user = \Session::user();
  72. $user->user_lang = $lang;
  73. $user->save();
  74. $label = $languages[$lang]['title'];
  75. \Response::json(array(
  76. 'notify' => strtr(__('Your Novius OS has switched to {{language}}. Okay, not quite. Actually it needs a <a>quick refresh</a>.'), array('{{language}}' => $label, '<a>' => '<a href="javascript:document.location.reload();">')),
  77. ));
  78. }
  79. $this->send_error(strtr(__('Sorry but your Novius OS doesn’t speak {{code}}.'), array(
  80. '{{code}}' => $lang,
  81. )));
  82. }
  83. public static function fieldset_display($user)
  84. {
  85. $configuration = $user->getConfiguration();
  86. $fields = array(
  87. 'background' => array(
  88. 'label' => __('Wallpaper'),
  89. 'renderer' => 'Nos\Media\Renderer_Media',
  90. 'form' => array(
  91. 'value' => \Arr::get($configuration, 'misc.display.background', ''),
  92. ),
  93. ),
  94. );
  95. $fieldset_display = \Fieldset::build_from_config(
  96. $fields,
  97. $user,
  98. array(
  99. 'form_name' => 'edit_user_display',
  100. 'complete' =>
  101. function ($data) use ($user) {
  102. $body = array();
  103. try {
  104. $configuration = $user->getConfiguration();
  105. if (!empty($data['background'])) {
  106. $media = \Nos\Media\Model_Media::find($data['background']);
  107. if (!empty($media)) {
  108. \Arr::set($configuration, 'misc.display.background', $data['background']);
  109. $notify = strtr(
  110. __('‘{{title}}’ is your new gorgeous wallpaper. Go quick to the home tab to see it.'),
  111. array(
  112. '{{title}}' => $media->media_title,
  113. )
  114. );
  115. $body['wallpaper_url'] = \Uri::create($media->url());
  116. } else {
  117. $data['background'] = null;
  118. $error = __('This is unexpected: The selected image doesn’t exist any more. It must have been deleted while you were selecting it.');
  119. }
  120. }
  121. if (empty($data['background'])) {
  122. \Arr::delete($configuration, 'misc.display.background');
  123. $notify = __('Your wallpaper is now the default one.');
  124. }
  125. $user->user_configuration = serialize($configuration);
  126. $user->save();
  127. } catch (\Exception $e) {
  128. \Log::exception($e, 'Error during the registration of the account setup. ');
  129. $error = \Fuel::$env == \Fuel::DEVELOPMENT ? $e->getMessage() : __('Something went wrong. Please refresh your browser window and try again. Contact your developer or Novius OS if the problem persists. We apologise for the inconvenience caused.');
  130. }
  131. if (!empty($notify)) {
  132. $body['notify'] = $notify;
  133. }
  134. if (!empty($error)) {
  135. $body['error'] = $error;
  136. }
  137. \Response::json($body);
  138. }
  139. )
  140. );
  141. $fieldset_display->js_validation();
  142. return $fieldset_display;
  143. }
  144. }