コード例 #1
0
ファイル: base.php プロジェクト: phpbb/phpbb-core
    /**
     * Find the users who want to receive notifications (helper)
     *
     * @param array|bool $user_ids User IDs to check if they want to receive notifications
     *                             (Bool False to check all users besides anonymous and bots (USER_IGNORE))
     * @param array      $options
     * @return array
     */
    protected function check_user_notification_options($user_ids = false, $options = array())
    {
        $options = array_merge(array('ignore_users' => array(), 'item_type' => $this->get_type(), 'item_id' => 0), $options);
        if ($user_ids === false) {
            $user_ids = array();
            $sql = 'SELECT user_id
				FROM ' . USERS_TABLE . '
				WHERE user_id <> ' . ANONYMOUS . '
					AND user_type <> ' . USER_IGNORE;
            $result = $this->db->sql_query($sql);
            while ($row = $this->db->sql_fetchrow($result)) {
                $user_ids[] = $row['user_id'];
            }
            $this->db->sql_freeresult($result);
        }
        if (empty($user_ids)) {
            return array();
        }
        $rowset = $output = array();
        $sql = 'SELECT user_id, method, notify
			FROM ' . $this->user_notifications_table . '
			WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . "\n\t\t\t\tAND item_type = '" . $this->db->sql_escape($options['item_type']) . "'\n\t\t\t\tAND item_id = " . (int) $options['item_id'];
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) {
                continue;
            }
            if (!isset($rowset[$row['user_id']])) {
                $rowset[$row['user_id']] = array();
            }
            $rowset[$row['user_id']][$row['method']] = $row['notify'];
            if (!isset($output[$row['user_id']])) {
                $output[$row['user_id']] = array();
            }
            if ($row['notify']) {
                $output[$row['user_id']][] = $row['method'];
            }
        }
        $this->db->sql_freeresult($result);
        $default_methods = $this->notification_manager->get_default_methods();
        foreach ($user_ids as $user_id) {
            if (isset($options['ignore_users'][$user_id])) {
                continue;
            }
            if (!array_key_exists($user_id, $rowset)) {
                // No rows at all for this user, use the default methods
                $output[$user_id] = $default_methods;
            } else {
                foreach ($default_methods as $default_method) {
                    if (!array_key_exists($default_method, $rowset[$user_id])) {
                        // No user preference for this type recorded, but it should be enabled by default.
                        $output[$user_id][] = $default_method;
                    }
                }
            }
        }
        return $output;
    }