update.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Loads update user page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class SecurityUserUpdateManagerController extends modManagerController {
  17. /** @var string $onUserFormRender */
  18. public $onUserFormRender = '';
  19. /** @var array $extendedFields */
  20. public $extendedFields = array();
  21. /** @var array $remoteFields */
  22. public $remoteFields = array();
  23. /** @var modUser $user */
  24. public $user;
  25. /**
  26. * Check for any permissions or requirements to load page
  27. * @return bool
  28. */
  29. public function checkPermissions() {
  30. return $this->modx->hasPermission('edit_user');
  31. }
  32. /**
  33. * Register custom CSS/JS for the page
  34. * @return void
  35. */
  36. public function loadCustomCssJs() {
  37. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  38. $this->addHtml('<script type="text/javascript">
  39. // <![CDATA[
  40. MODx.onUserFormRender = "'.$this->onUserFormRender.'";
  41. MODx.perm.set_sudo = '.($this->modx->hasPermission('set_sudo') ? 1 : 0).';
  42. // ]]>
  43. </script>');
  44. /* register JS scripts */
  45. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.orm.js');
  46. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.settings.js');
  47. $this->addJavascript($mgrUrl.'assets/modext/widgets/security/modx.grid.user.settings.js');
  48. $this->addJavascript($mgrUrl.'assets/modext/widgets/security/modx.grid.user.group.js');
  49. $this->addJavascript($mgrUrl.'assets/modext/widgets/security/modx.panel.user.js');
  50. $this->addJavascript($mgrUrl.'assets/modext/sections/security/user/update.js');
  51. $this->addHtml('<script type="text/javascript">
  52. // <![CDATA[
  53. Ext.onReady(function() {
  54. MODx.load({
  55. xtype: "modx-page-user-update"
  56. ,user: "'.$this->user->get('id').'"
  57. '.(!empty($this->remoteFields) ? ',remoteFields: '.$this->modx->toJSON($this->remoteFields) : '').'
  58. '.(!empty($this->extendedFields) ? ',extendedFields: '.$this->modx->toJSON($this->extendedFields) : '').'
  59. });
  60. });
  61. // ]]>
  62. </script>');
  63. }
  64. /**
  65. * Custom logic code here for setting placeholders, etc
  66. * @param array $scriptProperties
  67. * @return mixed
  68. */
  69. public function process(array $scriptProperties = array()) {
  70. $placeholders = array();
  71. /* get user */
  72. if (empty($scriptProperties['id']) || strlen($scriptProperties['id']) !== strlen((integer)$scriptProperties['id'])) {
  73. return $this->failure($this->modx->lexicon('user_err_ns'));
  74. }
  75. $this->user = $this->modx->getObject('modUser', array('id' => $scriptProperties['id']));
  76. if ($this->user == null) return $this->failure($this->modx->lexicon('user_err_nf'));
  77. /* process remote data, if existent */
  78. $this->remoteFields = array();
  79. $remoteData = $this->user->get('remote_data');
  80. if (!empty($remoteData)) {
  81. $this->remoteFields = $this->_parseCustomData($remoteData);
  82. }
  83. /* parse extended data, if existent */
  84. $this->user->getOne('Profile');
  85. if ($this->user->Profile) {
  86. $this->extendedFields = array();
  87. $extendedData = $this->user->Profile->get('extended');
  88. if (!empty($extendedData)) {
  89. $this->extendedFields = $this->_parseCustomData($extendedData);
  90. }
  91. }
  92. /* invoke OnUserFormPrerender event */
  93. $onUserFormPrerender = $this->modx->invokeEvent('OnUserFormPrerender', array(
  94. 'id' => $this->user->get('id'),
  95. 'user' => &$this->user,
  96. 'mode' => modSystemEvent::MODE_UPD,
  97. ));
  98. if (is_array($onUserFormPrerender)) {
  99. $onUserFormPrerender = implode('',$onUserFormPrerender);
  100. }
  101. $placeholders['OnUserFormPrerender'] = $onUserFormPrerender;
  102. /* invoke OnUserFormRender event */
  103. $onUserFormRender = $this->modx->invokeEvent('OnUserFormRender', array(
  104. 'id' => $this->user->get('id'),
  105. 'user' => &$this->user,
  106. 'mode' => modSystemEvent::MODE_UPD,
  107. ));
  108. if (is_array($onUserFormRender)) $onUserFormRender = implode('',$onUserFormRender);
  109. $this->onUserFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$onUserFormRender);
  110. $placeholders['OnUserFormRender'] = $this->onUserFormRender;
  111. return $placeholders;
  112. }
  113. private function _parseCustomData(array $remoteData = array(),$path = '') {
  114. $usemb = function_exists('mb_strlen') && (boolean)$this->modx->getOption('use_multibyte',null,false);
  115. $encoding = $this->modx->getOption('modx_charset',null,'UTF-8');
  116. $fields = array();
  117. foreach ($remoteData as $key => $value) {
  118. $field = array(
  119. 'name' => $key,
  120. 'id' => (!empty($path) ? $path.'.' : '').$key,
  121. );
  122. if (is_array($value)) {
  123. $field['iconCls'] = 'icon-folder';
  124. $field['text'] = htmlentities($key,ENT_QUOTES,$encoding);
  125. $field['leaf'] = false;
  126. $field['children'] = $this->_parseCustomData($value,$key);
  127. } else {
  128. $v = $value;
  129. if ($usemb) {
  130. if (mb_strlen($v, $encoding) > 30) {
  131. $v = mb_substr($v,0,30,$encoding).'...';
  132. }
  133. }
  134. elseif (strlen($v) > 30) {
  135. $v = substr($v,0,30).'...';
  136. }
  137. $field['iconCls'] = 'icon-terminal';
  138. $field['text'] = htmlentities($key,ENT_QUOTES,$encoding).' - <i>'.htmlentities($v,ENT_QUOTES,$encoding).'</i>';
  139. $field['leaf'] = true;
  140. $field['value'] = $value;
  141. }
  142. $fields[] = $field;
  143. }
  144. return $fields;
  145. }
  146. /**
  147. * Return the pagetitle
  148. *
  149. * @return string
  150. */
  151. public function getPageTitle() {
  152. if($this->user == null) {
  153. return $this->modx->lexicon('user_err_nf');
  154. } else {
  155. return $this->modx->lexicon('user').': '.$this->user->get('username');
  156. }
  157. }
  158. /**
  159. * Return the location of the template file
  160. * @return string
  161. */
  162. public function getTemplateFile() {
  163. return 'security/user/update.tpl';
  164. }
  165. /**
  166. * Specify the language topics to load
  167. * @return array
  168. */
  169. public function getLanguageTopics() {
  170. return array('user','setting','access');
  171. }
  172. /**
  173. * Get the Help URL
  174. * @return string
  175. */
  176. public function getHelpUrl() {
  177. return 'Users';
  178. }
  179. }