-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessAjaxSave.module
174 lines (142 loc) · 5.72 KB
/
ProcessAjaxSave.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
<?php namespace ProcessWire;
/**
* ProcessWire AjaxSave Proof of concept
*
* 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 ProcessAjaxSave extends Process {
const pageName = 'ajaxsave-page-edit';
/**
* getModuleInfo is a method required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Process Ajax Save',
'version' => 13,
'summary' => __('Ajax save(without reload) for pages in admin.'),
'author' => "AutoSave by Philipp 'Soma' Urlich, reworked to AjaxSave by Bernd 'Klenkes' Klenk",
'singular' => true,
'autoload' => false,
'requires' => array("AjaxSave"),
);
}
public function execute() { }
/**
* Save the page via Ajax
* we don't use ProcessPageEdit::ajaxSave() as it doesn't seem to work with language fields
* so we use some technic to build the form and process it like ProcessPageEdit does with a regular save
*
* @return json array with status of save with messages
*/
public function executeAjaxSave(){
if($this->config->ajax && count($_POST)) {
$this->pageId = (int) $this->input->post("id");
$this->pageContext = $this->pages->get($this->pageId);
$this->pageContext->setTrackChanges(true);// not sure this is needed, what does it do? Leftover from AutoSave?
$this->pageEdit = $this->modules->get("ProcessPageEdit");
$form = $this->buildForm();
$this->processInput($form);
$this->pageContext->save();
if(count($form->getErrors())) $errors = true;
else $errors = false;
$message = array();
if(!$errors) {
$message['error'] = false;
$message['message'] = $this->_("Page saved!");
return json_encode($message);
} else {
$message['error'] = true;
$message['message'] = $this->_("AjaxSave: Page saved but with errors...");
// get errors for showing and clear them, so they don't stack (true)
$message['errors'] = $form->getErrors(true);
return json_encode($message);
}
}
}
/*
* Borrowed from Fredi front end edit
*
* This method is simplified version of /wire/modules/process/ProcessPageEdit.module processInput() method
*
* First process the input, then loops through all the form fields, set page field values and finally saves the page
*
* If field is wrapper, then iterates (this case only with full page save)
*
*/
public function ___processInput(Inputfield $form, $level = 0){
$form->setTrackChanges(true);
if(!$level) $form->processInput($this->input->post);
// Loop all the elements on the form
foreach($form->children as $field) {
// Process their values
$field->setTrackChanges(true);
if(wire('languages') && $field->useLanguages) {
$v = $this->pageContext->get($field->name);
if(is_object($v)) {
$v->setFromInputfield($field);
$this->pageContext->set($field->name, $v);
$this->pageContext->trackChange($field->name);
}
} else {
// prevent page status being changed for autosave
// or it will publish unpublished pages
if($field->name == "status") continue;
$this->pageContext->set($field->name, $field->value);
}
if($field instanceof InputfieldWrapper && count($field->getChildren())) $this->processInput($field, $level + 1);
}
}
/**
* build the form for saving
* @return InputfieldWrapper the form with fields
*/
public function buildForm() {
$form = $this->modules->get('InputfieldForm');
$form = $this->pageEdit->buildForm($form);
return $form;
}
/**
* Install the process module page
*/
public function ___install() {
if($this->pages->get("name=" . self::pageName)->id) return;
// create the page our module will be assigned to
$page = new Page();
$page->template = 'admin';
$page->name = self::pageName;
$page->status = 1025;// set page with process to hidden so it won't show up under Pages
// installs to the admin "Setup" menu ... change as you see fit
$page->parent = $this->pages->get($this->config->adminRootPageID)->child('name=page');
$page->process = $this;
// we will make the page title the same as our module title
$info = self::getModuleInfo();
$page->title = $info['title'];
// save the page
$page->save();
// tell the user we created this page
$this->message("Created Page: {$page->path}");
}
/**
* delete the process page
*/
public function ___uninstall() {
// find the page we installed, locating it by the process field (which has the module ID)
// it would probably be sufficient just to locate by name, but this is just to be extra sure.
$moduleID = $this->modules->getModuleID($this);
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName);
if($page->id) {
// if we found the page, let the user know and delete it
$this->message("Deleting Page: {$page->path}");
$page->delete();
}
}
}