From: Hans-Emil Skogh Date: Sun, 11 Dec 2011 14:34:21 +0000 (+0100) Subject: Added grid view when listing apps in browse repository. X-Git-Tag: 0.1~1063^2~20 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=52a8fde86cc7213178d8551d43a419254cea05dc;p=fdroidserver.git Added grid view when listing apps in browse repository. Also, the WP plugin will now pass on all of the relevant query arguments via generated links by default. --- diff --git a/wp-fdroid/wp-fdroid.php b/wp-fdroid/wp-fdroid.php index 8a79ad6d..2cd8b98a 100644 --- a/wp-fdroid/wp-fdroid.php +++ b/wp-fdroid/wp-fdroid.php @@ -16,9 +16,10 @@ class FDroid { // Our text domain, for internationalisation - var $textdom='wp-fdroid'; + private $textdom='wp-fdroid'; - var $site_path = "/home/fdroid/public_html"; + // TODO: Fix site path... + private $site_path = "/home/hansemil/MyDocuments/f-droid"; // Constructor function FDroid() { @@ -34,6 +35,7 @@ class FDroid $qvars[]='fdfilter'; $qvars[]='fdid'; $qvars[]='fdpage'; + $qvars[]='fdstyle'; return $qvars; } @@ -55,19 +57,6 @@ class FDroid return $wp_query->query_vars[$name]; } - // Make a link to this page, with the given query parameter string added - function makelink($params) { - $link=get_permalink(); - if(strlen($params)==0) - return $link; - if(strpos($link,'?')===false) - $link.='?'; - else - $link.='&'; - $link.=$params; - return $link; - } - // Handler for the 'fdroidrepo' shortcode. // $attribs - shortcode attributes // $content - optional content enclosed between the starting and @@ -77,32 +66,37 @@ class FDroid global $wp_query,$wp_rewrite; $this->lazyinit(); - $page=1; - if(isset($wp_query->query_vars['fdpage'])) { - $page=(int)$wp_query->query_vars['fdpage']; - if($page==0) - $page=1; - } - - $filter=$wp_query->query_vars['fdfilter']; - $fdid=$wp_query->query_vars['fdid']; - - if($fdid!==null) - $out=$this->get_app($fdid); + + // Init local query vars + foreach($this->queryvars(array()) as $qv) { + if(array_key_exists($qv,$wp_query->query_vars)) { + $query_vars[$qv] = $wp_query->query_vars[$qv]; + } else { + $query_vars[$qv] = null; + } + } + + // Santiy check query vars + if(!isset($query_vars['fdpage']) || !is_numeric($query_vars['fdpage']) || $query_vars['fdpage'] <= 0) { + $query_vars['fdpage'] = 1; + } + + if($query_vars['fdid']!==null) + $out=$this->get_app($query_vars); else - $out=$this->get_apps($page,$filter); + $out=$this->get_apps($query_vars); return $out; } - function get_app($id) { + function get_app($query_vars) { $xml = simplexml_load_file($this->site_path."/repo/index.xml"); foreach($xml->children() as $app) { $attrs=$app->attributes(); - if($attrs['id']==$id) { + if($attrs['id']==$query_vars['fdid']) { $apks=array();; foreach($app->children() as $el) { switch($el->getName()) { @@ -199,7 +193,7 @@ class FDroid $out.="

"; } - $out.='

Index

'; + $out.='

Index

'; return $out; } @@ -208,98 +202,217 @@ class FDroid } - function get_apps($page,$filter=null) { + function get_apps($query_vars) { + + $out=''; - if($filter===null) - $out="

All applications"; + $out.='

'; + if($query_vars['fdfilter']===null) + $out.="All applications"; else - $out="

Applications matching ".$filter; - $out.="

"; + $out.="Applications matching ".$query_vars['fdfilter']; + $out.="
"; + + $out.='
'; + $out.='List | '; + $out.='Grid'; + $out.='
'; + + $out.='
'; + + $xml = simplexml_load_file($this->site_path."/repo/index.xml"); + $out.=$this->show_apps($xml,$query_vars,$numpages); + + $out.='

'; + if($query_vars['fdpage']==1) { + $out.="<<first "; + $out.="<prev "; + } else { + $out.='<<first '; + $out.='<<prev '; + } + $out.=' Page '.$query_vars['fdpage'].' of '.$numpages.' '; + if($query_vars['fdpage']==$numpages) { + $out.="next> "; + $out.="last>> "; + } else { + $out.='next> '; + $out.='last>> '; + } + $out.='

'; + + return $out; + } - $perpage=30; + + function show_apps($xml,$query_vars,&$numpages) { + $skipped=0; $got=0; $total=0; - $xml = simplexml_load_file($this->site_path."/repo/index.xml"); + if($query_vars['fdstyle']=='grid') { + $outputter = new FDOutGrid(); + } else { + $outputter = new FDOutList(); + } + + $out = ""; + + $out.=$outputter->outputStart(); + foreach($xml->children() as $app) { if($app->getName() == 'repo') continue; - $attrs=$app->attributes(); - $id=$attrs['id']; + $appinfo['attrs']=$app->attributes(); + $appinfo['id']=$appinfo['attrs']['id']; foreach($app->children() as $el) { switch($el->getName()) { case "name": - $name=$el; + $appinfo['name']=$el; break; case "icon": - $icon=$el; + $appinfo['icon']=$el; break; case "summary": - $summary=$el; + $appinfo['summary']=$el; break; case "license": - $license=$el; + $appinfo['license']=$el; break; } } - if($filter===null || stristr($name,$filter)) { - if($skipped<($page-1)*$perpage) { + if($query_vars['fdfilter']===null || stristr($appinfo['name'],$query_vars['fdfilter'])) { + if($skipped<($query_vars['fdpage']-1)*$outputter->perpage) { $skipped++; - } else if($got<$perpage) { - - $out.="
\n"; - $out.='
'; - - $out.='
'; - - $out.='
'; - $out.='

Details...'; - $out.="

"; - $out.="
\n"; - - $out.='

'.$name.""; - $out.="
".$summary."

\n"; - - $out.="
\n"; + } else if($got<$outputter->perpage) { + $out.=$outputter->outputEntry($query_vars, $appinfo); $got++; } $total++; } } + + $out.=$outputter->outputEnd(); + + $numpages = ceil((float)$total/$outputter->perpage); + + return $out; + } +} - $numpages=ceil((float)$total/$perpage); - - $out.='

'; - if($page==1) { - $out.="<<first "; - $out.="<prev "; - } else { - $out.='<<first '; - $out.='<<prev '; - } - $out.=" Page $page of $numpages "; - if($page==$numpages) { - $out.="next> "; - $out.="last>> "; - } else { - $out.='next> '; - $out.='last>> '; - } - $out.='

'; - - return $out; - } +// Class to output app entries in a detailed list format +class FDOutList +{ + var $perpage=30; + + function FDOutList() { + } + + function outputStart() { + return ''; + } + + function outputEntry($query_vars, $appinfo) { + $out=""; + $out.="
\n"; + $out.='
'; + + $out.='
'; + + $out.='
'; + $out.='

Details...'; + $out.="

"; + $out.="
\n"; + + $out.='

'.$appinfo['name'].""; + $out.="
".$appinfo['summary']."

\n"; + + $out.="
\n"; + + return $out; + } + + function outputEnd() { + return ''; + } +} +// Class to output app entries in a compact grid format +class FDOutGrid +{ + var $perpage=80; + + var $itemCount = 0; + + function FDOutGrid() { + } + + function outputStart() { + return "\n".''."\n"; + } + + function outputEntry($query_vars, $appinfo) { + $link=makelink($query_vars, array('fdid'=>$appinfo['id'])); + + $out=''; + + if($this->itemCount%4 == 0 && $this->itemCount > 0) + { + $out.=''."\n"; + } + + $out.='\n"; + + $this->itemCount++; + return $out; + } + + function outputEnd() { + return '
'; + $out.='

'; + $out.='

"; + $out.='

'; + $out.="
'."\n"; + } +} +// Make a link to this page, with the current query vars attached and desired params added/modified +function makelink($query_vars, $params=array()) { + $link=get_permalink(); + $vars=linkify(array_merge($query_vars, $params)); + if(strlen($vars)==0) + return $link; + if(strpos($link,'?')===false) + $link.='?'; + else + $link.='&'; + return $link.$vars; +} +// Return the key value pairs in http-get-parameter format as a string +function linkify($vars) { + $retvar = ''; + foreach($vars as $k => $v) { + if($k!==null && $v!==null && $v!='') + $retvar .= $k.'='.$v.'&'; + } + return substr($retvar,0,-1); } + $wp_fdroid = new FDroid();