diff --git a/easysocial/easysocial/group.php b/easysocial/easysocial/group.php index 3e539e1..9a8f4ce 100644 --- a/easysocial/easysocial/group.php +++ b/easysocial/easysocial/group.php @@ -9,19 +9,18 @@ * Work derived from the original RESTful API by Techjoomla (https://github.com/techjoomla/Joomla-REST-API) * and the com_api extension by Brian Edgerton (http://www.edgewebworks.com) */ - defined('_JEXEC') or die('Restricted access'); jimport('joomla.plugin.plugin'); -jimport('joomla.html.html'); - -require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/foundry.php'; -require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/models/groups.php'; -require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/models/covers.php'; -require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/models/albums.php'; -require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/models/fields.php'; -require_once JPATH_SITE . '/plugins/api/easysocial/libraries/mappingHelper.php'; -require_once JPATH_SITE . '/plugins/api/easysocial/libraries/uploadHelper.php'; + +use Joomla\Registry\Registry; + +JLoader::register("EasySocialApiUploadHelper", JPATH_SITE . '/plugins/api/easysocial/libraries/uploadHelper.php'); +JLoader::register("EasySocialApiMappingHelper", '/plugins/api/easysocial/libraries/mappingHelper.php'); + + +ES::import('fields:/group/permalink/helper'); + /** * API class EasysocialApiResourceGroup * @@ -30,429 +29,608 @@ class EasysocialApiResourceGroup extends ApiResource { /** - * Method description + * Method to get details of single Easysocial group * - * @return mixed + * @return ApiPlugin response object * * @since 1.0 */ public function get() { - // Init variable - $app = JFactory::getApplication(); - $log_user = JFactory::getUser($this->plugin->get('user')->id); - $group_id = $app->input->get('id', 0, 'INT'); - $other_user_id = $app->input->get('user_id', 0, 'INT'); - $userid = ($other_user_id)?$other_user_id:$log_user->id; + $input = JFactory::getApplication()->input; - // $user = FD::user($userid); - $mapp = new EasySocialApiMappingHelper; + // Get the group object + $id = $input->get('id', 0, 'int'); - // $grp_model = FD::model('Groups'); + $user = ES::user(); + $group = ES::group($id); - $res = new stdclass; - $res->result = array(); - $res->empty_message = ''; - - if ($group_id) + // Ensure that the id provided is valid + if (! $group || ! $group->id) { - $group[] = FD::group($group_id); - $res->result = $mapp->mapItem($group, 'group', $log_user->id); - $this->plugin->setResponse($res); + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_INVALID_GROUP_ID')); } - else + + // Ensure that the user has access to view group's item + if (! $group->canViewItem() || !$group->isPublished() || ($user->id != $group->creator_uid && $user->isBlockedBy($group->creator_uid))) { - $this->plugin->err_code = 403; - $this->plugin->err_message = 'Group Not Found'; - $this->plugin->setResponse(null); + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_NO_ACCESS')); } - } - /** - * Method description - * - * @return mixed - * - * @since 1.0 - */ + $EasySocialApiMappingHelper = new EasySocialApiMappingHelper; - public function post() - { - $this->CreateGroup(); + $apiResponse = new stdclass; + $apiResponse->result = array(); + $apiResponse->empty_message = ''; + + $apiResponse->result = $EasySocialApiMappingHelper->mapItem([$group], 'group', $user->id); + $this->plugin->setResponse($apiResponse); } /** - * Method description + * Method to create/update single Easysocial group * - * @return mixed + * @return ApiPlugin response object * * @since 1.0 */ - public function delete() + public function post() { - $app = JFactory::getApplication(); - $group_id = $app->input->get('id', 0, 'INT'); - $valid = 1; - $group = FD::group($group_id); - $groupsModel = FD::model('groups'); - $res = new stdclass; - - if (!$group->id || !$group_id) + $user = ES::user(); + $input = JFactory::getApplication()->input; + $id = $input->get('id', 0, 'int'); + $cid = $id; + $apiResponse = new stdClass; + $apiResponse->result = new stdClass; + $postValues = $input->post->getArray(); + + if (empty($postValues['title'])) { - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_INVALID_GROUP_MESSAGE'); - $valid = 0; + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_INVALID_GROUP_NAME')); } - // Only allow super admins to delete groups - $my = FD::user($this->plugin->get('user')->id); + // Check parmalink + if (empty($postValues['permalink'])) + { + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_INVALID_PARMALINK')); + } - if (!$my->isSiteAdmin() && !$groupsModel->isOwner($my->id, $group_id)) + // Check description + if (empty($postValues['description'])) { - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_ACCESS_DENIED_MESSAGE'); - $valid = 0; + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_EMPTY_DESCRIPTION')); } - if ($valid) + // Check group type + if (empty($postValues['type'])) { - // Try to delete the group - $group->delete(); - $res->result->status = 1; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_GROUP_DELETED_MESSAGE'); + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_ADD_GROUP_TYPE_MESSAGE')); } - $this->plugin->setResponse($res); - } + // Flag to see if this is new or edit + $isNew = empty($id); - /** - * Method function for create new group - * - * @return mixed - * - * @since 1.0 - */ - public function CreateGroup() - { - $app = JFactory::getApplication(); - $log_user = JFactory::getUser($this->plugin->get('user')->id); - $user = FD::user($log_user->id); - $config = FD::config(); - - // Create group post structure - $grp_data = array(); - $valid = 1; - $title = $app->input->get('title', null, 'STRING'); - $parmalink = $app->input->get('parmalink', null, 'STRING'); - $description = $app->input->get('description', null, 'STRING'); - $type = $app->input->get('type', 0, 'INT'); - $categoryId = $app->input->get('category_id', 0, 'INT'); - $avtar_pth = ''; - $avtar_scr = ''; - $avtar_typ = ''; - $phto_obj = null; - $canCreate = ES::user(); - - // Response object - $res = new stdclass; - - // Check if the user really has access to create groups - if (! $canCreate->getAccess()->allowed('groups.create') && ! $canCreate->isSiteAdmin()) + if ($isNew) + { + if (! $user->getAccess()->allowed('groups.create') && ! $user->isSiteAdmin()) { ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_NO_ACCESS_CREATE_GROUP')); } - if (!empty($_FILES['file']['name'])) - { - $upload_obj = new EasySocialApiUploadHelper; - - // Checking upload cover - $phto_obj = $upload_obj->ajax_avatar($_FILES['file']); - $avtar_pth = $phto_obj['temp_path']; - $avtar_scr = $phto_obj['temp_uri']; - $avtar_typ = 'upload'; - $avatar_file_name = $_FILES['file']['name']; - } + // Ensure that the user did not exceed their group creation limit + if ($user->getAccess()->intervalExceeded('groups.limit', $user->id) && ! $user->isSiteAdmin()) + { + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_EXCEEDED_LIMIT')); + } - $cover_data = null; + $this->validateGroupPermalink($postValues['permalink'], 0); - if (!empty($_FILES['cover_file']['name'])) - { - $upload_obj = new EasySocialApiUploadHelper; + // Load the group category + $category = ES::table('GroupCategory'); + $category->load($postValues['category_id']); - // Ckecking upload cover - $cover_data = $upload_obj->ajax_cover($_FILES['cover_file'], 'cover_file'); - } + if (! $category->id || ($category->type != SOCIAL_FIELDS_GROUP_GROUP)) + { + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_INVALID_CATEGORY_ID')); + } - // Check title - if (empty($title) || $title == null) - { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_INVALID_GROUP_NAME'); - } + // Check if the user really has access to create groups + if (! $user->getAccess()->allowed('groups.create') && ! $user->isSiteAdmin()) + { + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_NO_ACCESS_CREATE_GROUP')); + } - // Check parmalink - if (empty($parmalink) || $parmalink == null) - { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_INVALID_PARMALINK'); - } + // Need to check if this clsuter category has creation limit based on user points or not. + if (! $category->hasPointsToCreate($user->id)) + { + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_GROUPS_INSUFFICIENT_POINTS')); + } - // Check description - if (empty($description) || $description == null) - { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_EMPTY_DESCRIPTION'); - } + $options = array(); + $options['workflow_id'] = $category->getWorkflow()->id; + $options['group'] = SOCIAL_FIELDS_GROUP_GROUP; - // Check group type - if (empty($type) || $type == 0) - { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_ADD_GROUP_TYPE_MESSAGE'); - } + // Get fields model + $fieldsModel = ES::model('Fields'); - if (!$valid) - { - $this->plugin->setResponse($res); + // Special case for group AVTAR and COVER + $files = $input->post->files; + + // Retrieve all file objects if needed + + if ($files->get('cover')) + { + $uploadObj = new EasySocialApiUploadHelper; + $coverMeta = $uploadObj->ajax_cover($files->get('cover'), 'cover', $category->id, SOCIAL_TYPE_GROUP); + + if (! $coverMeta) + { + ApiError::raiseError(400, $uploadObj->getError()); + } + + $postValues['cover'] = $coverMeta; + } + + if ($files->get('avatar')) + { + $uploadObj = new EasySocialApiUploadHelper; + $avtarMeta = $uploadObj->ajax_avatar($files->get('avatar'), 'avatar', $category->id, SOCIAL_TYPE_GROUP); + + if (empty($avtarMeta)) + { + ApiError::raiseError(400, $uploadObj->getError()); + } + + $postValues['avatar'] = array( + 'source' => $avtarMeta['temp_uri'], 'path' => $avtarMeta['temp_path'], 'data' => '', 'type' => 'upload', + 'name' => $files->get('avatar')['name'] + ); + } + + // Get the custom fields + $fields = $fieldsModel->getCustomFields($options); + + // Now map the post values with Easysocial custom fields + $fieldArray = $this->createFieldArr($postValues, $options); + + // Get current user's info + $session = JFactory::getSession(); + + // Get necessary info about the current registration process. + $stepSession = ES::table('StepSession'); + $stepSession->load($session->getId()); + $stepSession->uid = $category->id; + + // Step is eliminated from here since it is single step registration + + // Merge the post values + $registry = ES::get('Registry'); + $registry->load($stepSession->values); + + // Load up groups model + $groupsModel = ES::model('Groups'); + + // Load json library. + $json = ES::json(); + + $post = $input->post->getArray(); + + $disallow = array( + 'option', 'cid', 'controller', 'task', 'option', 'currentStep' + ); + + // Process $_POST vars + foreach ($fieldArray as $key => $value) + { + if (! in_array($key, $disallow)) + { + if (is_array($value)) + { + $value = ES::json()->encode($value); + } + + $registry->set($key, $value); + } + } + + // Convert the values into an array. + $data = $registry->toArray(); + $args = array( + &$data, 'conditionalRequired' => '', &$stepSession + ); + + // Perform field validations here. Validation should only trigger apps that are loaded on the form + // @trigger onRegisterValidate + $fieldsLib = ES::fields(); + + // Format conditional data + $fieldsLib->trigger('onConditionalFormat', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); + + // Rebuild the arguments since the data is already changed previously. + $args = array( + &$data, 'conditionalRequired' => '', &$stepSession + ); + + // Some data need to be retrieved in raw value. let fire another trigger. #730 + $fieldsLib->trigger('onFormatData', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); + + // Get the trigger handler + $handler = $fieldsLib->getHandler(); + + // Get error messages + $errors = $fieldsLib->trigger('onRegisterValidate', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler, 'validate')); + + // The values needs to be stored in a JSON notation. + $stepSession->values = $json->encode($data); + + // Store registration into the temporary table. + $stepSession->store(); + + // Bind any errors into the registration object + $stepSession->setErrors($errors); + + // Saving was intercepted by one of the field applications. + if (is_array($errors) && count($errors) > 0) + { + ApiError::raiseError(400, JText::_('COM_EASYSOCIAL_REGISTRATION_SOME_ERRORS_IN_THE_REGISTRATION_FORM')); + } + + // Update creation date + $stepSession->created = ES::date()->toMySQL(); + + // Save the temporary data. + $stepSession->store(); + + // Create the group now. + $group = $groupsModel->createGroup($stepSession); + + // If there's no id, we know that there's some errors. + if (! $group->id) + { + $errors = $groupsModel->getError(); + ApiError::raiseError(400, $errors); + } + + $points = ES::points(); + $points->assign('groups.create', 'com_easysocial', $user->id); + + // Add this action into access logs. + ES::access()->log('groups.limit', $user->id, $group->id, SOCIAL_TYPE_GROUP); + $message = JText::_('COM_EASYSOCIAL_GROUPS_CREATED_PENDING_APPROVAL'); + + // If the group is published, we need to perform other activities + if ($group->state == SOCIAL_STATE_PUBLISHED) + { + $message = JText::_('COM_EASYSOCIAL_GROUPS_CREATED_SUCCESSFULLY'); + $this->addTostream($user, $group); + + // Update social goals + $user->updateGoals('joincluster'); + } + + $apiResponse->result->status = 1; + $apiResponse->result->message = $message; + + $this->plugin->setResponse($apiResponse); } else { - // Create steps - $db = FD::db(); - $group = FD::table('Group'); - FD::import('admin:/includes/group/group'); - $group = new SocialGroup; + $group = ES::group($id); - // Load front end's language file - FD::language()->loadSite(); + if (!$id || !$group->id) + { + ApiError::raiseError(400, JText::_("COM_EASYSOCIAL_GROUPS_INVALID_GROUP_ID")); + } - $category = FD::table('GroupCategory'); - $category->load($categoryId); + if (!$group->isOwner() && !$group->isAdmin() && !$user->isSiteAdmin()) + { + ApiError::raiseError(400, JText::_("COM_EASYSOCIAL_GROUPS_NO_ACCESS")); + } - // Get the steps - $stepsModel = FD::model('Steps'); - $steps = $stepsModel->getSteps($categoryId, SOCIAL_TYPE_CLUSTERS); + if (! SocialFieldsGroupPermalinkHelper::valid($postValues['permalink'], new Registry)) + { + ApiError::raiseError(400, JText::_('PLG_FIELDS_GROUP_PERMALINK_INVALID_PERMALINK')); + } - // Get the fields - $lib = FD::fields(); - $fieldsModel = FD::model('Fields'); + $this->validateGroupPermalink($postValues['permalink'], $group); - // Query written due to commented function not working - $query = "SELECT a.id, a.unique_key FROM `#__social_fields` AS `a` - LEFT JOIN `#__social_apps` AS `b` ON `b`.`id` = `a`.`app_id` - LEFT JOIN `#__social_fields_steps` AS `d` ON `a`.`step_id` = `d`.`id` - WHERE `a`.`step_id` = '" . $steps[0]->id . "' ORDER BY `d`.`sequence` ASC, `a`.`ordering` ASC"; + // Special case for group AVTAR and COVER + $files = $input->post->files; - $db->setQuery($query); - $field_ids = $db->loadAssocList(); + // Retrieve all file objects if needed + if ($files->get('cover')) + { + $uploadObj = new EasySocialApiUploadHelper; + $coverMeta = $uploadObj->ajax_cover($files->get('cover'), 'cover', $group->category_id, SOCIAL_TYPE_GROUP); - foreach ($field_ids as $field) + if (! $coverMeta) { - $grp_data['cid'][] = $field['id']; + ApiError::raiseError(400, $uploadObj->getError()); + } - switch ($field['unique_key']) - { - case 'HEADER': - break; - case 'TITLE': $grp_data['es-fields-' . $field['id']] = $title; - break; - case 'PERMALINK': $grp_data['es-fields-' . $field['id']] = $parmalink; - break; - case 'DESCRIPTION': $grp_data['es-fields-' . $field['id']] = $description; - break; - case 'TYPE': $grp_data['group_type'] = $type; - break; - case 'URL': $grp_data['es-fields-' . $field['id']] = $app->input->get('website', null, 'STRING'); - break; - case 'PHOTOS': $grp_data['photo_albums'] = $app->input->get('photo_album', false, 'BOOLEAN'); - break; - case 'NEWS': $grp_data['es-fields-' . $field['id']] = $app->input->get('announcements', false, 'BOOLEAN'); - break; - case 'DISCUSSIONS': $grp_data['es-fields-' . $field['id']] = $app->input->get('discussions', false, 'BOOLEAN'); - break; - case 'AVATAR': $grp_data['es-fields-' . $field['id']] = Array - ( - 'source' => $avtar_scr, - 'path' => $avtar_pth, - 'data' => '', - 'type' => $avtar_typ, - 'name' => $avatar_file_name - ); - break; - case 'COVER': $grp_data['es-fields-' . $field['id']] = Array( - 'data' => $cover_data, - 'position' => '{"x":0.5, "y":0.5}' - ); - break; - } + $postValues['cover'] = $coverMeta; + } + + if ($files->get('avatar')) + { + $uploadObj = new EasySocialApiUploadHelper; + $avtarMeta = $uploadObj->ajax_avatar($files->get('avatar'), 'avatar', $group->category_id, SOCIAL_TYPE_GROUP); + + if (empty($avtarMeta)) + { + ApiError::raiseError(400, $uploadObj->getError()); } - // For check group exceed limit - if (!$user->getAccess()->allowed('groups.create') && !$user->isSiteAdmin()) + $postValues['avatar'] = array( + 'source' => $avtarMeta['temp_uri'], 'path' => $avtarMeta['temp_path'], 'data' => '', 'type' => 'upload', + 'name' => $files->get('avatar')['name'] + ); + } + + $fieldsModel = ES::model('Fields'); + + $options = array( + 'group' => SOCIAL_TYPE_GROUP , + 'workflow_id' => $group->getWorkflow()->id, + 'data' => true, 'dataId' => $group->id, + 'dataType' => SOCIAL_TYPE_GROUP, + 'visible' => SOCIAL_PROFILES_VIEW_EDIT); + + $fields = $fieldsModel->getCustomFields($options); + $groupData = $this->generateGroupFieldData($fields); + $postValues = $this->createFieldArr($postValues, $options); + $registry = ES::registry(); + + foreach ($groupData as $key => $value) + { + if (isset($postValues[$key])) { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_CREATE_GROUP_ACCESS_DENIED'); - $this->plugin->setResponse($res); + $value = $postValues[$key]; } - // Ensure that the user did not exceed their group creation limit - if ($user->getAccess()->intervalExceeded('groups.limit', $user->id) && !$user->isSiteAdmin()) + if (is_array($value)) { - $valid = 0; - $res->result->status = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_GROUP_CREATION_LIMIT_EXCEEDS'); - $this->plugin->setResponse($res); + $value = json_encode($value); } - // Get current user's info - $session = JFactory::getSession(); + $registry->set($key, $value); + } - // Get necessary info about the current registration process. - $stepSession = FD::table('StepSession'); - $stepSession->load($session->getId()); - $stepSession->uid = $categoryId; + $data = $data = $registry->toArray(); + $fieldsLib = ES::fields(); + $handler = $fieldsLib->getHandler(); - // Load the group category - $category = FD::table('GroupCategory'); - $category->load($stepSession->uid); + // @TODO Add conditional field support + $args = array(&$data, 'conditionalRequired' => '', &$group); + $fieldsLib->trigger('onConditionalFormat', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler)); + $args = array(&$data, 'conditionalRequired' => '', &$group); + $errors = $fieldsLib->trigger('onAdminEditValidate', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler, 'validate')); - $sequence = $category->getSequenceFromIndex($stepSession->step, SOCIAL_GROUPS_VIEW_REGISTRATION); + if (is_array($errors) && count($errors) > 0) + { + ApiError::raiseError(400, JText::_("COM_EASYSOCIAL_GROUPS_PROFILE_SAVE_ERRORS")); + } - // Load the current step. - $step = FD::table('FieldStep'); - $step->load(array('uid' => $category->id, 'type' => SOCIAL_TYPE_CLUSTERS, 'sequence' => $sequence)); + $errors = $fieldsLib->trigger('onEditBeforeSave', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler, 'beforeSave')); - // Merge the post values - $registry = FD::get('Registry'); - $registry->load($stepSession->values); + if (is_array($errors) && count($errors) > 0) + { + ApiError::raiseError(400, JText::_("COM_EASYSOCIAL_PROFILE_ERRORS_IN_FORM")); + } - // Load up groups model - $groupsModel = FD::model('Groups'); + if ($group->isDraft() || $user->getAccess()->get('groups.moderate')) + { + $group->state = SOCIAL_CLUSTER_PENDING; + } - // Get all published fields apps that are available in the current form to perform validations - $fieldsModel = FD::model('Fields'); - $fields = $fieldsModel->getCustomFields(array('step_id' => $step->id, 'visible' => SOCIAL_GROUPS_VIEW_REGISTRATION)); + if ($user->isSiteAdmin()) + { + $group->state = SOCIAL_CLUSTER_PUBLISHED; + } - // Load json library. - $json = FD::json(); - $token = FD::token(); - $disallow = array($token, 'option', 'cid', 'controller', 'task', 'option', 'currentStep'); + $group->save(); + $args = array(&$data, &$group); + $fieldsLib->trigger('onEditAfterSave', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); + $group->bindCustomFields($data); + $args = array(&$data, &$group); + $fieldsLib->trigger('onEditAfterSaveFields', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); - foreach ($grp_data as $key => $value) - { - if (!in_array($key, $disallow)) - { - if (is_array($value)) - { - $value = FD::json()->encode($value); - } + if ($group->isPublished()) + { + $points = ES::points(); + $points->assign('groups.update', 'com_easysocial', $user->id); + $group->createStream($user->id, 'update'); + } - $registry->set($key, $value); - } - } + $messageLang = $group->isPending() ? 'COM_EASYSOCIAL_GROUPS_UPDATED_PENDING_APPROVAL' : 'COM_EASYSOCIAL_GROUPS_PROFILE_UPDATED_SUCCESSFULLY'; - // Convert the values into an array. - $data = $registry->toArray(); - $args = array(&$data, &$stepSession); + $apiResponse->result->status = 1; + $apiResponse->result->message = JText::_($messageLang); - /** Perform field validations here. Validation should only trigger apps that are loaded on the form - * @trigger onRegisterValidate - */ - $fieldsLib = FD::fields(); + $this->plugin->setResponse($apiResponse); + } + } - // Get the trigger handler - $handler = $fieldsLib->getHandler(); + /** + * Method to delete single Easysocial group + * + * @return ApiPlugin response object + * + * @since 1.0 + */ + public function delete() + { + $input = JFactory::getApplication()->input; - // Get error messages - $errors = $fieldsLib->trigger('onRegisterValidate', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler, 'validate')); + $user = ES::user(); - // The values needs to be stored in a JSON notation. - $stepSession->values = $json->encode($data); - $stepSession->created = FD::date()->toMySQL(); - $group = $groupsModel->createGroup($stepSession); + // Get the group + $id = $input->get('id', 0, 'int'); + $group = ES::group($id); - if ($group->id) - { - $res->result->status = 1; - $res->result->id = $group->id; + if (! $group->id || ! $id) + { + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_INVALID_GROUP_MESSAGE')); + } - // @points: groups.create - // Assign points to the user when a group is created - $points = FD::points(); - $points->assign('groups.create', 'com_easysocial', $log_user); + // Only group owner and site admins are allowed to delete the group + if (! $user->isSiteAdmin() && ! $group->isOwner()) + { + ApiError::raiseError(400, JText::_('PLG_API_EASYSOCIAL_ACCESS_DENIED_MESSAGE')); + } - // If the group is published, we need to perform other activities - if ($group->state == SOCIAL_STATE_PUBLISHED) - { - $this->addTostream($user, $group, $config); - } + // Try to delete the group + $group->delete(); - $my = FD::user(); - $group->state = $my->getAccess()->get('groups.moderate'); + $apiResponse = new stdclass; - if ($group->state) - { - $res->result->message = JText::_('COM_EASYSOCIAL_GROUPS_CREATED_PENDING_APPROVAL'); - } - else - { - $res->result->message = JText::_('COM_EASYSOCIAL_GROUPS_CREATED_SUCCESSFULLY'); - } - } - else + $apiResponse->result->status = 1; + $apiResponse->result->message = JText::_('PLG_API_EASYSOCIAL_GROUP_DELETED_MESSAGE'); + + $this->plugin->setResponse($apiResponse); + } + + /** + * Method to create stream on Easysocial wall + * + * @param Object $user SocialUser Object + * @param Object $group SocialGroup Object + * @param array $config config object + * + * @return boolean + * + * @since 1.0 + */ + public function addTostream($user, $group, $config) + { + // Add activity logging when a user creates a new group. + $stream = ES::stream(); + $streamTemplate = $stream->getTemplate(); + + // Set the actor + $streamTemplate->setActor($user, SOCIAL_TYPE_USER); + + // Set the context + $streamTemplate->setContext($group->id, SOCIAL_TYPE_GROUPS); + + $streamTemplate->setVerb('create'); + $streamTemplate->setSiteWide(); + $streamTemplate->setAccess('core.view'); + $streamTemplate->setCluster($group->id, SOCIAL_TYPE_GROUP, $group->type); + + // Set the params to cache the group data + $registry = ES::registry(); + $registry->set('group', $group); + + // Set the params to cache the group data + $streamTemplate->setParams($registry); + + // Add stream template. + $stream->add($streamTemplate); + + return true; + } + + /** + * create field array as per easysocial format for storing custom field data + * + * @param Object $postValues The group post data + * @param Object $fieldsOption The options required to get field data + * + * @return Array + * + * @since 1.0 + */ + public function createFieldArr($postValues, $fieldsOption) + { + $userfields = array(); + $fieldsModel = ES::model('Fields'); + + foreach ($postValues as $key => $value) + { + $fieldsOption['key'] = $key; + + $localfield = $fieldsModel->getCustomFields($fieldsOption); + + if (! empty($localfield)) + { + switch ($key) { - $res->result->status = 0; - $res->result->id = 0; - $res->result->message = JText::_('PLG_API_EASYSOCIAL_UNABLE_CREATE_GROUP_MESSAGE'); + case 'cover': + $userfields[SOCIAL_FIELDS_PREFIX . $localfield['0']->id] = array( + 'data' => $value, 'position' => '{"x":0, "y":0}' + ); + break; + default: + $userfields[SOCIAL_FIELDS_PREFIX . $localfield['0']->id] = $value; } - - $this->plugin->setResponse($res); + } } + + return $userfields; } /** - * Method addTostream + * create field array as per easysocial format for updating custom field data * - * @param array $my my - * @param array $group group object - * @param array $config config object - * - * @return string + * @param Array $fields The array SocialFields class object * - * @since 1.0 + * @return Array + * + * @since 2.0 */ - public function addTostream($my, $group, $config) + private function generateGroupFieldData($fields) { - $stream = FD::stream(); - $streamTemplate = $stream->getTemplate(); + $groupFields = array(); - // Set the actor - $streamTemplate->setActor($my->id, SOCIAL_TYPE_USER); + if (!empty($fields)) + { + foreach ($fields as $field) + { + $groupFields[SOCIAL_FIELDS_PREFIX . $field->id] = $field->data; + } + } - // Set the context - $streamTemplate->setContext($group->id, SOCIAL_TYPE_GROUPS); + return $groupFields; + } - $streamTemplate->setVerb('create'); - $streamTemplate->setSiteWide(); - $streamTemplate->setAccess('core.view'); - $streamTemplate->setCluster($group->id, SOCIAL_TYPE_GROUP, $group->type); + /** + * Check the permalink value provided for the group + * + * @param String $permalink The permalink value + * @param Object $group The SocialGroup class object + * + * @return boolean + * + * @since 2.0 + */ + private function validateGroupPermalink($permalink, $group) + { + if ($group) + { + // If the permalink is the same, just return true. + if ($group->alias == $permalink) + { + return true; + } + } - // Set the params to cache the group data - $registry = FD::registry(); - $registry->set('group', $group); + if (!SocialFieldsGroupPermalinkHelper::allowed($permalink)) + { + ApiError::raiseError(400, JText::_('PLG_FIELDS_PERMALINK_CONFLICTS_WITH_SYSTEM')); + } - // Set the params to cache the group data - $streamTemplate->setParams($registry); + // Peramalink validation + if (SocialFieldsGroupPermalinkHelper::exists($permalink)) + { + ApiError::raiseError(400, JText::_('PLG_FIELDS_GROUP_PERMALINK_NOT_AVAILABLE')); + } - // Add stream template. - $stream->add($streamTemplate); + if (! SocialFieldsGroupPermalinkHelper::valid($permalink, new Registry)) + { + ApiError::raiseError(400, JText::_('PLG_FIELDS_GROUP_PERMALINK_INVALID_PERMALINK')); + } - return true; + return true; } } diff --git a/easysocial/easysocial/groups.php b/easysocial/easysocial/groups.php index 52b0adc..10998f8 100644 --- a/easysocial/easysocial/groups.php +++ b/easysocial/easysocial/groups.php @@ -70,7 +70,7 @@ public function post() $apiResponse = new stdclass; $apiResponse->result = array(); - $apiResponse->empty_message = JText::_('COM_API_GROUPS_EMPTY_ALL'); + $apiResponse->empty_message = JText::_('PLG_API_GROUPS_EMPTY_ALL'); $limit = $app->input->get('limit', 10, 'INT'); $filters['limit'] = $limit; @@ -89,25 +89,25 @@ public function post() $options['userid'] = $user->id; $options['types'] = 'participated'; $options['featured'] = ''; - $apiResponse->empty_message = JText::_('COM_API_GROUPS_EMPTY_CREATED'); + $apiResponse->empty_message = JText::_('PLG_API_GROUPS_EMPTY_CREATED'); } elseif (isset($filters['invited'])) { $options['invited'] = $user->id; $options['types'] = 'all'; - $apiResponse->empty_message = JText::_('COM_API_GROUPS_EMPTY_INVITED'); + $apiResponse->empty_message = JText::_('PLG_API_GROUPS_EMPTY_INVITED'); } elseif (isset($filters['pending'])) { $options['uid'] = $user->id; $options['state'] = SOCIAL_CLUSTER_DRAFT; $options['types'] = 'user'; - $apiResponse->empty_message = JText::_('COM_API_CLUSTER_NO_PENDING_MODERATION_GROUP'); + $apiResponse->empty_message = JText::_('PLG_API_CLUSTER_NO_PENDING_MODERATION_GROUP'); } elseif (isset($filters['featured'])) { $options['featured'] = true; - $apiResponse->empty_message = JText::_('COM_API_GROUPS_EMPTY_FEATURED'); + $apiResponse->empty_message = JText::_('PLG_API_GROUPS_EMPTY_FEATURED'); } elseif (isset($filters['participated']) && $user->id) { @@ -149,7 +149,7 @@ public function post() $options['category'] = $categoryId; } - $apiResponse->empty_message = JText::_('COM_API_GROUPS_EMPTY_CATEGORY'); + $apiResponse->empty_message = JText::_('PLG_API_GROUPS_EMPTY_CATEGORY'); } $groups = $model->getGroups($options); diff --git a/easysocial/libraries/mappingHelper.php b/easysocial/libraries/mappingHelper.php index c10222d..1b78671 100644 --- a/easysocial/libraries/mappingHelper.php +++ b/easysocial/libraries/mappingHelper.php @@ -57,7 +57,7 @@ class EasySocialApiMappingHelper * @param string $strip_tags strip tags * @param int $text_length text length * @param string $skip skip - * + * * @return string * * @since 1.0 @@ -128,7 +128,7 @@ public function mapItem($rows, $obj_type = '', $userid = 0, $type = '', $strip_t * * @param string $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -183,7 +183,7 @@ public function photosSchema($rows, $userid) * * @param string $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -251,7 +251,7 @@ public function albumsSchema($rows, $userid) * @param string $rows array of data * @param int $userid user id * @param int $type type - * + * * @return array * * @since 1.0 @@ -380,7 +380,7 @@ public function fieldsSchema($rows, $userid, $type) * * @param string $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -616,7 +616,7 @@ public function streamSchema($rows, $userid) * * @param string $row array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -651,7 +651,7 @@ public function createLikeObj($row, $userid) * To build poll content * * @param int $pid poll id - * + * * @return array * * @since 1.0 @@ -732,7 +732,7 @@ public function createPollData($pid) * * @param object $poll poll object * @param array $opts opts array - * + * * @return array * * @since 1.0 @@ -764,7 +764,7 @@ public function createPollDataview($poll, $opts) * * @param date $date date * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -790,7 +790,7 @@ public function getOffsetServer($date, $userid) * @param array $row array of data * @param int $limitstart limitstart value * @param int $limit limit - * + * * @return array * * @since 1.0 @@ -858,7 +858,7 @@ public function createCommentsObj($row, $limitstart = 0, $limit = 10) * To build ablum object * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -903,7 +903,7 @@ public function discussionSchema($rows) * To build ablum object * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -938,7 +938,7 @@ public function discussionReply($rows) * Function for create category schema * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -970,7 +970,7 @@ public function categorySchema($rows) * * @param string $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -1148,7 +1148,7 @@ public function eventsSchema($rows, $userid) * * @param string $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -1161,26 +1161,19 @@ public function groupSchema($rows = null, $userid = 0) } $result = array(); - $user = JFactory::getUser($userid); - $user1 = FD::user($userid); - // Easysocial default profile - $profile = $user1->getProfile(); - $fmod_obj = new EasySocialModelFields; + $grp_model = ES::model('Groups'); foreach ($rows as $ky => $row) { - $group = ES::group($row->id); - $grp_model = FD::model('Groups'); - $steps = $grp_model->getAbout($group, $activeStep); - + $steps = $grp_model->getAbout($row); $fieldsArray = array(); // Get custom fields model. - $fieldsModel = FD::model('Fields'); + $fieldsModel = ES::model('Fields'); // Get custom fields library. - $fields = FD::fields(); + $fields = ES::fields(); $field_arr = array(); if ($steps) @@ -1197,7 +1190,7 @@ public function groupSchema($rows = null, $userid = 0) $fobj->title = JText::_($groupInfo->title); $fobj->field_name = JText::_($groupInfo->title); $fobj->step = $groupInfo->step_id; - $fobj->field_value = $groupInfo->output; + $fobj->field_value = isset($groupInfo->output)?$groupInfo->output:''; if ($fobj->unique_key == 'DESCRIPTION') { @@ -1228,7 +1221,6 @@ public function groupSchema($rows = null, $userid = 0) if (isset($row->id)) { - $grpobj = FD::group($row->id); $item = new GroupSimpleSchema; $item->id = $row->id; $item->type = 'group'; @@ -1245,10 +1237,9 @@ public function groupSchema($rows = null, $userid = 0) $item->category_id = $row->category_id; $item->category_name = $category->get('title'); $item->group_type = $row->type; - $item->cover = $grpobj->getCover(); - $grpobj = FD::group($row->id); - $x = $grpobj->cover->x; - $y = $grpobj->cover->y; + $item->cover = $row->getCover(); + $x = $row->cover->x; + $y = $row->cover->y; $item->cover_position = $x . '% ' . $y . '%'; $item->created_by = $row->creator_uid; $item->creator_name = JFactory::getUser($row->creator_uid)->username; @@ -1268,7 +1259,6 @@ public function groupSchema($rows = null, $userid = 0) } } - $grp_obj = FD::model('Groups'); $alb_model = FD::model('Albums'); $uid = $row->id . ':' . $row->title; $filters = array( @@ -1278,11 +1268,11 @@ public function groupSchema($rows = null, $userid = 0) // Get total album count $item->album_count = $alb_model->getTotalAlbums($filters); - $item->member_count = $grp_obj->getTotalMembers($row->id); - $item->isowner = $grp_obj->isOwner($userid, $row->id); - $item->ismember = $grp_obj->isMember($userid, $row->id); - $item->isinvited = $grp_obj->isInvited($userid, $row->id); - $item->approval_pending = $grp_obj->isPendingMember($userid, $row->id); + $item->member_count = $row->getTotalMembers($row->id); + $item->isowner = $row->isOwner($userid, $row->id); + $item->ismember = $row->isMember($userid, $row->id); + $item->isinvited = $row->isInvited($userid, $row->id); + $item->approval_pending = $row->isPendingMember($userid, $row->id); $result[] = $item; } } @@ -1295,7 +1285,7 @@ public function groupSchema($rows = null, $userid = 0) * * @param string $other_user_id other user id * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -1329,7 +1319,7 @@ public function profileSchema($other_user_id, $userid) * create badge object list * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -1360,7 +1350,7 @@ public function createBadge($rows) * function for create user schema * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -1387,7 +1377,7 @@ public function userSchema($rows) * * @param string $rows array of data * @param int $log_user user object - * + * * @return array * * @since 1.0 @@ -1437,7 +1427,7 @@ public function conversionSchema($rows, $log_user) * function for create message schema * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -1473,7 +1463,7 @@ public function messageSchema($rows) * To build ablum object * * @param string $rows array of data - * + * * @return array * * @since 1.0 @@ -1507,7 +1497,7 @@ public function replySchema($rows) * Calculate laps time * * @param date $date date - * + * * @return array * * @since 1.0 @@ -1526,7 +1516,7 @@ public function calLaps($date) * Create user object * * @param int $id id - * + * * @return array * * @since 1.0 @@ -1575,10 +1565,10 @@ public function createUserObj($id) } /** - * date formatting for ios 9 + * date formatting for ios 9 * * @param date $date date - * + * * @return array * * @since 1.0 @@ -1602,7 +1592,7 @@ public function listDate($date) * format date for event * * @param date $dt date - * + * * @return array * * @since 1.0 @@ -1618,7 +1608,7 @@ public function dateCreate($dt) * To build ablum object * * @param string $text text - * + * * @return array * * @since 1.0 @@ -1636,7 +1626,7 @@ public function sanitize($text) * * @param array $data array of data * @param array $user user object - * + * * @return array * * @since 1.0 @@ -1664,7 +1654,7 @@ public function frnd_nodes($data, $user) * function for getting polls * * @param array $rows array of data - * + * * @return array * * @since 1.0 @@ -1697,7 +1687,7 @@ public function pollsSchema($rows) * * @param array $rows array of data * @param int $userid user id - * + * * @return array * * @since 1.0 @@ -1783,7 +1773,7 @@ public function videosSchema($rows, $userid) * Function for mapping video object * * @param array $rows array of data - * + * * @return array * * @since 1.0 diff --git a/easysocial/libraries/uploadHelper.php b/easysocial/libraries/uploadHelper.php index 29e7d53..c005d9c 100644 --- a/easysocial/libraries/uploadHelper.php +++ b/easysocial/libraries/uploadHelper.php @@ -29,14 +29,14 @@ * * @since 1.0 */ -class EasySocialApiUploadHelper +class EasySocialApiUploadHelper extends JObject { /** * Method uploadCover photo * * @param integer $uid array of data * @param string $type object type - * + * * @return string * * @since 1.0 @@ -153,7 +153,7 @@ public function uploadCover($uid = 0,$type = SOCIAL_TYPE_USER) * * @param integer $log_usr user data * @param string $type type - * + * * @return string * * @since 1.0 @@ -264,23 +264,52 @@ public function uploadPhoto($log_usr = 0,$type = null) * Method To create temp group avtar data * * @param object $file file object - * + * * @return string * * @since 1.0 */ - public function ajax_avatar($file) + public function ajax_avatar($file, $fieldName = "avatar", $accessId = '', $type = '') { // Get the ajax library - $ajax = FD::ajax(); + $ajax = ES::ajax(); + $type = empty($type)? SOCIAL_TYPE_CLUSTERS : $type; + $accessId = empty($accessId)? ES::user()->id : $accessId; + + // Get user access + $access = ES::access($accessId, $type); + + // We need to perform sanity checking here + $options = array('name' => $fieldName, 'maxsize' => $access->get('photos.maxsize') . 'M', 'multiple' => false); + + + $uploader = ES::uploader($options); + $file = $uploader->getFile(null, 'image'); + + // If there was an error getting uploaded file, stop. + if ($file instanceof SocialException) + { + $this->setError($file->message); + + return false; + } + // Load up the image library so we can get the appropriate extension - $image = FD::image(); + $image = ES::image(); $image->load($file['tmp_name']); + + // Ensure that the image is valid. + if (!$image->isValid()) + { + $this->setError(JText::_("PLG_FIELDS_COVER_VALIDATION_INVALID_IMAGE")); + + return false; + } // Copy this to temporary location first $tmpPath = SocialFieldsUserAvatarHelper::getStoragePath('file'); - $tmpName = md5($file[ 'name' ] . 'file' . FD::date()->toMySQL()) . $image->getExtension(); + $tmpName = md5($file[ 'name' ] . 'file' . ES::date()->toMySQL()) . $image->getExtension(); $source = $file['tmp_name']; $target = $tmpPath . '/' . $tmpName; @@ -298,28 +327,41 @@ public function ajax_avatar($file) return $data; } - // To create temp group cover data /** * Method to create temp group cover data * * @param object $file file object * @param string $uname file name - * + * * @return string * * @since 1.0 */ - public function ajax_cover($file, $uname = 'cover_file') + public function ajax_cover($file, $uname = 'cover_file', $accessId = '', $type = '') { - /* - $cls_obj = new SocialFieldsUserCover(); - $cover_obj = $cls_obj->createCover($file,$uname); + $type = empty($type)? SOCIAL_TYPE_CLUSTERS : $type; + $accessId = empty($accessId)? ES::user()->id : $accessId; + + // Get user access + $access = ES::access($accessId, $type); + + // We need to perform sanity checking here + $options = array('name' => $uname, 'maxsize' => $access->get('photos.maxsize') . 'M', 'multiple' => false); + + + $uploader = ES::uploader($options); + $file = $uploader->getFile(null, 'image'); + + // If there was an error getting uploaded file, stop. + if ($file instanceof SocialException) + { + $this->setError($file->message); - return $cover_obj; - */ + return false; + } // Load our own image library - $image = FD::image(); + $image = ES::image(); // Generates a unique name for this image. $name = $file['name']; @@ -330,16 +372,16 @@ public function ajax_cover($file, $uname = 'cover_file') // Ensure that the image is valid. if (!$image->isValid()) { + $this->setError(JText::_("PLG_FIELDS_COVER_VALIDATION_INVALID_IMAGE")); + return false; - - // Need error code here } // Get the storage path $storage = SocialFieldsUserCoverHelper::getStoragePath($uname); // Create a new avatar object. - $photos = FD::get('Photos', $image); + $photos = ES::get('Photos', $image); // Create avatars $sizes = $photos->create($storage); @@ -371,7 +413,7 @@ public function ajax_cover($file, $uname = 'cover_file') * @param int $id user id * @param integer $log_usr user data * @param string $type type - * + * * @return string * * @since 1.0 @@ -540,7 +582,7 @@ public function albumPhotoUpload($id, $log_usr = 0, $type = null) * * @param integer $album_id object type * @param integer $log_usr array of data - * + * * @return string * * @since 1.0