Skip to content

Commit

Permalink
pkp#10060 Typed announcement code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Jun 25, 2024
1 parent d5717a9 commit a68a438
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 310 deletions.
120 changes: 36 additions & 84 deletions classes/announcement/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,92 +35,76 @@ class Announcement extends \PKP\core\DataObject
//
/**
* Get assoc ID for this announcement.
*
* @return int
*/
public function getAssocId()
public function getAssocId(): ?int
{
return $this->getData('assocId');
}

/**
* Set assoc ID for this announcement.
*
* @param int $assocId
*/
public function setAssocId($assocId)
public function setAssocId(?int $assocId): void
{
$this->setData('assocId', $assocId);
}

/**
* Get assoc type for this announcement.
*
* @return int
*/
public function getAssocType()
public function getAssocType(): ?int
{
return $this->getData('assocType');
}

/**
* Set assoc type for this announcement.
*
* @param int $assocType
*/
public function setAssocType($assocType)
public function setAssocType(?int $assocType): void
{
$this->setData('assocType', $assocType);
}

/**
* Get the announcement type of the announcement.
*
* @return int
*/
public function getTypeId()
public function getTypeId(): ?int
{
return $this->getData('typeId');
}

/**
* Set the announcement type of the announcement.
*
* @param int $typeId
*/
public function setTypeId($typeId)
public function setTypeId(?int $typeId): void
{
$this->setData('typeId', $typeId);
}

/**
* Get the announcement type name of the announcement.
*
* @return string|null
*/
public function getAnnouncementTypeName()
public function getAnnouncementTypeName(): ?string
{
if (!$this->getData('typeId')) {
return null;
}
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
$announcementType = $announcementTypeDao->getById($this->getData('typeId'));
return $announcementType ? $announcementType->getLocalizedTypeName() : null;
return $announcementTypeDao->getById($this->getData('typeId'))->getLocalizedTypeName();
}

/**
* Get localized announcement title
*
* @return string
*/
public function getLocalizedTitle()
public function getLocalizedTitle(): ?string
{
return $this->getLocalizedData('title');
}

/**
* Get full localized announcement title including type name
*
* @return string
*/
public function getLocalizedTitleFull()
public function getLocalizedTitleFull(): ?string
{
$typeName = $this->getAnnouncementTypeName();
if (!empty($typeName)) {
Expand All @@ -132,149 +116,124 @@ public function getLocalizedTitleFull()

/**
* Get announcement title.
*
* @param string $locale
*
* @return string
*/
public function getTitle($locale)
public function getTitle(?string $locale): array|string|null
{
return $this->getData('title', $locale);
return $this->getData('title', $locale) ?? '';
}

/**
* Set announcement title.
*
* @param string $title
* @param string $locale
*/
public function setTitle($title, $locale)
public function setTitle(array|string|null $title, ?string $locale): void
{
$this->setData('title', $title, $locale);
}

/**
* Get localized short description
*
* @return string
*/
public function getLocalizedDescriptionShort()
public function getLocalizedDescriptionShort(): ?string
{
return $this->getLocalizedData('descriptionShort');
}

/**
* Get announcement brief description.
*
* @param string $locale
*
* @return string
*/
public function getDescriptionShort($locale)
public function getDescriptionShort(?string $locale): array|string|null
{
return $this->getData('descriptionShort', $locale);
}

/**
* Set announcement brief description.
*
* @param string $descriptionShort
* @param string $locale
*/
public function setDescriptionShort($descriptionShort, $locale)
public function setDescriptionShort(array|string|null $descriptionShort, ?string $locale): void
{
$this->setData('descriptionShort', $descriptionShort, $locale);
}

/**
* Get localized full description
*
* @return string
*/
public function getLocalizedDescription()
public function getLocalizedDescription(): ?string
{
return $this->getLocalizedData('description');
}

/**
* Get announcement description.
*
* @param string $locale
*
* @return string
*/
public function getDescription($locale)
public function getDescription(?string $locale): array|string|null
{
return $this->getData('description', $locale);
}

/**
* Set announcement description.
*
* @param string $description
* @param string $locale
*/
public function setDescription($description, $locale)
public function setDescription(array|string|null $description, ?string $locale): void
{
$this->setData('description', $description, $locale);
}

/**
* Get announcement expiration date.
*
* @return string (YYYY-MM-DD)
* @return ?string Format (YYYY-MM-DD)
*/
public function getDateExpire()
public function getDateExpire(): ?string
{
return $this->getData('dateExpire');
}

/**
* Set announcement expiration date.
*
* @param string $dateExpire (YYYY-MM-DD)
* @param ?string $dateExpire Format YYYY-MM-DD HH:MM:SS
*/
public function setDateExpire($dateExpire)
public function setDateExpire(?string $dateExpire): void
{
$this->setData('dateExpire', $dateExpire);
}

/**
* Get announcement posted date.
*
* @return string (YYYY-MM-DD)
* @return ?string Format YYYY-MM-DD HH:MM:SS
*/
public function getDatePosted()
public function getDatePosted(): ?string
{
return date('Y-m-d', strtotime($this->getData('datePosted')));
}

/**
* Get announcement posted datetime.
*
* @return string (YYYY-MM-DD HH:MM:SS)
* @return ?string Format YYYY-MM-DD HH:MM:SS
*/
public function getDatetimePosted()
public function getDatetimePosted(): ?string
{
return $this->getData('datePosted');
}

/**
* Set announcement posted date.
*
* @param string $datePosted (YYYY-MM-DD)
* @param ?string $datePosted Format YYYY-MM-DD HH:MM:SS
*/
public function setDatePosted($datePosted)
public function setDatePosted(?string $datePosted): void
{
$this->setData('datePosted', $datePosted);
}

/**
* Set announcement posted datetime.
*
* @param string $datetimePosted (YYYY-MM-DD HH:MM:SS)
* @param ?string $datetimePosted Format YYYY-MM-DD HH:MM:SS
*/
public function setDatetimePosted($datetimePosted)
public function setDatetimePosted(?string $datetimePosted): void
{
$this->setData('datePosted', $datetimePosted);
}
Expand Down Expand Up @@ -304,7 +263,6 @@ public function setImage(array $image): void
public function getImageUrl(bool $withTimestamp = true): string
{
$image = $this->getImage();

if (!$image) {
return '';
}
Expand All @@ -319,7 +277,7 @@ public function getImageUrl(bool $withTimestamp = true): string
return join('/', [
Application::get()->getRequest()->getBaseUrl(),
$this->getAssocId()
? $publicFileManager->getContextFilesPath((int) $this->getAssocId())
? $publicFileManager->getContextFilesPath($this->getAssocId())
: $publicFileManager->getSiteFilesPath(),
Repo::announcement()->getImageSubdirectory(),
$filename
Expand All @@ -331,13 +289,7 @@ public function getImageUrl(bool $withTimestamp = true): string
*/
public function getImageAltText(): string
{
$image = $this->getImage();

if (!$image || !$image['altText']) {
return '';
}

return $image['altText'];
return $this->getImage()['altText'] ?? '';
}
}

Expand Down
26 changes: 5 additions & 21 deletions classes/announcement/AnnouncementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,42 @@

class AnnouncementType extends \PKP\core\DataObject
{
//
// Get/set methods
//
/**
* Get context ID for this announcement.
*
* @return int
*/
public function getContextId()
public function getContextId(): ?int
{
return $this->getData('contextId');
}

/**
* Set context ID for this announcement.
*
* @param int $contextId
*/
public function setContextId($contextId)
public function setContextId(?int $contextId): void
{
$this->setData('contextId', $contextId);
}

/**
* Get the type of the announcement type.
*
* @return string
*/
public function getLocalizedTypeName()
public function getLocalizedTypeName(): ?string
{
return $this->getLocalizedData('name');
}

/**
* Get the type of the announcement type.
*
* @param string $locale
*
* @return string
*/
public function getName($locale)
public function getName(?string $locale): array|string|null
{
return $this->getData('name', $locale);
}

/**
* Set the type of the announcement type.
*
* @param string $name
* @param string $locale
*/
public function setName($name, $locale)
public function setName(array|string|null $name, ?string $locale): void
{
$this->setData('name', $name, $locale);
}
Expand Down
Loading

0 comments on commit a68a438

Please sign in to comment.