/** * @Saving */ public static function saving($event, Field $field) { $userprofile = App::module('bixie/userprofile'); if (!($type = $userprofile->getFieldType($field->type))) { throw new Exception(__('Field type not found.')); } foreach (['multiple', 'required'] as $key) { if ($type[$key] != -1) { //check fixed value if ($type[$key] != $field->get($key)) { throw new Exception(__('Invalid value for ' . $key . ' option.')); } } } //slug $i = 2; $id = $field->id; if (!$field->slug) { $field->slug = $field->label; } while (self::where(['slug = ?'], [$field->slug])->where(function ($query) use($id) { if ($id) { $query->where('id <> ?', [$id]); } })->first()) { $field->slug = preg_replace('/-\\d+$/', '', $field->slug) . '-' . $i++; } if (!$field->id) { $next = self::getConnection()->fetchColumn('SELECT MAX(priority) + 1 FROM @userprofile_field'); $field->priority = $next ?: 0; } }
/** * {@inheritdoc} */ public function main(App $app) { $app['field'] = function ($app) { if ($id = $app['request']->attributes->get('_field') and $field = Field::find($id)) { return $field; } return new Field(); }; }
/** * @Route("/registration") */ public function registrationAction() { $user = App::user(); $userprofile = App::module('bixie/userprofile'); if ($user->isAuthenticated()) { return App::redirect('@userprofile'); } return ['$view' => ['title' => __('User registration'), 'name' => 'bixie/userprofile/registration.php'], '$data' => ['config' => $userprofile->config('default'), 'fields' => Field::getProfileFields(), 'profilevalues' => [], 'user' => ['id' => null, 'username' => '', 'name' => '', 'email' => '']]]; }
/** * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"}) * @Request({"id": "int"}, csrf=true) */ public function deleteAction($id) { if ($field = Field::find($id)) { foreach (Profilevalue::where(['field_id = :id'], [':id' => $id])->get() as $profilevalue) { $profilevalue->delete(); } $field->delete(); } return ['message' => 'success']; }
/** * @Saving */ public static function saving($event, Field $field) { $userprofile = App::module('bixie/userprofile'); if (!($type = $userprofile->getType($field->type))) { throw new Exception(__('Field type not found.')); } foreach (['multiple', 'required'] as $key) { if ($type[$key] != -1) { //check fixed value if ($type[$key] != $field->get($key)) { throw new Exception(__('Invalid value for ' . $key . ' option.')); } } } if (!$field->id) { $next = self::getConnection()->fetchColumn('SELECT MAX(priority) + 1 FROM @userprofile_field'); $field->priority = $next ?: 0; } }
/** * @Route("/ajax", methods="POST") * @Request({"field_id": "int", "action": "string"}) */ public function ajaxAction($field_id, $action) { if (!($field = Field::find($field_id))) { App::abort(400, __('Field not found.')); } $fieldValue = Profilevalue::create()->setField($field); $fieldType = $fieldValue->getFieldType(); if (method_exists($fieldType, $action)) { return call_user_func([$fieldType, $action], $fieldValue); } return 'No response'; }
/** * @Route("/", methods="GET") * @Route("/{id}", methods="GET", requirements={"id"="\d+"}) */ public function indexAction($id = 0) { $self = App::user(); $userprofile = App::module('bixie/userprofile'); $id = $id ?: $self->id; if (!$self->hasAccess('user: manage users') && $id != $self->id) { App::abort(403, 'Insufficient permissions.'); } if (!($user = User::find($id))) { App::abort(404, 'User not found.'); } return ['config' => $userprofile->config(), 'fields' => Field::getProfileFields(), 'profilevalues' => Profilevalue::getUserProfilevalues($user), 'user' => ['id' => $user->id, 'username' => $user->username, 'name' => $user->name, 'email' => $user->email]]; }
/** * @Route("/edit") * @Request({"id"}) * @Access("site: manage site", admin=true) */ public function editAction($id = '') { $userprofile = App::module('bixie/userprofile'); if (is_numeric($id)) { $field = Field::find($id); } else { $field = Field::create(); $field->setType($id); } if (!$field) { throw new NotFoundException(__('Field not found.')); } if (!($type = $userprofile->getType($field->type))) { throw new NotFoundException(__('Type not found.')); } return ['$view' => ['title' => __('Field'), 'name' => 'bixie/userprofile/admin/edit.php'], '$data' => ['field' => $field, 'type' => $type, 'roles' => array_values(Role::findAll())]]; }
/** * @param User|null $user * @param bool $asArray * @param bool $checkAccess * @return array|bool */ public function getProfile(User $user = null, $asArray = true, $checkAccess = true) { $profile = []; if (!$this->framework) { return $profile; } if ($user = $user ?: App::user() and $user->id > 0) { $profileValues = Profilevalue::getUserProfilevalues($user); } foreach (Field::getProfileFields($checkAccess) as $field) { $fieldValue = isset($profileValues[$field->id]) ? $profileValues[$field->id] : Profilevalue::create(['field_id' => $field->id, 'user_id' => $user->id, 'multiple' => $field->get('multiple') == 1 ? 1 : 0, 'data' => $field->get('data')])->setField($field)->setValue($field->get('value')); if ($asArray) { $profile[$field->slug] = $fieldValue->setField($field)->toFormattedArray(['id' => $fieldValue->id]); } else { $profile[$field->slug] = $fieldValue->setField($field); } } return $profile; }
/** * @Route("/edit") * @Request({"id"}) * @Access("site: manage site", admin=true) */ public function editAction($id = '') { /** @var \Bixie\Userprofile\UserprofileModule $userprofile */ $userprofile = App::module('bixie/userprofile'); if (is_numeric($id)) { $field = Field::find($id); } else { $field = Field::create(); $field->setFieldType($id); $field->set('value', []); $field->set('data', []); } if (!$field) { throw new NotFoundException(__('Field not found.')); } if (!($type = $userprofile->getFieldType($field->type))) { throw new NotFoundException(__('Type not found.')); } $fixedFields = ['multiple', 'required', 'controls', 'repeatable']; if (!$field->id) { foreach ($type->getConfig() as $key => $value) { if (!in_array($key, $fixedFields)) { $field->set($key, $value); } } } //check fixed value foreach ($fixedFields as $key) { if (!isset($type[$key])) { $type[$key] = 0; } if ($type[$key] != -1) { $field->set($key, $type[$key]); } } return ['$view' => ['title' => __('Field'), 'name' => 'bixie/userprofile/admin/edit.php'], '$data' => ['field' => $field, 'type' => $type, 'roles' => array_values(Role::findAll())]]; }
/** * @Access("system: access settings") */ public function settingsAction() { return ['$view' => ['title' => __('Userprofile settings'), 'name' => 'bixie/userprofile/admin/settings.php'], '$data' => ['fields' => array_values(Field::findAll()), 'config' => App::module('bixie/userprofile')->config()]]; }
$scripts->register('user-section-userprofile', 'bixie/userprofile:app/bundle/user-section-userprofile.js', ['~user-edit', 'bixie-fieldtypes'], ['version' => $version]); }, 'view.data' => function ($event, $data) use($app) { $route = $app->request()->attributes->get('_route'); if (strpos($route, '@userprofile') === 0 || $route == '@user/edit') { $data->add('$fieldtypes', ['ajax_url' => 'api/userprofile/profile/ajax']); } //load profile if (in_array($route, ['@userprofile', '@userprofile/registration', '@user/edit'])) { $self = $app->user(); $edit_id = $app->request()->get('id'); if ($route == '@user/edit') { //blank user when admin creates new user $user = $edit_id ? \Pagekit\User\Model\User::find($edit_id) : \Pagekit\User\Model\User::create(); } else { $user = $self; } if ($self->hasAccess('user: manage users') || $user->id == $self->id) { $profileUser = ProfileUser::load($user); $data->add('$userprofile', ['fields' => array_values(\Bixie\Userprofile\Model\Field::getProfileFields()), 'profilevalues' => $app->module('bixie/userprofile')->getProfile($user), 'profile_user' => $profileUser]); } } }, 'view.styles' => function ($event, $styles) use($app) { $route = $app->request()->attributes->get('_route'); if (strpos($route, '@userprofile') === 0 || in_array($route, ['@user/edit'])) { foreach ($app->module('bixie/userprofile')->getFieldTypes() as $type) { $type->addStyles($styles); } } }, 'console.init' => function ($event, $console) { $console->add(new Bixie\Userprofile\Console\Commands\TranslateCommand()); }]];
} }, 'uninstall' => function ($app) { $util = $app['db']->getUtility(); if ($util->tableExists('@userprofile_field')) { $util->dropTable('@userprofile_field'); } if ($util->tableExists('@userprofile_value')) { $util->dropTable('@userprofile_value'); } // remove the config $app['config']->remove('bixie/userprofile'); }, 'updates' => ['1.2.0' => function ($app) { $util = $app['db']->getUtility(); if ($util->tableExists('@userprofile_field')) { $table = $util->listTableDetails('@userprofile_field'); if (!$table->hasColumn('slug')) { $table->addColumn('slug', 'string', ['length' => 255]); $util->alterTable((new Comparator())->diffTable($util->listTableDetails('@userprofile_field'), $table)); foreach (Field::findAll() as $field) { $field->save(['slug' => $app->filter($field->label, 'slugify')]); } } } if ($util->tableExists('@userprofile_value')) { $table = $util->listTableDetails('@userprofile_value'); if (!$table->hasColumn('data')) { $table->addColumn('data', 'json_array', ['notnull' => false]); $util->alterTable((new Comparator())->diffTable($util->listTableDetails('@userprofile_value'), $table)); } } }]];