self::TYPE_INFO, 'i18n_catalogue' => 'stNotficationBackend', 'i18n_params' => array(), 'has_popup_message' => false, 'singular' => null, 'action' => null, 'icon' => null, 'id' => null, 'call_count' => null, ); /** * Singleton * @var static */ protected static $instance = null; /** * Ilość nieprzeczytanych notyfikacji * * @var int */ protected $countUnreadNotifications = null; /** * Nieprzeczytanych notyfikacji * * @var Notification[] */ protected $unreadNotifications = null; /** * Konfiguracja * * @var stConfig */ protected $config; /** * Zwraca instancje singleton * * @return static */ public static function getInstance() { if (null === self::$instance) { self::$instance = new static(); } return self::$instance; } public function __construct() { $this->config = stConfig::getInstance('stNotificationBackend'); if (null === $this->config->get('notify_groups')) { $this->config->set('notify_groups', array( 'stOrder' => true, 'stReview' => true, )); $this->config->save(); } } /** * Zwraca ilość nieprzeczytanych notyfikacji * * @return int * @throws PropelException * @throws SQLException */ public function countUnreadNotifications() { if (null === $this->countUnreadNotifications) { $this->countUnreadNotifications = NotificationPeer::doCountUnread(); } return $this->countUnreadNotifications; } /** * Zwraca nieprzeczytane notyfikacje * * @return Notification[] * @throws PropelException */ public function getUnreadNotifications() { if (null === $this->unreadNotifications) { $c = new Criteria(); $c->setLimit(self::LIMIT); $this->unreadNotifications = NotificationPeer::doSelectUnread($c); } return $this->unreadNotifications; } /** * Sprawdza czy są nieprzeczytane notyfikacje * * @return bool * @throws PropelException * @throws SQLException */ public function hasUnreadNotifications() { return $this->countUnreadNotifications() > 0; } /** * Zwraca najważniejszy typ notyfikacji * * @return int */ public function getMostImportantNoficationType() { $notifications = $this->getUnreadNotifications(); return $notifications && $notifications[0]->getType(); } /** * Dodaje nową notyfikacje * * @param string $group Nazwa grupy * @param string $message Treść notyfikacji * @param array $options Opcjonalne parametry * @return static * @throws stNotificationException * @throws PropelException */ public function add($group, $message, array $options = array()) { $notification = null; $enabled = $this->config->get('notify_groups', array()); $groups = stNotificationConfiguration::getGroups(); if (isset($groups[$group]) && !isset($enabled[$group])) { return $this; } if ($options) { $diff = array_diff_key($options, self::DEFAULTS); if (!empty($diff)) { throw new \stNotificationException(sprintf('The option "%s" do not exist. Available options are "%s"', implode(", ", array_keys($diff)), implode(", ", array_keys(self::DEFAULTS)))); } $options = array_merge(self::DEFAULTS, $options); } else { $options = self::DEFAULTS; } if (!isset($options['icon'])) { $options['icon'] = $group; } $id = sfInflector::underscore($options['id'] ? $options['id'] : $group); if ($options['singular']) { $notification = \NotificationPeer::retrieveByMessageId($id); if (null !== $notification) { $notification->updateNotifiedAt(); } } if (null === $notification) { $notification = new \Notification(); } $notification->setMessage($options['i18n_params'] ? '@' . serialize(array($message, $options['i18n_params'])) : $message); $notification->setType($options['type']); $notification->setHasPopupMessage($options['has_popup_message']); $notification->setI18nCatalogue($options['i18n_catalogue']); $notification->setAction($options['action']); $notification->setIcon($options['icon']); if ($options['singular']) { $notification->setMessageId($id); if (null !== $options['call_count']) { $notification->setCallCount($options['call_count']); } else { $notification->incrementCallCount(); } } $notification->save(); $this->unreadNotifications = null; return $this; } /** * Dodaje notyfikację w formie informacji * * @param string $group Nazwa grupy * @param string $message Treść notyfikacji * @param array $options Opcjonalne parametry * @return static * @throws stNotificationException * @throws PropelException */ public function addInfo($group, $message, array $options = array()) { $options['type'] = self::TYPE_INFO; return $this->add($group, $message, $options); } /** * Dodaje notyfikację w formie informacji o pomyślnym wykonaniu zadania * * @param string $group Nazwa grupy * @param string $message Treść notyfikacji * @param array $options Opcjonalne parametry * @return static * @throws stNotificationException * @throws PropelException */ public function addSuccess($group, $message, array $options = array()) { $options['type'] = self::TYPE_SUCCESS; return $this->add($group, $message, $options); } /** * Dodaje notyfikację w formie uwagi * * @param string $group Nazwa grupy * @param string $message Treść notyfikacji * @param array $options Opcjonalne parametry * @return static * @throws stNotificationException * @throws PropelException */ public function addAlert($group, $message, array $options = array()) { $options['type'] = self::TYPE_ALERT; return $this->add($group, $message, $options); } /** * Dodaje notyfikację w formie ostrzeżenia * * @param string $group Nazwa grupy * @param string $message Treść notyfikacji * @param array $options Opcjonalne parametry * @return static * @throws stNotificationException * @throws PropelException */ public function addWarning($group, $message, array $options = array()) { $options['type'] = self::TYPE_WARNING; return $this->add($group, $message, $options); } /** * Ustawia status wszystkich notyfikacji na przeczytane * Wpływa wyłącznie na notyfikacje oznaczone parameterem singular * * @param string $group Nazwa grupy lub id notyfikacji * @return static * @throws PropelException */ public function markAllAsRead($id) { $id = sfInflector::underscore($id); $notification = NotificationPeer::retrieveByMessageId($id); if ($notification) { $notification->setCallCount(0); $notification->setIsMarkedAsRead(true); $notification->save(); } return $this; } /** * Zmniejsza ilość wywołań pojedynczej notyfikacji * Wpływa wyłącznie na notyfikacje oznaczone parameterem singular * * @param string $group Nazwa grupy lub id notyfikacji * @return static * @throws PropelException */ public function markAsRead($id) { $id = sfInflector::underscore($id); $notification = NotificationPeer::retrieveByMessageId($id); if ($notification) { $notification->decrementCallCount(); if (!$notification->getCallCount()) { $notification->setIsMarkedAsRead(true); } $notification->save(); } return $this; } }