示例#1
0
    /**
     * Get a single alert by ID.
     *
     * @param int $id The ID of the alert to fetch.
     *
     * @return MybbSTuff_MyAlerts_Entity_Alert
     */
    public function getAlert($id = 0)
    {
        $id = (int) $id;
        $alert = null;
        $this->mybb->user['uid'] = (int) $this->mybb->user['uid'];
        $prefix = TABLE_PREFIX;
        $alertsQuery = <<<SQL
SELECT a.*, u.uid, u.username, u.avatar, u.usergroup, u.displaygroup, t.code FROM {$prefix}alerts a
LEFT JOIN {$prefix}users u ON (a.from_user_id = u.uid)
INNER JOIN {$prefix}alert_types t ON (a.alert_type_id = t.id)
WHERE a.uid = {$this->mybb->user['uid']} AND a.id = {$id};
SQL;
        $query = $this->db->write_query($alertsQuery);
        if ($this->db->num_rows($query) > 0) {
            while ($alertRow = $this->db->fetch_array($query)) {
                $alertType = $this->alertTypeManager->getByCode($alertRow['code']);
                if ($alertType != null) {
                    $alert = new MybbStuff_MyAlerts_Entity_Alert($alertRow['uid'], $alertType, $alertRow['object_id']);
                    $alert->setId($alertRow['id']);
                    $alert->setCreatedAt(new DateTime($alertRow['dateline']));
                    $alert->setUnread((bool) $alertRow['unread']);
                    $alert->setExtraDetails(json_decode($alertRow['extra_details'], true));
                    $user = array('uid' => (int) $alertRow['uid'], 'username' => $alertRow['username'], 'avatar' => $alertRow['avatar'], 'usergroup' => $alertRow['usergroup'], 'displaygroup' => $alertRow['displaygroup']);
                    $alert->setFromUser($user);
                }
            }
        }
        return $alert;
    }