Last active
January 1, 2016 09:49
-
-
Save Da-Fecto/8127425 to your computer and use it in GitHub Desktop.
ProcessWire files in folder Fieldtype & Inputfield.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ProcessWire files in folder Fieldtype | |
* | |
* ©2013 Martijn Geerts | |
* | |
* ProcessWire 2.x | |
* Copyright (C) 2010 by Ryan Cramer | |
* Licensed under GNU/GPL v2, see LICENSE.TXT | |
* | |
* http://www.processwire.com | |
* http://www.ryancramer.com | |
* | |
*/ | |
class FieldtypeFolder extends FieldtypeText { | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Files in folder', | |
'version' => 100, | |
'summary' => 'Field that stores a filename from a folder.', | |
'installs' => 'InputfieldFolder' | |
); | |
} | |
public function init() { | |
parent::init(); | |
$this->allowTextFormatters(false); | |
} | |
/** | |
* Sanitize value for storage | |
* | |
*/ | |
public function sanitizeValue(Page $page, Field $field, $value) { | |
$folder = trim($field->folderPath); | |
$file = $this->config->paths->templates . trim($field->folderPath, "/") . "/" . $value; | |
// not linked | |
if(!is_file($file)) return ''; | |
// don't allow directories | |
if(is_dir($file)) return ''; | |
return $value; | |
} | |
// Sets folderPath to the inputfield | |
public function getInputfield(Page $page, Field $field) { | |
$inputfield = $this->modules->get('InputfieldFolder'); | |
$inputfield->set('folderPath', $field->folderPath); | |
return $inputfield; | |
} | |
// clean it | |
public function ___formatValue(Page $page, Field $field, $value) { | |
return trim($value, "/"); | |
} | |
// details | |
public function ___getConfigInputfields(Field $field) { | |
$inputfields = parent::___getConfigInputfields($field); | |
$f = $this->modules->get('InputfieldText'); | |
$f->attr('name', 'folderPath'); | |
$f->label = __("Path to files"); | |
$f->attr('value', $field->folderPath); | |
$f->description = 'Relative to the templates folder.'; | |
$f->notes = 'If the files are located in /site/templates/scripts/, type: scripts/'; | |
$inputfields->add($f); | |
return $inputfields; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An Inputfield for handling files in a folder | |
* | |
* ©2013 Martijn Geerts | |
* | |
* ProcessWire 2.x | |
* Copyright (C) 2010 by Ryan Cramer | |
* Licensed under GNU/GPL v2, see LICENSE.TXT | |
* | |
* http://www.processwire.com | |
* http://www.ryancramer.com | |
* | |
*/ | |
class InputfieldFolder extends InputfieldText { | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Files in folder', | |
'version' => 100, | |
'summary' => 'Inputfield select to select a filename from a folder.', | |
'requires' => 'FieldtypeFolder' | |
); | |
} | |
public function __construct() { | |
parent::__construct(); | |
} | |
public function ___render() { | |
$value = $this->attr('value'); | |
$name = $this->attr('name'); | |
$folder = trim($this->folderPath); | |
$path = $this->config->paths->templates . trim($this->folderPath, "/") . "/"; | |
if(!is_dir($path)) return "<p>Not a valid directory given.</p>"; | |
// markup | |
$out = "<select name='$name'>"; | |
$out .= "<option value=''></option>"; | |
$handle = opendir($path); | |
while (false !== ($entry = readdir($handle))) { | |
if (strpos($entry, '.') !== 0 && is_file($path.$entry)) { | |
$selected = $entry == $value ? "selected" : null; | |
$out .= "<option value='$entry'$selected>$entry</option>"; | |
} | |
} | |
closedir($handle); | |
$out .= "</select>"; | |
return $out; | |
} | |
// be nice & trow error | |
protected function setAttributeValue($value) { | |
if(!strlen($value)) return ''; | |
$folder = trim($this->folderPath); | |
$file = $this->config->paths->templates . trim($this->folderPath, "/") . "/" . $value; | |
if(!is_file($file)) $this->error($this->name . ': ' . $this->_("File doesn't exist: ") . $file ); | |
return $value; | |
} | |
// remove some fields | |
public function ___getConfigInputfields() { | |
$inputfields = parent::___getConfigInputfields(); | |
$f = $inputfields->get('stripTags'); | |
if($f) $inputfields->remove($f); | |
$f = $inputfields->get('size'); | |
if($f) $inputfields->remove($f); | |
$f = $inputfields->get('maxlength'); | |
if($f) $inputfields->remove($f); | |
$f = $inputfields->get('placeholder'); | |
if($f) $inputfields->remove($f); | |
$f = $inputfields->get('pattern'); | |
if($f) $inputfields->remove($f); | |
return $inputfields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment