Exemple #1
0
    /**
     * Display contributions
     *
     * @param string $mode The mode (category, author)
     * @param int $id The parent id (only show contributions under this category, author, etc)
     * @param int|bool $branch	Branch to limit results to: 20|30|31. Defaults to false.
     * @param \phpbb\titania\sort|bool $sort
     * @param string $blockname The name of the template block to use (contribs by default)
     *
     * @return array
     */
    public static function display_contribs($mode, $id, $branch = false, $sort = false, $blockname = 'contribs')
    {
        phpbb::$user->add_lang_ext('phpbb/titania', 'contributions');
        $tracking = phpbb::$container->get('phpbb.titania.tracking');
        $types = phpbb::$container->get('phpbb.titania.contribution.type.collection');
        // Setup the sort tool if not sent, then request
        if ($sort === false) {
            $sort = self::build_sort();
        }
        $sort->request();
        $branch = $branch ? (int) $branch : null;
        $select = 'DISTINCT(c.contrib_id), c.contrib_name, c.contrib_name_clean,
			c.contrib_status, c.contrib_downloads, c.contrib_views, c.contrib_rating,
			c.contrib_rating_count, c.contrib_type, c.contrib_last_update, c.contrib_user_id,
			c.contrib_limited_support, c.contrib_categories, c.contrib_desc, c.contrib_desc_uid';
        switch ($mode) {
            case 'author':
                // Get the contrib_ids this user is an author in (includes as a co-author)
                $contrib_ids = titania::$cache->get_author_contribs($id, $types, phpbb::$user);
                if (!sizeof($contrib_ids)) {
                    return compact('sort');
                }
                $sql_ary = array('SELECT' => $select . ', a.attachment_id, a.thumbnail', 'FROM' => array(TITANIA_CONTRIBS_TABLE => 'c'), 'LEFT_JOIN' => array(array('FROM' => array(TITANIA_ATTACHMENTS_TABLE => 'a'), 'ON' => 'c.contrib_id = a.object_id
								AND a.object_type = ' . TITANIA_SCREENSHOT . '
								AND a.is_orphan = 0
								AND a.is_preview = 1')), 'WHERE' => phpbb::$db->sql_in_set('c.contrib_id', $contrib_ids) . '
						AND c.contrib_visible = 1', 'ORDER_BY' => $sort->get_order_by());
                break;
            case 'category':
                $sql_ary = array('SELECT' => $select . ', a.attachment_id, a.thumbnail', 'FROM' => array(TITANIA_CONTRIB_IN_CATEGORIES_TABLE => 'cic'), 'LEFT_JOIN' => array(array('FROM' => array(TITANIA_CONTRIBS_TABLE => 'c'), 'ON' => 'cic.contrib_id = c.contrib_id'), array('FROM' => array(TITANIA_REVISIONS_PHPBB_TABLE => 'rp'), 'ON' => 'cic.contrib_id = rp.contrib_id'), array('FROM' => array(TITANIA_ATTACHMENTS_TABLE => 'a'), 'ON' => 'c.contrib_id = a.object_id
								AND a.object_type = ' . TITANIA_SCREENSHOT . '
								AND a.is_orphan = 0
								AND a.is_preview = 1')), 'WHERE' => (is_array($id) && sizeof($id) ? phpbb::$db->sql_in_set('cic.category_id', array_map('intval', $id)) : 'cic.category_id = ' . (int) $id) . '
						AND c.contrib_visible = 1' . ($branch ? " AND rp.phpbb_version_branch = {$branch}" : ''), 'ORDER_BY' => $sort->get_order_by());
                break;
            case 'all':
                $sql_ary = array('SELECT' => $select . ', a.attachment_id, a.thumbnail', 'FROM' => array(TITANIA_CONTRIBS_TABLE => 'c'), 'LEFT_JOIN' => array(array('FROM' => array(TITANIA_REVISIONS_PHPBB_TABLE => 'rp'), 'ON' => 'c.contrib_id = rp.contrib_id'), array('FROM' => array(TITANIA_ATTACHMENTS_TABLE => 'a'), 'ON' => 'c.contrib_id = a.object_id
								AND a.object_type = ' . TITANIA_SCREENSHOT . '
								AND a.is_orphan = 0
								AND a.is_preview = 1')), 'WHERE' => 'c.contrib_visible = 1' . ($branch ? " AND rp.phpbb_version_branch = {$branch}" : ''), 'ORDER_BY' => $sort->get_order_by());
                break;
        }
        $tracking->get_track_sql($sql_ary, TITANIA_CONTRIB, 'c.contrib_id');
        $mod_contrib_mod = (bool) phpbb::$auth->acl_get('u_titania_mod_contrib_mod');
        // Permissions
        if (!$mod_contrib_mod) {
            $sql_ary['SELECT'] .= ', cc.user_id AS coauthor';
            $sql_ary['LEFT_JOIN'][] = array('FROM' => array(TITANIA_CONTRIB_COAUTHORS_TABLE => 'cc'), 'ON' => 'cc.contrib_id = c.contrib_id AND cc.user_id = ' . phpbb::$user->data['user_id']);
            $view_unapproved = array();
            if ($types->find_authed('moderate')) {
                $view_unapproved = array_merge($view_unapproved, $types->find_authed('moderate'));
            }
            if ($types->find_authed('view')) {
                $view_unapproved = array_merge($view_unapproved, $types->find_authed('view'));
            }
            $view_unapproved = array_unique($view_unapproved);
            $sql_ary['WHERE'] .= ' AND (' . phpbb::$db->sql_in_set('c.contrib_status', array(TITANIA_CONTRIB_APPROVED, TITANIA_CONTRIB_DOWNLOAD_DISABLED)) . (sizeof($view_unapproved) ? ' OR ' . phpbb::$db->sql_in_set('c.contrib_type', array_map('intval', $view_unapproved)) : '') . '
				OR c.contrib_user_id = ' . phpbb::$user->data['user_id'] . '
				OR cc.active = 1)';
        }
        // Main SQL Query
        $sql = phpbb::$db->sql_build_query('SELECT', $sql_ary);
        // Handle pagination
        if (!$sort->sql_count($sql_ary, 'DISTINCT(c.contrib_id)')) {
            // No results...no need to query more...
            return compact('sort');
        }
        $controller_helper = phpbb::$container->get('controller.helper');
        $path_helper = phpbb::$container->get('path_helper');
        $access = phpbb::$container->get('phpbb.titania.access');
        $url = $path_helper->get_url_parts($controller_helper->get_current_url());
        $sort->build_pagination($url['base']);
        $result = phpbb::$db->sql_query_limit($sql, $sort->limit, $sort->start);
        $contrib_ids = $user_ids = array();
        while ($row = phpbb::$db->sql_fetchrow($result)) {
            //Check to see if user has permission
            if (!$mod_contrib_mod && $row['contrib_user_id'] != phpbb::$user->data['user_id'] && $row['coauthor'] != phpbb::$user->data['user_id'] && !$access->is_team()) {
                //If the contribution has a status that is not accessible by the current user let's not add it
                if (in_array($row['contrib_status'], array(TITANIA_CONTRIB_NEW, TITANIA_CONTRIB_CLEANED, TITANIA_CONTRIB_HIDDEN, TITANIA_CONTRIB_DISABLED))) {
                    continue;
                }
            }
            $user_ids[] = $row['contrib_user_id'];
            $contrib_ids[] = $row['contrib_id'];
            self::$contribs[$row['contrib_id']] = $row;
        }
        phpbb::$db->sql_freeresult($result);
        // Get user data
        users_overlord::load_users($user_ids);
        // Get phpBB versions
        if (sizeof($contrib_ids)) {
            $validation_free = $types->find_validation_free();
            if (sizeof($validation_free) && titania::$config->require_validation) {
                $sql = 'SELECT rp.contrib_id, rp.phpbb_version_branch, rp.phpbb_version_revision
					FROM ' . TITANIA_REVISIONS_PHPBB_TABLE . ' rp, ' . TITANIA_CONTRIBS_TABLE . ' c
					WHERE ' . phpbb::$db->sql_in_set('rp.contrib_id', array_map('intval', $contrib_ids)) . '
					AND c.contrib_id = rp.contrib_id
					AND (rp.revision_validated = 1
						OR ' . phpbb::$db->sql_in_set('c.contrib_type', $validation_free) . ')
					ORDER BY rp.row_id DESC';
            } else {
                $sql = 'SELECT contrib_id, phpbb_version_branch, phpbb_version_revision FROM ' . TITANIA_REVISIONS_PHPBB_TABLE . '
					WHERE ' . phpbb::$db->sql_in_set('contrib_id', array_map('intval', $contrib_ids)) . (titania::$config->require_validation ? ' AND revision_validated = 1' : '') . '
					ORDER BY row_id DESC';
            }
            $result = phpbb::$db->sql_query($sql);
            while ($row = phpbb::$db->sql_fetchrow($result)) {
                self::$contribs[$row['contrib_id']]['phpbb_versions'][] = $row;
            }
            phpbb::$db->sql_freeresult($result);
        }
        // Setup some objects we'll use for temps
        $contrib = new titania_contribution();
        $contrib->author = new titania_author();
        $versions = titania::$cache->get_phpbb_versions();
        $author_contribs = titania::$cache->get_author_contribs(phpbb::$user->data['user_id'], $types, phpbb::$user, true);
        // Get the mark all tracking
        $tracking->get_track(TITANIA_CONTRIB, 0);
        foreach ($contrib_ids as $contrib_id) {
            $row = self::$contribs[$contrib_id];
            $contrib->__set_array($row);
            $contrib->set_type($row['contrib_type']);
            $contrib->author->user_id = $contrib->contrib_user_id;
            $contrib->author->__set_array($row);
            $contrib->fill_categories();
            // Author contrib variables
            $contrib->is_author = $contrib->contrib_user_id == phpbb::$user->data['user_id'] ? true : false;
            $contrib->is_active_coauthor = in_array($contrib->contrib_id, $author_contribs) ? true : false;
            $rating = new \titania_rating('contrib', $contrib);
            $rating->cannot_rate = true;
            $contrib->rating = $rating;
            // Store the tracking info we grabbed from the DB
            $tracking->store_from_db($row);
            // Get the folder image
            $folder_img = $folder_alt = '';
            $last_read_mark = $tracking->get_track(TITANIA_CONTRIB, $contrib->contrib_id, true);
            $last_complete_mark = $tracking->get_track(TITANIA_CONTRIB, 0, true);
            $is_unread = $contrib->contrib_last_update > $last_read_mark && $contrib->contrib_last_update > $last_complete_mark ? true : false;
            phpbb::$container->get('phpbb.titania.display')->topic_folder_img($folder_img, $folder_alt, 0, $is_unread);
            // Only get unique phpBB versions supported
            if (isset($row['phpbb_versions'])) {
                $ordered_phpbb_versions = versions::order_phpbb_version_list_from_db(titania::$cache, $row['phpbb_versions'], $contrib->options['all_versions']);
            }
            $preview_params = array();
            $stripped_desc = message::generate_clean_excerpt($contrib->contrib_desc, $contrib->contrib_desc_uid, 255, '…');
            if (!empty($row['attachment_id'])) {
                $preview_params['id'] = $row['attachment_id'];
                if ($row['thumbnail']) {
                    $preview_params['thumb'] = 1;
                }
            }
            phpbb::$template->assign_block_vars($blockname, array_merge($contrib->assign_details(true, true), array('FOLDER_STYLE' => $folder_img, 'FOLDER_IMG' => phpbb::$user->img($folder_img, $folder_alt), 'FOLDER_IMG_SRC' => phpbb::$user->img($folder_img, $folder_alt, false, '', 'src'), 'FOLDER_IMG_ALT' => phpbb::$user->lang[$folder_alt], 'FOLDER_IMG_ALT' => phpbb::$user->lang[$folder_alt], 'FOLDER_IMG_WIDTH' => phpbb::$user->img($folder_img, '', false, '', 'width'), 'FOLDER_IMG_HEIGHT' => phpbb::$user->img($folder_img, '', false, '', 'height'), 'PHPBB_VERSION' => isset($row['phpbb_versions']) && sizeof($ordered_phpbb_versions) == 1 ? $ordered_phpbb_versions[0] : '', 'DESC_SNIPPET' => $stripped_desc, 'PREVIEW' => $preview_params ? $controller_helper->route('phpbb.titania.download', $preview_params) : '')));
            if (isset($row['phpbb_versions'])) {
                $prev_branch = '';
                foreach ($ordered_phpbb_versions as $version_row) {
                    phpbb::$template->assign_block_vars($blockname . '.phpbb_versions', array('NAME' => $version_row));
                    $branch = versions::get_branch_from_string($version_row);
                    if ($prev_branch != $branch) {
                        phpbb::$template->assign_block_vars($blockname . '.branches', array('NAME' => $version_row));
                    }
                    $prev_branch = $branch;
                }
            }
            $contrib_type = $row['contrib_type'];
        }
        unset($contrib);
        return compact('sort');
    }