Skip to content

Instantly share code, notes, and snippets.

@adrianbj
Last active January 6, 2021 02:28
Show Gist options
  • Save adrianbj/2b3b6b40f64a816d397f to your computer and use it in GitHub Desktop.
Save adrianbj/2b3b6b40f64a816d397f to your computer and use it in GitHub Desktop.
Automatically creates pages for all images uploaded to an album page.
<?php
class AutoImagePages extends WireData implements Module, ConfigurableModule {
/**
* Data as used by the get/set functions
*
*/
protected $data = array();
/**
* Default configuration for module
*
*/
static public function getDefaultData() {
return array(
"templateAlbum" => "album",
"templateImage" => "image",
"fieldImages" => "images",
"fieldImage" => "image",
);
}
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => __('Auto Image Pages'),
'version' => 6,
'author' => 'Adrian Jones',
'summary' => __('Automatically creates pages for all images uploaded to an album page.'),
'singular' => true,
'autoload' => true
);
}
/**
* Populate the default config data
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
/**
* Init function and createImagePages
*
*/
public function init() {
$this->pages->addHookAfter('save', $this, 'createImagePages');
}
public function createImagePages($event){
$page = $event->arguments[0];
if($page->template == $this->data['templateAlbum'] && count($page->{$this->data['fieldImages']})>0){
foreach($page->{$this->data['fieldImages']} as $image){
//check if a image with the same filename exists
if($page->get("name={$image->basename}")->id) {
//send error message for double entries
$this->error($image->basename . " - image with the same filename already exists, so this was skipped!");
} else {
$np = new Page();
$np->of(false);
$np->template = $this->templates->get($this->data['templateImage']);
$np->title = $image->description ? $image->description : $image->basename;
$np->name = $image->basename;
$np->parent = $page;
$np->save();
$np->{$this->data['fieldImage']}->add($image->filename);
$np->{$this->data['fieldImage']}->last()->description = $image->description;
$np->save();
}
}
// delete all images from main album page
$page->{$this->data['fieldImages']}->deleteAll();
$page->save();
}
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$data = array_merge(self::getDefaultData(), $data);
$wrapper = new InputFieldWrapper();
$f = wire('modules')->get('InputfieldSelect');
$f->attr('name+id', 'templateAlbum');
$f->label = __('Album template name', __FILE__);
$f->columnWidth = 50;
$f->description = __('The name of the template file of the album page', __FILE__);
$f->attr('title', __('Root Parent Image', __FILE__));
foreach(wire('templates') as $templateoption) {
$f->addOption($templateoption->name);
}
if(isset($data['templateAlbum'])) $f->value = $data['templateAlbum'];
$wrapper->add($f);
$f = wire('modules')->get('InputfieldSelect');
$f->attr('name+id', 'templateImage');
$f->label = __('Image template name', __FILE__);
$f->columnWidth = 50;
$f->description = __('The name of the template file of the single image page.', __FILE__);
$f->attr('title', __('Name of the image template', __FILE__));
foreach(wire('templates') as $templateoption) {
$f->addOption($templateoption->name);
}
if(isset($data['templateImage'])) $f->value = $data['templateImage'];
$wrapper->add($f);
$f = wire('modules')->get('InputfieldSelect');
$f->attr('name+id', 'fieldImages');
$f->label = __('Images field name', __FILE__);
$f->columnWidth = 50;
$f->description = __('The name of the images field that uploaded on the album page.', __FILE__);
$f->attr('title', __('Name of the images field', __FILE__));
foreach(wire('fields') as $fieldoption) {
// filter out incompatible field types
if($fieldoption->type == "FieldtypeImage") $f->addOption($fieldoption->name);
}
if(isset($data['fieldImages'])) $f->value = $data['fieldImages'];
$wrapper->add($f);
$f = wire('modules')->get('InputfieldSelect');
$f->attr('name+id', 'fieldImage');
$f->label = __('Image field name', __FILE__);
$f->columnWidth = 50;
$f->description = __('The name of the image field of the single image pages.', __FILE__);
$f->attr('title', __('Name of the image field', __FILE__));
foreach(wire('fields') as $fieldoption) {
// filter out incompatible field types
if($fieldoption->type == "FieldtypeImage") $f->addOption($fieldoption->name);
}
if(isset($data['fieldImage'])) $f->value = $data['fieldImage'];
$wrapper->add($f);
return $wrapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment