use DataAccess\StepMania\ISimfileRepository;\r
use DataAccess\StepMania\IPackRepository;\r
use DataAccess\IFileRepository;\r
+use DataAccess\IDownloadRepository;\r
use Domain\Entities\StepMania\ISimfile;\r
use Domain\Entities\IFile;\r
+use Domain\Entities\StepMania\IPack;\r
use Domain\Util;\r
\r
class SimfileController implements IDivineController\r
private $_uploadManager;\r
private $_zipParser;\r
private $_smoMatcher;\r
+ private $_downloadRepository;\r
\r
public function __construct(\r
IHttpResponse $response,\r
IFileRepository $fileRepository,\r
IUserSession $userSession,\r
IZipParser $zipParser,\r
- ISMOMatcher $smoMatcher\r
+ ISMOMatcher $smoMatcher,\r
+ IDownloadRepository $downloadRepository\r
) {\r
$this->_response = $response;\r
$this->_uploadManager = $uploadManager;\r
$this->_fileRepository = $fileRepository;\r
$this->_zipParser = $zipParser;\r
$this->_smoMatcher = $smoMatcher;\r
+ $this->_downloadRepository = $downloadRepository;\r
}\r
\r
public function indexAction() {\r
\r
foreach($packs as $pack)\r
{\r
- $packSimfiles = array();\r
- foreach($pack->getSimfiles() as $simfile)\r
- {\r
- $packSimfiles[] = $this->simfileToArray($simfile);\r
- }\r
-\r
- $packMirrors = array();\r
- \r
- if($pack->getFile())\r
- {\r
- $packMirrors[] = array('source' => 'DivinElegy', 'uri' => 'files/pack/' . $pack->getFile()->getHash());\r
- }\r
- \r
- if($pack->getFile()->getMirrors())\r
- {\r
- foreach($pack->getFile()->getMirrors() as $mirror)\r
- {\r
- $packMirrors[] = array('source' => $mirror->getSource(), 'uri' => $mirror->getUri());\r
- }\r
- }\r
- \r
$packArray[] = array(\r
'title'=> $pack->getTitle(),\r
'contributors' => $pack->getContributors(),\r
- 'simfiles' => $packSimfiles,\r
+ 'simfiles' => $this->getPackSimfilesArray($pack),\r
'banner' => $pack->getBanner() ? 'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',\r
- 'mirrors' => $packMirrors,\r
- 'size' => $pack->getFile() ? Util::bytesToHumanReadable($pack->getFile()->getSize()) : null\r
+ 'mirrors' => $this->getPackMirrorsArray($pack),\r
+ 'size' => $pack->getFile() ? Util::bytesToHumanReadable($pack->getFile()->getSize()) : null,\r
+ 'uploaded' => $pack->getFile() ? date('F jS, Y', $pack->getFile()->getUploadDate()) : null\r
);\r
}\r
\r
->sendResponse();\r
}\r
\r
+ public function latestSimfileAction()\r
+ {\r
+ $simfile = $this->_simfileRepository->findRange(0, -1);\r
+ $this->_response->setHeader('Content-Type', 'application/json')\r
+ ->setBody(json_encode($this->simfileToArray(reset($simfile))))\r
+ ->sendResponse();\r
+ }\r
+ \r
+ public function latestPackAction()\r
+ {\r
+ $pack = $this->_packRepository->findRange(0, -1);\r
+ $pack = reset($pack);\r
+ \r
+ $packArray = array(\r
+ 'title'=> $pack->getTitle(),\r
+ 'contributors' => $pack->getContributors(),\r
+ 'simfiles' => $this->getPackSimfilesArray($pack),\r
+ 'banner' => $pack->getBanner() ? 'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',\r
+ 'mirrors' => $this->getPackMirrorsArray($pack),\r
+ 'size' => $pack->getFile() ? Util::bytesToHumanReadable($pack->getFile()->getSize()) : null,\r
+ 'uploaded' => $pack->getFile() ? date('F jS, Y', $pack->getFile()->getUploadDate()) : null\r
+ );\r
+ \r
+ $this->_response->setHeader('Content-Type', 'application/json')\r
+ ->setBody(json_encode($packArray))\r
+ ->sendResponse();\r
+ }\r
+ \r
+ public function popularAction()\r
+ {\r
+ $returnArray = array();\r
+ $popularDownloads = $this->_downloadRepository->findPopular();\r
+ $popularDownloads = reset($popularDownloads);\r
+ $packOrFileId = $popularDownloads->getFile()->getId();\r
+ \r
+ $simfile = $this->_simfileRepository->findByFileId($packOrFileId);\r
+ if($simfile)\r
+ {\r
+ $returnArray = $this->simfileToArray(reset($simfile));\r
+ } else {\r
+ $pack = $this->_packRepository->findByFileId($packOrFileId);\r
+ $pack = reset($pack);\r
+ $returnArray = array(\r
+ 'title'=> $pack->getTitle(),\r
+ 'contributors' => $pack->getContributors(),\r
+ 'simfiles' => $this->getPackSimfilesArray($pack),\r
+ 'banner' => $pack->getBanner() ? 'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',\r
+ 'mirrors' => $this->getPackMirrorsArray($pack),\r
+ 'size' => $pack->getFile() ? Util::bytesToHumanReadable($pack->getFile()->getSize()) : null,\r
+ 'uploaded' => $pack->getFile() ? date('F jS, Y', $pack->getFile()->getUploadDate()) : null\r
+ );\r
+ }\r
+ \r
+ $this->_response->setHeader('Content-Type', 'application/json')\r
+ ->setBody(json_encode($returnArray))\r
+ ->sendResponse();\r
+ \r
+ }\r
+ \r
public function uploadAction()\r
{ \r
//TODO: Put directory in config ?\r
}\r
}\r
\r
+ private function getPackMirrorsArray(IPack $pack)\r
+ {\r
+ $packMirrors = array();\r
+\r
+ if($pack->getFile())\r
+ {\r
+ $packMirrors[] = array('source' => 'DivinElegy', 'uri' => 'files/pack/' . $pack->getFile()->getHash());\r
+ }\r
+\r
+ if($pack->getFile()->getMirrors())\r
+ {\r
+ foreach($pack->getFile()->getMirrors() as $mirror)\r
+ {\r
+ $packMirrors[] = array('source' => $mirror->getSource(), 'uri' => $mirror->getUri());\r
+ }\r
+ }\r
+ \r
+ return $packMirrors;\r
+ }\r
+ \r
+ private function getPackSimfilesArray(IPack $pack)\r
+ {\r
+ $packSimfiles = array();\r
+ foreach($pack->getSimfiles() as $simfile)\r
+ {\r
+ $packSimfiles[] = $this->simfileToArray($simfile);\r
+ }\r
+ \r
+ return $packSimfiles;\r
+ }\r
+ \r
private function findAndAddSmoMirror(IFile $file)\r
{\r
$basename = pathinfo($file->getFilename(), PATHINFO_FILENAME);\r
'bpmChanges' => $simfile->hasBPMChanges() ? 'Yes' : 'No',\r
'banner' => $simfile->getBanner() ? 'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default',\r
'download' => $simfile->getSimfile() ? 'files/simfile/' . $simfile->getSimfile()->getHash() : null,\r
- 'size' => $simfile->getSimfile() ? Util::bytesToHumanReadable($simfile->getSimfile()->getSize()) : null\r
+ 'size' => $simfile->getSimfile() ? Util::bytesToHumanReadable($simfile->getSimfile()->getSize()) : null,\r
+ 'uploaded' => $simfile->getSimfile() ? date('F jS, Y', $simfile->getSimfile()->getUploadDate()) : null\r
);\r
}\r
}\r
protected $_whereClauses = array();
protected $_limitClause;
protected $_joinClauses = array();
+ protected $_orderClause;
+ protected $_countClause;
+ protected $_groupClause;
public function buildQuery()
{
$this->applyJoinClauses()
->applyWhereClauses()
- ->applyLimitClause();
-
+ ->applyGroupClause()
+ ->applyOrderClause()
+ ->applyLimitClause()
+ ->applyCountClause();
+
return $this->_queryString;
}
return $this->where($columnName, 'is', null);
}
+ public function count($column, $as = null)
+ {
+ if($as)
+ {
+ $this->_countClause = sprintf(', count(%s) as %s', $column, $as);
+ } else {
+ $this->_countClause = sprintf(', count(%s)', $column);
+ }
+
+ return $this;
+ }
+
+ public function group($column)
+ {
+ $this->_groupClause = sprintf(' group by %s', $column);
+
+ return $this;
+ }
+
+ private function applyCountClause()
+ {
+ $pos = strpos($this->_queryString, '*')+1;
+ $this->_queryString = substr_replace($this->_queryString, $this->_countClause, $pos, 0);
+
+ return $this;
+ }
+
+ private function applyGroupClause()
+ {
+ $this->_queryString .= $this->_groupClause;
+
+ return $this;
+ }
+
private function applyJoinClauses()
{
foreach($this->_joinClauses as $joinClause)
return $this;
}
+ public function orderBy($column, $direction)
+ {
+ $this->_orderClause = sprintf(' ORDER BY %s %s', $column, $direction);
+
+ return $this;
+ }
+
private function applyWhereClauses()
{
+ if(!$this->_whereClauses) return $this;
+
$this->_queryString .= ' WHERE ';
foreach($this->_whereClauses as $whereClause)
return $this;
}
+ private function applyOrderClause()
+ {
+ $this->_queryString .= $this->_orderClause;
+ return $this;
+ }
+
}
\ No newline at end of file