diff --git a/bootstrap.php b/bootstrap.php index 1eb565d..2c5bd1e 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -3,11 +3,12 @@ /* * This file is part of davis/flarum-ext-socialprofile * - * (c) Connor Davis + * © Connor Davis * * For the full copyright and license information, please view the MIT license */ + namespace Davis\SocialProfile; use Illuminate\Contracts\Events\Dispatcher; diff --git a/migrations/2016_01_14_000000_create_socialbuttons_table.php b/migrations/2016_01_14_000000_create_socialbuttons_table.php index fe2194c..cb951bd 100644 --- a/migrations/2016_01_14_000000_create_socialbuttons_table.php +++ b/migrations/2016_01_14_000000_create_socialbuttons_table.php @@ -3,11 +3,12 @@ /* * This file is part of davis/flarum-ext-socialprofile * - * (c) Connor Davis + * © Connor Davis * * For the full copyright and license information, please view the MIT license */ + use Flarum\Database\Migration; use Illuminate\Database\Schema\Blueprint; diff --git a/migrations/2016_10_20_000000_create_socialbuttons_column.php b/migrations/2016_10_20_000000_create_socialbuttons_column.php index 1144573..6cb25ba 100644 --- a/migrations/2016_10_20_000000_create_socialbuttons_column.php +++ b/migrations/2016_10_20_000000_create_socialbuttons_column.php @@ -1,15 +1,16 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -use Flarum\Database\Migration; - -return Migration::addColumns('users', [ - 'social_buttons' => ['longText', 'nullable' => true], -]); + + * + * For the full copyright and license information, please view the MIT license + */ + + +use Flarum\Database\Migration; + +return Migration::addColumns('users', [ + 'social_buttons' => ['longText', 'nullable' => true], +]); diff --git a/migrations/2016_10_21_000000_migrate_data_to_user_column.php b/migrations/2016_10_21_000000_migrate_data_to_user_column.php index f16dec4..eaeedde 100644 --- a/migrations/2016_10_21_000000_migrate_data_to_user_column.php +++ b/migrations/2016_10_21_000000_migrate_data_to_user_column.php @@ -1,30 +1,31 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -use Illuminate\Database\ConnectionInterface; - -return [ - 'up' => function (ConnectionInterface $db) { - $results = $db->table('socialbuttons')->get(); - foreach ($results as $result) { - $db->table('users') - ->where('id', $result->user_id) - ->update(['social_buttons' => $result->buttons]); - } - }, - 'down' => function (ConnectionInterface $db) { - $results = $db->table('users')->select('social_buttons')->get(); - foreach ($results as $result) { - $db->table('socialbuttons')->insert( - ['user_id' => $result->id, 'buttons' => $result->social_buttons] - ); - } - }, -]; + + * + * For the full copyright and license information, please view the MIT license + */ + + +use Illuminate\Database\ConnectionInterface; + +return [ + 'up' => function (ConnectionInterface $db) { + $results = $db->table('socialbuttons')->get(); + foreach ($results as $result) { + $db->table('users') + ->where('id', $result->user_id) + ->update(['social_buttons' => $result->buttons]); + } + }, + 'down' => function (ConnectionInterface $db) { + $results = $db->table('users')->select('social_buttons')->get(); + foreach ($results as $result) { + $db->table('socialbuttons')->insert( + ['user_id' => $result->id, 'buttons' => $result->social_buttons] + ); + } + }, +]; diff --git a/migrations/2016_10_22_000000_drop_socialbuttons_table.php b/migrations/2016_10_22_000000_drop_socialbuttons_table.php index 64ec34b..3c47511 100644 --- a/migrations/2016_10_22_000000_drop_socialbuttons_table.php +++ b/migrations/2016_10_22_000000_drop_socialbuttons_table.php @@ -1,25 +1,26 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Database\Schema\Builder; - -return [ - 'up' => function (Builder $schema) { - $schema->drop('socialbuttons'); - }, - 'down' => function (Builder $schema) { - $schema->create('socialbuttons', function (Blueprint $table) { - $table->increments('id'); - $table->integer('user_id')->unsigned(); - $table->longText('buttons'); - }); - }, -]; + + * + * For the full copyright and license information, please view the MIT license + */ + + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + $schema->drop('socialbuttons'); + }, + 'down' => function (Builder $schema) { + $schema->create('socialbuttons', function (Blueprint $table) { + $table->increments('id'); + $table->integer('user_id')->unsigned(); + $table->longText('buttons'); + }); + }, +]; diff --git a/src/Events/UserButtonsWereChanged.php b/src/Events/UserButtonsWereChanged.php index 04a882d..3852cf7 100644 --- a/src/Events/UserButtonsWereChanged.php +++ b/src/Events/UserButtonsWereChanged.php @@ -1,36 +1,37 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -namespace Davis\SocialProfile\Events; - -use Flarum\Core\User; - -class UserButtonsWereChanged -{ - /** - * @var User - */ - public $user; - - /** - * @var User - */ - public $actor; - - /** - * @param User $user - * @param User $actor - */ - public function __construct(User $user, User $actor = null) - { - $this->user = $user; - $this->actor = $actor; - } -} + + * + * For the full copyright and license information, please view the MIT license + */ + + +namespace Davis\SocialProfile\Events; + +use Flarum\Core\User; + +class UserButtonsWereChanged +{ + /** + * @var User + */ + public $user; + + /** + * @var User + */ + public $actor; + + /** + * @param User $user + * @param User $actor + */ + public function __construct(User $user, User $actor = null) + { + $this->user = $user; + $this->actor = $actor; + } +} diff --git a/src/Listeners/AddClientAssets.php b/src/Listeners/AddClientAssets.php index 30ec9b7..561531b 100644 --- a/src/Listeners/AddClientAssets.php +++ b/src/Listeners/AddClientAssets.php @@ -27,15 +27,15 @@ public function addAssets(ConfigureClientView $event) { if ($event->isForum()) { $event->addAssets([ - __DIR__ . '/../../js/forum/dist/extension.js', - __DIR__ . '/../../less/forum/extension.less', + __DIR__.'/../../js/forum/dist/extension.js', + __DIR__.'/../../less/forum/extension.less', ]); $event->addBootstrapper('Davis/SocialProfile/main'); } if ($event->isAdmin()) { $event->addAssets([ - __DIR__ . '/../../js/admin/dist/extension.js', + __DIR__.'/../../js/admin/dist/extension.js', ]); $event->addBootstrapper('Davis/SocialProfile/main'); } @@ -43,9 +43,9 @@ public function addAssets(ConfigureClientView $event) public function addLocales(ConfigureLocales $event) { - foreach (new DirectoryIterator(__DIR__ . '/../../locale') as $file) { + foreach (new DirectoryIterator(__DIR__.'/../../locale') as $file) { if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) { - $event->locales->addTranslations($file->getBasename('.' . $file->getExtension()), $file->getPathname()); + $event->locales->addTranslations($file->getBasename('.'.$file->getExtension()), $file->getPathname()); } } } diff --git a/src/Listeners/LoadSettingsFromDatabase.php b/src/Listeners/LoadSettingsFromDatabase.php index 801f9ec..dc75fd8 100644 --- a/src/Listeners/LoadSettingsFromDatabase.php +++ b/src/Listeners/LoadSettingsFromDatabase.php @@ -39,7 +39,7 @@ public function prepareApiAttributes(PrepareApiAttributes $event) { if ($event->isSerializer(ForumSerializer::class)) { foreach ($this->fieldsToGet as $field) { - $event->attributes[$this->packagePrefix . $field] = $this->settings->get($this->packagePrefix . $field); + $event->attributes[$this->packagePrefix.$field] = $this->settings->get($this->packagePrefix.$field); } } } diff --git a/src/Listeners/UpdateProfileInDatabase.php b/src/Listeners/UpdateProfileInDatabase.php index 2f17ea1..c2d8ab8 100644 --- a/src/Listeners/UpdateProfileInDatabase.php +++ b/src/Listeners/UpdateProfileInDatabase.php @@ -1,101 +1,101 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -namespace Davis\SocialProfile\Listeners; - -use Davis\SocialProfile\Events\UserButtonsWereChanged; -use Davis\SocialProfile\ProfileValidator; -use Flarum\Core\Access\AssertPermissionTrait; -use Flarum\Event\UserWillBeSaved; -use Illuminate\Contracts\Events\Dispatcher; - -class UpdateProfileInDatabase -{ - use AssertPermissionTrait; - - /** - * Validator for limited suspension. - * - * @var SuspendValidator - */ - protected $validator; - - /** - * @param SuspendValidator $validator - */ - public function __construct(ProfileValidator $validator) - { - $this->validator = $validator; - } - - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(UserWillBeSaved::class, [$this, 'whenUserWillBeSaved']); - } - - /** - * @param UserWillBeSaved $event - */ - public function whenUserWillBeSaved(UserWillBeSaved $event) - { - $attributes = array_get($event->data, 'attributes', []); - - if (array_key_exists('socialButtons', $attributes)) { - $this->validator->assertValid($attributes); - - $user = $event->user; - $actor = $event->actor; - - if ($actor->id !== $user->id) { - $this->assertPermission( - $this->elementsOnlyRemoved( - $user->social_buttons, - $attributes['socialButtons'] - ) - ); - $this->assertCan($actor, 'edit', $user); - } - - $user->social_buttons = $attributes['socialButtons']; - $user->raise(new UserButtonsWereChanged($user)); - } - } - - protected function elementsOnlyRemoved($current, $proposed) - { - $current = json_decode($current); - $proposed = json_decode($proposed); - - foreach ($proposed as $component) { - if (!$this->hasMatchingComponent($current, $component)) { - return false; - } - } - - return true; - } - - protected function hasMatchingComponent($array, $component) - { - $foundMatch = false; - - foreach ($array as $test) { - if ($component == $test) { - $foundMatch = true; - break; - } - } - - return $foundMatch; - } -} + + * + * For the full copyright and license information, please view the MIT license + */ + +namespace Davis\SocialProfile\Listeners; + +use Davis\SocialProfile\Events\UserButtonsWereChanged; +use Davis\SocialProfile\ProfileValidator; +use Flarum\Core\Access\AssertPermissionTrait; +use Flarum\Event\UserWillBeSaved; +use Illuminate\Contracts\Events\Dispatcher; + +class UpdateProfileInDatabase +{ + use AssertPermissionTrait; + + /** + * Validator for limited suspension. + * + * @var SuspendValidator + */ + protected $validator; + + /** + * @param SuspendValidator $validator + */ + public function __construct(ProfileValidator $validator) + { + $this->validator = $validator; + } + + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(UserWillBeSaved::class, [$this, 'whenUserWillBeSaved']); + } + + /** + * @param UserWillBeSaved $event + */ + public function whenUserWillBeSaved(UserWillBeSaved $event) + { + $attributes = array_get($event->data, 'attributes', []); + + if (array_key_exists('socialButtons', $attributes)) { + $this->validator->assertValid($attributes); + + $user = $event->user; + $actor = $event->actor; + + if ($actor->id !== $user->id) { + $this->assertPermission( + $this->elementsOnlyRemoved( + $user->social_buttons, + $attributes['socialButtons'] + ) + ); + $this->assertCan($actor, 'edit', $user); + } + + $user->social_buttons = $attributes['socialButtons']; + $user->raise(new UserButtonsWereChanged($user)); + } + } + + protected function elementsOnlyRemoved($current, $proposed) + { + $current = json_decode($current); + $proposed = json_decode($proposed); + + foreach ($proposed as $component) { + if (! $this->hasMatchingComponent($current, $component)) { + return false; + } + } + + return true; + } + + protected function hasMatchingComponent($array, $component) + { + $foundMatch = false; + + foreach ($array as $test) { + if ($component == $test) { + $foundMatch = true; + break; + } + } + + return $foundMatch; + } +} diff --git a/src/ProfileValidator.php b/src/ProfileValidator.php index 47f0ade..d4e32e8 100644 --- a/src/ProfileValidator.php +++ b/src/ProfileValidator.php @@ -1,88 +1,88 @@ - - * - * For the full copyright and license information, please view the MIT license - */ - -namespace Davis\SocialProfile; - -use Flarum\Core\Validator\AbstractValidator; -use Flarum\Event\ConfigureValidator; - -class ProfileValidator extends AbstractValidator -{ - /** - * {@inheritdoc} - */ - protected $rules = [ - 'socialButtons' => ['json', 'socialbuttons'], - 'title' => ['string', 'max:55', 'required'], - 'url' => ['url', 'max:120', 'required'], - 'icon' => ['string', 'max:15', 'required'], - 'favicon' => ['string', 'max:120', 'required'], - ]; - - /** - * {@inheritdoc} - */ - protected function getMessages() - { - return [ - 'socialButtons.socialbuttons' => 'The data you sent is missing required variables.', - ]; - } - - /** - * {@inheritdoc} - */ - protected function makeValidator(array $attributes) - { - $rules = array_only($this->getRules(), array_keys($attributes)); - - $this->validator->extend('socialbuttons', function ($attribute, $value, $parameters, $validator) { - return $this->validateSocialButtons($attribute, $value, $parameters, $validator); - }); - $validator = $this->validator->make($attributes, $rules, $this->getMessages()); - - $this->events->fire( - new ConfigureValidator($this, $validator) - ); - - return $validator; - } - - protected function validateSocialButtons($attribute, $value, $parameters, $validator) - { - if ($value != '[]') { - $data = json_decode($value); - - foreach ($data as $button) { - if (!isset($button->title)) { - return false; - } else if (!isset($button->url)) { - return false; - } else if (!isset($button->icon)) { - return false; - } else if (!isset($button->favicon)) { - return false; - } else { - $attributes = [ - 'title' => $button->title, - 'url' => $button->url, - 'icon' => $button->icon, - 'favicon' => $button->favicon, - ]; - $this->assertValid($attributes); - - return true; - } - } - } else { - return true; - } - } -} + + * + * For the full copyright and license information, please view the MIT license + */ + +namespace Davis\SocialProfile; + +use Flarum\Core\Validator\AbstractValidator; +use Flarum\Event\ConfigureValidator; + +class ProfileValidator extends AbstractValidator +{ + /** + * {@inheritdoc} + */ + protected $rules = [ + 'socialButtons' => ['json', 'socialbuttons'], + 'title' => ['string', 'max:55', 'required'], + 'url' => ['url', 'max:120', 'required'], + 'icon' => ['string', 'max:15', 'required'], + 'favicon' => ['string', 'max:120', 'required'], + ]; + + /** + * {@inheritdoc} + */ + protected function getMessages() + { + return [ + 'socialButtons.socialbuttons' => 'The data you sent is missing required variables.', + ]; + } + + /** + * {@inheritdoc} + */ + protected function makeValidator(array $attributes) + { + $rules = array_only($this->getRules(), array_keys($attributes)); + + $this->validator->extend('socialbuttons', function ($attribute, $value, $parameters, $validator) { + return $this->validateSocialButtons($attribute, $value, $parameters, $validator); + }); + $validator = $this->validator->make($attributes, $rules, $this->getMessages()); + + $this->events->fire( + new ConfigureValidator($this, $validator) + ); + + return $validator; + } + + protected function validateSocialButtons($attribute, $value, $parameters, $validator) + { + if ($value != '[]') { + $data = json_decode($value); + + foreach ($data as $button) { + if (! isset($button->title)) { + return false; + } elseif (! isset($button->url)) { + return false; + } elseif (! isset($button->icon)) { + return false; + } elseif (! isset($button->favicon)) { + return false; + } else { + $attributes = [ + 'title' => $button->title, + 'url' => $button->url, + 'icon' => $button->icon, + 'favicon' => $button->favicon, + ]; + $this->assertValid($attributes); + + return true; + } + } + } else { + return true; + } + } +}