1: <?php
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: use FieldtypePDF\PagePDF;
28: use FieldtypePDF\PagePDFs;
29: use FieldtypePDF\PDFConverter;
30:
31: class FieldtypePDF extends FieldtypeFile
32: {
33: public static function getModuleInfo()
34: {
35: return array(
36: 'version' => 110,
37: 'title' => __('PDF with thumbnail', __FILE__),
38: 'summary' => __('Field that stores one or more pdf files allowing thumbnail creation.', __FILE__),
39: 'href' => 'http://modules.processwire.com/modules/fieldtype-pdf',
40: 'author' => 'Richard Jedlička',
41: 'installs' => 'InputfieldPDF',
42: 'autoload' => true
43: );
44: }
45:
46: public function init()
47: {
48: spl_autoload_register(function($classname) {
49: $classname = ltrim($classname, '\\');
50: $filename = sprintf('%s/%s.php', __DIR__, str_replace('\\', DIRECTORY_SEPARATOR, $classname));
51:
52: if (is_file($filename)) {
53: require_once $filename;
54: }
55: });
56: }
57:
58: public function ___install()
59: {
60: if(! class_exists('Imagick')) {
61: throw new WireException(__('FieldtypePDF module requires the ImageMagick PHP extension.'));
62: }
63: }
64:
65: public function set($key, $value)
66: {
67: if($key === 'converterImagickOptions' && is_string($value)) {
68: $value = explode("\n", $value);
69: }
70:
71: return parent::set($key, $value);
72: }
73:
74: public function getBlankValue(Page $page, Field $field)
75: {
76: $pagePDFs = new PagePDFs($page);
77: $pagePDFs->setField($field);
78: $pagePDFs->setTrackChanges(true);
79:
80: return $pagePDFs;
81: }
82:
83: protected function getBlankPagefile(Pagefiles $pagefiles, $filename)
84: {
85: return new PagePDF($pagefiles, $filename);
86: }
87:
88: protected function getDefaultFileExtensions()
89: {
90: return 'pdf';
91: }
92:
93: public function ___getConfigInputfields(Field $field)
94: {
95: $inputfields = parent::___getConfigInputfields($field);
96:
97:
98: $extensionsInputField = $inputfields->get('extensions');
99: $extensionsInputField->collapsed = Inputfield::collapsedHidden;
100:
101:
102: $converterOptions = PDFConverter::$defaultOptions;
103:
104: $thumbnailFieldset = $this->modules->get('InputfieldFieldset');
105: $thumbnailFieldset->label = $this->_('PDF to image converter');
106: $thumbnailFieldset->description = $this->_('Options used when creating images from PDF files.');
107:
108: $formatField = $this->modules->get('InputfieldText');
109: $formatField->attr('name', 'converterFormat');
110: $formatField->attr('value', $field->converterFormat ?: $converterOptions['format']);
111: $formatField->label = $this->_('Image format');
112: $formatField->description = $this->_('Format used when creating the image (recomeneded are JPEG or PNG). Don\'t forget to check the file extension.');
113: $url = 'http://www.imagemagick.org/script/formats.php#supported';
114: $formatField->notes = $this->_("For supported formats see [$url]($url)");
115: $formatField->required = true;
116: $thumbnailFieldset->add($formatField);
117:
118: $extensionField = $this->modules->get('InputfieldText');
119: $extensionField->attr('name', 'imageExtenstion');
120: $extensionField->attr('value', $field->imageExtension ?: PagePDF::$defaultImageExtension);
121: $extensionField->label = $this->_('File extension');
122: $extensionField->description = $this->_('Sould correspond the image format.');
123: $extensionField->required = true;
124: $thumbnailFieldset->add($extensionField);
125:
126: $backgroundField = $this->modules->get('InputfieldText');
127: $backgroundField->attr('name', 'converterBackground');
128: $backgroundField->attr('value', $field->converterBackground ?: $converterOptions['background']);
129: $backgroundField->label = $this->_('Image background');
130: $backgroundField->description = $this->_('Color used as a background for transparent PDFs. Enter \'transparent\' if you don\'t want the background to be set. Default color is white.');
131: $url = 'http://www.imagemagick.org/script/color.php';
132: $backgroundField->notes = $this->_("For supported colors see [$url]($url)");
133: $thumbnailFieldset->add($backgroundField);
134:
135: $imagickFieldset = $this->modules->get('InputfieldFieldset');
136: $imagickFieldset->label = $this->_('ImageMagick settings (advanced)');
137: $imagickFieldset->collapsed = Inputfield::collapsedYes;
138: $imagickFieldset->description = $this->_('Settings set to the ImageMagick instance before reading the PDF file. **Change this only if you know what you are doing.**');
139:
140: $resolutionField = $this->modules->get('InputfieldText');
141: $resolutionField->attr('name', 'converterResolution');
142: $resolutionField->attr('value', $field->converterResolution ?: $converterOptions['resolution']);
143: $resolutionField->label = $this->_('density');
144: $url = 'http://www.imagemagick.org/script/command-line-options.php#density';
145: $resolutionField->notes = $this->_("see [$url]($url)");
146:
147: $colorspaceField = $this->modules->get('InputfieldSelect');
148: $colorspaceField->attr('name', 'converterColorspace');
149: $colorspaceField->label = $this->_('Color space');
150:
151: $imagickReflection = new ReflectionClass('Imagick');
152: foreach ($imagickReflection->getConstants() as $name => $value) {
153: if (preg_match('/^COLORSPACE_([^_]+)$/', $name, $matches)) {
154: $name = $matches[1];
155:
156: if ($name === 'UNDEFINED') {
157: $colorspaceField->addOption($value, '');
158: } else {
159: $colorspaceField->addOption($value, $name);
160: }
161: }
162: }
163:
164: $colorspaceField->attr('value', $field->converterColorspace ?: $converterOptions['colorspace']);
165:
166: $optionsField = $this->modules->get('InputfieldTextarea');
167: $optionsField->attr('name', 'converterImagickOptions');
168: $optionsField->attr('rows', 3);
169: $optionsField->label = $this->_('Options');
170: $optionsField->description = $this->_('One definition per line (key=value).');
171: $url = 'http://www.imagemagick.org/script/command-line-options.php#define';
172: $optionsField->notes = $this->_("See [$url]($url)");
173:
174: $optionsField->attr('value', $field->converterImagickOptions ?: implode("\n", $converterOptions['imagickOptions']));
175:
176: $imagickFieldset->append($resolutionField);
177: $imagickFieldset->append($colorspaceField);
178: $imagickFieldset->append($optionsField);
179:
180: $thumbnailFieldset->add($imagickFieldset);
181:
182: $inputfields->add($thumbnailFieldset);
183:
184: return $inputfields;
185: }
186: }
187:
188: