Skip to content

Commit

Permalink
Merge pull request #6762 from ChurchCRM/fix-dashboard-birthdays
Browse files Browse the repository at this point in the history
Fix dashboard birthdays and address various minor code smells
  • Loading branch information
DawoudIO authored Dec 22, 2023
2 parents cc0d6cc + 87024c7 commit 55e6013
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 218 deletions.
100 changes: 54 additions & 46 deletions src/ChurchCRM/Backup/BackupDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,67 @@

class BackupDownloader
{
public static function downloadBackup($filename)
public static function downloadBackup(string $filename): void
{
if ($filename === '') {
$message = 'filename must be non-empty: ' . $filename;
LoggerUtils::getAppLogger()->error($message);

throw new \Exception($message, 500);
}

$path = SystemURLs::getDocumentRoot() . "/tmp_attach/ChurchCRMBackups/$filename";
LoggerUtils::getAppLogger()->info('Download requested for :' . $path);
if (file_exists($path)) {
if ($fd = fopen($path, 'r')) {
$fsize = filesize($path);
$path_parts = pathinfo($path);
$ext = strtolower($path_parts['extension']);
switch ($ext) {
case 'gz':
header('Content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'tar.gz':
header('Content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'sql':
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'gpg':
header('Content-type: application/pgp-encrypted');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'zip':
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
// add more headers for other content types here
default:
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="' . $path_parts['basename'] . '"');
break;
}
header("Content-length: $fsize");
header('Cache-control: private'); //use this to open files directly
LoggerUtils::getAppLogger()->debug('Headers sent. sending backup file contents');
while (!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
LoggerUtils::getAppLogger()->debug('Backup file contents sent');
}
fclose($fd);
FileSystemUtils::recursiveRemoveDirectory(SystemURLs::getDocumentRoot() . '/tmp_attach/', true);
LoggerUtils::getAppLogger()->debug('Removed backup file from server filesystem');
} else {
if (!file_exists($path)) {
$message = 'Requested download does not exist: ' . $path;
LoggerUtils::getAppLogger()->error($message);

throw new \Exception($message, 500);
}

if ($fd = fopen($path, 'r')) {
$fsize = filesize($path);
$path_parts = pathinfo($path);
$ext = strtolower($path_parts['extension']);
switch ($ext) {
case 'gz':
header('Content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'tar.gz':
header('Content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'sql':
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'gpg':
header('Content-type: application/pgp-encrypted');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
case 'zip':
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $path_parts['basename'] . '"');
break;
// add more headers for other content types here
default:
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="' . $path_parts['basename'] . '"');
break;
}
header("Content-length: $fsize");
header('Cache-control: private'); //use this to open files directly
LoggerUtils::getAppLogger()->debug('Headers sent. sending backup file contents');
while (!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
LoggerUtils::getAppLogger()->debug('Backup file contents sent');
}
fclose($fd);

FileSystemUtils::recursiveRemoveDirectory(SystemURLs::getDocumentRoot() . '/tmp_attach/', true);
LoggerUtils::getAppLogger()->debug('Removed backup file from server filesystem');
}
}
5 changes: 3 additions & 2 deletions src/ChurchCRM/Backup/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ChurchCRM\Bootstrapper;
use ChurchCRM\dto\SystemURLs;
use ChurchCRM\Utils\ExecutionTime;
use ChurchCRM\Utils\LoggerUtils;
use Defuse\Crypto\File;
use Exception;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function copyToWebDAV($Endpoint, $Username, $Password)
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $this->BackupFile->getSize());
LoggerUtils::getAppLogger()->debug('Beginning to send file');
$time = new \ChurchCRM\Utils\ExecutionTime();
$time = new ExecutionTime();
$result = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
Expand Down Expand Up @@ -178,7 +179,7 @@ private function encryptBackupFile()

public function execute()
{
$time = new \ChurchCRM\Utils\ExecutionTime();
$time = new ExecutionTime();
LoggerUtils::getAppLogger()->info('Beginning backup job. Type: ' . $this->BackupType . '. BaseName: ' . $this->BackupFileBaseName);
if ($this->BackupType == BackupType::FULL_BACKUP) {
$this->createFullArchive();
Expand Down
67 changes: 26 additions & 41 deletions src/ChurchCRM/model/ChurchCRM/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
/**
* Skeleton subclass for representing a row from the 'person_per' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
Expand Down Expand Up @@ -56,20 +54,20 @@ public function getGenderName()
}
}

public function hideAge()
public function hideAge(): bool
{
return $this->getFlags() == 1 || $this->getBirthYear() == '' || $this->getBirthYear() == '0';
return $this->getFlags() === 1 || empty($this->getBirthYear());
}

public function getBirthDate()
public function getBirthDate(): ?\DateTimeImmutable
{
if (
$this->getBirthDay() !== null && $this->getBirthDay() !== '' &&
$this->getBirthMonth() !== null && $this->getBirthMonth() !== ''
!empty($this->getBirthDay()) &
!empty($this->getBirthMonth())
) {
$birthYear = $this->getBirthYear();
if ($birthYear === '') {
$birthYear = 1900;
if (empty($birthYear)) {
$birthYear = 0;
}

return \DateTimeImmutable::createFromFormat('Y-m-d', $birthYear . '-' . $this->getBirthMonth() . '-' . $this->getBirthDay());
Expand All @@ -91,7 +89,7 @@ public function getFormattedBirthDate()
}
}

public function getViewURI()
public function getViewURI(): string
{
return SystemURLs::getRootPath() . '/PersonView.php?PersonID=' . $this->getId();
}
Expand All @@ -107,7 +105,7 @@ public function getFamilyRole()
return $familyRole;
}

public function getFamilyRoleName()
public function getFamilyRoleName(): string
{
$roleName = '';
$role = $this->getFamilyRole();
Expand Down Expand Up @@ -181,7 +179,7 @@ private function createTimeLineNote($type)
$note->save();
}

public function isUser()
public function isUser(): bool
{
$user = UserQuery::create()->findPk($this->getId());

Expand All @@ -190,10 +188,8 @@ public function isUser()

/**
* Get address of a person. If empty, return family address.
*
* @return string
*/
public function getAddress()
public function getAddress(): string
{
if (!empty($this->getAddress1())) {
$address = [];
Expand Down Expand Up @@ -228,10 +224,8 @@ public function getAddress()

/**
* Get name of a person family.
*
* @return string
*/
public function getFamilyName()
public function getFamilyName(): string
{
if ($this->getFamily()) {
return $this->getFamily()
Expand All @@ -243,10 +237,8 @@ public function getFamilyName()

/**
* Get name of a person family.
*
* @return string
*/
public function getFamilyCountry()
public function getFamilyCountry(): string
{
if ($this->getFamily()) {
return $this->getFamily()
Expand All @@ -261,10 +253,8 @@ public function getFamilyCountry()
* 0 = Home
* 1 = Work
* 2 = Cell.
*
* @return string
*/
public function getFamilyPhone($type)
public function getFamilyPhone(int $type): string
{
switch ($type) {
case 0:
Expand Down Expand Up @@ -296,7 +286,7 @@ public function getFamilyPhone($type)
*
* @return array
*/
public function getLatLng()
public function getLatLng(): array
{
$address = $this->getAddress(); //if person address empty, this will get Family address
$lat = 0;
Expand All @@ -308,7 +298,7 @@ public function getLatLng()
$lng = $latLng['Longitude'];
}
} else {
// Philippe Logel : this is usefull when a person don't have a family : ie not an address
// note: this is useful when a person don't have a family (i.e. not an address)
if (!empty($this->getFamily())) {
if (!$this->getFamily()->hasLatitudeAndLongitude()) {
$this->getFamily()->updateLanLng();
Expand All @@ -324,7 +314,7 @@ public function getLatLng()
];
}

public function deletePhoto()
public function deletePhoto(): bool
{
if (AuthenticationManager::getCurrentUser()->isDeleteRecordsEnabled()) {
if ($this->getPhoto()->delete()) {
Expand All @@ -342,7 +332,7 @@ public function deletePhoto()
return false;
}

public function getPhoto()
public function getPhoto(): Photo
{
if (!$this->photo) {
$this->photo = new Photo('Person', $this->getId());
Expand All @@ -351,7 +341,7 @@ public function getPhoto()
return $this->photo;
}

public function setImageFromBase64($base64)
public function setImageFromBase64($base64): bool
{
if (AuthenticationManager::getCurrentUser()->isEditRecordsEnabled()) {
$note = new Note();
Expand Down Expand Up @@ -379,12 +369,8 @@ public function setImageFromBase64($base64)
* $Style = 6 : "LastName, Title FirstName"
* $Style = 7 : "LastName FirstName"
* $Style = 8 : "LastName, FirstName Middlename".
*
* @param $Style
*
* @return string
*/
public function getFormattedName($Style)
public function getFormattedName(int $Style): string
{
$nameString = '';
switch ($Style) {
Expand Down Expand Up @@ -640,11 +626,11 @@ public function postSave(ConnectionInterface $con = null)
return parent::postSave($con);
}

public function getAge($now = null)
public function getAge(?\DateTimeInterface $now = null): string
{
$birthDate = $this->getBirthDate();

if ($this->hideAge()) {
if ($birthDate === null || $this->hideAge()) {
return false;
}
if (empty($now)) {
Expand All @@ -662,7 +648,7 @@ public function getAge($now = null)
public function getNumericAge()
{
$birthDate = $this->getBirthDate();
if ($this->hideAge()) {
if ($birthDate === null || $this->hideAge()) {
return false;
}
if (empty($now)) {
Expand All @@ -678,8 +664,7 @@ public function getNumericAge()
return $ageValue;
}

/* Philippe Logel 2017 */
public function getFullNameWithAge()
public function getFullNameWithAge(): string
{
return $this->getFullName() . ' ' . $this->getAge();
}
Expand All @@ -692,12 +677,12 @@ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColum
return $array;
}

public function getThumbnailURL()
public function getThumbnailURL(): string
{
return SystemURLs::getRootPath() . '/api/person/' . $this->getId() . '/thumbnail';
}

public function getEmail()
public function getEmail(): ?string
{
if (parent::getEmail() == null) {
$family = $this->getFamily();
Expand Down
Loading

0 comments on commit 55e6013

Please sign in to comment.