-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProcessActivityLogService.module
More file actions
201 lines (156 loc) · 6.62 KB
/
ProcessActivityLogService.module
File metadata and controls
201 lines (156 loc) · 6.62 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
<?php
class ProcessActivityLogService extends Process {
const adminPageName = 'activity-log-service';
public static function getModuleInfo() {
return array(
'title' => __('Markup Activity Log (Service Module)'),
'summary' => __('Service module for making Ajax calls from MarkupActivityLog module'),
'version' => '1.0.1',
'author' => 'Tom Reno (Renobird)',
'href' => '',
'requires' => 'MarkupActivityLog',
);
}
static public function errorMessage() {
echo __("No entry found.");
}
public function init() {
$this->modules->get('JqueryWireTabs');
parent::init();
}
private function changesCKEditor($entry, $field, $editedPage) {
$markup = $this->modules->get('InputfieldMarkup');
$markup->markupText .= "<ul class='WireTabs nav change-triggers'>";
$markup->markupText .= "<li><a id='new' class='on'>" . __('New') . "</a></li>";
$markup->markupText .= "<li><a id='old'>" . __('Old') . "</a></li>";
$markup->markupText .= "</ul>";
$inputfield = $field->getInputfield($editedPage);
$inputfield->class = $field->inputfieldClass;
$inputfield->description = "";
$inputfield->notes = "";
$inputfield->attr("value", $entry->change->new);
$markup->markupText .= $inputfield->render();
// These are hidden by css, just there so JS can grab their content and swap out the contents of the editor.
$markup->markupText .= "<div id='new' class='textarea-change'>{$entry->change->new}</div>";
$markup->markupText .= "<div id='old' class='textarea-change'>{$entry->change->old}</div>";
return $markup->render();
}
private function changesTabs($entry, $field, $editedPage) {
$tabs = new InputfieldForm();
$tabs->attr('name+id', 'MarkupActivityLog-tabs');
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('New'));
$tab->attr('class', "change_tab");
$inputfield = $field->getInputfield($editedPage);
$inputfield->description = "";
$inputfield->notes = "";
$inputfield->attr("value", $entry->change->new);
$tab->append($inputfield);
$tabs->append($tab);
$tab = new InputfieldWrapper();
$tab->attr('title', $this->_('Old'));
$tab->attr('class', "change_tab");
$inputfield = $field->getInputfield($editedPage);
$inputfield->description = "";
$inputfield->notes = "";
$inputfield->name = "old";
$inputfield->attr("value", $entry->change->old);
$tab->append($inputfield);
$tabs->append($tab);
$out = $tabs->render();
if ($field->inputfieldClass == "InputfieldTinyMCE"){ // add script to initialize tinyMCE in modal, set to readonly.
$markup = $this->modules->get('InputfieldMarkup');
$markup->markupText = '<script>';
$markup->markupText .= 'tinyMCE.init({';
$markup->markupText .= 'mode:"textareas",';
$markup->markupText .= 'readonly: 1,';
foreach ($field->data as $k => $v) {
$markup->markupText .= $k . ': "' . preg_replace( "/\r|\n/", "", $v ) . '",'; // clean up new lines to prevent unexpected EOF error.
}
$markup->markupText .= "})";
$markup->markupText .= "</script>";
$out .= $markup->render();
}
return $out;
}
public function execute() {
list($pageID, $entryID, $fieldID) = explode('.', $this->input->get->ids);
// verify that we only have numeric characters to prevent injection.
if (!ctype_digit($pageID . $entryID . $fieldID)) $this->errorMessage();
$editedPage = $this->pages->get("id=$pageID");
$result = $this->database->query("SELECT * FROM MarkupActivityLog WHERE `id` = $entryID LIMIT 1");
if ($result->rowCount() > 0) {
$row = $result->fetch(PDO::FETCH_ASSOC);
// check that the pageID in the request is the same as the current edited page.
if ($pageID != $row['page_id']) $this->errorMessage();
// decode the changes field
$changes = json_decode($row['changes']);
// look for a matching field, and echo diff.
foreach($changes as $entry){
if ($entry->field != $fieldID) continue; // split if this is not the requested field.
$field = wire()->fields->get($entry->field);
$fieldLabel = $field->get('label|name');
$log = $this->modules->get('MarkupActivityLog');
$date = date($log->getDateFormat(), strtotime($row['date']));
$time = date($log->getTimeFormat(), strtotime($row['date']));
$u = wire('users')->get("id=".$row['user']);
$userFields = $log->getUserFields($u);
$markup = "<b>" . __("Changes to field: $fieldLabel") . "</b>";
$markup .= "<ul class='change-details'>
<li><i class='fa fa-calendar'></i> $date</li>
<li><i class='fa fa-clock-o'></i> $time</li>
<li><i class='fa fa-user'></i> $userFields</li>
</ul>
<div class='change-wrap'>"; // needed so AdminDefault and AdminReno have same spacing.
// if field isn't set, we aren't dealing with a core textarea field type.
// using switch here in the event I end up supporting other field types.
if (isset($field)){
switch ($field->inputfieldClass) {
case 'InputfieldCKEditor':
$markup .= $this->changesCKEditor($entry, $field, $editedPage);
break;
default:
$markup .= $this->changesTabs($entry, $field, $editedPage);
break;
}
}
$markup .= "</div>"; // end .change-wrap
echo "<div id='content' class='$this->className'>$markup</div>";
}
}
exit; // so that no additional output occurs.
}
/**
* Install/Uninstall lifted from FormBuilder module.
*
*/
protected function getInstalledPage($install = true) {
$admin = $this->pages->get($this->config->adminRootPageID);
$page = $admin->child("name=" . self::adminPageName .", include=hidden");
if ($install){
if(!$page->id) {
$page = new Page();
$page->parent = $admin;
$page->template = $this->templates->get('admin');
$page->name = self::adminPageName;
$page->title = 'Activity Log Service'; // Title of created page
$page->process = $this;
$page->status = Page::statusHidden;
$page->sort = $admin->numChildren;
$page->save();
}
}
return $page;
}
public function ___install() {
$page = $this->getInstalledPage();
$this->message(sprintf($this->_('Service Page installed to %s'), $page->path));
}
public function ___uninstall() {
$page = $this->getInstalledPage(false);
if($page->id) {
$this->message(sprintf($this->_('Removed %s'), $page->path));
$this->pages->delete($page);
}
}
}