Index: branches/5.2.x/core/kernel/application.php =================================================================== diff -u -N -r15252 -r15287 --- branches/5.2.x/core/kernel/application.php (.../application.php) (revision 15252) +++ branches/5.2.x/core/kernel/application.php (.../application.php) (revision 15287) @@ -1,6 +1,6 @@ registerClass('kHookManager', KERNEL_PATH . '/managers/hook_manager.php'); $this->registerClass('kScheduledTaskManager', KERNEL_PATH . '/managers/scheduled_task_manager.php'); $this->registerClass('kRequestManager', KERNEL_PATH . '/managers/request_manager.php'); + $this->registerClass('kSubscriptionManager', KERNEL_PATH . '/managers/subscription_manager.php'); $this->registerClass('kUrlManager', KERNEL_PATH . '/managers/url_manager.php'); $this->registerClass('kUrlProcessor', KERNEL_PATH . '/managers/url_processor.php'); Index: branches/5.2.x/core/kernel/managers/subscription_manager.php =================================================================== diff -u -N --- branches/5.2.x/core/kernel/managers/subscription_manager.php (revision 0) +++ branches/5.2.x/core/kernel/managers/subscription_manager.php (revision 15287) @@ -0,0 +1,204 @@ +subscriptions[] = new kSubscriptionItem($fields); + } + + /** + * Detects if current user is subscribed to new posts in given topic + * + * @return bool + * @access public + */ + public function subscribed() + { + foreach ($this->subscriptions as $subscription) { + if ( !$subscription->getSubscription()->isLoaded() ) { + /* @var $subscription kSubscriptionItem */ + + return false; + } + } + + return true; + } + + /** + * Subscribes current user to new posts in a given topic + * + * @return bool + * @access public + */ + public function subscribe() + { + foreach ($this->subscriptions as $subscription) { + if ( !$subscription->subscribe() ) { + /* @var $subscription kSubscriptionItem */ + + return false; + } + } + + return true; + } + + /** + * Unsubscribes current user from reciving e-mails about new posts in a gvein topic + * + * @return bool + * @access public + */ + public function unsubscribe() + { + foreach ($this->subscriptions as $subscription) { + if ( !$subscription->unsubscribe() ) { + /* @var $subscription kSubscriptionItem */ + + return false; + } + } + + return true; + } + + /** + * Returns e-mail event id or throws an exception, when such event not found + * + * @param string $email_event_name + * @param int $type + * @return string + * @throws Exception + * @access public + */ + public function getEmailEventId($email_event_name, $type = EmailEvent::EVENT_TYPE_FRONTEND) + { + $sql = 'SELECT EventId + FROM ' . TABLE_PREFIX . 'EmailEvents + WHERE Event = ' . $this->Conn->qstr($email_event_name) . ' AND Type = ' . $type; + $id = $this->Conn->GetOne($sql); + + if ( !$id ) { + throw new Exception('E-mail event "' . $email_event_name . '" not found'); + } + + return $id; + } +} + + +class kSubscriptionItem extends kBase { + + /** + * Fields set, that uniquely identifies subscription + * + * @var Array + * @access protected + */ + protected $fields = Array (); + + /** + * Creates new subscription item + * + * @param Array $fields + * @access public + */ + public function __construct($fields) + { + parent::__construct(); + + $this->fields = $fields; + } + + /** + * Returns user subscription + * + * @param bool $reload + * @return kBase|kDBItem + * @access public + */ + public function getSubscription($reload = false) + { + $special = kUtil::crc32(serialize($this->fields)); + + $subscription = $this->Application->recallObject('system-event-subscription.' . $special, null, Array ('skip_autoload' => true)); + /* @var $subscription kDBItem */ + + if ( !$subscription->isLoaded() || $reload ) { + $subscription->Load($this->fields); + } + + return $subscription; + } + + /** + * Subscribes user + * + * @return bool + * @access public + */ + public function subscribe() + { + $subscription = $this->getSubscription(); + + if ( $subscription->isLoaded() ) { + return true; + } + + $subscription->SetDBFieldsFromHash($this->fields); + + return $subscription->Create(); + } + + /** + * Unsubscribes user + * + * @return bool + * @access public + */ + public function unsubscribe() + { + $subscription = $this->getSubscription(); + + if ( !$subscription->isLoaded() ) { + return true; + } + + $temp_handler = $this->Application->recallObject($subscription->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + + $temp_handler->DeleteItems($subscription->Prefix, $subscription->Special, Array ($subscription->GetID())); + + return true; + } +} \ No newline at end of file