|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.documents |
|
4
|
|
|
* @author Nemein Oy http://www.nemein.com/ |
|
5
|
|
|
* @copyright Nemein Oy http://www.nemein.com/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* DBA class for org_openpsa_document |
|
11
|
|
|
* |
|
12
|
|
|
* Implements parameter and attachment methods for DM compatibility |
|
13
|
|
|
* |
|
14
|
|
|
* @property integer $author |
|
15
|
|
|
* @property integer $topic |
|
16
|
|
|
* @property integer $nextVersion |
|
17
|
|
|
* @property string $title |
|
18
|
|
|
* @property string $abstract |
|
19
|
|
|
* @property string $keywords |
|
20
|
|
|
* @property integer $docStatus For status flags like: DRAFT, etc, could even be a bitmask stored as integer |
|
21
|
|
|
status seems to be a reserved word in some layer between DM -> DB |
|
22
|
|
|
* @property string $content plaintext representation of content, non-ML |
|
23
|
|
|
* @property integer $orgOpenpsaAccesstype Shortcut for various ACL scenarios |
|
24
|
|
|
* @property string $orgOpenpsaOwnerWg The "owner" workgroup of this object |
|
25
|
|
|
* @package org.openpsa.documents |
|
26
|
|
|
*/ |
|
27
|
|
|
class org_openpsa_documents_document_dba extends midcom_core_dbaobject |
|
28
|
|
|
{ |
|
29
|
|
|
public $__midcom_class_name__ = __CLASS__; |
|
30
|
|
|
public $__mgdschema_class_name__ = 'org_openpsa_document'; |
|
31
|
|
|
|
|
32
|
|
|
public $autodelete_dependents = [ |
|
33
|
|
|
self::class => 'nextVersion' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
const STATUS_DRAFT = 4000; |
|
37
|
|
|
const STATUS_FINAL = 4001; |
|
38
|
|
|
const STATUS_REVIEW = 4002; |
|
39
|
|
|
|
|
40
|
5 |
|
public function _on_loaded() |
|
41
|
|
|
{ |
|
42
|
5 |
|
if ($this->title == "") { |
|
43
|
5 |
|
$this->title = "Document #{$this->id}"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
if (!$this->docStatus) { |
|
47
|
5 |
|
$this->docStatus = self::STATUS_DRAFT; |
|
48
|
|
|
} |
|
49
|
5 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function _on_creating() |
|
52
|
|
|
{ |
|
53
|
1 |
|
if (!$this->author) { |
|
54
|
1 |
|
$user = midcom::get()->auth->user->get_storage(); |
|
55
|
1 |
|
$this->author = $user->id; |
|
56
|
|
|
} |
|
57
|
1 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function _on_created() |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->_update_directory_timestamp(); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function _on_updated() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->_update_directory_timestamp(); |
|
68
|
|
|
|
|
69
|
|
|
// Sync the object's ACL properties into MidCOM ACL system |
|
70
|
1 |
|
$sync = new org_openpsa_core_acl_synchronizer(); |
|
71
|
1 |
|
$sync->write_acls($this, $this->orgOpenpsaOwnerWg, $this->orgOpenpsaAccesstype); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function _on_deleted() |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->_update_directory_timestamp(); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function _update_directory_timestamp() |
|
80
|
|
|
{ |
|
81
|
1 |
|
if ($this->nextVersion != 0) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
1 |
|
$parent = $this->get_parent(); |
|
85
|
1 |
|
if ( $parent |
|
86
|
1 |
|
&& $parent->component == 'org.openpsa.documents') { |
|
|
|
|
|
|
87
|
|
|
midcom::get()->auth->request_sudo('org.openpsa.documents'); |
|
88
|
|
|
|
|
89
|
|
|
$parent = new org_openpsa_documents_directory($parent); |
|
90
|
|
|
$parent->_use_rcs = false; |
|
91
|
|
|
$parent->update(); |
|
92
|
|
|
|
|
93
|
|
|
midcom::get()->auth->drop_sudo(); |
|
94
|
|
|
} |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function get_label() : string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->title; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Load the document's attachment |
|
104
|
|
|
* |
|
105
|
|
|
* @return midcom_db_attachment The attachment object |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function load_attachment() |
|
108
|
|
|
{ |
|
109
|
1 |
|
if (!$this->guid) { |
|
110
|
|
|
// Non-persistent object will not have attachments |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$attachments = org_openpsa_helpers::get_dm2_attachments($this, 'document'); |
|
115
|
1 |
|
if (empty($attachments)) { |
|
116
|
1 |
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (count($attachments) > 1) { |
|
120
|
|
|
debug_add("Multiple attachments have been found for document #" . $this->id . ", returning only the first.", MIDCOM_LOG_INFO); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return reset($attachments); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Try to generate a human-readable file type by doing some educated guessing based on mimetypes |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $mimetype The mimetype as reported by PHP |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function get_file_type($mimetype) : string |
|
132
|
|
|
{ |
|
133
|
|
|
if (!preg_match('/\//', $mimetype)) { |
|
134
|
|
|
return $mimetype; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//first, try if there is a direct translation |
|
138
|
|
|
if ($mimetype != midcom::get()->i18n->get_string($mimetype, 'org.openpsa.documents')) { |
|
139
|
|
|
return midcom::get()->i18n->get_string($mimetype, 'org.openpsa.documents'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
//if nothing is found, do some heuristics |
|
143
|
|
|
[$type, $subtype] = explode('/', $mimetype); |
|
144
|
|
|
$st_orig = $subtype; |
|
145
|
|
|
|
|
146
|
|
|
switch ($type) { |
|
147
|
|
|
case 'image': |
|
148
|
|
|
$subtype = strtoupper($subtype); |
|
149
|
|
|
break; |
|
150
|
|
|
case 'text': |
|
151
|
|
|
$type = 'document'; |
|
152
|
|
|
break; |
|
153
|
|
|
case 'application': |
|
154
|
|
|
$type = 'document'; |
|
155
|
|
|
|
|
156
|
|
|
if (preg_match('/^vnd\.oasis\.opendocument/', $subtype)) { |
|
157
|
|
|
$type = str_replace('vnd.oasis.opendocument.', '', $subtype); |
|
158
|
|
|
$subtype = 'OpenDocument'; |
|
159
|
|
|
} elseif (preg_match('/^vnd\.ms/', $subtype)) { |
|
160
|
|
|
$subtype = ucfirst(str_replace('vnd.ms-', '', $subtype)); |
|
161
|
|
|
} elseif (preg_match('/^vnd\.openxmlformats/', $subtype)) { |
|
162
|
|
|
$type = str_replace('vnd.openxmlformats-officedocument.', '', $subtype); |
|
163
|
|
|
$type = str_replace('ml.', ' ', $type); |
|
164
|
|
|
$subtype = 'OOXML'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$subtype = preg_replace('/^vnd\./', '', $subtype); |
|
168
|
|
|
$subtype = preg_replace('/^x-/', '', $subtype); |
|
169
|
|
|
|
|
170
|
|
|
break; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/* |
|
174
|
|
|
* if nothing matched so far and the subtype is alphanumeric, uppercase it on the theory |
|
175
|
|
|
* that it's probably a file extension |
|
176
|
|
|
*/ |
|
177
|
|
|
if ( $st_orig == $subtype |
|
178
|
|
|
&& preg_match('/^[a-z0-9]+$/', $subtype)) { |
|
179
|
|
|
$subtype = strtoupper($subtype); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return sprintf(midcom::get()->i18n->get_string('%s ' . $type, 'org.openpsa.documents'), $subtype); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function backup_version() : bool |
|
186
|
|
|
{ |
|
187
|
|
|
// Instantiate the backup object |
|
188
|
|
|
$backup = new org_openpsa_documents_document_dba(); |
|
189
|
|
|
$properties = $this->get_properties(); |
|
190
|
|
|
// Copy current properties |
|
191
|
|
|
foreach ($properties as $key) { |
|
192
|
|
|
if (!in_array($key, ['guid', 'id', 'metadata'])) { |
|
193
|
|
|
$backup->$key = $this->{$key}; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$backup->nextVersion = $this->id; |
|
198
|
|
|
if (!$backup->create()) { |
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// Copy parameters |
|
203
|
|
|
if ($params = $this->list_parameters()) { |
|
204
|
|
|
foreach ($params as $domain => $array) { |
|
205
|
|
|
foreach ($array as $name => $value) { |
|
206
|
|
|
$backup->set_parameter($domain, $name, $value); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// Find the attachments |
|
212
|
|
|
foreach ($this->list_attachments() as $original_attachment) { |
|
213
|
|
|
$backup_attachment = $backup->create_attachment($original_attachment->name, $original_attachment->title, $original_attachment->mimetype); |
|
214
|
|
|
|
|
215
|
|
|
$original_handle = $original_attachment->open('r'); |
|
216
|
|
|
if ( !$backup_attachment |
|
217
|
|
|
|| !$original_handle) { |
|
218
|
|
|
// Failed to copy the attachment, abort |
|
219
|
|
|
return $backup->delete(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
// Copy the contents |
|
223
|
|
|
$backup_handle = $backup_attachment->open('w'); |
|
224
|
|
|
|
|
225
|
|
|
stream_copy_to_stream($original_handle, $backup_handle); |
|
226
|
|
|
|
|
227
|
|
|
$original_attachment->close(); |
|
228
|
|
|
|
|
229
|
|
|
// Copy attachment parameters |
|
230
|
|
|
if ($params = $original_attachment->list_parameters()) { |
|
231
|
|
|
foreach ($params as $domain => $array) { |
|
232
|
|
|
foreach ($array as $name => $value) { |
|
233
|
|
|
if ($name == 'identifier') { |
|
234
|
|
|
$value = md5(time() . $backup_attachment->name); |
|
235
|
|
|
$backup->set_parameter('midcom.helper.datamanager2.type.blobs', 'guids_document', $value . ":" . $backup_attachment->guid); |
|
236
|
|
|
} |
|
237
|
|
|
$backup_attachment->set_parameter($domain, $name, $value); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
return true; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|