This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
/
ProductHelper.php
201 lines (165 loc) · 4.8 KB
/
ProductHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
namespace HeimrichHannot\IsotopeBundle\Helper;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\FilesModel;
use Contao\StringUtil;
use HeimrichHannot\IsotopeBundle\Model\ProductDataModel;
use HeimrichHannot\IsotopeBundle\Model\ProductModel;
use Isotope\Model\ProductType;
class ProductHelper
{
// licence
const ISO_LICENCE_FREE = 'free';
const ISO_LICENCE_COPYRIGHT = 'copyright';
const ISO_LICENCE_LOCKED = 'locked';
/**
* @var ContaoFrameworkInterface
*/
protected $framework;
public function __construct(ContaoFrameworkInterface $framework)
{
$this->framework = $framework;
}
public function prepareExifDataForSave($strExifTag, $arrExifData)
{
switch ($strExifTag) {
case \PHPExif\Exif::CREATION_DATE:
$strValue = $this->prepareDateTimes($arrExifData);
break;
case \PHPExif\Exif::KEYWORDS:
$strValue = $this->prepareKeywords($arrExifData);
break;
default:
$strValue = null;
}
return $strValue;
}
public function getFileName($file, $size)
{
return str_replace('.'.$file->extension, $this->getFileSizeName($file, $size), ltrim($file->name, '_'));
}
public function getFilePath($file, $name)
{
return str_replace($file->name, $name, $file->path);
}
/**
* @param $file object
* @param $size array
*
* @return string
*/
public function getFileSizeName($file, $size)
{
$suffix = '';
if ($GLOBALS['TL_LANG']['MSC']['originalSize'] != $size['name']) {
$suffix = '_'.$size['size'][0];
}
return $suffix.'.'.$file->extension;
}
/**
* return all product groups that are defined as editable in module.
*
* @param $module object
*
* @return array
*/
public function getEditableCategories($module)
{
$categories = [];
if (!$module->iso_editableCategories) {
return $categories;
}
foreach (StringUtil::deserialize($module->iso_editableCategories, true) as $cat) {
$categories[$cat] = $this->framework->getAdapter(ProductType::class)->findByPk($cat)->name;
}
asort($categories);
return $categories;
}
public function getLicenceTitle()
{
return [
static::ISO_LICENCE_FREE,
static::ISO_LICENCE_COPYRIGHT,
static::ISO_LICENCE_LOCKED,
];
}
/**
* @return array
*/
public function getTags($obj)
{
$options = [];
if (null === ($products = $this->framework->getAdapter(ProductDataModel::class)->findBy(['tag IS NOT NULL'], null))) {
return $options;
}
$result = [];
foreach ($products as $product) {
if (!$product->tag || '' == $product->tag) {
continue;
}
$options = StringUtil::deserialize($product->tag, true);
foreach ($options as $option) {
$result[] = $option;
}
}
return array_filter(array_unique($result));
}
public function getCopyrights()
{
return $this->framework->getAdapter(ProductModel::class)->getCopyrights();
}
/**
* @param FilesModel $file
*
* @return mixed|string
*/
public function getFileNameFromFile(FilesModel $file)
{
if ('mp3' == $file->extension) {
$title = str_replace(['_', '.'], [' ', ' '], $file->name);
} else {
$title = str_replace(['_', '.'.$file->extension], [' ', ''], $file->name);
}
$title = $this->ucfirstOnSign('-', $title);
$title = $this->ucfirstOnSign(' ', $title);
return $title;
}
protected function prepareDateTimes($arrExifData)
{
$objCreationDate = $arrExifData[\PHPExif\Exif::CREATION_DATE];
if (null === $objCreationDate) {
return null;
}
return $objCreationDate->getTimestamp();
}
protected function prepareKeywords($arrExifData)
{
if (is_array($arrExifData[\PHPExif\Exif::KEYWORDS])) {
$strKeywords = implode(', ', $arrExifData[\PHPExif\Exif::KEYWORDS]);
}
if (empty($strKeywords)) {
return null;
}
return '<p>'.$strKeywords.'</p>';
}
/**
* @param $sign
* @param $value
*
* @return string
*/
protected function ucfirstOnSign($sign, $value)
{
$split = explode($sign, $value);
$result = [];
foreach ($split as $part) {
$result[] = ucfirst($part);
}
return implode($sign, $result);
}
}