123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace Drupal\edlp_studio\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Drupal\Core\Session\AccountInterface;
- use Drupal\User\UserDataInterface;
- use Drupal\Core\Url;
- use Drupal\Core\Language\LanguageInterface;
- use Symfony\Component\HttpFoundation\JsonResponse;
- // use Drupal\Core\Cache\CacheableJsonResponse;
- // use Drupal\Core\Cache\CacheableMetadata;
- use Drupal\core\render\RenderContext;
- use Drupal\edlp_studio\Entity\Chutier;
- /**
- * Class StudioUIController.
- */
- class StudioUIController extends ControllerBase {
- protected $user;
- protected $userdata;
- /**
- * Class constructor.
- */
- public function __construct(AccountInterface $account, UserDataInterface $userdata) {
- $this->user = $account;
- $this->userdata = $userdata;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container) {
- // Instantiates this form class.
- return new static(
- // Load the service required to construct this class.
- $container->get('current_user'),
- $container->get('user.data')
- );
- }
- /**
- * Studio-ui.
- *
- * @return array
- * Return renderable array.
- */
- public function StudioUI() {
- return $this->buildStudioUI();
- }
- /**
- * Studio-ui.
- *
- * @return array
- * Return renderable array.
- */
- public function StudioUIJson() {
- $renderable = $this->buildStudioUI();
- $rendered = render($renderable);
- $data = ['rendered'=>$rendered];
- // translations links
- // use Drupal\Core\Url;
- // use Drupal\Core\Language\LanguageInterface;
- $route_name = 'edlp_studio.studio_ui';
- $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
- if (isset($links->links)) {
- $translations_build = [
- '#theme' => 'links__language_block',
- '#links' => $links->links,
- '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
- '#set_active_class' => TRUE,
- ];
- $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
- $data['translations_links'] = $translations_rendered;
- }
- // JSON
- $response = new JsonResponse();
- $response->setData($data);
- return $response;
- }
- public function StudioChutierUIJson(){
- $renderable = $this->buildChutierUI();
- $rendered = render($renderable);
- // TODO: make response cachable
- // JSON
- $response = new JsonResponse();
- $response->setData([
- 'rendered'=> $rendered,
- ]);
- return $response;
- }
- private function buildStudioUI(){
- return [
- '#theme' => 'edlp_studio_ui',
- '#chutier_ui' => $this->buildChutierUI(),
- '#composition_ui' => $this->buildCompostionUI(),
- ];
- }
- private function buildChutierUI(){
- // get his docs in chutier
- $documents_nids = Chutier::getUserChutiersContents($this->user->id());
- // dpm($documents_nids);
- $documents = entity_load_multiple('node', $documents_nids);
- // build content renderable array
- $chutier_ui = array(
- "#theme"=>'edlp_chutier_ui',
- '#title' => t('Favorites'),
- "#document_nodes" => $documents,
- '#uid' => $this->user->id(),
- );
- return $chutier_ui;
- }
- private function buildCompostionUI(){
- // build content renderable array
- $composition_ui = array(
- "#theme"=>'edlp_composition_ui',
- "#title" => t('Composition'),
- "#compositions" => $this->buildCompostionsList(),
- "#composer_header" => t("Create a new playlist, then drag and drop your bookmarked sounds on the timeline below"),
- "#lastcomposition" => $this->getLastCompo(),
- '#composer_actions' => array(
- 'play_composition'=>array(
- "#type"=>'container',
- "#attributes"=>array(
- "class"=>array("compo-player-controls")
- )
- ),// TODO: add social media links (what about composition visibility uotside studio_ui ?)
- ),
- );
- return $composition_ui;
- }
- private function buildCompostionsList(){
- // get compositions
- $query = \Drupal::entityQuery('composition')
- ->condition('user_id', $this->user->id());
- $compos_ids = $query->execute();
- // dpm($compos_ids);
- if(!count($compos_ids)){
- // create default compos
- $def_compos = \Drupal::entityManager()
- ->getStorage('composition')
- ->create(array(
- 'name' => 'composition',
- 'uid' => $this->user->id()
- )
- );
- $def_compos->save();
- $compos_ids = array($def_compos->id());
- }
- $compos = entity_load_multiple('composition', $compos_ids);
- $createurl = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'create'], ['absolute' => TRUE]);
- return array(
- '#theme' => 'edlp_compositions_list',
- '#composition_entities' => $compos,
- '#new_composition_url' => $createurl,
- );
- }
- private function getLastCompo(){
- // just get the last compositions
- $query = \Drupal::entityQuery('composition')
- ->condition('user_id', $this->user->id())
- ->range(0,1)
- ->sort('created', 'DESC');
- $compos_id = $query->execute();
- if(count($compos_id)){
- $lastcompo = entity_load('composition', array_pop($compos_id));
- }else{
- $lastcompo = null;
- }
- return $lastcompo;
- }
- }
|