'. l('Create Project', 'origo_admin/create_project') .'';
$output .= '
'. l('Remove Project', 'origo_admin/remove_project') .'
';
$output .= ''. l('Change Project', 'origo_admin/change_project_type') .'
';
$output .= ''. l('Mass Mail', 'origo_admin/massmail') .'
';
$output .= ''. l('System Status', 'origo_admin/status') .'
';
return $output;
}
function origo_admin_status_page() {
$status = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'origo_system.status');
if ((int)xmlrpc_errno() > 0) {
$output .= xmlrpc_error_msg();
}
//$output .= 'System Status
';
$output .= nl2br($status);
return $output;
}
function origo_admin_massmail_page() {
$output = drupal_get_form('origo_admin_massmail_form');
return $output;
}
function origo_admin_massmail_form(&$form_state) {
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 60,
'#maxlength' => 64,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#cols' => 60,
'#rows' => 10,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send mail'),
);
return $form;
}
// TODO: delete this or add some logic
function origo_admin_massmail_form_validate($form, &$form_state) {
}
function origo_admin_massmail_form_submit($form, &$form_state) {
// Prepare the sender:
$subject = html_entity_decode(strip_tags($form_state['values']['subject']), ENT_QUOTES);
$message = html_entity_decode(strip_tags($form_state['values']['message']), ENT_QUOTES);
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'origo_system.mail_all', $subject, $message);
if ((int)xmlrpc_errno() > 0) {
form_set_error('subject', t(xmlrpc_error_msg()));
}
else {
drupal_set_message(t('Message sent.'));
}
drupal_goto('origo_admin/massmail');
}
/**
* Displays the page to create a new project.
* @param $name Optional name of the project to create.
* A set value indicated that the project-creation request was Origo-user initiated.
* @return The rendered form.
*/
function origo_admin_create_project_page($name = NULL) {
$output = drupal_get_form('origo_admin_create_project_form', $name);
return $output;
}
function origo_admin_create_project_form(&$form_state, $name = NULL) {
$default_user_name = '';
$default_project_name = '';
$default_description = '';
$default_project_type = ORIGO_OPEN_SOURCE;
$default_rcs_type = ORIGO_REPO_SVN;
$default_visibility = ORIGO_PROJECT_VISIBLE;
if (isset($name) && $name != '') {
// A projectname was passed. This indicates that the request was Origo-user initiated,
// i.e. came from an external link that was mailed to Origo administrators.
// Note: this was formerly known as function "origo_admin_create_project_from_request_form", and got merged.
// We now load preset properties from the backend, and prepare some special fields to use in the submit-handler.
$request = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'project.request_retrieve', $name);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message('Retrieving project creation request failed:'. t(xmlrpc_error_msg()), 'error');
}
else {
// fetch default values
$default_user_name = $request['user_name'];
$default_project_name = $request['name'];
$default_description = $request['description'];
$default_project_type = $request['project_type'];
$default_rcs_type = $request['rcs_type'];
$default_visibility = $request['project_visibility'];
// form elements
$form['user_email'] = array(
'#type' => 'hidden',
'#value' => $request['user_email'],
);
$email_text = "Hi\r\n\r\n"
."Your project has been created and can be found at ". get_origo_project_link($request['name']) ."\r\n"
."If you encounter any problems or have questions please feel free to contact us.\r\n\r\n"
."Origo Team";
}
}
$form['projectname'] = array(
'#prefix' => "",
'#type' => 'textfield',
'#title' => t('Projectname'),
// '#size' => 30,
'#maxlength' => 64,
'#description' => t('No spaces, only a-z, 0-9 and - are allowed. Max. 50 characters.'),
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $default_project_name,
);
$form['user_name'] = array(
'#type' => 'textfield',
'#title' => t('Owner'),
// '#size' => 30,
'#maxlength' => 64,
'#description' => NULL,
'#required' => FALSE,
'#default_value' => $default_user_name,
'#suffix' => "
",
);
$form['description'] = array(
'#prefix' => "",
'#type' => 'textarea',
'#title' => t('Description'),
// '#cols' => 60,
'#rows' => 10,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $default_description,
);
if (isset($email_text)) {
// include the mail-body textfield
$form['mailtext'] = array(
'#type' => 'textarea',
'#title' => t('Mailtext'),
// '#cols' => 60,
'#rows' => 10,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $email_text,
'#suffix' => "
",
);
}
else {
$form['description']['#suffix'] = "";
}
$options = array(ORIGO_CLOSED_SOURCE => t('Closed Source'), ORIGO_OPEN_SOURCE => t('Open Source'));
$form['type'] = array(
'#prefix' => "",
'#type' => 'radios',
'#title' => t('Type'),
'#options' => $options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $default_project_type,
);
$options = array(ORIGO_REPO_SVN => t('Subversion'));
$form['rcs_type'] = array(
'#type' => 'radios',
'#title' => t('Repository Type'),
'#options' => $options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $default_rcs_type,
);
$options = array(ORIGO_PROJECT_VISIBLE => t('Public'), ORIGO_PROJECT_HIDDEN => t('Hidden'));
$form['visibility'] = array(
'#type' => 'radios',
'#title' => t('Visibility'),
'#options' => $options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $default_visibility,
'#suffix' => "
",
);
$form['submit'] = array(
'#prefix' => '',
'#suffix' => '
',
'#type' => 'submit',
'#value' => t('Create Project'),
'#weight' => 1,
'#submit' => array('origo_admin_create_project_form_submit'), // If no javascript action.
'#ahah' => array(
'event' => 'click',
'path' => 'origo_home/create_project_ahah',
'wrapper' => 'progressbar-wrapper',
'method' => 'replace',
'progress' => array(
'type' => 'bar',
'message' => t('Creating...')
),
),
);
return $form;
}
/**
* Callback function for project creation
* While submitting the form, i.e. starting project creation,
* a progress bar is displayed. When the submit-handler returns,
* display a link to the new project.
*
* Note: for the form-processing part, see http://drupal.org/node/331941
*/
function origo_admin_create_project_ahah() {
// retireve $form values
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
// Preparing for processing
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Process the form, thus populating $form_state and calling submit-handlers
drupal_process_form($form_id, $form, &$form_state);
$projectname = $form_state['values']['projectname'];
$pname_regex_result = preg_match('/^([A-Za-z])([A-Za-z]|[0-9]|\-){1,48}([A-Za-z]|[0-9])$/', $projectname);
$p_description_length = drupal_strlen($form_state['values']['description']);
$output = "";
// Check if and which errors should be shown
if ($pname_regex_result && $p_description_length > 0) {
$output = 'You may now continue to the '. l('new project', get_origo_project_link($projectname)) .'.
';
drupal_get_messages("", TRUE);
}
else {
$output .= '';
if (!$p_description_length > 0) {
$output .= 'Project Description is required.
';
}
if (!drupal_strlen($projectname) > 0) {
$output .= 'Projectname is required.
';
}
if (!$pname_regex_result && drupal_strlen($projectname) > 0) {
$output .= 'Projectname is not valid.
';
}
$output .= 'Please refresh and re-enter the values correctly.
' . '
';
drupal_get_messages("", TRUE);
}
return drupal_json(array('status' => TRUE, 'data' => $output));
}
/**
* Submit handler for project creation.
* Calls the backend to crate a new project, and sleeps a configurable amount of time before returning.
* Optionally sends an email to the owner, if requested.
*/
function origo_admin_create_project_form_submit($form, &$form_state) {
// Prepare the sender:
$description = html_entity_decode(strip_tags($form_state['values']['description']), ENT_QUOTES);
$owner = $form_state['values']['user_name'];
$project_type = (int)$form_state['values']['type'];
$rcs_type = (int)$form_state['values']['rcs_type'];
$project_visibility = (int)$form_state['values']['visibility'];
$form_state['values']['projectname'] = drupal_strtolower($form_state['values']['projectname']);
// call the backend, thus starting project creation.
origo_auth_xmlrpc_session(
variable_get('origo_api', ''),
'project.add',
$form_state['values']['projectname'],
$description,
$owner,
$project_type,
$rcs_type,
$project_visibility
);
if ((int)xmlrpc_errno() > 0) {
form_set_error('projectname', t(xmlrpc_error_msg()));
}
else {
// wait some time before continuing, thus giving the install-script enough time to create the files, database, etc.
sleep(variable_get('origo_project_creation_timeout', 30));
// if this creation-request was user-initiated, we send out an email.
if (isset($form_state['values']['mailtext'])) {
$from = variable_get('origo_support_email', '');
$to = $form_state['values']['user_email'];
$subject = 'Origo - Project '. $form_state['values']['projectname'] .' created';
$mailtext = html_entity_decode(strip_tags($form_state['values']['mailtext']), ENT_QUOTES);
if ($to != '') {
origo_mail('createproject', $to, $subject, $form_state['values']['mailtext'], $from, array(), '-f '. $from);
if ((int)xmlrpc_errno() > 0) {
drupal_set_message('Sending mail failed: '. t(xmlrpc_error_msg()), 'error');
}
}
else {
drupal_set_message('Sending mail omitted because no recipient was known.', 'error');
}
}
drupal_set_message(t('Created project:') .' '. $form_state['values']['projectname']);
}
}
function origo_admin_remove_project_page() {
$output = drupal_get_form('origo_admin_remove_project_form');
return $output;
}
function origo_admin_remove_project_form(&$form_state) {
$form['projectname'] = array(
'#type' => 'textfield',
'#title' => t('Projectname'),
'#size' => 60,
'#maxlength' => 64,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Remove Project'),
);
return $form;
}
/**
* Validation function for the project-removal form.
* Ensures that the project isn't deleted from its own site (see #682).
*/
function origo_admin_remove_project_form_validate($form, &$form_state) {
$project_name = variable_get('origo_project_name', '');
if ($project_name == $form_state['values']['projectname']) {
// can't delete the current project
form_set_error('projectname', "Can't delete $project_name from the project's own site. Choose another project or delete the project from another location");
}
}
function origo_admin_remove_project_form_submit($form, &$form_state) {
$project_id = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'project.retrieve_id', $form_state['values']['projectname']);
if ((int)xmlrpc_errno() > 0) {
form_set_error('projectname', t(xmlrpc_error_msg()));
}
else {
origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'project.remove', $project_id);
if ((int)xmlrpc_errno() > 0) {
form_set_error('projectname', t(xmlrpc_error_msg()));
}
else {
drupal_set_message(t('Removed project:') .' '. $form_state['values']['projectname']);
}
}
sleep(2);
drupal_goto('origo_admin/remove_project');
}
/**
* Form-builder function for changing a project's type.
* Depending on the state of `$form_state', this will render different
* form-fields, namely radio-buttons to enter a (new) project-type.
*/
function origo_admin_change_project_type_form(&$form_state) {
$form['project_name'] = array(
'#prefix' => '',
'#type' => 'textfield',
'#title' => t('Projectname'),
'#size' => 40,
'#maxlength' => 255,
'#multiple' => FALSE,
'#attributes' => NULL,
'#default_value' => $form_state['values']['project_name'],
'#submit' => array('origo_admin_change_project_type_form_submit'), // If no javascript action.
);
$submit_title = t('Check project');
$project_id = $form_state['storage']['project_id'][0];
if (isset($project_id) && $project_id > 0) {
$project_settings = origo_auth_xmlrpc_session(variable_get('origo_api', ''), 'project.retrieve_settings', $project_id);
if ((int)xmlrpc_errno() > 0) {
form_set_error('project_name', t(xmlrpc_error_msg()));
}
else {
// both calls succeeded, add the new form fields and set the default-values
$type_options = array(ORIGO_OPEN_SOURCE => t('Open Source'), ORIGO_CLOSED_SOURCE => t('Closed Source'));
$form['options']['type'] = array(
'#type' => 'radios',
'#title' => t('Type'),
'#options' => $type_options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $project_settings['project_type'],
);
$rcs_type_options = array(ORIGO_REPO_SVN => t('Subversion'));
$form['options']['rcs_type'] = array(
'#type' => 'radios',
'#title' => t('Repository Type'),
'#options' => $rcs_type_options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => (int)$project_settings['rcs_type'],
);
$visibility_options = array(ORIGO_PROJECT_VISIBLE => t('Public'), ORIGO_PROJECT_HIDDEN => t('Hidden'));
$form['options']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Visibility'),
'#options' => $visibility_options,
'#description' => NULL,
'#attributes' => NULL,
'#required' => TRUE,
'#default_value' => $project_settings['project_visibility'],
);
$submit_title = t('Change Project');
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => $submit_title,
'#suffix' => '
',
'#ahah' => array(
'event' => 'change',
'path' => 'ajax/ahah-change-project',
'keypress' => False,
'wrapper' => 'my-wrapper',
'method' => 'replace',
),
);
return $form;
}
/*
* AHAH callback for the `change-project-type' form. This function will cause
* the form to be submitted and rebuild.
*/
function ahah_change_project_callback() {
// We're starting in step #3, preparing for #4.
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Step #4.
$form = form_get_cache($form_build_id, $form_state);
// Preparing for #5.
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Step #5.
drupal_process_form($form_id, $form, $form_state);
// Step #6 and #7 and #8.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
unset($form['project_name']['#prefix'], $form['project_name']['#suffix']); // Prevent duplicate wrappers.
$output = theme('status_messages') . drupal_render($form);
// Final rendering callback.
print drupal_json(array('status' => TRUE, 'data' => $output));
exit();
}
/**
* Submit handler for the `change project type' form.
* This function will be called multiple times through the AHAH callback,
* we thus have to react on the data in $form_state and act accordingly.
*/
function origo_admin_change_project_type_form_submit($form, &$form_state) {
$project_id = $form_state['storage']['project_id'][0];
if (isset($project_id) && !empty($project_id)) {
// We have a project-ID, which was set in a previous call to this function.
// => Submit change to backend
origo_auth_xmlrpc_session(
variable_get('origo_api_internal', ''),
'internal_project.change_type',
(int)$project_id,
(int)$form_state['values']['type'],
(int)$form_state['values']['rcs_type'],
(int)$form_state['values']['visibility']);
if ((int)xmlrpc_errno() > 0) {
// something went wrong
drupal_set_message(t('The following error occured: '. xmlrpc_error_msg()), 'error');
unset($form_state['storage'], $form_state['values']['project_name']);
$form_state['storage']['backend_ok'] = array(False);
}
else {
// all ok
drupal_set_message(t('The project type has been successfully changed.'));
$form_state['storage'] = array('backend_ok' => array(True));
}
}
else {
// No project-ID set: was a project_name entered?
$project_name = $form_state['values']['project_name'];
if (isset($project_name) && !empty($project_name)) {
// Indeed it was, get ID from the backend
$project_id = origo_auth_xmlrpc_session(
variable_get('origo_api', ''),
'project.retrieve_id',
$project_name);
if ((int)xmlrpc_errno() > 0) {
form_set_error('project_name', t(xmlrpc_error_msg()));
}
else {
// store the project-ID in the form-data
$form_state['storage']['project_id'] = array($project_id);
}
}
}
}
/**
* Display a page for an Origo administrator to disable any user account.
*
* @param string $account_name The account name to disable
* @return string/HTHM The rendered form
*/
function origo_admin_disable_user_page($account_name) {
global $user;
$output = t('Well hello there, dear Administrator!
');
$output = t('Do you really want to permanently disable the account "'. $account_name .'"?
');
$output .= t('This action can not be undone!');
$output .= drupal_get_form('origo_home_settings_disable_account_form', $account_name);
return $output;
}