-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFromISBN.module
More file actions
111 lines (108 loc) · 3.59 KB
/
Copy pathImageFromISBN.module
File metadata and controls
111 lines (108 loc) · 3.59 KB
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php namespace ProcessWire;
class ImageFromISBN extends Process implements ConfigurableModule {
public static function getModuleInfo() {
return [
'title' => 'Image from ISBN',
'summary' => 'Get a book cover image from Open Library API with its ISBN and adds it to a page',
'icon' => 'picture-o',
'version' => '1.0.0',
'author' => 'Alexandre Texier',
'autoload' => true,
'selectedTemplate' => '',
'selectedImageField' => '',
'selectedISBNField' => ''
];
}
public function ready() {
$this->pages->addHookAfter('save', function ($event) {
$page = $event->arguments(0);
//
if ($page->template->id == $this->selectedTemplate) {
$event->message("Image from ISBN handler");
//
$imagefield = $page->getField($this->selectedImageField);
$isbnfield = $page->getField($this->selectedISBNField);
//
if ($page->$imagefield == '' && $page->$isbnfield != '') {
$event->message("Fetching book cover");
//
// More about OpenLibrary API: https://openlibrary.org/dev/docs/api/covers
$isbn = $page->$isbnfield;
$coverURL = "https://covers.openlibrary.org/b/isbn/" . $isbn . "-L.jpg?default=false";
//
// begin curl
$handle = curl_init($coverURL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
// Get the HTML or whatever is linked in $url.
$response = curl_exec($handle);
// get url http code
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$event->message("http code = " . $httpCode);
if ($httpCode == 200) {
// Code is not a 200, the targeted cover exists, so adding it
$event->message("Book cover found");
$page->of(false);
$page->$imagefield->add($coverURL);
$page->save();
} else {
// code must be a 404
$event->warning("Book cover not found");
}
// end curl
curl_close($handle);
} else {
return;
}
} else {
return;
}
});
}
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
//
// Selected template for use by module
$f = $this->modules->get('InputfieldSelect');
$f->label = 'Selected Template';
$f->description = 'Template where module will work, must contains fields specified below';
$f->columnWidth = 100;
$f->attr('name', 'selectedTemplate');
$f->attr('value', $this->selectedTemplate);
foreach ($this->templates as $t) {
if (!($t->flags & Template::flagSystem)) {
$f->addOption($t->id, $t->name);
}
}
$f->collapsed = Inputfield::collapsedBlank;
$inputfields->add($f);
//
// Selected imagefield for use by module
$f = $this->modules->get('InputfieldSelect');
$f->label = 'Selected Image Field';
$f->description = 'Must be an image fieldtype with a max image size of 1';
$f->columnWidth = 50;
$f->attr('name', 'selectedImageField');
$f->attr('value', $this->selectedImageField);
foreach ($this->fields as $t) {
if (!($t->flags & Field::flagSystem) && $t->type instanceof FieldtypeImage) {
$f->addOption($t->id, $t->name);
}
}
$f->collapsed = Inputfield::collapsedBlank;
$inputfields->add($f);
//
// Selected isbn for use in
$f = $this->modules->get('InputfieldSelect');
$f->label = 'Selected ISBN Field';
$f->description = 'Must a text fieldtype, optionnaly with ISBN regex patern';
$f->columnWidth = 50;
$f->attr('name', 'selectedISBNField');
$f->attr('value', $this->selectedISBNField);
foreach ($this->fields as $t) {
if (!($t->flags & Field::flagSystem) && $t->type instanceof FieldtypeText) {
$f->addOption($t->id, $t->name);
}
}
$f->collapsed = Inputfield::collapsedBlank;
$inputfields->add($f);
}
}