Location: PHPKode > projects > Jethro Pastoral Ministry Manager > jethro-pmm/include/installer.class.php
<?php
require_once 'db_objects/staff_member.class.php';
require_once 'db_objects/family.class.php';
require_once 'db_objects/congregation.class.php';

class Installer
{
	var $initial_person_fields = Array('first_name', 'last_name', 'gender', 'username', 'password', 'email');
	var $congregationids = Array();

	function run()
	{
		$sql = 'SELECT count(*) FROM PERSON';
		$res = $GLOBALS['db']->queryOne($sql);
		if (!PEAR::isError($res)) {
			trigger_error('System has already been installed, installer is aborting');
			exit();
		}
		include 'templates/installer.template.php';
	}


	function printBody()
	{
		if ($this->readyToInstall()) {
			require_once dirname(__FILE__).'/system_controller.class.php';
			$GLOBALS['system'] =& new System_Controller();
			
			$GLOBALS['JETHRO_INSTALLING'] = 1;
			$this->initDB();
			$this->initCongregations();
			$this->initUser();
			unset($GLOBALS['JETHRO_INSTALLING']);

			$this->printConfirmation();
		} else {
			$this->printForm();
		}
	}

	function readyToInstall()
	{
		if (empty($_POST)) {
			return FALSE;
		}

		foreach ($this->initial_person_fields as $field) {
			if (isset($_POST['install_'.$field]) && empty($_POST['install_'.$field])) {
				trigger_error('You must enter a value for '.$field.' to proceed');
				return FALSE;
			}
		}

		// if we get to here, all person details were supplied
		if (empty($_POST['congregation_name'])) {
			trigger_error('You must enter at least one congregation name to proceed');
			return FALSE;
		}
		$cong_found = FALSE;
		foreach ($_POST['congregation_name'] as $cname) {
			if (!empty($cname)) {
				$cong_found = TRUE;
				break;
			}
		}
		if (!$cong_found) {
			trigger_error('You must enter at least one congregation name to proceed');
			return FALSE;
		}

		return TRUE;
	}



	function initDB()
	{
		$sql = "
			CREATE TABLE `db_object_lock` (
			  `objectid` int(11) NOT NULL default '0',
			  `userid` int(11) NOT NULL default '0',
			  `object_type` varchar(255) collate latin1_general_ci NOT NULL default '',
			  `expires` datetime NOT NULL default '0000-00-00 00:00:00',
			  KEY `objectid` (`objectid`),
			  KEY `userid` (`userid`),
			  KEY `object_type` (`object_type`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
		";
		$r = $GLOBALS['db']->query($sql);
		check_db_result($r);

		$dh = opendir(dirname(dirname(__FILE__)).'/db_objects');
		while (FALSE !== ($filename = readdir($dh))) {
			if (($filename == '.') || ($filename == '..') || is_dir($filename)) continue;
			$classname = str_replace('.class.php', '', $filename);
			require_once dirname(dirname(__FILE__)).'/db_objects/'.$filename;
			$data_obj =& new $classname;
			if (method_exists($data_obj, 'getInitSQL')) {
				$sql = $data_obj->getInitSQL();
				if (!empty($sql)) {
					if (!is_array($sql)) $sql = Array($sql);
					foreach ($sql as $s) {
						$r = $GLOBALS['db']->query($s);
						check_db_result($r);
					}
				}
			}
		}
	}


	function initCongregations()
	{
		foreach ($_POST['congregation_name'] as $cname) {
			if (empty($cname)) continue;
			$c = new Congregation();
			$c->setValue('name', $cname);
			$c->create();
			$this->congregationids[] = $c->id;
		}
	}

	function initUser()
	{
		$sm = new Staff_Member();
		foreach ($this->initial_person_fields as $field) {
			$sm->processFieldInterface($field, 'install_');
		}
		$sm->setValue('congregationid', reset($this->congregationids));
		$sm->setValue('status', 0);
		$sm->setValue('is_admin', 1);

		$family =& new Family();
		$family->setValue('family_name', $sm->getValue('last_name'));
		$family->setValue('creator', 0);
		$family->create();

		$sm->setValue('familyid', $family->id);
		$sm->create();



		$sm->setValue('creator', $sm->id);
		$sm->save();

		$family->setValue('creator', $sm->id);
		$family->save();
	}


	function printForm()
	{
		?>
		<h2>Welcome</h2>
		<p>Welcome to the Jethro installer.  The installation process will set up your MySql database so that it's ready to run Jethro.  First we need to collect some details to get things started.</p>
		
		<form method="post">
			<h3>Initial Staff Member Account</h3>
			<p>Please enter the details of the first staff member you want to add to the system.  This is the user as which you will initially log in.  After you have logged in you can edit the rest of the details for this person.</p>

			<table>
			<?php
			$sm =& new Staff_Member();
			foreach ($this->initial_person_fields as $fieldname) {
				?>
				<tr>
					<th><?php echo $sm->getFieldLabel($fieldname); ?></th>
					<td><?php $sm->printFieldInterface($fieldname, 'install_'); ?></td>
				</tr>
				<?php
			}
			?>
			</table>

			<h3>Congregations</h3>
			<p>Please enter the names of the congregations your church has.  These can be edited later from the control panel.</p>
			<table class="expandable">
				<tr>
					<td>
						<input type="text" name="congregation_name[]" />
					</td>
				</tr>
			</table>
			<p class="smallprint">(List expands as you type)</p>

			<h3>Continue...</h3>
			<input type="submit" value="Set up the database" />
		</form>
		<?php
	}

	function printConfirmation()
	{
		dump_messages();
		?>
		<h2>Installation Complete!</h2>

		You can now:
		<ul>
			<li><a target="_blank" href="<?php echo BASE_URL; ?>/readme.html">View the readme file</a> for further information<li>
			<li><a target="_blank" href="<?php echo BASE_URL; ?>">Log in to the system</a> to start adding families and persons
			<li><a target="_blank" href="<?php echo BASE_URL; ?>/admin">Log in to the control panel</a> to configure more congregations and staff members</li>
		</ul>
		<?php
	}
}
?>
Return current item: Jethro Pastoral Ministry Manager