1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Trean_Api extends Horde_Registry_Api
18: {
19: 20: 21: 22: 23: 24: 25: 26:
27: public function getFolders($folderId)
28: {
29: if ($folderId == -1) {
30: $folder = null;
31: } else {
32: $folder = $GLOBALS['trean_shares']->getFolder($folderId);
33: }
34: if ($folder && is_a($folder, 'PEAR_Error')) {
35: return $folder;
36: }
37: $folderObs = Trean::listFolders(Horde_Perms::SHOW,
38: $folder ? $folder->getName() : null, false);
39: if (is_a($folderObs, 'PEAR_Error')) {
40: return $folderObs;
41: }
42:
43: $folders = array();
44: foreach ($folderObs as $folder) {
45: $folders[] = array('id' => $folder->getId(),
46: 'name' => $folder->get('name'));
47: }
48: return $folders;
49: }
50:
51: 52: 53: 54: 55:
56: public function addObjects($data)
57: {
58: $return_map = array();
59:
60: foreach ($data as $props) {
61: $children = null;
62:
63: if (!isset($props['folder_id']) || !isset($props['return_key'])) {
64: return new PEAR_Error('must specify folder_id and return_key properties');
65: }
66:
67: $return_key = $props['return_key'];
68: unset($props['return_key']);
69:
70: if (isset($props['name'])) {
71: $parentFolder = $GLOBALS['trean_shares']->getFolder($props['folder_id']);
72: unset($props['folder_id']);
73: if (isset($props['child_objects'])) {
74: $children = $props['child_objects'];
75: unset($props['child_objects']);
76: }
77: $ret = $parentFolder->addFolder($props);
78: } else {
79: if (!isset($props['bookmark_description'])) {
80: $props['bookmark_description'] = '';
81: }
82: $bookmark = new Trean_Bookmark($props);
83: $ret = $bookmark->save();
84: }
85:
86: if (is_a($ret, 'PEAR_Error')) {
87: return $ret;
88: }
89:
90: $return_map[$return_key] = $ret;
91:
92: if ($children) {
93: $id = $ret;
94: foreach ($children as $key => $child) {
95: $children[$key]['folder_id'] = $id;
96: }
97: $ret = addObjects($children);
98: if (is_a($ret, 'PEAR_Error')) {
99: return $ret;
100: }
101: $return_map = array_merge($return_map, $ret);
102: }
103: }
104:
105: return $return_map;
106: }
107:
108: 109: 110: 111: 112:
113: public function updateObjects($data)
114: {
115: foreach ($data as $props) {
116: if (isset($props['bookmark_id'])) {
117: $obj = $GLOBALS['trean_shares']->getBookmark($props['bookmark_id']);
118: } else if (isset($props['folder_id'])) {
119: $obj = $GLOBALS['trean_shares']->getFolder($props['folder_id']);
120: } else {
121: $obj = new PEAR_Error("each inner associative array must have a (bookmark|folder)id key");
122: }
123: if (is_a($obj, 'PEAR_Error')) {
124: return $obj;
125: }
126: foreach ($props as $name => $value) {
127: if ($name == 'id') {
128: continue;
129: }
130: if (is_a($obj, 'Trean_Bookmark')) {
131: $obj->$name = $value;
132: } else {
133: if ($name == 'folder') {
134: $ret = $GLOBALS['trean_shares']->move($obj,
135: $GLOBALS['trean_shares']->getFolder($value));
136: } else {
137: $ret = $obj->set($name, $value);
138: }
139: if (is_a($ret, 'PEAR_Error')) {
140: return $ret;
141: }
142: }
143: }
144: $obj->save();
145: }
146:
147: return true;
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161:
162: public function listBookmarks($folderId, $sortby = 'title', $sortdir = 0, $from = 0, $count = 0)
163: {
164: $folder = $GLOBALS['trean_shares']->getFolder($folderId);
165: if (is_a($folder, 'PEAR_Error')) {
166: return $folder;
167: }
168: return $folder->listBookmarks($sortby, $sortdir, $from, $count);
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178:
179: public function deleteFolder($folderId, $force)
180: {
181: $folder = $GLOBALS['trean_shares']->getFolder($folderId);
182: if (is_a($folder, 'PEAR_Error')) {
183: return $folder;
184: }
185: $result = $folder->delete();
186: if (is_a($result, 'PEAR_Error')) {
187: return $result;
188: }
189: return true;
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: public function deleteFolders($Ids)
200: {
201: $Ids = array_reverse($Ids);
202: foreach ($Ids as $Id) {
203: $ret = deleteFolder($Id, true);
204: if (is_a($ret, 'PEAR_Error')) {
205: return $ret;
206: }
207: }
208:
209: return true;
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: public function deleteBookmark($bookmarkId)
220: {
221: $result = $GLOBALS['trean_shares']->removeBookmark($bookmarkId);
222: if (is_a($result, 'PEAR_Error')) {
223: return $result;
224: }
225: return true;
226: }
227:
228: 229: 230: 231: 232: 233: 234:
235: public function deleteBookmarks($Ids)
236: {
237: foreach ($Ids as $Id) {
238: $ret = deleteBookmark($Id);
239: if (is_a($ret, 'PEAR_Error')) {
240: return $ret;
241: }
242: }
243:
244: return true;
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public function syncBookmarks($folderId, $bookmarkIds)
260: {
261: $rawbookmarks = listBookmarks($folderId);
262: if (is_a($rawbookmarks, 'PEAR_Error')) {
263: return $rawbookmarks;
264: }
265: $bookmarks = array();
266: foreach ($rawbookmarks as $bookmark) {
267: $bookmarks[$bookmark->id] = $bookmark;
268: }
269:
270:
271:
272: foreach ($bookmarkIds as $id) {
273: if (isset($bookmarks[$id])) {
274: unset($bookmarks[$id]);
275: } else {
276: $bookmarks[$id] = array('id' => $id, 'sync_deleted' => true);
277: }
278: }
279:
280:
281:
282: return array_values($bookmarks);
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296:
297: public function syncFolders($folderId, $folderIds)
298: {
299: $rawfolders = getFolders($folderId);
300: if (is_a($rawfolders, 'PEAR_Error')) {
301: return $rawfolders;
302: }
303: $folders = array();
304: foreach ($rawfolders as $folder) {
305: $folders[$folder['id']] = $folder;
306: }
307:
308:
309: foreach ($folderIds as $id) {
310: if (isset($folders[$id])) {
311: unset($folders[$id]);
312: } else {
313: $folders[$id] = array('id' => $id, 'sync_deleted' => true);
314: }
315: }
316:
317: return array_values($folders);
318: }
319:
320: 321: 322: 323: 324: 325: 326: 327: 328: 329:
330: public function getAddUrl($params = array())
331: {
332: $GLOBALS['no_compress'] = true;
333:
334: $browser = $GLOBALS['injector']->getInstance('Horde_Browser');
335: if ($browser->hasFeature('javascript')) {
336: if ($browser->hasFeature('dom')) {
337: $addurl = Horde_Util::addParameter(Horde::url('add.php', true, -1), 'iframe', 1);
338: $url = "javascript:(function(){o=document.createElement('div');o.id='overlay';o.style.background='#000';o.style.position='absolute';o.style.top=0;o.style.left=0;o.style.width='100%';o.style.height='100%';o.style.zIndex=5000;o.style.opacity=.8;document.body.appendChild(o);i=document.createElement('iframe');i.id='frame';i.style.zIndex=5001;i.style.border='thin solid #000';i.src='$addurl'+'&title=' + encodeURIComponent(document.title) + '&url=' + encodeURIComponent(location.href);i.style.position='absolute';i.style.width='350px';i.style.height='150px';i.style.left='100px';i.style.top='100px';document.body.appendChild(i);l=document.createElement('a');l.style.position='absolute';l.style.background='#ccc';l.style.color='#000';l.style.border='thin solid #000';l.style.display='block';l.style.top='250px';l.style.left='100px';l.style.zIndex=5001;l.style.padding='5px';l.appendChild(document.createTextNode('" . _("Close") . "'));l.onclick=function(){var o=document.getElementById('overlay');o.parentNode.removeChild(o);var i=document.getElementById('frame');i.parentNode.removeChild(i);this.parentNode.removeChild(this);};document.body.appendChild(l);})()";
339: } else {
340: $addurl = Horde::url(Horde_Util::addParameter('add.php', 'popup', 1), true, -1);
341: $url = "javascript:d = new Date(); w = window.open('$addurl' + '&title=' + encodeURIComponent(document.title) + '&url=' + encodeURIComponent(location.href) + '&d=' + d.getTime(), d.getTime(), 'height=200,width=400'); w.focus();";
342: }
343: } else {
344:
345: $url = Horde::url(Horde_Util::addParameter('add.php', $params), true);
346: }
347:
348: return $url;
349: }
350: }
351: