Issues (1369)

classes/SkinModel.php (1 issue)

Severity
1
<?php
2
3
use Core\GlobalContainer;
4
5
/**
6
 * Created by Gorlum 23.02.2017 12:20
7
 */
8
class SkinModel {
9
  const NO_IMAGE_ID = '_no_image';
10
  const NO_IMAGE_PATH = '/design/images/_no_image.png';
11
12
  /**
13
   * @var GlobalContainer $gc
14
   */
15
  protected $gc;
16
17
  /**
18
   * @var SkinInterface[] $skins
19
   */
20
  // TODO - lazy loading
21
  protected $skins;
22
23
  /**
24
   * @var SkinInterface $activeSkin
25
   */
26
  protected $activeSkin;
27
28
29
  // TODO - remove
30
  public function init() {
31
  }
32
33
34
  /**
35
   * SkinModel constructor.
36
   *
37
   * @param GlobalContainer $gc
38
   */
39
  public function __construct(GlobalContainer $gc) {
40
    $this->gc    = $gc;
41
    $this->skins = array();
42
43
    // Берем текущий скин
44
    $this->activeSkin = $this->getSkin(SN::$gc->theUser->getSkinPath());
45
  }
46
47
  /**
48
   * Returns skin with skin name. Loads it - if it is required
49
   *
50
   * @param string $skinName
51
   *
52
   * @return SkinInterface
53
   */
54
  public function getSkin($skinName) {
55
    $skinName = $this->sanitizeSkinName($skinName);
56
57
    if (empty($this->skins[$skinName])) {
58
      // Прогружаем текущий скин
59
      $this->skins[$skinName] = $this->loadSkin($skinName);
60
    }
61
62
    return $this->skins[$skinName];
63
  }
64
65
  /**
66
   * @param string        $image_tag
67
   * @param template|null $template
68
   *
69
   * @return string
70
   */
71
  public function getImageCurrent($image_tag, $template = null) {
72
    return $this->getImageFrom($this->activeSkin->getName(), $image_tag, $template);
73
  }
74
75
  /**
76
   * @param string        $image_tag
77
   * @param template|null $template
78
   *
79
   * @return bool
80
   */
81
  public function isImageFileExists($image_tag, $template = null) {
82
    $largeUrl = $this->getImageCurrent($image_tag, $template);
83
    if (strpos($largeUrl, SKIN_IMAGE_MISSED_FILE_PATH) !== false) {
84
      // Image not found in directory
85
      $result = false;
86
    } else {
87
      // Image found in directory. But what for actual file?
88
      $imageRelativePath = substr($largeUrl, strlen(SN_ROOT_VIRTUAL));
89
      $result            = file_exists(SN_ROOT_PHYSICAL . $imageRelativePath);
90
    }
91
92
    return $result;
93
  }
94
95
  public function getImageFrom($skinName, $image_tag, $template) {
96
    return $this->getSkin($skinName)->imageFromStringTag($image_tag, $template);
97
  }
98
99
  /**
100
   * Switches current skin
101
   *
102
   * @param $skinName
103
   */
104
  public function switchActive($skinName) {
105
    $this->activeSkin = $this->getSkin($skinName);
106
  }
107
108
  /**
109
   * Loads skin
110
   *
111
   * @param string $skinName
112
   *
113
   * @return SkinInterface
114
   */
115
  protected function loadSkin($skinName) {
116
    $skinClass = $this->gc->skinEntityClass;
117
118
    $skin = new $skinClass($skinName, $this);
119
120
//    $skin->load();
121
122
    return $skin;
123
  }
124
125
126
  /**
127
   * @param string $skinName
128
   *
129
   * @return string
130
   */
131
  protected function sanitizeSkinName($skinName) {
132
    strpos($skinName, 'skins/') !== false ? $skinName = substr($skinName, 6) : false;
133
    strpos($skinName, '/') !== false ? $skinName = str_replace('/', '', $skinName) : false;
134
135
    return is_string($skinName) ? $skinName : '';
0 ignored issues
show
The condition is_string($skinName) is always true.
Loading history...
136
  }
137
138
}
139