function watch($id = null) { if (is_null($id)) { $this->Session->setFlash('Repository not found'); $this->redirect('/'); } if (!$this->isLoggedIn()) { $this->redirect(array('controller' => 'login', 'action' => 'index')); } $user = $this->getConnectedUser(); $repo = $this->RepositoriesUser->find('first', array('conditions' => array('user_id' => $user['User']['id'], 'repository_id' => $id), 'recursive' => -1)); if (empty($repo)) { $this->Session->setFlash('Repository not found'); $this->redirect('/'); } $watching = $repo['RepositoriesUser']['watching']; $this->RepositoriesUser->read(null, $repo['RepositoriesUser']['id']); $this->RepositoriesUser->set('watching', !$watching); $this->RepositoriesUser->save(); if ($watching) { $msg = "Repository removed from watchlist"; } else { $msg = "Repository added to watchlist"; } $this->Session->setFlash($msg); $this->redirect(array('action' => 'set_repository_by_id', $id)); }