forked from hypercities/hypercities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateCollection.php
More file actions
220 lines (183 loc) · 6.46 KB
/
updateCollection.php
File metadata and controls
220 lines (183 loc) · 6.46 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
<?php
// vim: ts=4:sw=4:fdc=2:nu:nospell
/**
* @file
* Update the information of collection.
*
* @author HyperCities Tech Team
* @copyright Copyright 2008-2009, The Regents of the University of California
* @date 2009-04-30
* @version $Id$
*
*/
include_once('includes/dbUtil.inc');
include_once('includes/util.inc');
include_once("includes/kmlParser.inc");
include_once("includes/database.inc");
/**
* Update coauthors
* @param {Number} $object
* @param {Array} $coauthors
*/
function updateCoauthor($objectId, $coauthors) {
$db = database::getInstance();
$query = "SELECT user_id FROM objects_users WHERE object_id = ?";
$db->preparedQuery($query, array('type' => 'i', &$objectId));
$result = $db->preparedGetRows();
$oldCoauthors = array();
if (count($result) > 0) {
foreach ($result as $row) {
$oldCoauthors[] = $row['user_id'];
}
}
$coauthorsToDelete = array_diff($oldCoauthors, $coauthors);
$coauthorsToAdd = array_diff($coauthors, $oldCoauthors);
if (count($coauthorsToDelete) > 0) {
$query = "DELETE FROM objects_users WHERE object_id = $objectId AND";
if (count($coauthorsToDelete) >= 1) {
$query .= " user_id IN (".implode(',', $coauthorsToDelete).")";
}
$db->query($query);
}
if (count($coauthorsToAdd) > 0) {
$query = 'INSERT INTO objects_users (object_id, user_id, access_right_id,'
. 'created_at, updated_at) VALUES (?, ?, ?, NOW(), NOW())';
foreach ($coauthorsToAdd as $coauthor) {
$coauthor = (int)$coauthor;
$accessRight = 7;
$params = array('type' => 'iii', &$objectId, &$coauthor, &$accessRight);
$db->preparedQuery($query, $params);
}
}
}
// Test cases for php command line interface
if (!empty($argc) && strstr($argv[0], basename(__FILE__))) {
$_POST['objectId'] = 15630;
$_POST['creator'] = "jay";
$_POST['title'] = "Jay's world";
$_POST['copyright'] = "CCBY";
$_POST['content'] = "test update collection privilege";
$_POST['parents'] = '13765';
$_POST['state'] = HC_OBJECT_PRIVATE;
// Following fields are not yet implemented
// data will not received from the client
$_POST['password'] = "123";
$_POST['userIdArray'] = "19,17";
$_POST['accessRightIdArray'] = "7,5";
} else { // Start regular setup
// Check HTTP Referee
HC_checkReferer();
// Start session
include_once("includes/serverSession.inc");
cServerSession::start();
// Get session variables
$userId = cServerSession::getUserId();
$isAdmin = cServerSession::isAdmin();
}
$HC_POST = HC_cleanInput($_POST, array('objectId' => 'int',
'title' => 'str',
'state' => 'int',
'password' => 'str',
'content' => 'str',
'creator' => 'str',
'copyright' => 'str',
'parents' => 'str',
'view' => 'str',
'coauthors' => 'str',
'shareAll' => 'int',
'userIdArray' => 'str',
'accessRightIdArray' => 'str'),
array('objectId',
'title',
'state'),
array('content',
'creator',
'password',
'copyright',
'parents',
'view',
'coauthors',
'userIdArray',
'accessRightIdArray'));
$collectionId = $HC_POST['objectId'];
$parents = explode(',', $HC_POST['parents']);
$oldParents = array();
$coauthors = (empty($HC_POST['coauthors'])) ? array() : explode(',', $HC_POST['coauthors']);
$userId = cServerSession::getUserId();
$object['title'] = $HC_POST['title'];
$object['object_state_id'] = $HC_POST['state'];
$object['description'] = $HC_POST['content'];
$object['creator'] = $HC_POST['creator'];
$object['password'] = trim($HC_POST['password']);
$object['password'] = (empty($HC_POST['password'])) ? NULL : md5($HC_POST['password']);
$object['copyright'] = $HC_POST['copyright'];
try {
if (empty($HC_POST['parents'])) {
throw new Exception("Please select at least one collection to which"
." this collection will be added.");
}
if (checkCircular($collectionId, $parents)) {
throw new Exception("Circular reference occurs.");
}
$result = update("objects", $object, "`id` = $collectionId");
//update object view
if (!empty($HC_POST['view'])) {
$view = json_decode($HC_POST['view'], true);
$view = KmlParser::createViewFromArray($view);
$object = array("view" => $view);
$result = update("object_mappings", $object, "`object_id` = $collectionId");
}
//select the old parents
$result = getParents($collectionId);
foreach ($result as $row) {
$oldParents[] = $row['object_id'];
}
$deletedCollections = array_diff($oldParents, $parents);
$addedCollections = array_diff($parents, $oldParents);
//update object_relation table
foreach ($deletedCollections as $deletedParentId) {
$result = delete("object_relations", "`object_id` = $deletedParentId"
." and `subject_id`=$collectionId");
}
foreach ($addedCollections as $addedParentId) {
$object = array('object_id' => $addedParentId,
'subject_id' => $collectionId,
'scope_id' => $addedParentId,
'owner_id' => $userId,
'created_at' => 'NOW()',
'updated_at' => 'NOW()');
$result = insert("object_relations", $object);
$object = updateColTimeBoundBottomUp($addedParentId);
HC_debugLog("boundary and timespan = ".print_r($object, true));
}
// Update access right not implemented yet
/*
if (!empty($accessRightArray)) {
updatePrivilege($collectionId, $accessRightArray);
}
*/
updateCoauthor($collectionId, $coauthors);
if ($HC_POST['shareAll']) {
// TODO: recursively share all objects belonging to the user in the collection
// TODO: check the privilege for each dhild
$query = "SELECT ore.subject_id FROM (object_relations AS ore, objects AS o)"
. " LEFT JOIN objects_users AS ou ON ou.object_id = ore.subject_id"
. " WHERE (o.owner_id = $userId OR ou.user_id = $userId) AND o.id = ore.subject_id"
. " AND ore.object_id = $collectionId";
$children = sqlCommand($query);
foreach ($children as $child) {
$objectId = $child['subject_id'];
updateCoauthor($objectId, $coauthors);
}
}
HC_reportSuccess("Collection updated successfully!");
} catch (MysqlException $e) {
$message = 'Caught exception: '.$e->getMessage();
HC_errorLog($message);
HC_reportDBError("updating the collection");
} catch (Exception $e) {
$message = 'Caught exception: '.$e->getMessage();
HC_errorLog($message);
HC_reportGeneralError("updating the collection");
}
?>