__('User Profile', __FILE__), // getModuleInfo title 'summary' => __('Enables user to change their password, email address and other settings that you define.', __FILE__), // getModuleInfo summary 'version' => 101, 'permanent' => true, 'permission' => 'profile-edit', ); } protected $user; public function __construct() { $this->set('profileFields', array()); } public function init() { return parent::init(); } public function ___execute() { $this->user = $this->wire('user'); $this->setFuel('processHeadline', $this->_("Profile:") . ' ' . $this->user->name); // Primary Headline (precedes the username) $form = $this->buildForm(); if($this->input->post->submit_save_profile) { $this->processInput($form); $this->session->redirect("./"); } else { return $form->render(); } } /** * Build the form fields for adding a page * */ protected function buildForm() { $form = $this->modules->get('InputfieldForm'); $form->attr('id', 'ProcessProfile'); $form->attr('action', './'); $form->attr('method', 'post'); $form->attr('enctype', 'multipart/form-data'); $form->attr('autocomplete', 'off'); foreach($this->user->fields as $field) { if($field->name == 'roles' || !in_array($field->name, $this->profileFields)) continue; $field = $this->user->fields->getFieldContext($field); $inputfield = $field->getInputfield($this->user); $inputfield->value = $this->user->get($field->name); if($field->name == 'admin_theme' && !$inputfield->value) $inputfield->value = 'AdminThemeDefault'; if($field->type instanceof FieldtypeFile) $inputfield->noAjax = true; $form->add($inputfield); } $field = $this->modules->get('InputfieldSubmit'); $field->attr('id+name', 'submit_save_profile'); $field->addClass('head_button_clone'); $form->add($field); return $form; } /** * Save the submitted page add form * */ protected function processInput(Inputfield $form) { $user = $this->user; $form->processInput($this->input->post); if(count($form->getErrors())) { $this->error($this->_("Profile not saved")); return; } $user->of(false); $user->setTrackChanges(true); foreach($user->fields as $field) { if($field->name == 'roles' || !in_array($field->name, $this->profileFields)) continue; $field = $this->user->fields->getFieldContext($field); $inputfield = $form->get($field->name); $value = $inputfield->attr('value'); if($field->name == 'email' && strlen($value)) { if(count($this->users->find("id!={$user->id}, email=" . $this->sanitizer->selectorValue($value)))) { $this->error(sprintf($this->_('Email address "%s" already in use by another user.'), $value)); continue; } } if($field->name == 'pass' && empty($value)) continue; if($user->get($field->name) !== $value) { $user->set($field->name, $value); } } if($user->isChanged()) { $changes = implode(', ', array_unique($user->getChanges())); $message = $this->_('Profile saved') . ' - ' . $changes; $this->message($message); $this->wire('log')->message($message); $this->wire('users')->save($user); } $user->of(true); } static public function getModuleConfigInputfields(array $data) { $profileFields = isset($data['profileFields']) ? $data['profileFields'] : array(); $fieldOptions = wire('templates')->get('user')->fieldgroup; $inputfields = new InputfieldWrapper(); $f = wire('modules')->get('InputfieldCheckboxes'); $f->label = __("What fields can a user edit in their own profile?"); $f->attr('id+name', 'profileFields'); foreach($fieldOptions as $field) { if($field->name == 'roles') continue; $f->addOption($field->name); } $f->attr('value', $profileFields); $inputfields->add($f); return $inputfields; } }