/** * Checks if the user is subscribed to the * passed object. * * @param object $user * @param object $object */ public static function isSubscribed($user, $object) { $class = new ReflectionClass(get_class($object)); $type = strtolower($class->getShortName()); $sub = SubscriptionModel::select()->where('project_id = ?', $type == 'project' ? $object->id : $object->project_id)->andWhere('user_id = ?', $user->id)->andWhere('type = ?', $type)->andWhere('object_id = ?', $object->id)->fetch(); return $sub !== false; }
/** * Toggles the subscription. * * @param string $type Subscription type (Project, Milestone, Ticket) * @param integer $id Subscribed object ID */ public function action_toggle($type, $id) { switch ($type) { // Project case 'project': // Delete subscription if (is_subscribed($this->user, $this->project)) { $sub = Subscription::select()->where(array(array('project_id', $this->project->id), array('user_id', $this->user->id), array('type', 'project')))->exec()->fetch(); $sub->delete(); } else { $sub = new Subscription(array('type' => "project", 'project_id' => $this->project->id, 'user_id' => $this->user->id, 'object_id' => $this->project->id)); $sub->save(); } Request::redirectTo($this->project->href()); break; // Milestone // Milestone case 'milestone': // Get milestone $milestone = Milestone::select()->where(array(array('project_id', $this->project->id), array('slug', $id)))->exec()->fetch(); // Delete subscription if (is_subscribed($this->user, $milestone)) { $sub = Subscription::select()->where(array(array('project_id', $this->project->id), array('user_id', $this->user->id), array('type', 'milestone'), array('object_id', $milestone->id)))->exec()->fetch(); $sub->delete(); } else { $sub = new Subscription(array('type' => "milestone", 'project_id' => $this->project->id, 'user_id' => $this->user->id, 'object_id' => $milestone->id)); $sub->save(); } Request::redirectTo($milestone->href()); break; // Milestone // Milestone case 'ticket': // Get ticket $ticket = Ticket::select()->where(array(array('project_id', $this->project->id), array('ticket_id', $id)))->exec()->fetch(); // Delete subscription if (is_subscribed($this->user, $ticket)) { $sub = Subscription::select()->where(array(array('project_id', $this->project->id), array('user_id', $this->user->id), array('type', 'ticket'), array('object_id', $ticket->id)))->exec()->fetch(); $sub->delete(); } else { $sub = new Subscription(array('type' => "ticket", 'project_id' => $this->project->id, 'user_id' => $this->user->id, 'object_id' => $ticket->id)); $sub->save(); } Request::redirectTo($ticket->href()); break; } }
/** * Subscriptions page */ public function action_subscriptions() { $subscriptions = Subscription::select()->where('user_id', $this->user->id)->exec()->fetch_all(); View::set('subscriptions', $subscriptions); }