+ function get_available_offers($contact = null) {
+ $q = new OfferQuery;
+ $q->where("(valid_from is null or valid_from<=now())");
+ $q->where("(valid_to is null or valid_to>now())");
+ if (! is_null($contact)) {
+ $qq = new OfferStateQuery;
+ $qq->filterByContactId($contact->getId());
+ $ids = Array();
+ foreach ($qq->find() as $state) $ids[] = $state->getOfferId();
+ if (count($ids)) $q->_or()->filterById($ids);
+ }
+ return $q->orderById()->find();
+ }
+
+ function get_offer_state($contact, $offer) {
+ $q = new OfferStateQuery;
+ $q->filterByOfferId($offer->getId());
+ $q->filterByContactId($contact->getId());
+ return $q->orderByUpdated('desc')->findOne();
+ }
+