-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputfieldImageMarkdownCodeAdditionalFields.module
345 lines (270 loc) · 10.7 KB
/
InputfieldImageMarkdownCodeAdditionalFields.module
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php namespace ProcessWire;
/**
* ProcessWire 'Image Field Markdown Code Additional Fields' module
*
* Adds Markdown Code and Other Info to Image Fields.
*
* This file is licensed under the MIT license
*
* Copyright 2017 by Camilo Castro (clsource)
* https://ninjas.cl
*
*/
class InputfieldImageMarkdownCodeAdditionalFields extends WireData implements Module, ConfigurableModule
{
protected static $defaultConfigData = [
'enablePath' => true,
'enableUrl' => true,
'enableMdPath' => true,
'enableMdUrl' => true,
'enableHtmlPath' => true,
'enableHtmlUrl' => true,
'includeButtons' => true,
'includeCache' => false,
];
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo()
{
return [
// The module's title, typically a little more descriptive than the class name
'title' => 'Image Field Markdown Code',
// version number
'version' => 102,
// summary is brief description of what this module is
'summary' => 'Adds information to image fields. Helpful when using the markdown editor in text fields.',
// Optional URL to more information about the module
'href' => 'https://github.com/NinjasCL/InputfieldImageMarkdownCodeAdditionalFields',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => "template=admin",
// Optional font-awesome icon name, minus the 'fa-' part
'icon' => 'picture-o',
'requires' => ['ProcessWire>=3.0', 'InputfieldImage']
];
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init()
{
$this->addHookAfter('InputfieldImage::renderAdditionalFields', $this, 'process');
}
public function ready()
{
$this->addHookAfter('ProcessPageEdit::execute', $this, 'addScript');
}
public function addScript($event)
{
// return if ProcessPageEdit is being loaded via AJAX (image field)
if($this->config->ajax) return;
// add JS
$config = $this->config;
$info = $this->getModuleInfo();
$version = (int) $info['version'];
$config->scripts->add("{$config->urls->$this}{$this}.js?v={$version}");
}
/**
* Create the Configuration Page
*/
public function getModuleConfigInputFields(array $data)
{
foreach(self::$defaultConfigData as $key => $value)
{
if(!isset($data[$key])) $data[$key] = $value;
}
$fields = $this->wire(new InputfieldWrapper());
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enablePath';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Path?');
$f->description = $this->_('Add Path Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enableUrl';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Url?');
$f->description = $this->_('Add Url Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enableMdPath';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Markdown Path?');
$f->description = $this->_('Add Markdown Path Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enableMdUrl';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Markdown Url?');
$f->description = $this->_('Add Markdown Url Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enableHtmlPath';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Html Path?');
$f->description = $this->_('Add Html Path Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'enableHtmlUrl';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Enable Html Url?');
$f->description = $this->_('Add Html Url Info to Image Additional Fields');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'includeCache';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Include Cache Param?');
$f->description = $this->_('Add ?nc=timestamp Info to Paths and Urls.');
$fields->append($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$name = 'includeButtons';
$f->attr('name', $name);
$f->attr('checked', $data[$name]);
$f->label = $this->_('Include Quick Copy Buttons?');
$f->description = $this->_('Adds buttons that enable quick copy paths and urls.');
$fields->append($f);
return $fields;
}
/**
* Process the Hook to InputfieldImage::renderAdditionalFields
* @param $event
*/
public function process($event)
{
$pagefile = $event->arguments('pagefile');
$data = $this->wire('modules')->getModuleConfigData($this);
$enablePath = (isset($data['enablePath']) ? $data['enablePath'] : true);
$enableUrl = (isset($data['enableUrl']) ? $data['enableUrl'] : true);
$enableMDPath = (isset($data['enableMdPath']) ? $data['enableMdPath'] : true);
$enableMDUrl = (isset($data['enableMdUrl']) ? $data['enableMdUrl'] : true);
$enableHtmlPath = (isset($data['enableHtmlPath']) ? $data['enableHtmlPath'] : true);
$enableHtmlUrl = (isset($data['enableHtmlUrl']) ? $data['enableHtmlUrl'] : true);
$includeCache = (isset($data['includeCache']) ? $data['includeCache'] : false);
$includeButtons = (isset($data['includeButtons']) ? $data['includeButtons'] : true);
$event->return = $this->___render(
$pagefile,
$enablePath,
$enableUrl,
$enableMDPath,
$enableMDUrl,
$enableHtmlPath,
$enableHtmlUrl,
$includeCache,
$includeButtons
);
}
/**
* Renders the Html for the Additional Fields Hook
* @param (Pagefile) $pagefile
* @param (bool) $enablePath
* @param (bool) $enableUrl
* @param (bool) $enableMDPath
* @param (bool) $enableMDUrl
* @param (bool) $enableHtmlPath
* @param (bool) $enableHtmlUrl
* @param (bool) $includeCache
* @param (bool) $includeButtons
*/
public function ___render(
$pagefile,
$enablePath = true,
$enableUrl = true,
$enableMDPath = true,
$enableMDUrl = true,
$enableHtmlPath = true,
$enableHtmlUrl = true,
$includeCache = false,
$includeButtons = true
)
{
// remove trailing slash
$home = substr($this->wire('config')->urls->httpRoot, 0, -1);
$path = ($includeCache ? $pagefile->URL : $pagefile->url);
$url = $home . $path;
$html = '<div id="InputfieldImageMarkdownCodeAdditionalFields"><ul>';
if($enablePath)
{
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $path .'" data-type="string">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Path') . '</strong>: <code>' . $path . '</code>';
$html .= '</li>';
}
if($enableUrl)
{
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $url .'" data-type="string">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Url') . '</strong>: <code>' . $url . '</code>';
$html .= '</li>';
}
if($enableMDPath)
{
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $path .'" data-type="markdown">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Markdown Path') . '</strong>: <code>![](' . $path . ')</code>';
$html .= '</li>';
}
if($enableMDUrl)
{
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $url .'" data-type="markdown">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Markdown Url') . '</strong>: <code>![](' . $url . ')</code>';
$html .= '</li>';
}
if($enableHtmlPath)
{
$ahref = htmlentities('<img src="' . $path . '">');
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $path .'" data-type="html">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Html Path') . '</strong>: <code>' . $ahref . '</code>';
$html .= '</li>';
}
if($enableHtmlUrl)
{
$ahref = htmlentities('<img src="' . $url . '">');
$html .= '<li class="InputfieldImageMarkdownCodeAdditionalFieldsItem">';
if($includeButtons)
{
$html .= '<small><button class="InputfieldImageMarkdownCodeAdditionalFieldsButton ui-button ui-corner-all ui-state-default" data-href="' . $url .'" data-type="html">' . $this->_('Copy') . '</button></small> ';
}
$html .= '<strong>' . $this->_('Html Url') . '</strong>: <code>' . $ahref . '</code>';
$html .= '</li>';
}
$html .= '</ul></div>';
return $html;
}
}