KTAPIDocument.inc.php
42.7 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
<?php
/**
* $Id$
*
* KnowledgeTree Open Source Edition
* Document Management Made Simple
* Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
* Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*
*/
class KTAPI_Document extends KTAPI_FolderItem
{
/**
* This is a reference to the internal document object.
*
* @var Document
*/
var $document;
/**
* This is the id of the document.
*
* @var int
*/
var $documentid;
/**
* This is a reference to the parent folder.
*
* @var KTAPI_Folder
*/
var $ktapi_folder;
function get_documentid()
{
return $this->documentid;
}
/**
* This is used to get a document based on document id.
*
* @static
* @access public
* @param KTAPI $ktapi
* @param int $documentid
* @return KTAPI_Document
*/
function &get(&$ktapi, $documentid)
{
assert(!is_null($ktapi));
assert(is_a($ktapi, 'KTAPI'));
assert(is_numeric($documentid));
$documentid += 0;
$document = &Document::get($documentid);
if (is_null($document) || PEAR::isError($document))
{
return new KTAPI_Error(KTAPI_ERROR_DOCUMENT_INVALID,$document );
}
$user = $ktapi->can_user_access_object_requiring_permission($document, KTAPI_PERMISSION_READ);
if (is_null($user) || PEAR::isError($user))
{
return $user;
}
$folderid = $document->getParentID();
if (!is_null($folderid))
{
$ktapi_folder = &KTAPI_Folder::get($ktapi, $folderid);
}
else
{
$ktapi_folder = null;
}
// We don't do any checks on this folder as it could possibly be deleted, and is not required right now.
return new KTAPI_Document($ktapi, $ktapi_folder, $document);
}
function is_deleted()
{
return ($this->document->getStatusID() == 3);
}
/**
* This is the constructor for the KTAPI_Folder.
*
* @access private
* @param KTAPI $ktapi
* @param Document $document
* @return KTAPI_Document
*/
function KTAPI_Document(&$ktapi, &$ktapi_folder, &$document)
{
assert(is_a($ktapi,'KTAPI'));
assert(is_null($ktapi_folder) || is_a($ktapi_folder,'KTAPI_Folder'));
$this->ktapi = &$ktapi;
$this->ktapi_folder = &$ktapi_folder;
$this->document = &$document;
$this->documentid = $document->getId();
}
/**
* This checks a document into the repository
*
* @param string $filename
* @param string $reason
* @param string $tempfilename
* @param bool $major_update
*/
function checkin($filename, $reason, $tempfilename, $major_update=false)
{
if (!is_file($tempfilename))
{
return new PEAR_Error('File does not exist.');
}
$user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
if (!$this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT);
}
$options = array('major_update'=>$major_update);
$currentfilename = $this->document->getFileName();
if ($filename != $currentfilename)
{
$options['newfilename'] = $filename;
}
DBUtil::startTransaction();
$result = KTDocumentUtil::checkin($this->document, $tempfilename, $reason, $user, $options);
if (PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$result);
}
DBUtil::commit();
$tempfilename=addslashes($tempfilename);
$sql = "DELETE FROM uploaded_files WHERE tempfilename='$tempfilename'";
$result = DBUtil::runQuery($sql);
if (PEAR::isError($result))
{
return $result;
}
}
/**
* Link a document to another
*
* @param KTAPI_Document $document
*/
function link_document($document, $type)
{
$typeid = $this->ktapi->get_link_type_id($type);
if (PEAR::isError($typeid))
{
return $result;
}
$link = new DocumentLink($this->get_documentid(), $document->get_documentid(), $typeid );
$created = $link->create();
if ($created === false || PEAR::isError($created))
{
return new PEAR_Error(_kt('Could not create link'));
}
}
/**
* Unlink a document to another
*
* @param KTAPI_Document $document
*/
function unlink_document($document)
{
$sql = "DELETE FROM document_link WHERE parent_document_id=$this->documentid AND child_document_id=$document->documentid";
$result = DBUtil::runQuery($sql);
if (empty($result) || PEAR::isError($created))
{
return new PEAR_Error(_kt('Could not remove link'));
}
}
/**
*
* @return boolean
*/
function is_checked_out()
{
return ($this->document->getIsCheckedOut());
}
/**
* This reverses the checkout process.
*
* @param string $reason
*/
function undo_checkout($reason)
{
$user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
if (!$this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT);
}
DBUtil::startTransaction();
$this->document->setIsCheckedOut(0);
$this->document->setCheckedOutUserID(-1);
$res = $this->document->update();
if (($res === false) || PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res);
}
$oDocumentTransaction = & new DocumentTransaction($this->document, $reason, 'ktcore.transactions.force_checkin');
$res = $oDocumentTransaction->create();
if (($res === false) || PEAR::isError($res)) {
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res);
}
DBUtil::commit();
}
function get_linked_documents()
{
$sql = "
SELECT
dl.child_document_id as document_id,
dmv.name as title,
dcv.size,
w.name as workflow,
ws.name as workflow_state,
dlt.name as link_type
FROM
document_link dl
INNER JOIN document_link_types dlt ON dl.link_type_id=dlt.id
INNER JOIN documents d ON dl.child_document_id=d.id
INNER JOIN document_metadata_version dmv ON d.metadata_version_id=dmv.id
INNER JOIN document_content_version dcv ON dmv.content_version_id=dcv.id
LEFT OUTER JOIN workflow_documents wd ON d.id=wd.document_id
LEFT OUTER JOIN workflows w ON w.id=wd.workflow_id
LEFT OUTER JOIN workflow_states ws ON wd.state_id=ws.id
WHERE
dl.parent_document_id=$this->documentid
";
$rows = DBUtil::getResultArray($sql);
if (PEAR::isError($rows))
{
return $rows;
}
$result=array();
$read_permission = &KTPermission::getByName(KTAPI_PERMISSION_READ);
$user = $this->ktapi->get_user();
foreach($rows as $row)
{
$document = Document::get($row['document_id']);
if (PEAR::isError($document) || is_null($document))
{
continue;
}
if(!KTPermissionUtil::userHasPermissionOnItem($user, $read_permission, $document))
{
continue;
}
$result[] = array(
'document_id'=>(int)$row['document_id'],
'title'=> $row['title'],
'size'=>(int)$row['size'],
'workflow'=>empty($row['workflow'])?'n/a':$row['workflow'],
'workflow_state'=>empty($row['workflow_state'])?'n/a':$row['workflow_state'],
'link_type'=>empty($row['link_type'])?'unknown':$row['link_type'],
);
}
return $result;
}
/**
* This returns a URL to the file that can be downloaded.
*
* @param string $reason
*/
function checkout($reason)
{
$user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
if ($this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
}
DBUtil::startTransaction();
$res = KTDocumentUtil::checkout($this->document, $reason, $user);
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
}
DBUtil::commit();
}
/**
* This deletes a document from the folder.
*
* @param string $reason
*/
function delete($reason)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_DELETE);
if (PEAR::isError($user))
{
return $user;
}
if ($this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
}
DBUtil::startTransaction();
$res = KTDocumentUtil::delete($this->document, $reason);
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
}
DBUtil::commit();
}
/**
* This changes the owner of the file.
*
* @param string $ktapi_newuser
*/
function change_owner($newusername, $reason='Changing of owner.')
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_CHANGE_OWNERSHIP);
if (PEAR::isError($user))
{
return $user;
}
DBUtil::startTransaction();
$user = &User::getByUserName($newusername);
if (is_null($user) || PEAR::isError($user))
{
return new KTAPI_Error('User could not be found',$user);
}
$newuserid = $user->getId();
$this->document->setOwnerID($newuserid);
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR ,$res );
}
$res = KTPermissionUtil::updatePermissionLookup($this->document);
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
$oDocumentTransaction = & new DocumentTransaction($this->document, $reason, 'ktcore.transactions.permissions_change');
$res = $oDocumentTransaction->create();
if (($res === false) || PEAR::isError($res)) {
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
DBUtil::commit();
}
/**
* This copies the document to another folder.
*
* @param KTAPI_Folder $ktapi_target_folder
* @param string $reason
* @param string $newname
* @param string $newfilename
* @return KTAPI_Document
*/
function copy(&$ktapi_target_folder, $reason, $newname=null, $newfilename=null)
{
assert(!is_null($ktapi_target_folder));
assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
if (empty($newname))
{
$newname=null;
}
if (empty($newfilename))
{
$newfilename=null;
}
$user = $this->ktapi->get_user();
if ($this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
}
$target_folder = &$ktapi_target_folder->get_folder();
$result = $this->can_user_access_object_requiring_permission( $target_folder, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($result))
{
return $result;
}
$name = $this->document->getName();
$clash = KTDocumentUtil::nameExists($target_folder, $name);
if ($clash && !is_null($newname))
{
$name = $newname;
$clash = KTDocumentUtil::nameExists($target_folder, $name);
}
if ($clash)
{
return new PEAR_Error('A document with this title already exists in your chosen folder. Please choose a different folder, or specify a new title for the copied document.');
}
$filename=$this->document->getFilename();
$clash = KTDocumentUtil::fileExists($target_folder, $filename);
if ($clash && !is_null($newname))
{
$filename = $newfilename;
$clash = KTDocumentUtil::fileExists($target_folder, $filename);
}
if ($clash)
{
return new PEAR_Error('A document with this filename already exists in your chosen folder. Please choose a different folder, or specify a new filename for the copied document.');
}
DBUtil::startTransaction();
$new_document = KTDocumentUtil::copy($this->document, $target_folder, $reason);
if (PEAR::isError($new_document))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$new_document );
}
$new_document->setName($name);
$new_document->setFilename($filename);
$res = $new_document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
DBUtil::commit();
// FIXME do we need to refactor all trigger usage into the util function?
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('copyDocument', 'postValidate');
foreach ($aTriggers as $aTrigger) {
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$aInfo = array(
'document' => $new_document,
'old_folder' => $this->ktapi_folder->get_folder(),
'new_folder' => $target_folder,
);
$oTrigger->setInfo($aInfo);
$ret = $oTrigger->postValidate();
}
return KTAPI_Document::get($this->ktapi, $new_document->getId());
}
/**
* This moves the document to another folder.
*
* @param KTAPI_Folder $ktapi_target_folder
* @param string $reason
* @param string $newname
* @param string $newfilename
*/
function move(&$ktapi_target_folder, $reason, $newname=null, $newfilename=null)
{
assert(!is_null($ktapi_target_folder));
assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
if (empty($newname))
{
$newname=null;
}
if (empty($newfilename))
{
$newfilename=null;
}
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_DOCUMENT_MOVE);
if (PEAR::isError($user))
{
return $user;
}
if ($this->document->getIsCheckedOut())
{
return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
}
$target_folder = $ktapi_target_folder->get_folder();
$result= $this->can_user_access_object_requiring_permission( $target_folder, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($result))
{
return $result;
}
if (!KTDocumentUtil::canBeMoved($this->document))
{
return new PEAR_Error('Document cannot be moved.');
}
$name = $this->document->getName();
$clash = KTDocumentUtil::nameExists($target_folder, $name);
if ($clash && !is_null($newname))
{
$name = $newname;
$clash = KTDocumentUtil::nameExists($target_folder, $name);
}
if ($clash)
{
return new PEAR_Error('A document with this title already exists in your chosen folder. Please choose a different folder, or specify a new title for the moved document.');
}
$filename=$this->document->getFilename();
$clash = KTDocumentUtil::fileExists($target_folder, $filename);
if ($clash && !is_null($newname))
{
$filename = $newfilename;
$clash = KTDocumentUtil::fileExists($target_folder, $filename);
}
if ($clash)
{
return new PEAR_Error('A document with this filename already exists in your chosen folder. Please choose a different folder, or specify a new filename for the moved document.');
}
DBUtil::startTransaction();
$res = KTDocumentUtil::move($this->document, $target_folder, $user, $reason);
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res );
}
$this->document->setName($name);
$this->document->setFilename($filename);
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
DBUtil::commit();
}
/**
* This changes the filename of the document.
*
* @param string $newname
*/
function renameFile($newname)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
DBUtil::startTransaction();
$res = KTDocumentUtil::rename($this->document, $newname, $user);
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
DBUtil::commit();
}
/**
* This changes the document type of the document.
*
* @param string $newname
*/
function change_document_type($documenttype)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
$doctypeid = KTAPI::get_documenttypeid($documenttype);
if (PEAR::isError($doctypeid))
{
return $doctypeid;
}
if ($this->document->getDocumentTypeId() != $doctypeid)
{
DBUtil::startTransaction();
$this->document->setDocumentTypeId($doctypeid);
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
$metadata = $this->get_packed_metadata();
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
foreach ($aTriggers as $aTrigger)
{
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$aInfo = array(
"document" => $this->document,
"aOptions" => $packed,
);
$oTrigger->setInfo($aInfo);
$ret = $oTrigger->postValidate();
}
DBUtil::commit();
}
}
/**
* This changes the title of the document.
*
* @param string $newname
*/
function rename($newname)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
if ($this->document->getName() != $newname)
{
DBUtil::startTransaction();
$this->document->setName($newname);
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
}
DBUtil::commit();
}
}
/**
* This flags the document as 'archived'.
*
* @param string $reason
*/
function archive($reason)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
list($permission, $user) = $perm_and_user;
DBUtil::startTransaction();
$this->document->setStatusID(ARCHIVED);
$res = $this->document->update();
if (($res === false) || PEAR::isError($res)) {
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
}
$oDocumentTransaction = & new DocumentTransaction($this->document, sprintf(_kt('Document archived: %s'), $reason), 'ktcore.transactions.update');
$oDocumentTransaction->create();
DBUtil::commit();
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('archive', 'postValidate');
foreach ($aTriggers as $aTrigger)
{
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$aInfo = array(
'document' => $this->document,
);
$oTrigger->setInfo($aInfo);
$ret = $oTrigger->postValidate();
}
}
/**
* This starts a workflow on a document.
*
* @param string $workflow
*/
function start_workflow($workflow)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WORKFLOW);
if (PEAR::isError($user))
{
return $user;
}
$workflowid = $this->document->getWorkflowId();
if (!empty($workflowid))
{
return new PEAR_Error('A workflow is already defined.');
}
$workflow = KTWorkflow::getByName($workflow);
if (is_null($workflow) || PEAR::isError($workflow))
{
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $workflow);
}
DBUtil::startTransaction();
$result = KTWorkflowUtil::startWorkflowOnDocument($workflow, $this->document);
if (is_null($result) || PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $result);
}
DBUtil::commit();
}
/**
* This deletes the workflow on the document.
*
*/
function delete_workflow()
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WORKFLOW);
if (PEAR::isError($user))
{
return $user;
}
$workflowid=$this->document->getWorkflowId();
if (empty($workflowid))
{
return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
}
DBUtil::startTransaction();
$result = KTWorkflowUtil::startWorkflowOnDocument(null, $this->document);
if (is_null($result) || PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID,$result);
}
DBUtil::commit();
}
/**
* This performs a transition on the workflow
*
* @param string $transition
* @param string $reason
*/
function perform_workflow_transition($transition, $reason)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WORKFLOW);
if (PEAR::isError($user))
{
return $user;
}
$workflowid=$this->document->getWorkflowId();
if (empty($workflowid))
{
return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
}
$transition = &KTWorkflowTransition::getByName($transition);
if (is_null($transition) || PEAR::isError($transition))
{
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $transition);
}
DBUtil::startTransaction();
$result = KTWorkflowUtil::performTransitionOnDocument($transition, $this->document, $user, $reason);
if (is_null($result) || PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $transition);
}
DBUtil::commit();
}
/**
* This returns all metadata for the document.
*
* @return array
*/
function get_metadata()
{
$doctypeid = $this->document->getDocumentTypeID();
$fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->document, $doctypeid);
if (is_null($fieldsets) || PEAR::isError($fieldsets))
{
return array();
}
$results = array();
foreach ($fieldsets as $fieldset)
{
if ($fieldset->getIsConditional()) { /* this is not implemented...*/ continue; }
$fields = $fieldset->getFields();
$result = array('fieldset' => $fieldset->getName(),
'description' => $fieldset->getDescription());
$fieldsresult = array();
foreach ($fields as $field)
{
$value = 'n/a';
$fieldvalue = DocumentFieldLink::getByDocumentAndField($this->document, $field);
if (!is_null($fieldvalue) && (!PEAR::isError($fieldvalue)))
{
$value = $fieldvalue->getValue();
}
$controltype = 'string';
if ($field->getHasLookup())
{
$controltype = 'lookup';
if ($field->getHasLookupTree())
{
$controltype = 'tree';
}
}
switch ($controltype)
{
case 'lookup':
$selection = KTAPI::get_metadata_lookup($field->getId());
break;
case 'tree':
$selection = KTAPI::get_metadata_tree($field->getId());
break;
default:
$selection= array();
}
$fieldsresult[] = array(
'name' => $field->getName(),
'required' => $field->getIsMandatory(),
'value' => $value,
'description' => $field->getDescription(),
'control_type' => $controltype,
'selection' => $selection
);
}
$result['fields'] = $fieldsresult;
$results [] = $result;
}
return $results;
}
function get_packed_metadata($metadata=null)
{
global $default;
if (is_null($metadata))
{
$metadata = $this->get_metadata();
}
$packed = array();
foreach($metadata as $fieldset_metadata)
{
if (is_array($fieldset_metadata))
{
$fieldsetname=$fieldset_metadata['fieldset'];
$fields=$fieldset_metadata['fields'];
}
elseif (is_a($fieldset_metadata, 'stdClass'))
{
$fieldsetname=$fieldset_metadata->fieldset;
$fields=$fieldset_metadata->fields;
}
else
{
$default->log->debug("unexpected fieldset type");
continue;
}
$fieldset = KTFieldset::getByName($fieldsetname);
if (is_null($fieldset) || PEAR::isError($fieldset))
{
$default->log->debug("could not resolve fieldset: $fieldsetname");
// exit graciously
continue;
}
foreach($fields as $fieldinfo)
{
if (is_array($fieldinfo))
{
$fieldname = $fieldinfo['name'];
$value = $fieldinfo['value'];
}
elseif (is_a($fieldinfo, 'stdClass'))
{
$fieldname = $fieldinfo->name;
$value = $fieldinfo->value;
}
else
{
$default->log->debug("unexpected fieldinfo type");
continue;
}
$field = DocumentField::getByFieldsetAndName($fieldset, $fieldname);
if (is_null($field) || PEAR::isError($fieldset))
{
$default->log->debug("could not resolve field: $fieldname");
// exit graciously
continue;
}
$packed[] = array($field, $value);
}
}
return $packed;
}
/**
* This updates the metadata on the file. This includes the 'title'.
*
* @param array This is an array containing the metadata to be associated with the file.
*/
function update_metadata($metadata)
{
global $default;
if (empty($metadata))
{
return;
}
$packed = $this->get_packed_metadata($metadata);
DBUtil::startTransaction();
$result = KTDocumentUtil::saveMetadata($this->document, $packed);
if (is_null($result))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR);
}
if (PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error(sprintf(_kt("Unexpected validation failure: %s."), $result->getMessage()));
}
DBUtil::commit();
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
foreach ($aTriggers as $aTrigger) {
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$aInfo = array(
"document" => $this->document,
"aOptions" => $packed,
);
$oTrigger->setInfo($aInfo);
$ret = $oTrigger->postValidate();
}
}
/**
* This updates the system metadata on the document.
*
* @param array $sysdata
*/
function update_sysdata($sysdata)
{
if (empty($sysdata))
{
return;
}
$owner_mapping = array(
'created_by'=>'creator_id',
'modified_by'=>'modified_user_id',
'owner'=>'owner_id'
);
$documents = array();
$document_content = array();
foreach($sysdata as $rec)
{
if (is_object($rec))
{
$name = $rec->name;
$value = sanitizeForSQL($rec->value);
}
elseif(is_array($rec))
{
$name = $rec['name'];
$value = sanitizeForSQL($rec['value']);
}
else
{
// just ignore
continue;
}
switch($name)
{
case 'created_date':
$documents['created'] = $value;
break;
case 'modified_date':
$documents['modified'] = $value;
break;
case 'is_immutable':
$documents['immutable'] = in_array(strtolower($value), array('1','true','on','yes'))?'1':'0';
break;
case 'filename':
$document_content['filename'] = $value;
break;
case 'major_version':
$document_content['major_version'] = $value;
break;
case 'minor_version':
$document_content['minor_version'] = $value;
break;
case 'version':
$version = number_format($value + 0,5);
list($major_version, $minor_version) = explode('.', $version);
$document_content['major_version'] = $major_version;
$document_content['minor_version'] = $minor_version;
break;
case 'mime_type':
$sql = "select id from mime_types where mimetypes='$value'";
$value = DBUtil::getResultArray($sql);
if (PEAR::isError($value))
{
return $value;
}
if (count($value) == 0)
{
break;
}
$value = $value[0]['id'];
$document_content['mime_id'] = $value;
break;
case 'owner':
case 'created_by':
case 'modified_by':
$sql = "select id from users where name='$value'";
$userId = DBUtil::getResultArray($sql);
if (PEAR::isError($userId))
{
return $userId;
}
if (empty($userId))
{
$sql = "select id from users where username='$value'";
$userId = DBUtil::getResultArray($sql);
if (PEAR::isError($userId))
{
return $userId;
}
}
if (empty($userId))
{
// if not found, not much we can do
break;
}
$userId=$userId[0];
$userId=$userId['id'];
$name = $owner_mapping[$name];
$documents[$name] = $userId;
break;
default:
// TODO: we should do some logging
//return new PEAR_Error('Unexpected field: ' . $name);
}
}
if (count($documents) > 0)
{
$sql = "UPDATE documents SET ";
$i=0;
foreach($documents as $name=>$value)
{
if ($i++ > 0) $sql .= ",";
if (is_numeric($value))
$sql .= "$name=$value";
else
$sql .= "$name='$value'";
}
$sql .= " WHERE id=$this->documentid";
$result = DBUtil::runQuery($sql);
if (PEAR::isError($result))
{
return $result;
}
}
if (count($document_content) > 0)
{
$content_id = $this->document->getContentVersionId();
$sql = "UPDATE document_content_version SET ";
$i=0;
foreach($document_content as $name=>$value)
{
if ($i++ > 0) $sql .= ",";
$sql .= "$name='$value'";
}
$sql .= " WHERE id=$content_id";
$result = DBUtil::runQuery($sql);
if (PEAR::isError($result))
{
return $result;
}
}
}
function clearCache()
{
// TODO: we should only clear the cache for the document we are working on
// this is a quick fix but not optimal!!
$metadataid = $this->document->getMetadataVersionId();
$contentid = $this->document->getContentVersionId();
$cache = KTCache::getSingleton();
$cache->remove('KTDocumentMetadataVersion/id', $metadataid);
$cache->remove('KTDocumentContentVersion/id', $contentid);
$cache->remove('KTDocumentCore/id', $this->documentid);
$cache->remove('Document/id', $this->documentid);
unset($GLOBALS['_OBJECTCACHE']['KTDocumentMetadataVersion'][$metadataid]);
unset($GLOBALS['_OBJECTCACHE']['KTDocumentContentVersion'][$contentid]);
unset($GLOBALS['_OBJECTCACHE']['KTDocumentCore'][$this->documentid]);
$this->document = &Document::get($this->documentid);
}
function mergeWithLastMetadataVersion()
{
// keep latest metadata version
$metadata_version = $this->document->getMetadataVersion();
if ($metadata_version == 0)
{
// this could theoretically happen in the case we are updating metadata and sysdata, but no metadata fields are specified.
return;
}
$metadata_id = $this->document->getMetadataVersionId();
// get previous version
$sql = "SELECT id, metadata_version FROM document_metadata_version WHERE id<$metadata_id AND document_id=$this->documentid order by id desc";
$old = DBUtil::getResultArray($sql);
if (is_null($old) || PEAR::isError($old))
{
return new PEAR_Error('Previous version could not be resolved');
}
// only interested in the first one
$old=$old[0];
$old_metadata_id = $old['id'];
$old_metadata_version = $old['metadata_version'];
DBUtil::startTransaction();
// delete previous metadata version
$sql = "DELETE FROM document_metadata_version WHERE id=$old_metadata_id";
$rs = DBUtil::runQuery($sql);
if (PEAR::isError($rs))
{
DBUtil::rollback();
return $rs;
}
// make latest equal to previous
$sql = "UPDATE document_metadata_version SET metadata_version=$old_metadata_version WHERE id=$metadata_id";
$rs = DBUtil::runQuery($sql);
if (PEAR::isError($rs))
{
DBUtil::rollback();
return $rs;
}
$sql = "UPDATE documents SET metadata_version=$old_metadata_version WHERE id=$this->documentid";
$rs = DBUtil::runQuery($sql);
if (PEAR::isError($rs))
{
DBUtil::rollback();
return $rs;
}
DBUtil::commit();
$this->clearCache();
}
/**
* This returns a workflow transition
*
* @return array
*/
function get_workflow_transitions()
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WORKFLOW);
if (PEAR::isError($user))
{
return $user;
}
$workflowid=$this->document->getWorkflowId();
if (empty($workflowid))
{
return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
}
$result = array();
$transitions = KTWorkflowUtil::getTransitionsForDocumentUser($this->document, $user);
if (is_null($transitions) || PEAR::isError($transitions))
{
return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $transitions);
}
foreach($transitions as $transition)
{
$result[] = $transition->getName();
}
return $result;
}
/**
* This returns the current workflow state
*
* @return string
*/
function get_workflow_state()
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WORKFLOW);
if (PEAR::isError($user))
{
return $user;
}
$workflowid=$this->document->getWorkflowId();
if (empty($workflowid))
{
return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
}
$result = array();
$state = KTWorkflowUtil::getWorkflowStateForDocument($this->document);
if (is_null($state) || PEAR::isError($state))
{
return new PEAR_Error(KTAPI_ERROR_WORKFLOW_INVALID);
}
$statename = $state->getName();
return $statename;
}
/**
* This returns detailed information on the document.
*
* @return array
*/
function get_detail()
{
global $default;
// make sure we ge tthe latest
$this->clearCache();
$detail = array();
$document = $this->document;
// get the document id
$detail['document_id'] = (int) $document->getId();
// get the title
$detail['title'] = $document->getName();
// get the document type
$documenttypeid=$document->getDocumentTypeID();
$documenttype = '* unknown *';
if (is_numeric($documenttypeid))
{
$dt = DocumentType::get($documenttypeid);
if (!is_null($dt) && !PEAR::isError($dt))
{
$documenttype=$dt->getName();
}
}
$detail['document_type'] = $documenttype;
// get the filename
$detail['filename'] = $document->getFilename();
// get the filesize
$detail['filesize'] = (int) $document->getFileSize();
// get the folder id
$detail['folder_id'] = (int) $document->getFolderID();
// get the creator
$userid = $document->getCreatorID();
$username='n/a';
if (is_numeric($userid))
{
$username = '* unknown *';
$user = User::get($userid);
if (!is_null($user) && !PEAR::isError($user))
{
$username = $user->getName();
}
}
$detail['created_by'] = $username;
// get the creation date
$detail['created_date'] = $document->getCreatedDateTime();
// get the checked out user
$userid = $document->getCheckedOutUserID();
$username='n/a';
if (is_numeric($userid))
{
$username = '* unknown *';
$user = User::get($userid);
if (!is_null($user) && !PEAR::isError($user))
{
$username = $user->getName();
}
}
$detail['checked_out_by'] = $username;
// get the checked out date
list($major, $minor, $fix) = explode('.', $default->systemVersion);
if ($major == 3 && $minor >= 5)
{
$detail['checked_out_date'] = $document->getCheckedOutDate();
}
else
{
$detail['checked_out_date'] = $detail['modified_date'];
}
if (is_null($detail['checked_out_date'])) $detail['checked_out_date'] = 'n/a';
// get the modified user
$userid = $document->getModifiedUserId();
$username='n/a';
if (is_numeric($userid))
{
$username = '* unknown *';
$user = User::get($userid);
if (!is_null($user) && !PEAR::isError($user))
{
$username = $user->getName();
}
}
$detail['modified_by'] = $detail['updated_by'] = $username;
// get the modified date
$detail['updated_date'] = $detail['modified_date'] = $document->getLastModifiedDate();
// get the owner
$userid = $document->getOwnerID();
$username='n/a';
if (is_numeric($userid))
{
$username = '* unknown *';
$user = User::get($userid);
if (!is_null($user) && !PEAR::isError($user))
{
$username = $user->getName();
}
}
$detail['owned_by'] = $username;
// get the version
$detail['version'] = $document->getVersion();
// check immutability
$detail['is_immutable'] = (bool) $document->getImmutable();
// check permissions
$detail['permissions'] = 'not available';
// get workflow name
$workflowid = $document->getWorkflowId();
$workflowname='n/a';
if (is_numeric($workflowid))
{
$workflow = KTWorkflow::get($workflowid);
if (!is_null($workflow) && !PEAR::isError($workflow))
{
$workflowname = $workflow->getName();
}
}
$detail['workflow'] = $workflowname;
// get the workflow state
$stateid = $document->getWorkflowStateId();
$workflowstate = 'n/a';
if (is_numeric($stateid))
{
$state = KTWorkflowState::get($stateid);
if (!is_null($state) && !PEAR::isError($state))
{
$workflowstate = $state->getName();
}
}
$detail['workflow_state']=$workflowstate;
// get the full path
$detail['full_path'] = $this->ktapi_folder->get_full_path() . '/' . $this->get_title();
// get mime info
$mimetypeid = $document->getMimeTypeID();
$detail['mime_type'] =KTMime::getMimeTypeName($mimetypeid);
$detail['mime_icon_path'] =KTMime::getIconPath($mimetypeid);
$detail['mime_display'] =KTMime::getFriendlyNameForString($type);
// get the storage path
$detail['storage_path'] = $document->getStoragePath();
return $detail;
}
function get_title()
{
return $this->document->getDescription();
}
/**
* This does a download of a version of the document.
*
* @param string $version
*/
function download($version=null)
{
$storage =& KTStorageManagerUtil::getSingleton();
$options = array();
$oDocumentTransaction = & new DocumentTransaction($this->document, 'Document downloaded', 'ktcore.transactions.download', $aOptions);
$oDocumentTransaction->create();
}
/**
* This returns the transaction history for the document.
*
* @return array
*/
function get_transaction_history()
{
$sQuery = 'SELECT DTT.name AS transaction_name, U.name AS username, DT.version AS version, DT.comment AS comment, DT.datetime AS datetime ' .
'FROM ' . KTUtil::getTableName('document_transactions') . ' AS DT INNER JOIN ' . KTUtil::getTableName('users') . ' AS U ON DT.user_id = U.id ' .
'INNER JOIN ' . KTUtil::getTableName('transaction_types') . ' AS DTT ON DTT.namespace = DT.transaction_namespace ' .
'WHERE DT.document_id = ? ORDER BY DT.datetime DESC';
$aParams = array($this->documentid);
$transactions = DBUtil::getResultArray(array($sQuery, $aParams));
if (is_null($transactions) || PEAR::isError($transactions))
{
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $transactions );
}
return $transactions;
}
/**
* This returns the version history on the document.
*
* @return array
*/
function get_version_history()
{
$metadata_versions = KTDocumentMetadataVersion::getByDocument($this->document);
$versions = array();
foreach ($metadata_versions as $version)
{
$document = &Document::get($this->documentid, $version->getId());
$version = array();
$userid = $document->getModifiedUserId();
$user = User::get($userid);
if (PEAR::isError($user))
{
$username = $user->getName();
}
else
{
$username = 'Unknown';
}
$version['user'] = $username;
$version['metadata_version'] = $document->getMetadataVersion();
$version['content_version'] = $document->getVersion();
$versions[] = $version;
}
return $versions;
}
/**
* This expunges a document from the system.
*
* @access public
*/
function expunge()
{
if ($this->document->getStatusID() != 3)
{
return new PEAR_Error('You should not purge this');
}
DBUtil::startTransaction();
$transaction = & new DocumentTransaction($this->document, "Document expunged", 'ktcore.transactions.expunge');
$transaction->create();
$this->document->delete();
$this->document->cleanupDocumentData($this->documentid);
$storage =& KTStorageManagerUtil::getSingleton();
$result= $storage->expunge($this->document);
DBUtil::commit();
}
/**
* This expunges a document from the system.
*
* @access public
*/
function restore()
{
DBUtil::startTransaction();
$storage =& KTStorageManagerUtil::getSingleton();
$folder = Folder::get($this->document->getRestoreFolderId());
if (PEAR::isError($folder))
{
$this->document->setFolderId(1);
$folder = Folder::get(1);
}
else
{
$this->document->setFolderId($this->document->getRestoreFolderId());
}
$storage->restore($this->document);
$this->document->setStatusId(LIVE);
$this->document->setPermissionObjectId($folder->getPermissionObjectId());
$res = $this->document->update();
$res = KTPermissionUtil::updatePermissionLookup($this->document);
$user = $this->ktapi->get_user();
$oTransaction = new DocumentTransaction($this->document, 'Restored from deleted state by ' . $user->getName(), 'ktcore.transactions.update');
$oTransaction->create();
DBUtil::commit();
}
}
?>