Repository navigation
Expand file tree
/
Copy pathInlineEditor.module
More file actions
379 lines (285 loc) · 11.1 KB
/
Copy pathInlineEditor.module
File metadata and controls
379 lines (285 loc) · 11.1 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Copyright Simon Fernandes
* @author: Simon Fernandes
* Date: 13/05/2013
* Time: 12:39
*/
class InlineEditor extends WireData implements Module, ConfigurableModule {
protected $user; //Reference to the logged in user
protected $admin_url; // Reference to the admin page that handles the AJAX requests
//Configurable options
public $background_color;
public $text_color;
public $link_color;
public $link_color_hover;
public $admin;
public $editable_color;
protected static $version = "004";
protected static $config = array(
"background_color" => "#181616",
"text_color" => "#fff",
"link_color" => "#83C1FF",
"link_color_hover" => "#ff0000",
"editable_color" => "#ff0000"
);
public function __construct(){
$this->background_color = self::$config['background_color'];
$this->text_color = self::$config['text_color'];
$this->link_color = self::$config['link_color'];
$this->link_color_hover = self::$config['link_color_hover'];
$this->editable_color = self::$config['editable_color'];
}
/**
* Returns an array of information about this module
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'An inline editor for front-end content management',
'summary' => 'This plugin allows you to edit content inline in the front end',
'version' => self::$version,
'author' => 'Simon Fernandes (sinmok@gmail.com)',
'permission' => 'page-edit',
'autoload' => true,
'installs' => array("InlineEditorProcess")
);
}
/**
* Used to configure the look and feel of the editor bar
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data)
{
$inputfields = new InputfieldWrapper();
//Background colour
$field = wire('modules')->get('InputfieldText');
$field->name = 'background_color';
$field->label = "Editor bar background colour";
$field->value = self::$config['background_color'];
$field->setAttribute("type","color");
if(isset($data['background_color'])){
$field->value = $data['background_color'];
}
$inputfields->add($field);
//text colour
$field = wire('modules')->get('InputfieldText');
$field->name = 'text_color';
$field->label = "Editor bar text colour";
$field->value = self::$config['text_color'];
$field->setAttribute("type","color");
if(isset($data['text_color'])){
$field->value = $data['text_color'];
}
$inputfields->add($field);
//link colour
$field = wire('modules')->get('InputfieldText');
$field->name = 'link_color';
$field->label = "Editor bar link colour";
$field->value = self::$config['link_color'];
$field->setAttribute("type","color");
if(isset($data['link_color'])){
$field->value = $data['link_color'];
}
$inputfields->add($field);
//link colour hover
$field = wire('modules')->get('InputfieldText');
$field->name = 'link_color_hover';
$field->label = "Editor bar link hover colour";
$field->value = self::$config['link_color_hover'];
$field->setAttribute("type","color");
if(isset($data['link_color_hover'])){
$field->value = $data['link_color_hover'];
}
$inputfields->add($field);
//Editable border colour
$field = wire('modules')->get('InputfieldText');
$field->name = 'editable_color';
$field->label = "Editable area border colour";
$field->value = self::$config['editable_color'];
$field->setAttribute("type","color");
if(isset($data['editable_color'])){
$field->value = $data['editable_color'];
}
$inputfields->add($field);
return $inputfields;
}
/**
* Adds the hooks. In particular the Page::render hook.
*/
public function init(){
// add a hook after each page is rendered and modify the output. This will cause the editor bar to be
// injected to the bottom of the page
$this->addHookAfter('Page::render', $this, 'setupDiv');
}
/**
* Setup some information
*/
public function ready(){
$this->user = wire("user");
$admin = $this->pages->get("template=admin, name=Inline_Editor_for_PW, limit=1");
$this->admin_url = $admin->httpUrl;
$this->admin = $this->user->name;
}
/**
* @param bool $jquery
* @return string An output
*/
public function scripts($jquery = false){
$page = wire("page");
//Get name of this folder
$folder = explode("/",__DIR__);
$folder_name = array_pop($folder);
$output = "";
//Does the user have permission?
if($page->editable()){
//Optionally add jQuery if the user needs it
if($jquery){
$output .= "\n \t <script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>";
}
$output .= "\n \t <script src='". wire('config')->urls->siteModules ."InlineEditor/js/dropzone/dropzone.min.js'/></script>";
$output .= "\n \t <script src='". wire('config')->urls->siteModules ."InlineEditor/js/ckeditor/ckeditor.js'></script>";
$output .= "\n \t <script src='". wire('config')->urls->siteModules ."InlineEditor/js/inlineeditor.js'></script>";
}
return $output;
}
/**
* This method injects the editor into the rendered page just before the HTML is sent to the browser
* Called once the page is rendered automatically by the PW core.
* Do NOT call this method yourself!
* @param $event
*/
public function setupDiv($event){
$page = $event->object;
// This should only effect public pages.
if($page->template == 'admin'){
return;
}
//Does the user have permission to edit the page?
if($page->editable()){
//Load the template
$content = $this->getInjectableEditor();
//Append the editor div to the bottom
$event->return = str_replace("</body>", $content."</body>", $event->return);
}
}
/**
* Sets up a HTML element ready to be edited
* The element that this function is called in should be a block element, otherwise CKEditor will not
* render the inline editor and you will lose some functionality.
* @usage <div $inline->setupField("a_field_name") > <?=$page->a_field_name?> </div>
* @param string $field_name The name of the field that we want to edit
* @param Page $page_ref
* @return string
*/
public function setupField($field_name = "",Page $page_ref = null){
$field_type = "text";
//Do we use the current page, or the page reference passed to us?
if(is_null($page_ref)){
$page = wire("page");
}
else{
$page = $page_ref;
}
//What type of field are we working with?
if(is_object($page->$field_name)){
$field_type = get_class($page->$field_name);
}
//Setup the string that will be injected into the HTML element
$return_string = " data-field-type='{$field_type}' contenteditable='true' data-pw-inline='true' data-page-id='{$page->id}' data-field-name='{$field_name}'";
//Is the user allowed to edit this page in the first place?
if($this->page->editable()){
return $return_string;
}
else{
//If not, return nothing
return "";
}
}
/**
* Called after the page is rendered by setupDiv()
* This method provides the CSS and HTML contents that will be injected just before the last <body> tag
* @return string Injectable HTML and CSS string
*/
protected function getInjectableEditor(){
$config = wire("config");
$version = wordwrap(self::$version, 1, '.', true);
$logout_url = $this->pages->get($config->adminRootPageID)->httpUrl."login/logout";
$content = <<<CONTENT
<!--
Injected editing bar from Inline Editor
© 2013 Simon Fernandes (sinmok[at]gmail.com)
-->
<style type="text/css">
.inline-editor-pw-module-editable{
border: 1px dashed {$this->editable_color};
}
#inline-editor-pw-module{
bottom: 0px;
height: 30px;
background-color: {$this->background_color};
color: {$this->text_color};
position: fixed;
width: 100%;
padding: 15px;
overflow: auto;
font-size: 13px;
}
#inline-editor-pw-module-close{
float: right;
color: {$this->link_color};
text-decoration: none;
}
#inline-editor-pw-module-close:hover{
color: {$this->link_color_hover};
}
#inline-editor-pw-module-status{
}
.inline-editor-pw-module-success{
color: green;
}
.inline-editor-pw-module-failure{
color: red;
}
#inline-editor-pw-module-url{
display: none;
}
.inline-editor-pw-module-33{
width: 33%;
float: left;
overflow: auto;
padding-top: 1px;
padding-bottom: 5px;
}
.inline-editor-pw-module-right{
text-align: right;
}
.inline-editor-pw-module-center{
text-align: center;
}
</style>
<div id="inline-editor-pw-module" style="float: left">
<div class="inline-editor-pw-module-33">
Inline Editor v {$version} Logged in as: {$this->admin}
</div>
<div class="inline-editor-pw-module-33 inline-editor-pw-module-center">
<div id="inline-editor-pw-module-status">
Click on a <span style="border:1px dashed{$this->editable_color}">dashed area</span> to edit.
Click off the area to save.
<br/>Double click <span style="border:1px dashed{$this->editable_color}">dashed images</span> to edit (Single Image field only)
</div>
</div>
<div class="inline-editor-pw-module-33 inline-editor-pw-module-right">
<a href="{$logout_url}" id="inline-editor-pw-module-close">
Close / Logout
</a>
<span id='inline-editor-pw-module-url'>{$this->admin_url}</span>
</div>
</div>
CONTENT;
return $content;
}
}