Skip to content

Commit

Permalink
Small simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
flack committed Dec 22, 2023
1 parent d0980f2 commit d72487e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 43 deletions.
56 changes: 27 additions & 29 deletions lib/midcom/baseclasses/core/dbobject.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ private static function _set_owner_privileges(midcom_core_dbaobject $object)
*/
public static function create_pre_checks(midcom_core_dbaobject $object) : bool
{
$parent = $object->get_parent();

if ($parent !== null) {
if ($parent = $object->get_parent()) {
// Attachments are a special case
if ($object instanceof midcom_db_attachment) {
if ( !$parent->can_do('midgard:attachments')
Expand Down Expand Up @@ -443,19 +441,19 @@ public static function get_by_id(midcom_core_dbaobject $object, int $id) : bool

$object->__object->get_by_id($id);

if ($object->id != 0) {
if (!$object->can_do('midgard:read')) {
debug_add("Failed to load object, read privilege on the " . get_class($object) . " {$object->guid} not granted for the current user.",
MIDCOM_LOG_ERROR);
$object->__object = new $object->__mgdschema_class_name__;
return false;
}

$object->_on_loaded();
return true;
if ($object->id == 0) {
debug_add("Failed to load the record identified by {$id}, last Midgard error was:" . midcom_connection::get_error_string(), MIDCOM_LOG_INFO);
return false;
}
debug_add("Failed to load the record identified by {$id}, last Midgard error was:" . midcom_connection::get_error_string(), MIDCOM_LOG_INFO);
return false;
if (!$object->can_do('midgard:read')) {
debug_add("Failed to load object, read privilege on the " . get_class($object) . " {$object->guid} not granted for the current user.",
MIDCOM_LOG_ERROR);
$object->__object = new $object->__mgdschema_class_name__;
return false;
}

$object->_on_loaded();
return true;
}

/**
Expand All @@ -471,12 +469,12 @@ public static function get_by_guid(midcom_core_dbaobject $object, string $guid)
}
$object->__object->get_by_guid($guid);

if ($object->id != 0) {
$object->_on_loaded();
return true;
if ($object->id == 0) {
debug_add("Failed to load the record identified by {$guid}, last Midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_INFO);
return false;
}
debug_add("Failed to load the record identified by {$guid}, last Midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_INFO);
return false;
$object->_on_loaded();
return true;
}

/**
Expand All @@ -487,16 +485,16 @@ public static function get_by_path(midcom_core_dbaobject $object, string $path)
{
$object->__object->get_by_path($path);

if ($object->id != 0) {
if (!$object->can_do('midgard:read')) {
$object->__object = new $object->__mgdschema_class_name__;
return false;
}

$object->_on_loaded();
return true;
if ($object->id == 0) {
return false;
}
if (!$object->can_do('midgard:read')) {
$object->__object = new $object->__mgdschema_class_name__;
return false;
}
return false;

$object->_on_loaded();
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/midcom/core/nullcomponent/handler/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function _handler_index(array &$data)
midcom::get()->metadata->set_request_metadata($this->_topic->metadata->revised, $this->_topic->guid);
$data['node'] = $this->_topic;

if ($this->_topic->id == midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC)->id
if ( $this->_topic->id == midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC)->id
&& !midcom::get()->config->get('midcom_root_component')) {
// if we have an ad hoc root topic, give user some options to do the initial setup
return new midcom_response_relocate($this->router->generate('wizard'));
Expand Down
1 change: 0 additions & 1 deletion lib/midcom/core/nullcomponent/style/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
?>
&(node.code:p);
<?php

}
?>
3 changes: 1 addition & 2 deletions lib/midcom/core/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public function get_first_group_guid() : ?string
$mc->set_key_property('gid');
$mc->set_limit(1);
$mc->execute();
$result = $mc->list_keys();
if (!empty($result)) {
if ($result = $mc->list_keys()) {
if ($group = midcom::get()->auth->get_group(key($result))) {
return $group->get_storage()->guid;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/midcom/db/attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function open(string $mode = 'w')
}

if ($this->_open_handle !== null) {
debug_add("Warning, the Attachment {$this->id} already had an open file handle, we close it implicitly.", MIDCOM_LOG_WARN);
debug_add("Warning, the attachment {$this->id} already had an open file handle, we close it implicitly.", MIDCOM_LOG_WARN);
$this->close();
}

Expand Down
4 changes: 1 addition & 3 deletions lib/midcom/helper/search/handler/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ private function process_results(array $result, int $page)

if ($count == 0) {
midcom::get()->cache->content->uncached();
}

if ($count > 0) {
} else {
$results_per_page = $this->_config->get('results_per_page');
$max_pages = ceil($count / $results_per_page);
$page = min($page, $max_pages);
Expand Down
5 changes: 1 addition & 4 deletions lib/midcom/response/relocate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public function setTargetUrl($url)
{
if ( !str_starts_with($url, "/")
&& !preg_match('|^https?://|', $url)) {
$prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
if ($prefix == '') {
$prefix = '/';
}
$prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) ?: '/';
$url = $prefix . $url;
debug_add("This is a relative URL from the local site, prepending anchor prefix: {$url}");
}
Expand Down
3 changes: 1 addition & 2 deletions lib/midgard/admin/asgard/handler/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ private function _prepare_toolbar(array $data)
$qb = new midgard_query_builder($this->type);
$qb->include_deleted();
$qb->add_constraint('metadata.deleted', '=', true);
$deleted = $qb->count();
if ($deleted > 0) {
if ($deleted = $qb->count()) {
$buttons[] = [
MIDCOM_TOOLBAR_URL => $this->router->generate('trash_type', ['type' => $this->type]),
MIDCOM_TOOLBAR_LABEL => sprintf($this->_l10n->get('%s deleted items'), $deleted),
Expand Down

0 comments on commit d72487e

Please sign in to comment.