name, (int)7);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('Successfully joined community.'));
}
drupal_goto('communities/'. $community_name);
}
/**
* Leave a community.
*
* @param String $community_name Community name
*/
function origo_home_community_leave($community_name) {
global $user;
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.change_group', $community_name, $user->name, (int)0);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('Community membership successfully cancelled.'));
}
drupal_goto('communities/'. $community_name);
}
/**
* Promote a member to admin.
*
* @param String $community_name Community name
* @param String $user_name User name
*/
function origo_home_community_promote($community_name, $user_name) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.change_group', $community_name, $user_name, (int)6);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('User successfully promoted.'));
}
drupal_goto('communities/'. $community_name);
}
/**
* Demote an admin to member.
*
* @param String $community_name Community name
* @param String $user_name User name
*/
function origo_home_community_demote($community_name, $user_name) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.change_group', $community_name, $user_name, (int)7);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('User successfully demoted.'));
}
drupal_goto('communities/'. $community_name);
}
/**
* Delete a community page.
*
* @param String $community_name Community name
* @return String Page
*/
function origo_home_community_delete_page($community_name) {
drupal_set_title('Are you sure you want to delete the community '. $community_name .'?');
$output = t('This step can not be undone.
');
$output .= drupal_get_form('origo_home_community_delete_form', $community_name);
return $output;
}
/**
* Form to delete a community.
*
* @param String $community_name Community name
*/
function origo_home_community_delete_form(&$form_state, $community_name) {
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
return $form;
}
/**
* Submit hook for community delete form.
*
* @param String $form_id Name of the form
* @param Array $form_value Form values
*/
function origo_home_community_delete_form_submit($form, &$form_state) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.delete', $form_state['values']['community_name']);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('Community successfully deleted.'));
}
drupal_goto('communities');
}
/**
* Show all communities.
* For Origo admins - show special operations.
*
* @return String Communities page
*/
function origo_home_communities_page() {
global $user;
drupal_set_title('Origo - Communities');
drupal_set_breadcrumb('');
$origo_admin = user_access('origo admin access');
$output = '';
$output .= t('A FAQ regarding communities is here: ');
$output .= l('Community FAQ', 'http://'. variable_get('origo_web_domain', '') .'/wiki/community_faq', array('absolute' => TRUE, 'alias' => TRUE));
$output .= t('
');
// Origo API call to get communities
$communities = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list');
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
$header = array(array('data' => t('Community'), 'colspan' => '2'));
$rows = array();
usort($communities, "_cmp_names");
foreach ($communities as $community) {
$link = l($community['name'], 'communities/'. $community['name']);
if ($origo_admin) {
$delete_link = l('[Delete]', 'communities/'. $community['name'] .'/delete');
}
else {
$delete_link = '';
}
$row = array();
$row[] = array(
'data' => $link,
// 'style' => 'height:35px'
);
$row[] = array(
'data' => $delete_link,
// 'style' => 'height:35px'
);
$rows[] = $row;
}
// Show table
$output .= theme('table', $header, $rows, array('style' => 'width:100%'));
}
return $output;
}
/**
* Display main page of a community.
*
* @param String $community_name Community name
* @return Community main üage
*/
function origo_home_community_main_page($community_name) {
// access permissions (were moved here from the menu-definition in `origo_home.module')
$is_community_admin = origo_home_community_is_community_admin($community_name);
$is_origo_admin = origo_home_community_is_origo_admin();
$is_member = origo_home_community_is_community_member($community_name);
// extract visitor object from global variables
$visitor = $GLOBALS['user'];
drupal_set_breadcrumb(array('Communities'));
$error = '';
// get specific data for this community
$community_info = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.retrieve', $community_name);
if ((int)xmlrpc_errno() > 0) {
$error .= t(xmlrpc_error_msg() .'
');
}
// get all administrators for this community
$admins = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list_members', $community_name, (int)6);
if ((int)xmlrpc_errno() > 0) {
$error .= t(xmlrpc_error_msg() .'
');
}
else {
usort($admins, "_cmp_names");
}
// get all members for this community
$members = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list_members', $community_name, (int)7);
if ((int)xmlrpc_errno() > 0) {
$error .= t(xmlrpc_error_msg() .'
');
}
else {
usort($members, "_cmp_names");
}
// get all projects associated with this community
$projects = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list_projects', $community_name);
if ((int)xmlrpc_errno() > 0) {
$error .= t(xmlrpc_error_msg() .'
');
}
else {
usort($projects, "_cmp_names");
}
// get all wiki pages for this community
$wiki_pages = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list_wiki_pages', $community_name);
if ((int)xmlrpc_errno() > 0) {
$error .= t(xmlrpc_error_msg() .'
');
}
else {
usort($wiki_pages, "_cmp_titles");
}
// check if any xmlrpc errors occured
if (drupal_strlen($error) > 0) {
$error_msg = t('The following error(s) occured:
');
$error_msg .= t($error);
drupal_set_message($error_msg, 'error');
return '';
}
$output = t('
'); // end community
return $output;
}
/**
* Display form to add a project to a community.
*
* @param String $community_name Community name
* @return String Form
*/
function origo_home_community_add_project_page($community_name) {
$output = drupal_get_form('origo_home_community_add_project_form', $community_name);
return $output;
}
/**
* Form to add a project to a community.
*
* @param String $community_name Community name
*/
function origo_home_community_add_project_form(&$form_state, $community_name) {
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name,
);
$form['project_name'] = array(
'#type' => 'textfield',
'#title' => t('Project Name'),
'#size' => 60,
'#maxlength' => 50,
'#description' => t('Project to be associated with this community'),
'#attributes' => NULL,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Project'),
);
return $form;
}
/**
* Submit hook for form to add a project to a community.
*
* @param String $form_id Form name
* @param Array $edit Form values
*/
function origo_home_community_add_project_form_submit($form, &$form_state) {
$result = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.add_project', $form_state['values']['community_name'], $form_state['values']['project_name']);
if ((int)xmlrpc_errno() > 0) {
form_set_error('project_name', t(xmlrpc_error_msg()));
}
else {
drupal_set_message(t('Project successfully added.'));
drupal_goto('communities/'. $form_state['values']['community_name']);
}
}
/**
* Remove a project from a community.
*
* @param String $community_name Community name
* @param String $project_name Project name
*/
function origo_home_community_remove_project($community_name, $project_name) {
$result = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.remove_project', $community_name, $project_name);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
drupal_goto('communities/'. $community_name);
}
else {
drupal_set_message(t('Project successfully removed.'));
drupal_goto('communities/'. $community_name);
}
}
/**
* Display form to edit a community description.
*
* @return String Page
*/
function origo_home_community_change_description_page($community_name) {
$output = drupal_get_form('origo_home_community_change_description_form', $community_name);
return $output;
}
/**
* Form to edit community description.
*
* @param String $community_name Community name
* @return String Form
*/
function origo_home_community_change_description_form(&$form_state, $community_name) {
$community = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.retrieve', $community_name);
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Community Description'),
'#default_value' => t($community['description']),
'#description' => t('You can use wiki syntax.'),
'#required' => TRUE,
'#cols' => 60,
'#rows' => 20,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Submit hook for community edit description form.
*
* @param String $form_id Form name
* @param Array $form_values Form values
*/
function origo_home_community_change_description_form_submit($form, &$form_state) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.change_description', $form_state['values']['community_name'], $form_state['values']['description']);
if ((int)xmlrpc_errno() > 0) {
form_set_error('description', t('The following error occured: '. xmlrpc_error_msg()));
}
else {
drupal_set_message(t('Description saved.'));
drupal_goto('communities/'. $form_state['values']['community_name']);
}
}
/**
* Show all wiki pages of a community.
*
* @param String $community_name Community name
* @return String List of all wiki pages
*/
function origo_home_community_list_wiki_page($community_name) {
global $user;
drupal_set_title('Wiki pages for '. drupal_ucfirst($community_name) .' Community');
$output = '';
// get all wiki pages for this community
$wiki_pages = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.list_wiki_pages', $community_name);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
usort($wiki_pages, "_cmp_titles");
$header = array(array('data' => t('Wiki Page'), 'colspan' => '2'));
$rows = array();
foreach ($wiki_pages as $wiki_page) {
$link = l($wiki_page['title'], 'communities/'. $community_name .'/wiki/'. $wiki_page['title']);
if (origo_home_community_is_origo_admin() || origo_home_community_is_community_admin($community_name)) {
$delete_link = l('[delete]', 'communities/'. $community_name .'/wiki/'. $wiki_page['title'] .'/delete');
}
else {
$delete_link = '';
}
$row = array();
$row[] = array(
'data' => $link,
// 'style' => 'height:35px'
);
$row[] = array(
'data' => $delete_link,
// 'style' => 'height:35px'
);
$rows[] = $row;
}
// Show table
$output .= theme('table', $header, $rows, array('style' => 'width:100%'));
}
return $output;
}
/**
* Display a wiki page of a community.
*
* @param String $community_name Community name
* @param String $wiki_page Wiki page title
* @return String Wiki page
*/
function origo_home_community_show_wiki_page($community_name, $wiki_page) {
// get data from backend
$wiki_page = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.retrieve_wiki_page', $community_name, $wiki_page);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
return '';
}
if ($wiki_page['is_private']) {
drupal_set_title($wiki_page['title'] .' ');
}
else {
drupal_set_title($wiki_page['title']);
}
$output = check_markup($wiki_page['text'], 'wiki', FALSE);
return $output;
}
/**
* Display form to edit a wiki page.
*
* @param String $community_name Community name
* @param String $wiki_page Wiki page title
* @return String Form
*/
function origo_home_community_edit_wiki_page($community_name, $wiki_page) {
$output = drupal_get_form('origo_home_community_edit_wiki_form', $community_name, $wiki_page);
return $output;
}
/**
* Form to edit a wiki page.
*
* @param String $community_name Community name
* @param String $wiki_page Wiki page name
* @return String Form
*/
function origo_home_community_edit_wiki_form(&$form_state, $community_name, $wiki_page) {
$community = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.retrieve_wiki_page', $community_name, $wiki_page);
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name,
);
$form['old_title'] = array(
'#type' => 'hidden',
'#value' => $wiki_page,
);
$form['old_text'] = array(
'#type' => 'hidden',
'#value' => $community['text'],
);
$form['old_private'] = array(
'#type' => 'hidden',
'#value' => $community['is_private'],
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => t($community['title']),
'#required' => TRUE,
'#maxlength' => 250,
);
$form['text'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#default_value' => t($community['text']),
'#cols' => 60,
'#rows' => 20,
);
$form['private'] = array(
'#type' => 'checkbox',
'#title' => t('Private'),
'#description' => t('Check here if this content should be set private and only shown to community administrators and members.'),
'#default_value' => t($community['is_private']),
);
// $form['preview'] = array(
// '#type' => 'button',
// '#value' => t('Preview'),
// );
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['delete'] = array(
'#type' => 'button',
'#value' => t('Delete'),
);
return $form;
}
/**
* Validate hook for edit wiki page form.
*
* @param String $form_id Form name
* @param Array $form_values Form values
*/
function origo_home_community_edit_wiki_form_validate($form, &$form_state) {
// check for operation
switch ($form_state['values']['op']) {
// case 'Preview':
// break;
case 'Submit':
// check if title changed
$title = $form_state['values']['old_title'];
if ($form_state['values']['old_title'] != $form_state['values']['title']) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.rename_wiki_page', $form_state['values']['community_name'], $form_state['values']['old_title'], $form_state['values']['title']);
if ((int)xmlrpc_errno() > 0) {
form_set_error('title', t('The following error occured: '. xmlrpc_error_msg()));
}
// update title
$title = $form_state['values']['title'];
}
// edit wiki page if necessary
$private = FALSE;
if ($form_state['values']['private'] == 1) {
$private = TRUE;
}
if ($form_state['values']['old_text'] != $form_state['values']['text'] || $form_state['values']['old_private'] != $private) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.edit_wiki_page', $form_state['values']['community_name'], $title, $form_state['values']['text'], $private);
if ((int)xmlrpc_errno() > 0) {
form_set_error('text', t('The following error occured: '. xmlrpc_error_msg()));
}
else {
drupal_set_message(t('The Wiki Page has been updated.'));
}
}
drupal_goto('communities/'. $form_state['values']['community_name'] .'/wiki/'. $form_state['values']['title']);
break;
case 'Delete':
// go to delete handler
drupal_goto('communities/'. $form_state['values']['community_name'] .'/wiki/'. $form_state['values']['title'] .'/delete');
break;
default:
drupal_set_message(t('Something went wrong.'), 'error');
break;
}
}
/**
* Delete a wiki page.
*
* @param String $community_name Community name
* @param String $wiki_page Wiki page name
* @return String Page
*/
function origo_home_community_delete_wiki_page($community_name, $wiki_page) {
drupal_set_title('Are you sure you want to delete '. $wiki_page .'?');
$output = t('This action can not be undone.
');
$output .= drupal_get_form('origo_home_community_delete_wiki_form', $community_name, $wiki_page);
return $output;
}
/**
* Form to delete a wiki page.
*
* @param String $community_name Community name
* @param String $wiki_page Wiki page name
*/
function origo_home_community_delete_wiki_form(&$form_state, $community_name, $wiki_page) {
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name,
);
$form['wiki_page'] = array(
'#type' => 'hidden',
'#value' => $wiki_page,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete')
);
return $form;
}
/**
* Submit hook for wiki page delete form.
*
* @param String $form_id Name of the form
* @param Array $form_value Form values
*/
function origo_home_community_delete_wiki_form_submit($form, &$form_state) {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.delete_wiki_page', $form_state['values']['community_name'], $form_state['values']['wiki_page']);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
else {
drupal_set_message(t('Wiki page successfully deleted.'));
}
drupal_goto('communities/'. $form_state['values']['community_name']);
}
/**
* Display form to add a wiki page.
*
* @param String $community_name Community name
* @return String Form
*/
function origo_home_community_add_wiki_page($community_name) {
$output = drupal_get_form('origo_home_community_add_wiki_form', $community_name);
return $output;
}
/**
* Form to edit a wiki page.
*
* @param String $community_name Community name
* @return String Form
*/
function origo_home_community_add_wiki_form(&$form_state, $community_name) {
$form['community_name'] = array(
'#type' => 'hidden',
'#value' => $community_name,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
'#maxlength' => 250,
);
$form['text'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#cols' => 60,
'#rows' => 20,
);
$form['private'] = array(
'#type' => 'checkbox',
'#title' => t('Private'),
'#description' => t('Check here if this content should be set private and only shown to community administrators and members.'),
);
// $form['preview'] = array(
// '#type' => 'button',
// '#value' => t('Preview'),
// );
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Validate hook for community add wiki form.
*
* @param String $form_id Form name
* @param Array $form_state['values'] Form values
*/
function origo_home_community_add_wiki_form_validate($form, &$form_state) {
// check for operation
switch ($form_state['values']['op']) {
// case 'Preview':
// break;
case 'Submit':
$private = FALSE;
if ($form_state['values']['private'] == 1) {
$private = TRUE;
}
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'community.add_wiki_page', $form_state['values']['community_name'], $form_state['values']['title'], $form_state['values']['text'], $private);
if ((int)xmlrpc_errno() > 0) {
form_set_error('title', t('The following error occured: '. xmlrpc_error_msg()));
}
else {
drupal_set_message(t('The Wiki Page has been created.'));
drupal_goto('communities/'. $form_state['values']['community_name'] .'/wiki/'. $form_state['values']['title']);
}
break;
default:
drupal_set_message(t('Something went wrong.'), 'error');
break;
}
}
/**
* Utility function for checking if the user currently logged-in is an Origo-admin.
* @return Boolean
*/
function origo_home_community_is_origo_admin() {
return user_access('origo admin access');
}
/**
* Utility function for checking if the user currently logged-in has
* admin-rights on the community $community_name.
* @param $community_name String denoting the name of the community of interest.
* @return Boolean
*/
function origo_home_community_is_community_admin($community_name) {
// is user admin of current community?
$is_community_admin = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'authorization.is_allowed_community', 'is_community_admin' , $community_name);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
return $is_community_admin;
}
/**
* Utility function for checking if the user currently logged-in is a member of
* the community $community_name.
* @param $community_name String denoting the name of the community of interest.
* @return Boolean
*/
function origo_home_community_is_community_member($community_name) {
$is_member = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'authorization.is_allowed_community', 'is_community_member' , $community_name);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
}
return $is_member;
}