/** * Counts stars made by a given user. * * @param int $user_id * @return int */ public static function countUserStars($user_id = null) { if (!$user_id) { $user_id = sfPropelActAsStarredBehaviorToolkit::getUserId(); } if (!$user_id) { throw new sfException('Cannot retrieve current user.'); } $c = new Criteria(); $c->add(StarPeer::USER_ID, $user_id); return StarPeer::doCount($c); }
/** * Clear a star for a given object * * @param BaseObject $object **/ public function clearStar(BaseObject $object) { $user_id = sfPropelActAsStarredBehaviorToolkit::getUserId(); if (!$user_id) { throw new sfException('Cannot retrieve curent user.'); } $c = new Criteria(); $c->add(StarPeer::STARRED_ID, $object->getPrimaryKey()); $c->add(StarPeer::STARRED_MODEL, get_class($object)); $c->add(StarPeer::USER_ID, $user_id); StarPeer::doDelete($c); }
/** * Star a propel object. * */ public function executeStarit() { $this->model = $this->getRequestParameter('model'); $this->id = $this->getRequestParameter('id'); $object = sfPropelActAsStarredBehaviorToolkit::retrieveStarredObject($this->model, $this->id); if (!$object->isStarred()) { $object->setStar(); } else { $object->clearStar(); } if (!$this->getRequest()->isXmlHttpRequest()) { $referer = $this->getRequest()->getReferer(); $this->redirect($referer); } $this->object = $object; }
/** * Retrieve a starred object * * @param string $object_model * @param int $object_id * @return object */ public static function retrieveStarredObject($object_model, $object_id) { try { $peer = sprintf('%sPeer', $object_model); if (!class_exists($peer)) { throw new Exception(sprintf('Unable to load class %s', $peer)); } $object = call_user_func(array($peer, 'retrieveByPk'), $object_id); if (is_null($object)) { throw new Exception(sprintf('Unable to retrieve %s with primary key %s', $object_model, $object_id)); } if (!sfPropelActAsStarredBehaviorToolkit::isStarred($object)) { throw new Exception(sprintf('Class %s does not have the starred behavior', $object_model)); } return $object; } catch (Exception $e) { return sfContext::getInstance()->getLogger()->log($e->getMessage()); } }