Repository navigation
Expand file tree
/
Copy pathInputfieldImagePicker.module
More file actions
110 lines (96 loc) · 3.87 KB
/
Copy pathInputfieldImagePicker.module
File metadata and controls
110 lines (96 loc) · 3.87 KB
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
<?php
/**
* Class InputfieldImagePicker
*/
class InputfieldImagePicker extends Inputfield {
const adminPageName = 'ImagePickerList';
protected $page;
protected $field;
protected $sid;
protected $lastfieldtype;
public static function getModuleInfo() {
return [
'title' => 'ImagePicker Input',
'version' => 90,
'summary' => 'Input field for Images.',
'requires' => 'FieldtypeImagePicker',
];
}
public function setPage(Page $page) {
$this->page = $page;
}
public function setField(Field $field) {
$this->field = $field;
}
private function getAImage($imgpath) {
$parts = explode('/', $imgpath);
$cnt=count($parts);
if ($cnt<2) return null;
$fn=$parts[$cnt-1];
$pgid=$parts[$cnt-2];
$aroot=$this->wire();
foreach ($aroot->fields->find("type=FieldtypeImage") as $f) {
$sele="id=$pgid, $f.data='$fn'";
foreach ($aroot->pages->find($sele) as $p) {
$ofstatus=$p->of();
$p->of(false);
foreach ($p->$f as $img) {
if (is_object($img)) {
$imgurl_without_ext = preg_replace('/(?:jpg|jpeg|png|gif|webp)$/i', '', $img->url);
$imgpath_withour_ext = preg_replace('/(?:jpg|jpeg|png|gif|webp)$/i', '', $imgpath);
if (strpos($imgurl_without_ext, $imgpath_withour_ext) !== false) {
$this->lastfieldtype = $f;
return $img;
break 2;
}
}
}
$p->of($ofstatus);
}
}
return null;
}
public function getImage() {
if (($this->page) && ($this->field)) {
$ifn = $this->page->get($this->field->name);
if ($ifn)
return $this->getAImage($ifn);
else
return null;
} else
return null;
}
public function ___render() {
$field = new InputfieldURL();
$this->sid = 'ip_' . $this->page->id . '_' . $this->field->name;
$field->set('name', $this->attr('name'));
$field->set('value', $this->attr('value'));
$field->set('class', 'InputfieldImagePickerInput');
$field->set('id', "isi_" . $this->sid);
$field->attr('data-sid', $this->sid);
$edurl = '';
if ($this->attr('value')) {
$imgobj = $this->getAImage($this->attr('value'));
if ($imgobj)
$edurl = $imgobj->page->editURL;
};
$pickerpage = $this->pages->get("name=" . self::adminPageName . ",template=admin")->url;
$html = $this->modules->get('InputfieldMarkup');
//Pass translations to JS
$html->markupText = '<script>document.pp_translations_title="' . $this->_('Choose image') . '";document.pp_translations_cancel="' . $this->_('Cancel') . '";document.pp_pickerpage="' . $pickerpage . '";</script>';
$html->markupText .= '<div class="IpPreviewImageDiv">';
if ($edurl)
$html->markupText .= '<a title="' . $this->_('Edit Original') . '" target="_blank" href="' . $edurl . '#wrap_Inputfield_' . $this->lastfieldtype . '">';
$html->markupText .= '<img class="IpPreviewImage" onerror="this.style.display=\'none\'" id="pv_' . $this->sid . '" src="' . $this->attr('value') . '">';
if ($edurl)
$html->markupText .= '</a>';
$html->markupText .= '</div>';
$btn = $this->modules->get('InputfieldButton');
$btn->attr('id', $this->attr('name') . "_imageselector_open");
$btn->attr('data-sid', $this->sid);
$btn->class .= " InputfieldImagePickerOpen";
$btn->icon = 'image';
$btn->value = '';
return $btn->render() . '<div class="InputfieldImagePickerText">' . $field->render() . '</div>' . $html->render();
}
}