Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
topiconformailtemplate.php
1<?php
2
3
5
6
11
13{
14 protected const FILE_TYPE = '.png';
15 protected const MODULE_SUB_DIR = 'calendar';
16 protected const ICAL_SUB_DIR = 'ical';
17
21 private $date;
25 private $path;
29 private $fileName;
33 private $fontPath;
37 protected $filePath;
41 private $templatePath;
42
47 public static function fromDate(Date $date): TopIconForMailTemplate
48 {
49 $iconObject = new self();
50 $iconObject->date = $date;
51 $uploadDir = \COption::GetOptionString("main", "upload_dir", "upload");
52 $iconObject->path = "/".$uploadDir."/".self::MODULE_SUB_DIR."/".self::ICAL_SUB_DIR;
53 $iconObject->fileName = mb_strtolower($date->format('M')).self::FILE_TYPE;
54 $iconObject->filePath = $iconObject->path."/".$iconObject->fileName;
55 return $iconObject;
56 }
57
61 public function __construct()
62 {
63 $this->fontPath = $_SERVER['DOCUMENT_ROOT'] .BX_ROOT.'/modules/main/install/fonts/opensans-bold.ttf';
64 $this->templatePath = $_SERVER["DOCUMENT_ROOT"].BX_ROOT.'/components/bitrix/calendar.ical.mail/templates/.default/images/date-template-top.png';
65 }
66
71 public function setDate(Date $date): TopIconForMailTemplate
72 {
73 $this->date = $date;
74
75 return $this;
76 }
77
81 public function getPath(): string
82 {
83 return $this->filePath;
84 }
85
89 public function createImage(): bool
90 {
91 if (File::isFileExists($_SERVER["DOCUMENT_ROOT"].$this->path."/".$this->fileName))
92 {
93 return true;
94 }
95
96 $image = $this->getImage();
97 $tmpFile = $this->getTmpPath();
98
99 if (
100 is_resource($image)
101 && $tmpFile !== ''
102 && imagepng($image, $tmpFile, 0)
103 )
104 {
105 $fileData = $this->getFileData($tmpFile);
106 $fileId = \CFile::SaveFile($fileData, 'calendar', false, false, self::ICAL_SUB_DIR);
107 $fileArray = \CFile::GetFileArray($fileId);
108 if (is_array($fileArray))
109 {
110 if ($fileArray['SRC'] !== $this->filePath)
111 {
112 $this->filePath = $fileArray['SRC'];
113 }
114
115 return true;
116 }
117 }
118
119 return false;
120 }
121
125 protected function getMonthName(): string
126 {
127 $month = Util::checkRuZone()
128 ? mb_strtoupper(FormatDate('M', $this->date->getTimestamp()))
129 : mb_strtoupper($this->date->format('M'))
130 ;
131
132 return is_string($month)
133 ? $month
134 : $this->date->format('M')
135 ;
136 }
137
141 protected function getTmpPath(): string
142 {
143 $tmpFile = \CTempFile::getFileName($this->fileName);
144 return CheckDirPath($tmpFile)
145 ? $tmpFile
146 : ''
147 ;
148
149 }
150
154 protected function getImage()
155 {
156 if (!\Bitrix\Main\IO\File::isFileExists($this->templatePath))
157 {
158 return null;
159 }
160
161 $image = @imagecreatefrompng($this->templatePath);
162 $month = Encoding::convertEncoding(Helper::getShortMonthName($this->date), SITE_CHARSET, "utf-8");
163 $color = imagecolorallocate($image, 255, 255, 255);
164 imagettftext($image, 30, 0, 55, 57, $color, $this->fontPath, $month);
165
166 return $image;
167 }
168
173 protected function getFileData($tmpFile): array
174 {
175 return [
176 'name' => $this->fileName,
177 'type' => 'image/png',
178 'content' => File::getFileContents($tmpFile),
179 'MODULE_ID' => 'calendar',
180 ];
181 }
182}
format(string $format=null)
Definition date.php:107
static checkRuZone()
Definition util.php:124