/
ImageStyle.php
75 lines (67 loc) · 2.47 KB
/
ImageStyle.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
<?php
namespace Concrete\Core\StyleCustomizer\Style;
use Concrete\Core\StyleCustomizer\Style\Value\ImageValue;
use Less_Environment;
use File;
use Loader;
use Permissions;
class ImageStyle extends Style
{
public function render($value = false)
{
$r = \Concrete\Core\Http\ResponseAssetGroup::get();
$r->requireAsset('core/style-customizer');
$strOptions = '';
$i = 0;
$options['inputName'] = $this->getVariable();
if (is_object($value)) {
$options['value'] = $value->getUrl();
}
$strOptions = json_encode($options);
echo '<span class="ccm-style-customizer-display-swatch-wrapper" data-image-selector="' . $this->getVariable() . '"></span>';
echo "<script type=\"text/javascript\">";
echo "$(function() { $('span[data-image-selector=" . $this->getVariable() . "]').concreteStyleCustomizerImageSelector({$strOptions}); });";
echo "</script>";
}
public function getValueFromRequest(\Symfony\Component\HttpFoundation\ParameterBag $request)
{
$image = $request->get($this->getVariable());
$image = $image['image'];
if ($image) {
$iv = new ImageValue($this->getVariable());
if (Loader::helper('validation/numbers')->integer($image)) {
// it's a file ID.
$f = File::getByID($image);
if (is_object($f)) {
$fp = new Permissions($f);
if ($fp->canViewFile()) {
$iv->setFileID($image);
$iv->setUrl($f->getRelativePath());
}
}
} else {
$iv->setUrl($image);
}
return $iv;
}
}
public static function getValuesFromVariables($rules = [])
{
$values = [];
foreach ($rules as $rule) {
if (preg_match('/@(.+)\-image/i', isset($rule->name) ? $rule->name : '', $matches)) {
$entryURI = $rule->value->value[0]->value[0]->currentFileInfo['entryUri'];
$value = $rule->value->value[0]->value[0]->value;
if ($entryURI) {
$value = Less_Environment::normalizePath($entryURI . $value);
}
$iv = new ImageValue($matches[1]);
$iv->setUrl($value);
if (is_object($iv)) {
$values[] = $iv;
}
}
}
return $values;
}
}