Overview

Packages

  • Agora
  • None

Classes

  • Agora
  • Agora_Api
  • Agora_Driver
  • Agora_Driver_SplitSql
  • Agora_Driver_Sql
  • Agora_Exception
  • Agora_Factory_Driver
  • Agora_Form_Forum
  • Agora_Form_Message
  • Agora_Form_Search
  • Agora_View
  • Horde_Form_Renderer_MessageForm
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Agora external API interface.
  4:  *
  5:  * This file defines Agora's external API interface. Other
  6:  * applications can interact with Agora through this API.
  7:  *
  8:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  9:  *
 10:  * See the enclosed file COPYING for license information (GPL). If you
 11:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 12:  *
 13:  * @author  Marko Djukic <marko@oblo.com>
 14:  * @author  Duck <duck@obala.net>
 15:  * @package Agora
 16:  */
 17: 
 18: class Agora_Api extends Horde_Registry_Api
 19: {
 20:     /**
 21:      * Get back a list of available forums.
 22:      *
 23:      * @param integer $forum_id  Supplying this parameter will return a list of
 24:      *                           child forums of the requested forum id.
 25:      * @param string $scope      If set, limit the forums to requested application.
 26:      *
 27:      * @return array  The list of available forums.
 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:      * Retrieve the name of a forum
 37:      *
 38:      * @param string $scope      Scope which form belongs to
 39:      * @param integer $forum_id  The forum id to fetch the name for.
 40:      *
 41:      * @return mixed The forum name | Pear_Error
 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:      * Create or modify an agora forum. This is used for apps to create
 56:      * forums for their own use. They will not show up in the regular
 57:      * agora forum view since they will be using a datatree group
 58:      * 'agora.forums.<sope>'.
 59:      *
 60:      * @param string $scope   The Horde application that is saving this forum.
 61:      * @param string $parent  The parent forum.
 62:      * @param array  $info    The forum information to save
 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:      * Allow other applications to delete forums. Used when an object that
 77:      * has been commented on has been deleted.
 78:      *
 79:      * @param string $scope       The Horde application that the forum belongs to.
 80:      * @param string $forum_name  The unique forum name to delete.
 81:      *
 82:      * @return boolean True on success.
 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:      * Returns all messages of a forum, in a threaded order.
104:      *
105:      * @param string  $forum_name  The unique name for the forum.
106:      * @param boolean $bodies      Whether to include message bodies in the view.
107:      * @param string  $sort_by     Return messages sorted by this property.
108:      * @param integer $sort_dir    The direction by which to sort:
109:      *                                0 - ascending
110:      *                                1 - descending
111:      * @param string  $scope       The application that the specified forum belongs
112:      *                             to.
113:      * @param string  $base_url    An alternate link where edit/delete/reply links
114:      *                             point to.
115:      * @param string  $from        The thread to begin listing at.
116:      * @param string  $count       The number of threads to return.
117:      *
118:      * @return array  All messages of the specified forum.
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:      * Returns all messages for the forums requested, in a threaded order.
142:      *
143:      * @param array   $forum_names  An array of unique forum names.
144:      * @param boolean $bodies       Whether to include message bodies in the view.
145:      * @param string  $sort_by      Return messages sorted by this property.
146:      * @param integer $sort_dir     The direction by which to sort:
147:      *                                0 - ascending
148:      *                                1 - descending
149:      * @param string  $scope        The application that the specified forum belongs
150:      *                              to.
151:      * @param string  $base_url     An alternate link where edit/delete/reply links
152:      *                              point to.
153:      * @param string  $from         The thread to begin listing at.
154:      * @param string  $count        The number of threads to return.
155:      *
156:      * @return array  An array of message arrays of the specified forum.
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:      * Returns all messages of a forum, in a threaded order.
180:      *
181:      * @param string  $forum_owner   Forum owner
182:      * @param boolean $bodies      Whether to include message bodies in the view.
183:      * @param string  $sort_by     Return messages sorted by this property.
184:      * @param integer $sort_dir    The direction by which to sort:
185:      *                                0 - ascending
186:      *                                1 - descending
187:      * @param string  $scope       The application that the specified forum belongs
188:      *                             to.
189:      * @param string  $from        The thread to begin listing at.
190:      * @param string  $count       The number of threads to return.
191:      *
192:      * @return array  All messages of the specified forum.
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:      * Returns the number of messages in a forum.
204:      *
205:      * @param string  $forum_name  The unique name for the forum.
206:      * @param string  $scope       The application that the specified forum
207:      *                             belongs to.
208:      * @param int     $thread_id   The thread to count, if not supplied it
209:      *                             will count all messages
210:      *
211:      * @return int  The number of messages in the specified forum.
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:      * Returns the number of messages for the requested forums.
232:      * All requested forums must belong to the same scope.
233:      *
234:      * @param array   $forum_name  An array of unique forum names.
235:      * @param string  $scope       The application that the specified forum
236:      *                             belongs to.
237:      * @param int     $thread_id   The thread to count, if not supplied it
238:      *                             will count all messages
239:      *
240:      * @return mixed  An array containing the message counts with the forum name as
241:      *                the key | PEAR_Error
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:                 // In case of error, just return zero but log the error - so
254:                 // the calling app always gets an array with all the image ids.
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:      * Returns all threads of a forum in a threaded view.
272:      *
273:      * @param string  $forum_name     The unique name for the forum.
274:      * @param boolean $bodies         Whether to include message bodies in the view.
275:      * @param string  $scope          The application that the specified forum belongs to.
276:      * @param string  $base_url       An alternate link where edit/delete/reply links
277:      *                                point to.
278:      * @param string  $template_file  Template file to use.
279:      *
280:      * @return string  The HTML code of the thread view.
281:      */
282:     public function renderThreads($forum_name, $bodies = false, $scope = 'agora', $base_url = null, $template_file = false)
283:     {
284:         /* An agora parameter may already be present. If so it would
285:          * interfere; remove it. */
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:      * Allows other Horde apps to add/edit messages.
318:      *
319:      * The forum name is constructed by just the $forum_name variable
320:      * under the data root 'agora.forums.<app>'. It is up to the apps
321:      * themselves to make sure that the forum name is unique.
322:      *
323:      * If the forum does not exist, it will be automatically created by
324:      * Agora.
325:      *
326:      * @access private
327:      *
328:      * @param string $scope       The application which is posting this message.
329:      * @param string $forum_name  The unique name for the forum.
330:      * @param string $callback    A callback method of the specified application
331:      *                            that gets called to make sure that posting to
332:      *                            this forum is allowed.
333:      * @param array $params       Any parameters for the forum message posting.
334:      * <pre>
335:      * message_id        - An existing message to edit
336:      * message_parent_id - The ID of the parent message
337:      * message_body      - Message body
338:      * </pre>
339:      *
340:      * @return mixed  Returns message id if the message was posted
341:      *                or PEAR_Error object on error
342:      */
343:     public function addMessage($scope, $forum_name, $callback, $params = array())
344:     {
345:         global $registry;
346: 
347:         /* Check if adding messages is allowed. */
348:         $check = $registry->callByPackage($scope, $callback, array($forum_name));
349:         if ($check instanceof PEAR_Error || !$check) {
350:             return '';
351:         }
352: 
353:         /* Check if the forum exists and fetch the ID, or create a new one. */
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:         /* Set up the messages control object. */
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:      * Allows other Horde apps to post messages.
379:      *
380:      * The forum name is constructed by just the $forum_name variable under the
381:      * data root 'agora.forums.<app>'. It is up to the apps themselves to make
382:      * sure that the forum name is unique.
383:      *
384:      * If the forum does not exist, it will be automatically created by Agora.
385:      *
386:      * @access private
387:      *
388:      * @param string $scope       The application which is posting this message.
389:      * @param string $forum_name  The unique name for the forum.
390:      * @param string $callback    A callback method of the specified application
391:      *                            that gets called to make sure that posting to
392:      *                            this forum is allowed.
393:      * @param array $params       Any parameters for the forum message posting.
394:      * <pre>
395:      * message_id        - An existing message to edit
396:      * message_parent_id - The ID of the parent message
397:      * title             - Posting title
398:      * </pre>
399:      * @param string $url         If specified, the form gets submitted to this URL
400:      *                            instead of the current page.
401:      * @param array $variables    A hash with all variables of a submitted form
402:      *                            generated by this method.
403:      *
404:      * @return mixed  Returns either the rendered Horde_Form for posting a message
405:      *                or PEAR_Error object on error, or true in case of a
406:      *                successful post.
407:      */
408:     public function postMessage($scope, $forum_name, $callback, $params = array(),
409:                                 $url = null, $variables = null)
410:     {
411:         global $registry;
412: 
413:         /* Check if posting messages is allowed. */
414:         $check = $registry->callByPackage($scope, $callback, array($forum_name));
415:         if ($check instanceof PEAR_Error || !$check) {
416:             return '';
417:         }
418: 
419:         /* Create a separate notification queue. */
420:         $queue = Horde_Notification::singleton('agoraPostMessage');
421:         $queue->attach('status');
422: 
423:         /* Set up the forums object. */
424:         $forums = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create($scope);
425: 
426:         /* Set up form variables. */
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:         /* Check if the forum exists and fetch the ID. */
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:         /* Set up the messages control object. */
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:         /* Check post permissions. */
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:         /* Set a default title if one not specified. */
473:         if (!isset($params['title'])) {
474:             $params['title'] = ($editing) ? _("Edit Message") : _("Post a New Message");
475:         }
476: 
477:         /* Get the form object. */
478:         $form = $messages->getForm($vars, $params['title'], $editing, is_null($params['forum_id']));
479: 
480:         /* Validate the form. */
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:             /* Try and store this message and get back a new message_id */
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:         /* Replying to a previous post? */
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:                 /* Bad parent message id, offer to do a regular post. */
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:      * Allows other Horde apps to remove messages.
535:      *
536:      * The forum name is constructed by just the $forum_name variable
537:      * under the data root 'agora.forums.<app>'. It is up to the apps
538:      * themselves to make sure that the forum name is unique.
539:      *
540:      * @access private
541:      *
542:      * @param string $scope       The application which is posting this message.
543:      * @param string $forum_name  The unique name for the forum.
544:      * @param string $callback    A callback method of the specified application
545:      *                            that gets called to make sure that posting to
546:      *                            this forum is allowed.
547:      * @param array $params       Any parameters for the forum message posting.
548:      * <pre>
549:      * message_id        - An existing message to delete
550:      * </pre>
551:      * @param array $variables    A hash with all variables of a submitted form
552:      *                            generated by this method.
553:      *
554:      * @return mixed  Returns either the rendered Horde_Form for posting a message
555:      *                or PEAR_Error object on error, or true in case of a
556:      *                successful post.
557:      */
558:     public function removeMessage($scope, $forum_name, $callback, $params = array(),
559:                                 $variables = null)
560:     {
561:         global $registry;
562: 
563:         /* Check if posting messages is allowed. */
564:         $check = $registry->callByPackage($scope, $callback, array($forum_name));
565:         if ($check instanceof PEAR_Error || !$check) {
566:             return '';
567:         }
568: 
569:         /* Create a separate notification queue. */
570:         $queue = Horde_Notification::singleton('agoraRemoveMessage');
571:         $queue->attach('status');
572: 
573:         /* Set up the forums object. */
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:         /* Set up the messages control object. */
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:         /* Check delete permissions. */
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:         /* Get the message to be deleted. */
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:         /* Set up the form. */
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:      * Allows other Horde apps to post messages.
630:      *
631:      * In most apps we use the same code to make comments possible. This function
632:      * does most of that. Allow comments to be added to any app. The app itself
633:      * should check if the agora api is present, determine a key and call this
634:      * function before app::menu is called (before any output has started. At the
635:      * end of its output it can print the array returned to show the comments.
636:      *
637:      * @access private
638:      *
639:      * @param string $scope          The application which is posting this message.
640:      * @param string $key            Unique key from the object (picture etc we're
641:      *                               viewing. It will be used as the forum name.
642:      * @param string $callback       A callback method of the specified application
643:      *                               that gets called to make sure that posting to
644:      *                               this forum is allowed.
645:      * @param boolean $body          Show the comment bodies in the thread view or
646:      *                               not.
647:      * @param string $base_url       Base URL the edit/delete/reply links should
648:      *                               point to.
649:      * @param string $url            If specified, the form gets submitted to this
650:      *                               URL instead of the current page.
651:      * @param array $variables       A hash with all variables of a submitted form
652:      *                               generated by this method.
653:      * @param string $template_file  Template file to use.
654:      *
655:      * @return mixed array  Returns either the rendered Horde_Form for comments
656:      *                      and threads for posting/viewing a message or PEAR
657:      *                      objects on error.
658:      */
659:     public function doComments($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:         // See if we're editing.
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:      * Fill up a form data array.
711:      *
712:      * @param string $scope     The Horde application that is saving this forum.
713:      * @param string $parent    The parent forum.
714:      * @param array  $info      The forum information to consisting of:
715:      *                              forum_parent_id
716:      *                              forum_name
717:      *                              forum_description
718:      *                              forum_moderated
719:      *                              forum_attachments
720:      * @param string $callback  A callback method of the specified application
721:      *                          that gets called to make sure that posting to
722:      *                          this forum is allowed.
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:             /* Get the data owner */
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:             /* Get description */
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:      * Prepare the moderate form
789:      *
790:      * @param string $scope     The Horde application that is saving this forum.
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: 
API documentation generated by ApiGen