protected function savePermissions($type)
{
foreach (Request::$post['permissions'] as $group => $perms) {
$permissions = [];
if ($group == 'defaults') {
foreach (PermissionsAPI::getPermissions() as $name => $default) {
if (isset($perms[$name])) {
$permissions[$name] = true;
} else {
$permissions[$name] = false;
}
}
$this->db->update(PREFIX . 'permissions', ['permissions' => json_encode($permissions)], ['type' => $type, 'type_id' => 0, 'project_id' => 0]);
} else {
// Ignore 'null' values
foreach ($perms as $name => $value) {
if ($value == '1' || $value == '0') {
$permissions[$name] = (bool) $value;
}
}
// If there are no permissions, delete the row
if (!count($permissions)) {
$this->db->delete(PREFIX . 'permissions', ['type' => $type, 'type_id' => $group, 'project_id' => 0]);
} else {
// Check if the row exists already
$query = queryBuilder()->select('id')->from(PREFIX . 'permissions')->where('type = ?')->andWhere('type_id = ?')->andWhere('project_id = ?')->setParameter(0, $type)->setParameter(1, $group)->setParameter(2, 0)->execute();
// Update the row
if ($query->rowCount()) {
$this->db->update(PREFIX . 'permissions', ['permissions' => json_encode($permissions)], ['type' => $type, 'type_id' => $group, 'project_id' => 0]);
} else {
// Insert a new row
$this->db->insert(PREFIX . 'permissions', ['type' => $type, 'type_id' => $group, 'project_id' => 0, 'permissions' => json_encode($permissions)]);
}
}
}
}
}