StudioUIController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Drupal\edlp_studio\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Drupal\User\UserDataInterface;
  7. use Drupal\Core\Url;
  8. use Drupal\Core\Language\LanguageInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. // use Drupal\Core\Cache\CacheableJsonResponse;
  11. // use Drupal\Core\Cache\CacheableMetadata;
  12. use Drupal\core\render\RenderContext;
  13. use Drupal\edlp_studio\Entity\Chutier;
  14. /**
  15. * Class StudioUIController.
  16. */
  17. class StudioUIController extends ControllerBase {
  18. protected $user;
  19. protected $userdata;
  20. /**
  21. * Class constructor.
  22. */
  23. public function __construct(AccountInterface $account, UserDataInterface $userdata) {
  24. $this->user = $account;
  25. $this->userdata = $userdata;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function create(ContainerInterface $container) {
  31. // Instantiates this form class.
  32. return new static(
  33. // Load the service required to construct this class.
  34. $container->get('current_user'),
  35. $container->get('user.data')
  36. );
  37. }
  38. /**
  39. * Studio-ui.
  40. *
  41. * @return array
  42. * Return renderable array.
  43. */
  44. public function StudioUI() {
  45. return $this->buildStudioUI();
  46. }
  47. /**
  48. * Studio-ui.
  49. *
  50. * @return array
  51. * Return renderable array.
  52. */
  53. public function StudioUIJson() {
  54. $renderable = $this->buildStudioUI();
  55. $rendered = render($renderable);
  56. $data = ['rendered'=>$rendered];
  57. // translations links
  58. // use Drupal\Core\Url;
  59. // use Drupal\Core\Language\LanguageInterface;
  60. $route_name = 'edlp_studio.studio_ui';
  61. $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
  62. if (isset($links->links)) {
  63. $translations_build = [
  64. '#theme' => 'links__language_block',
  65. '#links' => $links->links,
  66. '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
  67. '#set_active_class' => TRUE,
  68. ];
  69. $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
  70. $data['translations_links'] = $translations_rendered;
  71. }
  72. // JSON
  73. $response = new JsonResponse();
  74. $response->setData($data);
  75. return $response;
  76. }
  77. public function StudioChutierUIJson(){
  78. $renderable = $this->buildChutierUI();
  79. $rendered = render($renderable);
  80. // TODO: make response cachable
  81. // JSON
  82. $response = new JsonResponse();
  83. $response->setData([
  84. 'rendered'=> $rendered,
  85. ]);
  86. return $response;
  87. }
  88. private function buildStudioUI(){
  89. return [
  90. '#theme' => 'edlp_studio_ui',
  91. '#chutier_ui' => $this->buildChutierUI(),
  92. '#composition_ui' => $this->buildCompostionUI(),
  93. ];
  94. }
  95. private function buildChutierUI(){
  96. // get his docs in chutier
  97. $documents_nids = Chutier::getUserChutiersContents($this->user->id());
  98. // dpm($documents_nids);
  99. $documents = entity_load_multiple('node', $documents_nids);
  100. // build content renderable array
  101. $chutier_ui = array(
  102. "#theme"=>'edlp_chutier_ui',
  103. '#title' => t('Favorites'),
  104. "#document_nodes" => $documents,
  105. '#uid' => $this->user->id(),
  106. );
  107. return $chutier_ui;
  108. }
  109. private function buildCompostionUI(){
  110. // build content renderable array
  111. $composition_ui = array(
  112. "#theme"=>'edlp_composition_ui',
  113. "#title" => t('Composition'),
  114. "#compositions" => $this->buildCompostionsList(),
  115. "#composer_header" => t("Create a new playlist, then drag and drop your bookmarked sounds on the timeline below"),
  116. "#lastcomposition" => $this->getLastCompo(),
  117. '#composer_actions' => array(
  118. 'play_composition'=>array(
  119. "#type"=>'container',
  120. "#attributes"=>array(
  121. "class"=>array("compo-player-controls")
  122. )
  123. ),// TODO: add social media links (what about composition visibility uotside studio_ui ?)
  124. ),
  125. );
  126. return $composition_ui;
  127. }
  128. private function buildCompostionsList(){
  129. // get compositions
  130. $query = \Drupal::entityQuery('composition')
  131. ->condition('user_id', $this->user->id());
  132. $compos_ids = $query->execute();
  133. // dpm($compos_ids);
  134. if(!count($compos_ids)){
  135. // create default compos
  136. $def_compos = \Drupal::entityManager()
  137. ->getStorage('composition')
  138. ->create(array(
  139. 'name' => 'composition',
  140. 'uid' => $this->user->id()
  141. )
  142. );
  143. $def_compos->save();
  144. $compos_ids = array($def_compos->id());
  145. }
  146. $compos = entity_load_multiple('composition', $compos_ids);
  147. $createurl = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'create'], ['absolute' => TRUE]);
  148. return array(
  149. '#theme' => 'edlp_compositions_list',
  150. '#composition_entities' => $compos,
  151. '#new_composition_url' => $createurl,
  152. );
  153. }
  154. private function getLastCompo(){
  155. // just get the last compositions
  156. $query = \Drupal::entityQuery('composition')
  157. ->condition('user_id', $this->user->id())
  158. ->range(0,1)
  159. ->sort('created');
  160. $compos_id = $query->execute();
  161. if(count($compos_id)){
  162. $lastcompo = entity_load('composition', array_pop($compos_id));
  163. }else{
  164. $lastcompo = null;
  165. }
  166. return $lastcompo;
  167. }
  168. }