forked from TIU11/Basic-CMIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-cmis.php
218 lines (199 loc) · 6.65 KB
/
basic-cmis.php
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
<?php
/*
Plugin Name: Basic CMIS for Wordpress
Plugin URI: https://github.com/TIU11/Basic-CMIS
Description: Wordpress Plugin for basic CMIS integration. Searches a CMIS compliant system and renders the matching documents. Tested with Alfresco Enterprise's CMIS service.
Version: 0.0.3-alpha
Author: Anson Hoyt
Author URI: https://github.com/ansonhoyt
*/
// Required PHP files
$PLUGIN_PATH = dirname(__FILE__).'/';
require_once($PLUGIN_PATH . '/admin.php');
require_once ($PLUGIN_PATH . '/lib/cmis_repository_wrapper.php');
require_once ($PLUGIN_PATH . '/stream.php');
/*
Define CMIS shortcode
Usage:
[cmis folder="/my particular/folder name/"] # Docs in the folder
[cmis tree="/my particular/folder name/"] # Docs in the folder, including subfolders
[cmis keywords="coffee tea"] # Docs containing the keywords
[cmis name="Agenda%.doc"] # Docs whose name matches. May include wildcard character '%'.
*/
function cmis_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array(
'folder' => '',
'tree' => '',
'keywords' => '',
'name' => '',
), $attr ) );
return do_cmis($folder, $tree, $keywords, $name);
}
add_shortcode('cmis', 'cmis_shortcode');
/*
Perform specified retrieve and return rendered table of documents.
*/
function do_cmis($folder, $tree, $keywords, $name) {
// Initialize CMIS Client
$repo_url = get_option('cmis_repository_url');
$repo_username = get_option('cmis_username');
$repo_password = get_option('cmis_password');
$client = new CMISService($repo_url, $repo_username, $repo_password);
$query_conditions = array();
$error_message = 'Error. Unable to show documents.';
try {
if ($folder) {
$f = $client->getObjectByPath($folder);
array_push($query_conditions, "IN_FOLDER('$f->id')");
}
elseif ($tree) {
$f = $client->getObjectByPath($tree);
array_push($query_conditions, "IN_TREE(d,'$f->id')");
}
if ($keywords) {
array_push($query_conditions, "CONTAINS('$keywords')");
}
if ($name) {
array_push($query_conditions, "d.cmis:name LIKE '$name'");
}
// Perform query
if(sizeof($query_conditions)) {
$query = "SELECT d.*, o.* FROM cmis:document AS d JOIN cm:titled AS o ON d.cmis:objectId = o.cmis:objectId WHERE " . join(" AND ", $query_conditions) . "ORDER BY d.cmis:name";
//$query = "SELECT * FROM cmis:document WHERE " . join(" AND ", $query_conditions);
$objs = $client->query($query);
return display_cmis_objects($objs);
}
}
catch (CmisObjectNotFoundException $e) {
$error_message = "No documents found.";
}
catch (CmisRuntimeException $e) {
$error_message = "Error. Unexpected problem when retrieving documents.";
}
return "<div class=\"alert\">$error_message</div>";
}
function display_cmis_objects($objs) {
ob_start();
?>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<?php
//var_dump($objs);
foreach ($objs->objectList as $obj) {
display_cmis_object($obj);
}
?>
</tbody>
</table>
<?php
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
function display_cmis_object($obj) {
$name = $obj->properties['cmis:name'];
$title = $obj->properties['cm:title'];
?>
<tr>
<td>
<a href="<?php echo get_object_url($obj) ?>">
<?php echo get_object_icon($obj); ?>
<?php echo $name ?>
</a>
</td>
<td>
<?php echo $title; ?>
</td>
<td>
<?php
$date_string = $obj->properties['cmis:lastModificationDate'];
$timestamp = strtotime($date_string);
#$timestamp = DateTime::createFromFormat('Y-m-dThh:mm:ss:mmmT', $date_string);
echo date( "M d Y h:i a", $timestamp);
?>
</td>
</tr>
<?php
}
function get_object_icon($obj) {
$name = $obj->properties['cmis:name'];
$mimeType = $obj->properties['cmis:contentStreamMimeType'];
$docType = $obj->properties['cmis:baseTypeId'];
$icon = '_default.gif';
if($docType == 'cmis:folder') {
$icon = 'folder.gif';
} else {
// Determine icon image based on Mime Type
switch($mimeType) {
case 'image/bmp':
$icon = 'bmp.gif';
break;
case 'image/jpeg':
$icon = 'jpg.gif';
break;
case 'application/x-shockwave-flash':
$icon = 'swf.gif';
break;
case 'text/html':
$icon = 'htm.gif';
break;
case 'application/zip':
$icon = 'zip.gif';
break;
case 'video/x-ms-wmv':
case 'video/x-msvideo':
$icon = 'wmv.gif';
break;
case 'application/msword':
$icon = 'doc.gif';
break;
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
$icon = 'pptx.gif';
break;
case 'application/vnd.powerpoint':
case 'application/vnd.ms-powerpoint':
$icon = 'ppt.gif';
break;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
$icon = 'docx.gif';
break;
case 'audio/x-mpeg':
$icon = 'mp3.gif';
break;
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
$icon = 'xls.gif';
break;
case 'application/x-javascript':
$icon = 'js.gif';
break;
case 'application/pdf':
$icon = 'pdf.gif';
break;
case 'application/acp':
$icon = 'acp.gif';
break;
case 'text/plain':
$icon = 'txt.gif';
break;
default:
$icon = '_default.gif';
}
}
return <<<EOD
<img src="/wp-content/plugins/basic-cmis/img/$icon" alt="$name" title="$name">
EOD;
}
function get_object_url($obj) {
$siteurl = get_option('siteurl');
$id = $obj->properties['cmis:objectId'];
return $siteurl . '?cmis_obj_id=' . urlencode($id);
}
?>