Ejemplo n.º 1
0
 /**
  * Creates a new discussion
  *
  * @since	1.2
  * @access	public
  * @param	string
  * @return
  */
 public function save()
 {
     // Check for request forgeriess
     FD::checkToken();
     // Ensure that the user is logged in.
     FD::requireLogin();
     // Load up ajax lib
     $ajax = FD::ajax();
     // Load the discussion
     $id = JRequest::getInt('id');
     $discussion = FD::table('Discussion');
     $discussion->load($id);
     // Get the current logged in user.
     $my = FD::user();
     // Get the group
     $groupId = JRequest::getInt('cluster_id', 0);
     $group = FD::group($groupId);
     // Only allow owner and admin to modify the
     if ($discussion->id) {
         if ($discussion->created_by != $my->id && !$group->isAdmin() && !$my->isSiteAdmin()) {
             return $this->redirect($group->getPermalink(false));
         }
     }
     // Check if the user is allowed to create a discussion
     if (!$group->isMember()) {
         FD::info()->set(JText::_('APP_GROUP_DISCUSSIONS_NOT_ALLOWED_CREATE'), SOCIAL_MSG_ERROR);
         // Perform a redirection
         return JFactory::getApplication()->redirect(FRoute::dashboard());
     }
     // Assign discussion properties
     $discussion->uid = $group->id;
     $discussion->type = SOCIAL_TYPE_GROUP;
     $discussion->title = JRequest::getVar('title', '');
     $discussion->content = JRequest::getVar('content', '', 'POST', 'none', JREQUEST_ALLOWRAW);
     // If discussion is edited, we don't want to modify the following items
     if (!$discussion->id) {
         $discussion->created_by = $my->id;
         $discussion->parent_id = 0;
         $discussion->hits = 0;
         $discussion->state = SOCIAL_STATE_PUBLISHED;
         $discussion->votes = 0;
         $discussion->lock = false;
     }
     $app = $this->getApp();
     // Ensure that the title is valid
     if (!$discussion->title) {
         Foundry::info()->set(JText::_('APP_GROUP_DISCUSSIONS_INVALID_TITLE'), SOCIAL_MSG_ERROR);
         // Get the redirection url
         $url = FRoute::apps(array('layout' => 'canvas', 'customView' => 'create', 'uid' => $group->getAlias(), 'type' => SOCIAL_TYPE_GROUP, 'id' => $app->getAlias()), false);
         return $this->redirect($url);
     }
     // Lock the discussion
     $state = $discussion->store();
     if (!$state) {
         FD::info()->set(JText::_('APP_GROUP_DISCUSSIONS_DISCUSSION_CREATED_FAILED'));
         // Get the redirection url
         $url = FRoute::apps(array('layout' => 'canvas', 'customView' => 'form', 'uid' => $group->getAlias(), 'type' => SOCIAL_TYPE_GROUP, 'id' => $app->getAlias()), false);
         return $this->redirect($url);
     }
     // Process any files that needs to be created.
     $discussion->mapFiles();
     // Get the app
     $app = $this->getApp();
     // If it is a new discussion, we want to run some other stuffs here.
     if (!$id) {
         // @points: groups.discussion.create
         // Add points to the user that updated the group
         $points = FD::points();
         $points->assign('groups.discussion.create', 'com_easysocial', $my->id);
         // Create a new stream item for this discussion
         $stream = FD::stream();
         // Get the stream template
         $tpl = $stream->getTemplate();
         // Someone just joined the group
         $tpl->setActor($my->id, SOCIAL_TYPE_USER);
         // Set the context
         $tpl->setContext($discussion->id, 'discussions');
         // Set the cluster
         $tpl->setCluster($group->id, SOCIAL_TYPE_GROUP, $group->type);
         // Set the verb
         $tpl->setVerb('create');
         // Set the params to cache the group data
         $registry = FD::registry();
         $registry->set('group', $group);
         $registry->set('discussion', $discussion);
         $tpl->setParams($registry);
         $tpl->setAccess('core.view');
         // Add the stream
         $stream->add($tpl);
         // Set info message
         FD::info()->set(false, JText::_('APP_GROUP_DISCUSSIONS_DISCUSSION_CREATED_SUCCESS'), SOCIAL_MSG_SUCCESS);
         // Send notification to group members only if it is new discussion
         $options = array();
         $options['permalink'] = FRoute::apps(array('layout' => 'canvas', 'customView' => 'item', 'uid' => $group->getAlias(), 'type' => SOCIAL_TYPE_GROUP, 'id' => $app->getAlias(), 'discussionId' => $discussion->id, 'external' => true), false);
         $options['discussionId'] = $discussion->id;
         $options['discussionTitle'] = $discussion->title;
         $options['discussionContent'] = $discussion->getContent();
         $options['userId'] = $discussion->created_by;
         $group->notifyMembers('discussion.create', $options);
     }
     // Get the redirection url
     $url = FRoute::apps(array('layout' => 'canvas', 'customView' => 'item', 'uid' => $group->getAlias(), 'type' => SOCIAL_TYPE_GROUP, 'id' => $app->getAlias(), 'discussionId' => $discussion->id), false);
     // Perform a redirection
     $this->redirect($url);
 }