'Page Edit Image', 'summary' => 'Provides an image select capability as used by some Fieldtype modules (like TinyMCE)', 'version' => 108, 'permanent' => true, 'permission' => 'page-edit', ); } protected $maxImageWidth = 835; protected $page = null; protected $editorPage = null; protected $defaultClass = 'Pageimage'; protected $file = ''; protected $editWidth = 0; protected $editHeight = 0; protected $extensions = 'jpg|jpeg|gif|png|svg'; protected static $defaultConfig = array( 'skipFields' => '', 'noSizeAttrs' => 0, 'alignLeftClass' => 'align_left', 'alignRightClass' => 'align_right', 'alignCenterClass' => 'align_center' ); public function __construct() { foreach(self::$defaultConfig as $key => $value) $this->set($key, $value); } public function init() { if($this->config->demo) throw new WireException("Sorry, image editing functions are disabled in demo mode"); $this->modules->get("ProcessPageList"); $id = (int) $this->input->get->id; $editID = (int) $this->input->get->edit_page_id; if($editID) { $this->wire('session')->set($this, 'edit_page_id', $editID); } else { $editID = (int) $this->wire('session')->get($this, 'edit_page_id'); } if($editID) { $this->editorPage = $this->wire('pages')->get($editID); if(!$this->editorPage->editable()) { $this->editorPage = null; $this->wire('session')->remove($this, 'edit_page_id'); } } // if no ID was specified, then retrieive ID from filename path, if it's there if($this->input->get->file && preg_match('{[/,]}', $this->input->get->file)) { if(preg_match('{(\d+)[/,][^/,]+\.(' . $this->extensions . ')$}iD', $this->input->get->file, $matches)) { // ..........ID.../,filename.ext // format: 123/filename.jpg OR 123,filename.jpg // also covers new pagefileSecure URLs $id = (int) $matches[1]; } else if(preg_match('{(/[\d/]+)/([-_.a-z0-9]+)\.(' . $this->extensions . ')$}iD', $this->input->get->file, $matches)) { // .................ID........filename........ext // extended asset path format: 1/2/3/filename.jpg $id = PagefilesManager::dirToPageID($matches[1]); } else if(preg_match('{^' . wire('config')->urls->root . '([-_./a-zA-Z0-9]*/)' . wire('config')->pagefileUrlPrefix . '[^/]+\.(' . $this->extensions . ')$}iD', $this->input->get->file, $matches)) { // .............................../....................path/to/page.........................-?....................filename..........ext // legacy pagefileSecure URL format: /path/to/page/filename.jpg // @todo: does this still need to be here or can it be dropped? $this->page = wire('pages')->get('/' . $matches[1]); $id = $this->page->id; } } if(!$id) throw new WireException("No page specified"); if(!$this->page) $this->page = $this->pages->get($id); if(!$this->page) throw new WireException("No page specified"); if(!$this->page->id) throw new WireException("Unknown page"); if(!$this->editorPage) $this->editorPage = $this->page; // note we use hasPermission('page-view') rather than viewable() here becuase we want to allow pages without template files if(!$this->user->hasPermission('page-view', $this->page)) throw new WireException("You do not have access to images on this page"); if($this->input->get->winwidth) $this->maxImageWidth = ((int) $this->input->get->winwidth) - 70; if($this->maxImageWidth < 400) $this->maxImageWidth = 400; parent::init(); } public function getPageimage() { $images = $this->getImages($this->page); $filename = basename($this->input->get->file); if(strpos($filename, ',') === false) $filename = $this->page->id . ',' . $filename; // prepend ID if it's not there, needed for ajax in-editor resize if(!preg_match('/\.(' . $this->extensions . ')$/iD', $filename, $matches)) throw new WireException("Unknown image file"); // get the original, non resized version, if present if(preg_match('/(\.(\d+)x(\d+)(-[-_a-z0-9]+)?)\.' . $matches[1] . '$/', $filename, $matches)) { // original.600x400-suffix1-suffix2.ext $this->editWidth = (int) $matches[2]; $this->editHeight = (int) $matches[3]; $filename = str_replace($matches[1], '', $filename); // remove dimensions and optional suffix } if(!array_key_exists($filename, $images)) throw new WireException("Invalid image file: $filename"); return $images[$filename]; } public function getImages(Page $page, $level = 0) { $allImages = array(); $numImages = 0; $numImageFields = 0; $skipFields = explode(' ', $this->skipFields); if(!$page->id) return $allImages; foreach($page->fields as $field) { if(in_array($field->name, $skipFields)) continue; if($field->type instanceof FieldtypeRepeater) { // get images that are possibly in a repeater foreach($page->get($field->name) as $p) { $images = $this->getImages($p, $level+1); if(!count($images)) continue; $allImages = array_merge($allImages, $images); $numImages += count($images); $numImageFields++; } continue; } if(!$field->type instanceof FieldtypeImage) continue; $numImageFields++; $images = $page->get($field->name); if(!count($images)) continue; foreach($images as $image) { $numImages++; $key = $page->id . ',' . $image->basename; // page_id,basename for repeater support $allImages[$key] = $image; } } if(!$level) { if(!$numImageFields) $this->message($this->_("There are no image fields on this page. Choose another page to select images from.")); // Message when page has no image fields else if(!$numImages) $this->message($this->_("There are no images present on this page. Close this window and upload images, or select images from another page.")); // Message when page has no images } return $allImages; } public function ___execute() { if($this->config->demo) throw new WireException("Sorry, image editing functions are disabled in demo mode"); if($this->input->get->file) return $this->executeEdit(); $images = $this->getImages($this->page); $out = ''; if(count($images)) { $winwidth = (int) $this->input->get->winwidth; foreach($images as $image) { $width = $image->width(); if($width > $this->maxImageWidth) $width = $this->maxImageWidth; $out .= "\n\t
" .
"$widthx" .
"$height
" .
"" .
"