123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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 Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Drupal\Core\Url;
- use Drupal\edlp_studio\Entity\Compositon;
- /**
- * Class CompositionController.
- */
- class CompositionController 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')
- );
- }
- /**
- * AdddContent.
- *
- * @return json
- * Return status.
- */
- public function CompositionActionJson($action, $cid, Request $request) {
- $this->error_message = null;
- $status = 'ok';
- $message = 'Hello';
- $response = new JsonResponse();
- switch($action){
- case 'create':
- $name = $request->query->get('new_name');
- if($name){
- $this->createComposition($name);
- }else{
- $this->error_message = t("Composition creation needs a name as query paramater!");
- }
- break;
- case 'open':
- if($cid){
- $this->openComposition($cid);
- }else{
- $this->error_message = t("Composition opening needs a cid as url paramater!");
- }
- break;
- case 'save':
- if($cid){
- $name = $request->query->get('new_name');
- $documents = $request->query->get('documents');
- $this->saveComposition($cid, $name, $documents);
- }else{
- $this->error_message = t("Composition saving needs a cid as query paramater!");
- }
- break;
- case 'delete':
- if($cid){
- $this->deleteComposition($cid);
- }else{
- $this->error_message = t("Composition deletion needs a cid as url paramater!");
- }
- break;
- }
- if($this->error_message){
- $status = 'error';
- $message = $this->error_message;
- }
- $data = array(
- 'action' => $action,
- 'status' => $status,
- 'message' => $this->error_message
- );
- if($status == 'ok'){
- switch ($action) {
- case 'create':
- $url = Url::fromRoute('entity.composition.canonical', ['composition' => $this->compo->id()], ['absolute' => TRUE]);
- $title = $this->compo->getName();
- $new_link_build = array(
- '#title' => $title,
- '#type' => 'link',
- '#url' => $url,
- '#options'=>array(
- 'attributes' => array(
- 'data-drupal-link-system-path' => $url->getInternalPath(),
- 'cid' => $this->compo->id(),
- 'class' => ['composition-link'],
- 'title'=>$title,
- ),
- ),
- );
- $delete_url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'delete', 'cid' => $this->compo->id()], ['absolute' => TRUE]);
- $deletelink_build = array(
- '#title' => t('Delete'),
- '#type' => 'link',
- '#url' => $delete_url,
- '#options'=>array(
- 'attributes' => array(
- 'data-drupal-link-system-path' => $delete_url->getInternalPath(),
- 'cid' => $this->compo->id(),
- 'class' => ['delete-composition-link'],
- 'title'=>t('Delete @title', array('@title'=>$title)),
- ),
- ),
- );
- $data += array(
- 'new_name' => $title,
- 'new_link' => render($new_link_build),
- 'delete_link' => render($deletelink_build),
- );
- break;
- case 'open':
- $this->getRendredComposition();
- $data += array(
- 'compo' => $this->rendered_compo,
- );
- break;
- case 'save':
- $data += array(
- 'name'=>$name,
- 'documents'=>$documents
- );
- break;
- }
- }
- $response->setData($data);
- return $response;
- // // classic html
- // return array(
- // '#markup'=>'Status : ' . $status . ' | Message : ' . $message,
- // );
- }
- private function createComposition($name){
- $compo = array(
- 'id' => NULL,
- 'uid' => $this->user->id(),
- 'status' => TRUE,
- 'name' => $name,
- );
- $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->create($compo);
- $this->compo->save();
- }
- private function getRendredComposition(){
- $view_builder = \Drupal::entityTypeManager()->getViewBuilder('composition');
- $compobuild = $view_builder->view($this->compo, 'studio_ui');
- $this->rendered_compo = render($compobuild);
- }
- private function openComposition($cid){
- $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
- }
- private function saveComposition($cid, $name = "", $new_documents = array()){
- $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
- if($name != ""){
- $this->compo->setName($name);
- }
- $documents = array();
- if(count($new_documents)){
- foreach ($new_documents as $nid) {
- $documents[] = ['target_id'=>$nid];
- }
- }
- $this->compo->set('documents', $documents);
- $this->compo->save();
- }
- private function deleteComposition($cid){
- $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid);
- $this->compo->delete();
- }
- }
|