<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[1739] sites/trunk/translate.wordpress.org: Translate: Add paging to the project portals.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/1739">1739</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/1739","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-07-14 05:42:00 +0000 (Tue, 14 Jul 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Translate: Add paging to the project portals. Currently paging is set to 20 per page.
See <a href="http://meta.trac.wordpress.org/ticket/1091">#1091</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunktranslatewordpressorgincludesgppluginswporgroutesrouteslocalephp">sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php</a></li>
<li><a href="#sitestrunktranslatewordpressorgpublic_htmlgptemplateslocaleprojectsphp">sites/trunk/translate.wordpress.org/public_html/gp-templates/locale-projects.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunktranslatewordpressorgincludesgppluginswporgroutesrouteslocalephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php    2015-07-14 03:49:50 UTC (rev 1738)
+++ sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php      2015-07-14 05:42:00 UTC (rev 1739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,6 +14,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $project_path     Path of a project.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_locale_projects( $locale_slug, $set_slug = 'default', $project_path = 'wp' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                global $gpdb;
+
+               $per_page = 20;
+               $page = (int) gp_get( 'page', 1 );
+               $search = gp_get( 's', '' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $locale = GP_Locales::by_slug( $locale_slug );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $locale ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return $this->die_with_404();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,24 +30,39 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $this->die_with_404();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $sub_projects = $this->get_active_sub_projects( $project );
-               if ( ! $sub_projects ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $paged_sub_projects = $this->get_paged_active_sub_projects(
+                       $project,
+                       array(
+                               'page' => $page,
+                               'per_page' => $per_page,
+                               'orderby' => 'name',
+                               'search' => $search,
+                               'set_slug' => $set_slug,
+                               'locale' => $locale_slug,
+                       )
+               );
+
+               if ( ! $paged_sub_projects ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $this->die_with_404();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                usort( $sub_projects, array( $this, '_sort_name_callback' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $sub_projects   = $paged_sub_projects['projects'];
+               $pages          = $paged_sub_projects['pages'];
+               unset( $paged_sub_projects );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $project_status = $project_icons = array();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $sub_projects as $key => $sub_project ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $status = $this->get_project_status( $sub_project, $locale_slug, $set_slug );
-                       if ( ! $status->all_count ) {
-                               unset( $sub_projects[ $key ] );
-                       }
-
-                       $project_status[ $sub_project->id ] = $status;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $project_status[ $sub_project->id ] = $this->get_project_status( $sub_project, $locale_slug, $set_slug );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $project_icons[ $sub_project->id ] = $this->get_project_icon( $project, $sub_project );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $project_ids = array_keys( $project_status );
+               $project_ids[] = $project->id;
+               $project_ids = array_merge(
+                       $project_ids,
+                       $gpdb->get_col( "SELECT id FROM {$gpdb->projects} WHERE parent_project_id IN(" . implode(', ', $project_ids  ) . ")" )
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $contributors_count ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $contributors_count = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,7 +71,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $top_level_projects = GP::$project->top_level();
</span><span class="cx" style="display: block; padding: 0 10px">                usort( $top_level_projects, array( $this, '_sort_reverse_name_callback' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $variants = $this->get_locale_variants( $locale_slug, array_keys( $project_status ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $variants = $this->get_locale_variants( $locale_slug, $project_ids );
+               // If there were no results for the current variant in the current project branch, it should still show it.
+               if ( ! in_array( $set_slug, $variants, true ) ) {
+                       $variants[] = $set_slug;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->tmpl( 'locale-projects', get_defined_vars() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -118,6 +143,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                switch( $project->slug ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'wp':
</span><span class="cx" style="display: block; padding: 0 10px">                                return '<div class="wordpress-icon"><span class="dashicons dashicons-wordpress-alt"></span></div>';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        case 'wp-themes':
+                               $screenshot = gp_get_meta( 'wp-themes', $sub_project->id, 'screenshot' );
+                               if ( $screenshot ) {
+                                       return '<div class="theme icon"><img src="https://i0.wp.com/' . $screenshot . '?w=' . $size . '&amp;strip=all"></div>';
+                               } else {
+                                       return '<div class="default-icon"><span class="dashicons dashicons-admin-appearance"></span></div>';
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         case 'bbpress':
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'buddypress':
</span><span class="cx" style="display: block; padding: 0 10px">                                require_once WPORGPATH . 'extend/plugins-plugins/_plugin-icons.php';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,6 +286,91 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $projects;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Retrieves active sub projects with paging.
+        *
+        * This method is horribly inefficient when there exists many sub-projects, as it can't use SQL.
+        *
+        * @param GP_Project $project           The parent project
+        * @param array $args {
+        *      @type int    $per_page Number of items per page. Default 20
+        *      @type int    $page     The page of results to view. Default 1.
+        *      @type string $orderby  The field to order by, id or name. Default id.
+        *      @type string $order    The sorting order, ASC or DESC. Default ASC.
+        *      @type string $search   The search string
+        *      @type string $set_slug The translation set to view.
+        *      @type string $locale   The locale of the translation set to view.
+        * }
+        * @return array List of sub projects.
+        */
+       private function get_paged_active_sub_projects( $project, $args = array() ) {
+               global $gpdb;
+
+               $defaults = array(
+                       'per_page' => 20,
+                       'page'     => 1,
+                       'orderby'  => 'id',
+                       'order'    => 'ASC',
+                       'search'   => false,
+                       'set_slug' => '',
+                       'locale'   => '',
+               );
+               $r = wp_parse_args( $args, $defaults );
+               extract( $r, EXTR_SKIP );
+
+               $order = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
+               $orderby = ( in_array( $orderby, array( 'id', 'name' ) ) ? $orderby : 'id' );
+
+               $limit_sql = '';
+               if ( $per_page ) {
+                       $limit_sql = $gpdb->prepare( 'LIMIT %d, %d', ( $page - 1 ) * $per_page, $per_page );
+               }
+               $search_sql = '';
+               if ( $search ) {
+                       $esc_search = '%%' . like_escape( $search ) . '%%';
+                       $search_sql = $gpdb->prepare( 'AND ( tp.name LIKE %s OR tp.slug LIKE %s )', $esc_search, $esc_search );
+               }
+
+               /*
+                * Find all child projects with translation sets that match the current locale/slug.
+                *
+                * 1. We need to fetch all sub-projects of the current project (so, if we're at wp-plugins, we want akismet, debug bar, importers, etc)
+                * 2. Next, we fetch the sub-projects of those sub-projects, that gets us things like Development, Readme, etc.
+                * 3. Next, we fetch the translation sets of both the sub-projects(1), and any sub-sub-projects(2).
+                * Once we have the sets in 3, we can then check to see if there exists any translation sets for the current (locale, slug) (ie. en-au/default)
+                * If not, we can simply filter them out, so that paging only has items returned that actually exist.
+                */
+               $translation_sets_table = GP::$translation_set->table;
+               $_projects = $project->many( "
+                       SELECT SQL_CALC_FOUND_ROWS tp.*
+                               FROM {$project->table} tp
+                               LEFT JOIN {$project->table} tp_sub ON tp.id = tp_sub.parent_project_id AND tp_sub.active = 1
+                               LEFT JOIN {$translation_sets_table} sets ON sets.project_id = tp.id AND sets.locale = %s AND sets.slug = %s
+                               LEFT JOIN {$translation_sets_table} sets_sub ON sets_sub.project_id = tp_sub.id AND sets_sub.locale = %s AND sets_sub.slug = %s
+                       WHERE
+                               tp.parent_project_id = %d
+                               AND tp.active = 1
+                               AND ( sets.id IS NOT NULL OR sets_sub.id IS NOT NULL )
+                               $search_sql
+                       GROUP BY tp.id
+                       ORDER BY tp.$orderby $order
+                       $limit_sql
+               ", $locale, $set_slug, $locale, $set_slug, $project->id  );
+
+               $results = (int) $project->found_rows();
+               $pages = (int)ceil( $results / $per_page );
+
+               $projects = array();
+               foreach ( $_projects as $project ) {
+                       $projects[ $project->id ] = $project;
+               }
+
+               return array(
+                       'pages' => compact( 'pages', 'page', 'per_page', 'results' ),
+                       'projects' => $projects,
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         private function _sort_reverse_name_callback( $a, $b ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return - strcasecmp( $a->name, $b->name );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="sitestrunktranslatewordpressorgpublic_htmlgptemplateslocaleprojectsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/translate.wordpress.org/public_html/gp-templates/locale-projects.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/translate.wordpress.org/public_html/gp-templates/locale-projects.php  2015-07-14 03:49:50 UTC (rev 1738)
+++ sites/trunk/translate.wordpress.org/public_html/gp-templates/locale-projects.php    2015-07-14 05:42:00 UTC (rev 1739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,8 +67,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ?>
</span><span class="cx" style="display: block; padding: 0 10px">        </ul>
</span><span class="cx" style="display: block; padding: 0 10px">        <div class="search-form">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                <label class="screen-reader-text" for="projects-filter"><?php esc_attr_e( 'Search projects...' ); ?></label>
-               <input placeholder="<?php esc_attr_e( 'Search projects...' ); ?>" type="search" id="projects-filter" class="filter-search">
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         <form>
+                       <label class="screen-reader-text" for="projects-filter"><?php esc_attr_e( 'Search projects...' ); ?></label>
+                       <input placeholder="<?php esc_attr_e( 'Search projects...' ); ?>" type="search" id="projects-filter" name="s" value="<?php if ( !empty( $search ) ) { echo esc_attr( $search ); } ?>" class="filter-search">
+                       <input type="submit" value="<?php esc_attr_e( 'Search' ); ?>" class="screen-reader-text" />
+               </form>
</ins><span class="cx" style="display: block; padding: 0 10px">         </div>
</span><span class="cx" style="display: block; padding: 0 10px"> </div>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -139,11 +142,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px">        ?>
</span><span class="cx" style="display: block; padding: 0 10px"> </div>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+       if ( isset( $pages ) && $pages['pages'] > 1 ) {
+               echo gp_pagination( $pages['page'], $pages['per_page'], $pages['results'] );
+       }
+?>
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> <script>
</span><span class="cx" style="display: block; padding: 0 10px">        jQuery( document ).ready( function( $ ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Don't filter if there's an existing search term, or if we're paginated
+               // Fall back to a full page reload for those cases.
+               var live_filtering_enabled = ( ! $( '#projects-filter' ).val() && ! $( '.paging' ).length );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $rows = $( '#projects' ).find( '.project' );
</span><span class="cx" style="display: block; padding: 0 10px">                $( '#projects-filter' ).on( 'input keyup', function() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( ! live_filtering_enabled ) {
+                               return;
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         var words = this.value.toLowerCase().split( ' ' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( '' === this.value.trim() ) {
</span></span></pre>
</div>
</div>

</body>
</html>