1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: class Agora_Api extends Horde_Registry_Api
19: {
20: 21: 22: 23: 24: 25: 26: 27: 28:
29: public function listForums($forum_id = 0, $scope = null)
30: {
31: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
32: return $forums->getForums($forum_id, true, 'forum_name', 0, isset($scope));
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: public function getForumName($scope, $forum_id)
44: {
45: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
46: $forum = $forums->getForum($forum_id);
47: if ($forum instanceof PEAR_Error) {
48: return $forum;
49: }
50:
51: return $forum['forum_name'];
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
64: public function saveForum($scope, $parent, $info)
65: {
66: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
67: $forum_info = $this->prepareFormData($scope, $parent, $info);
68: if ($forum_info instanceof PEAR_Error) {
69: return $forum_info;
70: }
71:
72: return $forums->saveForum($forum_info);
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83:
84: public function deleteForum($scope, $forum_name)
85: {
86: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
87: $id = $forums->getForumId($forum_name);
88: if ($id instanceof PEAR_Error) {
89: Horde::logMessage($id, 'ERR');
90: return false;
91: }
92:
93: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $id);
94: $result = $forums->deleteForum($id);
95: if ($result instanceof PEAR_Error) {
96: Horde::logMessage($result, 'ERR');
97: return false;
98: }
99: return true;
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119:
120: public function getThreads($forum_name, $sort_by = 'message_timestamp', $sort_dir = 0, $bodies = false,
121: $scope = 'agora', $base_url = null, $from = 0, $count = 0)
122: {
123: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
124: if (empty($forum_name)) {
125: return $forums->getThreads(0, false, $sort_by, $sort_dir, true, '', $base_url, $from, $count);
126: } elseif (($forum_id = $forums->getForumId($forum_name)) instanceof PEAR_Error) {
127: return $forum_id;
128: } elseif (empty($forum_id)) {
129: return array();
130: }
131:
132: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $forum_id);
133: if ($messages instanceof PEAR_Error) {
134: return $messages;
135: }
136:
137: return $messages->getThreads(0, true, $sort_by, $sort_dir, true, '', $base_url, $from, $count);
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157:
158: public function getThreadsBatch($forum_names, $sort_by = 'message_timestamp', $sort_dir = 0, $bodies = false,
159: $scope = 'agora', $base_url = null, $from = 0, $count = 0)
160: {
161: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
162: $results = array();
163: foreach ($forum_names as $forum) {
164: $forum_id = $forums->getForumId($forum);
165: if ($forum_id instanceof PEAR_Error || empty($forum_id)) {
166: $results[$forum] = array();
167: } else {
168: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $forum_id);
169: if ($messages instanceof PEAR_Error) {
170: return $messages;
171: }
172: $results[$forum] = $messages->getThreads(0, true, $sort_by, $sort_dir, true, '', $base_url, $from, $count);
173: }
174: }
175: return $results;
176: }
177:
178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193:
194: public function getThreadsByForumOwner($owner, $sort_by = 'message_timestamp', $sort_dir = 0, $bodies = false,
195: $scope = 'agora', $from = 0, $count = 0)
196: {
197: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
198:
199: return $forums->getThreadsByForumOwner($owner, 0, true, $sort_by, $sort_dir, true, $from, $count);
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
213: public function numMessages($forum_name, $scope = 'agora', $thread_id = null)
214: {
215: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
216:
217: if (($forum_id = $forums->getForumId($forum_name)) instanceof PEAR_Error) {
218: return $forum_id;
219: } elseif (empty($forum_id)) {
220: return 0;
221: }
222:
223: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $forum_id);
224: if (is_a($messages, 'PEAR_Error')) {
225: return $messages;
226: }
227: return ($thread_id === null) ? $messages->_forum['message_count'] : $messages->countThreads($thread_id);
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242:
243: public function numMessagesBatch($forum_name, $scope = 'agora', $thread_id = null)
244: {
245: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
246: if ($forums instanceof PEAR_Error) {
247: return $forums;
248: }
249:
250: $results = array();
251: foreach ($forum_name as $forum) {
252: if (($forum_id = $forums->getForumId($forum)) instanceof PEAR_Error) {
253:
254:
255: Horde::logMessage($forum_id, 'ERR');
256: $results[$forum] = 0;
257: } elseif (empty($forum_id)) {
258: $results[$forum] = 0;
259: } else {
260: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $forum_id);
261: if ($messages instanceof PEAR_Error) {
262: return $messages;
263: }
264: $results[$forum] = ($thread_id === null) ? $messages->_forum['message_count'] : $messages->countThreads($thread_id);
265: }
266: }
267: return $results;
268: }
269:
270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281:
282: public function renderThreads($forum_name, $bodies = false, $scope = 'agora', $base_url = null, $template_file = false)
283: {
284: 285:
286: if ($base_url) {
287: $base_url = Horde_Util::removeParameter($base_url, array('agora', 'message_parent_id', 'delete'));
288: }
289:
290: $threads = $this->getThreads($forum_name, 'message_thread', 0, $bodies, $scope, $base_url);
291: if (!count($threads)) {
292: return '';
293: }
294:
295: $col_headers = array(
296: 'message_thread' => _("Subject"),
297: 'message_thread_class_plain' => 'msgThreadPlain',
298: 'message_author' => _("Posted by"),
299: 'message_author_class_plain' => 'msgAuthorPlain',
300: 'message_timestamp' => _("Date"),
301: 'message_timestamp_class_plain' => 'msgTimestampPlain'
302: );
303:
304: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
305: $forum_id = $forums->getForumId($forum_name);
306:
307: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $forum_id);
308: if ($messages instanceof PEAR_Error) {
309: return $messages;
310: }
311: return '<h1 class="header">' . _("Comments") . '</h1>' .
312: $messages->getThreadsUi($threads, $col_headers, $bodies, $template_file);
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: public function addMessage($scope, $forum_name, $callback, $params = array())
344: {
345: global $registry;
346:
347:
348: $check = $registry->callByPackage($scope, $callback, array($forum_name));
349: if ($check instanceof PEAR_Error || !$check) {
350: return '';
351: }
352:
353:
354: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
355: if (($params['forum_id'] = $forums->getForumId($forum_name)) instanceof PEAR_Error) {
356: return $params['forum_id'];
357: } elseif (empty($params['forum_id'])) {
358: $forum_info = $this->prepareFormData($scope, false, array('forum_name' => $forum_name), $callback);
359: if ($forum_info instanceof PEAR_Error) {
360: return $forum_info;
361: }
362: $params['forum_id'] = $forums->saveForum($forum_info);
363: if ($params['forum_id'] instanceof PEAR_Error) {
364: return $params['forum_id'];
365: }
366: }
367:
368:
369: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $params['forum_id']);
370: if ($messages instanceof PEAR_Error) {
371: return $messages;
372: }
373:
374: return $messages->saveMessage($params);
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: public function postMessage($scope, $forum_name, $callback, $params = array(),
409: $url = null, $variables = null)
410: {
411: global $registry;
412:
413:
414: $check = $registry->callByPackage($scope, $callback, array($forum_name));
415: if ($check instanceof PEAR_Error || !$check) {
416: return '';
417: }
418:
419:
420: $queue = Horde_Notification::singleton('agoraPostMessage');
421: $queue->attach('status');
422:
423:
424: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
425:
426:
427: $vars = Horde_Variables::getDefaultVariables();
428: if (is_array($variables)) {
429: foreach ($variables as $varname => $value) {
430: $vars->add($varname, $value);
431: }
432: }
433: $formname = $vars->get('formname');
434:
435:
436: $params['forum_id'] = $forums->getForumId($forum_name);
437: if ($params['forum_id'] === null) {
438: $vars->set('new_forum', $forum_name);
439: } else {
440: $vars->set('forum_id', $params['forum_id']);
441: }
442:
443:
444: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $params['forum_id']);
445: if ($messages instanceof PEAR_Error) {
446: $queue->push(_("Could not post the message: ") . $messages->getMessage(), 'horde.error');
447:
448: Horde::startBuffer();
449: $queue->notify(array('listeners' => 'status'));
450: return Horde::endBuffer();
451: }
452:
453:
454: if (!$messages->hasPermission(Horde_Perms::EDIT)) {
455: Horde::permissionDeniedError('agora', null);
456: return PEAR::raiseError(sprintf(_("You don't have permission to post messages in forum %s."), $params['forum_id']));
457: }
458:
459: if (isset($params['message_id'])) {
460: $message = $messages->getMessage($params['message_id']);
461: if (!$formname) {
462: $vars = new Horde_Variables($message);
463: $vars->set('message_subject', $message['message_subject']);
464: $vars->set('message_body', $message['body']);
465: }
466: $editing = true;
467: } else {
468: $editing = false;
469: $params['message_id'] = null;
470: }
471:
472:
473: if (!isset($params['title'])) {
474: $params['title'] = ($editing) ? _("Edit Message") : _("Post a New Message");
475: }
476:
477:
478: $form = $messages->getForm($vars, $params['title'], $editing, is_null($params['forum_id']));
479:
480:
481: if ($form->validate($vars)) {
482: $form->getInfo($vars, $info);
483:
484: if (isset($info['new_forum'])) {
485: $forum_info = $this->prepareFormData($scope, false, array('forum_name' => $info['new_forum']), $callback);
486: if ($forum_info instanceof PEAR_Error) {
487: return $forum_info;
488: }
489: $info['forum_id'] = $m_params['forum_id'] = $forums->saveForum($forum_info);
490: $result = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $info['forum_id']);
491: if ($result instanceof PEAR_Error) {
492: return $result;
493: }
494: }
495:
496:
497: $message_id = $messages->saveMessage($info);
498: if ($message_id instanceof PEAR_Error) {
499: $queue->push(_("Could not post the message: ") . $message_id->getMessage(), 'horde.error');
500: } else {
501: $queue->push(_("Message posted."), 'horde.success');
502: $count = $messages->countMessages();
503: $registry->callByPackage($scope, $callback, array($forum_name, 'messages', $count));
504:
505: Horde::startBuffer();
506: $queue->notify(array('listeners' => 'status'));
507: return Horde::endBuffer();
508: }
509: }
510:
511:
512: if (isset($params['message_parent_id']) && !$form->isSubmitted()) {
513: $message = $messages->replyMessage($params['message_parent_id']);
514: if (!($message instanceof PEAR_Error)) {
515: $vars->set('message_parent_id', $params['message_parent_id']);
516: $vars->set('message_subject', $message['message_subject']);
517: $vars->set('message_body', $message['body']);
518: } else {
519:
520: $vars->set('message_parent_id', '');
521: }
522: }
523:
524: if (!$url) {
525: $url = Horde::selfUrl(true, false, true);
526: }
527:
528: Horde::startBuffer();
529: $form->renderActive(null, $vars, $url, 'post', null, false);
530: return Horde::endBuffer();
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: public function removeMessage($scope, $forum_name, $callback, $params = array(),
559: $variables = null)
560: {
561: global $registry;
562:
563:
564: $check = $registry->callByPackage($scope, $callback, array($forum_name));
565: if ($check instanceof PEAR_Error || !$check) {
566: return '';
567: }
568:
569:
570: $queue = Horde_Notification::singleton('agoraRemoveMessage');
571: $queue->attach('status');
572:
573:
574: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
575: $params['forum_id'] = $forums->getForumId($forum_name);
576: if (empty($params['forum_id'])) {
577: return PEAR::raiseError(sprintf(_("Forum %s does not exist."), $forum_name));
578: }
579:
580:
581: $messages = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope, $params['forum_id']);
582: if ($messages instanceof PEAR_Error) {
583: PEAR::raiseError(sprintf(_("Could not delete the message. %s"), $messages->getMessage()));
584: }
585:
586:
587: if (!$messages->hasPermission(Horde_Perms::DELETE)) {
588: return PEAR::raiseError(sprintf(_("You don't have permission to delete messages in forum %s."), $params['forum_id']));
589: }
590:
591:
592: $message = $messages->getMessage($params['message_id']);
593: if ($message instanceof PEAR_Error) {
594: return PEAR::raiseError(sprintf(_("Could not delete the message. %s"), $message->getMessage()));
595: }
596:
597:
598: $vars = new Horde_Variables($variables);
599: $form = new Horde_Form($vars, sprintf(_("Delete \"%s\" and all replies?"), $message['message_subject']), 'delete_agora_message');
600: $form->setButtons(array(_("Delete"), _("Cancel")));
601: $form->addHidden('', 'forum_id', 'int', true);
602: $form->addHidden('', 'message_id', 'int', true);
603:
604: if ($form->validate()) {
605: if ($vars->get('submitbutton') == _("Delete")) {
606: $result = $messages->deleteMessage($params['message_id']);
607: if ($result instanceof PEAR_Error) {
608: $queue->push(sprintf(_("Could not delete the message. %s"), $result->getMessage()), 'horde.error');
609: } else {
610: $queue->push(_("Message deleted."), 'horde.success');
611: $count = $messages->countMessages();
612: $registry->callByPackage($scope, $callback, array($forum_name, 'messages', $count));
613: }
614: } else {
615: $queue->push(_("Message not deleted."), 'horde.message');
616: }
617:
618: Horde::startBuffer();
619: $queue->notify(array('listeners' => 'status'));
620: return Horde::endBuffer();
621: }
622:
623: Horde::startBuffer();
624: $form->renderActive(null, null, null, 'post', null, false);
625: return Horde::endBuffer();
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: public function ($scope, $key, $callback, $bodies = true,
660: $base_url = null, $url = null, $variables = null,
661: $template_file = false)
662: {
663: if (is_null($base_url)) {
664: $base_url = Horde::selfUrl(true);
665: }
666:
667: list($forum_id, $message_id) = Agora::getAgoraId();
668:
669: $params = array();
670: if ($message_id) {
671: $params['message_id'] = $message_id;
672: }
673:
674: if ($parent = Horde_Util::getFormData('message_parent_id')) {
675: $params['message_parent_id'] = $parent;
676: }
677:
678:
679: if (isset($params['message_id'])) {
680: $params['title'] = _("Edit a comment");
681: } else {
682: $params['title'] = _("Add a comment");
683: $params['message_id'] = null;
684: }
685:
686: if (Horde_Util::getFormData('delete') === null) {
687: $comments = $this->postMessage($scope, $key, $callback, $params, $url, $variables);
688: } else {
689: $comments = $this->removeMessage($scope, $key, $callback, $params, $url, $variables);
690: }
691:
692: if ($comments instanceof PEAR_Error) {
693: return $comments;
694: }
695:
696: include AGORA_BASE . '/lib/Comments.php';
697: $threads = Agora_ViewComments::render($key, $scope, $base_url, $template_file);
698:
699: if ($threads instanceof PEAR_Error) {
700: $threads = $threads->getMessage();
701: }
702: if ($comments instanceof PEAR_Error) {
703: $comments = $comments->getMessage();
704: }
705:
706: return array('threads' => $threads, 'comments' => $comments);
707: }
708:
709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723:
724: public function prepareFormData($scope, $parent = false, $info = array(), $callback = null)
725: {
726: $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
727:
728: if ($parent) {
729: $parent_id = $forums->getForumId($parent);
730: $parent_form = $forums->getForum($parent_id);
731: $info['forum_parent_id'] = $parent_id;
732: if (!isset($info['forum_moderated'])) {
733: $info['forum_moderated'] = $parent_form->isModerated();
734: }
735: if (!isset($info['forum_attachments'])) {
736: $info['forum_attachments'] = $parent_form->forum->data['forum_attachments'];
737: }
738: } elseif (isset($info['forum_name'])) {
739: $forum_id = $forums->getForumId($info['forum_name']);
740: if (!empty($forum_id)) {
741: $forum = $forums->getForum($forum_id);
742: $info = array_merge($forum, $info);
743: }
744: }
745:
746: if (!isset($info['forum_parent_id'])) {
747: $info['forum_parent_id'] = 0;
748: }
749:
750: if (!isset($info['forum_attachments'])) {
751: $info['forum_attachments'] = ($GLOBALS['conf']['forums']['enable_attachments'] == '-1') ? false : true;
752: }
753:
754: if (!isset($info['forum_moderated'])) {
755: $info['forum_moderated'] = false;
756: }
757:
758: if (!isset($info['forum_description'])) {
759: $info['forum_description'] = '';
760: }
761:
762: if (!isset($info['author'])) {
763: $info['author'] = '';
764: }
765:
766: if ($callback) {
767:
768: if (empty($info['author'])) {
769: $info['author'] = $GLOBALS['registry']->callByPackage($scope, $callback, array($info['forum_name'], 'owner'));
770: if ($info['author'] instanceof PEAR_Error) {
771: return $info['author'];
772: }
773: }
774:
775:
776: if (empty($info['forum_description'])) {
777: $info['forum_description'] = $GLOBALS['registry']->callByPackage($scope, $callback, array($info['forum_name']));
778: if ($info['forum_description'] instanceof PEAR_Error) {
779: return $info['forum_description'];
780: }
781: }
782: }
783:
784: return $info;
785: }
786:
787: 788: 789: 790: 791:
792: public function moderateForm($scope)
793: {
794: global $notification, $prefs, $registry;
795:
796: $api_call = true;
797:
798: return require AGORA_BASE . '/moderate.php';
799: }
800: }
801: