Skip to content

Commit

Permalink
Merge pull request #83 from ChurchCRM/feature/api
Browse files Browse the repository at this point in the history
Making API Service More Ogranized
  • Loading branch information
DawoudIO committed Dec 20, 2015
2 parents 8f82f9c + 1d2f4a1 commit 9fabe36
Show file tree
Hide file tree
Showing 33 changed files with 467 additions and 284 deletions.
380 changes: 96 additions & 284 deletions churchinfo/api/index.php

Large diffs are not rendered by default.

224 changes: 224 additions & 0 deletions churchinfo/api/services/DataSeedService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

class DataSeedService
{

function getPerson($rs, &$personPointer)
{
$user = $rs[$personPointer]->user;
$personPointer += 1;
return $user;
}

function sanitize($str)
{
return str_replace("'", "", $str);
}

function insertPerson($user)
{
$sSQL = "INSERT INTO person_per
(per_Title,
per_FirstName,
per_MiddleName,
per_LastName,
per_Suffix,
per_Gender,
per_Address1,
per_Address2,
per_City,
per_State,
per_Zip,
per_Country,
per_HomePhone,
per_WorkPhone,
per_CellPhone,
per_Email,
per_WorkEmail,
per_BirthMonth,
per_BirthDay,
per_BirthYear,
per_Envelope,
per_fam_ID,
per_fmr_ID,
per_MembershipDate,
per_cls_ID,
per_DateEntered,
per_EnteredBy,
per_FriendDate,
per_Flags )
VALUES ('" .
$this->sanitize($user->name->title) . "','" .
$this->sanitize($user->name->first) . "',NULL,'" .
$this->sanitize($user->name->last) . "',NULL,'" .
$this->sanitize($user->gender) . "','" .
$this->sanitize($user->location->street) . "',NULL,'" .
$this->sanitize($user->location->city) . "','" .
$this->sanitize($user->location->state) . "','" .
$this->sanitize($user->location->zip) . "','USA','" .
$this->sanitize($user->phone) . "',NULL,'" .
$this->sanitize($user->cell) . "','" .
$this->sanitize($user->email) . "',NULL," .
date('m', $user->dob) . "," .
date('d', $user->dob) . "," .
date('Y', $user->dob) . ",NULL,'" .
$this->sanitize($user->famID) . "'," .
$this->sanitize($user->per_fmr_id) . "," . "\"" .
date('Y-m-d', $user->registered) .
"\"" . ",1,'" .
date("YmdHis") .
"'," .
$this->sanitize($_SESSION['iUserID']) . ",";

if (strlen($dFriendDate) > 0)
$sSQL .= "\"" . $dFriendDate . "\"";
else
$sSQL .= "NULL";
$sSQL .= ", 0";
$sSQL .= ")";
$bGetKeyBack = True;
RunQuery($sSQL);
// If this is a new person, get the key back and insert a blank row into the person_custom table
if ($bGetKeyBack) {
$sSQL = "SELECT MAX(per_ID) AS iPersonID FROM person_per";
$rsPersonID = RunQuery($sSQL);
extract(mysql_fetch_array($rsPersonID));
$sSQL = "INSERT INTO `person_custom` (`per_ID`) VALUES ('" . $iPersonID . "')";
RunQuery($sSQL);
}

}

function insertFamily($user)
{
$dWeddingDate = "NULL";
$iCanvasser = 0;
$nLatitude = 0;
$nLongitude = 0;
$nEnvelope = 0;
$sSQL = "INSERT INTO family_fam (
fam_Name,
fam_Address1,
fam_Address2,
fam_City,
fam_State,
fam_Zip,
fam_Country,
fam_HomePhone,
fam_WorkPhone,
fam_CellPhone,
fam_Email,
fam_WeddingDate,
fam_DateEntered,
fam_EnteredBy,
fam_SendNewsLetter,
fam_OkToCanvass,
fam_Canvasser,
fam_Latitude,
fam_Longitude,
fam_Envelope)
VALUES ('" .
$user->name->last . "','" .
$user->location->street . "','" .
$sAddress2 . "','" .
$user->location->city . "','" .
$user->location->state . "','" .
$user->location->zip . "','" .
$sCountry . "','" .
$sHomePhone . "','" .
$sWorkPhone . "','" .
$sCellPhone . "','" .
$sEmail . "'," .
$dWeddingDate . ",'" .
date("YmdHis") . "'," .
$_SESSION['iUserID'] . "," .
"FALSE," .
"FALSE,'" .
$iCanvasser . "'," .
$nLatitude . "," .
$nLongitude . "," .
$nEnvelope . ")";
RunQuery($sSQL);
$sSQL = "SELECT MAX(fam_ID) AS iFamilyID FROM family_fam";

$rsLastEntry = RunQuery($sSQL);
extract(mysql_fetch_array($rsLastEntry));
return $iFamilyID;

}

function generateFamilies($families)
{
echo "Generating Families";
$kidsPerFamily = 3;
$kidsdev = 3;
$personPointer = 1;
$count = $families * ($kidsPerFamily + $kidsdev + 2);
$response = file_get_contents("http://api.randomuser.me/?nat=US&results=" . $count);
$data = json_decode($response);
$rs = $data->results;
$rTotalHoh = 0;
$rTotalSpouse = 0;
$rTotalChildren = 0;

for ($i = 0; $i < $families; $i++) {

$hoh = $this->getPerson($rs, $personPointer);

$FamilyID = $this->insertFamily($hoh);
$familyName = $hoh->name->last;
$hoh->famID = $FamilyID;
$hoh->per_fmr_id = 1;

$spouse = $this->getPerson($rs, $personPointer);
$spouse->name->last = $familyName;
$spouse->famID = $FamilyID;
$spouse->per_fmr_id = 2;

$this->insertPerson($hoh);
$rTotalHoh += 1;
$this->insertPerson($spouse);
$rTotalSpouse += 1;

#$thisFamChildren = stats_rand_gen_normal ($kidsPerFamily, $stddev);
$thisFamChildren = rand($kidsPerFamily - $kidsdev, $kidsPerFamily + $kidsdev);

for ($y = 0; $y < $thisFamChildren; $y++) {
$child = $this->getPerson($rs, $personPointer);
$child->name->last = $familyName;
$child->famID = $FamilyID;
$child->per_fmr_id = 3;
$this->insertPerson($child);
$rTotalChildren += 1;
}

}
echo '{"random.me response":' . $response . '"families created": ' . $families . ',"heads of household created": ' . $rTotalHoh . ', "spouses created":' . $rTotalSpouse . ', "children created":' . $rTotalChildren . '}';

}

function generateSundaySchoolClasses($classes, $childrenPerTeacher)
{

echo '{"status":"Sunday School Seed Data Not Implemented"}';

}

function generateEvents($events, $averageAttendance)
{

echo '{"status":"Events Seed Data Not Implemented"}';

}

function generateDeposits($deposits, $averagedepositvalue)
{
echo '{"status":"Deposits Seed Data Not Implemented"}';
}

function generateFundRaisers($fundraisers, $averageItems, $averageItemPrice)
{
echo '{"status":"Fundraisers Seed Data Not Implemented"}';
}

}
45 changes: 45 additions & 0 deletions churchinfo/api/services/FamilyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class FamilyService {

function search($searchTerm) {
$fetch = 'SELECT fam_ID, fam_Name, fam_City, fam_State FROM family_fam WHERE family_fam.fam_Name LIKE \'%'.$searchTerm.'%\' LIMIT 15';
$result=mysql_query($fetch);

$families = array();
while($row=mysql_fetch_array($result)) {
$row_array['id']=$row['fam_ID'];
$row_array['fam_name']=$row['fam_Name'];

array_push($families,$row_array);
}
$this->returnFamilies($families);
}


function lastEdited() {

$sSQL = "select * from family_fam order by fam_DateLastEdited desc LIMIT 10;";
$rsLastFamilies = RunQuery($sSQL);

$families = array();

while ($row = mysql_fetch_array($rsLastFamilies)) {
$row_array['id'] = $row['fam_ID'];
$row_array['name'] = $row['fam_Name'];
$row_array['address'] = $row['fam_Address1'];
$row_array['city'] = $row['fam_City'];

array_push($families,$row_array);
}

$this->returnFamilies($families);

}

function returnFamilies($families) {
echo '{"families": ' . json_encode($families) . '}';
}
}

?>
102 changes: 102 additions & 0 deletions churchinfo/api/services/PersonService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

class PersonService
{

function photo($id)
{
$sSQL = 'SELECT per_ID, per_FirstName, per_LastName, per_Gender, per_Email FROM person_per WHERE per_ID =' . $id;
$person = RunQuery($sSQL);
extract(mysql_fetch_array($person));

if ($per_ID != "") {

$photoFile = $this->getUploadedPhoto($per_ID);

if ($photoFile == "" && $per_Email != "") {
$photoFile = $this->getGravatar($per_Email);
}

if ($photoFile == "") {
$photoFile = $this->getDefaultPhoto();
}

echo $photoFile;
} else {
echo "{ error: person not found for id ".$id. "}";
}

}

private
function getUploadedPhoto($personId)
{
$validextensions = array("jpeg", "jpg", "png");
$hasFile = false;
while (list(, $ext) = each($validextensions)) {
$photoFile = "../Images/Person/thumbnails/" . $personId . "." . $ext;
if (file_exists($photoFile)) {
$hasFile = true;
$photoFile = "Images/Person/thumbnails/" . $personId . "." . $ext;
break;
}
}

if ($hasFile) {
return $photoFile;
} else {
return "";
}
}

private
function getGravatar($email, $s = 60, $d = '404', $r = 'g', $img = false, $atts = array())
{
$url = 'http://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";

$headers = @get_headers($url);
if (strpos($headers[0], '404') === false) {
return $url;
} else {
return "";
}
}

function search($searchTerm)
{
$fetch = 'SELECT per_ID, per_FirstName, per_LastName, CONCAT_WS(" ",per_FirstName,per_LastName) AS fullname, per_fam_ID FROM person_per WHERE per_FirstName LIKE \'%' . $searchTerm . '%\' OR per_LastName LIKE \'%' . $searchTerm . '%\' OR per_Email LIKE \'%' . $searchTerm . '%\' OR CONCAT_WS(" ",per_FirstName,per_LastName) LIKE \'%' . $searchTerm . '%\' LIMIT 15';
$result = mysql_query($fetch);

$return = array();
while ($row = mysql_fetch_array($result)) {
$values['id'] = $row['per_ID'];
$values['famID'] = $row['per_fam_ID'];
$values['per_FirstName'] = $row['per_FirstName'];
$values['per_LastName'] = $row['per_LastName'];
$values['value'] = $row['per_FirstName'] . " " . $row['per_LastName'];

array_push($return, $values);
}

echo '{"persons": ' . json_encode($return) . '}';
}

private function getDefaultPhoto($gender, $famRole)
{
$photoFile = "img/man-128.png";
if ($gender == 1 && $famRole == "Child") {
$photoFile = "img/kid_boy-128.png";
} else if ($gender == 2 && $famRole != "Child") {
$photoFile = "img/woman-128.png";
} else if ($gender == 2 && $famRole == "Child") {
$photoFile = "img/kid_girl-128.png";
}

return $photoFile;
}

}

?>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 9fabe36

Please sign in to comment.