update.class.php 6.4 KB

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