class ActivityNotifications in Open Social 10.1.x
Same name and namespace in other branches
- 8.9 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.2 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.3 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.4 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.5 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.6 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.7 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 8.8 modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 10.3.x modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 10.0.x modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
- 10.2.x modules/custom/activity_creator/src/ActivityNotifications.php \Drupal\activity_creator\ActivityNotifications
Class ActivityNotifications to get Personalised activity items for account.
@package Drupal\activity_creator
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\activity_creator\ActivityNotifications
Expanded class hierarchy of ActivityNotifications
4 files declare their use of ActivityNotifications
- AccountHeaderBlock.php in modules/
social_features/ social_user/ src/ Plugin/ Block/ AccountHeaderBlock.php - ActivitySendEmailJobType.php in modules/
custom/ activity_send/ modules/ activity_send_email/ src/ Plugin/ AdvancedQueue/ JobType/ ActivitySendEmailJobType.php - ActivitySendEmailWorker.php in modules/
custom/ activity_send/ modules/ activity_send_email/ src/ Plugin/ QueueWorker/ ActivitySendEmailWorker.php - NotificationsController.php in modules/
custom/ activity_creator/ src/ Controller/ NotificationsController.php
1 string reference to 'ActivityNotifications'
- activity_creator.services.yml in modules/
custom/ activity_creator/ activity_creator.services.yml - modules/custom/activity_creator/activity_creator.services.yml
1 service uses ActivityNotifications
- activity_creator.activity_notifications in modules/
custom/ activity_creator/ activity_creator.services.yml - Drupal\activity_creator\ActivityNotifications
File
- modules/
custom/ activity_creator/ src/ ActivityNotifications.php, line 18
Namespace
Drupal\activity_creatorView source
class ActivityNotifications extends ControllerBase {
/**
* Database services.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* ActivityNotifications constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* Database services.
*/
public function __construct(Connection $connection) {
$this->database = $connection;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
/**
* Returns the Notifications for a given account.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account object to get notifications for.
* @param array $status
* Filter by status.
*
* @return array
* Return array of notification ids.
*/
public function getNotifications(AccountInterface $account, array $status = [
ACTIVITY_STATUS_RECEIVED,
]) : array {
return $this
->getNotificationIds($account, $status);
}
/**
* Returns the Activity objects with destination 'notification' for account.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account object.
* @param array $status
* Status string: activity_creator_field_activity_status_allowed_values().
*
* @return array
* Return array of notifications as activity objects or an empty array.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getNotificationsActivities(AccountInterface $account, array $status = [
ACTIVITY_STATUS_RECEIVED,
]) : array {
if (!empty($ids = $this
->getNotificationIds($account, $status))) {
return $this
->entityTypeManager()
->getStorage('activity')
->loadMultiple($ids);
}
return [];
}
/**
* Gets all activity IDs by given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
*
* @return array
* Return array of activity IDs or an empty array.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getActivityIdsByEntity(EntityInterface $entity) : array {
$ids = [];
$entity_id = $entity
->id();
$entity_type = $entity
->getEntityTypeId();
switch ($entity_type) {
case 'user':
case 'group':
$entity_query = $this
->entityTypeManager()
->getStorage('activity')
->getQuery();
$entity_query
->condition('field_activity_recipient_' . $entity_type, $entity_id, '=');
$ids = $entity_query
->execute();
break;
case 'group_content':
/** @var \Drupal\group\Entity\GroupContent $entity */
$group_content = $entity;
$linked_entity = $entity
->getEntity();
$group = $entity
->getGroup();
if ($linked_entity && $linked_entity
->getEntityTypeId() === 'node' && $group
->id()) {
$entity_query = $this
->entityTypeManager()
->getStorage('activity')
->getQuery();
$entity_query
->condition('field_activity_entity.target_id', $linked_entity
->id(), '=');
$entity_query
->condition('field_activity_entity.target_type', $linked_entity
->getEntityTypeId(), '=');
$entity_query
->condition('field_activity_recipient_group', $group
->id(), '=');
$ids = $entity_query
->execute();
}
break;
default:
if ($entity_type !== 'activity') {
$entity_query = $this
->entityTypeManager()
->getStorage('activity')
->getQuery();
$entity_query
->condition('field_activity_entity.target_id', $entity_id, '=');
$entity_query
->condition('field_activity_entity.target_type', $entity_type, '=');
$ids = $entity_query
->execute();
}
break;
}
return $ids;
}
/**
* Mark all notifications as Seen for account.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account object.
*
* @return bool
* TRUE or FALSE depending upon update status.
*/
public function markAllNotificationsAsSeen(AccountInterface $account) : bool {
// Retrieve all the activities referring this entity for this account.
if (!empty($ids = $this
->getNotificationIds($account, [
ACTIVITY_STATUS_RECEIVED,
]))) {
return $this
->changeStatusOfActivity($ids, $account, ACTIVITY_STATUS_SEEN);
}
return FALSE;
}
/**
* Mark an entity as read for a given account.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account object.
* @param \Drupal\Core\Entity\EntityBase $entity
* Entity object.
*
* @deprecated in opensocial:8.x-7.1 and is removed from opensocial:8.x-8.0. Use
* \Drupal\activity_creator\ActivityNotifications
* ::markEntityNotificationsAsSeen() instead.
*
* @todo Change @see to point to a change record.
* @see https://www.drupal.org/project/social/issues/3087083
*/
public function markEntityAsRead(AccountInterface $account, EntityBase $entity) {
// Retrieve all the activities referring this entity for this account.
$ids = $this
->getNotificationIds($account, [
ACTIVITY_STATUS_RECEIVED,
ACTIVITY_STATUS_SEEN,
]);
$this
->changeStatusOfActivity($ids, $account);
}
/**
* Change the status of an activity.
*
* @param array $ids
* Array of Activity entity IDs.
* @param \Drupal\Core\Session\AccountInterface $account
* Account object.
* @param int $status
* See: activity_creator_field_activity_status_allowed_values()
*
* @return bool
* Status of update query.
*/
public function changeStatusOfActivity(array $ids, AccountInterface $account, $status = ACTIVITY_STATUS_RECEIVED) : bool {
if (!empty($ids)) {
// The transaction opens here.
$txn = $this->database
->startTransaction();
try {
// Collect the information about affected rows.
$this->database
->update('activity_notification_status')
->fields([
'status' => $status,
])
->condition('uid', $account
->id())
->condition('aid', $ids, 'IN')
->execute();
return TRUE;
} catch (\Exception $exception) {
// Something went wrong somewhere, so roll back now.
$txn
->rollBack();
// Log the exception to watchdog.
$this
->getLogger('default')
->error($exception
->getMessage());
}
}
return FALSE;
}
/**
* Returns the Activity ids for an account with destination 'notification'.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Account object.
* @param array $status
* Array of notification statuses.
*
* @return array
* Returns an array of notification ids or empty array.
*/
protected function getNotificationIds(AccountInterface $account, array $status = []) : array {
// Get the user ID.
if (!empty($uid = $account
->id())) {
try {
$query = $this->database
->select('activity_notification_status', 'ans')
->fields('ans', [
'aid',
])
->condition('uid', $uid);
if (!empty($status)) {
$query
->condition('status', $status, 'IN');
}
return $query
->execute()
->fetchCol();
} catch (\Exception $exception) {
// Log the exception to watchdog.
$this
->getLogger('default')
->error($exception
->getMessage());
return [];
}
}
return [];
}
/**
* Deletes all entries in activity_notification_table by given ids.
*
* @param array $activity_ids
* Array of activity ids to be deleted.
*
* @return bool
* Status of update query.
*/
public function deleteNotificationsbyIds(array $activity_ids) : bool {
if (!empty($activity_ids)) {
// The transaction opens here.
$txn = $this->database
->startTransaction();
try {
$this->database
->delete('activity_notification_status')
->condition('aid', $activity_ids, 'IN')
->execute();
} catch (\Exception $exception) {
// Something went wrong somewhere, so roll back now.
$txn
->rollBack();
// Log the exception to watchdog.
$this
->getLogger('default')
->error($exception
->getMessage());
}
return TRUE;
}
return FALSE;
}
/**
* Returns the activity notification status.
*
* @param \Drupal\activity_creator\Entity\Activity $activity
* Activity entity.
* @param \Drupal\Core\Session\AccountInterface $account
* Activity Notification of current account.
*
* @return mixed
* FALSE or the status of activity depending upon the execution of query.
*/
public function getActivityStatus(Activity $activity, AccountInterface $account) {
// Get the user ID.
if (!empty($id = $activity
->id())) {
try {
$query = $this->database
->select('activity_notification_status', 'ans')
->fields('ans', [
'status',
])
->condition('aid', $id)
->condition('uid', $account
->id());
return $query
->execute()
->fetchField();
} catch (\Exception $exception) {
// Log the exception to watchdog.
$this
->getLogger('default')
->error($exception
->getMessage());
}
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActivityNotifications:: |
protected | property | Database services. | |
ActivityNotifications:: |
public | function | Change the status of an activity. | |
ActivityNotifications:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ActivityNotifications:: |
public | function | Deletes all entries in activity_notification_table by given ids. | |
ActivityNotifications:: |
public | function | Gets all activity IDs by given entity. | |
ActivityNotifications:: |
public | function | Returns the activity notification status. | |
ActivityNotifications:: |
protected | function | Returns the Activity ids for an account with destination 'notification'. | |
ActivityNotifications:: |
public | function | Returns the Notifications for a given account. | |
ActivityNotifications:: |
public | function | Returns the Activity objects with destination 'notification' for account. | |
ActivityNotifications:: |
public | function | Mark all notifications as Seen for account. | |
ActivityNotifications:: |
public | function | Mark an entity as read for a given account. | |
ActivityNotifications:: |
public | function | ActivityNotifications constructor. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |