X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FNotificationsManager.php;h=8ac5d93c729d33779573990638b978af602dcf3e;hb=c34204cf50ced68a14d220706c4dc6ef93eb94c9;hp=756a5ceb4b714cd04d8b469eefbb6e4c449bee12;hpb=070aa016e0915c6bba9326a94f7394acd2b21e7f;p=friendica.git diff --git a/src/Core/NotificationsManager.php b/src/Core/NotificationsManager.php index 756a5ceb4b..8ac5d93c72 100644 --- a/src/Core/NotificationsManager.php +++ b/src/Core/NotificationsManager.php @@ -9,8 +9,6 @@ namespace Friendica\Core; use Friendica\BaseObject; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; -use Friendica\Core\Logger; -use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Model\Item; @@ -38,7 +36,7 @@ class NotificationsManager extends BaseObject * - msg_plain: message as plain text string * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private function _set_extra($notes) + private function _set_extra(array $notes) { $rets = []; foreach ($notes as $n) { @@ -57,50 +55,28 @@ class NotificationsManager extends BaseObject * @brief Get all notifications for local_user() * * @param array $filter optional Array "column name"=>value: filter query by columns values - * @param string $order optional Space separated list of column to sort by. - * Prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date" + * @param array $order optional Array to order by * @param string $limit optional Query limits * - * @return array of results or false on errors + * @return array|bool of results or false on errors * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function getAll($filter = [], $order = "-date", $limit = "") + public function getAll($filter = [], $order = ['date' => 'DESC'], $limit = "") { - $filter_str = []; - $filter_sql = ""; - foreach ($filter as $column => $value) { - $filter_str[] = sprintf("`%s` = '%s'", $column, DBA::escape($value)); - } - if (count($filter_str) > 0) { - $filter_sql = "AND " . implode(" AND ", $filter_str); - } + $params = []; - $aOrder = explode(" ", $order); - $asOrder = []; - foreach ($aOrder as $o) { - $dir = "asc"; - if ($o[0] === "-") { - $dir = "desc"; - $o = substr($o, 1); - } - if ($o[0] === "+") { - $dir = "asc"; - $o = substr($o, 1); - } - $asOrder[] = "$o $dir"; - } - $order_sql = implode(", ", $asOrder); + $params['order'] = $order; - if ($limit != "") { - $limit = " LIMIT " . $limit; + if (!empty($limit)) { + $params['limit'] = $limit; } - $r = q( - "SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit", - intval(local_user()) - ); - if (DBA::isResult($r)) { - return $this->_set_extra($r); + $dbFilter = array_merge($filter, ['uid' => local_user()]); + + $stmtNotifies = DBA::select('notify', [], $dbFilter, $params); + + if (DBA::isResult($stmtNotifies)) { + return $this->_set_extra(DBA::toArray($stmtNotifies)); } return false; @@ -115,13 +91,9 @@ class NotificationsManager extends BaseObject */ public function getByID($id) { - $r = q( - "SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($id), - intval(local_user()) - ); - if (DBA::isResult($r)) { - return $this->_set_extra($r)[0]; + $stmtNotify = DBA::selectFirst('notify', [], ['id' => $id, 'uid' => local_user()]); + if (DBA::isResult($stmtNotify)) { + return $this->_set_extra([$stmtNotify])[0]; } return null; } @@ -136,14 +108,13 @@ class NotificationsManager extends BaseObject */ public function setSeen($note, $seen = true) { - return q( - "UPDATE `notify` SET `seen` = %d WHERE (`link` = '%s' OR (`parent` != 0 AND `parent` = %d AND `otype` = '%s')) AND `uid` = %d", - intval($seen), - DBA::escape($note['link']), - intval($note['parent']), - DBA::escape($note['otype']), - intval(local_user()) - ); + return DBA::update('notify', ['seen' => $seen], [ + '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?', + $note['link'], + $note['parent'], + $note['otype'], + local_user() + ]); } /** @@ -155,11 +126,7 @@ class NotificationsManager extends BaseObject */ public function setAllSeen($seen = true) { - return q( - "UPDATE `notify` SET `seen` = %d WHERE `uid` = %d", - intval($seen), - intval(local_user()) - ); + return DBA::update('notify', ['seen' => $seen], ['uid' => local_user()]); } /** @@ -231,7 +198,6 @@ class NotificationsManager extends BaseObject */ private function formatNotifs(array $notifs, $ident = "") { - $notif = []; $arr = []; if (DBA::isResult($notifs)) { @@ -428,7 +394,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; $items = Item::selectForUser(local_user(), $fields, $condition, $params); @@ -463,20 +429,22 @@ class NotificationsManager extends BaseObject $notifs = []; $sql_seen = ""; + $filter = ['uid' => local_user()]; if ($seen === 0) { - $sql_seen = " AND NOT `seen` "; + $filter['seen'] = false; } - $r = q( - "SELECT `id`, `url`, `photo`, `msg`, `date`, `seen`, `verb` FROM `notify` - WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ", - intval(local_user()), - intval($start), - intval($limit) - ); + $params = []; + $params['order'] = ['date' => 'DESC']; + $params['limit'] = [$start, $limit]; - if (DBA::isResult($r)) { - $notifs = $this->formatNotifs($r, $ident); + $stmtNotifies = DBA::select('notify', + ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'], + $filter, + $params); + + if (DBA::isResult($stmtNotifies)) { + $notifs = $this->formatNotifs(DBA::toArray($stmtNotifies), $ident); } $arr = [ @@ -517,7 +485,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; $items = Item::selectForUser(local_user(), $fields, $condition, $params); @@ -559,7 +527,7 @@ class NotificationsManager extends BaseObject $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['created' => true], 'limit' => [$start, $limit]]; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; $items = Item::selectForUser(local_user(), $fields, $condition, $params); if (DBA::isResult($items)) { @@ -595,11 +563,11 @@ class NotificationsManager extends BaseObject $sql_extra = ""; if (!$all) { - $sql_extra = " AND `ignore` = 0 "; + $sql_extra = " AND NOT `ignore` "; } /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact - $r = q( + $stmtNotifies = DBA::p( "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`, `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`, @@ -610,14 +578,14 @@ class NotificationsManager extends BaseObject LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` - WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 - LIMIT %d, %d", - intval($_SESSION['uid']), - intval($start), - intval($limit) + WHERE `intro`.`uid` = ? $sql_extra AND `intro`.`blocked` = 0 + LIMIT ?, ?", + $_SESSION['uid'], + $start, + $limit ); - if (DBA::isResult($r)) { - $notifs = $this->formatIntros($r); + if (DBA::isResult($stmtNotifies)) { + $notifs = $this->formatIntros(DBA::toArray($stmtNotifies)); } $arr = [ @@ -640,6 +608,8 @@ class NotificationsManager extends BaseObject { $knowyou = ''; + $arr = []; + foreach ($intros as $it) { // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. // We have to distinguish between these two because they use different data.