ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Settings.php
Go to the documentation of this file.
1 <?php
2 
20 
23 use ilSetting;
24 
28 class Settings extends ilSetting implements Setting
29 {
30  public const V_MAX_PREVIEWS_DEFAULT = 5;
31  public const V_MAX_PREVIEWS_MIN = 1;
32  public const V_MAX_PREVIEWS_MAX = 100;
33 
34  public const V_IMAGE_SIZE_DEFAULT = 868;
35  public const V_IMAGE_SIZE_MIN = 50;
36  public const V_IMAGE_SIZE_MAX = 1000;
37 
38  private const V_IMAGE_QUALITY_DEFAULT = 85;
39  private const V_IMAGE_QUALITY_MIN = 1;
40  private const V_IMAGE_QUALITY_MAX = 100;
41 
42  public const MODULE_NAME = 'preview';
43 
44  private const F_PREVIEW_ENABLED = 'preview_enabled';
45  private const F_MAX_PREVIEWS_PER_OBJECT = 'max_previews_per_object';
46  private const F_PREVIEW_IMAGE_SIZE = 'preview_image_size';
47  private const F_PREVIEW_PERSISTING = 'preview_persisting';
48  private const F_PREVIEW_IMAGE_QUALITY = 'preview_image_quality';
49  private const F_TILE_PREVIEWS = 'tile_previews';
54 
55  public function __construct()
56  {
57  parent::__construct(self::MODULE_NAME, false);
58  $this->imagick = new ImagickEngine();
59  }
60 
61  public function setPersisting(bool $a_value): void
62  {
63  $this->set(self::F_PREVIEW_PERSISTING, $this->boolToStr($a_value));
64  }
65 
66  public function isPersisting(): bool
67  {
68  return $this->strToBool($this->get(self::F_PREVIEW_PERSISTING, '1'));
69  }
70 
71  public function setTilePreviews(bool $a_value): void
72  {
73  $this->set(self::F_TILE_PREVIEWS, $this->boolToStr($a_value));
74  }
75 
76  public function hasTilePreviews(): bool
77  {
78  return $this->strToBool($this->get(self::F_TILE_PREVIEWS, '1'));
79  }
80 
81  public function isPreviewPossible(): bool
82  {
83  return $this->imagick->isRunning(); // &&(new GDEngine())->isRunning();
84  }
85 
86  public function setPreviewEnabled(bool $a_value): void
87  {
88  $this->set(self::F_PREVIEW_ENABLED, $this->boolToStr($a_value));
89  }
90 
91  public function isPreviewEnabled(): bool
92  {
93  if (!$this->isPreviewPossible()) {
94  return false;
95  }
96  return $this->strToBool($this->get(self::F_PREVIEW_ENABLED, '1'));
97  }
98 
99  public function setMaximumPreviews(int $max_previews): void
100  {
101  $max_previews = $this->adjustNumeric(
102  $max_previews,
103  self::V_MAX_PREVIEWS_MIN,
104  self::V_MAX_PREVIEWS_MAX,
105  self::V_MAX_PREVIEWS_DEFAULT
106  );
107  $this->set(self::F_MAX_PREVIEWS_PER_OBJECT, $this->intToStr($max_previews));
108  }
109 
110  public function getMaximumPreviews(): int
111  {
112  return $this->strToInt($this->get(self::F_MAX_PREVIEWS_PER_OBJECT, (string) self::V_MAX_PREVIEWS_DEFAULT));
113  }
114 
115  public function setImageSize(int $image_size): void
116  {
117  $image_size = $this->adjustNumeric(
118  $image_size,
119  self::V_IMAGE_SIZE_MIN,
120  self::V_IMAGE_SIZE_MAX,
121  self::V_IMAGE_SIZE_DEFAULT
122  );
123  $this->set(self::F_PREVIEW_IMAGE_SIZE, $this->intToStr($image_size));
124  }
125 
126  public function getImageSize(): int
127  {
128  return $this->strToInt($this->get(self::F_PREVIEW_IMAGE_SIZE, (string) self::V_IMAGE_SIZE_DEFAULT));
129  }
130 
131  public function setImageQuality(int $quality): void
132  {
133  $quality = $this->adjustNumeric(
134  $quality,
135  self::V_IMAGE_QUALITY_MIN,
136  self::V_IMAGE_QUALITY_MAX,
137  self::V_IMAGE_QUALITY_DEFAULT
138  );
139  $this->set(self::F_PREVIEW_IMAGE_QUALITY, $this->intToStr($quality));
140  }
141 
142  public function getImageQuality(): int
143  {
144  return $this->strToInt($this->get(self::F_PREVIEW_IMAGE_QUALITY, (string) self::V_IMAGE_QUALITY_DEFAULT));
145  }
146 
147  private function adjustNumeric(int $value, int $min, int $max, int $default): int
148  {
149  // is number?
150  if (is_numeric($value)) {
151  // don't allow to large numbers
152  $value = (int) $value;
153  if ($value < $min) {
154  $value = $min;
155  } elseif ($value > $max) {
156  $value = $max;
157  }
158  } else {
159  $value = $default;
160  }
161 
162  return $value;
163  }
164 
165  // HELPERS
166 
167  private function strToBool(string $value): bool
168  {
169  return $value === '1';
170  }
171 
172  private function boolToStr(bool $value): string
173  {
174  return $value ? '1' : '0';
175  }
176 
177  private function intToStr(int $int): string
178  {
179  return (string) $int;
180  }
181 
182  private function strToInt(string $str): int
183  {
184  return (int) $str;
185  }
186 }
adjustNumeric(int $value, int $min, int $max, int $default)
Definition: Settings.php:147
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Form.php:19
setMaximumPreviews(int $max_previews)
Definition: Settings.php:99