Location: PHPKode > projects > Jethro Pastoral Ministry Manager > jethro-pmm/db_objects/person.class.php
<?php
include_once 'include/db_object.class.php';
class Person extends DB_Object
{
	function _getFields()
	{
		return Array(
			'first_name'	=> Array(
									'type'		=> 'text',
									'width'		=> 30,
									'maxlength'	=> 128,
									'allow_empty'	=> false,
									'initial_cap'	=> true,
									'trim'			=> TRUE,
								   ),
			'last_name'		=> Array(
									'type'		=> 'text',
									'width'		=> 30,
									'maxlength'	=> 128,
									'allow_empty'	=> false,
									'initial_cap'	=> true,
									'trim'			=> TRUE,
								   ),
			'gender'		=> Array(
									'type'			=> 'select',
									'options'		=> Array('male' => 'Male', 'female' => 'Female', '' => 'Unknown'),
									'default'		=> '',
							   ),
			'age_bracket'	=> Array(
									'type'			=> 'select',
									'options'		=> explode(',', AGE_BRACKET_OPTIONS),
									'default'		=> '0',
							   ),
			'email'			=> Array(
									'type'		=> 'text',
									'width'		=> 50,
									'maxlength'	=> 255,
									'class'		=> 'valid-email',
								   ),
			'mobile_tel'	=> Array(
									'type'			=> 'int',
									'width'			=> 10,
									'fixed_length'	=> 10,
									'allow_empty'	=> TRUE,
								   ),
			'work_tel'	=> Array(
									'type'			=> 'int',
									'width'			=> 8,
									'fixed_length'	=> 8,
									'allow_empty'	=> TRUE,
								   ),
			'remarks'	=> Array(
									'type'		=> 'text',
									'width'		=> 50,
									'height'	=> 2,
									'maxlength'	=> 255,
									'label'		=> 'Contact Remarks',
									'initial_cap'	=> true,
							),
			'familyid'	=> Array(
								'type'	=> 'reference',
								'references'	=> 'family',
								'editable'		=> false,
								'label'			=> 'Family',
								'show_in_summary'	=> false,
						   ),
			'congregationid'	=> Array(
									'type'				=> 'reference',
									'references'		=> 'congregation',
									'order_by'			=> 'name',
									'label'				=> 'Congregation',
									'show_id'			=> FALSE,
									'allow_empty'		=> TRUE,
									'class'				=> 'person-congregation',
							   ),
			'status'	=> Array(
								'type'	=> 'select',
								'options'	=> explode(',', PERSON_STATUS_OPTIONS) 
											+ Array('contact' => 'Contact', 'archived' => 'Archived'),
								'default'	=> 'contact',
								'class'		=> 'person-status',
						   ),
			'status_last_changed' => Array(
									'type'			=> 'datetime',
									'show_in_summary' => false,
									'allow_empty'	=> TRUE,
									'editable'			=> false,
								   ),
			'created'			=> Array(
									'type'			=> 'datetime',
									'readonly'		=> true,
									'show_in_summary'	=> false,
									'editable'			=> false,
								   ),
			'creator'			=> Array(
									'type'			=> 'reference',
									'editable'		=> false,
									'references'	=> 'staff_member',
									'show_in_summary'	=> false,
								   ),
			'history'			=> Array(
									'type'			=> 'serialise',
									'editable'		=> false,
									'show_in_summary'	=> false,

								   ),
												
		);

	}


	function getInitSQL()
	{
		return "
			CREATE TABLE `person` (
			  `id` int(11) NOT NULL auto_increment,
			  `first_name` varchar(255) collate latin1_general_ci NOT NULL default '',
			  `last_name` varchar(255) collate latin1_general_ci NOT NULL default '',
			  `gender` varchar(64) collate latin1_general_ci NOT NULL default '',
			  `age_bracket` varchar(64) collate latin1_general_ci NOT NULL default '',
			  `email` varchar(255) collate latin1_general_ci NOT NULL default '',
			  `mobile_tel` varchar(10) collate latin1_general_ci NOT NULL default '',
			  `work_tel` varchar(8) collate latin1_general_ci NOT NULL default '',
			  `remarks` text collate latin1_general_ci NOT NULL,
			  `status` varchar(8) collate latin1_general_ci NOT NULL default '',
			  `status_last_changed` datetime NULL default NULL,
			  `history` text collate latin1_general_ci NOT NULL,
			  `creator` int(11) NOT NULL default '0',
			  `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
			  `congregationid` int(11) default NULL,
			  `familyid` int(11) NOT NULL default '0',
			  PRIMARY KEY  (`id`),
			  KEY `first_name` (`first_name`),
			  KEY `last_name` (`last_name`),
			  KEY `email` (`email`),
			  KEY `mobile_tel` (`mobile_tel`),
			  KEY `work_tel` (`work_tel`),
			  KEY `status` (`status`),
			  KEY `familyid` (`familyid`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
		";
	}

	function toString()
	{
		return $this->values['first_name'].' '.$this->values['last_name'];
	}

	function printFieldValue($name)
	{
		if ($name == 'email') {
			?>
			<a href="mailto: <?php echo $this->values['first_name'].' '.$this->values['last_name']; ?><<?php echo $this->values[$name]; ?>>"><?php echo $this->values[$name]; ?></a>
			<?php
		} else 	if ($name == 'work_tel') {
			$value = $this->values[$name];
			if ($value) {
				// echo '<span class="clickable phone-no">'.substr($value, 0, 4).'-'.substr($value, 4, 4).'</span>';
				echo substr($value, 0, 4).'-'.substr($value, 4, 4);
			}
		} else 	if ($name == 'mobile_tel') {
			$value = $this->values[$name];
			if ($value) {
				//echo '<span class="clickable phone-no">'.substr($value, 0, 4).'-'.substr($value, 4, 3).'-'.substr($value, 7, 3).'</span>';
                                echo substr($value, 0, 4).'-'.substr($value, 4, 3).'-'.substr($value, 7, 3);
			}
		} else {
			return parent::printFieldValue($name);
		}
	}


	function getNotesHistory()
	{
		$family_notes = $GLOBALS['system']->getDBObjectData('family_note', Array('familyid' => $this->getValue('familyid')));
		$person_notes = $GLOBALS['system']->getDBObjectData('person_note', Array('personid' => $this->id));
		$all_notes = $family_notes + $person_notes;
		uasort($all_notes, Array($this, '_compareCreatedDates'));
		return $all_notes;
	}

	function _compareCreatedDates($a, $b)
	{
		return $a['created'] > $b['created'];

	}

	function validateFields()
	{
		if (!parent::validateFields()) return FALSE;
		if (empty($this->values['congregationid']) && ($this->values['status'] != 'contact') && ($this->values['status'] != 'archived')) {
			trigger_error('Only persons with status "contact" may have a blank congregation');
			return FALSE;
		}
		return TRUE;
	}


	function printRecentAttendance($num_weeks)
	{
		$since = date('Y-m-d', strtotime('-'.$num_weeks.' weeks'));
		$db =& $GLOBALS['db'];
		$sql = 'SELECT g.name, date, present
				FROM attendance_record ar
					LEFT OUTER JOIN person_group g ON ar.groupid = g.id
				WHERE personid = '.$db->quote($this->id).'
					AND date >= '.$db->quote($since).'
				GROUP BY groupid, date
				ORDER BY groupid, date';
		$attendances = $db->queryAll($sql, null, null, true, true, true);
		check_db_result($attendances);

		$colours = Array(
					'0'	=> 'Red',
					'1'	=> 'Green',
					'?' => 'Yellow'
				   );
		$labels = Array(
					'0'	=> 'A',
					'1'	=> 'P',
					'?' => '?'
				   );
		$width = floor(100 / $num_weeks);
		
		foreach ($attendances as $group_name => $group_attendances) {
			if (empty($group_name)) {
				?>
				<h4>Congregational Attendance</h4>
				<?php
			} else {
				?>
				<h4>Attendance at <?php echo htmlentities($group_name); ?></h4>
				<?php
			}
			?>
			<table class="standard">
				<thead>
					<tr>
					<?php
					foreach ($group_attendances as $att) {
						?>
						<th><?php echo date('j M', strtotime($att['date'])); ?></th>
						<?php
					}
					?>
					</tr>
				</thead>
				<tbody>
					<tr>
					<?php
					foreach ($group_attendances as $att) {
						?>
						<td style="background-color: <?php echo $colours[$att['present']]; ?>;">
							<?php echo $labels[$att['present']]; ?>
						</td>
						<?php
					}
					?>
					</tr>
				</tbody>
			</table>
			<?php
		}
	}


	function getPersonsByName($name, $include_archived=true)
	{
		$params = Array('CONCAT(first_name, " ", last_name)' => $name);
		if (!$include_archived) {
			$params['!status'] = 'archived';
		}
		$results = $GLOBALS['system']->getDBObjectData('person', $params, 'AND', 'last_name');
		if (empty($results)) {
			$params['CONCAT(first_name, " ", last_name)'] = '%'.$name.'%';
			$results = $GLOBALS['system']->getDBObjectData('person', $params, 'AND', 'last_name');
		}
		return $results;
	}


	function save($update_family_status=TRUE)
	{
		$msg = '';
		if ($update_family_status) {
			if (!empty($this->_old_values['status']) && ($this->getValue('status') == 'archived')) {
				// status has just been changed to 'archived'
				$family =& $GLOBALS['system']->getDBObject('family', $this->getValue('familyid'));
				$members = $family->getMemberData();


				$found_live_member = false;
				foreach ($members as $id => $details) {
					if ($id == $this->id) continue;
					if ($details['status'] != 'archived') {
						$found_live_member = true;
						break;
					}
				}
				if (!$found_live_member) {
					if ($family->canAcquireLock()) {
						$family->acquireLock();
						$family->setValue('status', 'archived');
						$family->save(FALSE);
						$family->releaseLock();
						$msg = '"'.$this->toString().'" has been archived, and since all members of the "'.$family->getValue('family_name').'" family are now archived, the family itself has been archived also';
					} else {
						$msg = '"'.$this->toString().'" has been archived, which means all members of the "'.$family->getValue('family_name').'" family are now archived.  However the lock on the family could not be acquired and so the family remains unarchived.';
					}
				}
			}
		}
		$res = parent::save();
		if ($msg) add_message($msg);
		return $res;
	}

	static function printSingleFinder($name, $currentval)
	{
		$currentid = 0;
		$currentname = '';
		if (is_int($currentval) && $currentval) {
			$currentid = $currentval;
			$person = $GLOBALS['system']->getDBObject('person', $currentid);
			if ($person) {
				$currentname = $person->toString();
			} else {
				$currentid = 0;
			}
		} else if (is_array($currentval)) {
			list($currentid, $currentname) = each ($currentval);
		}
		$displayname = $currentid ? $currentname.' (#'.$currentid.')' : '';
		?>
		<input type="text" id="<?php echo $name; ?>-input" class="person-search-single" value="<?php echo htmlentities($displayname); ?>" />
		<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $currentid; ?>" />
		<?php
	}

	static function printMultipleFinder($name, $val=Array())
	{
		if (!empty($val) && reset($val) == '') {
			// contains only IDs - need to get names
			$persons = $GLOBALS['system']->getDBObjectData('person', Array('id' => array_keys($val)));
			foreach ($persons as $id => $details) {
				$val[$id] = $details['first_name'].' '.$details['last_name'];
			}
		}
		?>
		<ul class="multi-person-finder" id="<?php echo $name; ?>-list">
		<?php
		foreach ($val as $id => $pname) {
			if (!$id) continue;
			echo '<li><div class="delete-chosen-person" onclick="deletePersonChooserListItem(this)"></div>'.$pname.'<input type="hidden" name="'.$name.'[]" value="'.$id.'" /></li>';
		}
		?>
		</ul>
		<input type="text" id="<?php echo $name; ?>-input" class="person-search-multiple" style="width: 25ex" />
		<?php
	}

	function getStatusOptions()
	{
		return $this->fields['status']['options'];
	}

	static function getStatusStats()
	{
		$dummy =& new Person();
		$status_options = $dummy->getStatusOptions();
		$sql = 'SELECT status, count(id)
				FROM person
				GROUP BY status';
		$res = $GLOBALS['db']->queryAll($sql, NULL, NULL, true);
		check_db_result($res);
		$out = Array();
		foreach ($status_options as $k => $v) {
			$out[$v] = (int)array_get($res, $k, 0);
		}
		return $out;
	}
		
		


}
?>
Return current item: Jethro Pastoral Ministry Manager