Better way to fetch items

This commit is contained in:
Michael 2018-04-26 07:11:18 +00:00
parent b70b020490
commit a0c5a84ce8

View file

@ -19,8 +19,6 @@ use dba;
require_once 'include/items.php'; require_once 'include/items.php';
/// @todo This is some ugly code that needs to be split into several methods
class Delivery { class Delivery {
public static function execute($cmd, $item_id, $contact_id) { public static function execute($cmd, $item_id, $contact_id) {
global $a; global $a;
@ -46,34 +44,33 @@ class Delivery {
} elseif ($cmd == DELIVER_RELOCATION) { } elseif ($cmd == DELIVER_RELOCATION) {
$uid = $item_id; $uid = $item_id;
} else { } else {
// find ancestors $item = dba::selectFirst('item', ['parent'], ['id' => $item_id]);
$target_item = dba::fetch_first("SELECT `item`.*, `contact`.`uid` AS `cuid`, if (!DBM::is_result($item) || empty($item['parent'])) {
`sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
FROM `item`
INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
WHERE `item`.`id` = ? AND `visible` AND NOT `moderated`", $item_id);
if (!DBM::is_result($target_item) || !intval($target_item['parent'])) {
return; return;
} }
$parent_id = intval($item['parent']);
$parent_id = intval($target_item['parent']); $itemdata = dba::p("SELECT `item`.*, `contact`.`uid` AS `cuid`,
$uid = $target_item['cuid']; `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
FROM `item`
if ($parent_id != $item_id) { INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
$parent = dba::fetch_first("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
FROM `item` WHERE `item`.`id` IN (?, ?) AND `visible` AND NOT `moderated`
LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` ORDER BY `item`.`id`",
WHERE `item`.`id` = ? AND `visible` AND NOT `moderated`", $parent_id); $item_id, $parent_id);
if (!DBM::is_result($parent)) { $items = [];
return; while ($item = dba::fetch($itemdata)) {
if ($item['id'] == $parent_id) {
$parent = $item;
} }
$items = [$parent, $target_item]; if ($item['id'] == $item_id) {
} else { $target_item = $item;
$parent = $target_item; }
$items = [$target_item]; $items[] = $item;
} }
dba::close($itemdata);
$uid = $target_item['cuid'];
// avoid race condition with deleting entries // avoid race condition with deleting entries
if ($items[0]['deleted']) { if ($items[0]['deleted']) {