UpdateProfile.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Login
  4. *
  5. * Copyright 2010 by Jason Coward <jason@modx.com> and Shaun McCormick <shaun+login@modx.com>
  6. *
  7. * Login is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option) any
  10. * later version.
  11. *
  12. * Login is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package login
  21. */
  22. /**
  23. * Update the user's profile
  24. *
  25. * @package login
  26. * @subpackage processors
  27. */
  28. class LoginUpdateProfileProcessor extends LoginProcessor {
  29. /** @var modUserProfile $profile */
  30. public $profile;
  31. /** @var boolean $usernameChanged */
  32. public $usernameChanged = false;
  33. /** @var string $oldUsername */
  34. public $oldUsername;
  35. /** @var LoginUpdateProfileController $controller */
  36. public $controller;
  37. /**
  38. * @return boolean|string
  39. */
  40. public function process() {
  41. $this->getProfile();
  42. if (empty($this->profile)) {
  43. return $this->modx->lexicon('login.profile_err_nf');
  44. }
  45. $this->removeSpamFields();
  46. $this->setExtended();
  47. $this->setFields();
  48. if (!$this->syncUsername()) {
  49. return $this->modx->lexicon('login.username_err_ae');
  50. }
  51. if (!$this->save()) {
  52. return $this->modx->lexicon('login.profile_err_save');
  53. }
  54. $this->runPostHooks();
  55. $this->handleSuccess();
  56. return true;
  57. }
  58. /**
  59. * Get the user's profile
  60. * @return modUserProfile
  61. */
  62. public function getProfile() {
  63. $this->profile = $this->controller->user->getOne('Profile');
  64. return $this->profile;
  65. }
  66. /**
  67. * Remove any spam/submitVar fields from the field list
  68. * @return void
  69. */
  70. public function removeSpamFields() {
  71. $this->controller->dictionary->remove('nospam');
  72. $this->controller->dictionary->remove('blank');
  73. $submitVar = $this->controller->getProperty('submitVar');
  74. if (!empty($submitVar)) {
  75. $this->controller->dictionary->remove($submitVar);
  76. }
  77. }
  78. /**
  79. * If desired, set any extended fields
  80. * @return void
  81. */
  82. public function setExtended() {
  83. if ($this->controller->getProperty('useExtended',true,'isset')) {
  84. $allowedExtendedFields = $this->controller->getProperty('allowedExtendedFields','');
  85. $allowedExtendedFields = !empty($allowedExtendedFields) ? explode(',',$allowedExtendedFields) : array();
  86. /* first cut out regular fields */
  87. $excludeExtended = $this->controller->getProperty('excludeExtended','');
  88. $excludeExtended = explode(',',$excludeExtended);
  89. $profileFields = $this->profile->toArray();
  90. $userFields = $this->controller->user->toArray();
  91. $newExtended = array();
  92. $fields = $this->controller->dictionary->toArray();
  93. foreach ($fields as $field => $value) {
  94. $isValidExtended = true;
  95. if (!empty($allowedExtendedFields)) {
  96. if (!in_array($field,$allowedExtendedFields)) {
  97. $isValidExtended = false;
  98. }
  99. }
  100. if (isset($profileFields[$field]) || isset($userFields[$field]) || $field == 'password_confirm' || $field == 'passwordconfirm' || in_array($field,$excludeExtended) || $field == 'nospam' || $field == 'nospam:blank') {
  101. $isValidExtended = false;
  102. }
  103. if ($isValidExtended) {
  104. $newExtended[$field] = $value;
  105. }
  106. }
  107. /* now merge with existing extended data */
  108. $extended = $this->profile->get('extended');
  109. $extended = is_array($extended) ? array_merge($extended,$newExtended) : $newExtended;
  110. $this->profile->set('extended',$extended);
  111. }
  112. }
  113. /**
  114. * Set the form fields to the user
  115. * @return void
  116. */
  117. public function setFields() {
  118. $allowedFields = $this->controller->getProperty('allowedFields','');
  119. $allowedFields = !empty($allowedFields) ? explode(',',$allowedFields) : array();
  120. $fields = $this->controller->dictionary->toArray();
  121. foreach ($fields as $key => $value) {
  122. $isValidField = true;
  123. if (!empty($allowedFields)) {
  124. if (!in_array($key,$allowedFields)) {
  125. $isValidField = false;
  126. }
  127. }
  128. if ($isValidField) {
  129. $this->profile->set($key,$value);
  130. }
  131. }
  132. }
  133. /**
  134. * Allow changing of username for user via syncUsername property
  135. * @return boolean
  136. */
  137. public function syncUsername() {
  138. $synced = true;
  139. $syncUsername = $this->controller->getProperty('syncUsername',false,'isset');
  140. $this->oldUsername = $this->controller->user->get('username');
  141. if (!empty($syncUsername)) {
  142. $newUsername = $this->profile->get($syncUsername);
  143. if (!empty($newUsername) && strcmp($newUsername,$this->oldUsername) != 0) {
  144. $alreadyExists = $this->modx->getCount('modUser',array('username' => $newUsername));
  145. if (!empty($alreadyExists)) {
  146. $synced = false;
  147. } else {
  148. $this->controller->user->set('username',$newUsername);
  149. $this->usernameChanged = true;
  150. $synced = $this->controller->user->save();
  151. }
  152. }
  153. }
  154. return $synced;
  155. }
  156. /**
  157. * Save the user data
  158. * @return boolean
  159. */
  160. public function save() {
  161. $this->controller->user->addOne($this->profile,'Profile');
  162. $saved = $this->controller->user->save();
  163. if (!$saved) {
  164. /* revert username change */
  165. if ($this->usernameChanged) {
  166. $this->controller->user->set('username',$this->oldUsername);
  167. $this->controller->user->save();
  168. }
  169. }
  170. return $saved;
  171. }
  172. /**
  173. * Run any post-update hooks
  174. * @return void
  175. */
  176. public function runPostHooks() {
  177. $postHooks = $this->controller->getProperty('postHooks','');
  178. $this->controller->loadHooks('postHooks');
  179. $fields = $this->dictionary->toArray();
  180. $fields['updateprofile.user'] = &$this->controller->user;
  181. $fields['updateprofile.profile'] =& $this->profile;
  182. $fields['updateprofile.usernameChanged'] = $this->usernameChanged;
  183. $this->controller->postHooks->loadMultiple($postHooks,$fields);
  184. /* process hooks */
  185. if ($this->controller->postHooks->hasErrors()) {
  186. $errors = array();
  187. $errTpl = $this->controller->getProperty('errTpl');
  188. $errs = $this->controller->postHooks->getErrors();
  189. foreach ($errs as $key => $error) {
  190. $errors[$key] = str_replace('[[+error]]',$error,$errTpl);
  191. }
  192. $this->modx->toPlaceholders($errors,'error');
  193. $errorMsg = $this->controller->postHooks->getErrorMessage();
  194. $this->modx->toPlaceholder('message',$errorMsg,'error');
  195. }
  196. }
  197. /**
  198. * Set the success placeholder
  199. * @return void
  200. */
  201. public function handleSuccess() {
  202. $successMsg = $this->controller->getProperty('successMsg',$this->modx->lexicon('login.profile_updated'));
  203. $this->modx->toPlaceholder($this->controller->getProperty('successMsgPlaceholder','error.message'),$successMsg);
  204. }
  205. }
  206. return 'LoginUpdateProfileProcessor';