/** * Display the list of the profile questions */ public function listQuestions() { // Get all ProfileQuestions $questions = ProfileQuestion::getAll(); // Get all Roles $roles = Role::getAll(); // Create parameters for form $param = array('id' => 'display-questions-form', 'action' => App::router()->getUri('profile-questions'), 'fieldsets' => array('form' => array(), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get('main.valid-button'))), new ButtonInput(array('name' => 'new-question', 'value' => Lang::get($this->_plugin . '.new-question-btn'), 'class' => 'btn-success', 'href' => App::router()->getUri('edit-profile-question', array('name' => '_new')), 'target' => 'dialog', 'icon' => 'plus'))))); // For each ProfileQuestion add roles, displayInRegister and displayInProfile foreach ($questions as $question) { // Add the input to display in register form $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "register-display-{$question->name}", 'default' => $question->displayInRegister, 'nl' => false)); // Add the input to display in the user profile $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "profile-display-{$question->name}", 'default' => $question->displayInProfile, 'nl' => false)); // Get roles associate to this ProfileQuestion in json parameters $attributesRoles = ProfileQuestion::getByName($question->name)->getRoles(); // For each roles create a Checkbox foreach ($roles as $role) { // Add the input to display in the user profile $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "role-{$role->name}-question-{$question->name}", 'default' => in_array($role->id, $attributesRoles) ? 1 : 0, 'nl' => false)); } } // Create form $form = new Form($param); // Create parameters for the list to display $paramList = array('id' => 'profile-questions-list', 'model' => 'ProfileQuestion', 'action' => App::router()->getUri('profile-questions'), 'lines' => 'all', 'navigation' => false, 'sort' => array('order' => DB::SORT_ASC), 'fields' => array('name' => array('hidden' => true), 'editable' => array('hidden' => true), 'actions' => array('independant' => true, 'display' => function ($value, $field, $line) { if ($line->editable) { return Icon::make(array('icon' => 'pencil', 'class' => 'text-info', 'href' => App::router()->getUri('edit-profile-question', array('name' => $line->name)), 'target' => 'dialog', 'title' => Lang::get($this->_plugin . '.edit-profile-question'))) . Icon::make(array('icon' => 'times', 'class' => 'text-danger delete-question', 'data-question' => $line->name, 'title' => Lang::get($this->_plugin . '.delete-profile-question'))); } else { return ''; } }, 'sort' => false, 'search' => false), 'label' => array('independant' => true, 'display' => function ($value, $field, $line) { return Lang::get($this->_plugin . ".profile-question-{$line->name}-label") . " ( {$line->name} )"; }, 'sort' => false, 'search' => false), 'displayInRegister' => array('label' => Lang::get($this->_plugin . ".list-questions-register-visible-label"), 'sort' => false, 'search' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["register-display-{$line->name}"]; }), 'displayInProfile' => array('label' => Lang::get($this->_plugin . '.list-questions-profile-visible-label'), 'sort' => false, 'search' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["profile-display-{$line->name}"]; }))); // For each roles create a checkbox by line profileQuestion! foreach ($roles as $role) { // Add the input to display in register form $paramList['fields'][$role->name] = array('independant' => true, 'label' => Lang::get("roles.role-{$role->id}-label"), 'search' => false, 'sort' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["role-{$field->name}-question-{$line->name}"]; }); } // Create List $list = new ItemList($paramList); if (!$form->submitted()) { if ($list->isRefreshing()) { return $list->display(); } $this->addKeysToJavaScript($this->_plugin . ".confirm-delete-question"); $content = View::make(Plugin::current()->getView("questions-list.tpl"), array('list' => $list, 'form' => $form)); return $form->wrap($content); } // Extract from form, all infos abour roles associate to ProfileQuestion $listRoles = array(); $roles = Role::getAll('name'); $save = array(); foreach ($form->inputs as $name => $field) { // Manage displayInRegister and displayInProfile if (preg_match("/^(register|profile)\\-display\\-(\\w+)\$/", $name, $match)) { $qname = $match[2]; $func = $match[1] == "register" ? 'displayInRegister' : 'displayInProfile'; if (!isset($save[$qname])) { $save[$qname] = new ProfileQuestion(); $save[$qname]->set('name', $qname); } $save[$qname]->set($func, (int) App::request()->getBody($name)); } else { if (preg_match("/^role\\-(\\w+)\\-question\\-(\\w+)\$/", $name, $match)) { $qname = $match[2]; $roleName = $match[1]; // If tab doesn't exit create it to avoid exception if (!isset($listRoles[$qname])) { $listRoles[$qname] = array(); } $role = $roles[$roleName]; // If checkbox is tag, add roleId if ($field->dbvalue()) { array_push($listRoles[$qname], intval($role->id)); } } } } foreach ($save as $question) { $question->update(); } // Save each ProfileQuestions foreach ($questions as $question) { $params = json_decode($question->parameters, true); $params['roles'] = $listRoles[$question->name]; $question->set('parameters', json_encode($params)); $question->update(); } return $form->response(Form::STATUS_SUCCESS); }
/** * Create or edit an user */ public function edit() { $user = App::session()->getUser(); $roles = array_map(function ($role) { return $role->getLabel(); }, Role::getAll('id')); $param = array('id' => 'user-profile-form', 'upload' => true, 'object' => $user, 'fieldsets' => array('general' => array('legend' => Lang::get('admin.user-form-general-legend'), new TextInput(array('name' => 'username', 'required' => true, 'label' => Lang::get('admin.user-form-username-label'), 'disabled' => true)), new EmailInput(array('name' => 'email', 'required' => true, 'label' => Lang::get('admin.user-form-email-label')))), 'profile' => array('legend' => Lang::get('admin.user-form-profile-legend')), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get($this->_plugin . '.valid-button'))))), 'onsuccess' => 'app.dialog("close")'); // Get the user profile questions $questions = ProfileQuestion::getAll('name', array(), array('order' => DB::SORT_ASC)); // Generate the question fields foreach ($questions as $question) { if ($question->displayInProfile && $question->isAllowedForRole($user->roleId)) { $classname = '\\Hawk\\' . ucwords($question->type) . 'Input'; $field = json_decode($question->parameters, true); $field['name'] = $question->name; $field['id'] = 'user-form-' . $question->name . '-input'; $field['independant'] = true; $field['label'] = Lang::get('admin.profile-question-' . $question->name . '-label'); if (isset($field['readonly'])) { if ($field['readonly']) { $field['required'] = false; } } if ($user) { if ($question->type == "file") { $field['after'] = sprintf('<img src="%s" class="profile-image" />', $user->getProfileData($question->name) ? $user->getProfileData($question->name) : ''); } else { $field['default'] = $user->getProfileData($question->name); } } if ($question->name == 'language') { // Get language options $languages = Language::getAllActive(); $options = array(); foreach ($languages as $language) { $options[$language->tag] = $language->label; } $field['options'] = $options; if (!$field['default']) { $field['default'] = Option::get($this->_plugin . '.language'); } } $param['fieldsets']['profile'][] = new $classname($field); } } $form = new Form($param); if (!$form->submitted()) { return NoSidebarTab::make(array('title' => Lang::get('admin.user-form-title'), 'page' => array('content' => $form))); } else { try { foreach ($questions as $question) { if ($question->displayInProfile && $question->isAllowedForRole($user->roleId)) { if ($question->type === 'file') { $upload = Upload::getInstance($question->name); if ($upload) { $file = $upload->getFile(0); $dir = Plugin::current()->getPublicUserfilesDir() . 'img/'; $url = Plugin::current()->getUserfilesUrl() . 'img/'; if (!is_dir($dir)) { mkdir($dir, 0755, true); } $basename = uniqid() . $file->extension; $upload->move($file, $dir, $basename); $user->setProfileData($question->name, $url . $basename); } } else { $user->setProfileData($question->name, $form->inputs[$question->name]->dbvalue()); } } } $user->saveProfile(); if ($form->getData('email') !== $user->email) { // The user asked to reset it email // Check this email is not used by another user on the application $existingUser = User::getByExample(new DBExample(array('id' => array('$ne' => $user->id), 'email' => $form->getData('email')))); if ($existingUser) { return $form->response(Form::STATUS_CHECK_ERROR, Lang::get($this->_plugin . '.reset-email-already-used')); } // Send the email to validate the new email // Create the token to validate the new email $tokenData = array('userId' => $user->id, 'currentEmail' => $user->email, 'newEmail' => $form->getData('email'), 'createTime' => time()); $token = base64_encode(Crypto::aes256Encode(json_encode($tokenData))); // Create the email content $emailContent = View::make($this->getPlugin()->getView('change-email-validation.tpl'), array('sitename' => Option::get($this->_plugin . '.sitename'), 'validationUrl' => App::router()->getUrl('validate-new-email', array('token' => $token)))); $email = new Mail(); $email->to($form->getData('email'))->from(Option::get('main.mailer-from'), Option::get('main.mailer-from-name'))->title(Lang::get($this->_plugin . '.reset-email-title', array('sitename' => Option::get($this->_plugin . '.sitename'))))->content($emailContent)->subject(Lang::get($this->_plugin . '.reset-email-title', array('sitename' => Option::get($this->_plugin . '.sitename'))))->send(); return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.user-profile-update-success-with-email')); } return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.user-profile-update-success')); } catch (Exception $e) { return $form->response(Form::STATUS_ERROR, Lang::get($this->_plugin . '.user-profile-update-error')); } } }