2017-01-17 09:46:43 +01:00
|
|
|
<?php
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Entity\Repository;
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Radio\Adapters;
|
|
|
|
use App\Radio\Configuration;
|
|
|
|
use App\Radio\Frontend\FrontendAbstract;
|
|
|
|
use App\Entity;
|
2018-12-05 08:15:51 +01:00
|
|
|
use App\Sync\Task\Media;
|
2018-11-12 17:59:15 +01:00
|
|
|
use Azura\Doctrine\Repository;
|
2018-12-05 08:15:51 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\Mapping;
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2018-11-12 17:59:15 +01:00
|
|
|
class StationRepository extends Repository
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2018-12-05 08:15:51 +01:00
|
|
|
/** @var Media */
|
|
|
|
protected $media_sync;
|
|
|
|
|
|
|
|
/** @var Adapters */
|
|
|
|
protected $adapters;
|
|
|
|
|
|
|
|
/** @var Configuration */
|
|
|
|
protected $configuration;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
$em,
|
|
|
|
Mapping\ClassMetadata $class,
|
|
|
|
Media $media_sync,
|
|
|
|
Adapters $adapters,
|
|
|
|
Configuration $configuration
|
|
|
|
) {
|
|
|
|
parent::__construct($em, $class);
|
|
|
|
|
|
|
|
$this->media_sync = $media_sync;
|
|
|
|
$this->adapters = $adapters;
|
|
|
|
$this->configuration = $configuration;
|
|
|
|
}
|
|
|
|
|
2017-01-17 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fetchAll()
|
|
|
|
{
|
2017-01-24 01:35:16 +01:00
|
|
|
return $this->_em->createQuery('SELECT s FROM ' . $this->_entityName . ' s ORDER BY s.name ASC')
|
2017-01-17 09:46:43 +01:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $add_blank
|
|
|
|
* @param \Closure|NULL $display
|
|
|
|
* @param string $pk
|
|
|
|
* @param string $order_by
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-24 01:35:16 +01:00
|
|
|
public function fetchSelect($add_blank = false, \Closure $display = null, $pk = 'id', $order_by = 'name')
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2017-01-24 01:35:16 +01:00
|
|
|
$select = [];
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Specify custom text in the $add_blank parameter to override.
|
2017-01-24 01:35:16 +01:00
|
|
|
if ($add_blank !== false) {
|
|
|
|
$select[''] = ($add_blank === true) ? 'Select...' : $add_blank;
|
|
|
|
}
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Build query for records.
|
|
|
|
$results = $this->fetchArray();
|
|
|
|
|
|
|
|
// Assemble select values and, if necessary, call $display callback.
|
2017-01-24 01:35:16 +01:00
|
|
|
foreach ((array)$results as $result) {
|
2017-01-17 09:46:43 +01:00
|
|
|
$key = $result[$pk];
|
2017-01-24 01:35:16 +01:00
|
|
|
$value = ($display === null) ? $result['name'] : $display($result);
|
2017-01-17 09:46:43 +01:00
|
|
|
$select[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $select;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $short_code
|
|
|
|
* @return null|object
|
|
|
|
*/
|
|
|
|
public function findByShortCode($short_code)
|
|
|
|
{
|
2017-12-08 10:51:08 +01:00
|
|
|
return $this->findOneBy(['short_name' => $short_code]);
|
2017-01-17 09:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a station based on the specified data.
|
|
|
|
*
|
2018-02-01 12:49:40 +01:00
|
|
|
* @param array $data Array of data to populate the station with.
|
2017-01-17 09:46:43 +01:00
|
|
|
* @return Entity\Station
|
2017-12-08 10:51:08 +01:00
|
|
|
* @throws \Exception
|
2017-01-17 09:46:43 +01:00
|
|
|
*/
|
2018-12-05 08:15:51 +01:00
|
|
|
public function create($data)
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
|
|
|
$station = new Entity\Station;
|
2017-08-17 20:28:48 +02:00
|
|
|
$this->fromArray($station, $data);
|
2017-05-10 11:27:22 +02:00
|
|
|
|
|
|
|
// Create path for station.
|
2018-02-13 17:31:10 +01:00
|
|
|
$station_base_dir = dirname(APP_INCLUDE_ROOT) . '/stations';
|
2017-05-10 11:27:22 +02:00
|
|
|
|
|
|
|
$station_dir = $station_base_dir . '/' . $station->getShortName();
|
|
|
|
$station->setRadioBaseDir($station_dir);
|
|
|
|
|
2017-01-17 09:46:43 +01:00
|
|
|
$this->_em->persist($station);
|
|
|
|
|
|
|
|
// Generate station ID.
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Scan directory for any existing files.
|
|
|
|
set_time_limit(600);
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->media_sync->importMusic($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
$this->_em->refresh($station);
|
|
|
|
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->media_sync->importPlaylists($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
$this->_em->refresh($station);
|
|
|
|
|
|
|
|
// Load adapters.
|
2018-12-05 08:15:51 +01:00
|
|
|
$frontend_adapter = $this->adapters->getFrontendAdapter($station);
|
|
|
|
$backend_adapter = $this->adapters->getBackendAdapter($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Create default mountpoints if station supports them.
|
2018-02-01 12:49:40 +01:00
|
|
|
$this->resetMounts($station, $frontend_adapter);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Load configuration from adapter to pull source and admin PWs.
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend_adapter->read($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Write the adapter configurations and update supervisord.
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->configuration->writeConfiguration($station, true);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Save changes and continue to the last setup step.
|
|
|
|
$this->_em->persist($station);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
return $station;
|
|
|
|
}
|
|
|
|
|
2017-04-13 00:19:02 +02:00
|
|
|
/**
|
|
|
|
* Reset mount points to their adapter defaults (in the event of an adapter change).
|
|
|
|
*
|
|
|
|
* @param Entity\Station $station
|
2018-02-01 12:49:40 +01:00
|
|
|
* @param FrontendAbstract $frontend_adapter
|
2017-04-13 00:19:02 +02:00
|
|
|
*/
|
2018-02-01 12:49:40 +01:00
|
|
|
public function resetMounts(Entity\Station $station, FrontendAbstract $frontend_adapter)
|
2017-04-13 00:19:02 +02:00
|
|
|
{
|
2017-08-17 20:28:48 +02:00
|
|
|
foreach($station->getMounts() as $mount) {
|
2017-04-13 00:19:02 +02:00
|
|
|
$this->_em->remove($mount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create default mountpoints if station supports them.
|
2018-09-22 13:52:43 +02:00
|
|
|
if ($frontend_adapter::supportsMounts()) {
|
2017-04-13 00:19:02 +02:00
|
|
|
// Create default mount points.
|
2018-09-22 13:52:43 +02:00
|
|
|
$mount_points = $frontend_adapter::getDefaultMounts();
|
2017-04-13 00:19:02 +02:00
|
|
|
|
|
|
|
foreach ($mount_points as $mount_point) {
|
2017-08-17 20:28:48 +02:00
|
|
|
$mount_record = new Entity\StationMount($station);
|
|
|
|
$this->fromArray($mount_record, $mount_point);
|
2017-04-13 00:19:02 +02:00
|
|
|
|
|
|
|
$this->_em->persist($mount_record);
|
|
|
|
}
|
|
|
|
|
2017-05-11 07:48:38 +02:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->refresh($station);
|
|
|
|
}
|
2017-04-13 00:19:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* @param Entity\Station $station
|
2017-12-08 10:51:08 +01:00
|
|
|
* @throws \Exception
|
2017-01-17 09:46:43 +01:00
|
|
|
*/
|
2018-12-05 08:15:51 +01:00
|
|
|
public function destroy(Entity\Station $station)
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->configuration->removeConfiguration($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Remove media folders.
|
|
|
|
$radio_dir = $station->getRadioBaseDir();
|
|
|
|
\App\Utilities::rmdir_recursive($radio_dir);
|
|
|
|
|
|
|
|
// Save changes and continue to the last setup step.
|
|
|
|
$this->_em->remove($station);
|
|
|
|
$this->_em->flush();
|
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|