diff --git a/controller/contribution/base.php b/controller/contribution/base.php index 1ab810483..35719c334 100644 --- a/controller/contribution/base.php +++ b/controller/contribution/base.php @@ -172,7 +172,7 @@ protected function generate_navigation($page) if ($this->contrib->contrib_demo) { $demo_menu = array(); - $allowed_branches = $this->contrib->type->get_allowed_branches(true); + $allowed_branches = $this->contrib->get_approved_branches(); krsort($allowed_branches); $is_external = $this->contrib->contrib_status != ext::TITANIA_CONTRIB_APPROVED || !$this->contrib->options['demo']; diff --git a/controller/contribution/manage.php b/controller/contribution/manage.php index a4ba5f753..4ff5e7a7b 100644 --- a/controller/contribution/manage.php +++ b/controller/contribution/manage.php @@ -125,7 +125,7 @@ public function manage($contrib_type, $contrib) ); $this->settings['custom'] = $this->contrib->get_custom_fields(); - foreach ($this->contrib->type->get_allowed_branches(true) as $branch => $name) + foreach ($this->contrib->get_approved_branches() as $branch => $name) { $this->settings['demo'][$branch] = $this->contrib->get_demo_url($branch); } @@ -145,7 +145,7 @@ public function manage($contrib_type, $contrib) )); $demos = $this->request->variable('demo', array(0 => '')); - foreach ($this->contrib->type->get_allowed_branches(true) as $branch => $name) + foreach ($this->contrib->get_approved_branches() as $branch => $name) { if (isset($demos[$branch])) { @@ -155,7 +155,8 @@ public function manage($contrib_type, $contrib) $this->contrib->post_data($this->message); $this->contrib->__set_array(array( - 'contrib_demo' => ($this->can_edit_demo) ? json_encode($this->settings['demo']) : $this->contrib->contrib_demo, + // Preserve stored URLs for branches not open for editing. + 'contrib_demo' => ($this->can_edit_demo) ? json_encode($this->settings['demo'] + $this->contrib->get_demo_urls()) : $this->contrib->contrib_demo, 'contrib_limited_support' => $this->settings['limited_support'], )); } diff --git a/includes/objects/contribution.php b/includes/objects/contribution.php index 9818f7db0..47d45a10e 100644 --- a/includes/objects/contribution.php +++ b/includes/objects/contribution.php @@ -1035,6 +1035,80 @@ public function get_url($page = '', $parameters = array()) return $this->controller_helper->route($controller, $parameters); } + /** + * Get the branches for which the contribution has an approved revision. + * + * @return array Branch names limited to the branches that have an approved + * revision, keyed by branch - example: 31 => 'phpBB 3.1.x' + */ + public function get_approved_branches() + { + $allowed_branches = $this->type->get_allowed_branches(true); + + $this->get_download(); + if (!empty($this->download)) + { + return array_intersect_key($allowed_branches, $this->download); + } + + // get_download() may intentionally return early (e.g. downloads disabled for non-team) + // so fall back to checking which branches have an approved, validated revision. + $sql = 'SELECT DISTINCT(phpbb_version_branch), MAX(revision_id) AS revision_id + FROM ' . TITANIA_REVISIONS_PHPBB_TABLE . ' + WHERE contrib_id = ' . (int) $this->contrib_id . ' + AND revision_validated = 1 + GROUP BY phpbb_version_branch'; + $result = phpbb::$db->sql_query($sql); + $revisions = array(); + while ($row = phpbb::$db->sql_fetchrow($result)) + { + $revisions[(int) $row['phpbb_version_branch']] = (int) $row['revision_id']; + } + phpbb::$db->sql_freeresult($result); + + if (empty($revisions)) + { + return array(); + } + + $sql = 'SELECT revision_id + FROM ' . TITANIA_REVISIONS_TABLE . ' + WHERE contrib_id = ' . (int) $this->contrib_id . ' + AND ' . phpbb::$db->sql_in_set('revision_id', array_values($revisions)) . ' + AND revision_status = ' . ext::TITANIA_REVISION_APPROVED . ' + AND revision_submitted = 1'; + $result = phpbb::$db->sql_query($sql); + $revisions = array_flip($revisions); // revision_id => branch + $approved = array(); + while ($row = phpbb::$db->sql_fetchrow($result)) + { + $branch = (int) $revisions[(int) $row['revision_id']]; + if (isset($allowed_branches[$branch])) + { + $approved[$branch] = $allowed_branches[$branch]; + } + } + phpbb::$db->sql_freeresult($result); + + return $approved; + } + + /** + * Get all stored demo URLs. + * + * @return array Demo URLs keyed by branch - example: 31 => 'http://...' + */ + public function get_demo_urls() + { + if (empty($this->contrib_demo)) + { + return array(); + } + $demos = json_decode($this->contrib_demo, true); + + return (is_array($demos)) ? $demos : array(); + } + /** * Get demo URL. * @@ -1045,11 +1119,7 @@ public function get_url($page = '', $parameters = array()) */ public function get_demo_url($branch, $integrated_url = false) { - if (empty($this->contrib_demo)) - { - return ''; - } - $demos = json_decode($this->contrib_demo, true); + $demos = $this->get_demo_urls(); if (empty($demos[$branch])) { diff --git a/styles/prosilver/template/contributions/contribution_manage.html b/styles/prosilver/template/contributions/contribution_manage.html index 15b961d27..066fc2de1 100644 --- a/styles/prosilver/template/contributions/contribution_manage.html +++ b/styles/prosilver/template/contributions/contribution_manage.html @@ -143,7 +143,7 @@

{{ lang('OPTIONS') }}

- {% if S_CAN_EDIT_DEMO %} + {% if S_CAN_EDIT_DEMO and loops.demo|length %}

{{ lang('DEMO_URL_EXPLAIN') }}