PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/default_www/backend/modules/users/actions/add.php

https://github.com/yoniweb/forkcms-Golden-Gate
PHP | 183 lines | 101 code | 27 blank | 55 comment | 11 complexity | 43964472516d12ce03e2d20fe57f767a MD5 | raw file
  1. <?php
  2. /**
  3. * This is the add-action, it will display a form to create a new user
  4. *
  5. * @package backend
  6. * @subpackage users
  7. *
  8. * @author Tijs Verkoyen <tijs@netlash.com>
  9. * @author Davy Hellemans <davy@netlash.com>
  10. * @since 2.0
  11. */
  12. class BackendUsersAdd extends BackendBaseActionAdd
  13. {
  14. /**
  15. * Execute the action
  16. *
  17. * @return void
  18. */
  19. public function execute()
  20. {
  21. // call parent, this will probably add some general CSS/JS or other required files
  22. parent::execute();
  23. // load the form
  24. $this->loadForm();
  25. // validate the form
  26. $this->validateForm();
  27. // parse the datagrid
  28. $this->parse();
  29. // display the page
  30. $this->display();
  31. }
  32. /**
  33. * Load the form
  34. *
  35. * @return void
  36. */
  37. private function loadForm()
  38. {
  39. // create form
  40. $this->frm = new BackendForm('add');
  41. $groups = BackendUsersModel::getGroups();
  42. $groupIds = array_keys($groups);
  43. $defaultGroupId = BackendModel::getModuleSetting('users', 'default_group', $groupIds[0]);
  44. // create elements
  45. $this->frm->addText('email', null, 255);
  46. $this->frm->addPassword('password', null, 75, 'inputText inputPassword passwordGenerator', 'inputTextError inputPasswordError passwordGenerator');
  47. $this->frm->addPassword('confirm_password', null, 75);
  48. $this->frm->addText('nickname', null, 24);
  49. $this->frm->addText('name', null, 255);
  50. $this->frm->addText('surname', null, 255);
  51. $this->frm->addDropdown('interface_language', BackendLanguage::getInterfaceLanguages());
  52. $this->frm->addDropdown('date_format', BackendUsersModel::getDateFormats(), BackendAuthentication::getUser()->getSetting('date_format'));
  53. $this->frm->addDropdown('time_format', BackendUsersModel::getTimeFormats(), BackendAuthentication::getUser()->getSetting('time_format'));
  54. $this->frm->addDropdown('number_format', BackendUsersModel::getNumberFormats(), BackendAuthentication::getUser()->getSetting('number_format', 'dot_nothing'));
  55. $this->frm->addImage('avatar');
  56. $this->frm->addCheckbox('active', true);
  57. $this->frm->addCheckbox('api_access', false);
  58. $this->frm->addDropdown('group', $groups, $defaultGroupId);
  59. // disable autocomplete
  60. $this->frm->getField('password')->setAttributes(array('autocomplete' => 'off'));
  61. $this->frm->getField('confirm_password')->setAttributes(array('autocomplete' => 'off'));
  62. }
  63. /**
  64. * Validate the form
  65. *
  66. * @return void
  67. */
  68. private function validateForm()
  69. {
  70. // is the form submitted?
  71. if($this->frm->isSubmitted())
  72. {
  73. // cleanup the submitted fields, ignore fields that were added by hackers
  74. $this->frm->cleanupFields();
  75. // email is present
  76. if($this->frm->getField('email')->isFilled(BL::err('EmailIsRequired')))
  77. {
  78. // is this an email-address
  79. if($this->frm->getField('email')->isEmail(BL::err('EmailIsInvalid')))
  80. {
  81. // was this emailaddress deleted before
  82. if(BackendUsersModel::emailDeletedBefore($this->frm->getField('email')->getValue())) $this->frm->getField('email')->addError(sprintf(BL::err('EmailWasDeletedBefore'), BackendModel::createURLForAction('undo_delete', null, null, array('email' => $this->frm->getField('email')->getValue()))));
  83. else
  84. {
  85. // email already exists
  86. if(BackendUsersModel::existsEmail($this->frm->getField('email')->getValue())) $this->frm->getField('email')->addError(BL::err('EmailAlreadyExists'));
  87. }
  88. }
  89. }
  90. // required fields
  91. $this->frm->getField('password')->isFilled(BL::err('PasswordIsRequired'));
  92. $this->frm->getField('nickname')->isFilled(BL::err('NicknameIsRequired'));
  93. $this->frm->getField('name')->isFilled(BL::err('NameIsRequired'));
  94. $this->frm->getField('surname')->isFilled(BL::err('SurnameIsRequired'));
  95. $this->frm->getField('interface_language')->isFilled(BL::err('FieldIsRequired'));
  96. $this->frm->getField('date_format')->isFilled(BL::err('FieldIsRequired'));
  97. $this->frm->getField('time_format')->isFilled(BL::err('FieldIsRequired'));
  98. $this->frm->getField('number_format')->isFilled(BL::err('FieldIsRequired'));
  99. if($this->frm->getField('password')->isFilled())
  100. {
  101. if($this->frm->getField('password')->getValue() !== $this->frm->getField('confirm_password')->getValue()) $this->frm->getField('confirm_password')->addError(BL::err('ValuesDontMatch'));
  102. }
  103. // validate avatar
  104. if($this->frm->getField('avatar')->isFilled())
  105. {
  106. // correct extension
  107. if($this->frm->getField('avatar')->isAllowedExtension(array('jpg', 'jpeg', 'gif', 'png'), BL::err('JPGGIFAndPNGOnly')))
  108. {
  109. // correct mimetype?
  110. $this->frm->getField('avatar')->isAllowedMimeType(array('image/gif', 'image/jpg', 'image/jpeg', 'image/png'), BL::err('JPGGIFAndPNGOnly'));
  111. }
  112. }
  113. // no errors?
  114. if($this->frm->isCorrect())
  115. {
  116. // build settings-array
  117. $settings['nickname'] = $this->frm->getField('nickname')->getValue();
  118. $settings['name'] = $this->frm->getField('name')->getValue();
  119. $settings['surname'] = $this->frm->getField('surname')->getValue();
  120. $settings['interface_language'] = $this->frm->getField('interface_language')->getValue();
  121. $settings['date_format'] = $this->frm->getField('date_format')->getValue();
  122. $settings['time_format'] = $this->frm->getField('time_format')->getValue();
  123. $settings['datetime_format'] = $settings['date_format'] . ' ' . $settings['time_format'];
  124. $settings['number_format'] = $this->frm->getField('number_format')->getValue();
  125. $settings['password_key'] = uniqid();
  126. $settings['avatar'] = 'no-avatar.gif';
  127. $settings['api_access'] = (bool) $this->frm->getField('api_access')->getChecked();
  128. // build user-array
  129. $user['email'] = $this->frm->getField('email')->getValue();
  130. $user['password'] = BackendAuthentication::getEncryptedString($this->frm->getField('password')->getValue(true), $settings['password_key']);
  131. $user['group_id'] = $this->frm->getField('group')->getValue();
  132. // save changes
  133. $user['id'] = (int) BackendUsersModel::insert($user, $settings);
  134. // has the user submitted an avatar?
  135. if($this->frm->getField('avatar')->isFilled())
  136. {
  137. // create new filename
  138. $filename = rand(0,3) . '_' . $user['id'] . '.' . $this->frm->getField('avatar')->getExtension();
  139. // add into settings to update
  140. $settings['avatar'] = $filename;
  141. // resize (128x128)
  142. $this->frm->getField('avatar')->createThumbnail(FRONTEND_FILES_PATH . '/backend_users/avatars/128x128/' . $filename, 128, 128, true, false, 100);
  143. // resize (64x64)
  144. $this->frm->getField('avatar')->createThumbnail(FRONTEND_FILES_PATH . '/backend_users/avatars/64x64/' . $filename, 64, 64, true, false, 100);
  145. // resize (32x32)
  146. $this->frm->getField('avatar')->createThumbnail(FRONTEND_FILES_PATH . '/backend_users/avatars/32x32/' . $filename, 32, 32, true, false, 100);
  147. }
  148. // update settings (in this case the avatar)
  149. BackendUsersModel::update($user, $settings);
  150. // everything is saved, so redirect to the overview
  151. $this->redirect(BackendModel::createURLForAction('index') . '&report=added&var=' . $settings['nickname'] . '&highlight=row-' . $user['id']);
  152. }
  153. }
  154. }
  155. }
  156. ?>