Skip to content

Commit

Permalink
pkp#10506 Fix setData() method and snake case usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Nov 29, 2024
1 parent 82a3050 commit 41c503d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 101 deletions.
31 changes: 15 additions & 16 deletions classes/core/SettingsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Illuminate\Support\Str;
use PKP\core\traits\EntityUpdate;
use stdClass;
use PKP\facades\Locale;

class SettingsBuilder extends Builder
{
Expand Down Expand Up @@ -159,19 +158,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
if ($column instanceof ConditionExpression || $column instanceof Closure) {
return parent::where($column, $operator, $value, $boolean);
}

// Prepare value and operator
[$value, $operator] = $this->query->prepareValueAndOperator(
$value,
$operator,
func_num_args() === 2
);

$modelSettingsList = $this->model->getSettings();

$settings = [];
$primaryColumn = null;

if (is_string($column)) {
if (in_array($column, $modelSettingsList)) {
$settings[$column] = $value;
Expand All @@ -183,11 +182,11 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
$primaryColumn = array_diff_key($column, $settings);
$primaryColumn = array_map([$this, 'getSnakeKey'], array_keys($primaryColumn));
}

if (empty($settings)) {
return parent::where($primaryColumn ?? $column, $operator, $value, $boolean);
}

// Handle settings
$where = [];
foreach ($settings as $settingName => $settingValue) {
Expand All @@ -196,20 +195,20 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
'setting_value' => $settingValue,
]);
}

$this->query->whereIn(
$this->model->getKeyName(),
fn (QueryBuilder $query) =>
$query->select($this->getPrimaryKeyName())->from($this->getSettingsTable())->where($where, null, null, $boolean)
);

if (!empty($primaryColumn)) {
parent::where($primaryColumn, $operator, $value, $boolean);
}

return $this;
}

protected function getSnakeKey($key)
{
return Str::snake($key);
Expand All @@ -230,13 +229,13 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
if ($column instanceof Expression) {
return parent::whereIn($column, $values, $boolean, $not);
}

$column = $this->getSnakeKey($column);

if (!in_array($column, $this->model->getSettings())) {
return parent::whereIn($column, $values, $boolean, $not);
}

$this->query->whereIn(
$this->model->getKeyName(),
fn (QueryBuilder $query) =>
Expand All @@ -246,10 +245,10 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
->where('setting_name', $column)
->whereIn('setting_value', $values, $boolean, $not)
);

return $this;
}


public function getSettingsTable(): ?string
{
Expand Down
18 changes: 7 additions & 11 deletions classes/install/PKPInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,6 @@ public function createData()
$user->setInlineHelp(1);
Repo::user()->add($user);

// Create an admin user group
$adminUserGroup = new UserGroup([
'roleId' => Role::ROLE_ID_SITE_ADMIN,
'contextId' => \PKP\core\PKPApplication::SITE_CONTEXT_ID,
'isDefault' => true,
'permitSettings' => true,
]);

// Prepare multilingual 'name' and 'namePlural' settings
$names = [];
$namePlurals = [];
Expand All @@ -248,8 +240,12 @@ public function createData()
$namePlurals[$locale] = __('default.groups.plural.siteAdmin', [], $locale);
}

// Set the 'name' and 'namePlural' settings
$adminUserGroup->fill([
// Create an admin user group
$adminUserGroup = new UserGroup([
'roleId' => Role::ROLE_ID_SITE_ADMIN,
'contextId' => \PKP\core\PKPApplication::SITE_CONTEXT_ID,
'isDefault' => true,
'permitSettings' => true,
'name' => $names,
'namePlural' => $namePlurals,
]);
Expand All @@ -259,7 +255,7 @@ public function createData()

// Assign the user to the admin user group
$repository = Repo::userGroup();
$repository->assignUserToGroup($user->getId(), $adminUserGroup->userGroupId);
$repository->assignUserToGroup($user->getId(), $adminUserGroup->id);

// Add initial site data
/** @var SiteDAO */
Expand Down
Loading

0 comments on commit 41c503d

Please sign in to comment.