-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclass.draft.php
More file actions
194 lines (167 loc) · 6.23 KB
/
class.draft.php
File metadata and controls
194 lines (167 loc) · 6.23 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
<?php
/**
* Class: Draft
*
* Defines a simple draft-saving mechanism for osTicket which supports draft
* fetch and update via an ajax mechanism (include/ajax.draft.php).
*
* Fields:
* id - (int:auto:pk) Draft ID number
* body - (text) Body of the draft
* namespace - (string) Identifier of draft grouping — useful for multiple
* drafts on the same document by different users
* staff_id - (int:null) Staff owner of the draft
* extra - (text:json) Extra attributes of the draft
* created - (date) Date draft was initially created
* updated - (date:null) Date draft was last updated
*/
class Draft extends VerySimpleModel {
static $meta = array(
'table' => DRAFT_TABLE,
'pk' => array('id'),
'joins' => array(
'attachments' => array(
'constraint' => array(
"'D'" => 'Attachment.type',
'id' => 'Attachment.object_id',
),
'list' => true,
'null' => true,
'broker' => 'GenericAttachments',
),
),
);
function getId() { return $this->id; }
function getBody() { return $this->body; }
function getStaffId() { return $this->staff_id; }
function getNamespace() { return $this->namespace; }
static protected function getCurrentUserId() {
global $thisstaff, $thisclient;
$user = $thisstaff ?: $thisclient;
if ($user)
return $user->getId();
return 1 << 31;
}
static function getDraftAndDataAttrs($namespace, $id=0, $original='') {
$draft_body = null;
$attrs = array(sprintf('data-draft-namespace="%s"', Format::htmlchars($namespace)));
$criteria = array(
'namespace' => $namespace,
'staff_id' => self::getCurrentUserId(),
);
if ($id) {
$attrs[] = sprintf('data-draft-object-id="%s"', Format::htmlchars($id));
$criteria['namespace'] .= '.' . $id;
}
if ($draft = static::objects()->filter($criteria)->first()) {
$attrs[] = sprintf('data-draft-id="%s"', $draft->getId());
$draft_body = $draft->getBody();
}
$attrs[] = sprintf('data-draft-original="%s"',
Format::viewableImages($original, [], true));
return array(Format::viewableImages($draft_body, [], true),
implode(' ', $attrs));
}
static function getAttachmentIds($body=false) {
$attachments = array();
$body = Format::localizeInlineImages($body);
$matches = array();
if (preg_match_all('/"cid:([\\w.-]{32})"/', $body, $matches)) {
$files = AttachmentFile::objects()
->filter(array('key__in' => $matches[1]));
foreach ($files as $F) {
$attachments[] = array(
'id' => $F->getId(),
'name' => $F->getName(),
'inline' => true
);
}
}
return $attachments;
}
/*
* Ensures that the inline attachments cited in the body of this draft
* are also listed in the draft_attachment table. After calling this,
* the ::getAttachments() function should correctly return all inline
* attachments. This function should be called after creating a draft
* with an existing body
*/
function syncExistingAttachments() {
$matches = array();
if (!preg_match_all('/"cid:([\\w.-]{32})"/', $this->getBody(), $matches))
return;
// Purge current attachments
$this->attachments->deleteInlines();
foreach (AttachmentFile::objects()
->filter(array('key__in' => $matches[1]))
as $F
) {
$this->attachments->upload($F->getId(), true);
}
}
function setBody($body) {
// Change file.php urls back to content-id's
$body = Format::sanitize($body, false,
// Preserve annotation information, if any
'img=data-annotations,data-orig-annotated-image-src');
$this->body = $body ?: ' ';
$this->updated = SqlFunction::NOW();
return $this->save();
}
function delete() {
$this->attachments->deleteAll();
return parent::delete();
}
function isValid() {
// Required fields
return $this->namespace && isset($this->staff_id);
}
function save($refetch=false) {
if (!$this->isValid())
return false;
return parent::save($refetch);
}
static function create($vars=false) {
$attachments = @$vars['attachments'];
unset($vars['attachments']);
$vars['created'] = SqlFunction::NOW();
$vars['staff_id'] = self::getCurrentUserId();
$draft = new static($vars);
// Cloned attachments ...
if (false && $attachments && is_array($attachments))
// XXX: This won't work until the draft is saved
$draft->attachments->upload($attachments, true);
return $draft;
}
static function lookupByNamespaceAndStaff($namespace, $staff_id) {
return static::lookup(array(
'namespace'=>$namespace,
'staff_id'=>$staff_id
));
}
/**
* Delete drafts saved for a particular namespace. If the staff_id is
* specified, only drafts owned by that staff are deleted. Usually, if
* closing a ticket, the staff_id should be left null so that all drafts
* are cleaned up.
*/
static function deleteForNamespace($namespace, $staff_id=false) {
$attachments = Attachment::objects()
->filter(array('draft__namespace__startswith' => $namespace));
if ($staff_id)
$attachments->filter(array('draft__staff_id' => $staff_id));
$attachments->delete();
$criteria = array('namespace__like'=>$namespace);
if ($staff_id)
$criteria['staff_id'] = $staff_id;
return static::objects()->filter($criteria)->delete();
}
static function cleanup() {
// Keep drafts for two weeks (14 days)
$sql = 'DELETE FROM '.DRAFT_TABLE
." WHERE (updated IS NULL AND datediff(now(), created) > 14)
OR datediff(now(), updated) > 14";
return db_query($sql);
}
}
?>