Location: PHPKode > projects > Volunteer Management OpenSource Software > vmoss_alpha02/mod/admin/vmoss_admin.inc
<?php

/**
 * VMOSS-specific Admin Functionality
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to LGPL license
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html
 *
 * @author       Giovanni Capalbo
 * @copyright    Lanka Software Foundation - http://www.opensource.lk
 * @copyright    Trinity Humanitarian-FOSS Project - http://www.cs.trincoll.edu/hfoss
 * @license        http://www.gnu.org/copyleft/lesser.html GNU Lesser General
 * Public License (LGPL)
 */

require_once $global['approot']."inc/lib_form.inc";
require_once $global['approot']."inc/lib_security/constants.inc";

/**
 * Displays a list of organizations
 */

function shn_admin_vmoss_view_orgs() {
	global $global;
	$db = $global['db'];
	$result = $db->execute("SELECT o_uuid, name FROM org_main");
	if(!$result->EOF) {
		?>
		<h2 style="text-align: center;">Organizations</h2>
		<table align="center">
			<thead>
				<tr>
					<td>Name</td>
					<td>Edit</td>
				</tr>
			</thead>
			<tbody>
		<?php
		while(!$result->EOF) {
			echo '<tr><td>'.$result->fields['name'].'</td>';
			echo '<td>[<a href="index.php?mod=admin&amp;act=vmoss_edit_org&amp;o_uuid='.$result->fields['o_uuid'].'">edit</a>]</td></tr>';
			$result->moveNext();
		}
		?>
			</tbody>
		</table>
		<?php
	} else {
		?>
		<center>
			<h2>Organizations</h2>
			<br />
			<i>There are no registered organizations.</i>
		</center>
		<?php
	}

	?>
	<br />
	<br />
	<center>
		<a href="index.php?mod=admin&amp;act=vmoss_add_org">Register Organization</a>
	</center>
	<?php
}

/**
 * Displays a form to add an organization.
 *
 * @return void
 */

function shn_admin_vmoss_add_org() {
	echo '<h2 style="text-algin: center">Register Organization</h2><br />';
	shn_form_fopen("vmoss_process_add_org");
		shn_form_fsopen("Register Organization");
			shn_form_text("Name", "org_name", "", array("req" => true, "value" => isset($_POST['org_name'])?$_POST['org_name']:''));
		shn_form_fsclose();
		shn_form_submit("Register");
		shn_form_button('Cancel', 'onClick="window.location=\'index.php?mod=admin&amp;act=vmoss_view_orgs\'"');
	shn_form_fclose();
}

/**
 * Validates and processes form data from shn_admin_vmoss_add_org()
 *
 * @return void
 */

function shn_admin_vmoss_process_add_org() {
	$org_name = $_POST['org_name'];
	if(trim($org_name) == '') {
		add_error("Please enter a name");
		shn_admin_vmoss_add_org();
	} else {
		global $global;
		require_once $global['approot']."inc/lib_uuid.inc";
		$db = $global['db'];
		$id = shn_create_uuid('organization');
		$result = $db->execute("INSERT INTO org_main (o_uuid, name) VALUES ('$id', '$org_name')");
		if($result === false) {
			add_error("A database error has occurred. Please try again.");
			shn_admin_vmoss_add_org();
		} else {
			add_confirmation("Organization registered successfully");
		}
		shn_admin_vmoss_view_orgs();
	}
}

/**
 * Displays a form to edit an organization's name.
 *
 * @return void
 */

function shn_admin_vmoss_edit_org() {
	global $global;
	$o_uuid = $_REQUEST['o_uuid'];
	$db = $global['db'];
	$result = $db->execute("SELECT name FROM org_main WHERE o_uuid = '$o_uuid'");
	if($result->EOF) {
		add_error("The given organization was not found");
		shn_admin_vmoss_view_orgs();
	} else {
		echo '<h2 style="text-align: center">Edit Organization</h2><br />';
		shn_form_fopen("vmoss_process_edit_org");
			shn_form_fsopen("Edit Organization");
				shn_form_text("Name", "org_name", "", array("req" => true, "value" => $result->fields['name']));
				shn_form_hidden(array('o_uuid' => $o_uuid));
			shn_form_fsclose();
			shn_form_submit('Save');
			shn_form_button('Cancel', 'onClick="window.location=\'index.php?mod=admin&amp;act=vmoss_view_orgs\'"');
		shn_form_fclose();
	}
}

/**
 * Validates and processes form data from shn_admin_vmoss_add_org()
 *
 * @return void
 */

function shn_admin_vmoss_process_edit_org() {
	$org_name = $_POST['org_name'];
	$o_uuid = $_POST['o_uuid'];
	if(trim($org_name) == '') {
		add_error("Please enter a name");
		shn_admin_vmoss_edit_org();
	} else {
		global $global;
		require_once $global['approot']."inc/lib_uuid.inc";
		$db = $global['db'];
		$result = $db->execute("UPDATE org_main SET name = '$org_name' WHERE o_uuid = '$o_uuid'");
		if($result === false) {
			add_error("A database error has occurred. Please try again.");
			shn_admin_vmoss_add_org();
		} else {
			add_confirmation("Organization edited successfully");
		}
		shn_admin_vmoss_view_orgs();
	}
}

/**
 * Displays form to add a system administrator
 *
 * @return void
 */

function shn_admin_vmoss_add_sysadmin() {
	echo '<h2 style="text-align: center">Add System Administrator</h2><br />';
	shn_form_fopen("vmoss_process_add_sysadmin");
		shn_form_fsopen("Personal Information");
			?><label>What is this?</label><?php
			shn_form_extra_opts(array('help' => 'Use this form to create a system-wide administrator. A VM administrator only has access to everything within the Volunteer Management portion of the site. A system-wide administrator, such as yourself, also has access to the Administration portion of the site.'));
			shn_form_text("Name", "name", "", array("req" => true, 'help' => 'This is the administrator\'s name.'));
			shn_form_text("Username", "username", "", array("req" => true, 'help' => 'This is the username to give to the administrator.'));
			shn_form_password("Password", "pass1", "", array("req" => true, 'help' => 'This is the password that the administrator will use to log into the system.'));
			shn_form_password("Password (again)", "pass2", "", array("req" => true));
		shn_form_fsclose();
		shn_form_submit('Save');
		shn_form_button('Cancel', 'onClick="window.location=\'index.php?mod=admin\'"');
	shn_form_fclose();
}

/**
 * Validates and processes form data from shn_admin_vmoss_add_sysadmin
 *
 * @return void
 */

function shn_admin_vmoss_process_add_sysadmin() {
	global $global;
	$db = $global['db'];
	require_once($global['approot'].'mod/vm/lib/Constants.php');
	$account_name = $_POST['name'];
	$user_name = $_POST['username'];
	$user_password = $_POST['pass1'];
	$pass2 = $_POST['pass2'];
	$error = false;
	if(trim($account_name == '')) {
		add_error("Please specify an account name.");
		$error = true;
	}
	if(trim($user_name == '')) {
		add_error("Please specify a username.");
		$error = true;
	}
	if(trim($user_password) == '') {
		add_error("Please specify a password.");
		$error = true;
	}
	if($user_password != $pass2) {
		add_error("Passwords do not match.");
		$error = true;
	}

	if(!$error) {
		if(shn_auth_add_user($account_name, $user_name, $user_password, SUPERUSER)) {
			$result = $db->execute("SELECT p_uuid FROM users WHERE user_name = '$user_name'");
			$p_uuid = $result->fields['p_uuid'];
			$db->execute("INSERT INTO vm_user_role (fk_user, fk_role) VALUES ('$p_uuid', ".VM_ROLE_VM_ADMIN.")");
			add_confirmation("Administrator account added successfully.");
		} else {
			shn_admin_vmoss_add_sysadmin();
		}
	} else {
		shn_admin_vmoss_add_sysadmin();
	}
}
?>
Return current item: Volunteer Management OpenSource Software