KTAPIDocument.inc.php
66.1 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
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
<?php
/**
* Document API for KnowledgeTree
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008, 2009 KnowledgeTree Inc.
* Portions copyright 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 KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, 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.
*
* @copyright 2008-2009, KnowledgeTree Inc.
* @license GNU General Public License version 3
* @author KnowledgeTree Team
* @package KnowledgeTree API
* @version Version 0.9
*/
require_once(KT_DIR . '/ktwebservice/KTDownloadManager.inc.php');
/**
* API for the handling document operations within KnowledgeTree
*
* @author KnowledgeTree Team
* @package KnowledgeTree API
* @version 0.9
*/
class KTAPI_Document extends KTAPI_FolderItem
{
/**
* This is a reference to the internal document object.
*
* @access protected
* @var object of Document
*/
var $document;
/**
* This is the id of the document.
*
* @access protected
* @var int
*/
var $documentid;
/**
* This is a reference to the parent folder.
*
* @access protected
* @var object of KTAPI_Folder
*/
var $ktapi_folder;
/**
* Gets the id of the current document
*
* @author KnowledgeTree Team
* @access public
* @return string The document id
*/
function get_documentid()
{
return $this->documentid;
}
/**
* This is used to get a document based on document id.
*
* @author KnowledgeTree Team
* @static
* @access public
* @param KTAPI $ktapi The ktapi object
* @param int $documentid The document id
* @return KTAPI_Document The document object
*/
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);
}
/**
* Checks if a document has been deleted
*
* @author KnowledgeTree Team
* @access public
* @return boolean TRUE if deleted | FALSE if in a different state
*/
function is_deleted()
{
return ($this->document->getStatusID() == 3);
}
/**
* Checks if the document is a shortcut
*
* @author KnowledgeTree Team
* @access public
* @return boolean TRUE if it is a shortcut | FALSE if not
*/
function is_shortcut()
{
return $this->document->isSymbolicLink();
}
/**
* Retrieves the shortcuts linking to this document
*
* @author KnowledgeTree Team
* @access public
* @return array The shortcuts linked to the document
*/
function get_shortcuts()
{
return $this->document->getSymbolicLinks();
}
/**
* This is the constructor for the KTAPI_Document
*
* @author KnowledgeTree Team
* @access private
* @param KTAPI $ktapi The KTAPI object
* @param KTAPI_Folder $ktapi_folder The parent folder object
* @param Document $document The internal document object
* @return KTAPI_Document
*/
function KTAPI_Document(&$ktapi, &$ktapi_folder, &$document)
{
assert($ktapi instanceof KTAPI); //is_a($ktapi,'KTAPI'));
assert(is_null($ktapi_folder) || $ktapi_folder instanceof 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.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* if($document->is_checked_out()){
* $document->checkin('filename.txt', 'Reason for checkin', '/tmp/filename');
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param string $filename The name of the file
* @param string $reason The reason for checking the document in
* @param string $tempfilename The location of the temporary file
* @param bool $major_update Determines if the version number should have a major increment (+1) or a minor increment (+0.1)
*/
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);
}
$filename = KTUtil::replaceInvalidCharacters($filename);
$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();
KTUploadManager::temporary_file_imported($tempfilename);
}
/**
* Removes the update notification for the document
*
* @author KnowledgeTree Team
* @access public
*/
function removeUpdateNotification()
{
$sql = "DELETE FROM notifications WHERE data_int_1=$this->documentid AND data_str_1='ModifyDocument'";
DBUtil::runQuery($sql);
}
/**
* Creates a link between two documents
*
* @author KnowledgeTree Team
* @access public
* @param KTAPI_Document $document The document object
* @param string $type The link relationship type: depended on|Attachment|Reference|Copy|Default
* @return PEAR_Error|void Returns nothing on success | a PEAR_Error object on failure
*
*/
function link_document($document, $type)
{
$typeid = $this->ktapi->get_link_type_id($type);
if (PEAR::isError($typeid))
{
return $typeid;
}
$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'));
}
}
/**
* Removes the link between two documents
*
* @author KnowledgeTree Team
* @access public
* @param KTAPI_Document $document The document object
* @return PEAR_Error|void Returns nothing on success | a PEAR_Error object on failure
*/
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'));
}
}
/**
* Checks whether the document has been checked out.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* if($document->is_checked_out()){
* continue;
* }else{
* $document->checkout('Reason for document checkout');
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @return boolean TRUE if checked out | FALSE if not
*/
function is_checked_out()
{
return ($this->document->getIsCheckedOut());
}
/**
* Cancels the checkout on a document
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* if($document->is_checked_out()){
* $document->undo_checkout('Reason for cancelling checkout');
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param string $reason The reason for cancelling
* @return PEAR_Error|void Returns nothing on success | a PEAR_Error on failure
*/
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();
}
/**
* Gets a list of linked documents
*
* @author KnowledgeTree Team
* @access public
* @return array|PEAR_Error Returns a list of linked documents on success | a PEAR_Error on failure
*/
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, dtl.name as document_type,
dcv.major_version, dcv.minor_version, d.oem_no
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
INNER JOIN document_types_lookup dtl ON dtl.id=dmv.document_type_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;
}
$oem_no = $row['oem_no'];
if (empty($oem_no)) $oem_no = 'n/a';
$result[] = array(
'document_id'=>(int)$row['document_id'],
'custom_document_no'=>'n/a',
'oem_document_no'=>$oem_no,
'title'=> $row['title'],
'document_type'=> $row['document_type'],
'version'=> (float) ($row['major_version'] . '.' . $row['minor_version']),
'filesize'=>(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;
}
/**
* Checks out a document
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $document->checkout('Reason for document checkout');
* if($document->is_checked_out()){
* continue;
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param string $reason The reason for checking out the document
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function checkout($reason)
{
$user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
//if the document is checked-out by the current user, just return
//as no need to check-out again BUT we do need to download
//returning here will allow download, but skip check-out
if ( ($this->document->getIsCheckedOut()) &&
($this->document->getCheckedOutUserID() == $_SESSION['userID']) )
{
return;
}
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();
}
/**
* Deletes a document from the folder.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $document->delete('Reason for deletion');
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param string $reason The reason for deleting the document
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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();
}
/**
* Changes the owner of a document and updates its permissions.
*
* @author KnowledgeTree Team
* @access public
* @param string $newusername The username of the new owner
* @param string $reason The reason for changing the owner
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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();
}
/**
* Copies the document from one folder to another.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $newFolder = $this->root->add_folder("New folder");
* $copyOfDoc = $document->copy($newFolder, 'Reason for copying document');
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param KTAPI_Folder $ktapi_target_folder The new parent folder where the document is being copied into
* @param string $reason The reason for the copy
* @param string $newname Optional. The title of the document to be used in the case of a name clash
* @param string $newfilename Optional. The filename of the document to be used in the case of a name clash
* @return KTAPI_Document|PEAR_Error Returns the new KTAPI Document object | a PEAR_Error on failure
*/
function copy(&$ktapi_target_folder, $reason, $newname=null, $newfilename=null)
{
assert(!is_null($ktapi_target_folder));
assert($ktapi_target_folder instanceof KTAPI_FOLDER); //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)
{
if (is_null($newname))
{
$name = KTDocumentUtil::getUniqueDocumentName($target_folder, $name);
}
else
{
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)
{
if (is_null($newfilename))
{
$filename = KTDocumentUtil::getUniqueFilename($target_folder, $newfilename);
}
else
{
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());
}
/**
* Moves the document from one folder to another.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $newFolder = $this->root->add_folder("New folder");
* $document->move($newFolder, 'Reason for moving the document');
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param KTAPI_Folder $ktapi_target_folder The folder object where the document is being moved into
* @param string $reason The reason for the move
* @param string $newname Optional. The title of the document to be used in the case of a name clash
* @param string $newfilename Optional. The filename of the document to be used in the case of a name clash
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function move(&$ktapi_target_folder, $reason, $newname=null, $newfilename=null)
{
assert(!is_null($ktapi_target_folder));
assert($ktapi_target_folder instanceof KTAPI_Folder); // 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();
}
/**
* Changes the filename of the document.
* If the filename contains any invalid characters they are replaced with a dash (-). For example: ?, *, %, \, /
*
* @author KnowledgeTree Team
* @access public
* @param string $newname The new filename
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function renameFile($newname)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
$newname = KTUtil::replaceInvalidCharacters($newname);
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();
}
/**
* Changes the document type of the document.
*
* @author KnowledgeTree Team
* @access public
* @param string $documenttype The new document type
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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)
{
// Get the current document type, fieldsets and metadata
$iOldDocTypeID = $this->document->getDocumentTypeID();
$fieldsets = KTMetadataUtil::fieldsetsForDocument($this->document, $iOldDocTypeID);
$mdlist = DocumentFieldLink::getByDocument($this->document);
$field_values = array();
foreach ($mdlist as $oFieldLink) {
$field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue();
}
DBUtil::startTransaction();
$this->document->startNewMetadataVersion($user);
$this->document->setDocumentTypeId($doctypeid);
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
}
// Ensure all values for fieldsets common to both document types are retained
$fs_ids = array();
$doctype_fieldsets = KTFieldSet::getForDocumentType($doctypeid);
foreach($doctype_fieldsets as $fieldset)
{
$fs_ids[] = $fieldset->getId();
}
$MDPack = array();
foreach ($fieldsets as $oFieldset)
{
if ($oFieldset->getIsGeneric() || in_array($oFieldset->getId(), $fs_ids))
{
$fields = $oFieldset->getFields();
foreach ($fields as $oField)
{
$val = isset($field_values[$oField->getId()]) ? $field_values[$oField->getId()] : '';
if (!empty($val))
{
$MDPack[] = array($oField, $val);
}
}
}
}
$core_res = KTDocumentUtil::saveMetadata($this->document, $MDPack, array('novalidate' => true));
if (PEAR::isError($core_res)) {
DBUtil::rollback();
return $core_res;
}
$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();
}
}
/**
* Changes the title of the document.
* If the title contains any invalid characters they are replaced with a dash (-). For example: ?, *, %, \, /
*
* @author KnowledgeTree Team
* @access public
* @param string $newname The new document title
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function rename($newname)
{
$user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_WRITE);
if (PEAR::isError($user))
{
return $user;
}
$newname = KTUtil::replaceInvalidCharacters($newname);
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();
}
}
/**
* Sets the status of the document to 'archived'.
*
* @author KnowledgeTree Team
* @access public
* @param string $reason The reason for archiving the document
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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();
}
}
/**
* Starts a workflow on a document.
*
* @author KnowledgeTree Team
* @access public
* @param string $workflow The workflow being applied
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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.
*
* @author KnowledgeTree Team
* @access public
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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 to a new state of the workflow on the document
*
* @author KnowledgeTree Team
* @access public
* @param string $transition The transition to perform
* @param string $reason The reason for transitioning the document to a new state
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $metadata = $document->get_metadata();
* foreach($metadata as $fieldset){
* echo '<br><br>Fieldset: '.$fieldset['fieldset'];
*
* foreach($fieldset['fields'] as $field){
* echo '<br>Field name: '.$field['name'] . ' Value: '. $field['value'];
* }
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @return array An array of metadata fieldsets and fields
*/
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 = '';
$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 == '' ? 'n/a' : $value,
'blankvalue' => $value=='' ? '1' : '0',
'description' => $field->getDescription(),
'control_type' => $controltype,
'selection' => $selection
);
}
$result['fields'] = $fieldsresult;
$results [] = $result;
}
return $results;
}
/**
* Gets a simple array of document metadata fields
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $metadata = $document->get_packed_metadata();
* foreach($metadata as $field){
* echo '<br><br>Fieldset: ' . $field[0]->getParentFieldset();
* echo '<br>Field name: ' .$field[0]->getName();
* echo ' Value: ' . $field[1];
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param array $metadata The full metadata fieldsets and fields
* @return array An array of metadata field object and value pairs
*/
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 ($fieldset_metadata instanceof stdClass) //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) || $fieldset instanceof KTEntityNoObjects) //is_a($fieldset, 'KTEntityNoObjects'))
{
$default->log->debug("could not resolve fieldset: $fieldsetname for document id: $this->documentid");
// exit graciously
continue;
}
foreach($fields as $fieldinfo)
{
if (is_array($fieldinfo))
{
$fieldname = $fieldinfo['name'];
$value = $fieldinfo['value'];
}
elseif ($fieldinfo instanceof stdClass) // 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($field) || is_a($field, 'KTEntityNoObjects'))
{
$default->log->debug("Could not resolve field: $fieldname on fieldset $fieldsetname for document id: $this->documentid");
// exit graciously
continue;
}
$packed[] = array($field, $value);
}
}
return $packed;
}
/**
* This updates the metadata on the document. This includes the 'title'.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $metadata = $document->get_metadata();
* foreach($metadata as $key => $fieldset){
* if($fieldset['fieldset'] == 'XYZ'){
*
* foreach($fieldset['fields'] as $k => $field){
* if($field['name'] == 'ABC'){
* $metadata[$key][fields][$k]['value'] = 'new value';
* }
* }
* }
* }
*
* $res = $document->update_metadata($metadata);
* </code>
*
* @author KnowledgeTree Team
* @access public
* @param array This is an array containing the metadata to be associated with the document.
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function update_metadata($metadata)
{
global $default;
if (empty($metadata))
{
return;
}
$packed = $this->get_packed_metadata($metadata);
DBUtil::startTransaction();
$user = $this->ktapi->get_user();
$this->document->setLastModifiedDate(getCurrentDateTime());
$this->document->setModifiedUserId($user->getId());
// Update the content version / document version
if($default->updateContentVersion){
$this->document->startNewContentVersion($user);
$this->document->setMinorVersionNumber($this->document->getMinorVersionNumber()+1);
}else {
$this->document->startNewMetadataVersion($user);
}
$res = $this->document->update();
if (PEAR::isError($res))
{
DBUtil::rollback();
return new KTAPI_Error('Unexpected failure updating document', $res);
}
$result = KTDocumentUtil::saveMetadata($this->document, $packed, array('novalidate'=>true));
if (is_null($result))
{
DBUtil::rollback();
return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR . ': Null result returned but not expected.');
}
if (PEAR::isError($result))
{
DBUtil::rollback();
return new KTAPI_Error('Unexpected validation failure', $result);
}
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.
*
* @author KnowledgeTree Team
* @access public
* @param array $sysdata The system metadata to be applied
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
function update_sysdata($sysdata)
{
global $default;
if (empty($sysdata))
{
return;
}
$owner_mapping = array(
'created_by'=>'creator_id',
'modified_by'=>'modified_user_id',
'owner'=>'owner_id'
);
$documents = array();
$document_content = array();
$indexContent = null;
$uniqueOemNo = false;
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(strtolower($name))
{
case 'unique_oem_document_no':
$documents['oem_no'] = $value;
$uniqueOemNo = true;
break;
case 'oem_document_no':
$documents['oem_no'] = $value;
break;
case 'index_content':
$indexContent = $value;
break;
case 'created_date':
if (!empty($value)) $documents['created'] = $value;
break;
case 'modified_date':
if (!empty($value)) $documents['modified'] = $value;
break;
case 'is_immutable':
$documents['immutable'] = in_array(strtolower($value), array('1','true','on','yes'))?'1':'0';
break;
case 'filename':
$value = KTUtil::replaceInvalidCharacters($value);
$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))
{
$default->log->error("Problem resolving mime type '$value' for document id $this->documentid. Reason: " . $value->getMessage());
return $value;
}
if (count($value) == 0)
{
$default->log->error("Problem resolving mime type '$value' for document id $this->documentid. None found.");
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))
{
$default->log->error("Problem resolving user '$value' for document id $this->documentid. Reason: " . $userId->getMessage());
return $userId;
}
if (empty($userId))
{
$sql = "select id from users where username='$value'";
$userId = DBUtil::getResultArray($sql);
if (PEAR::isError($userId))
{
$default->log->error("Problem resolving username '$value' for document id $this->documentid. Reason: " . $userId->getMessage());
return $userId;
}
}
if (empty($userId))
{
$default->log->error("Problem resolving user based on '$value' for document id $this->documentid. No user found");
// if not found, not much we can do
break;
}
$userId=$userId[0];
$userId=$userId['id'];
$name = $owner_mapping[$name];
$documents[$name] = $userId;
break;
default:
$default->log->error("Problem updating field '$name' with value '$value' for document id $this->documentid. Field is unknown.");
// 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 .= ",";
$value = sanitizeForSQL($value);
$sql .= "$name='$value'";
}
$sql .= " WHERE id=$this->documentid";
$result = DBUtil::runQuery($sql);
if (PEAR::isError($result))
{
return $result;
}
if ($uniqueOemNo)
{
$oem_no = sanitizeForSQL($documents['oem_no']);
$sql = "UPDATE documents SET oem_no=null WHERE oem_no = '$oem_no' AND id != $this->documentid";
$result = DBUtil::runQuery($sql);
}
}
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 .= ",";
$value = sanitizeForSQL($value);
$sql .= "$name='$value'";
}
$sql .= " WHERE id=$content_id";
$result = DBUtil::runQuery($sql);
if (PEAR::isError($result))
{
return $result;
}
}
if (!is_null($indexContent))
{
$indexer = Indexer::get();
$result = $indexer->diagnose();
if (empty($result))
{
$indexer->updateDocumentIndex($this->documentid, $indexContent);
}
else
{
$default->log->error("Problem updating index with value '$value' for document id $this->documentid. Problem with indexer.");
}
}
}
/**
* Clears the cached data on the document and refreshes the document object.
*
* @author KnowledgeTree Team
* @access public
*/
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);
}
/**
* Merge new metadata with previous metadata version
*
* @author KnowledgeTree Team
* @access public
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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 the workflow transitions available for the user on the document
*
* @author KnowledgeTree Team
* @access public
* @return array|PEAR_Error Array of the workflow transitions | a PEAR_Error on failure
*/
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 array();
}
$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
*
* @author KnowledgeTree Team
* @access public
* @return string Returns the name of the state | a PEAR_Error on failure
*/
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;
}
/**
* Get the available permissions on the document.
* R = read, W = write, E = edit - if the document is checked out by the user.
* The method assumes read permissions is available.
*
* @author KnowledgeTree Team
* @access public
* @param Document $document The internal document object
* @return string The available permissions
*/
function get_permission_string($document)
{
$perms = 'R';
if (Permission::userHasDocumentWritePermission($document))
{
$perms .= 'W';
$user_id = $_SESSION['userID'];
$co_user_id = $document->getCheckedOutUserID();
if (!empty($co_user_id) && ($user_id == $co_user_id))
{
$perms .= 'E';
}
}
return $perms;
}
/**
* This returns detailed information on the document.
*
* @author KnowledgeTree Team
* @access public
* @return array The document information
*/
function get_detail()
{
global $default;
// make sure we ge tthe latest
$this->clearCache();
$config = KTConfig::getSingleton();
$wsversion = $config->get('webservice/version', LATEST_WEBSERVICE_VERSION);
$detail = array();
$document = $this->document;
// get the document id
$detail['document_id'] = (int) $document->getId();
$oem_document_no = null;
if ($wsversion >= 2)
{
$oem_document_no = $document->getOemNo();
}
if (empty($oem_document_no))
{
$oem_document_no = 'n/a';
}
$detail['custom_document_no'] = 'n/a';
$detail['oem_document_no'] = $oem_document_no;
// 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();
if ($wsversion >= 2)
{
$detail['version'] = (float) $detail['version'];
}
//might be unset at the bottom in case of old webservice version
//make sure we're using the real document for this one
$this->document->switchToRealCore();
$detail['linked_document_id'] = $document->getLinkedDocumentId();
$this->document->switchToLinkedCore();
// check immutability
$detail['is_immutable'] = (bool) $document->getImmutable();
// check permissions
$detail['permissions'] = KTAPI_Document::get_permission_string($document);
// 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->document->getFullPath();
// get mime info
$mimetypeid = $document->getMimeTypeID();
$detail['mime_type'] =KTMime::getMimeTypeName($mimetypeid);
$detail['mime_icon_path'] =KTMime::getIconPath($mimetypeid);
$detail['mime_display'] =KTMime::getFriendlyNameForString($detail['mime_type']);
// get the storage path
$detail['storage_path'] = $document->getStoragePath();
if ($wsversion >= 2)
{
unset($detail['updated_by']);
unset($detail['updated_date']);
}
if($wsversion < 3){
unset($detail['linked_document_id']);
}
return $detail;
}
/**
* Gets the title of the document
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* $title = $document->get_title();
* </code>
*
* @author KnowledgeTree Team
* @access public
* @return string The document title
*/
function get_title()
{
return $this->document->getDescription();
}
/**
* Gets the url which can be used to download the document.
*
* @param int $version Not implemented. The content version of the document
*/
function get_download_url($version = null)
{
$session = $this->ktapi->get_session();
// Create the url that can be used to download the document
$download_manager = new KTDownloadManager();
$download_manager->set_session($session->session);
$download_manager->cleanup();
$url = $download_manager->allow_download($this);
// Log the transaction
$this->download();
return $url;
}
/**
* Logs the document transaction for a download.
*
* @author KnowledgeTree Team
* @access public
*/
function download()
{
$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.
*
* @author KnowledgeTree Team
* @access public
* @return array The list of transactions | a PEAR_Error on failure
*/
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 );
}
$config = KTConfig::getSingleton();
$wsversion = $config->get('webservice/version', LATEST_WEBSERVICE_VERSION);
foreach($transactions as $key=>$transaction)
{
$transactions[$key]['version'] = (float) $transaction['version'];
}
return $transactions;
}
/**
* This returns the version history on the document.
*
* @author KnowledgeTree Team
* @access public
* @return array The version history
*/
function get_version_history()
{
$metadata_versions = KTDocumentMetadataVersion::getByDocument($this->document);
$config = KTConfig::getSingleton();
$wsversion = $config->get('webservice/version', LATEST_WEBSERVICE_VERSION);
$versions = array();
foreach ($metadata_versions as $version)
{
$document = &Document::get($this->documentid, $version->getId());
$version = array();
$userid = $document->getModifiedUserId();
$user = User::get($userid);
$username = 'Unknown';
if (!PEAR::isError($user))
{
$username = is_null($user)?'n/a':$user->getName();
}
$version['user'] = $username;
$version['metadata_version'] = $document->getMetadataVersion();
$version['content_version'] = $document->getVersion();
if ($wsversion >= 2)
{
$version['metadata_version'] = (int) $version['metadata_version'];
$version['content_version'] = (float) $version['content_version'];
}
$versions[] = $version;
}
return $versions;
}
/**
* This expunges a document from the system.
*
* <code>
* $ktapi = new KTAPI();
* $session = $ktapi->start_system_session();
* $document = $ktapi->get_document_by_id($documentid);
* if($document->is_deleted()){
* $document->expunge();
* }
* </code>
*
* @author KnowledgeTree Team
* @access public
* @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
*/
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();
}
/**
* Restores a deleted document
*
* @author KnowledgeTree Team
* @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();
}
/**
* Returns the internal document object
*
* @author KnowledgeTree Team
* @access public
* @return Document The document object
*/
public function getObject()
{
return $this->document;
}
/**
* Checks if the user is subscribed to the document
*
* @author KnowledgeTree Team
* @access public
* @return bool TRUE if subscribed | FALSE if not
*/
public function isSubscribed()
{
$subscriptionType = SubscriptionEvent::subTypes('Document');
$user = $this->ktapi->get_user();
$document = $this->document;
return Subscription::exists($user->getId(), $document->getId(), $subscriptionType);
}
/**
* Removes the users subscription to the document
*
* @author KnowledgeTree Team
* @access public
*/
public function unsubscribe()
{
if (!$this->isSubscribed())
{
return;
}
$subscriptionType = SubscriptionEvent::subTypes('Document');
$user = $this->ktapi->get_user();
$document = $this->document;
$subscription = & Subscription::getByIDs($user->getId(), $document->getId(), $subscriptionType);
$subscription->delete();
}
/**
* Subscribes the user to the document
*
* @author KnowledgeTree Team
* @access public
*/
public function subscribe()
{
if ($this->isSubscribed())
{
return;
}
$subscriptionType = SubscriptionEvent::subTypes('Document');
$user = $this->ktapi->get_user();
$document = $this->document;
$subscription = new Subscription($user->getId(), $document->getId(), $subscriptionType);
$subscription->create();
}
/**
* Checks if the document is immutable
*
* @author KnowledgeTree Team
* @access public
* @return bool TRUE if it is immutable | FALSE if not
*/
public function isImmutable()
{
return $this->document->getImmutable();
}
/**
* Sets a document to be immutable
*
* @author KnowledgeTree Team
* @access public
*/
public function immute()
{
$this->document->setImmutable(true);
$this->document->update();
}
/**
* Removes the immutability of a document
*
* @author KnowledgeTree Team
* @access public
*/
public function unimmute()
{
$this->document->setImmutable(false);
$this->document->update();
}
/**
* Emails a document as an attachment or hyperlink to a list of users, groups or external email addresses.
* In the case of external addresses, if a hyperlink is used then a timed download link (via webservices) is sent allowing the recipient a window period in which to download the document.
* The period is set through the webservices config option webservice/downloadExpiry. Defaults to 30 minutes.
*
* @author KnowledgeTree Team
* @access public
* @param array $members The email recipients - KTPAI_Users, KTAPI_Groups or email addresses
* @param string $comment Content to be appended to the email
* @param bool $attachDocument TRUE if document is an attachment | FALSE if using a hyperlink to the document
*/
public function email($members, $comment, $attachDocument = true)
{
if (empty($members))
{
return;
}
$userIds = array();
$groupIds = array();
$emailAddrs = array();
foreach($members as $member)
{
if ($member instanceof KTAPI_User)
{
$userIds[] = $member->Id;
}
elseif ($member instanceof KTAPI_Group)
{
$groupIds[] = $member->Id;
}
elseif (is_string($member))
{
$emailAddrs[] = $member;
}
}
$config = KTConfig::getSingleton();
$allowAttachment = $config->get('email/allowAttachment', false);
$allowEmailAddresses = $oConfig->get('email/allowEmailAddresses', false);
$emailErrors = array();
$userEmails = array();
$listEmails = array();
sendGroupEmails($groupIds, $userEmails, $emailErrors);
sendUserEmails($userIds, $userEmails, $emailErrors);
if ($attachDocument)
{
sendManualEmails($aEmailAddresses, $userEmails, $emailErrors);
}
else
{
sendExternalEmails($aEmailAddresses, $this->document->getID(), $this->document->getName(), $comment, $emailErrors);
}
if(empty($userEmails)){
return;
}
$listEmails = array_keys($userEmails);
sendEmail($listEmails, $this->document->getID(), $this->document->getName(), $comment, (boolean)$fAttachDocument, $aEmailErrors);
}
}
?>