Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
109 lines (94 sloc) 3.69 KB
<?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)) {
if (strpos($img->url, $imgpath) !== 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();
}
}