CompositionController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Drupal\Core\Url;
  10. use Drupal\edlp_studio\Entity\Compositon;
  11. /**
  12. * Class CompositionController.
  13. */
  14. class CompositionController extends ControllerBase {
  15. protected $user;
  16. protected $userdata;
  17. /**
  18. * Class constructor.
  19. */
  20. public function __construct(AccountInterface $account, UserDataInterface $userdata) {
  21. $this->user = $account;
  22. $this->userdata = $userdata;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function create(ContainerInterface $container) {
  28. // Instantiates this form class.
  29. return new static(
  30. // Load the service required to construct this class.
  31. $container->get('current_user'),
  32. $container->get('user.data')
  33. );
  34. }
  35. /**
  36. * AdddContent.
  37. *
  38. * @return json
  39. * Return status.
  40. */
  41. public function CompositionActionJson($action, $cid, Request $request) {
  42. $this->error_message = null;
  43. $status = 'ok';
  44. $message = 'Hello';
  45. $response = new JsonResponse();
  46. switch($action){
  47. case 'create':
  48. $name = $request->query->get('new_name');
  49. if($name){
  50. $this->createComposition($name);
  51. }else{
  52. $this->error_message = t("Composition creation needs a name as query paramater!");
  53. }
  54. break;
  55. case 'open':
  56. if($cid){
  57. $this->openComposition($cid);
  58. }else{
  59. $this->error_message = t("Composition opening needs a cid as url paramater!");
  60. }
  61. break;
  62. case 'save':
  63. if($cid){
  64. $name = $request->query->get('new_name');
  65. $documents = $request->query->get('documents');
  66. $this->saveComposition($cid, $name, $documents);
  67. }else{
  68. $this->error_message = t("Composition saving needs a cid as query paramater!");
  69. }
  70. break;
  71. case 'delete':
  72. if($cid){
  73. $this->deleteComposition($cid);
  74. }else{
  75. $this->error_message = t("Composition deletion needs a cid as url paramater!");
  76. }
  77. break;
  78. }
  79. if($this->error_message){
  80. $status = 'error';
  81. $message = $this->error_message;
  82. }
  83. $data = array(
  84. 'action' => $action,
  85. 'status' => $status,
  86. 'message' => $this->error_message
  87. );
  88. if($status == 'ok'){
  89. switch ($action) {
  90. case 'create':
  91. $url = Url::fromRoute('entity.composition.canonical', ['composition' => $this->compo->id()], ['absolute' => TRUE]);
  92. $title = $this->compo->getName();
  93. $new_link_build = array(
  94. '#title' => $title,
  95. '#type' => 'link',
  96. '#url' => $url,
  97. '#options'=>array(
  98. 'attributes' => array(
  99. 'data-drupal-link-system-path' => $url->getInternalPath(),
  100. 'cid' => $this->compo->id(),
  101. 'class' => ['composition-link'],
  102. 'title'=>$title,
  103. ),
  104. ),
  105. );
  106. $delete_url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'delete', 'cid' => $this->compo->id()], ['absolute' => TRUE]);
  107. $deletelink_build = array(
  108. '#title' => t('Delete'),
  109. '#type' => 'link',
  110. '#url' => $delete_url,
  111. '#options'=>array(
  112. 'attributes' => array(
  113. 'data-drupal-link-system-path' => $delete_url->getInternalPath(),
  114. 'cid' => $this->compo->id(),
  115. 'class' => ['delete-composition-link'],
  116. 'title'=>t('Delete @title', array('@title'=>$title)),
  117. ),
  118. ),
  119. );
  120. $data += array(
  121. 'new_name' => $title,
  122. 'new_link' => render($new_link_build),
  123. 'delete_link' => render($deletelink_build),
  124. );
  125. break;
  126. case 'open':
  127. $this->getRendredComposition();
  128. $data += array(
  129. 'compo' => $this->rendered_compo,
  130. );
  131. break;
  132. case 'save':
  133. $data += array(
  134. 'name'=>$name,
  135. 'documents'=>$documents
  136. );
  137. break;
  138. }
  139. }
  140. $response->setData($data);
  141. return $response;
  142. // // classic html
  143. // return array(
  144. // '#markup'=>'Status : ' . $status . ' | Message : ' . $message,
  145. // );
  146. }
  147. private function createComposition($name){
  148. $compo = array(
  149. 'id' => NULL,
  150. 'uid' => $this->user->id(),
  151. 'status' => TRUE,
  152. 'name' => $name,
  153. );
  154. $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->create($compo);
  155. $this->compo->save();
  156. }
  157. private function getRendredComposition(){
  158. $view_builder = \Drupal::entityTypeManager()->getViewBuilder('composition');
  159. $compobuild = $view_builder->view($this->compo, 'studio_ui');
  160. $this->rendered_compo = render($compobuild);
  161. }
  162. private function openComposition($cid){
  163. $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
  164. }
  165. private function saveComposition($cid, $name = "", $new_documents = array()){
  166. $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
  167. if($name != ""){
  168. $this->compo->setName($name);
  169. }
  170. $documents = array();
  171. if(count($new_documents)){
  172. foreach ($new_documents as $nid) {
  173. $documents[] = ['target_id'=>$nid];
  174. }
  175. }
  176. $this->compo->set('documents', $documents);
  177. $this->compo->save();
  178. }
  179. private function deleteComposition($cid){
  180. $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
  181. $this->compo->delete();
  182. }
  183. }