|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package K2 API plugin |
| 4 | + * @version 1.0 |
| 5 | + * @author Rafael Corral |
| 6 | + * @link http://www.rafaelcorral.com |
| 7 | + * @copyright Copyright (C) 2011 Rafael Corral. All rights reserved. |
| 8 | + * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php |
| 9 | + */ |
| 10 | + |
| 11 | +defined('_JEXEC') or die( 'Restricted access' ); |
| 12 | + |
| 13 | +jimport('joomla.plugin.plugin'); |
| 14 | +jimport('joomla.html.html'); |
| 15 | + |
| 16 | +require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/includes/foundry.php'; |
| 17 | +require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/models/groups.php'; |
| 18 | +require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/models/covers.php'; |
| 19 | +require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/models/albums.php'; |
| 20 | +require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/models/apps.php'; |
| 21 | + |
| 22 | +require_once JPATH_SITE.'/plugins/api/easysocial/libraries/mappingHelper.php'; |
| 23 | + |
| 24 | +class EasysocialApiResourceDiscussion extends ApiResource |
| 25 | +{ |
| 26 | + public function get() |
| 27 | + { |
| 28 | + $this->plugin->setResponse($this->getGroupDiscussion()); |
| 29 | + } |
| 30 | + |
| 31 | + public function post() |
| 32 | + { |
| 33 | + $this->plugin->setResponse($this->createGroupDiscussion()); |
| 34 | + } |
| 35 | + |
| 36 | + public function delete() |
| 37 | + { |
| 38 | + $result = new stdClass; |
| 39 | + $group_id = $app->input->get('id',0,'INT'); |
| 40 | + $appId = $app->input->get('discussion_id',0,'INT'); |
| 41 | + |
| 42 | + $discussion = FD::table( 'Discussion' ); |
| 43 | + $discussion->load( $appId ); |
| 44 | + |
| 45 | + $my = FD::user(); |
| 46 | + $group = FD::group( $group_id ); |
| 47 | + |
| 48 | + if( !$group->isAdmin() && $discussion->created_by != $this->plugin->get('user')->id) |
| 49 | + { |
| 50 | + //error message; |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // Delete the discussion |
| 55 | + $res = $discussion->delete(); |
| 56 | + $result->status = ($res)?'Conversation deleted successfully':'Unable to delete converstion.'; |
| 57 | + $this->plugin->setResponse($result); |
| 58 | + } |
| 59 | + //function use for get friends data |
| 60 | + function getGroupDiscussion() |
| 61 | + { |
| 62 | + //init variable |
| 63 | + $mainframe = JFactory::getApplication(); |
| 64 | + |
| 65 | + $group_id = $mainframe->input->get('id',0,'INT'); |
| 66 | + $appId = $mainframe->input->get('discussion_id',0,'INT'); |
| 67 | + $wres = new stdClass; |
| 68 | + $valid = 0; |
| 69 | + |
| 70 | + if(!$group_id) |
| 71 | + { |
| 72 | + $wres->status = 0; |
| 73 | + $wres->message[] = 'Group id is empty'; |
| 74 | + return $wres; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + $group = FD::group( $group_id ); |
| 79 | + |
| 80 | + // Get the current filter type |
| 81 | + $filter = $mainframe->input->get('filter','all','STRING'); |
| 82 | + |
| 83 | + $options = array(); |
| 84 | + |
| 85 | + if( $filter == 'unanswered' ) |
| 86 | + { |
| 87 | + $options[ 'unanswered' ] = true; |
| 88 | + } |
| 89 | + |
| 90 | + if( $filter == 'locked' ) |
| 91 | + { |
| 92 | + $options[ 'locked' ] = true; |
| 93 | + } |
| 94 | + |
| 95 | + if( $filter == 'resolved' ) |
| 96 | + { |
| 97 | + $options[ 'resolved' ] = true; |
| 98 | + } |
| 99 | + |
| 100 | + $options[ 'limit' ] = $mainframe->input->get('limit',10,'INT'); |
| 101 | + |
| 102 | + $mapp = new EasySocialApiMappingHelper(); |
| 103 | + |
| 104 | + $model = FD::model( 'Discussions' ); |
| 105 | + $discussions_row = $model->getDiscussions( $group->id , SOCIAL_TYPE_GROUP , $options ); |
| 106 | + |
| 107 | + $data['data'] = $mapp->mapItem($discussions_row,'discussion',$this->plugin->get('user')->id); |
| 108 | + // |
| 109 | + return( $data ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + //function for create new group |
| 114 | + function createGroupDiscussion() |
| 115 | + { |
| 116 | + //init variable |
| 117 | + $mainframe = JFactory::getApplication(); |
| 118 | + $log_user = $this->plugin->get('user')->id; |
| 119 | + // Load the discussion |
| 120 | + $discuss_id = $mainframe->input->get('discussion_id',0,'INT'); |
| 121 | + $groupId = $mainframe->input->get('group_id',0,'INT'); |
| 122 | + |
| 123 | + $wres = new stdClass; |
| 124 | + |
| 125 | + $discussion = FD::table( 'Discussion' ); |
| 126 | + $discussion->load( $discuss_id ); |
| 127 | + |
| 128 | + |
| 129 | + // Get the current logged in user. |
| 130 | + $my = FD::user($log_user); |
| 131 | + |
| 132 | + // Get the group |
| 133 | + $group = FD::group( $groupId ); |
| 134 | + |
| 135 | + |
| 136 | + // Check if the user is allowed to create a discussion |
| 137 | + /*if( !$group->isMember() ) |
| 138 | + { |
| 139 | + //error code add here |
| 140 | + return false; |
| 141 | + }*/ |
| 142 | + |
| 143 | + // Assign discussion properties |
| 144 | + $discussion->uid = $group->id; |
| 145 | + $discussion->type = 'group'; |
| 146 | + $discussion->title = $mainframe->input->get('title',0,'STRING'); |
| 147 | + $discussion->content = $mainframe->input->get('content',0,'STRING'); |
| 148 | + |
| 149 | + // If discussion is edited, we don't want to modify the following items |
| 150 | + if( !$discussion->id ) |
| 151 | + { |
| 152 | + $discussion->created_by = $my->id; |
| 153 | + $discussion->parent_id = 0; |
| 154 | + $discussion->hits = 0; |
| 155 | + $discussion->state = SOCIAL_STATE_PUBLISHED; |
| 156 | + $discussion->votes = 0; |
| 157 | + $discussion->lock = false; |
| 158 | + } |
| 159 | + |
| 160 | + //$app = $this->getApp(); |
| 161 | + $app = FD::table('App'); |
| 162 | + $app->load(25); |
| 163 | + |
| 164 | + // Ensure that the title is valid |
| 165 | + if (!$discussion->title) { |
| 166 | + |
| 167 | + $wres->status = 0; |
| 168 | + $wres->message[] = 'Discussion title is empty'; |
| 169 | + return $wres; |
| 170 | + } |
| 171 | + |
| 172 | + // Lock the discussion |
| 173 | + $state = $discussion->store(); |
| 174 | + |
| 175 | + if( !$state ) |
| 176 | + { |
| 177 | + $wres->status = 0; |
| 178 | + $wres->message[] = 'Unable to create discussion,check params'; |
| 179 | + return $wres; |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + // Process any files that needs to be created. |
| 184 | + $discussion->mapFiles(); |
| 185 | + |
| 186 | + // Get the app |
| 187 | + //$app = $this->getApp(); |
| 188 | + |
| 189 | + // If it is a new discussion, we want to run some other stuffs here. |
| 190 | + if( !$discuss_id && $state) |
| 191 | + { |
| 192 | + // @points: groups.discussion.create |
| 193 | + // Add points to the user that updated the group |
| 194 | + $points = FD::points(); |
| 195 | + $points->assign( 'groups.discussion.create' , 'com_easysocial' , $my->id ); |
| 196 | + |
| 197 | + // Create a new stream item for this discussion |
| 198 | + $stream = FD::stream(); |
| 199 | + |
| 200 | + // Get the stream template |
| 201 | + $tpl = $stream->getTemplate(); |
| 202 | + |
| 203 | + // Someone just joined the group |
| 204 | + $tpl->setActor( $my->id , SOCIAL_TYPE_USER ); |
| 205 | + |
| 206 | + // Set the context |
| 207 | + $tpl->setContext( $discussion->id , 'discussions' ); |
| 208 | + |
| 209 | + // Set the cluster |
| 210 | + $tpl->setCluster( $group->id , SOCIAL_TYPE_GROUP, $group->type ); |
| 211 | + |
| 212 | + // Set the verb |
| 213 | + $tpl->setVerb( 'create' ); |
| 214 | + |
| 215 | + // Set the params to cache the group data |
| 216 | + $registry = FD::registry(); |
| 217 | + $registry->set( 'group' , $group ); |
| 218 | + $registry->set( 'discussion', $discussion ); |
| 219 | + |
| 220 | + $tpl->setParams( $registry ); |
| 221 | + |
| 222 | + $tpl->setAccess('core.view'); |
| 223 | + |
| 224 | + // Add the stream |
| 225 | + $stream->add( $tpl ); |
| 226 | + |
| 227 | + // Set info message |
| 228 | + //FD::info()->set(false, JText::_( 'APP_GROUP_DISCUSSIONS_DISCUSSION_CREATED_SUCCESS' ), SOCIAL_MSG_SUCCESS ); |
| 229 | + |
| 230 | + // Send notification to group members only if it is new discussion |
| 231 | + $options = array(); |
| 232 | + $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 ); |
| 233 | + $options['discussionId'] = $discussion->id; |
| 234 | + $options[ 'discussionTitle' ] = $discussion->title; |
| 235 | + $options[ 'discussionContent'] = $discussion->getContent(); |
| 236 | + $options[ 'userId' ] = $discussion->created_by; |
| 237 | + |
| 238 | + $group->notifyMembers( 'discussion.create' , $options ); |
| 239 | + } |
| 240 | + |
| 241 | + $wres->id = $discussion->id; |
| 242 | + $wres->message[] = 'Group discussion created'; |
| 243 | + return $wres; |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | +/* |
| 248 | + //format friends object into required object |
| 249 | + function baseGrpObj($node=null) |
| 250 | + { |
| 251 | + if($node==null) |
| 252 | + return 0; |
| 253 | +
|
| 254 | + $user = JFactory::getUser($this->plugin->get('user')->id); |
| 255 | +
|
| 256 | + $list = array(); |
| 257 | + |
| 258 | + $grp_obj = FD::model('Groups'); |
| 259 | + |
| 260 | + $obj = new stdclass; |
| 261 | + $obj->id = $node->id; |
| 262 | + $obj->title = $node->title; |
| 263 | + $obj->description = $node->description; |
| 264 | + $obj->hits = $node->hits; |
| 265 | + $obj->state = $node->state; |
| 266 | + $obj->created_date = $node->created; |
| 267 | + |
| 268 | + //get category name |
| 269 | + $category = FD::table('GroupCategory'); |
| 270 | + $category->load($node->category_id); |
| 271 | + $obj->category_id = $node->category_id; |
| 272 | + $obj->category_name = $category->get('title'); |
| 273 | + |
| 274 | + $obj->created_by = $node->creator_uid; |
| 275 | + $obj->creator_name = JFactory::getUser($node->creator_uid)->username; |
| 276 | + $obj->type = ($node->type == 1 )?'Private':'Public'; |
| 277 | + |
| 278 | + foreach($node->avatars As $ky=>$avt) |
| 279 | + { |
| 280 | + $avt_key = 'avatar_'.$ky; |
| 281 | + $obj->$avt_key = JURI::root().'media/com_easysocial/avatars/group/'.$node->id.'/'.$avt; |
| 282 | + } |
| 283 | + |
| 284 | + //$obj->members = $node->members; |
| 285 | + $obj->member_count = $grp_obj->getTotalMembers($node->id); |
| 286 | + //$obj->cover = $grp_obj->getMeta($node->id); |
| 287 | + |
| 288 | + $alb_model = FD::model('Albums'); |
| 289 | + |
| 290 | + $uid = $node->id.':'.$node->title; |
| 291 | +
|
| 292 | + $filters = array('uid'=>$uid,'type'=>'group'); |
| 293 | + //get total album count |
| 294 | + $obj->album_count = $alb_model->getTotalAlbums($filters); |
| 295 | +
|
| 296 | + //get group album list |
| 297 | + //$albums = $alb_model->getAlbums($uid,'group'); |
| 298 | + |
| 299 | + $obj->isowner = ( $node->creator_uid == $userid )?true:false; |
| 300 | + $obj->ismember = in_array( $log_user->id,$node->members ); |
| 301 | + $obj->approval_pending = in_array( $log_user->id,$node->pending ); |
| 302 | +
|
| 303 | + /*$news_obj = new EasySocialModelGroups(); |
| 304 | + $news = $news_obj->getNews($node->id); */ |
| 305 | + |
| 306 | + /*return $obj; |
| 307 | + } |
| 308 | +*/ |
| 309 | + |
| 310 | +} |
0 commit comments