d91643e8e5d99306c17b758fdfd246a8a0878be3
[yaffs-website] / web / modules / contrib / fontyourface / src / Controller / FontYourFaceController.php
1 <?php
2
3 namespace Drupal\fontyourface\Controller;
4
5 use Drupal\Core\Url;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\ReplaceCommand;
8 use Drupal\Core\Controller\ControllerBase;
9 use Drupal\fontyourface\Entity\Font;
10
11 /**
12  * Controller routines for forum routes.
13  */
14 class FontYourFaceController extends ControllerBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function activateFont(Font $font, $js) {
20     try {
21       $font->activate();
22       if ($js == 'ajax') {
23         $url = Url::fromRoute('entity.font.deactivate', ['js' => 'nojs', 'font' => $font->id()], ['query' => \Drupal::destination()->getAsArray()]);
24         $url->setOptions(['attributes' => ['id' => 'font-status-' . $font->id(), 'class' => ['font-status', 'enabled', 'use-ajax']]]);
25         $text = $this->t('Enable');
26         $link = \Drupal::l($text, $url);
27
28         $response = new AjaxResponse();
29         return $response->addCommand(new ReplaceCommand('#font-status-' . $font->id(), $link));
30       }
31       else {
32         drupal_set_message($this->t('Font @font successfully enabled', ['@font' => $font->name->value]));
33         return $this->redirect('entity.font.collection');
34       }
35     }
36     catch (Exception $e) {
37       $error = $e->getMessage();
38       if ($js == 'ajax') {
39         return new AjaxResponse([
40           'response' => TRUE,
41           'message' => $error,
42         ], 503);
43       }
44       else {
45         drupal_set_message($error, 'error');
46         return $this->redirect('entity.font.collection');
47       }
48     }
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function deactivateFont(Font $font, $js) {
55     try {
56       $font->deactivate();
57       if ($js == 'ajax') {
58         $url = Url::fromRoute('entity.font.activate', ['js' => 'nojs', 'font' => $font->id()], ['query' => \Drupal::destination()->getAsArray()]);
59         $url->setOptions(['attributes' => ['id' => 'font-status-' . $font->id(), 'class' => ['font-status', 'disabled', 'use-ajax']]]);
60         $text = $this->t('Enable');
61         $link = \Drupal::l($text, $url);
62
63         $response = new AjaxResponse();
64         return $response->addCommand(new ReplaceCommand('#font-status-' . $font->id(), $link));
65       }
66       else {
67         drupal_set_message($this->t('Font @font successfully disabled', ['@font' => $font->name->value]));
68         return $this->redirect('entity.font.collection');
69       }
70     }
71     catch (Exception $e) {
72       $error = $e->getMessage();
73       if ($js == 'ajax') {
74         return new AjaxResponse([
75           'response' => TRUE,
76           'message' => $error,
77         ], 503);
78       }
79       else {
80         drupal_set_message($error, 'error');
81         return $this->redirect('entity.font.collection');
82       }
83     }
84   }
85
86 }